...

Source file src/github.com/chrismellard/docker-credential-acr-env/pkg/token/token.go

Documentation: github.com/chrismellard/docker-credential-acr-env/pkg/token

     1  /*
     2  Copyright © 2020 Chris Mellard chris.mellard@icloud.com
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package token
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/Azure/go-autorest/autorest/adal"
    23  	"github.com/Azure/go-autorest/autorest/azure/auth"
    24  )
    25  
    26  func GetServicePrincipalTokenFromEnvironment() (*adal.ServicePrincipalToken, auth.EnvironmentSettings, error) {
    27  	settings, err := auth.GetSettingsFromEnvironment()
    28  	if err != nil {
    29  		return &adal.ServicePrincipalToken{}, auth.EnvironmentSettings{}, fmt.Errorf("failed to get auth settings from environment - %w", err)
    30  	}
    31  
    32  	spToken, err := getServicePrincipalToken(settings, settings.Environment.ResourceManagerEndpoint)
    33  	if err != nil {
    34  		return &adal.ServicePrincipalToken{}, auth.EnvironmentSettings{}, fmt.Errorf("failed to initialise sp token config %w", err)
    35  	}
    36  
    37  	return spToken, settings, nil
    38  }
    39  
    40  // getServicePrincipalToken retrieves an Azure AD OAuth2 token from the supplied environment settings for the specified resource
    41  func getServicePrincipalToken(settings auth.EnvironmentSettings, resource string) (*adal.ServicePrincipalToken, error) {
    42  
    43  	//1.Client Credentials
    44  	if _, e := settings.GetClientCredentials(); e == nil {
    45  		clientCredentialsConfig, err := settings.GetClientCredentials()
    46  		if err != nil {
    47  			return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to get client credentials settings from environment - %w", err)
    48  		}
    49  		oAuthConfig, err := adal.NewOAuthConfig(settings.Environment.ActiveDirectoryEndpoint, clientCredentialsConfig.TenantID)
    50  		if err != nil {
    51  			return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to initialise OAuthConfig - %w", err)
    52  		}
    53  		return adal.NewServicePrincipalToken(*oAuthConfig, clientCredentialsConfig.ClientID, clientCredentialsConfig.ClientSecret, clientCredentialsConfig.Resource)
    54  	}
    55  
    56  	//2. Client Certificate
    57  	if _, e := settings.GetClientCertificate(); e == nil {
    58  		return &adal.ServicePrincipalToken{}, fmt.Errorf("authentication method currently unsupported")
    59  	}
    60  
    61  	//3. Username Password
    62  	if _, e := settings.GetUsernamePassword(); e == nil {
    63  		return &adal.ServicePrincipalToken{}, fmt.Errorf("authentication method currently unsupported")
    64  	}
    65  
    66  	// federated OIDC JWT assertion
    67  	jwt, err := jwtLookup()
    68  	if err == nil {
    69  		clientID, isPresent := os.LookupEnv("AZURE_CLIENT_ID")
    70  		if !isPresent {
    71  			return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to get client id from environment")
    72  		}
    73  		tenantID, isPresent := os.LookupEnv("AZURE_TENANT_ID")
    74  		if !isPresent {
    75  			return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to get client id from environment")
    76  		}
    77  
    78  		oAuthConfig, err := adal.NewOAuthConfig(settings.Environment.ActiveDirectoryEndpoint, tenantID)
    79  		if err != nil {
    80  			return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to initialise OAuthConfig - %w", err)
    81  		}
    82  
    83  		return adal.NewServicePrincipalTokenFromFederatedToken(*oAuthConfig, clientID, *jwt, resource)
    84  	}
    85  
    86  	// 4. MSI
    87  	return adal.NewServicePrincipalTokenFromManagedIdentity(resource, &adal.ManagedIdentityOptions{
    88  		ClientID: os.Getenv("AZURE_CLIENT_ID"),
    89  	})
    90  }
    91  
    92  func jwtLookup() (*string, error) {
    93  	jwt, isPresent := os.LookupEnv("AZURE_FEDERATED_TOKEN")
    94  	if isPresent {
    95  		return &jwt, nil
    96  	}
    97  
    98  	if jwtFile, isPresent := os.LookupEnv("AZURE_FEDERATED_TOKEN_FILE"); isPresent {
    99  		jwtBytes, err := os.ReadFile(jwtFile)
   100  		if err != nil {
   101  			return nil, err
   102  		}
   103  		jwt = string(jwtBytes)
   104  		return &jwt, nil
   105  	}
   106  
   107  	return nil, fmt.Errorf("no JWT found")
   108  }
   109  

View as plain text