Skip to content

Commit 0c7af20

Browse files
committed
Non-tested support for azure challenge 11
1 parent 06b7bec commit 0c7af20

File tree

8 files changed

+54
-23
lines changed

8 files changed

+54
-23
lines changed

azure/iam.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ resource "azurerm_role_assignment" "aks_identity_operator" {
1818
principal_id = azurerm_kubernetes_cluster.cluster.kubelet_identity[0].object_id
1919
}
2020

21+
resource "azurerm_role_assignment" "aks_extra_identity_operator" {
22+
scope = azurerm_user_assigned_identity.aks_extra_pod_identity.id
23+
role_definition_name = "Managed Identity Operator"
24+
principal_id = azurerm_kubernetes_cluster.cluster.kubelet_identity[0].object_id
25+
}
26+
2127
resource "azurerm_role_assignment" "aks_vm_contributor" {
2228
scope = "/subscriptions/${data.azurerm_client_config.current.subscription_id}/resourcegroups/${azurerm_kubernetes_cluster.cluster.node_resource_group}"
2329
role_definition_name = "Virtual Machine Contributor"

azure/k8s-vault-azure-start.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ checkCommandsAvailable helm minikube jq vault sed grep docker grep cat az envsub
1919
echo "This is a script to bootstrap the configuration. You need to have installed: helm, kubectl, jq, vault, grep, cat, sed, envsubst, and azure cli, and is only tested on mac, Debian and Ubuntu"
2020
echo "This script is based on the steps defined in https://learn.hashicorp.com/tutorials/vault/kubernetes-minikube. Vault is awesome!"
2121

22+
# Most of the variables below are used in envsubst later.
2223
export AZURE_SUBSCRIPTION_ID="$(az account show --query id --output tsv)"
2324
export AZURE_TENANT_ID="$(az account show --query tenantId --output tsv)"
2425

@@ -31,6 +32,10 @@ export IDENTITY_NAME="wrongsecrets-identity"
3132

3233
export AZ_POD_RESOURCE_ID="$(terraform output -raw aad_pod_identity_resource_id)"
3334
export AZ_POD_CLIENT_ID="$(terraform output -raw aad_pod_identity_client_id)"
35+
36+
export AZ_EXTRA_POD_RESOURCE_ID="$(terraform output -raw aad_extra_pod_identity_resource_id)"
37+
export AZ_EXTRA_POD_CLIENT_ID="$(terraform output -raw aad_extra_pod_identity_client_id)"
38+
3439
export AZ_VAULT_URI="$(terraform output -raw vault_uri)"
3540
export AZ_KEY_VAULT_TENANT_ID="$(terraform output -raw tenant_id)"
3641
export AZ_KEY_VAULT_NAME="$(terraform output -raw vault_name)"

azure/k8s/pod-id.yml.tpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,20 @@ metadata:
1414
spec:
1515
azureIdentity: wrongsecrets-pod-id
1616
selector: wrongsecrets-pod-id
17+
---
18+
apiVersion: "aadpodidentity.k8s.io/v1"
19+
kind: AzureIdentity
20+
metadata:
21+
name: separate-workload-pod-id
22+
spec:
23+
type: 0
24+
resourceID: ${AZ_EXTRA_POD_RESOURCE_ID}
25+
clientID: ${AZ_EXTRA_POD_CLIENT_ID}
26+
---
27+
apiVersion: "aadpodidentity.k8s.io/v1"
28+
kind: AzureIdentityBinding
29+
metadata:
30+
name: wrongsecrets-extra-podid-binding
31+
spec:
32+
azureIdentity: separate-workload-pod-id
33+
selector: separate-workload-pod-id

azure/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ output "aad_pod_identity_client_id" {
2323
description = "Client ID for the Managed Identity for AAD Pod Identity"
2424
}
2525

26+
output "aad_extra_pod_identity_resource_id" {
27+
value = azurerm_user_assigned_identity.aks_extra_pod_identity.id
28+
description = "Resource ID for the Managed Identity for AAD Pod Identity"
29+
}
30+
31+
output "aad_extra_pod_identity_client_id" {
32+
value = azurerm_user_assigned_identity.aks_extra_pod_identity.client_id
33+
description = "Client ID for the Managed Identity for AAD Pod Identity"
34+
}
35+
2636
output "vault_uri" {
2737
value = azurerm_key_vault.vault.vault_uri
2838
description = "Vault URI"

azure/secrets.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#########################
44

55
resource "random_integer" "suffix" {
6-
min = 11111
6+
min = 00000
77
max = 99999
88
}
99

@@ -92,6 +92,8 @@ resource "azurerm_key_vault_secret" "wrongsecret_3" {
9292
]
9393
}
9494

95+
# With Azure key vault, you grant access per vault instead of per secret. Below is a bad idea if these workloads should
96+
# be separated
9597
resource "azurerm_key_vault_access_policy" "extra_identity_access" {
9698
key_vault_id = azurerm_key_vault.vault.id
9799
tenant_id = data.azurerm_client_config.current.tenant_id
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
=== Azure Key Vault part 3
22

3-
In our GKE cluster, there's a service account for workloads, which maps to a service account in kubernetes.
3+
In our AKS cluster, there are two Azure Identity bindings. These are used by Azure to inject metadata. In our case, we use these to access Azure Key Vault.
44

5-
Unfortunately the developers used only one GCP service account to bind to multiple K8s service accounts. This means a different pod can access our secrets...
5+
The interesting thing about key vault is that it sets permissions per vault, rather than per secret. The developers unfortunately only have one key vault for two identities...
66

7-
Try provisioning `wrongsecret-3` with a pod with the `default` service account in the default namespace.
8-
9-
10-
Hint: You could use a job with the image `gcr.io/google.com/cloudsdktool/cloud-sdk:latest` and use the `gcloud` cli.
11-
See https://cloud.google.com/secret-manager/docs/creating-and-accessing-secrets#a_note_on_resource_consistency[accessing a secret] for some help.
7+
Hint: There's a binding to `separate-workload-pod-id`. Try creating a pod with the right labels to steal wrongsecret-3 (see also[https://docs.microsoft.com/en-us/azure/aks/use-azure-ad-pod-identity#run-a-sample-application]).
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
You can solve this challenge by the following steps:
22

3-
- Create a new job file `job.yaml` with the following content:
3+
- Check your vault name with `terraform output vault_name`.
4+
5+
- Create a new pod file `pod.yaml` with the following content (replace the vault name with output from the previous step):
46
57
```yaml
68
apiVersion: batch/v1
79
kind: Job
810
metadata:
911
name: wrongsecret-3
12+
labels:
13+
aadpodidbinding: separate-workload-pod-id
1014
spec:
1115
template:
1216
spec:
13-
serviceAccountName: default
1417
containers:
15-
- name: gcloud
16-
image: gcr.io/google.com/cloudsdktool/cloud-sdk:latest
17-
command:
18-
[
19-
"gcloud",
20-
"secrets",
21-
"versions",
22-
"access",
23-
"latest",
24-
"--secret=wrongsecret-3",
25-
]
18+
- name: az
19+
image: mcr.microsoft.com/azure-cli:latest
20+
command: ["az", "keyvault", "secret", "show", "--name=wrongsecret-3", "--vault-name=YOUR_VAULT_NAME_HERE"]
2621
restartPolicy: Never
2722
```
2823

29-
This job executes the command `gcloud secrets versions access latest --secret=wrongsecret-3`. Since the job can access the same service account as the secret challenge pod, it can access GCP with the same privileges.
24+
This job executes something like the command `az keyvault secret show --name wrongsecret-3 --vault-name wrongsecrets-vault-00000`. Since the job can access the same vault as the secret challenge pod, it has access to its secrets.
3025

3126
- Apply the job with `kubectl apply -f job.yaml`
3227
- Check the job's logs with `kubectl logs job/wrongsecret-3`. This should show you the solution :)

src/main/resources/explanations/challenge11-azure_reason.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ Secrets management is more than secure storage:
22

33
As you can tell by now: there are many ways to get to a secret: whether hardcoded, stored in a misconfigured third party solution, or stored correctly, but with the wrong IAM access rights in accounts next to it. You will, by now see, why we say that "your security maturity reflects in your secrets management".
44

5-
In this specific case, it would be a bad idea to have this kind of production access without triggering some "break glass" procedure and appropriate alarms. Additionally, anything that explicitly logs secrets is dangerous. If you spot any code logging secrets, flag it!
5+
In this specific case two separate workloads were able to access each other's secrets. This could have been prevented by creating different key vaults for different purposes.

0 commit comments

Comments
 (0)