Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: worker auth endpoints
  • Loading branch information
plyr4 committed Mar 17, 2023
commit 96952ad05c7ee50880af5d69bf88e69392c2f404
21 changes: 21 additions & 0 deletions vela/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package vela

import (
"fmt"

"github.com/go-vela/types/library"
)

Expand All @@ -20,6 +22,7 @@ type (
Service *AdminSvcService
Step *AdminStepService
User *AdminUserService
Worker *AdminWorkerService
}

// AdminBuildService handles retrieving admin builds from
Expand Down Expand Up @@ -53,6 +56,10 @@ type (
// AdminUserService handles retrieving admin users from
// the server methods of the Vela API.
AdminUserService service

// AdminWorkerService handles managing admin worker functionality
// from the server methods of the Vela API.
AdminWorkerService service
)

// GetQueueOptions specifies the optional parameters to the
Expand Down Expand Up @@ -196,3 +203,17 @@ func (svc *AdminUserService) Update(u *library.User) (*library.User, *Response,

return v, resp, err
}

// RegistrationToken generates a worker registration token with the provided details.
func (svc *AdminUserService) RegistrationToken(w *library.Worker) (*library.Token, *Response, error) {
// set the API endpoint path we send the request to
url := fmt.Sprintf("/api/v1/workers/%s/register-token", w.GetHostname())

// library Token type we want to return
t := new(library.Token)

// send request using client
resp, err := svc.client.Call("POST", url, nil, t)

return t, resp, err
}
15 changes: 15 additions & 0 deletions vela/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ type AuthenticationService struct {
authType AuthenticationType
}

// SetTokenAuth sets the authentication type as a plain token.
func (svc *AuthenticationService) GetToken() string {
return *svc.token
}

// SetTokenAuth sets the authentication type as a plain token.
func (svc *AuthenticationService) SetTokenAuth(token string) {
svc.token = String(token)
Expand Down Expand Up @@ -203,3 +208,13 @@ func extractRefreshToken(cookies []*http.Cookie) string {

return c
}

// ValidateToken makes a request to validate tokens with the Vela server.
func (svc *AuthenticationService) ValidateToken() (*Response, error) {
// set the API endpoint path we send the request to
u := "/validate-token"

// attempt to validate a server token
resp, err := svc.client.Call("GET", u, nil, nil)
return resp, err
}
1 change: 1 addition & 0 deletions vela/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func NewClient(baseURL, id string, httpClient *http.Client) (*Client, error) {
&AdminSvcService{client: c},
&AdminStepService{client: c},
&AdminUserService{client: c},
&AdminWorkerService{client: c},
}
c.Build = &BuildService{client: c}
c.Deployment = &DeploymentService{client: c}
Expand Down
1 change: 1 addition & 0 deletions vela/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestVela_NewClient(t *testing.T) {
&AdminSvcService{client: want},
&AdminStepService{client: want},
&AdminUserService{client: want},
&AdminWorkerService{client: want},
}
want.Build = &BuildService{client: want}
want.Deployment = &DeploymentService{client: want}
Expand Down