-
Notifications
You must be signed in to change notification settings - Fork 86
/
graph.go
160 lines (131 loc) · 5.85 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
package cloud
import (
"context"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/microsoftgraph/msgraph-sdk-go/applications"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/microsoftgraph/msgraph-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) (models.ServicePrincipalable, error) {
body := models.NewServicePrincipal()
body.SetAppId(to.Ptr(appID))
body.SetTags(tags)
mlog.Debug("Creating service principal for application", "id", appID)
sp, err := c.graphServiceClient.ServicePrincipals().Post(ctx, body, nil)
if err != nil {
return nil, maybeExtractGraphError(err)
}
return sp, nil
}
// CreateApplication creates an application.
func (c *AzureClient) CreateApplication(ctx context.Context, displayName string) (models.Applicationable, error) {
body := models.NewApplication()
body.SetDisplayName(to.Ptr(displayName))
mlog.Debug("Creating application", "displayName", displayName)
app, err := c.graphServiceClient.Applications().Post(ctx, body, nil)
if err != nil {
return nil, maybeExtractGraphError(err)
}
return app, nil
}
// GetServicePrincipal gets a service principal by its display name.
func (c *AzureClient) GetServicePrincipal(ctx context.Context, displayName string) (models.ServicePrincipalable, error) {
mlog.Debug("Getting service principal", "displayName", displayName)
spGetOptions := &serviceprincipals.ServicePrincipalsRequestBuilderGetRequestConfiguration{
QueryParameters: &serviceprincipals.ServicePrincipalsRequestBuilderGetQueryParameters{
Filter: to.Ptr(getDisplayNameFilter(displayName)),
},
}
resp, err := c.graphServiceClient.ServicePrincipals().Get(ctx, spGetOptions)
if err != nil {
return nil, maybeExtractGraphError(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) (models.Applicationable, error) {
mlog.Debug("Getting application", "displayName", displayName)
appGetOptions := &applications.ApplicationsRequestBuilderGetRequestConfiguration{
QueryParameters: &applications.ApplicationsRequestBuilderGetQueryParameters{
Filter: to.Ptr(getDisplayNameFilter(displayName)),
},
}
resp, err := c.graphServiceClient.Applications().Get(ctx, appGetOptions)
if err != nil {
return nil, maybeExtractGraphError(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 {
mlog.Debug("Deleting service principal", "objectID", objectID)
return c.graphServiceClient.ServicePrincipals().ByServicePrincipalId(objectID).Delete(ctx, nil)
}
// DeleteApplication deletes an application.
func (c *AzureClient) DeleteApplication(ctx context.Context, objectID string) error {
mlog.Debug("Deleting application", "objectID", objectID)
return c.graphServiceClient.Applications().ByApplicationId(objectID).Delete(ctx, nil)
}
// AddFederatedCredential adds a federated credential to the cloud provider.
func (c *AzureClient) AddFederatedCredential(ctx context.Context, objectID string, fic models.FederatedIdentityCredentialable) error {
mlog.Debug("Adding federated credential", "objectID", objectID)
if _, err := c.graphServiceClient.Applications().ByApplicationId(objectID).FederatedIdentityCredentials().Post(ctx, fic, nil); err != nil {
return maybeExtractGraphError(err)
}
return nil
}
// GetFederatedCredential gets a federated credential from the cloud provider.
func (c *AzureClient) GetFederatedCredential(ctx context.Context, objectID, issuer, subject string) (models.FederatedIdentityCredentialable, error) {
mlog.Debug("Getting federated credential",
"objectID", objectID,
"issuer", issuer,
"subject", subject,
)
ficGetOptions := &applications.ItemFederatedIdentityCredentialsRequestBuilderGetRequestConfiguration{
QueryParameters: &applications.ItemFederatedIdentityCredentialsRequestBuilderGetQueryParameters{
// Filtering on more than one resource is currently not supported.
Filter: to.Ptr(getSubjectFilter(subject)),
},
}
resp, err := c.graphServiceClient.Applications().ByApplicationId(objectID).FederatedIdentityCredentials().Get(ctx, ficGetOptions)
if err != nil {
return nil, maybeExtractGraphError(err)
}
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.Applications().ByApplicationId(objectID).FederatedIdentityCredentials().ByFederatedIdentityCredentialId(federatedCredentialID).Delete(ctx, 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)
}