-
Notifications
You must be signed in to change notification settings - Fork 880
/
main.go
93 lines (83 loc) · 2.85 KB
/
main.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
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.
package main
import (
"context"
"fmt"
armauth "github.com/Azure/azure-sdk-for-go/services/authorization/mgmt/2015-07-01/authorization"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/pulumi/pulumi-azure-native-sdk/authorization/v2"
"github.com/pulumi/pulumi-azure-native-sdk/containerregistry/v2"
"github.com/pulumi/pulumi-azure-native-sdk/resources/v2"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Create an Azure Resource Group
resourceGroup, err := resources.NewResourceGroup(ctx, "resourceGroup", nil)
if err != nil {
return err
}
registry, err := containerregistry.NewRegistry(ctx, "registry", &containerregistry.RegistryArgs{
ResourceGroupName: resourceGroup.Name,
Sku: containerregistry.SkuArgs{Name: pulumi.String("Basic")},
AdminUserEnabled: pulumi.Bool(true),
})
if err != nil {
return err
}
clientConfig, err := authorization.GetClientConfig(ctx)
if err != nil {
return err
}
currentPrincipal := clientConfig.ObjectId
roleDef, err := getRoleIdByName(ctx, "AcrPull", "")
if err != nil {
return err
}
_, err = authorization.NewRoleAssignment(ctx, "access-from-cluster", &authorization.RoleAssignmentArgs{
PrincipalId: pulumi.String(currentPrincipal),
PrincipalType: pulumi.String(authorization.PrincipalTypeServicePrincipal), // adjust the type if you are running as a user
RoleDefinitionId: pulumi.String(*roleDef),
Scope: registry.ID(),
})
if err != nil {
return err
}
return nil
})
}
func getRoleDefinitionsClient(ctx *pulumi.Context) (*armauth.RoleDefinitionsClient, error) {
config, err := authorization.GetClientConfig(ctx)
if err != nil {
return nil, err
}
token, err := authorization.GetClientToken(ctx, nil)
if err != nil {
return nil, err
}
authorizer := autorest.NewBearerAuthorizer(&adal.Token{AccessToken: token.Token})
// Note: reuse the credentials and/or the client in case your scenario needs
// multiple calls to Azure SDKs.
client := armauth.NewRoleDefinitionsClient(config.SubscriptionId)
client.Authorizer = authorizer
return &client, nil
}
func getRoleIdByName(ctx *pulumi.Context, roleName string, scope string) (*string, error) {
client, err := getRoleDefinitionsClient(ctx)
if err != nil {
return nil, err
}
roles, err := client.List(context.Background(), scope, fmt.Sprintf("roleName eq '%s'", roleName))
if err != nil {
return nil, err
}
if len(roles.Values()) == 0 {
return nil, fmt.Errorf("role %q not found at scope %q", roleName, scope)
}
if len(roles.Values()) > 1 {
return nil, fmt.Errorf("too many roles %q found at scope %q. Found: %d", roleName, scope, len(roles.Values()))
}
role := roles.Values()[0]
return role.ID, nil
}