Skip to content

Commit a6bfa94

Browse files
chlowelljhendrixMSFT
authored andcommitted
ARM bearer auth policy opts in to CAE (Azure#21367)
1 parent e98678a commit a6bfa94

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

sdk/azcore/arm/runtime/policy_bearer_token.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ type acquiringResourceState struct {
2828
tenant string
2929
}
3030

31-
// acquire acquires or updates the resource; only one
32-
// thread/goroutine at a time ever calls this function
33-
func acquire(state acquiringResourceState) (newResource azcore.AccessToken, newExpiration time.Time, err error) {
31+
// acquireAuxToken acquires a token from an auxiliary tenant. Only one thread/goroutine at a time ever calls this function.
32+
func acquireAuxToken(state acquiringResourceState) (newResource azcore.AccessToken, newExpiration time.Time, err error) {
3433
tk, err := state.p.cred.GetToken(state.ctx, azpolicy.TokenRequestOptions{
35-
Scopes: state.p.scopes,
36-
TenantID: state.tenant,
34+
EnableCAE: true,
35+
Scopes: state.p.scopes,
36+
TenantID: state.tenant,
3737
})
3838
if err != nil {
3939
return azcore.AccessToken{}, time.Time{}, err
@@ -59,7 +59,7 @@ func NewBearerTokenPolicy(cred azcore.TokenCredential, opts *armpolicy.BearerTok
5959
p := &BearerTokenPolicy{cred: cred}
6060
p.auxResources = make(map[string]*temporal.Resource[azcore.AccessToken, acquiringResourceState], len(opts.AuxiliaryTenants))
6161
for _, t := range opts.AuxiliaryTenants {
62-
p.auxResources[t] = temporal.NewResource(acquire)
62+
p.auxResources[t] = temporal.NewResource(acquireAuxToken)
6363
}
6464
p.scopes = make([]string, len(opts.Scopes))
6565
copy(p.scopes, opts.Scopes)
@@ -80,7 +80,7 @@ func (b *BearerTokenPolicy) onChallenge(req *azpolicy.Request, res *http.Respons
8080
return err
8181
} else if claims != "" {
8282
// request a new token having the specified claims, send the request again
83-
return authNZ(azpolicy.TokenRequestOptions{Claims: claims, Scopes: b.scopes})
83+
return authNZ(azpolicy.TokenRequestOptions{Claims: claims, EnableCAE: true, Scopes: b.scopes})
8484
}
8585
// auth challenge didn't include claims, so this is a simple authorization failure
8686
return azruntime.NewResponseError(res)
@@ -89,7 +89,7 @@ func (b *BearerTokenPolicy) onChallenge(req *azpolicy.Request, res *http.Respons
8989
// onRequest authorizes requests with one or more bearer tokens
9090
func (b *BearerTokenPolicy) onRequest(req *azpolicy.Request, authNZ func(azpolicy.TokenRequestOptions) error) error {
9191
// authorize the request with a token for the primary tenant
92-
err := authNZ(azpolicy.TokenRequestOptions{Scopes: b.scopes})
92+
err := authNZ(azpolicy.TokenRequestOptions{EnableCAE: true, Scopes: b.scopes})
9393
if err != nil || len(b.auxResources) == 0 {
9494
return err
9595
}

sdk/azcore/arm/runtime/policy_bearer_token_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type mockCredential struct {
3434
}
3535

3636
func (mc mockCredential) GetToken(ctx context.Context, options azpolicy.TokenRequestOptions) (azcore.AccessToken, error) {
37+
if !options.EnableCAE {
38+
return azcore.AccessToken{}, errors.New("ARM clients should set EnableCAE to true")
39+
}
3740
if mc.getTokenImpl != nil {
3841
return mc.getTokenImpl(ctx, options)
3942
}

0 commit comments

Comments
 (0)