1717package login
1818
1919import (
20+ "encoding/json"
21+ "strconv"
2022 "time"
2123
2224 "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
2325 "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
2426 "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2019-12-01/containerinstance"
2527 "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
2628 "github.com/Azure/go-autorest/autorest"
29+ "github.com/Azure/go-autorest/autorest/adal"
30+ "github.com/Azure/go-autorest/autorest/date"
2731 "github.com/pkg/errors"
2832
2933 "github.com/docker/compose-cli/api/errdefs"
@@ -32,8 +36,12 @@ import (
3236
3337// NewContainerGroupsClient get client toi manipulate containerGrouos
3438func NewContainerGroupsClient (subscriptionID string ) (containerinstance.ContainerGroupsClient , error ) {
35- containerGroupsClient := containerinstance .NewContainerGroupsClient (subscriptionID )
36- err := setupClient (& containerGroupsClient .Client )
39+ authorizer , mgmtURL , err := getClientSetupData ()
40+ if err != nil {
41+ return containerinstance.ContainerGroupsClient {}, err
42+ }
43+ containerGroupsClient := containerinstance .NewContainerGroupsClientWithBaseURI (mgmtURL , subscriptionID )
44+ setupClient (& containerGroupsClient .Client , authorizer )
3745 if err != nil {
3846 return containerinstance.ContainerGroupsClient {}, err
3947 }
@@ -43,68 +51,100 @@ func NewContainerGroupsClient(subscriptionID string) (containerinstance.Containe
4351 return containerGroupsClient , nil
4452}
4553
46- func setupClient (aciClient * autorest.Client ) error {
54+ func setupClient (aciClient * autorest.Client , auth autorest. Authorizer ) {
4755 aciClient .UserAgent = internal .UserAgentName + "/" + internal .Version
48- auth , err := NewAuthorizerFromLogin ()
49- if err != nil {
50- return err
51- }
5256 aciClient .Authorizer = auth
53- return nil
5457}
5558
5659// NewStorageAccountsClient get client to manipulate storage accounts
5760func NewStorageAccountsClient (subscriptionID string ) (storage.AccountsClient , error ) {
58- containerGroupsClient := storage .NewAccountsClient (subscriptionID )
59- err := setupClient (& containerGroupsClient .Client )
61+ authorizer , mgmtURL , err := getClientSetupData ()
6062 if err != nil {
6163 return storage.AccountsClient {}, err
6264 }
63- containerGroupsClient .PollingDelay = 5 * time .Second
64- containerGroupsClient .RetryAttempts = 30
65- containerGroupsClient .RetryDuration = 1 * time .Second
66- return containerGroupsClient , nil
65+ storageAccuntsClient := storage .NewAccountsClientWithBaseURI (mgmtURL , subscriptionID )
66+ setupClient (& storageAccuntsClient .Client , authorizer )
67+ storageAccuntsClient .PollingDelay = 5 * time .Second
68+ storageAccuntsClient .RetryAttempts = 30
69+ storageAccuntsClient .RetryDuration = 1 * time .Second
70+ return storageAccuntsClient , nil
6771}
6872
6973// NewFileShareClient get client to manipulate file shares
7074func NewFileShareClient (subscriptionID string ) (storage.FileSharesClient , error ) {
71- containerGroupsClient := storage .NewFileSharesClient (subscriptionID )
72- err := setupClient (& containerGroupsClient .Client )
75+ authorizer , mgmtURL , err := getClientSetupData ()
7376 if err != nil {
7477 return storage.FileSharesClient {}, err
7578 }
76- containerGroupsClient .PollingDelay = 5 * time .Second
77- containerGroupsClient .RetryAttempts = 30
78- containerGroupsClient .RetryDuration = 1 * time .Second
79- return containerGroupsClient , nil
79+ fileSharesClient := storage .NewFileSharesClientWithBaseURI (mgmtURL , subscriptionID )
80+ setupClient (& fileSharesClient .Client , authorizer )
81+ fileSharesClient .PollingDelay = 5 * time .Second
82+ fileSharesClient .RetryAttempts = 30
83+ fileSharesClient .RetryDuration = 1 * time .Second
84+ return fileSharesClient , nil
8085}
8186
8287// NewSubscriptionsClient get subscription client
8388func NewSubscriptionsClient () (subscription.SubscriptionsClient , error ) {
84- subc := subscription .NewSubscriptionsClient ()
85- err := setupClient (& subc .Client )
89+ authorizer , mgmtURL , err := getClientSetupData ()
8690 if err != nil {
8791 return subscription.SubscriptionsClient {}, errors .Wrap (errdefs .ErrLoginRequired , err .Error ())
8892 }
93+ subc := subscription .NewSubscriptionsClientWithBaseURI (mgmtURL )
94+ setupClient (& subc .Client , authorizer )
8995 return subc , nil
9096}
9197
9298// NewGroupsClient get client to manipulate groups
9399func NewGroupsClient (subscriptionID string ) (resources.GroupsClient , error ) {
94- groupsClient := resources .NewGroupsClient (subscriptionID )
95- err := setupClient (& groupsClient .Client )
100+ authorizer , mgmtURL , err := getClientSetupData ()
96101 if err != nil {
97102 return resources.GroupsClient {}, err
98103 }
104+ groupsClient := resources .NewGroupsClientWithBaseURI (mgmtURL , subscriptionID )
105+ setupClient (& groupsClient .Client , authorizer )
99106 return groupsClient , nil
100107}
101108
102109// NewContainerClient get client to manipulate containers
103110func NewContainerClient (subscriptionID string ) (containerinstance.ContainersClient , error ) {
104- containerClient := containerinstance .NewContainersClient (subscriptionID )
105- err := setupClient (& containerClient .Client )
111+ authorizer , mgmtURL , err := getClientSetupData ()
106112 if err != nil {
107113 return containerinstance.ContainersClient {}, err
108114 }
115+ containerClient := containerinstance .NewContainersClientWithBaseURI (mgmtURL , subscriptionID )
116+ setupClient (& containerClient .Client , authorizer )
109117 return containerClient , nil
110118}
119+
120+ func getClientSetupData () (autorest.Authorizer , string , error ) {
121+ return getClientSetupDataImpl (GetTokenStorePath ())
122+ }
123+
124+ func getClientSetupDataImpl (tokenStorePath string ) (autorest.Authorizer , string , error ) {
125+ als , err := newAzureLoginServiceFromPath (tokenStorePath , azureAPIHelper {}, CloudEnvironments )
126+ if err != nil {
127+ return nil , "" , err
128+ }
129+
130+ oauthToken , _ , err := als .GetValidToken ()
131+ if err != nil {
132+ return nil , "" , errors .Wrap (err , "not logged in to azure, you need to run \" docker login azure\" first" )
133+ }
134+
135+ ce , err := als .GetCloudEnvironment ()
136+ if err != nil {
137+ return nil , "" , err
138+ }
139+
140+ token := adal.Token {
141+ AccessToken : oauthToken .AccessToken ,
142+ Type : oauthToken .TokenType ,
143+ ExpiresIn : json .Number (strconv .Itoa (int (time .Until (oauthToken .Expiry ).Seconds ()))),
144+ ExpiresOn : json .Number (strconv .Itoa (int (oauthToken .Expiry .Sub (date .UnixEpoch ()).Seconds ()))),
145+ RefreshToken : "" ,
146+ Resource : "" ,
147+ }
148+
149+ return autorest .NewBearerAuthorizer (& token ), ce .ResourceManagerURL , nil
150+ }
0 commit comments