Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3
github.com/coreos/go-semver v0.3.1
github.com/gin-gonic/gin v1.9.0
github.com/go-vela/server v0.18.1
github.com/go-vela/types v0.18.1
github.com/go-vela/server v0.18.2-0.20230323213747-5ffbe819aa29
github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/go-cmp v0.5.9
github.com/google/go-querystring v1.1.0
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/go-vela/server v0.18.1 h1:INd+nwLh0c+WA+8diIh4scLkByGBGZHiyVd5doLSolQ=
github.com/go-vela/server v0.18.1/go.mod h1:WyJEXyJYYASfqN9PDuHqlBTbhsSRIzOn1E7tM2phZMA=
github.com/go-vela/types v0.18.1 h1:V/luHLnCEaJhD1m9PZCZicIasg8Op6MCK+utkz+gQiU=
github.com/go-vela/types v0.18.1/go.mod h1:6MzMhLaXKSZ9wiJveieqnBd2+4ZMS7yv7+POGSITyS8=
github.com/go-vela/server v0.18.2-0.20230323213747-5ffbe819aa29 h1:DSbHMwxvI65vXAumZ1V9o82Q8W3J7Cce+grG8aqPPEw=
github.com/go-vela/server v0.18.2-0.20230323213747-5ffbe819aa29/go.mod h1:b+7XeGHO4ynIinY9mpWb6ye9psdwHpsAqMWy5oC+zJ0=
github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c h1:lnCL1knUGvgZQG4YBHSs/CZnxNBfqFUBlGhyq9LO9uk=
github.com/go-vela/types v0.18.2-0.20230321015315-6c723879639c/go.mod h1:6MzMhLaXKSZ9wiJveieqnBd2+4ZMS7yv7+POGSITyS8=
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
Expand Down
30 changes: 29 additions & 1 deletion vela/admin.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

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)

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")
}
}
13 changes: 12 additions & 1 deletion vela/authentication.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand Down 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
}
42 changes: 42 additions & 0 deletions vela/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,45 @@ 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.StatusCode != http.StatusOK {
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
20 changes: 17 additions & 3 deletions vela/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,33 @@ 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)

return v, resp, err
}

// RefreshAuth exchanges a worker token for a new one.
func (svc *WorkerService) RefreshAuth(worker string) (*library.Token, *Response, error) {
// set the API endpoint path we send the request to
u := fmt.Sprintf("/api/v1/workers/%s/refresh", worker)

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

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

return v, resp, err
}

// Update modifies a worker with the provided details.
func (svc *WorkerService) Update(worker string, w *library.Worker) (*library.Worker, *Response, error) {
// set the API endpoint path we send the request to
Expand Down
Loading