-
Notifications
You must be signed in to change notification settings - Fork 170
/
manager.go
1019 lines (857 loc) · 31.5 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package auth
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
azcloud "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/public"
"github.com/azure/azure-dev/cli/azd/internal/tracing"
"github.com/azure/azure-dev/cli/azd/internal/tracing/fields"
"github.com/azure/azure-dev/cli/azd/pkg/cloud"
"github.com/azure/azure-dev/cli/azd/pkg/config"
"github.com/azure/azure-dev/cli/azd/pkg/github"
"github.com/azure/azure-dev/cli/azd/pkg/httputil"
"github.com/azure/azure-dev/cli/azd/pkg/input"
"github.com/azure/azure-dev/cli/azd/pkg/oneauth"
"github.com/azure/azure-dev/cli/azd/pkg/osutil"
"github.com/azure/azure-dev/cli/azd/pkg/output"
"github.com/azure/azure-dev/cli/azd/pkg/output/ux"
"github.com/cli/browser"
)
// TODO(azure/azure-dev#710): Right now, we re-use the App Id of the `az` CLI, until we have our own.
//
// nolint:lll
// https://github.com/Azure/azure-cli/blob/azure-cli-2.41.0/src/azure-cli-core/azure/cli/core/auth/identity.py#L23
const cAZD_CLIENT_ID = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
// cCurrentUserKey is the key we use in config for the storing identity information of the currently logged in user.
const cCurrentUserKey = "auth.account.currentUser"
// cUseAzCli is the key we use in config to denote that we want to use the az CLI for authentication instead of managing
// it ourselves. The value should be a string as specified by [strconv.ParseBool].
const cUseAzCliAuthKey = "auth.useAzCliAuth"
// cAuthConfigFileName is the name of the file we store in the user configuration directory which is used to persist
// auth related configuration information (e.g. the home account id of the current user). This information is not secret.
const cAuthConfigFileName = "auth.json"
const cUseCloudShellAuthEnvVar = "AZD_IN_CLOUDSHELL"
// HttpClient interface as required by MSAL library.
type HttpClient interface {
httputil.HttpClient
// CloseIdleConnections closes any idle connections in a "keep-alive" state.
CloseIdleConnections()
}
// Manager manages the authentication system of azd. It allows a user to log in, either as a user principal or service
// principal. Manager stores information so that the user can stay logged in across invocations of the CLI. When logged in
// as a user (either interactively or via a device code flow), we provide a durable cache to MSAL which is used to cache
// information to allow silent logins across process runs. This cache is stored inside the user's home directory, ACL'd such
// that it can only be read by the current user. In addition, on Windows, this cache is encrypted, using CryptProtectData.
// The home account id of the signed in user is stored as a property under [cCurrentUserKey]. This behavior matches the
// AZ CLI.
//
// When logged in as a service principal, the same cache strategy that backed the MSAL cache is used to store the private
// key or secret and the public components (the client ID and tenant ID) are stored under [cCurrentUserKey].
//
// Logging out removes this cached authentication data.
//
// You can configure azd to ignore its native credential system and instead delegate to AZ CLI (useful for cases where azd
// does not yet support your preferred method of authentication by setting [cUseLegacyAzCliAuthKey] in config to true.
type Manager struct {
publicClient publicClient
publicClientOptions []public.Option
cloud *cloud.Cloud
configManager config.FileConfigManager
userConfigManager config.UserConfigManager
credentialCache Cache
ghClient *github.FederatedTokenClient
httpClient HttpClient
console input.Console
externalAuthCfg ExternalAuthConfiguration
}
type ExternalAuthConfiguration struct {
Endpoint string
Key string
Client httputil.HttpClient
}
func NewManager(
configManager config.FileConfigManager,
userConfigManager config.UserConfigManager,
cloud *cloud.Cloud,
httpClient HttpClient,
console input.Console,
externalAuthCfg ExternalAuthConfiguration,
) (*Manager, error) {
cfgRoot, err := config.GetUserConfigDir()
if err != nil {
return nil, fmt.Errorf("getting config dir: %w", err)
}
authRoot := filepath.Join(cfgRoot, "auth")
if err := os.MkdirAll(authRoot, osutil.PermissionDirectoryOwnerOnly); err != nil {
return nil, fmt.Errorf("creating auth root: %w", err)
}
cacheRoot := filepath.Join(authRoot, "msal")
if err := os.MkdirAll(cacheRoot, osutil.PermissionDirectoryOwnerOnly); err != nil {
return nil, fmt.Errorf("creating msal cache root: %w", err)
}
authorityUrl, err := url.JoinPath(cloud.Configuration.ActiveDirectoryAuthorityHost, "organizations")
if err != nil {
return nil, fmt.Errorf("joining authority url: %w", err)
}
options := []public.Option{
public.WithCache(newCache(cacheRoot)),
public.WithAuthority(authorityUrl),
public.WithHTTPClient(httpClient),
}
publicClientApp, err := public.New(cAZD_CLIENT_ID, options...)
if err != nil {
return nil, fmt.Errorf("creating msal client: %w", err)
}
ghClient := github.NewFederatedTokenClient(nil)
return &Manager{
publicClient: &msalPublicClientAdapter{client: &publicClientApp},
publicClientOptions: options,
cloud: cloud,
configManager: configManager,
userConfigManager: userConfigManager,
credentialCache: newCredentialCache(authRoot),
ghClient: ghClient,
httpClient: httpClient,
console: console,
externalAuthCfg: externalAuthCfg,
}, nil
}
// LoginScopes returns the scopes that we request an access token for when checking if a user is signed in.
func LoginScopes(cloud *cloud.Cloud) []string {
resourceManagerUrl := cloud.Configuration.Services[azcloud.ResourceManager].Endpoint
return []string{
fmt.Sprintf("%s//.default", resourceManagerUrl),
}
}
func (m *Manager) LoginScopes() []string {
return LoginScopes(m.cloud)
}
func loginScopesMap(cloud *cloud.Cloud) map[string]struct{} {
resourceManagerUrl := cloud.Configuration.Services[azcloud.ResourceManager].Endpoint
return map[string]struct{}{resourceManagerUrl: {}}
}
// EnsureLoggedInCredential uses the credential's GetToken method to ensure an access token can be fetched.
// On success, the token we fetched is returned.
func EnsureLoggedInCredential(
ctx context.Context,
credential azcore.TokenCredential,
cloud *cloud.Cloud,
) (*azcore.AccessToken, error) {
token, err := credential.GetToken(ctx, policy.TokenRequestOptions{
Scopes: LoginScopes(cloud),
})
if err != nil {
return &azcore.AccessToken{}, err
}
return &token, nil
}
// CredentialForCurrentUser returns a TokenCredential instance for the current user. If `auth.useLegacyAzCliAuth` is set to
// a truthy value in config, an instance of azidentity.AzureCLICredential is returned instead. To accept the default options,
// pass nil.
func (m *Manager) CredentialForCurrentUser(
ctx context.Context,
options *CredentialForCurrentUserOptions,
) (azcore.TokenCredential, error) {
if options == nil {
options = &CredentialForCurrentUserOptions{}
}
if m.UseExternalAuth() {
log.Printf("delegating auth to external process")
return newRemoteCredential(
m.externalAuthCfg.Endpoint,
m.externalAuthCfg.Key,
options.TenantID,
m.externalAuthCfg.Client), nil
}
userConfig, err := m.userConfigManager.Load()
if err != nil {
return nil, fmt.Errorf("fetching current user: %w", err)
}
if shouldUseLegacyAuth(userConfig) {
log.Printf("delegating auth to az since %s is set to true", cUseAzCliAuthKey)
cred, err := azidentity.NewAzureCLICredential(&azidentity.AzureCLICredentialOptions{
TenantID: options.TenantID,
})
if err != nil {
return nil, fmt.Errorf("failed to create credential: %w: %w", err, ErrNoCurrentUser)
}
return cred, nil
}
authConfig, err := m.readAuthConfig()
if err != nil {
return nil, fmt.Errorf("reading auth config: %w", err)
}
currentUser, err := readUserProperties(authConfig)
if errors.Is(err, ErrNoCurrentUser) {
// User is not logged in, not using az credentials, try CloudShell if possible
if ShouldUseCloudShellAuth() {
cloudShellCredential, err := m.newCredentialFromCloudShell()
if err != nil {
return nil, err
}
return cloudShellCredential, nil
}
if oneauth.Supported && strings.EqualFold(os.Getenv("IsDevBox"), "True") {
// Try logging in the active OS account. If that fails for any reason, tell the user to run `azd auth login`.
if err := m.LoginWithBrokerAccount(); err == nil {
if config, err := m.readAuthConfig(); err == nil {
user, err := readUserProperties(config)
if err == nil && user != nil && user.HomeAccountID != nil && *user.HomeAccountID != "" {
tenant := options.TenantID
if tenant == "" {
tenant = "organizations"
}
authority := m.cloud.Configuration.ActiveDirectoryAuthorityHost + tenant
return oneauth.NewCredential(authority, cAZD_CLIENT_ID, oneauth.CredentialOptions{
HomeAccountID: *user.HomeAccountID,
})
}
}
}
}
return nil, ErrNoCurrentUser
}
if currentUser.HomeAccountID != nil {
if currentUser.FromOneAuth {
tenant := options.TenantID
if tenant == "" {
tenant = "organizations"
}
authority, err := url.JoinPath(m.cloud.Configuration.ActiveDirectoryAuthorityHost, tenant)
if err != nil {
return nil, fmt.Errorf("joining authority url: %w", err)
}
return oneauth.NewCredential(authority, cAZD_CLIENT_ID, oneauth.CredentialOptions{
HomeAccountID: *currentUser.HomeAccountID,
NoPrompt: options.NoPrompt,
})
}
accounts, err := m.publicClient.Accounts(ctx)
if err != nil {
return nil, err
}
for i, account := range accounts {
if account.HomeAccountID == *currentUser.HomeAccountID {
if options.TenantID == "" {
return newAzdCredential(m.publicClient, &accounts[i], m.cloud), nil
} else {
newAuthority := m.cloud.Configuration.ActiveDirectoryAuthorityHost + options.TenantID
newOptions := make([]public.Option, 0, len(m.publicClientOptions)+1)
newOptions = append(newOptions, m.publicClientOptions...)
// It is important that this option comes after the saved public client options since it will
// override the default authority.
newOptions = append(newOptions, public.WithAuthority(newAuthority))
clientWithNewTenant, err := public.New(cAZD_CLIENT_ID, newOptions...)
if err != nil {
return nil, err
}
return newAzdCredential(&msalPublicClientAdapter{client: &clientWithNewTenant}, &accounts[i], m.cloud), nil
}
}
}
} else if currentUser.TenantID != nil && currentUser.ClientID != nil {
ps, err := m.loadSecret(*currentUser.TenantID, *currentUser.ClientID)
if err != nil {
return nil, fmt.Errorf("loading secret: %w: %w", err, ErrNoCurrentUser)
}
// by default we used the stored tenant (i.e. the one provided with the tenant id parameter when a user ran
// `azd auth login`), but we allow an override using the options bag, when
// TenantID is non-empty and PreferFallbackTenant is not true.
tenantID := *currentUser.TenantID
if options.TenantID != "" {
tenantID = options.TenantID
}
if ps.ClientSecret != nil {
return m.newCredentialFromClientSecret(tenantID, *currentUser.ClientID, *ps.ClientSecret)
} else if ps.ClientCertificate != nil {
return m.newCredentialFromClientCertificate(tenantID, *currentUser.ClientID, *ps.ClientCertificate)
} else if ps.FederatedAuth != nil && ps.FederatedAuth.TokenProvider != nil {
return m.newCredentialFromFederatedTokenProvider(
tenantID, *currentUser.ClientID, *ps.FederatedAuth.TokenProvider)
}
}
return nil, ErrNoCurrentUser
}
type ClaimsForCurrentUserOptions = CredentialForCurrentUserOptions
// ClaimsForCurrentUser returns claims for the currently logged in user.
func (m *Manager) ClaimsForCurrentUser(ctx context.Context, options *ClaimsForCurrentUserOptions) (TokenClaims, error) {
if options == nil {
options = &ClaimsForCurrentUserOptions{}
}
// The user's credential is used to obtain an access token.
// This implementation works well even in cases where a remote credential protocol is used to provide the credential.
cred, err := m.CredentialForCurrentUser(ctx, options)
if err != nil {
return TokenClaims{}, err
}
accessToken, err := cred.GetToken(ctx, policy.TokenRequestOptions{
Scopes: LoginScopes(m.cloud),
TenantID: options.TenantID,
})
if err != nil {
return TokenClaims{}, err
}
claims, err := GetClaimsFromAccessToken(accessToken.Token)
if err != nil {
return TokenClaims{}, err
}
return claims, nil
}
func shouldUseLegacyAuth(cfg config.Config) bool {
if useLegacyAuth, has := cfg.Get(cUseAzCliAuthKey); has {
if use, err := strconv.ParseBool(useLegacyAuth.(string)); err == nil && use {
return true
}
}
return false
}
func ShouldUseCloudShellAuth() bool {
if useCloudShellAuth, has := os.LookupEnv(cUseCloudShellAuthEnvVar); has {
if use, err := strconv.ParseBool(useCloudShellAuth); err == nil && use {
log.Printf("using CloudShell auth")
return true
}
}
return false
}
// GetLoggedInServicePrincipalTenantID returns the stored service principal's tenant ID.
//
// Service principals are fixed to a particular tenant.
//
// This can be used to determine if the tenant is fixed, and if so short circuit performance intensive tenant-switching
// for service principals.
func (m *Manager) GetLoggedInServicePrincipalTenantID(ctx context.Context) (*string, error) {
if m.UseExternalAuth() {
// When delegating to an external system, we have no way to determine what principal was used
return nil, nil
}
cfg, err := m.userConfigManager.Load()
if err != nil {
return nil, fmt.Errorf("fetching current user: %w", err)
}
if shouldUseLegacyAuth(cfg) {
// When delegating to az, we have no way to determine what principal was used
return nil, nil
}
authCfg, err := m.readAuthConfig()
if err != nil {
return nil, fmt.Errorf("fetching auth config: %w", err)
}
currentUser, err := readUserProperties(authCfg)
if err != nil {
// No user is logged in, if running in CloudShell use tenant id from
// CloudShell session (single tenant)
if ShouldUseCloudShellAuth() {
// Tenant ID is not required when requesting a token from CloudShell
credential, err := m.CredentialForCurrentUser(ctx, nil)
if err != nil {
return nil, err
}
token, err := EnsureLoggedInCredential(ctx, credential, m.cloud)
if err != nil {
return nil, err
}
tenantId, err := GetTenantIdFromToken(token.Token)
if err != nil {
return nil, err
}
return &tenantId, nil
}
return nil, ErrNoCurrentUser
}
// Record type of account found
if currentUser.TenantID != nil {
tracing.SetGlobalAttributes(fields.AccountTypeKey.String(fields.AccountTypeServicePrincipal))
}
if currentUser.HomeAccountID != nil {
tracing.SetGlobalAttributes(fields.AccountTypeKey.String(fields.AccountTypeUser))
}
return currentUser.TenantID, nil
}
func (m *Manager) newCredentialFromClientSecret(
tenantID string,
clientID string,
clientSecret string,
) (azcore.TokenCredential, error) {
options := &azidentity.ClientSecretCredentialOptions{
ClientOptions: policy.ClientOptions{
Transport: m.httpClient,
// TODO: Inject client options instead? this can be done if we're OK
// using the default user agent string.
Cloud: m.cloud.Configuration,
},
}
cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, options)
if err != nil {
return nil, fmt.Errorf("creating credential: %w: %w", err, ErrNoCurrentUser)
}
return cred, nil
}
func (m *Manager) newCredentialFromClientCertificate(
tenantID string,
clientID string,
clientCertificate string,
) (azcore.TokenCredential, error) {
certData, err := base64.StdEncoding.DecodeString(clientCertificate)
if err != nil {
return nil, fmt.Errorf("decoding certificate: %w: %w", err, ErrNoCurrentUser)
}
certs, key, err := azidentity.ParseCertificates(certData, nil)
if err != nil {
return nil, fmt.Errorf("parsing certificate: %w: %w", err, ErrNoCurrentUser)
}
options := &azidentity.ClientCertificateCredentialOptions{
ClientOptions: policy.ClientOptions{
Transport: m.httpClient,
// TODO: Inject client options instead? this can be done if we're OK
// using the default user agent string.
Cloud: m.cloud.Configuration,
},
}
cred, err := azidentity.NewClientCertificateCredential(
tenantID, clientID, certs, key, options)
if err != nil {
return nil, fmt.Errorf("creating credential: %w: %w", err, ErrNoCurrentUser)
}
return cred, nil
}
func (m *Manager) newCredentialFromFederatedTokenProvider(
tenantID string,
clientID string,
provider federatedTokenProvider,
) (azcore.TokenCredential, error) {
if provider != gitHubFederatedAuth {
return nil, fmt.Errorf("unsupported federated token provider: '%s'", string(provider))
}
options := &azidentity.ClientAssertionCredentialOptions{
ClientOptions: policy.ClientOptions{
Transport: m.httpClient,
// TODO: Inject client options instead? this can be done if we're OK
// using the default user agent string.
Cloud: m.cloud.Configuration,
},
}
cred, err := azidentity.NewClientAssertionCredential(
tenantID,
clientID,
func(ctx context.Context) (string, error) {
federatedToken, err := m.ghClient.TokenForAudience(ctx, "api://AzureADTokenExchange")
if err != nil {
return "", fmt.Errorf("fetching federated token: %w", err)
}
return federatedToken, nil
},
options)
if err != nil {
return nil, fmt.Errorf("creating credential: %w", err)
}
return cred, nil
}
func (m *Manager) newCredentialFromCloudShell() (azcore.TokenCredential, error) {
return NewCloudShellCredential(m.httpClient), nil
}
// WithOpenUrl defines a custom strategy for browsing to the url.
type WithOpenUrl func(url string) error
// LoginInteractiveOptions holds the optional inputs for interactive login.
type LoginInteractiveOptions struct {
TenantID string
RedirectPort int
WithOpenUrl WithOpenUrl
}
// LoginInteractive opens a browser for authenticate the user.
func (m *Manager) LoginInteractive(
ctx context.Context,
scopes []string,
options *LoginInteractiveOptions) (azcore.TokenCredential, error) {
if scopes == nil {
scopes = m.LoginScopes()
}
acquireTokenOptions := []public.AcquireInteractiveOption{}
if options == nil {
options = &LoginInteractiveOptions{}
}
if options.RedirectPort > 0 {
acquireTokenOptions = append(
acquireTokenOptions, public.WithRedirectURI(fmt.Sprintf("http://localhost:%d", options.RedirectPort)))
}
if options.TenantID != "" {
acquireTokenOptions = append(acquireTokenOptions, public.WithTenantID(options.TenantID))
}
if options.WithOpenUrl != nil {
acquireTokenOptions = append(acquireTokenOptions, public.WithOpenURL(options.WithOpenUrl))
}
res, err := m.publicClient.AcquireTokenInteractive(ctx, scopes, acquireTokenOptions...)
if err != nil {
return nil, err
}
if err := m.saveLoginForPublicClient(res); err != nil {
return nil, err
}
return newAzdCredential(m.publicClient, &res.Account, m.cloud), nil
}
// LoginWithBrokerAccount logs in an account provided by the system authentication broker via OneAuth.
// For example, it will log in the user currently signed in to Windows. This method never prompts for
// user interaction and returns an error when the broker doesn't provide an account.
func (m *Manager) LoginWithBrokerAccount() error {
accountID, err := oneauth.LogInSilently(cAZD_CLIENT_ID)
if err == nil {
err = m.saveUserProperties(&userProperties{
FromOneAuth: true,
HomeAccountID: &accountID,
})
}
return err
}
// LoginWithOneAuth starts OneAuth's interactive login flow.
func (m *Manager) LoginWithOneAuth(ctx context.Context, tenantID string, scopes []string) error {
if len(scopes) == 0 {
scopes = m.LoginScopes()
}
authority := m.cloud.Configuration.ActiveDirectoryAuthorityHost + tenantID
accountID, err := oneauth.LogIn(authority, cAZD_CLIENT_ID, strings.Join(scopes, " "))
if err == nil {
err = m.saveUserProperties(&userProperties{
FromOneAuth: true,
HomeAccountID: &accountID,
})
}
return err
}
func (m *Manager) LoginWithDeviceCode(
ctx context.Context, tenantID string, scopes []string, withOpenUrl WithOpenUrl) (azcore.TokenCredential, error) {
if scopes == nil {
scopes = m.LoginScopes()
}
options := []public.AcquireByDeviceCodeOption{}
if tenantID != "" {
options = append(options, public.WithTenantID(tenantID))
}
if withOpenUrl == nil {
withOpenUrl = browser.OpenURL
}
code, err := m.publicClient.AcquireTokenByDeviceCode(ctx, scopes, options...)
if err != nil {
return nil, err
}
url := "https://microsoft.com/devicelogin"
if ShouldUseCloudShellAuth() {
m.console.MessageUxItem(ctx, &ux.MultilineMessage{
Lines: []string{
// nolint:lll
"Cloud Shell is automatically authenticated under the initial account used to sign in. Run 'azd auth login' only if you need to use a different account.",
fmt.Sprintf(
"To sign in, use a web browser to open the page %s and enter the code %s to authenticate.",
output.WithUnderline(url),
output.WithBold(code.UserCode()),
),
},
})
} else {
m.console.Message(ctx, fmt.Sprintf("Start by copying the next code: %s", output.WithBold(code.UserCode())))
if err := withOpenUrl(url); err != nil {
log.Println("error launching browser: ", err.Error())
m.console.Message(ctx, fmt.Sprintf("Error launching browser. Manually go to: %s", url))
}
m.console.Message(ctx, "Waiting for you to complete authentication in the browser...")
}
res, err := code.AuthenticationResult(ctx)
if err != nil {
return nil, err
}
m.console.Message(ctx, "Device code authentication completed.")
if err := m.saveLoginForPublicClient(res); err != nil {
return nil, err
}
return newAzdCredential(m.publicClient, &res.Account, m.cloud), nil
}
func (m *Manager) LoginWithServicePrincipalSecret(
ctx context.Context, tenantId, clientId, clientSecret string,
) (azcore.TokenCredential, error) {
cred, err := azidentity.NewClientSecretCredential(tenantId, clientId, clientSecret, nil)
if err != nil {
return nil, fmt.Errorf("creating credential: %w", err)
}
if err := m.saveLoginForServicePrincipal(
tenantId,
clientId,
&persistedSecret{
ClientSecret: &clientSecret,
},
); err != nil {
return nil, err
}
return cred, nil
}
func (m *Manager) LoginWithServicePrincipalCertificate(
ctx context.Context, tenantId, clientId string, certData []byte,
) (azcore.TokenCredential, error) {
certs, key, err := azidentity.ParseCertificates(certData, nil)
if err != nil {
return nil, fmt.Errorf("parsing certificate: %w", err)
}
cred, err := azidentity.NewClientCertificateCredential(tenantId, clientId, certs, key, nil)
if err != nil {
return nil, fmt.Errorf("creating credential: %w", err)
}
encodedCert := base64.StdEncoding.EncodeToString(certData)
if err := m.saveLoginForServicePrincipal(
tenantId,
clientId,
&persistedSecret{
ClientCertificate: &encodedCert,
},
); err != nil {
return nil, err
}
return cred, nil
}
func (m *Manager) LoginWithServicePrincipalFederatedTokenProvider(
ctx context.Context, tenantId, clientId, provider string,
) (azcore.TokenCredential, error) {
cred, err := m.newCredentialFromFederatedTokenProvider(tenantId, clientId, federatedTokenProvider(provider))
if err != nil {
return nil, err
}
if err := m.saveLoginForServicePrincipal(
tenantId,
clientId,
&persistedSecret{
FederatedAuth: &federatedAuth{
TokenProvider: &gitHubFederatedAuth,
},
},
); err != nil {
return nil, err
}
return cred, nil
}
// Logout signs out the current user and removes any cached authentication information
func (m *Manager) Logout(ctx context.Context) error {
act, err := m.getSignedInAccount(ctx)
if err != nil && !errors.Is(err, ErrNoCurrentUser) {
return fmt.Errorf("fetching current user: %w", err)
}
if act != nil {
if err := m.publicClient.RemoveAccount(ctx, *act); err != nil {
return fmt.Errorf("removing account from msal cache: %w", err)
}
}
cfg, err := m.readAuthConfig()
if err != nil {
return fmt.Errorf("loading config: %w", err)
}
// we are fine to ignore the error here, it just means there's nothing to clean up.
currentUser, _ := readUserProperties(cfg)
if currentUser != nil {
if currentUser.FromOneAuth {
if err := oneauth.Logout(cAZD_CLIENT_ID); err != nil {
return fmt.Errorf("logging out of OneAuth: %w", err)
}
} else if currentUser.TenantID != nil && currentUser.ClientID != nil {
// When logged in as a service principal, remove the stored credential
if err := m.saveLoginForServicePrincipal(
*currentUser.TenantID, *currentUser.ClientID, &persistedSecret{},
); err != nil {
return fmt.Errorf("removing authentication secrets: %w", err)
}
}
}
if err := cfg.Unset(cCurrentUserKey); err != nil {
return fmt.Errorf("un-setting current user: %w", err)
}
if err := m.saveAuthConfig(cfg); err != nil {
return fmt.Errorf("saving config: %w", err)
}
return nil
}
func (m *Manager) UseExternalAuth() bool {
return m.externalAuthCfg.Endpoint != "" && m.externalAuthCfg.Key != ""
}
func (m *Manager) saveLoginForPublicClient(res public.AuthResult) error {
if err := m.saveUserProperties(&userProperties{HomeAccountID: &res.Account.HomeAccountID}); err != nil {
return err
}
return nil
}
func (m *Manager) saveLoginForServicePrincipal(tenantId, clientId string, secret *persistedSecret) error {
if err := m.saveSecret(tenantId, clientId, secret); err != nil {
return err
}
if err := m.saveUserProperties(&userProperties{ClientID: &clientId, TenantID: &tenantId}); err != nil {
return err
}
return nil
}
// getSignedInAccount fetches the public.Account for the signed in user, or nil if one does not exist
// (e.g when logged in with a service principal).
func (m *Manager) getSignedInAccount(ctx context.Context) (*public.Account, error) {
cfg, err := m.readAuthConfig()
if err != nil {
return nil, fmt.Errorf("fetching current user: %w", err)
}
currentUser, err := readUserProperties(cfg)
if err != nil {
return nil, ErrNoCurrentUser
}
if currentUser.HomeAccountID != nil {
accounts, err := m.publicClient.Accounts(ctx)
if err != nil {
return nil, err
}
for _, account := range accounts {
if account.HomeAccountID == *currentUser.HomeAccountID {
return &account, nil
}
}
}
return nil, nil
}
// saveUserProperties writes the properties under [cCurrentUserKey], overwriting any existing value.
func (m *Manager) saveUserProperties(user *userProperties) error {
cfg, err := m.readAuthConfig()
if err != nil {
return fmt.Errorf("fetching current user: %w", err)
}
if err := cfg.Set(cCurrentUserKey, *user); err != nil {
return fmt.Errorf("setting account id in config: %w", err)
}
return m.saveAuthConfig(cfg)
}
// readAuthConfig loads the configuration from [cAuthConfigFileName] and returns a parsed version of it. If the config
// file does not exist, an empty [config.Config] is returned, with no error.
func (m *Manager) readAuthConfig() (config.Config, error) {
cfgPath, err := config.GetUserConfigDir()
if err != nil {
return nil, fmt.Errorf("getting user config dir: %w", err)
}
authCfgFile := filepath.Join(cfgPath, cAuthConfigFileName)
authCfg, err := m.configManager.Load(authCfgFile)
if err == nil {
return authCfg, nil
} else if !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("reading auth config: %w", err)
}
// We used to store auth related configuration in the user configuration file directly. If above file did not exist,
// see if there is the data in the old location, and if so migrate it to the new location. This upgrades the old
// format to the new format.
userCfg, err := m.userConfigManager.Load()
if err != nil {
return nil, fmt.Errorf("reading user config: %w", err)
}
curUserData, has := userCfg.Get(cCurrentUserKey)
if !has {
return config.NewEmptyConfig(), nil
}
authCfg = config.NewEmptyConfig()
if err := authCfg.Set(cCurrentUserKey, curUserData); err != nil {
return nil, err
}
if err := m.saveAuthConfig(authCfg); err != nil {
return nil, err
}
if err := userCfg.Unset(cCurrentUserKey); err != nil {
return nil, err
}
if err := m.userConfigManager.Save(userCfg); err != nil {
return nil, err
}
return authCfg, nil
}
func (m *Manager) saveAuthConfig(c config.Config) error {
cfgPath, err := config.GetUserConfigDir()
if err != nil {
return fmt.Errorf("getting user config dir: %w", err)
}
authCfgFile := filepath.Join(cfgPath, cAuthConfigFileName)
return m.configManager.Save(c, authCfgFile)
}
// persistedSecretLookupKey returns the cache key we use for a given tenantId, clientId pair.
func persistedSecretLookupKey(tenantId, clientId string) string {
return fmt.Sprintf("%s.%s", tenantId, clientId)
}
// loadSecret reads a secret from the credential cache for a given client and tenant.
func (m *Manager) loadSecret(tenantId, clientId string) (*persistedSecret, error) {
val, err := m.credentialCache.Read(persistedSecretLookupKey(tenantId, clientId))
if err != nil {
return nil, err
}
var ps persistedSecret
if err := json.Unmarshal(val, &ps); err != nil {
return nil, err
}
return &ps, nil
}
// saveSecret writes a secret into the credential cache for a given client and tenant.
func (m *Manager) saveSecret(tenantId, clientId string, ps *persistedSecret) error {
data, err := json.Marshal(ps)
if err != nil {
return err
}
return m.credentialCache.Set(persistedSecretLookupKey(tenantId, clientId), data)
}
type CredentialForCurrentUserOptions struct {
// NoPrompt controls whether the credential may prompt for user interaction.
NoPrompt bool
// The tenant ID to use when constructing the credential, instead of the default tenant.
TenantID string
}
// persistedSecret is the model type for the value we store in the credential cache. It is logically a discriminated union
// of the different supported authentication modes
type persistedSecret struct {
// The client secret.
ClientSecret *string `json:"clientSecret,omitempty"`
// The bytes of the client certificate, which can be presented to azidentity.ParseCertificates, encoded as a
// base64 string.
ClientCertificate *string `json:"clientCertificate,omitempty"`
// The federated auth credential.
FederatedAuth *federatedAuth `json:"federatedAuth,omitempty"`
}
// federated auth token providers
var (
gitHubFederatedAuth federatedTokenProvider = "github"
)
// token provider for federated auth
type federatedTokenProvider string
// federatedAuth stores federated authentication information.
type federatedAuth struct {
// The auth token provider. Tokens are obtained by calling the provider as needed.
TokenProvider *federatedTokenProvider `json:"tokenProvider,omitempty"`
}
// userProperties is the model type for the value we store in the user's config. It is logically a discriminated union of
// either an home account id (when logging in using a public client) or a client and tenant id (when using a confidential
// client).
type userProperties struct {
HomeAccountID *string `json:"homeAccountId,omitempty"`
FromOneAuth bool `json:"fromOneAuth,omitempty"`
ClientID *string `json:"clientId,omitempty"`
TenantID *string `json:"tenantId,omitempty"`
}