Skip to content

sdk - add configurable IMDS API version for non-standard service implementations #1207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sdk/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ func NewAuthorizerFromCredentials(ctx context.Context, c Credentials, api enviro

if c.EnableAuthenticatingUsingManagedIdentity {
opts := ManagedIdentityAuthorizerOptions{
Api: api,
ClientId: c.ClientID,
CustomManagedIdentityEndpoint: c.CustomManagedIdentityEndpoint,
Api: api,
ClientId: c.ClientID,
CustomManagedIdentityEndpoint: c.CustomManagedIdentityEndpoint,
CustomManagedIdentityAPIVersion: c.CustomManagedIdentityAPIVersion,
}
a, err := NewManagedIdentityAuthorizer(ctx, opts)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion sdk/auth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/hashicorp/go-azure-sdk/sdk/environments"
)

// Credentials sets up NewAuthorizer to return an Authorizer based on the provided credentails.
// Credentials sets up NewAuthorizer to return an Authorizer based on the provided credentials.
type Credentials struct {
// Specifies the national cloud environment to use
Environment environments.Environment
Expand Down Expand Up @@ -44,6 +44,9 @@ type Credentials struct {
// CustomManagedIdentityEndpoint specifies a custom endpoint which should be used for Managed Identity.
CustomManagedIdentityEndpoint string

// CustomManagedIdentityAPIVersion specifies the API version to use for IMDS.
CustomManagedIdentityAPIVersion string

// Enables OIDC authentication (federated client credentials).
EnableAuthenticationUsingOIDC bool
// OIDCAssertionToken specifies the OIDC Assertion Token to authenticate using Client Credentials.
Expand Down
19 changes: 14 additions & 5 deletions sdk/auth/managed_identity_authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ type ManagedIdentityAuthorizerOptions struct {
// CustomManagedIdentityEndpoint is an optional endpoint from which to obtain an access
// token. When blank, the default is used.
CustomManagedIdentityEndpoint string

// CustomManagedIdentityAPIVersion is an optional API version to use when requesting a token.
// This is required when using an endpoint that does not support the default API version such as Azure Container Apps.
CustomManagedIdentityAPIVersion string
}

// NewManagedIdentityAuthorizer returns an authorizer using a Managed Identity for authentication.
Expand All @@ -36,7 +40,7 @@ func NewManagedIdentityAuthorizer(ctx context.Context, options ManagedIdentityAu
if err != nil {
return nil, fmt.Errorf("determining resource for api %q: %+v", options.Api.Name(), err)
}
conf, err := newManagedIdentityConfig(*resource, options.ClientId, options.CustomManagedIdentityEndpoint)
conf, err := newManagedIdentityConfig(*resource, options.ClientId, options.CustomManagedIdentityEndpoint, options.CustomManagedIdentityAPIVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -70,9 +74,9 @@ func (a *ManagedIdentityAuthorizer) Token(ctx context.Context, _ *http.Request)
query["client_id"] = []string{a.conf.ClientID}
}

url := fmt.Sprintf("%s?%s", a.conf.MsiEndpoint, query.Encode())
u := fmt.Sprintf("%s?%s", a.conf.MsiEndpoint, query.Encode())

body, err := azureMetadata(ctx, url)
body, err := azureMetadata(ctx, u)
if err != nil {
return nil, fmt.Errorf("ManagedIdentityAuthorizer: failed to request token from metadata endpoint: %v", err)
}
Expand Down Expand Up @@ -135,16 +139,21 @@ type managedIdentityConfig struct {

// newManagedIdentityConfig returns a new managedIdentityConfig with a configured metadata endpoint and resource.
// clientId and objectId can be left blank when a single managed identity is available
func newManagedIdentityConfig(resource, clientId, customManagedIdentityEndpoint string) (*managedIdentityConfig, error) {
func newManagedIdentityConfig(resource, clientId, customManagedIdentityEndpoint string, customManagedIdentityAPIVersion string) (*managedIdentityConfig, error) {
endpoint := msiDefaultEndpoint
if customManagedIdentityEndpoint != "" {
endpoint = customManagedIdentityEndpoint
}

apiVersion := msiDefaultApiVersion
if customManagedIdentityAPIVersion != "" {
apiVersion = customManagedIdentityAPIVersion
}

return &managedIdentityConfig{
ClientID: clientId,
Resource: resource,
MsiApiVersion: msiDefaultApiVersion,
MsiApiVersion: apiVersion,
MsiEndpoint: endpoint,
}, nil
}
Expand Down
Loading