Skip to content

Commit 73eee20

Browse files
authored
User credential types consistently log token scopes (Azure#23712)
1 parent 4a2f743 commit 73eee20

File tree

6 files changed

+109
-7
lines changed

6 files changed

+109
-7
lines changed

sdk/azidentity/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88

99
### Bugs Fixed
10+
* User credential types inconsistently log access token scopes
1011

1112
### Other Changes
1213

sdk/azidentity/azidentity.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const (
4242
developerSignOnClientID = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
4343
defaultSuffix = "/.default"
4444

45+
scopeLogFmt = "%s.GetToken() acquired a token for scope %q"
46+
4547
traceNamespace = "Microsoft.Entra"
4648
traceOpGetToken = "GetToken"
4749
traceOpAuthenticate = "Authenticate"

sdk/azidentity/confidential_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (c *confidentialClient) GetToken(ctx context.Context, tro policy.TokenReque
115115
err = newAuthenticationFailedErrorFromMSAL(c.name, err)
116116
}
117117
} else {
118-
msg := fmt.Sprintf("%s.GetToken() acquired a token for scope %q", c.name, strings.Join(ar.GrantedScopes, ", "))
118+
msg := fmt.Sprintf(scopeLogFmt, c.name, strings.Join(ar.GrantedScopes, ", "))
119119
log.Write(EventAuthentication, msg)
120120
}
121121
return azcore.AccessToken{Token: ar.AccessToken, ExpiresOn: ar.ExpiresOn.UTC()}, err
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package azidentity
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
"testing"
10+
11+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
12+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
13+
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestConfidentialClientLogging(t *testing.T) {
18+
logMsgs := []string{}
19+
log.SetListener(func(e log.Event, msg string) {
20+
if e == EventAuthentication {
21+
logMsgs = append(logMsgs, msg)
22+
}
23+
})
24+
defer log.SetListener(nil)
25+
26+
cred, err := confidential.NewCredFromSecret(fakeSecret)
27+
require.NoError(t, err)
28+
29+
c, err := newConfidentialClient(fakeTenantID, fakeClientID, credNameSecret, cred, confidentialClientOptions{
30+
ClientOptions: azcore.ClientOptions{
31+
Transport: &mockSTS{},
32+
},
33+
})
34+
require.NoError(t, err)
35+
36+
// client should log token scopes when acquiring a token from the cache or authority
37+
expected := fmt.Sprintf(scopeLogFmt, credNameSecret, strings.Join(testTRO.Scopes, ", "))
38+
for i := 0; i < 2; i++ {
39+
logMsgs = []string{}
40+
_, err = c.GetToken(ctx, testTRO)
41+
require.NoError(t, err)
42+
43+
scopesLogged := false
44+
for _, msg := range logMsgs {
45+
require.Contains(t, msg, credNameSecret)
46+
if strings.Contains(msg, testTRO.Scopes[0]) {
47+
scopesLogged = true
48+
require.Equal(t, expected, msg)
49+
}
50+
}
51+
require.True(t, scopesLogged)
52+
}
53+
}

sdk/azidentity/public_client.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,7 @@ func (p *publicClient) GetToken(ctx context.Context, tro policy.TokenRequestOpti
154154
if p.opts.DisableAutomaticAuthentication {
155155
return azcore.AccessToken{}, newAuthenticationRequiredError(p.name, tro)
156156
}
157-
at, err := p.reqToken(ctx, client, tro)
158-
if err == nil {
159-
msg := fmt.Sprintf("%s.GetToken() acquired a token for scope %q", p.name, strings.Join(ar.GrantedScopes, ", "))
160-
log.Write(EventAuthentication, msg)
161-
}
162-
return at, err
157+
return p.reqToken(ctx, client, tro)
163158
}
164159

165160
// reqToken requests a token from the MSAL public client. It's separate from GetToken() to enable Authenticate() to bypass the cache.
@@ -242,6 +237,8 @@ func (p *publicClient) newMSALClient(enableCAE bool) (msalPublicClient, error) {
242237

243238
func (p *publicClient) token(ar public.AuthResult, err error) (azcore.AccessToken, error) {
244239
if err == nil {
240+
msg := fmt.Sprintf(scopeLogFmt, p.name, strings.Join(ar.GrantedScopes, ", "))
241+
log.Write(EventAuthentication, msg)
245242
p.record, err = newAuthenticationRecord(ar)
246243
} else {
247244
err = newAuthenticationFailedErrorFromMSAL(p.name, err)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package azidentity
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
"testing"
10+
11+
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
12+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestPublicClientLogging(t *testing.T) {
17+
logMsgs := []string{}
18+
log.SetListener(func(e log.Event, msg string) {
19+
if e == EventAuthentication {
20+
logMsgs = append(logMsgs, msg)
21+
}
22+
})
23+
defer log.SetListener(nil)
24+
25+
pc, err := newPublicClient(fakeTenantID, fakeClientID, credNameUserPassword, publicClientOptions{
26+
ClientOptions: azcore.ClientOptions{
27+
Transport: &mockSTS{},
28+
},
29+
})
30+
require.NoError(t, err)
31+
32+
// client should log token scopes when acquiring a token from the cache or authority
33+
expected := fmt.Sprintf(scopeLogFmt, credNameUserPassword, strings.Join(testTRO.Scopes, ", "))
34+
for i := 0; i < 2; i++ {
35+
logMsgs = []string{}
36+
_, err = pc.GetToken(ctx, testTRO)
37+
require.NoError(t, err)
38+
39+
scopesLogged := false
40+
for _, msg := range logMsgs {
41+
require.Contains(t, msg, credNameUserPassword)
42+
if strings.Contains(msg, testTRO.Scopes[0]) {
43+
scopesLogged = true
44+
require.Equal(t, expected, msg)
45+
}
46+
}
47+
require.True(t, scopesLogged)
48+
}
49+
}

0 commit comments

Comments
 (0)