-
Notifications
You must be signed in to change notification settings - Fork 170
/
authorizer.go
66 lines (54 loc) · 2.07 KB
/
authorizer.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
60
61
62
63
64
65
66
package clusterauthorizer
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
"net/http"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/go-autorest/autorest"
"github.com/jongio/azidext/go/azidext"
"github.com/sirupsen/logrus"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/util/azureclient"
)
type Credentials struct {
ClientID []byte
ClientSecret []byte
TenantID []byte
}
type azRefreshableAuthorizer struct {
log *logrus.Entry
azureEnvironment *azureclient.AROEnvironment
client client.Client
getTokenCredential func(*azureclient.AROEnvironment) (azcore.TokenCredential, error)
}
// NewAzRefreshableAuthorizer returns a new refreshable authorizer
// using Cluster Service Principal.
func NewAzRefreshableAuthorizer(log *logrus.Entry, azEnv *azureclient.AROEnvironment, client client.Client) (*azRefreshableAuthorizer, error) {
if log == nil {
return nil, fmt.Errorf("log entry cannot be nil")
}
if azEnv == nil {
return nil, fmt.Errorf("azureEnvironment cannot be nil")
}
return &azRefreshableAuthorizer{
log: log,
azureEnvironment: azEnv,
client: client,
getTokenCredential: GetTokenCredential,
}, nil
}
func (a *azRefreshableAuthorizer) NewRefreshableAuthorizerToken(ctx context.Context) (autorest.Authorizer, error) {
tokenCredential, err := a.getTokenCredential(a.azureEnvironment)
if err != nil {
return nil, api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidServicePrincipalCredentials, "properties.servicePrincipalProfile", "the provided service principal is invalid")
}
scopes := []string{a.azureEnvironment.ResourceManagerScope}
return azidext.NewTokenCredentialAdapter(tokenCredential, scopes), nil
}
func GetTokenCredential(environment *azureclient.AROEnvironment) (azcore.TokenCredential, error) {
return azidentity.NewDefaultAzureCredential(environment.DefaultAzureCredentialOptions())
}