-
Notifications
You must be signed in to change notification settings - Fork 0
/
managementPermissions.go
98 lines (81 loc) · 2.62 KB
/
managementPermissions.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
package manage
import (
"context"
"github.com/fident/go-manage/fidentapi"
"github.com/fident/go-manage/permissions"
"github.com/fident/go-manage/tls"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)
// GetManagementPermissions retrieves all management permissions for given identity id
func (i *Instance) GetManagementPermissions(identityID string) ([]permissions.Entry, error) {
meta, err := i.preflightAuth()
if err != nil {
return []permissions.Entry{}, err
}
ctx := metadata.NewOutgoingContext(context.Background(), meta)
conn, err := grpc.Dial(i.fidentEndpoint, grpc.WithTransportCredentials(credentials.NewTLS(tls.FidentTSLConfig)))
if err != nil {
return []permissions.Entry{}, err
}
defer conn.Close()
c := fidentapi.NewAuthClient(conn)
res, err := c.GetManagementPermissionsForIdentityIDs(ctx, &fidentapi.GetManagementPermissionsRequest{
IdentityId: []string{identityID},
})
if err != nil {
return []permissions.Entry{}, err
}
fin := []permissions.Entry{}
for _, re := range res.Results {
for _, pem := range re.Permissions {
fin = append(fin, permissions.Entry(pem))
}
}
return fin, nil
}
// AddManagementPermission adds given management permission to given identity id
func (i *Instance) AddManagementPermission(identityID string, permission permissions.Entry) error {
meta, err := i.preflightAuth()
if err != nil {
return err
}
ctx := metadata.NewOutgoingContext(context.Background(), meta)
conn, err := grpc.Dial(i.fidentEndpoint, grpc.WithTransportCredentials(credentials.NewTLS(tls.FidentTSLConfig)))
if err != nil {
return err
}
defer conn.Close()
c := fidentapi.NewAuthClient(conn)
_, err = c.AddManagementPermissionToIdentityIDs(ctx, &fidentapi.AddManagementPermissionRequest{
IdentityId: []string{identityID},
Permission: string(permission),
})
if err != nil {
return err
}
return nil
}
// RemoveManagementPermission removes given management permission from given identity id
func (i *Instance) RemoveManagementPermission(identityID string, permission permissions.Entry) error {
meta, err := i.preflightAuth()
if err != nil {
return err
}
ctx := metadata.NewOutgoingContext(context.Background(), meta)
conn, err := grpc.Dial(i.fidentEndpoint, grpc.WithTransportCredentials(credentials.NewTLS(tls.FidentTSLConfig)))
if err != nil {
return err
}
defer conn.Close()
c := fidentapi.NewAuthClient(conn)
_, err = c.RemoveManagementPermissionFromIdentityIDs(ctx, &fidentapi.RemoveManagementPermissionRequest{
IdentityId: []string{identityID},
Permission: string(permission),
})
if err != nil {
return err
}
return nil
}