-
Notifications
You must be signed in to change notification settings - Fork 1
/
azure.go
227 lines (211 loc) · 6.73 KB
/
azure.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
// Package azure contains the Azure API calls
package azure
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription"
"github.com/google/uuid"
)
const defaultTimeout = 30 * time.Second
type subscriptionID string
// Session holds the active session login credentials and related settings.
type Session struct {
ctx context.Context
timeout time.Duration
principalID string
credential *azidentity.DefaultAzureCredential
}
// NewSession returns default credentials using the information from the OS
// environment.
// TODO: extract the users principal id somehow. Current solution is to extract
// it with: `az ad signed-in-user show | jq .id`
func NewSession(ctx context.Context, principalID string) (*Session, error) {
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return nil, err
}
return &Session{
ctx: ctx,
timeout: defaultTimeout,
principalID: principalID,
credential: cred,
}, nil
}
// Subscription returns all enabled subscriptions.
func (s *Session) Subscriptions() (map[string]subscriptionID, error) {
sc, err := armsubscription.NewSubscriptionsClient(s.credential, nil)
if err != nil {
return nil, err
}
pager := sc.NewListPager(&armsubscription.SubscriptionsClientListOptions{})
ctx, cancel := context.WithTimeout(s.ctx, s.timeout)
defer cancel()
var subs = make(map[string]subscriptionID)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
if err != nil {
return subs, err
}
for _, v := range nextResult.Value {
if v == nil || v.DisplayName == nil || v.SubscriptionID == nil || v.State == nil {
return subs, errors.New("unexptected nil value returned")
}
if *v.State != armsubscription.SubscriptionStateEnabled {
// ignore all disabled, deleted, etc subscriptions.
continue
}
if _, found := subs[*v.DisplayName]; found {
log.Printf("WARNING: duplicate subscription ignored: %s",
*v.DisplayName)
} else {
subs[*v.DisplayName] = subscriptionID(*v.SubscriptionID)
}
}
}
return subs, nil
}
// RolesForSubscription lists the available roles that have
// RoleEligibilitySchedules for the given subscription name.
func (s *Session) RolesForSubscription(subscriptionName string) ([]string, error) {
subs, err := s.Subscriptions()
if err != nil {
return []string{}, err
}
sub, found := subs[subscriptionName]
if !found {
return []string{}, errors.New("subscription not found")
}
res, err := s.roleEligibilitySchedules(fmt.Sprintf("/subscriptions/%s", sub))
if err != nil {
return []string{}, err
}
roles := []string{}
for k := range res {
roles = append(roles, k)
}
return roles, nil
}
// RoleEligibilitySchedules returns all the RoleEligibitlySchedules available
// for the given scope.
// XXX example scope: "subscriptions/"+subscriptionID,
func (s *Session) roleEligibilitySchedules(scope string) (map[string]*armauthorization.RoleEligibilitySchedule, error) {
rsi, err := armauthorization.NewRoleEligibilitySchedulesClient(s.credential, nil)
if err != nil {
return nil, err
}
filter := "asTarget()"
pager := rsi.NewListForScopePager(
scope,
&armauthorization.RoleEligibilitySchedulesClientListForScopeOptions{
Filter: &filter,
},
)
ctx, cancel := context.WithTimeout(s.ctx, s.timeout)
defer cancel()
var r = make(map[string]*armauthorization.RoleEligibilitySchedule)
for pager.More() {
nextResult, err := pager.NextPage(ctx)
if err != nil {
return r, err
}
for _, v := range nextResult.Value {
if v == nil || v.Properties == nil ||
v.Properties.ExpandedProperties == nil ||
v.Properties.ExpandedProperties.RoleDefinition == nil ||
v.Properties.ExpandedProperties.RoleDefinition.DisplayName == nil {
return r, errors.New("unexptected nil value returned")
}
if _, found := r[*v.Properties.ExpandedProperties.RoleDefinition.DisplayName]; found {
log.Printf("WARNING: duplicate eligiblity ignored for role %s",
*v.Properties.ExpandedProperties.RoleDefinition.DisplayName)
} else {
r[*v.Properties.ExpandedProperties.RoleDefinition.DisplayName] = v
}
}
}
return r, nil
}
// ActiveRoleAssignment will schedule a RoleAssignment for the given
// roleDisplayName scoped to the subscription.
func (s *Session) ActiveRoleAssignment(subscriptionName, roleDisplayName, justifiction string, duration time.Duration) error {
subs, err := s.Subscriptions()
if err != nil {
return err
}
subscriptionID, found := subs[subscriptionName]
if !found {
return errors.New("subscription not found")
}
res, err := s.roleEligibilitySchedules(fmt.Sprintf("subscriptions/%s", subscriptionID))
if err != nil {
return err
}
re, found := res[roleDisplayName]
if !found {
return errors.New("Role Eligibility Schedule not found")
}
/*
if b, err := json.MarshalIndent(re, "", "\t"); err == nil {
log.Printf("%s\n", b)
}
*/
// Make sure no nil values returned for the fields used below.
if re == nil || re.ID == nil || re.Properties == nil ||
re.Properties.ExpandedProperties == nil ||
re.Properties.ExpandedProperties.RoleDefinition == nil ||
re.Properties.RoleDefinitionID == nil {
return errors.New("unexptected nil value returned")
}
requestType := armauthorization.RequestTypeSelfActivate
startTime := time.Now()
expirationType := armauthorization.TypeAfterDuration
expirationDuration := fmt.Sprintf("PT%dM", int(duration.Minutes()))
req := armauthorization.RoleAssignmentScheduleRequest{
Properties: &armauthorization.RoleAssignmentScheduleRequestProperties{
PrincipalID: &s.principalID,
RequestType: &requestType,
RoleDefinitionID: re.Properties.RoleDefinitionID,
Justification: &justifiction,
ScheduleInfo: &armauthorization.RoleAssignmentScheduleRequestPropertiesScheduleInfo{
StartDateTime: &startTime,
Expiration: &armauthorization.RoleAssignmentScheduleRequestPropertiesScheduleInfoExpiration{
Type: &expirationType,
Duration: &expirationDuration,
},
},
LinkedRoleEligibilityScheduleID: re.ID,
},
}
scope := fmt.Sprintf("/subscriptions/%s", subscriptionID)
guid, err := uuid.NewUUID()
if err != nil {
return err
}
rasc, err := armauthorization.NewRoleAssignmentScheduleRequestsClient(
s.credential, nil)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(s.ctx, s.timeout)
defer cancel()
resp, err := rasc.Create(
ctx,
scope,
guid.String(),
req,
&armauthorization.RoleAssignmentScheduleRequestsClientCreateOptions{},
)
if err != nil {
return err
}
if b, err := json.MarshalIndent(resp, "", "\t"); err == nil {
log.Printf("%s\n", b)
}
return nil
}