-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5815 from lodrem/arm/sdk/keyvault-credential
Introduce KeyVaultCredential for SDK v2
- Loading branch information
Showing
7 changed files
with
214 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package armauth | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
) | ||
|
||
const ( | ||
HeaderAuthorizationAuxiliary = "x-ms-authorization-auxiliary" | ||
) | ||
|
||
type AuxiliaryAuthPolicy struct { | ||
credentials []azcore.TokenCredential | ||
scope string | ||
} | ||
|
||
func NewAuxiliaryAuthPolicy(credentials []azcore.TokenCredential, scope string) *AuxiliaryAuthPolicy { | ||
return &AuxiliaryAuthPolicy{ | ||
credentials: credentials, | ||
scope: scope, | ||
} | ||
} | ||
|
||
func (p *AuxiliaryAuthPolicy) Do(req *policy.Request) (*http.Response, error) { | ||
tokens := make([]string, 0, len(p.credentials)) | ||
|
||
for _, cred := range p.credentials { | ||
token, err := cred.GetToken(context.TODO(), policy.TokenRequestOptions{ | ||
Scopes: []string{p.scope}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tokens = append(tokens, fmt.Sprintf("Bearer %s", token.Token)) | ||
} | ||
req.Raw().Header.Set(HeaderAuthorizationAuxiliary, strings.Join(tokens, ", ")) | ||
return req.Next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package armauth | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
"github.com/Azure/azure-sdk-for-go/sdk/keyvault/azsecrets" | ||
) | ||
|
||
type KeyVaultCredential struct { | ||
secretClient *azsecrets.Client | ||
secretPath string | ||
|
||
token *azcore.AccessToken | ||
} | ||
|
||
type KeyVaultCredentialSecret struct { | ||
AccessToken string `json:"access_token"` | ||
ExpiresOn time.Time `json:"expires_on"` | ||
} | ||
|
||
func NewKeyVaultCredential( | ||
msiCredential azcore.TokenCredential, | ||
keyVaultURL string, | ||
secretName string, | ||
) (*KeyVaultCredential, error) { | ||
cli, err := azsecrets.NewClient(keyVaultURL, msiCredential, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("create KeyVault client: %w", err) | ||
} | ||
|
||
rv := &KeyVaultCredential{ | ||
secretClient: cli, | ||
secretPath: secretName, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
if err := rv.refreshToken(ctx); err != nil { | ||
return nil, fmt.Errorf("refresh token: %w", err) | ||
} | ||
|
||
return rv, nil | ||
} | ||
|
||
func (c *KeyVaultCredential) refreshToken(ctx context.Context) error { | ||
const LatestVersion = "" | ||
|
||
resp, err := c.secretClient.GetSecret(ctx, c.secretPath, LatestVersion, nil) | ||
if err != nil { | ||
return err | ||
} | ||
if resp.Value == nil { | ||
return fmt.Errorf("secret value is nil") | ||
} | ||
|
||
var secret KeyVaultCredentialSecret | ||
if err := json.Unmarshal([]byte(*resp.Value), &secret); err != nil { | ||
return fmt.Errorf("unmarshal secret value `%s`: %w", *resp.Value, err) | ||
} | ||
|
||
c.token = &azcore.AccessToken{ | ||
Token: secret.AccessToken, | ||
ExpiresOn: secret.ExpiresOn, | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *KeyVaultCredential) GetToken(ctx context.Context, opts policy.TokenRequestOptions) (azcore.AccessToken, error) { | ||
const RefreshTokenOffset = 5 * time.Minute | ||
|
||
if c.token != nil && c.token.ExpiresOn.Add(RefreshTokenOffset).Before(time.Now()) { | ||
return *c.token, nil | ||
} | ||
|
||
if err := c.refreshToken(ctx); err != nil { | ||
return azcore.AccessToken{}, fmt.Errorf("refresh token: %w", err) | ||
} | ||
|
||
return *c.token, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters