Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Support multiple AcsEngineClientIDs #2293

Merged
merged 4 commits into from
Feb 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions pkg/armhelpers/azureclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ import (
)

const (
// AcsEngineClientID is the AAD ClientID for the CLI native application
AcsEngineClientID = "76e0feec-6b7f-41f0-81a7-b1b944520261"

// ApplicationDir is the name of the dir where the token is cached
ApplicationDir = ".acsengine"
)
Expand Down Expand Up @@ -74,11 +71,14 @@ func NewAzureClientWithDeviceAuth(env azure.Environment, subscriptionID string)
return nil, err
}

// AcsEngineClientID is the AAD ClientID for the CLI native application
acsEngineClientID := getAcsEngineClientID(env.Name)

home, err := homedir.Dir()
if err != nil {
return nil, fmt.Errorf("Failed to get user home directory to look for cached token: %q", err)
}
cachePath := filepath.Join(home, ApplicationDir, "cache", fmt.Sprintf("%s_%s.token.json", tenantID, AcsEngineClientID))
cachePath := filepath.Join(home, ApplicationDir, "cache", fmt.Sprintf("%s_%s.token.json", tenantID, acsEngineClientID))

rawToken, err := tryLoadCachedToken(cachePath)
if err != nil {
Expand All @@ -87,15 +87,15 @@ func NewAzureClientWithDeviceAuth(env azure.Environment, subscriptionID string)

var armSpt *adal.ServicePrincipalToken
if rawToken != nil {
armSpt, err = adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, AcsEngineClientID, env.ServiceManagementEndpoint, *rawToken, tokenCallback(cachePath))
armSpt, err = adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, acsEngineClientID, env.ServiceManagementEndpoint, *rawToken, tokenCallback(cachePath))
if err != nil {
return nil, err
}
err = armSpt.Refresh()
if err != nil {
log.Warnf("Refresh token failed. Will fallback to device auth. %q", err)
} else {
graphSpt, err := adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, AcsEngineClientID, env.GraphEndpoint, armSpt.Token)
graphSpt, err := adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, acsEngineClientID, env.GraphEndpoint, armSpt.Token)
if err != nil {
return nil, err
}
Expand All @@ -107,7 +107,7 @@ func NewAzureClientWithDeviceAuth(env azure.Environment, subscriptionID string)

client := &autorest.Client{}

deviceCode, err := adal.InitiateDeviceAuth(client, *oauthConfig, AcsEngineClientID, env.ServiceManagementEndpoint)
deviceCode, err := adal.InitiateDeviceAuth(client, *oauthConfig, acsEngineClientID, env.ServiceManagementEndpoint)
if err != nil {
return nil, err
}
Expand All @@ -117,15 +117,15 @@ func NewAzureClientWithDeviceAuth(env azure.Environment, subscriptionID string)
return nil, err
}

armSpt, err = adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, AcsEngineClientID, env.ServiceManagementEndpoint, *deviceToken, tokenCallback(cachePath))
armSpt, err = adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, acsEngineClientID, env.ServiceManagementEndpoint, *deviceToken, tokenCallback(cachePath))
if err != nil {
return nil, err
}
armSpt.Refresh()

adRawToken := armSpt.Token
adRawToken.Resource = env.GraphEndpoint
graphSpt, err := adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, AcsEngineClientID, env.GraphEndpoint, adRawToken)
graphSpt, err := adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, acsEngineClientID, env.GraphEndpoint, adRawToken)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -252,6 +252,16 @@ func getOAuthConfig(env azure.Environment, subscriptionID string) (*adal.OAuthCo
return oauthConfig, tenantID, nil
}

func getAcsEngineClientID(envName string) string {
switch envName {
case "AzureUSGovernmentCloud":
// TODO: Replace with AppId for Azure US Government Cloud
return "76e0feec-6b7f-41f0-81a7-b1b944520261"
default:
return "76e0feec-6b7f-41f0-81a7-b1b944520261"
}
}

func getClient(env azure.Environment, subscriptionID, tenantID string, armSpt *adal.ServicePrincipalToken, graphSpt *adal.ServicePrincipalToken) *AzureClient {
c := &AzureClient{
environment: env,
Expand Down