/
authorizers.go
212 lines (171 loc) · 5.29 KB
/
authorizers.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) Microsoft and contributors. All rights reserved.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
package iam
import (
"fmt"
"net/url"
"strings"
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/config"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure/auth"
)
var (
armAuthorizer autorest.Authorizer
batchAuthorizer autorest.Authorizer
graphAuthorizer autorest.Authorizer
keyvaultAuthorizer autorest.Authorizer
)
// OAuthGrantType specifies which grant type to use.
type OAuthGrantType int
const (
// OAuthGrantTypeServicePrincipal for client credentials flow
OAuthGrantTypeServicePrincipal OAuthGrantType = iota
// OAuthGrantTypeDeviceFlow for device flow
OAuthGrantTypeDeviceFlow
)
// GrantType returns what grant type has been configured.
func grantType() OAuthGrantType {
if config.UseDeviceFlow() {
return OAuthGrantTypeDeviceFlow
}
return OAuthGrantTypeServicePrincipal
}
// GetResourceManagementAuthorizer gets an OAuthTokenAuthorizer for Azure Resource Manager
func GetResourceManagementAuthorizer() (autorest.Authorizer, error) {
if armAuthorizer != nil {
return armAuthorizer, nil
}
var a autorest.Authorizer
var err error
a, err = getAuthorizerForResource(
grantType(), config.Environment().ResourceManagerEndpoint)
if err == nil {
// cache
armAuthorizer = a
} else {
// clear cache
armAuthorizer = nil
}
return armAuthorizer, err
}
// GetBatchAuthorizer gets an OAuthTokenAuthorizer for Azure Batch.
func GetBatchAuthorizer() (autorest.Authorizer, error) {
if batchAuthorizer != nil {
return batchAuthorizer, nil
}
var a autorest.Authorizer
var err error
a, err = getAuthorizerForResource(
grantType(), config.Environment().BatchManagementEndpoint)
if err == nil {
// cache
batchAuthorizer = a
} else {
// clear cache
batchAuthorizer = nil
}
return batchAuthorizer, err
}
// GetGraphAuthorizer gets an OAuthTokenAuthorizer for graphrbac API.
func GetGraphAuthorizer() (autorest.Authorizer, error) {
if graphAuthorizer != nil {
return graphAuthorizer, nil
}
var a autorest.Authorizer
var err error
a, err = getAuthorizerForResource(grantType(), config.Environment().GraphEndpoint)
if err == nil {
// cache
graphAuthorizer = a
} else {
graphAuthorizer = nil
}
return graphAuthorizer, err
}
// GetKeyvaultAuthorizer gets an OAuthTokenAuthorizer for use with Key Vault
// keys and secrets. Note that Key Vault *Vaults* are managed by Azure Resource
// Manager.
func GetKeyvaultAuthorizer() (autorest.Authorizer, error) {
if keyvaultAuthorizer != nil {
return keyvaultAuthorizer, nil
}
// BUG: default value for KeyVaultEndpoint is wrong
vaultEndpoint := strings.TrimSuffix(config.Environment().KeyVaultEndpoint, "/")
// BUG: alternateEndpoint replaces other endpoints in the configs below
alternateEndpoint, _ := url.Parse(
"https://login.windows.net/" + config.TenantID() + "/oauth2/token")
var a autorest.Authorizer
var err error
switch grantType() {
case OAuthGrantTypeServicePrincipal:
oauthconfig, err := adal.NewOAuthConfig(
config.Environment().ActiveDirectoryEndpoint, config.TenantID())
if err != nil {
return a, err
}
oauthconfig.AuthorizeEndpoint = *alternateEndpoint
token, err := adal.NewServicePrincipalToken(
*oauthconfig, config.ClientID(), config.ClientSecret(), vaultEndpoint)
if err != nil {
return a, err
}
a = autorest.NewBearerAuthorizer(token)
case OAuthGrantTypeDeviceFlow:
deviceConfig := auth.NewDeviceFlowConfig(config.ClientID(), config.TenantID())
deviceConfig.Resource = vaultEndpoint
deviceConfig.AADEndpoint = alternateEndpoint.String()
a, err = deviceConfig.Authorizer()
default:
return a, fmt.Errorf("invalid grant type specified")
}
if err == nil {
keyvaultAuthorizer = a
} else {
keyvaultAuthorizer = nil
}
return keyvaultAuthorizer, err
}
func getAuthorizerForResource(grantType OAuthGrantType, resource string) (autorest.Authorizer, error) {
var a autorest.Authorizer
var err error
switch grantType {
case OAuthGrantTypeServicePrincipal:
oauthConfig, err := adal.NewOAuthConfig(
config.Environment().ActiveDirectoryEndpoint, config.TenantID())
if err != nil {
return nil, err
}
token, err := adal.NewServicePrincipalToken(
*oauthConfig, config.ClientID(), config.ClientSecret(), resource)
if err != nil {
return nil, err
}
a = autorest.NewBearerAuthorizer(token)
case OAuthGrantTypeDeviceFlow:
deviceconfig := auth.NewDeviceFlowConfig(config.ClientID(), config.TenantID())
deviceconfig.Resource = resource
a, err = deviceconfig.Authorizer()
if err != nil {
return nil, err
}
default:
return a, fmt.Errorf("invalid grant type specified")
}
return a, err
}
// GetResourceManagementTokenHybrid retrieves auth token for hybrid environment
func GetResourceManagementTokenHybrid(activeDirectoryEndpoint, tokenAudience string) (adal.OAuthTokenProvider, error) {
oauthConfig, err := adal.NewOAuthConfig(activeDirectoryEndpoint, config.TenantID())
if err != nil {
return nil, err
}
tokenProvider, err := adal.NewServicePrincipalToken(
*oauthConfig,
config.ClientID(),
config.ClientSecret(),
tokenAudience)
return tokenProvider, err
}