client.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package login
  14. import (
  15. "encoding/json"
  16. "strconv"
  17. "time"
  18. "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
  19. "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
  20. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2019-12-01/containerinstance"
  21. "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
  22. "github.com/Azure/go-autorest/autorest"
  23. "github.com/Azure/go-autorest/autorest/adal"
  24. "github.com/Azure/go-autorest/autorest/date"
  25. "github.com/pkg/errors"
  26. "github.com/docker/compose-cli/internal"
  27. "github.com/docker/compose-cli/pkg/api"
  28. )
  29. // UserAgentName is the default user agent used by the cli
  30. const UserAgentName = "docker-cli"
  31. // NewContainerGroupsClient get client toi manipulate containerGrouos
  32. func NewContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) {
  33. authorizer, mgmtURL, err := getClientSetupData()
  34. if err != nil {
  35. return containerinstance.ContainerGroupsClient{}, err
  36. }
  37. containerGroupsClient := containerinstance.NewContainerGroupsClientWithBaseURI(mgmtURL, subscriptionID)
  38. setupClient(&containerGroupsClient.Client, authorizer)
  39. if err != nil {
  40. return containerinstance.ContainerGroupsClient{}, err
  41. }
  42. containerGroupsClient.PollingDelay = 5 * time.Second
  43. containerGroupsClient.RetryAttempts = 30
  44. containerGroupsClient.RetryDuration = 1 * time.Second
  45. return containerGroupsClient, nil
  46. }
  47. func setupClient(aciClient *autorest.Client, auth autorest.Authorizer) {
  48. aciClient.UserAgent = UserAgentName + "/" + internal.Version
  49. aciClient.Authorizer = auth
  50. }
  51. // NewStorageAccountsClient get client to manipulate storage accounts
  52. func NewStorageAccountsClient(subscriptionID string) (storage.AccountsClient, error) {
  53. authorizer, mgmtURL, err := getClientSetupData()
  54. if err != nil {
  55. return storage.AccountsClient{}, err
  56. }
  57. storageAccuntsClient := storage.NewAccountsClientWithBaseURI(mgmtURL, subscriptionID)
  58. setupClient(&storageAccuntsClient.Client, authorizer)
  59. storageAccuntsClient.PollingDelay = 5 * time.Second
  60. storageAccuntsClient.RetryAttempts = 30
  61. storageAccuntsClient.RetryDuration = 1 * time.Second
  62. return storageAccuntsClient, nil
  63. }
  64. // NewFileShareClient get client to manipulate file shares
  65. func NewFileShareClient(subscriptionID string) (storage.FileSharesClient, error) {
  66. authorizer, mgmtURL, err := getClientSetupData()
  67. if err != nil {
  68. return storage.FileSharesClient{}, err
  69. }
  70. fileSharesClient := storage.NewFileSharesClientWithBaseURI(mgmtURL, subscriptionID)
  71. setupClient(&fileSharesClient.Client, authorizer)
  72. fileSharesClient.PollingDelay = 5 * time.Second
  73. fileSharesClient.RetryAttempts = 30
  74. fileSharesClient.RetryDuration = 1 * time.Second
  75. return fileSharesClient, nil
  76. }
  77. // NewSubscriptionsClient get subscription client
  78. func NewSubscriptionsClient() (subscription.SubscriptionsClient, error) {
  79. authorizer, mgmtURL, err := getClientSetupData()
  80. if err != nil {
  81. return subscription.SubscriptionsClient{}, errors.Wrap(api.ErrLoginRequired, err.Error())
  82. }
  83. subc := subscription.NewSubscriptionsClientWithBaseURI(mgmtURL)
  84. setupClient(&subc.Client, authorizer)
  85. return subc, nil
  86. }
  87. // NewGroupsClient get client to manipulate groups
  88. func NewGroupsClient(subscriptionID string) (resources.GroupsClient, error) {
  89. authorizer, mgmtURL, err := getClientSetupData()
  90. if err != nil {
  91. return resources.GroupsClient{}, err
  92. }
  93. groupsClient := resources.NewGroupsClientWithBaseURI(mgmtURL, subscriptionID)
  94. setupClient(&groupsClient.Client, authorizer)
  95. return groupsClient, nil
  96. }
  97. // NewContainerClient get client to manipulate containers
  98. func NewContainerClient(subscriptionID string) (containerinstance.ContainersClient, error) {
  99. authorizer, mgmtURL, err := getClientSetupData()
  100. if err != nil {
  101. return containerinstance.ContainersClient{}, err
  102. }
  103. containerClient := containerinstance.NewContainersClientWithBaseURI(mgmtURL, subscriptionID)
  104. setupClient(&containerClient.Client, authorizer)
  105. return containerClient, nil
  106. }
  107. func getClientSetupData() (autorest.Authorizer, string, error) {
  108. return getClientSetupDataImpl(GetTokenStorePath())
  109. }
  110. func getClientSetupDataImpl(tokenStorePath string) (autorest.Authorizer, string, error) {
  111. als, err := newAzureLoginServiceFromPath(tokenStorePath, azureAPIHelper{}, CloudEnvironments)
  112. if err != nil {
  113. return nil, "", err
  114. }
  115. oauthToken, _, err := als.GetValidToken()
  116. if err != nil {
  117. return nil, "", errors.Wrap(err, "not logged in to azure, you need to run \"docker login azure\" first")
  118. }
  119. ce, err := als.GetCloudEnvironment()
  120. if err != nil {
  121. return nil, "", err
  122. }
  123. token := adal.Token{
  124. AccessToken: oauthToken.AccessToken,
  125. Type: oauthToken.TokenType,
  126. ExpiresIn: json.Number(strconv.Itoa(int(time.Until(oauthToken.Expiry).Seconds()))),
  127. ExpiresOn: json.Number(strconv.Itoa(int(oauthToken.Expiry.Sub(date.UnixEpoch()).Seconds()))),
  128. RefreshToken: "",
  129. Resource: "",
  130. }
  131. return autorest.NewBearerAuthorizer(&token), ce.ResourceManagerURL, nil
  132. }