|
| 1 | +// Copyright (c) 2026 Riptides Labs, Inc. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package vault |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "time" |
| 9 | + |
| 10 | + "emperror.dev/errors" |
| 11 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 12 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" |
| 13 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 14 | + "github.com/cenkalti/backoff/v5" |
| 15 | + "github.com/go-logr/logr" |
| 16 | + |
| 17 | + "go.riptides.io/tokenex/pkg/credential" |
| 18 | + "go.riptides.io/tokenex/pkg/util" |
| 19 | +) |
| 20 | + |
| 21 | +// VaultAzureSecret represents the structure of the secret data returned by Vault's Azure secrets engine when configured to return Azure credentials. |
| 22 | +type vaultAzureSecret struct { |
| 23 | + ClientID string `mapstructure:"client_id"` |
| 24 | + ClientSecret string `mapstructure:"client_secret"` |
| 25 | +} |
| 26 | + |
| 27 | +type azureAccessTokenProvider struct { |
| 28 | + tenantID string |
| 29 | + clientID string |
| 30 | + clientSecret string |
| 31 | + scopes []string |
| 32 | + |
| 33 | + logger logr.Logger |
| 34 | +} |
| 35 | + |
| 36 | +// GetCredentials begins the process of exchanging the client ID and client secret for an Azure access token and refreshing it as needed until the context is canceled. |
| 37 | +func (r *azureAccessTokenProvider) GetCredentials(ctx context.Context, credsChan chan credential.Result) { |
| 38 | + b := backoff.NewExponentialBackOff() |
| 39 | + |
| 40 | + for { |
| 41 | + azClientCreds, err := azidentity.NewClientSecretCredential(r.tenantID, r.clientID, r.clientSecret, nil) |
| 42 | + if err != nil { |
| 43 | + util.SendErrorToChannel(credsChan, errors.WrapIf(err, "failed to create Azure client secret credential")) |
| 44 | + |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + token, err := backoff.Retry(ctx, func() (azcore.AccessToken, error) { |
| 49 | + token, err := azClientCreds.GetToken(ctx, policy.TokenRequestOptions{ |
| 50 | + Scopes: r.scopes, |
| 51 | + }) |
| 52 | + |
| 53 | + return token, err |
| 54 | + }, backoff.WithBackOff(b), backoff.WithMaxElapsedTime(30*time.Second)) |
| 55 | + if err != nil { |
| 56 | + util.SendErrorToChannel(credsChan, errors.WrapIf(err, "failed to get Azure access token from client secret credential")) |
| 57 | + |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + // Calculate when to refresh |
| 62 | + timeUntilExpiry := time.Until(token.ExpiresOn) |
| 63 | + |
| 64 | + // If credentials are already expired, this is an error |
| 65 | + if timeUntilExpiry <= 0 { |
| 66 | + util.SendErrorToChannel(credsChan, errors.NewWithDetails("received already expired access token from Azure", "expiresAt", token.ExpiresOn)) |
| 67 | + |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + // Send credentials |
| 72 | + azureCredential := &credential.Oauth2Creds{ |
| 73 | + AccessToken: token.Token, |
| 74 | + TokenType: "Bearer", |
| 75 | + Expiry: token.ExpiresOn, |
| 76 | + } |
| 77 | + util.SendToChannel(credsChan, credential.Result{ |
| 78 | + Credential: azureCredential, |
| 79 | + Err: nil, |
| 80 | + Event: credential.UpdateEventType, |
| 81 | + }) |
| 82 | + r.logger.V(2).Info("Sent credentials", "expires", token.ExpiresOn) |
| 83 | + |
| 84 | + refreshBuffer := util.CalculateRefreshBuffer(timeUntilExpiry) |
| 85 | + refreshTime := timeUntilExpiry - refreshBuffer |
| 86 | + |
| 87 | + if !token.RefreshOn.IsZero() { |
| 88 | + // if refresh time is recommended in the received token, use that |
| 89 | + r.logger.V(2).Info("Using RefreshOn time from token", "refreshOn", token.RefreshOn) |
| 90 | + |
| 91 | + rt := time.Until(token.RefreshOn) |
| 92 | + if rt > 0 { |
| 93 | + refreshTime = rt |
| 94 | + } else { |
| 95 | + r.logger.V(2).Info("RefreshOn time is in the past, using calculated refresh time", "refreshTime", refreshTime) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + select { |
| 100 | + case <-ctx.Done(): |
| 101 | + r.logger.V(1).Info("Context cancelled, stopping credential refresh") |
| 102 | + |
| 103 | + return |
| 104 | + case <-time.After(refreshTime): |
| 105 | + // Continue to next iteration to refresh |
| 106 | + r.logger.V(2).Info("Refreshing access token") |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments