Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: worker auth endpoints #212

Merged
merged 12 commits into from
Mar 24, 2023
28 changes: 28 additions & 0 deletions vela/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
package vela

import (
"errors"
"fmt"
"strings"

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

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

// AdminBuildService handles retrieving admin builds from
Expand Down Expand Up @@ -53,6 +58,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 +205,22 @@ func (svc *AdminUserService) Update(u *library.User) (*library.User, *Response,

return v, resp, err
}

// RegisterToken generates a worker registration token with the provided details.
func (svc *AdminWorkerService) RegisterToken(hostname string) (*library.Token, *Response, error) {
// validate input
if strings.EqualFold(hostname, "") {
return nil, nil, errors.New("bad request, no hostname provided")
}

// set the API endpoint path we send the request to
url := fmt.Sprintf("/api/v1/admin/workers/%s/register-token", hostname)

// 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
}
50 changes: 50 additions & 0 deletions vela/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,53 @@ func TestAdmin_Build_Queue_200(t *testing.T) {
t.Errorf("GetQueue() mismatch (-want +got):\n%s", diff)
}
}

func TestAdmin_Worker_RegistrationToken_201(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

data := []byte(server.RegisterTokenResp)
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
plyr4 marked this conversation as resolved.
Show resolved Hide resolved

var want *library.Token

err := json.Unmarshal(data, &want)
if err != nil {
t.Error(err)
}

hostname := "foo"

// run test
got, resp, err := c.Admin.Worker.RegisterToken(hostname)
if err != nil {
t.Errorf("RegisterToken returned err: %v", err)
}

if resp.StatusCode != http.StatusCreated {
t.Errorf("RegisterToken returned %v, want %v", resp.StatusCode, http.StatusCreated)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("RegisterToken() mismatch (-want +got):\n%s", diff)
}
}

func TestAdmin_Worker_RegistrationToken_NoHostname(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

// bad hostname
hostname := ""

// run test
_, _, err := c.Admin.Worker.RegisterToken(hostname)
if err == nil {
t.Error("RegisterToken should have returned err")
}
}
11 changes: 11 additions & 0 deletions vela/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,14 @@ 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
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
}
47 changes: 47 additions & 0 deletions vela/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,50 @@ func TestVela_Authentication_ExchangeTokens_BadInput(t *testing.T) {
t.Errorf("ExchangeTokens should not set Refresh Token")
}
}

func TestVela_Authentication_ValidateToken_200(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

c.Authentication.SetTokenAuth("foo")

// run test
resp, err := c.Authentication.ValidateToken()

if err != nil {
t.Errorf("ValidateToken returned error %v", err)
}

if resp == nil {
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
// Fatal so that nil resp is not checked
t.Fatal("ValidateToken returned nil response")
}

if resp.StatusCode != http.StatusOK {
plyr4 marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("ValidateToken returned %v, want %v", resp.StatusCode, http.StatusOK)
}
}

func TestVela_Authentication_ValidateToken_NoToken(t *testing.T) {
// setup context
gin.SetMode(gin.TestMode)

s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

c.Authentication.SetTokenAuth("")

// run test
resp, err := c.Authentication.ValidateToken()

if err == nil {
t.Error("ValidateToken should have returned error")
}

if resp != nil {
t.Error("ValidateToken response should be nil")
}
}
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
6 changes: 3 additions & 3 deletions vela/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (svc *WorkerService) GetAll() (*[]library.Worker, *Response, error) {
}

// Add constructs a worker with the provided details.
func (svc *WorkerService) Add(w *library.Worker) (*library.Worker, *Response, error) {
func (svc *WorkerService) Add(w *library.Worker) (*library.Token, *Response, error) {
// set the API endpoint path we send the request to
u := "/api/v1/workers"

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

// send request using client
resp, err := svc.client.Call("POST", u, w, v)
Expand Down
30 changes: 15 additions & 15 deletions vela/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func TestWorker_Get_200(t *testing.T) {
got, resp, err := c.Worker.Get("worker_1")

if err != nil {
t.Errorf("New returned err: %v", err)
t.Errorf("Worker get returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
t.Errorf("Worker returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker get returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
Expand All @@ -59,11 +59,11 @@ func TestWorker_Get_404(t *testing.T) {
got, resp, err := c.Worker.Get("0")

if err == nil {
t.Errorf("New returned err: %v", err)
t.Errorf("Worker get returned err: %v", err)
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Worker returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker get returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
Expand All @@ -87,7 +87,7 @@ func TestWorker_GetAll_200(t *testing.T) {
got, resp, err := c.Worker.GetAll()

if err != nil {
t.Errorf("New returned err: %v", err)
t.Errorf("Worker get all returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
Expand All @@ -106,9 +106,9 @@ func TestWorker_Add_201(t *testing.T) {
s := httptest.NewServer(server.FakeHandler())
c, _ := NewClient(s.URL, "", nil)

data := []byte(server.WorkerResp)
data := []byte(server.AddWorkerResp)
plyr4 marked this conversation as resolved.
Show resolved Hide resolved

var want library.Worker
var want library.Token
_ = json.Unmarshal(data, &want)

req := library.Worker{
Expand All @@ -128,11 +128,11 @@ func TestWorker_Add_201(t *testing.T) {
got, resp, err := c.Worker.Add(&req)

if err != nil {
t.Errorf("New returned err: %v", err)
t.Errorf("Worker add returned err: %v", err)
}

if resp.StatusCode != http.StatusCreated {
t.Errorf("Worker returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker add returned %v, want %v", resp.StatusCode, http.StatusOK)
}

if !reflect.DeepEqual(got, &want) {
Expand Down Expand Up @@ -160,7 +160,7 @@ func TestWorker_Update_200(t *testing.T) {
got, resp, err := c.Worker.Update("worker_1", &req)

if err != nil {
t.Errorf("New returned err: %v", err)
t.Errorf("Worker update returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestWorker_Update_404(t *testing.T) {
got, resp, err := c.Worker.Update("0", &req)

if err == nil {
t.Errorf("New returned err: %v", err)
t.Errorf("Worker update returned err: %v", err)
}

if resp.StatusCode != http.StatusNotFound {
Expand All @@ -212,11 +212,11 @@ func TestWorker_Remove_200(t *testing.T) {
_, resp, err := c.Worker.Remove("worker_1")

if err != nil {
t.Errorf("New returned err: %v", err)
t.Errorf("Worker remove returned err: %v", err)
}

if resp.StatusCode != http.StatusOK {
t.Errorf("Worker returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker remove returned %v, want %v", resp.StatusCode, http.StatusOK)
}
}

Expand All @@ -231,11 +231,11 @@ func TestWorker_Remove_404(t *testing.T) {
_, resp, err := c.Worker.Remove("0")

if err == nil {
t.Errorf("New returned err: %v", err)
t.Errorf("Worker remove returned err: %v", err)
}

if resp.StatusCode != http.StatusNotFound {
t.Errorf("Worker returned %v, want %v", resp.StatusCode, http.StatusOK)
t.Errorf("Worker remove returned %v, want %v", resp.StatusCode, http.StatusOK)
}
}

Expand Down