-
Notifications
You must be signed in to change notification settings - Fork 86
/
graph.go
162 lines (139 loc) · 6.16 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
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"
log "github.com/sirupsen/logrus"
)
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)
log.Debugf("Creating service principal for application with id=%s", appID)
return c.graphServiceClient.ServicePrincipals().Post(spPostOptions)
}
// 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))
log.Debugf("Creating application with display name=%s", displayName)
return c.graphServiceClient.Applications().Post(appPostOptions)
}
// GetServicePrincipal gets a service principal by its display name.
func (c *AzureClient) GetServicePrincipal(ctx context.Context, displayName string) (*graph.ServicePrincipal, error) {
log.Debugf("Getting service principal with display name=%s", 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
}
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) {
log.Debugf("Getting application with display name=%s", 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
}
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 {
log.Debugf("Deleting service principal with object id=%s", objectID)
return c.graphServiceClient.ServicePrincipalsById(objectID).Delete(nil)
}
// DeleteApplication deletes an application.
func (c *AzureClient) DeleteApplication(ctx context.Context, objectID string) error {
log.Debugf("Deleting application with object id=%s", 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 {
log.Debugf("Adding federated credential for objectID=%s", 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) {
log.Debugf("Getting federated credential for objectID=%s, issuer=%s, subject=%s", objectID, issuer, 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 {
log.Debugf("Deleting federated credential for objectID=%s, federatedCredentialID=%s", objectID, 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)
}