| 1 | package azuretoken |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | |
| 10 | kvauth "github.com/Azure/azure-sdk-for-go/services/keyvault/auth" |
| 11 | "github.com/Azure/go-autorest/autorest" |
| 12 | "github.com/Azure/go-autorest/autorest/adal" |
| 13 | "github.com/Azure/go-autorest/autorest/azure" |
| 14 | "github.com/Azure/go-autorest/autorest/azure/auth" |
| 15 | "github.com/sassoftware/relic/v7/config" |
| 16 | ) |
| 17 | |
| 18 | // Configure azure authentication based on the token config and/or process |
| 19 | // environment. |
| 20 | func newAuthorizer(tconf *config.TokenConfig) (autorest.Authorizer, error) { |
| 21 | if tconf.Pin == nil { |
| 22 | // PIN not present means use environment |
| 23 | return newAuthorizerFromEnvironment() |
| 24 | } |
| 25 | credFile := *tconf.Pin |
| 26 | if credFile == "" { |
| 27 | // PIN present but empty means use azure CLI auth |
| 28 | return kvauth.NewAuthorizerFromCLI() |
| 29 | } |
| 30 | // PIN is path to a file with credentials and settings |
| 31 | os.Setenv("AZURE_AUTH_LOCATION", credFile) |
| 32 | return kvauth.NewAuthorizerFromFile() |
| 33 | } |
| 34 | |
| 35 | // If AZURE_BEARER_TOKEN_FILE is set then auth using that file, otherwise follow |
| 36 | // the same route as keyvault/auth. |
| 37 | // |
| 38 | // The bearer token case looks something like this: |
| 39 | // https://docs.microsoft.com/en-us/azure/active-directory/develop/workload-identity-federation-create-trust-gcp?tabs=typescript#exchange-a-google-token-for-an-access-token |
| 40 | func newAuthorizerFromEnvironment() (autorest.Authorizer, error) { |
| 41 | // parse settings from environment |
| 42 | resource, err := getResource() |
| 43 | if err != nil { |
| 44 | return nil, fmt.Errorf("AZURE_ENVIRONMENT: %w", err) |
| 45 | } |
| 46 | settings, err := auth.GetSettingsFromEnvironment() |
| 47 | if err != nil { |
| 48 | return nil, fmt.Errorf("parsing environment: %w", err) |
| 49 | } |
| 50 | settings.Values[auth.Resource] = resource |
| 51 | |
| 52 | // read bearer token from file |
| 53 | tokenFile := os.Getenv("AZURE_BEARER_TOKEN_FILE") |
| 54 | if tokenFile == "" { |
| 55 | // MSI or other env auth |
| 56 | return settings.GetAuthorizer() |
| 57 | } |
| 58 | clientID := settings.Values[auth.ClientID] |
| 59 | tenantID := settings.Values[auth.TenantID] |
| 60 | if clientID == "" || tenantID == "" { |
| 61 | return nil, errors.New("AZURE_CLIENT_ID and AZURE_TENANT_ID are required") |
| 62 | } |
| 63 | |
| 64 | secret := &bearerTokenFileSecret{tokenFile: tokenFile} |
| 65 | oauthConf, err := adal.NewOAuthConfig(settings.Environment.ActiveDirectoryEndpoint, tenantID) |
| 66 | if err != nil { |
| 67 | return nil, fmt.Errorf("ADAL.NewOAuthConfig: %w", err) |
| 68 | } |
| 69 | spt, err := adal.NewServicePrincipalTokenWithSecret(*oauthConf, clientID, resource, secret) |
| 70 | if err != nil { |
| 71 | return nil, fmt.Errorf("configuring service principal: %w", err) |
| 72 | } |
| 73 | return autorest.NewBearerAuthorizer(spt), nil |
| 74 | } |
| 75 | |
| 76 | // copied from keyvault/auth |
| 77 | func getResource() (string, error) { |
| 78 | var env azure.Environment |
| 79 | |
| 80 | if envName := os.Getenv("AZURE_ENVIRONMENT"); envName == "" { |
| 81 | env = azure.PublicCloud |
| 82 | } else { |
| 83 | var err error |
| 84 | env, err = azure.EnvironmentFromName(envName) |
| 85 | if err != nil { |
| 86 | return "", err |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | resource := os.Getenv("AZURE_KEYVAULT_RESOURCE") |
| 91 | if resource == "" { |
| 92 | resource = env.ResourceIdentifiers.KeyVault |
| 93 | } |
| 94 | |
| 95 | return resource, nil |
| 96 | } |
| 97 | |
| 98 | type bearerTokenFileSecret struct { |
| 99 | tokenFile string |
| 100 | } |
| 101 | |
| 102 | func (b *bearerTokenFileSecret) SetAuthenticationValues(spt *adal.ServicePrincipalToken, v *url.Values) error { |
| 103 | blob, err := os.ReadFile(b.tokenFile) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | bearerToken := string(bytes.TrimSpace(blob)) |
| 108 | |
| 109 | v.Set("client_assertion", bearerToken) |
| 110 | v.Set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer") |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | // MarshalJSON implements the json.Marshaler interface. |
| 115 | func (bearerTokenFileSecret) MarshalJSON() ([]byte, error) { |
| 116 | return nil, errors.New("not implemented") |
| 117 | } |