-
Notifications
You must be signed in to change notification settings - Fork 568
/
auth.go
178 lines (153 loc) · 5.31 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package testutil
import (
"context"
"fmt"
"strings"
"testing"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/pachyderm/pachyderm/v2/src/auth"
"github.com/pachyderm/pachyderm/v2/src/internal/client"
"github.com/pachyderm/pachyderm/v2/src/internal/config"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/pctx"
"github.com/pachyderm/pachyderm/v2/src/internal/require"
"github.com/pachyderm/pachyderm/v2/src/pfs"
"github.com/pachyderm/pachyderm/v2/src/pps"
)
const (
// RootToken is the hard-coded admin token used on all activated test clusters
RootToken = "iamroot"
)
func TSProtoOrDie(t testing.TB, ts time.Time) *timestamppb.Timestamp {
return timestamppb.New(ts)
}
func activateAuthHelper(tb testing.TB, client *client.APIClient, port ...string) {
client.SetAuthToken(RootToken)
ActivateEnterprise(tb, client, port...)
_, err := client.Activate(client.Ctx(),
&auth.ActivateRequest{RootToken: RootToken},
)
if err != nil && !strings.HasSuffix(err.Error(), "already activated") {
tb.Fatalf("could not activate auth service: %v", err.Error())
}
require.NoError(tb, config.WritePachTokenToConfig(RootToken, false))
// Activate auth for PPS
client = client.WithCtx(pctx.Background("activateAuthHelper"))
client.SetAuthToken(RootToken)
_, err = client.PfsAPIClient.ActivateAuth(client.Ctx(), &pfs.ActivateAuthRequest{})
require.NoError(tb, err)
_, err = client.PpsAPIClient.ActivateAuth(client.Ctx(), &pps.ActivateAuthRequest{})
require.NoError(tb, err)
}
// ActivateAuthClient activates the auth service in the test cluster, if it isn't already enabled
func ActivateAuthClient(tb testing.TB, c *client.APIClient, port ...string) {
tb.Helper()
activateAuthHelper(tb, c, port...)
}
// creates a new authenticated pach client, without re-activating
func AuthenticateClient(tb testing.TB, c *client.APIClient, subject string) *client.APIClient {
tb.Helper()
rootClient := UnauthenticatedPachClient(tb, c)
rootClient.SetAuthToken(RootToken)
if subject == auth.RootUser {
return rootClient
}
var token *auth.GetRobotTokenResponse
var err error
require.NoErrorWithinTRetryConstant(tb, 2*time.Minute, func() error {
token, err = rootClient.GetRobotToken(rootClient.Ctx(), &auth.GetRobotTokenRequest{Robot: subject})
if err != nil {
return errors.Wrap(err, "get robot token request")
}
return nil
}, 5*time.Second, "authenticating pach client")
client := UnauthenticatedPachClient(tb, c)
client.SetAuthToken(token.Token)
return client
}
func AuthenticatedPachClient(tb testing.TB, c *client.APIClient, subject string, port ...string) *client.APIClient {
tb.Helper()
activateAuthHelper(tb, c, port...)
return AuthenticateClient(tb, c, subject)
}
// GetUnauthenticatedPachClient returns a copy of the testing pach client with no auth token
func UnauthenticatedPachClient(tb testing.TB, c *client.APIClient) *client.APIClient {
tb.Helper()
client := c.WithCtx(context.Background())
client.SetAuthToken("")
return client
}
func RepoInfoToName(repoInfo interface{}) interface{} {
return repoInfo.(*pfs.RepoInfo).Repo.Name
}
func User(email string) string {
return auth.UserPrefix + email
}
func Pl(projectName, pipelineName string) string {
return fmt.Sprintf("%s%s/%s", auth.PipelinePrefix, projectName, pipelineName)
}
func Robot(robot string) string {
return auth.RobotPrefix + robot
}
func BuildClusterBindings(s ...string) *auth.RoleBinding {
return BuildBindings(append(s,
auth.AllClusterUsersSubject, auth.ProjectCreatorRole,
auth.RootUser, auth.ClusterAdminRole,
auth.InternalPrefix+"auth-server", auth.ClusterAdminRole,
)...)
}
func BuildBindings(s ...string) *auth.RoleBinding {
var b auth.RoleBinding
b.Entries = make(map[string]*auth.Roles)
for i := 0; i < len(s); i += 2 {
if _, ok := b.Entries[s[i]]; !ok {
b.Entries[s[i]] = &auth.Roles{Roles: make(map[string]bool)}
}
b.Entries[s[i]].Roles[s[i+1]] = true
}
return &b
}
func GetRepoRoleBinding(t *testing.T, c *client.APIClient, projectName, repoName string) *auth.RoleBinding {
t.Helper()
resp, err := c.GetRepoRoleBinding(projectName, repoName)
require.NoError(t, err)
return resp
}
func GetProjectRoleBinding(t *testing.T, c *client.APIClient, project string) *auth.RoleBinding {
t.Helper()
resp, err := c.GetProjectRoleBinding(project)
require.NoError(t, err)
return resp
}
// CommitCnt uses 'c' to get the number of commits made to the repo 'repo'
func CommitCnt(t *testing.T, c *client.APIClient, repo *pfs.Repo) int {
t.Helper()
commitList, err := c.ListCommitByRepo(repo)
require.NoError(t, err)
return len(commitList)
}
// PipelineNames returns the names of all pipelines that 'c' gets from
// ListPipeline in the specified project.
func PipelineNames(t *testing.T, c *client.APIClient, project string) []string {
t.Helper()
ps, err := c.ListPipeline(false)
require.NoError(t, err)
var result []string
if project == "" {
project = pfs.DefaultProjectName
}
for _, p := range ps {
if project == p.GetPipeline().GetProject().GetName() {
result = append(result, p.GetPipeline().GetName())
}
}
return result
}
func Group(group string) string {
return auth.GroupPrefix + group
}
func RandomRobot(t *testing.T, c *client.APIClient, name string) (string, *client.APIClient) {
name = Robot(UniqueString(name))
return name, AuthenticateClient(t, c, name)
}