-
Notifications
You must be signed in to change notification settings - Fork 567
/
auth.go
100 lines (89 loc) · 3.05 KB
/
auth.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
package client
import (
"github.com/pachyderm/pachyderm/v2/src/auth"
"github.com/pachyderm/pachyderm/v2/src/pfs"
"github.com/pachyderm/pachyderm/v2/src/internal/grpcutil"
)
// IsAuthActive returns whether auth is activated on the cluster
func (c APIClient) IsAuthActive() (bool, error) {
_, err := c.GetRoleBinding(c.Ctx(), &auth.GetRoleBindingRequest{
Resource: &auth.Resource{Type: auth.ResourceType_CLUSTER},
})
switch {
case err == nil:
return true, nil
case auth.IsErrNotAuthorized(err):
return true, nil
case auth.IsErrNotSignedIn(err):
return true, nil
case auth.IsErrNotActivated(err):
return false, nil
default:
return false, grpcutil.ScrubGRPC(err)
}
}
func (c APIClient) GetClusterRoleBinding() (*auth.RoleBinding, error) {
resp, err := c.GetRoleBinding(c.Ctx(), &auth.GetRoleBindingRequest{
Resource: &auth.Resource{Type: auth.ResourceType_CLUSTER},
})
if err != nil {
return nil, err
}
return resp.Binding, nil
}
func (c APIClient) ModifyClusterRoleBinding(principal string, roles []string) error {
_, err := c.ModifyRoleBinding(c.Ctx(), &auth.ModifyRoleBindingRequest{
Resource: &auth.Resource{Type: auth.ResourceType_CLUSTER},
Principal: principal,
Roles: roles,
})
if err != nil {
return err
}
return nil
}
func (c APIClient) GetProjectRoleBinding(project string) (*auth.RoleBinding, error) {
resp, err := c.GetRoleBinding(c.Ctx(), &auth.GetRoleBindingRequest{
Resource: &auth.Resource{Type: auth.ResourceType_PROJECT, Name: project},
})
if err != nil {
return nil, err
}
return resp.Binding, nil
}
// Deprecated: use GetProjectRepoRoleBinding instead.
func (c APIClient) GetRepoRoleBinding(repoName string) (*auth.RoleBinding, error) {
return c.GetProjectRepoRoleBinding(pfs.DefaultProjectName, repoName)
}
// Return the roles bound to a repo within a project.
func (c APIClient) GetProjectRepoRoleBinding(projectName, repoName string) (*auth.RoleBinding, error) {
resp, err := c.GetRoleBinding(c.Ctx(), &auth.GetRoleBindingRequest{
Resource: NewProjectRepo(projectName, repoName).AuthResource(),
})
if err != nil {
return nil, err
}
return resp.Binding, nil
}
// Deprecated: use ModifyProjectRepoRoleBinding instead.
func (c APIClient) ModifyRepoRoleBinding(repoName, principal string, roles []string) error {
return c.ModifyProjectRepoRoleBinding(pfs.DefaultProjectName, repoName, principal, roles)
}
// Update the roles bound to a repo within a project.
func (c APIClient) ModifyProjectRepoRoleBinding(projectName, repoName, principal string, roles []string) error {
_, err := c.ModifyRoleBinding(c.Ctx(), &auth.ModifyRoleBindingRequest{
Resource: NewProjectRepo(projectName, repoName).AuthResource(),
Principal: principal,
Roles: roles,
})
return err
}
// ModifyProjectRoleBinding binds a user's roles to a project.
func (c APIClient) ModifyProjectRoleBinding(projectName, principal string, roles []string) error {
_, err := c.ModifyRoleBinding(c.Ctx(), &auth.ModifyRoleBindingRequest{
Resource: NewProject(projectName).AuthResource(),
Principal: principal,
Roles: roles,
})
return err
}