-
Notifications
You must be signed in to change notification settings - Fork 85
/
graph.go
205 lines (182 loc) · 6.79 KB
/
graph.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
package cloud
import (
"context"
"fmt"
"github.com/Azure/go-autorest/autorest/to"
"github.com/microsoftgraph/msgraph-beta-sdk-go/applications"
"github.com/microsoftgraph/msgraph-beta-sdk-go/applications/item/federatedidentitycredentials"
"github.com/microsoftgraph/msgraph-beta-sdk-go/models/microsoft/graph"
"github.com/microsoftgraph/msgraph-beta-sdk-go/serviceprincipals"
"github.com/pkg/errors"
"monis.app/mlog"
)
var (
// ErrFederatedCredentialNotFound is returned when the federated credential is not found.
ErrFederatedCredentialNotFound = errors.New("federated credential not found")
)
// CreateServicePrincipal creates a service principal for the given application.
// No secret or certificate is generated.
func (c *AzureClient) CreateServicePrincipal(ctx context.Context, appID string, tags []string) (*graph.ServicePrincipal, error) {
spPostOptions := &serviceprincipals.ServicePrincipalsRequestBuilderPostOptions{
Body: graph.NewServicePrincipal(),
}
spPostOptions.Body.SetAppId(to.StringPtr(appID))
spPostOptions.Body.SetTags(tags)
mlog.Debug("Creating service principal for application", "id", appID)
sp, err := c.graphServiceClient.ServicePrincipals().Post(spPostOptions)
if err != nil {
return nil, err
}
graphErr, err := GetGraphError(sp.GetAdditionalData())
if err != nil {
return nil, err
}
if graphErr != nil {
return nil, *graphErr
}
return sp, nil
}
// CreateApplication creates an application.
func (c *AzureClient) CreateApplication(ctx context.Context, displayName string) (*graph.Application, error) {
appPostOptions := &applications.ApplicationsRequestBuilderPostOptions{
Body: graph.NewApplication(),
}
appPostOptions.Body.SetDisplayName(to.StringPtr(displayName))
mlog.Debug("Creating application", "displayName", displayName)
app, err := c.graphServiceClient.Applications().Post(appPostOptions)
if err != nil {
return nil, err
}
graphErr, err := GetGraphError(app.GetAdditionalData())
if err != nil {
return nil, err
}
if graphErr != nil {
return nil, *graphErr
}
return app, nil
}
// GetServicePrincipal gets a service principal by its display name.
func (c *AzureClient) GetServicePrincipal(ctx context.Context, displayName string) (*graph.ServicePrincipal, error) {
mlog.Debug("Getting service principal", "displayName", displayName)
spGetOptions := &serviceprincipals.ServicePrincipalsRequestBuilderGetOptions{
Q: &serviceprincipals.ServicePrincipalsRequestBuilderGetQueryParameters{
Filter: to.StringPtr(getDisplayNameFilter(displayName)),
},
}
resp, err := c.graphServiceClient.ServicePrincipals().Get(spGetOptions)
if err != nil {
return nil, err
}
graphErr, err := GetGraphError(resp.GetAdditionalData())
if err != nil {
return nil, err
}
if graphErr != nil {
return nil, *graphErr
}
if len(resp.GetValue()) == 0 {
return nil, errors.Errorf("service principal %s not found", displayName)
}
return &resp.GetValue()[0], nil
}
// GetApplication gets an application by its display name.
func (c *AzureClient) GetApplication(ctx context.Context, displayName string) (*graph.Application, error) {
mlog.Debug("Getting application", "displayName", displayName)
appGetOptions := &applications.ApplicationsRequestBuilderGetOptions{
Q: &applications.ApplicationsRequestBuilderGetQueryParameters{
Filter: to.StringPtr(getDisplayNameFilter(displayName)),
},
}
resp, err := c.graphServiceClient.Applications().Get(appGetOptions)
if err != nil {
return nil, err
}
graphErr, err := GetGraphError(resp.GetAdditionalData())
if err != nil {
return nil, err
}
if graphErr != nil {
return nil, *graphErr
}
if len(resp.GetValue()) == 0 {
return nil, errors.Errorf("application with display name '%s' not found", displayName)
}
return &resp.GetValue()[0], nil
}
// DeleteServicePrincipal deletes a service principal.
func (c *AzureClient) DeleteServicePrincipal(ctx context.Context, objectID string) error {
mlog.Debug("Deleting service principal", "objectID", objectID)
return c.graphServiceClient.ServicePrincipalsById(objectID).Delete(nil)
}
// DeleteApplication deletes an application.
func (c *AzureClient) DeleteApplication(ctx context.Context, objectID string) error {
mlog.Debug("Deleting application", "objectID", objectID)
return c.graphServiceClient.ApplicationsById(objectID).Delete(nil)
}
// AddFederatedCredential adds a federated credential to the cloud provider.
func (c *AzureClient) AddFederatedCredential(ctx context.Context, objectID string, fic *graph.FederatedIdentityCredential) error {
mlog.Debug("Adding federated credential", "objectID", objectID)
ficPostOptions := &federatedidentitycredentials.FederatedIdentityCredentialsRequestBuilderPostOptions{
Body: fic,
}
fic, err := c.graphServiceClient.ApplicationsById(objectID).FederatedIdentityCredentials().Post(ficPostOptions)
if err != nil {
return err
}
graphErr, err := GetGraphError(fic.GetAdditionalData())
if err != nil {
return err
}
if graphErr != nil {
return *graphErr
}
return nil
}
// GetFederatedCredential gets a federated credential from the cloud provider.
func (c *AzureClient) GetFederatedCredential(ctx context.Context, objectID, issuer, subject string) (*graph.FederatedIdentityCredential, error) {
mlog.Debug("Getting federated credential",
"objectID", objectID,
"issuer", issuer,
"subject", subject,
)
ficGetOptions := &federatedidentitycredentials.FederatedIdentityCredentialsRequestBuilderGetOptions{
Q: &federatedidentitycredentials.FederatedIdentityCredentialsRequestBuilderGetQueryParameters{
// Filtering on more than one resource is currently not supported.
Filter: to.StringPtr(getSubjectFilter(subject)),
},
}
resp, err := c.graphServiceClient.ApplicationsById(objectID).FederatedIdentityCredentials().Get(ficGetOptions)
if err != nil {
return nil, err
}
graphErr, err := GetGraphError(resp.GetAdditionalData())
if err != nil {
return nil, err
}
if graphErr != nil {
return nil, *graphErr
}
for _, fic := range resp.GetValue() {
if *fic.GetIssuer() == issuer {
return &fic, nil
}
}
return nil, ErrFederatedCredentialNotFound
}
// DeleteFederatedCredential deletes a federated credential from the cloud provider.
func (c *AzureClient) DeleteFederatedCredential(ctx context.Context, objectID, federatedCredentialID string) error {
mlog.Debug("Deleting federated credential",
"objectID", objectID,
"federatedCredentialID", federatedCredentialID,
)
return c.graphServiceClient.ApplicationsById(objectID).FederatedIdentityCredentialsById(federatedCredentialID).Delete(nil)
}
// getDisplayNameFilter returns a filter string for the given display name.
func getDisplayNameFilter(displayName string) string {
return fmt.Sprintf("displayName eq '%s'", displayName)
}
// getSubjectFilter returns a filter string for the given subject.
func getSubjectFilter(subject string) string {
return fmt.Sprintf("subject eq '%s'", subject)
}