Skip to content

Commit 4836a97

Browse files
authored
feat: add AKS workload identity authentication (#522)
1 parent a5398c1 commit 4836a97

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

docs/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ More information on [how to authenticate to Azure using Managed Identity can be
140140

141141
---
142142

143+
When authenticating using AKS Managed Identity, the following fields can be set:
144+
145+
* `use_aks_workload_identity` - (Optional) Should AKS Workload Identity be used for Authentication? This can also be sourced from the `ARM_USE_AKS_WORKLOAD_IDENTITY` Environment Variable. Defaults to `false`. When set, `client_id`, `tenant_id` and `oidc_token_file_path` will be detected from the environment and do not need to be specified. Defaults to `false`.
146+
147+
---
148+
143149
For Azure CLI authentication, the following fields can be set:
144150

145151
* `use_cli` - (Optional) Should Azure CLI be used for authentication? This can also be sourced from the `ARM_USE_CLI` environment variable. Defaults to `true`.

internal/provider/provider.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type providerData struct {
5959
UseOIDC types.Bool `tfsdk:"use_oidc"`
6060
UseCLI types.Bool `tfsdk:"use_cli"`
6161
UseMSI types.Bool `tfsdk:"use_msi"`
62+
UseAKSWorkloadIdentity types.Bool `tfsdk:"use_aks_workload_identity"`
6263
PartnerID types.String `tfsdk:"partner_id"`
6364
CustomCorrelationRequestID types.String `tfsdk:"custom_correlation_request_id"`
6465
DisableCorrelationRequestID types.Bool `tfsdk:"disable_correlation_request_id"`
@@ -117,6 +118,18 @@ func (model providerData) GetClientSecret() (*string, error) {
117118
return &clientSecret, nil
118119
}
119120

121+
func (model providerData) GetOIDCTokenFilePath() string {
122+
if !model.OIDCTokenFilePath.IsNull() && model.OIDCTokenFilePath.ValueString() != "" {
123+
return model.OIDCTokenFilePath.ValueString()
124+
}
125+
126+
if model.UseAKSWorkloadIdentity.ValueBool() && os.Getenv("AZURE_FEDERATED_TOKEN_FILE") != "" {
127+
return os.Getenv("AZURE_FEDERATED_TOKEN_FILE")
128+
}
129+
130+
return ""
131+
}
132+
120133
type providerEndpointData struct {
121134
ActiveDirectoryAuthorityHost types.String `tfsdk:"active_directory_authority_host"`
122135
ResourceManagerEndpoint types.String `tfsdk:"resource_manager_endpoint"`
@@ -267,6 +280,11 @@ func (p Provider) Schema(ctx context.Context, request provider.SchemaRequest, re
267280
Description: "Allow Managed Service Identity to be used for Authentication.",
268281
},
269282

283+
"use_aks_workload_identity": schema.BoolAttribute{
284+
Optional: true,
285+
Description: "Should AKS Workload Identity be used for Authentication? This can also be sourced from the `ARM_USE_AKS_WORKLOAD_IDENTITY` Environment Variable. Defaults to `false`. When set, `client_id`, `tenant_id` and `oidc_token_file_path` will be detected from the environment and do not need to be specified.",
286+
},
287+
270288
// TODO@mgd: azidentity doesn't support msi_endpoint
271289
// "msi_endpoint": {
272290
// Type: schema.TypeString,
@@ -483,6 +501,14 @@ func (p Provider) Configure(ctx context.Context, request provider.ConfigureReque
483501
}
484502
}
485503

504+
if model.UseAKSWorkloadIdentity.IsNull() {
505+
if v := os.Getenv("ARM_USE_AKS_WORKLOAD_IDENTITY"); v != "" {
506+
model.UseAKSWorkloadIdentity = types.BoolValue(v == "true")
507+
} else {
508+
model.UseAKSWorkloadIdentity = types.BoolValue(false)
509+
}
510+
}
511+
486512
if model.UseCLI.IsNull() {
487513
if v := os.Getenv("ARM_USE_CLI"); v != "" {
488514
model.UseCLI = types.BoolValue(v == "true")
@@ -696,8 +722,8 @@ func buildChainedTokenCredential(model providerData, options azidentity.DefaultA
696722
log.Printf("[DEBUG] building chained token credential")
697723
var creds []azcore.TokenCredential
698724

699-
if model.UseOIDC.ValueBool() {
700-
log.Printf("[DEBUG] oidc credential enabled")
725+
if model.UseOIDC.ValueBool() || model.UseAKSWorkloadIdentity.ValueBool() {
726+
log.Printf("[DEBUG] oidc credential or AKS Workload Identity enabled")
701727
if cred, err := buildOidcCredential(model, options); err == nil {
702728
creds = append(creds, cred)
703729
} else {
@@ -816,7 +842,7 @@ func buildOidcCredential(model providerData, options azidentity.DefaultAzureCred
816842
RequestToken: model.OIDCRequestToken.ValueString(),
817843
RequestUrl: model.OIDCRequestURL.ValueString(),
818844
Token: model.OIDCToken.ValueString(),
819-
TokenFilePath: model.OIDCTokenFilePath.ValueString(),
845+
TokenFilePath: model.GetOIDCTokenFilePath(),
820846
}
821847
return NewOidcCredential(o)
822848
}

0 commit comments

Comments
 (0)