sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/token/client.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     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  
    17  package token
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
    23  	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    24  	"github.com/pkg/errors"
    25  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    26  	"sigs.k8s.io/cluster-api-provider-azure/util/tele"
    27  )
    28  
    29  // AzureClient to get azure active directory token.
    30  type AzureClient struct {
    31  	aadToken *azidentity.ClientSecretCredential
    32  }
    33  
    34  // NewClient creates a new azure active directory token client from an authorizer.
    35  func NewClient(auth azure.Authorizer) (*AzureClient, error) {
    36  	aadToken, err := newAzureActiveDirectoryTokenClient(auth.TenantID(),
    37  		auth.ClientID(),
    38  		auth.ClientSecret(),
    39  		auth.CloudEnvironment())
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  	return &AzureClient{
    44  		aadToken: aadToken,
    45  	}, nil
    46  }
    47  
    48  // newAzureActiveDirectoryTokenClient creates a new aad token client from an authorizer.
    49  func newAzureActiveDirectoryTokenClient(tenantID, clientID, clientSecret, envName string) (*azidentity.ClientSecretCredential, error) {
    50  	cliOpts, err := azure.ARMClientOptions(envName)
    51  	if err != nil {
    52  		return nil, errors.Wrap(err, "error while getting client options")
    53  	}
    54  	clientOptions := &azidentity.ClientSecretCredentialOptions{
    55  		ClientOptions: cliOpts.ClientOptions,
    56  	}
    57  	cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, clientOptions)
    58  	if err != nil {
    59  		return nil, errors.Wrap(err, "error while getting az client secret credentials")
    60  	}
    61  	return cred, nil
    62  }
    63  
    64  // GetAzureActiveDirectoryToken gets the token for authentication with azure active directory.
    65  func (ac *AzureClient) GetAzureActiveDirectoryToken(ctx context.Context, resourceID string) (string, error) {
    66  	ctx, _, done := tele.StartSpanWithLogger(ctx, "aadToken.GetToken")
    67  	defer done()
    68  
    69  	spnAccessToken, err := ac.aadToken.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{resourceID + "/.default"}})
    70  	if err != nil {
    71  		return "", errors.Wrap(err, "failed to get token")
    72  	}
    73  	return spnAccessToken.Token, nil
    74  }