-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathazure.go
59 lines (51 loc) · 1.8 KB
/
azure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package azure
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/Azure/azure-sdk-for-go/profiles/preview/preview/containerregistry/runtime/containerregistry"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
type Registry struct {
domain string
}
const (
azureURL = ".azurecr.io"
scope = "https://management.azure.com/.default"
scheme = "https"
)
func (r *Registry) CheckOptions(domain string, _ types.RegistryOptions) error {
if !strings.HasSuffix(domain, azureURL) {
return xerrors.Errorf("Azure registry: %w", types.InvalidURLPattern)
}
r.domain = domain
return nil
}
func (r *Registry) GetCredential(ctx context.Context) (string, string, error) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return "", "", xerrors.Errorf("unable to generate acr credential error: %w", err)
}
aadToken, err := cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{scope}})
if err != nil {
return "", "", xerrors.Errorf("unable to get an access token: %w", err)
}
rt, err := refreshToken(ctx, aadToken.Token, r.domain)
if err != nil {
return "", "", xerrors.Errorf("unable to refresh token: %w", err)
}
return "00000000-0000-0000-0000-000000000000", *rt.RefreshToken, err
}
func refreshToken(ctx context.Context, accessToken, domain string) (containerregistry.RefreshToken, error) {
tenantID := os.Getenv("AZURE_TENANT_ID")
if tenantID == "" {
return containerregistry.RefreshToken{}, errors.New("missing environment variable AZURE_TENANT_ID")
}
repoClient := containerregistry.NewRefreshTokensClient(fmt.Sprintf("%s://%s", scheme, domain))
return repoClient.GetFromExchange(ctx, "access_token", domain, tenantID, "", accessToken)
}