@@ -9,20 +9,25 @@ import (
9
9
"github.com/pkg/errors"
10
10
"github.com/spf13/pflag"
11
11
"k8s.io/client-go/rest"
12
+
13
+ apisv1alpha1 "github.com/crossplane-contrib/provider-kubernetes/apis/v1alpha1"
12
14
)
13
15
14
16
// Credentials Secret content is a json whose keys are below.
15
17
const (
16
- CredentialsKeyClientID = "clientId"
17
- CredentialsKeyClientSecret = "clientSecret"
18
- CredentialsKeyTenantID = "tenantId"
19
- CredentialsKeyClientCert = "clientCertificate"
20
- CredentialsKeyClientCertPass = "clientCertificatePassword"
18
+ CredentialsKeyClientID = "clientId"
19
+ CredentialsKeyClientSecret = "clientSecret"
20
+ CredentialsKeyTenantID = "tenantId"
21
+ CredentialsKeyClientCert = "clientCertificate"
22
+ CredentialsKeyClientCertPass = "clientCertificatePassword"
23
+ CredentialsKeyFederatedTokenFile = "federatedTokenFile"
24
+ CredentialsKeyAuthorityHost = "authorityHost"
25
+ CredentialsKeyServerID = "serverId"
21
26
)
22
27
23
28
// WrapRESTConfig configures the supplied REST config to use OAuth2 bearer
24
29
// tokens fetched using the supplied Azure Credentials.
25
- func WrapRESTConfig (_ context.Context , rc * rest.Config , credentials []byte , _ ... string ) error {
30
+ func WrapRESTConfig (_ context.Context , rc * rest.Config , credentials []byte , identityType apisv1alpha1. IdentityType , _ ... string ) error { // nolint:gocyclo // todo: refactor
26
31
m := map [string ]string {}
27
32
if err := json .Unmarshal (credentials , & m ); err != nil {
28
33
return err
@@ -44,21 +49,30 @@ func WrapRESTConfig(_ context.Context, rc *rest.Config, credentials []byte, _ ..
44
49
return errors .Wrap (err , "could not parse execProvider arguments in kubeconfig" )
45
50
}
46
51
rc .ExecProvider = nil
47
- // TODO: support other login methods like MSI, Workload Identity in the future
48
- opts .LoginMethod = token .ServicePrincipalLogin
49
- opts .ClientID = m [CredentialsKeyClientID ]
50
- opts .ClientSecret = m [CredentialsKeyClientSecret ]
51
- opts .TenantID = m [CredentialsKeyTenantID ]
52
- if cert , ok := m [CredentialsKeyClientCert ]; ok {
53
- opts .ClientCert = cert
54
- if certpass , ok2 := m [CredentialsKeyClientCertPass ]; ok2 {
55
- opts .ClientCertPassword = certpass
52
+ switch identityType {
53
+ case apisv1alpha1 .IdentityTypeAzureServicePrincipalCredentials :
54
+ opts .LoginMethod = token .ServicePrincipalLogin
55
+ opts .ClientID = m [CredentialsKeyClientID ]
56
+ opts .ClientSecret = m [CredentialsKeyClientSecret ]
57
+ opts .TenantID = m [CredentialsKeyTenantID ]
58
+ if cert , ok := m [CredentialsKeyClientCert ]; ok {
59
+ opts .ClientCert = cert
60
+ if certpass , ok2 := m [CredentialsKeyClientCertPass ]; ok2 {
61
+ opts .ClientCertPassword = certpass
62
+ }
56
63
}
64
+ case apisv1alpha1 .IdentityTypeAzureWorkloadIdentityCredentials :
65
+ opts .LoginMethod = token .WorkloadIdentityLogin
66
+ opts .ClientID = m [CredentialsKeyClientID ]
67
+ opts .TenantID = m [CredentialsKeyTenantID ]
68
+ opts .ServerID = m [CredentialsKeyServerID ]
69
+ opts .FederatedTokenFile = m [CredentialsKeyFederatedTokenFile ]
70
+ opts .AuthorityHost = m [CredentialsKeyAuthorityHost ]
57
71
}
58
72
59
73
p , err := token .NewTokenProvider (& opts )
60
74
if err != nil {
61
- return errors .New ( "cannot build azure token provider" )
75
+ return errors .Wrap ( err , "cannot build azure token provider" )
62
76
}
63
77
64
78
rc .Wrap (func (rt http.RoundTripper ) http.RoundTripper {
0 commit comments