Skip to content

Commit 9e2faae

Browse files
authored
add configurable IDMS API version for non-standard service implementations (#1207)
1 parent 9e3f221 commit 9e2faae

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

sdk/auth/auth.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,10 @@ func NewAuthorizerFromCredentials(ctx context.Context, c Credentials, api enviro
133133

134134
if c.EnableAuthenticatingUsingManagedIdentity {
135135
opts := ManagedIdentityAuthorizerOptions{
136-
Api: api,
137-
ClientId: c.ClientID,
138-
CustomManagedIdentityEndpoint: c.CustomManagedIdentityEndpoint,
136+
Api: api,
137+
ClientId: c.ClientID,
138+
CustomManagedIdentityEndpoint: c.CustomManagedIdentityEndpoint,
139+
CustomManagedIdentityAPIVersion: c.CustomManagedIdentityAPIVersion,
139140
}
140141
a, err := NewManagedIdentityAuthorizer(ctx, opts)
141142
if err != nil {

sdk/auth/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/hashicorp/go-azure-sdk/sdk/environments"
88
)
99

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

47+
// CustomManagedIdentityAPIVersion specifies the API version to use for IMDS.
48+
CustomManagedIdentityAPIVersion string
49+
4750
// Enables OIDC authentication (federated client credentials).
4851
EnableAuthenticationUsingOIDC bool
4952
// OIDCAssertionToken specifies the OIDC Assertion Token to authenticate using Client Credentials.

sdk/auth/managed_identity_authorizer.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ type ManagedIdentityAuthorizerOptions struct {
2828
// CustomManagedIdentityEndpoint is an optional endpoint from which to obtain an access
2929
// token. When blank, the default is used.
3030
CustomManagedIdentityEndpoint string
31+
32+
// CustomManagedIdentityAPIVersion is an optional API version to use when requesting a token.
33+
// This is required when using an endpoint that does not support the default API version such as Azure Container Apps.
34+
CustomManagedIdentityAPIVersion string
3135
}
3236

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

73-
url := fmt.Sprintf("%s?%s", a.conf.MsiEndpoint, query.Encode())
77+
u := fmt.Sprintf("%s?%s", a.conf.MsiEndpoint, query.Encode())
7478

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

136140
// newManagedIdentityConfig returns a new managedIdentityConfig with a configured metadata endpoint and resource.
137141
// clientId and objectId can be left blank when a single managed identity is available
138-
func newManagedIdentityConfig(resource, clientId, customManagedIdentityEndpoint string) (*managedIdentityConfig, error) {
142+
func newManagedIdentityConfig(resource, clientId, customManagedIdentityEndpoint string, customManagedIdentityAPIVersion string) (*managedIdentityConfig, error) {
139143
endpoint := msiDefaultEndpoint
140144
if customManagedIdentityEndpoint != "" {
141145
endpoint = customManagedIdentityEndpoint
142146
}
143147

148+
apiVersion := msiDefaultApiVersion
149+
if customManagedIdentityAPIVersion != "" {
150+
apiVersion = customManagedIdentityAPIVersion
151+
}
152+
144153
return &managedIdentityConfig{
145154
ClientID: clientId,
146155
Resource: resource,
147-
MsiApiVersion: msiDefaultApiVersion,
156+
MsiApiVersion: apiVersion,
148157
MsiEndpoint: endpoint,
149158
}, nil
150159
}

0 commit comments

Comments
 (0)