2018-11-21 22:06:03 +01:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2018-11-22 18:02:33 +01:00
|
|
|
"log"
|
|
|
|
"net/url"
|
2018-11-21 22:06:03 +01:00
|
|
|
"os"
|
2018-11-22 18:02:33 +01:00
|
|
|
"strings"
|
2018-11-21 22:06:03 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources"
|
|
|
|
armStorage "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/storage/mgmt/storage"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/storage"
|
|
|
|
"github.com/Azure/go-autorest/autorest"
|
|
|
|
"github.com/Azure/go-autorest/autorest/adal"
|
|
|
|
"github.com/Azure/go-autorest/autorest/azure"
|
|
|
|
"github.com/hashicorp/go-azure-helpers/authentication"
|
|
|
|
"github.com/hashicorp/terraform/httpclient"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ArmClient struct {
|
|
|
|
// These Clients are only initialized if an Access Key isn't provided
|
|
|
|
groupsClient *resources.GroupsClient
|
|
|
|
storageAccountsClient *armStorage.AccountsClient
|
|
|
|
|
|
|
|
accessKey string
|
|
|
|
environment azure.Environment
|
|
|
|
resourceGroupName string
|
|
|
|
storageAccountName string
|
2018-11-22 18:02:33 +01:00
|
|
|
sasToken string
|
2018-11-21 22:06:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildArmClient(config BackendConfig) (*ArmClient, error) {
|
2018-11-26 14:42:16 +01:00
|
|
|
env, err := buildArmEnvironment(config)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-26 14:42:16 +01:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
client := ArmClient{
|
|
|
|
environment: *env,
|
|
|
|
resourceGroupName: config.ResourceGroupName,
|
|
|
|
storageAccountName: config.StorageAccountName,
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we have an Access Key - we don't need the other clients
|
|
|
|
if config.AccessKey != "" {
|
|
|
|
client.accessKey = config.AccessKey
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
2018-11-22 18:02:33 +01:00
|
|
|
// likewise with a SAS token
|
|
|
|
if config.SasToken != "" {
|
|
|
|
client.sasToken = config.SasToken
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
builder := authentication.Builder{
|
2018-11-26 14:42:16 +01:00
|
|
|
ClientID: config.ClientID,
|
|
|
|
ClientSecret: config.ClientSecret,
|
|
|
|
SubscriptionID: config.SubscriptionID,
|
|
|
|
TenantID: config.TenantID,
|
|
|
|
CustomResourceManagerEndpoint: config.CustomResourceManagerEndpoint,
|
|
|
|
Environment: config.Environment,
|
|
|
|
MsiEndpoint: config.MsiEndpoint,
|
2018-11-21 22:06:03 +01:00
|
|
|
|
|
|
|
// Feature Toggles
|
2018-12-10 22:23:30 +01:00
|
|
|
SupportsAzureCliToken: true,
|
2018-11-22 16:52:27 +01:00
|
|
|
SupportsClientSecretAuth: true,
|
|
|
|
SupportsManagedServiceIdentity: config.UseMsi,
|
2018-12-10 22:23:30 +01:00
|
|
|
// TODO: support for Client Certificate auth
|
2018-11-21 22:06:03 +01:00
|
|
|
}
|
|
|
|
armConfig, err := builder.Build()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error building ARM Config: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, armConfig.TenantID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-30 02:00:41 +02:00
|
|
|
sender := buildSender()
|
|
|
|
|
|
|
|
auth, err := armConfig.GetAuthorizationToken(sender, oauthConfig, env.TokenAudience)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
accountsClient := armStorage.NewAccountsClientWithBaseURI(env.ResourceManagerEndpoint, armConfig.SubscriptionID)
|
2019-07-30 02:00:41 +02:00
|
|
|
client.configureClient(&accountsClient.Client, sender, auth)
|
2018-11-21 22:06:03 +01:00
|
|
|
client.storageAccountsClient = &accountsClient
|
|
|
|
|
|
|
|
groupsClient := resources.NewGroupsClientWithBaseURI(env.ResourceManagerEndpoint, armConfig.SubscriptionID)
|
2019-07-30 02:00:41 +02:00
|
|
|
client.configureClient(&groupsClient.Client, sender, auth)
|
2018-11-21 22:06:03 +01:00
|
|
|
client.groupsClient = &groupsClient
|
|
|
|
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
2018-11-26 14:42:16 +01:00
|
|
|
func buildArmEnvironment(config BackendConfig) (*azure.Environment, error) {
|
|
|
|
if config.CustomResourceManagerEndpoint != "" {
|
|
|
|
log.Printf("[DEBUG] Loading Environment from Endpoint %q", config.CustomResourceManagerEndpoint)
|
|
|
|
return authentication.LoadEnvironmentFromUrl(config.CustomResourceManagerEndpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Loading Environment %q", config.Environment)
|
|
|
|
return authentication.DetermineEnvironment(config.Environment)
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
func (c ArmClient) getBlobClient(ctx context.Context) (*storage.BlobStorageClient, error) {
|
|
|
|
if c.accessKey != "" {
|
2018-11-26 14:42:16 +01:00
|
|
|
log.Printf("[DEBUG] Building the Blob Client from an Access Token")
|
2018-11-21 22:06:03 +01:00
|
|
|
storageClient, err := storage.NewBasicClientOnSovereignCloud(c.storageAccountName, c.accessKey, c.environment)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating storage client for storage account %q: %s", c.storageAccountName, err)
|
|
|
|
}
|
|
|
|
client := storageClient.GetBlobService()
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
2018-11-22 18:02:33 +01:00
|
|
|
if c.sasToken != "" {
|
2018-11-26 14:42:16 +01:00
|
|
|
log.Printf("[DEBUG] Building the Blob Client from a SAS Token")
|
2018-11-22 18:02:33 +01:00
|
|
|
token := strings.TrimPrefix(c.sasToken, "?")
|
|
|
|
uri, err := url.ParseQuery(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error parsing SAS Token: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
storageClient := storage.NewAccountSASClient(c.storageAccountName, uri, c.environment)
|
|
|
|
client := storageClient.GetBlobService()
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
2018-11-26 14:42:16 +01:00
|
|
|
log.Printf("[DEBUG] Building the Blob Client from an Access Token (using user credentials)")
|
2018-11-21 22:06:03 +01:00
|
|
|
keys, err := c.storageAccountsClient.ListKeys(ctx, c.resourceGroupName, c.storageAccountName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving keys for Storage Account %q: %s", c.storageAccountName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.Keys == nil {
|
|
|
|
return nil, fmt.Errorf("Nil key returned for storage account %q", c.storageAccountName)
|
|
|
|
}
|
|
|
|
|
|
|
|
accessKeys := *keys.Keys
|
|
|
|
accessKey := accessKeys[0].Value
|
|
|
|
|
|
|
|
storageClient, err := storage.NewBasicClientOnSovereignCloud(c.storageAccountName, *accessKey, c.environment)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error creating storage client for storage account %q: %s", c.storageAccountName, err)
|
|
|
|
}
|
|
|
|
client := storageClient.GetBlobService()
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
2019-07-30 02:00:41 +02:00
|
|
|
func (c *ArmClient) configureClient(client *autorest.Client, sender autorest.Sender, auth autorest.Authorizer) {
|
2018-11-21 22:06:03 +01:00
|
|
|
client.UserAgent = buildUserAgent()
|
|
|
|
client.Authorizer = auth
|
2019-07-30 02:00:41 +02:00
|
|
|
client.Sender = sender
|
2018-11-21 22:06:03 +01:00
|
|
|
client.SkipResourceProviderRegistration = false
|
|
|
|
client.PollingDuration = 60 * time.Minute
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildUserAgent() string {
|
|
|
|
userAgent := httpclient.UserAgentString()
|
|
|
|
|
|
|
|
// append the CloudShell version to the user agent if it exists
|
|
|
|
if azureAgent := os.Getenv("AZURE_HTTP_USER_AGENT"); azureAgent != "" {
|
|
|
|
userAgent = fmt.Sprintf("%s %s", userAgent, azureAgent)
|
|
|
|
}
|
|
|
|
|
|
|
|
return userAgent
|
|
|
|
}
|