-
Notifications
You must be signed in to change notification settings - Fork 19
/
service_principals_aad.go
48 lines (41 loc) · 1.2 KB
/
service_principals_aad.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
package api
import (
"context"
"time"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/Azure/go-autorest/autorest/date"
"github.com/Azure/go-autorest/autorest/to"
"github.com/hashicorp/go-uuid"
)
var _ ServicePrincipalClient = (*AADServicePrincipalsClient)(nil)
type AADServicePrincipalsClient struct {
Client graphrbac.ServicePrincipalsClient
Passwords Passwords
}
func (c AADServicePrincipalsClient) CreateServicePrincipal(ctx context.Context, appID string, startDate time.Time, endDate time.Time) (string, string, error) {
keyID, err := uuid.GenerateUUID()
if err != nil {
return "", "", err
}
password, err := c.Passwords.Generate(ctx)
if err != nil {
return "", "", err
}
clientParams := graphrbac.ServicePrincipalCreateParameters{
AppID: to.StringPtr(appID),
AccountEnabled: to.BoolPtr(true),
PasswordCredentials: &[]graphrbac.PasswordCredential{
graphrbac.PasswordCredential{
StartDate: &date.Time{startDate},
EndDate: &date.Time{endDate},
KeyID: &keyID,
Value: &password,
},
},
}
sp, err := c.Client.Create(ctx, clientParams)
if err != nil {
return "", "", err
}
return *sp.ObjectID, password, nil
}