-
Notifications
You must be signed in to change notification settings - Fork 50
/
robot.go
260 lines (209 loc) · 7.66 KB
/
robot.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package robot
import (
"context"
"github.com/go-openapi/runtime"
v2client "github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client"
"github.com/mittwald/goharbor-client/v5/apiv2/internal/api/client/robot"
"github.com/mittwald/goharbor-client/v5/apiv2/model"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/config"
"github.com/mittwald/goharbor-client/v5/apiv2/pkg/errors"
)
const (
// Definitions in this block may be used to interact with the package methods.
// LevelProject defines a project-wide access level for a robot account.
LevelProject Level = "project"
// LevelSystem defines a system-wide access level for a robot account.
LevelSystem Level = "system"
ResourceRepository AccessResource = "repository"
ResourceArtifact AccessResource = "artifact"
ResourceHelmChart AccessResource = "helm-chart"
ResourceHelmChartVersion AccessResource = "helm-chart-version"
ResourceTag AccessResource = "tag"
ResourceArtifactLabel AccessResource = "artifact-label"
ResourceScan AccessResource = "scan"
ActionPush AccessAction = "push"
ActionPull AccessAction = "pull"
ActionCreate AccessAction = "create"
ActionDelete AccessAction = "delete"
ActionRead AccessAction = "read"
)
// RESTClient is a subclient for handling project related actions.
type RESTClient struct {
// Options contains optional configuration when making API calls.
Options *config.Options
// The new client of the harbor v2 API
V2Client *v2client.Harbor
// AuthInfo contains the auth information that is provided on API calls.
AuthInfo runtime.ClientAuthInfoWriter
}
func NewClient(v2Client *v2client.Harbor, opts *config.Options, authInfo runtime.ClientAuthInfoWriter) *RESTClient {
return &RESTClient{
Options: opts,
V2Client: v2Client,
AuthInfo: authInfo,
}
}
type Client interface {
ListRobotAccounts(ctx context.Context) ([]*model.Robot, error)
GetRobotAccountByName(ctx context.Context, name string) (*model.Robot, error)
GetRobotAccountByID(ctx context.Context, id int64) (*model.Robot, error)
NewRobotAccount(ctx context.Context, r *model.RobotCreate) (*model.RobotCreated, error)
DeleteRobotAccountByName(ctx context.Context, name string) error
DeleteRobotAccountByID(ctx context.Context, id int64) error
UpdateRobotAccount(ctx context.Context, r *model.Robot) error
RefreshRobotAccountSecretByID(ctx context.Context, id int64, sec string) (*model.RobotSec, error)
RefreshRobotAccountSecretByName(ctx context.Context, name string, sec string) (*model.RobotSec, error)
}
type Level string
func (in Level) String() string {
return string(in)
}
type AccessResource string
func (in AccessResource) String() string {
return string(in)
}
type AccessAction string
func (in AccessAction) String() string {
return string(in)
}
// ListRobotAccounts ListProjectRobots returns a list of all robot accounts.
func (c *RESTClient) ListRobotAccounts(ctx context.Context) ([]*model.Robot, error) {
var robotAccounts []*model.Robot
page := c.Options.Page
params := &robot.ListRobotParams{
Page: &page,
PageSize: &c.Options.PageSize,
Q: &c.Options.Query,
Sort: &c.Options.Sort,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
for {
resp, err := c.V2Client.Robot.ListRobot(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRobotErrors(err)
}
if len(resp.Payload) == 0 {
break
}
totalCount := resp.XTotalCount
robotAccounts = append(robotAccounts, resp.Payload...)
if int64(len(robotAccounts)) >= totalCount {
break
}
page++
}
return robotAccounts, nil
}
// GetRobotAccountByName GetRobotByName lists all existing robot accounts and returns the one matching the provided name.
// Note that the generic 'robot$'-prefix of the robot name is implicitly used for getting the resource.
func (c *RESTClient) GetRobotAccountByName(ctx context.Context, name string) (*model.Robot, error) {
robots, err := c.ListRobotAccounts(ctx)
if err != nil {
return nil, err
}
for _, r := range robots {
if r.Name == "robot$"+name {
return r, nil
}
}
return nil, &errors.ErrRobotAccountUnknownResource{}
}
// GetRobotAccountByID GetRobotByID returns a robot account identified by its 'id'.
func (c *RESTClient) GetRobotAccountByID(ctx context.Context, id int64) (*model.Robot, error) {
params := &robot.GetRobotByIDParams{
RobotID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Robot.GetRobotByID(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRobotErrors(err)
}
return resp.Payload, nil
}
// NewRobotAccount creates a new robot account from the specification of 'r' and returns a 'RobotCreated' response.
func (c *RESTClient) NewRobotAccount(ctx context.Context, r *model.RobotCreate) (*model.RobotCreated, error) {
params := &robot.CreateRobotParams{
Robot: r,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Robot.CreateRobot(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRobotErrors(err)
}
return resp.Payload, nil
}
// DeleteRobotAccountByName deletes a robot account identified by its 'name'.
// Note that the generic 'robot$'-prefix of the robot name is implicitly used for deletion.
func (c *RESTClient) DeleteRobotAccountByName(ctx context.Context, name string) error {
robots, err := c.ListRobotAccounts(ctx)
if err != nil {
return err
}
for _, r := range robots {
if r.Name == "robot$"+name {
return c.DeleteRobotAccountByID(ctx, r.ID)
}
}
return &errors.ErrRobotAccountUnknownResource{}
}
// DeleteRobotAccountByID DeleteProjectRobotByID deletes a robot account identified by its id.
func (c *RESTClient) DeleteRobotAccountByID(ctx context.Context, id int64) error {
params := &robot.DeleteRobotParams{
RobotID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Robot.DeleteRobot(params, c.AuthInfo)
if err != nil {
return handleSwaggerRobotErrors(err)
}
return nil
}
// UpdateRobotAccount updates the robot account 'r' with the provided specification.
// Note that model.Robot.Name & model.Robot.Level are immutable by API definitions.
// NOTE: Updating existing system-level robot accounts with wildcard access to all projects
// will fail until https://github.com/goharbor/harbor/pull/17117 is merged.
func (c *RESTClient) UpdateRobotAccount(ctx context.Context, r *model.Robot) error {
params := &robot.UpdateRobotParams{
Robot: r,
RobotID: r.ID,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Robot.UpdateRobot(params, c.AuthInfo)
if err != nil {
return handleSwaggerRobotErrors(err)
}
return nil
}
// RefreshRobotAccountSecretByID updates the robot account secret with the provided string "sec", by its id and return a 'RobotSec' response.
func (c *RESTClient) RefreshRobotAccountSecretByID(ctx context.Context, id int64, sec string) (*model.RobotSec, error) {
r := &model.RobotSec{Secret: sec}
params := &robot.RefreshSecParams{
RobotSec: r,
RobotID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Robot.RefreshSec(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRobotErrors(err)
}
return resp.Payload, nil
}
// RefreshRobotAccountSecretByName updates the robot account secret with the provided string "sec", by its name and return a 'RobotSec' response.
func (c *RESTClient) RefreshRobotAccountSecretByName(ctx context.Context, name string, sec string) (*model.RobotSec, error) {
robots, err := c.ListRobotAccounts(ctx)
if err != nil {
return nil, err
}
for _, r := range robots {
if r.Name == "robot$"+name {
return c.RefreshRobotAccountSecretByID(ctx, r.ID, sec)
}
}
return nil, &errors.ErrRobotAccountUnknownResource{}
}