|
| 1 | +/* |
| 2 | +Copyright 2025 The Flux 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 azure |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "net/url" |
| 25 | + "path" |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" |
| 29 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 30 | + "github.com/golang-jwt/jwt/v5" |
| 31 | + corev1 "k8s.io/api/core/v1" |
| 32 | + |
| 33 | + "github.com/fluxcd/pkg/auth" |
| 34 | +) |
| 35 | + |
| 36 | +// ProviderName is the name of the Azure authentication provider. |
| 37 | +const ProviderName = "azure" |
| 38 | + |
| 39 | +// Provider implements the auth.Provider interface for Azure authentication. |
| 40 | +type Provider struct{} |
| 41 | + |
| 42 | +// GetName implements auth.Provider. |
| 43 | +func (Provider) GetName() string { |
| 44 | + return ProviderName |
| 45 | +} |
| 46 | + |
| 47 | +// NewDefaultToken implements auth.Provider. |
| 48 | +func (Provider) NewDefaultToken(ctx context.Context, opts ...auth.Option) (auth.Token, error) { |
| 49 | + var o auth.Options |
| 50 | + o.Apply(opts...) |
| 51 | + |
| 52 | + azOpts := &azidentity.DefaultAzureCredentialOptions{} |
| 53 | + |
| 54 | + if hc := o.GetHTTPClient(); hc != nil { |
| 55 | + azOpts.Transport = hc |
| 56 | + } |
| 57 | + |
| 58 | + cred, err := NewDefaultAzureCredential(azOpts) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + token, err := cred.GetToken(ctx, policy.TokenRequestOptions{ |
| 63 | + Scopes: getScopes(&o), |
| 64 | + }) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + return &Token{token}, nil |
| 70 | +} |
| 71 | + |
| 72 | +// GetAudience implements auth.Provider. |
| 73 | +func (Provider) GetAudience(context.Context, corev1.ServiceAccount) (string, error) { |
| 74 | + return "api://AzureADTokenExchange", nil |
| 75 | +} |
| 76 | + |
| 77 | +// GetIdentity implements auth.Provider. |
| 78 | +func (Provider) GetIdentity(serviceAccount corev1.ServiceAccount) (string, error) { |
| 79 | + return getIdentity(serviceAccount) |
| 80 | +} |
| 81 | + |
| 82 | +// NewTokenForServiceAccount implements auth.Provider. |
| 83 | +func (Provider) NewTokenForServiceAccount(ctx context.Context, oidcToken string, |
| 84 | + serviceAccount corev1.ServiceAccount, opts ...auth.Option) (auth.Token, error) { |
| 85 | + |
| 86 | + var o auth.Options |
| 87 | + o.Apply(opts...) |
| 88 | + |
| 89 | + identity, err := getIdentity(serviceAccount) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + s := strings.Split(identity, "/") |
| 94 | + tenantID, clientID := s[0], s[1] |
| 95 | + |
| 96 | + azOpts := &azidentity.ClientAssertionCredentialOptions{} |
| 97 | + |
| 98 | + if hc := o.GetHTTPClient(); hc != nil { |
| 99 | + azOpts.Transport = hc |
| 100 | + } |
| 101 | + |
| 102 | + cred, err := azidentity.NewClientAssertionCredential(tenantID, clientID, func(context.Context) (string, error) { |
| 103 | + return oidcToken, nil |
| 104 | + }, azOpts) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + token, err := cred.GetToken(ctx, policy.TokenRequestOptions{ |
| 109 | + Scopes: getScopes(&o), |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + return &Token{token}, nil |
| 116 | +} |
| 117 | + |
| 118 | +// GetArtifactCacheKey implements auth.Provider. |
| 119 | +func (Provider) GetArtifactCacheKey(artifactRepository string) string { |
| 120 | + return getACRHost(artifactRepository) |
| 121 | +} |
| 122 | + |
| 123 | +// NewArtifactRegistryToken implements auth.Provider. |
| 124 | +func (Provider) NewArtifactRegistryToken(ctx context.Context, artifactRepository string, |
| 125 | + accessToken auth.Token, opts ...auth.Option) (auth.Token, error) { |
| 126 | + |
| 127 | + t := accessToken.(*Token) |
| 128 | + |
| 129 | + var o auth.Options |
| 130 | + o.Apply(opts...) |
| 131 | + |
| 132 | + // Build request. |
| 133 | + registryURL := artifactRepository |
| 134 | + if !strings.HasPrefix(artifactRepository, "http") { |
| 135 | + registryURL = fmt.Sprintf("https://%s", getACRHost(artifactRepository)) |
| 136 | + } |
| 137 | + exchangeURL, err := url.Parse(registryURL) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + exchangeURL.Path = path.Join(exchangeURL.Path, "oauth2/exchange") |
| 142 | + parameters := url.Values{} |
| 143 | + parameters.Add("grant_type", "access_token") |
| 144 | + parameters.Add("service", exchangeURL.Hostname()) |
| 145 | + parameters.Add("access_token", t.Token) |
| 146 | + body := strings.NewReader(parameters.Encode()) |
| 147 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, exchangeURL.String(), body) |
| 148 | + if err != nil { |
| 149 | + return nil, err |
| 150 | + } |
| 151 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 152 | + |
| 153 | + // Send request. |
| 154 | + httpClient := http.DefaultClient |
| 155 | + if hc := o.GetHTTPClient(); hc != nil { |
| 156 | + httpClient = hc |
| 157 | + } |
| 158 | + resp, err := httpClient.Do(req) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + defer resp.Body.Close() |
| 163 | + |
| 164 | + // Parse response. |
| 165 | + if resp.StatusCode != http.StatusOK { |
| 166 | + return nil, fmt.Errorf("unexpected status from ACR exchange request: %d", resp.StatusCode) |
| 167 | + } |
| 168 | + var tokenResp struct { |
| 169 | + RefreshToken string `json:"refresh_token"` |
| 170 | + } |
| 171 | + if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + var claims jwt.MapClaims |
| 175 | + if _, _, err := jwt.NewParser().ParseUnverified(tokenResp.RefreshToken, &claims); err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + expiry, err := claims.GetExpirationTime() |
| 179 | + if err != nil { |
| 180 | + return nil, err |
| 181 | + } |
| 182 | + |
| 183 | + return &auth.ArtifactRegistryCredentials{ |
| 184 | + // https://docs.microsoft.com/en-us/azure/container-registry/container-registry-authentication?tabs=azure-cli#az-acr-login-with---expose-token |
| 185 | + Username: "00000000-0000-0000-0000-000000000000", |
| 186 | + Password: tokenResp.RefreshToken, |
| 187 | + ExpiresAt: expiry.Time, |
| 188 | + }, nil |
| 189 | +} |
0 commit comments