client.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // NewContainerGroupsClient get client toi manipulate containerGrouos
  30. func NewContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) {
  31. authorizer, mgmtURL, err := getClientSetupData()
  32. if err != nil {
  33. return containerinstance.ContainerGroupsClient{}, err
  34. }
  35. containerGroupsClient := containerinstance.NewContainerGroupsClientWithBaseURI(mgmtURL, subscriptionID)
  36. setupClient(&containerGroupsClient.Client, authorizer)
  37. if err != nil {
  38. return containerinstance.ContainerGroupsClient{}, err
  39. }
  40. containerGroupsClient.PollingDelay = 5 * time.Second
  41. containerGroupsClient.RetryAttempts = 30
  42. containerGroupsClient.RetryDuration = 1 * time.Second
  43. return containerGroupsClient, nil
  44. }
  45. func setupClient(aciClient *autorest.Client, auth autorest.Authorizer) {
  46. aciClient.UserAgent = internal.UserAgentName + "/" + internal.Version
  47. aciClient.Authorizer = auth
  48. }
  49. // NewStorageAccountsClient get client to manipulate storage accounts
  50. func NewStorageAccountsClient(subscriptionID string) (storage.AccountsClient, error) {
  51. authorizer, mgmtURL, err := getClientSetupData()
  52. if err != nil {
  53. return storage.AccountsClient{}, err
  54. }
  55. storageAccuntsClient := storage.NewAccountsClientWithBaseURI(mgmtURL, subscriptionID)
  56. setupClient(&storageAccuntsClient.Client, authorizer)
  57. storageAccuntsClient.PollingDelay = 5 * time.Second
  58. storageAccuntsClient.RetryAttempts = 30
  59. storageAccuntsClient.RetryDuration = 1 * time.Second
  60. return storageAccuntsClient, nil
  61. }
  62. // NewFileShareClient get client to manipulate file shares
  63. func NewFileShareClient(subscriptionID string) (storage.FileSharesClient, error) {
  64. authorizer, mgmtURL, err := getClientSetupData()
  65. if err != nil {
  66. return storage.FileSharesClient{}, err
  67. }
  68. fileSharesClient := storage.NewFileSharesClientWithBaseURI(mgmtURL, subscriptionID)
  69. setupClient(&fileSharesClient.Client, authorizer)
  70. fileSharesClient.PollingDelay = 5 * time.Second
  71. fileSharesClient.RetryAttempts = 30
  72. fileSharesClient.RetryDuration = 1 * time.Second
  73. return fileSharesClient, nil
  74. }
  75. // NewSubscriptionsClient get subscription client
  76. func NewSubscriptionsClient() (subscription.SubscriptionsClient, error) {
  77. authorizer, mgmtURL, err := getClientSetupData()
  78. if err != nil {
  79. return subscription.SubscriptionsClient{}, errors.Wrap(api.ErrLoginRequired, err.Error())
  80. }
  81. subc := subscription.NewSubscriptionsClientWithBaseURI(mgmtURL)
  82. setupClient(&subc.Client, authorizer)
  83. return subc, nil
  84. }
  85. // NewGroupsClient get client to manipulate groups
  86. func NewGroupsClient(subscriptionID string) (resources.GroupsClient, error) {
  87. authorizer, mgmtURL, err := getClientSetupData()
  88. if err != nil {
  89. return resources.GroupsClient{}, err
  90. }
  91. groupsClient := resources.NewGroupsClientWithBaseURI(mgmtURL, subscriptionID)
  92. setupClient(&groupsClient.Client, authorizer)
  93. return groupsClient, nil
  94. }
  95. // NewContainerClient get client to manipulate containers
  96. func NewContainerClient(subscriptionID string) (containerinstance.ContainersClient, error) {
  97. authorizer, mgmtURL, err := getClientSetupData()
  98. if err != nil {
  99. return containerinstance.ContainersClient{}, err
  100. }
  101. containerClient := containerinstance.NewContainersClientWithBaseURI(mgmtURL, subscriptionID)
  102. setupClient(&containerClient.Client, authorizer)
  103. return containerClient, nil
  104. }
  105. func getClientSetupData() (autorest.Authorizer, string, error) {
  106. return getClientSetupDataImpl(GetTokenStorePath())
  107. }
  108. func getClientSetupDataImpl(tokenStorePath string) (autorest.Authorizer, string, error) {
  109. als, err := newAzureLoginServiceFromPath(tokenStorePath, azureAPIHelper{}, CloudEnvironments)
  110. if err != nil {
  111. return nil, "", err
  112. }
  113. oauthToken, _, err := als.GetValidToken()
  114. if err != nil {
  115. return nil, "", errors.Wrap(err, "not logged in to azure, you need to run \"docker login azure\" first")
  116. }
  117. ce, err := als.GetCloudEnvironment()
  118. if err != nil {
  119. return nil, "", err
  120. }
  121. token := adal.Token{
  122. AccessToken: oauthToken.AccessToken,
  123. Type: oauthToken.TokenType,
  124. ExpiresIn: json.Number(strconv.Itoa(int(time.Until(oauthToken.Expiry).Seconds()))),
  125. ExpiresOn: json.Number(strconv.Itoa(int(oauthToken.Expiry.Sub(date.UnixEpoch()).Seconds()))),
  126. RefreshToken: "",
  127. Resource: "",
  128. }
  129. return autorest.NewBearerAuthorizer(&token), ce.ResourceManagerURL, nil
  130. }