-
Notifications
You must be signed in to change notification settings - Fork 86
/
azureclient.go
292 lines (240 loc) · 11.4 KB
/
azureclient.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package cloud
import (
"context"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"net/http"
"os"
"regexp"
"time"
azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2018-01-01-preview/authorization"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2016-06-01/subscriptions"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/azure/cli"
"github.com/microsoft/kiota/abstractions/go/authentication"
kiotaauth "github.com/microsoft/kiota/authentication/go/azure"
msgraphbetasdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
"github.com/microsoftgraph/msgraph-beta-sdk-go/models/microsoft/graph"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
// ref: https://docs.microsoft.com/en-us/graph/migrate-azure-ad-graph-request-differences#basic-requests
var msGraphEndpoint = map[azure.Environment]string{
azure.PublicCloud: "https://graph.microsoft.com/",
azure.USGovernmentCloud: "https://graph.microsoft.us/",
azure.ChinaCloud: "https://microsoftgraph.chinacloudapi.cn/",
azure.GermanCloud: "https://graph.microsoft.de/",
}
type Interface interface {
CreateServicePrincipal(ctx context.Context, appID string, tags []string) (*graph.ServicePrincipal, error)
CreateApplication(ctx context.Context, displayName string) (*graph.Application, error)
DeleteServicePrincipal(ctx context.Context, objectID string) error
DeleteApplication(ctx context.Context, objectID string) error
GetServicePrincipal(ctx context.Context, displayName string) (*graph.ServicePrincipal, error)
GetApplication(ctx context.Context, displayName string) (*graph.Application, error)
// Role assignment methods
CreateRoleAssignment(ctx context.Context, scope, roleName, principalID string) (authorization.RoleAssignment, error)
DeleteRoleAssignment(ctx context.Context, roleAssignmentID string) (authorization.RoleAssignment, error)
// Role definition methods
GetRoleDefinitionIDByName(ctx context.Context, scope, roleName string) (authorization.RoleDefinition, error)
// Federation methods
AddFederatedCredential(ctx context.Context, objectID string, fic *graph.FederatedIdentityCredential) error
GetFederatedCredential(ctx context.Context, objectID, issuer, subject string) (*graph.FederatedIdentityCredential, error)
DeleteFederatedCredential(ctx context.Context, objectID, federatedCredentialID string) error
}
type AzureClient struct {
environment azure.Environment
subscriptionID string
graphServiceClient *msgraphbetasdk.GraphServiceClient
roleAssignmentsClient authorization.RoleAssignmentsClient
roleDefinitionsClient authorization.RoleDefinitionsClient
}
// NewAzureClientWithCLI creates an AzureClient configured from Azure CLI 2.0 for local development scenarios.
func NewAzureClientWithCLI(env azure.Environment, subscriptionID, tenantID string) (*AzureClient, error) {
_, tenantID, err := getOAuthConfig(env, subscriptionID, tenantID)
if err != nil {
return nil, err
}
token, err := cli.GetTokenFromCLI(env.ResourceManagerEndpoint)
if err != nil {
return nil, err
}
adalToken, err := token.ToADALToken()
if err != nil {
return nil, err
}
cred, err := azidentity.NewAzureCLICredential(nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create credential")
}
auth, err := kiotaauth.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{getGraphScope(env)})
if err != nil {
return nil, errors.Wrap(err, "failed to create authentication provider")
}
return getClient(env, subscriptionID, tenantID, autorest.NewBearerAuthorizer(&adalToken), auth)
}
// NewAzureClientWithClientSecret returns an AzureClient via client_id and client_secret
func NewAzureClientWithClientSecret(env azure.Environment, subscriptionID, clientID, clientSecret, tenantID string) (*AzureClient, error) {
oauthConfig, tenantID, err := getOAuthConfig(env, subscriptionID, tenantID)
if err != nil {
return nil, err
}
armSpt, err := adal.NewServicePrincipalToken(*oauthConfig, clientID, clientSecret, env.ServiceManagementEndpoint)
if err != nil {
return nil, err
}
cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create credential")
}
auth, err := kiotaauth.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{getGraphScope(env)})
if err != nil {
return nil, errors.Wrap(err, "failed to create authentication provider")
}
return getClient(env, subscriptionID, tenantID, autorest.NewBearerAuthorizer(armSpt), auth)
}
// NewAzureClientWithClientCertificateFile returns an AzureClient via client_id and jwt certificate assertion
func NewAzureClientWithClientCertificateFile(env azure.Environment, subscriptionID, clientID, tenantID, certificatePath, privateKeyPath string) (*AzureClient, error) {
certificateData, err := os.ReadFile(certificatePath)
if err != nil {
return nil, errors.Wrap(err, "Failed to read certificate")
}
block, _ := pem.Decode(certificateData)
if block == nil {
return nil, errors.New("Failed to decode pem block from certificate")
}
certificate, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse certificate")
}
privateKey, err := parseRsaPrivateKey(privateKeyPath)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse rsa private key")
}
return NewAzureClientWithClientCertificate(env, subscriptionID, clientID, tenantID, certificate, privateKey)
}
// NewAzureClientWithClientCertificate returns an AzureClient via client_id and jwt certificate assertion
func NewAzureClientWithClientCertificate(env azure.Environment, subscriptionID, clientID, tenantID string, certificate *x509.Certificate, privateKey *rsa.PrivateKey) (*AzureClient, error) {
oauthConfig, tenantID, err := getOAuthConfig(env, subscriptionID, tenantID)
if err != nil {
return nil, err
}
return newAzureClientWithCertificate(env, oauthConfig, subscriptionID, clientID, tenantID, certificate, privateKey)
}
// NewAzureClientWithClientCertificateExternalTenant returns an AzureClient via client_id and jwt certificate assertion against a 3rd party tenant
func NewAzureClientWithClientCertificateExternalTenant(env azure.Environment, subscriptionID, tenantID, clientID string, certificate *x509.Certificate, privateKey *rsa.PrivateKey) (*AzureClient, error) {
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, tenantID)
if err != nil {
return nil, err
}
return newAzureClientWithCertificate(env, oauthConfig, subscriptionID, clientID, tenantID, certificate, privateKey)
}
func getOAuthConfig(env azure.Environment, subscriptionID, tenantID string) (*adal.OAuthConfig, string, error) {
oauthConfig, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, tenantID)
if err != nil {
return nil, "", err
}
return oauthConfig, tenantID, nil
}
func newAzureClientWithCertificate(env azure.Environment, oauthConfig *adal.OAuthConfig, subscriptionID, clientID, tenantID string, certificate *x509.Certificate, privateKey *rsa.PrivateKey) (*AzureClient, error) {
if certificate == nil {
return nil, errors.New("certificate should not be nil")
}
if privateKey == nil {
return nil, errors.New("privateKey should not be nil")
}
armSpt, err := adal.NewServicePrincipalTokenFromCertificate(*oauthConfig, clientID, certificate, privateKey, env.ServiceManagementEndpoint)
if err != nil {
return nil, err
}
cred, err := azidentity.NewClientCertificateCredential(tenantID, clientID, []*x509.Certificate{certificate}, privateKey, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create credential")
}
auth, err := kiotaauth.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{getGraphScope(env)})
if err != nil {
return nil, errors.Wrap(err, "failed to create authentication provider")
}
return getClient(env, subscriptionID, tenantID, autorest.NewBearerAuthorizer(armSpt), auth)
}
func getClient(env azure.Environment, subscriptionID, tenantID string, armAuthorizer autorest.Authorizer, auth authentication.AuthenticationProvider) (*AzureClient, error) {
adapter, err := msgraphbetasdk.NewGraphRequestAdapter(auth)
if err != nil {
return nil, errors.Wrap(err, "failed to create request adapter")
}
azClient := &AzureClient{
environment: env,
subscriptionID: subscriptionID,
graphServiceClient: msgraphbetasdk.NewGraphServiceClient(adapter),
roleAssignmentsClient: authorization.NewRoleAssignmentsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
roleDefinitionsClient: authorization.NewRoleDefinitionsClientWithBaseURI(env.ResourceManagerEndpoint, subscriptionID),
}
azClient.roleAssignmentsClient.Authorizer = armAuthorizer
azClient.roleDefinitionsClient.Authorizer = armAuthorizer
return azClient, nil
}
// GetTenantID figures out the AAD tenant ID of the subscription by making an
// unauthenticated request to the Get Subscription Details endpoint and parses
// the value from WWW-Authenticate header.
// TODO this should probably to to the armhelpers library
func GetTenantID(resourceManagerEndpoint string, subscriptionID string) (string, error) {
const hdrKey = "WWW-Authenticate"
c := subscriptions.NewClientWithBaseURI(resourceManagerEndpoint)
log.Debugf("Resolving tenantID for subscriptionID: %s", subscriptionID)
// we expect this request to fail (err != nil), but we are only interested
// in headers, so surface the error if the Response is not present (i.e.
// network error etc)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*150)
defer cancel()
subs, err := c.Get(ctx, subscriptionID)
if subs.Response.Response == nil {
return "", errors.Wrap(err, "Request failed")
}
// Expecting 401 StatusUnauthorized here, just read the header
if subs.StatusCode != http.StatusUnauthorized {
return "", errors.Errorf("Unexpected response from Get Subscription: %v", subs.StatusCode)
}
hdr := subs.Header.Get(hdrKey)
if hdr == "" {
return "", errors.Errorf("Header %v not found in Get Subscription response", hdrKey)
}
// Example value for hdr:
// Bearer authorization_uri="https://login.windows.net/996fe9d1-6171-40aa-945b-4c64b63bf655", error="invalid_token", error_description="The authentication failed because of missing 'Authorization' header."
r := regexp.MustCompile(`authorization_uri=".*/([0-9a-f\-]+)"`)
m := r.FindStringSubmatch(hdr)
if m == nil {
return "", errors.Errorf("Could not find the tenant ID in header: %s %q", hdrKey, hdr)
}
return m[1], nil
}
func parseRsaPrivateKey(path string) (*rsa.PrivateKey, error) {
privateKeyData, err := os.ReadFile(path)
if err != nil {
return nil, err
}
block, _ := pem.Decode(privateKeyData)
if block == nil {
return nil, errors.New("Failed to decode a pem block from private key")
}
privatePkcs1Key, errPkcs1 := x509.ParsePKCS1PrivateKey(block.Bytes)
if errPkcs1 == nil {
return privatePkcs1Key, nil
}
privatePkcs8Key, errPkcs8 := x509.ParsePKCS8PrivateKey(block.Bytes)
if errPkcs8 == nil {
privatePkcs8RsaKey, ok := privatePkcs8Key.(*rsa.PrivateKey)
if !ok {
return nil, errors.New("pkcs8 contained non-RSA key. Expected RSA key")
}
return privatePkcs8RsaKey, nil
}
return nil, errors.Errorf("failed to parse private key as Pkcs#1 or Pkcs#8. (%s). (%s)", errPkcs1, errPkcs8)
}
func getGraphScope(env azure.Environment) string {
return fmt.Sprintf("%s.default", msGraphEndpoint[env])
}