Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azure: allow installer to consume AZURE_AUTH_LOCATION env for credentials file #1785

Merged
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
19 changes: 13 additions & 6 deletions pkg/asset/installconfig/azure/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import (

const azureAuthEnv = "AZURE_AUTH_LOCATION"

var authFilePath = os.Getenv("HOME") + "/.azure/osServicePrincipal.json"
var defaultAuthFilePath = filepath.Join(os.Getenv("HOME"), ".azure", "osServicePrincipal.json")

//Session is an object representing session for subscription
type Session struct {
Authorizer autorest.Authorizer
Credentials Credentials
}

//Credentials is the data type for credentials as undestood by the azure sdk
//Credentials is the data type for credentials as understood by the azure sdk
type Credentials struct {
SubscriptionID string `json:"subscriptionId,omitempty"`
ClientID string `json:"clientId,omitempty"`
Expand All @@ -35,18 +35,25 @@ type Credentials struct {
// GetSession returns an azure session by using credentials found in ~/.azure/osServicePrincipal.json
// and, if no creds are found, asks for them and stores them on disk in a config file
func GetSession() (*Session, error) {
os.Setenv(azureAuthEnv, authFilePath)
return newSessionFromFile()
authFile := defaultAuthFilePath
if f := os.Getenv(azureAuthEnv); len(f) > 0 {
authFile = f
}
return newSessionFromFile(authFile)
}

func newSessionFromFile() (*Session, error) {
func newSessionFromFile(authFilePath string) (*Session, error) {
// NewAuthorizerFromFileWithResource uses `auth.GetSettingsFromFile`, which uses the `azureAuthEnv` to fetch the auth credentials.
// therefore setting the local env here to authFilePath allows NewAuthorizerFromFileWithResource to load credentials.
os.Setenv(azureAuthEnv, authFilePath)
authorizer, err := auth.NewAuthorizerFromFileWithResource(azureenv.PublicCloud.ResourceManagerEndpoint)
if err != nil {
logrus.Debug("could not get an azure authorizer from file. Asking user to provide authentication info")
logrus.Debug("Could not get an azure authorizer from file. Asking user to provide authentication info")
credentials, err := askForCredentials()
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve credentials from user")
}
logrus.Infof("Saving user credentials to %q", authFilePath)
if err = saveCredentials(*credentials, authFilePath); err != nil {
return nil, errors.Wrap(err, "failed to save credentials")
}
Expand Down