Skip to content

Commit 3c75f44

Browse files
committed
Add security warning for token usage with Terraform state file
1 parent c35b42d commit 3c75f44

File tree

1 file changed

+20
-10
lines changed
  • website/blog/2025-12-16-deploy-aks-automatic-terraform-helm

1 file changed

+20
-10
lines changed

website/blog/2025-12-16-deploy-aks-automatic-terraform-helm/index.md

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ resource "azurerm_resource_group" "this" {
8787
}
8888
8989
resource "azapi_resource" "this" {
90-
type = "Microsoft.ContainerService/managedClusters@2025-10-02-preview"
90+
type = "Microsoft.ContainerService/managedClusters@2025-10-01"
9191
parent_id = azurerm_resource_group.this.id
9292
location = azurerm_resource_group.this.location
9393
name = "aks-${random_pet.this.id}"
@@ -145,6 +145,8 @@ With that context, let's explore the two viable options for configuring the Helm
145145

146146
Both options require retrieving the connection details—the host URL and cluster CA certificate—from the AKS cluster. The AzApi provider doesn't expose these values directly; however, you can use the `azurerm_kubernetes_cluster` data source as a workaround.
147147

148+
:::
149+
148150
Add the following to the bottom of your `main.tf` file:
149151

150152
```hcl
@@ -154,13 +156,11 @@ data "azurerm_kubernetes_cluster" "this" {
154156
}
155157
```
156158

157-
:::
158-
159159
### Option 1: Configure the Helm provider with Azure bearer token authentication
160160

161161
The Helm provider's [`kubernetes` block](https://registry.terraform.io/providers/hashicorp/helm/latest/docs#kubernetes-1) supports a [`token` argument](https://registry.terraform.io/providers/hashicorp/helm/latest/docs#token-1) that lets you supply a bearer token directly for authentication.
162162

163-
You can obtain access tokens using the Azure CLI. If you're authenticated with `az login`, you can get a token for the AKS resource like this:
163+
You can obtain short-lived access tokens using the Azure CLI. If you're authenticated with `az login`, you can get a token for the AKS resource like this:
164164

165165
```bash
166166
az account get-access-token --resource 6dae42f8-4368-4678-94ff-3960e28e3630
@@ -172,7 +172,7 @@ The resource ID `6dae42f8-4368-4678-94ff-3960e28e3630` is the well-known applica
172172

173173
:::
174174

175-
You can use the [`external` provider](https://registry.terraform.io/providers/hashicorp/external/latest/docs) in Terraform to run this command and capture the token as an external data source.
175+
Combining the above command with the [`external` data source](https://developer.hashicorp.com/terraform/language/data-sources/external) in Terraform allows you to retrieve the token dynamically and use it in the Helm provider configuration.
176176

177177
Add the following to your `main.tf` file:
178178

@@ -182,9 +182,7 @@ data "external" "this" {
182182
}
183183
```
184184

185-
This command runs the Azure CLI and extracts the access token in JSON format, which you can then use in the Helm provider configuration.
186-
187-
Now you have everything needed to configure the Helm provider:
185+
You can now reference the token in the Helm provider configuration like this:
188186

189187
```hcl
190188
provider "helm" {
@@ -198,12 +196,24 @@ provider "helm" {
198196

199197
This configuration uses the host and cluster CA certificate from the AKS cluster data source and gains access to the cluster using the bearer token from the external data source.
200198

201-
If you are solely using Azure CLI authentication (e.g., `az login`), this approach works well. However, if you're deploying from a CI/CD pipeline or using service principals or managed identities, the next option is more flexible.
199+
:::danger
200+
201+
Terraform stores the access token in plain text in the state file. Although the token expires after about one hour, this poses a security risk in shared environments or CI/CD pipelines. For production use, secure your state file appropriately or use Option 2 instead.
202+
203+
:::
204+
205+
For quick local demos, this approach is convenient. For CI/CD pipelines or service principal authentication, the next option is more flexible.
202206

203207
### Option 2: Configure the Helm provider to use the exec plugin with kubelogin
204208

205209
The Helm provider also supports using the `exec` plugin mechanism to obtain credentials dynamically. This approach is more flexible and works well with various authentication methods supported by [kubelogin](https://azure.github.io/kubelogin/index.html) which is a [Kubernetes client-go credential plugin](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins) for Azure.
206210

211+
:::tip
212+
213+
This is a more ideal approach because it does not require storing tokens in the Terraform state file. Instead, the Helm provider invokes the specified command to obtain fresh credentials each time it needs to authenticate.
214+
215+
:::
216+
207217
To use this approach, add the following to your `main.tf`:
208218

209219
```hcl
@@ -220,7 +230,7 @@ provider "helm" {
220230
"--login",
221231
"azurecli",
222232
"--server-id",
223-
"6dae42f8-4368-4678-94ff-3960e28e3630" # Azure Kubernetes Service AAD Server
233+
"6dae42f8-4368-4678-94ff-3960e28e3630"
224234
]
225235
}
226236
}

0 commit comments

Comments
 (0)