Skip to content

Commit 65a4d8a

Browse files
committed
fix: merge with main
2 parents 0800322 + 73626fb commit 65a4d8a

86 files changed

Lines changed: 5889 additions & 1724 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/CODE_OF_CONDUCT.md

Lines changed: 0 additions & 74 deletions
This file was deleted.

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
uses: actions/checkout@v3
1717

1818
- name: install go
19-
uses: actions/setup-go@v3
19+
uses: actions/setup-go@v4
2020
with:
2121
# use version from go.mod file
2222
go-version-file: 'go.mod'

.github/workflows/prerelease.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
fetch-depth: 0
2121

2222
- name: install go
23-
uses: actions/setup-go@v3
23+
uses: actions/setup-go@v4
2424
with:
2525
# use version from go.mod file
2626
go-version-file: 'go.mod'
@@ -40,7 +40,7 @@ jobs:
4040
make build-static-ci
4141
4242
- name: publish
43-
uses: elgohr/Publish-Docker-Github-Action@v4
43+
uses: elgohr/Publish-Docker-Github-Action@v5
4444
with:
4545
name: target/vela-worker
4646
cache: true
@@ -49,7 +49,7 @@ jobs:
4949
password: ${{ secrets.DOCKER_PASSWORD }}
5050

5151
- name: publish-alpine
52-
uses: elgohr/Publish-Docker-Github-Action@v4
52+
uses: elgohr/Publish-Docker-Github-Action@v5
5353
with:
5454
name: target/vela-worker
5555
cache: true

.github/workflows/publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
fetch-depth: 0
2020

2121
- name: install go
22-
uses: actions/setup-go@v3
22+
uses: actions/setup-go@v4
2323
with:
2424
# use version from go.mod file
2525
go-version-file: 'go.mod'
@@ -34,15 +34,15 @@ jobs:
3434
make build-static-ci
3535
3636
- name: publish
37-
uses: elgohr/Publish-Docker-Github-Action@v4
37+
uses: elgohr/Publish-Docker-Github-Action@v5
3838
with:
3939
name: target/vela-worker
4040
cache: true
4141
username: ${{ secrets.DOCKER_USERNAME }}
4242
password: ${{ secrets.DOCKER_PASSWORD }}
4343

4444
- name: publish-alpine
45-
uses: elgohr/Publish-Docker-Github-Action@v4
45+
uses: elgohr/Publish-Docker-Github-Action@v5
4646
with:
4747
name: target/vela-worker
4848
cache: true

.github/workflows/reviewdog.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
uses: actions/checkout@v3
1515

1616
- name: install go
17-
uses: actions/setup-go@v3
17+
uses: actions/setup-go@v4
1818
with:
1919
# use version from go.mod file
2020
go-version-file: 'go.mod'
@@ -37,7 +37,7 @@ jobs:
3737
uses: actions/checkout@v3
3838

3939
- name: install go
40-
uses: actions/setup-go@v3
40+
uses: actions/setup-go@v4
4141
with:
4242
# use version from go.mod file
4343
go-version-file: 'go.mod'

.github/workflows/spec.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
uses: actions/checkout@v3
1717

1818
- name: install go
19-
uses: actions/setup-go@v3
19+
uses: actions/setup-go@v4
2020
with:
2121
# use version from go.mod file
2222
go-version-file: 'go.mod'
@@ -29,8 +29,8 @@ jobs:
2929
3030
- name: create spec
3131
run: |
32-
make spec-install
33-
make spec
32+
sudo make spec-install
33+
sudo make spec
3434
3535
- name: upload spec
3636
uses: skx/github-action-publish-binaries@master

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
uses: actions/checkout@v3
1717

1818
- name: install go
19-
uses: actions/setup-go@v3
19+
uses: actions/setup-go@v4
2020
with:
2121
# use version from go.mod file
2222
go-version-file: 'go.mod'

.github/workflows/validate.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
uses: actions/checkout@v3
1717

1818
- name: install go
19-
uses: actions/setup-go@v3
19+
uses: actions/setup-go@v4
2020
with:
2121
# use version from go.mod file
2222
go-version-file: 'go.mod'

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ release/
3535
*.iws
3636
*.xml
3737

38+
# VSCode project folder
39+
.vscode/
40+
41+
# VSCode project files
42+
__debug_bin
43+
3844
# Secrets environment file
3945
secrets.env
4046

api/register.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
2+
//
3+
// Use of this source code is governed by the LICENSE file in this repository.
4+
5+
package api
6+
7+
import (
8+
"fmt"
9+
"net/http"
10+
11+
"github.com/gin-gonic/gin"
12+
"github.com/go-vela/worker/router/middleware/token"
13+
"github.com/golang-jwt/jwt/v5"
14+
)
15+
16+
// swagger:operation POST /register system Register
17+
//
18+
// Fill registration token channel in worker to continue operation
19+
//
20+
// ---
21+
// produces:
22+
// - application/json
23+
// parameters:
24+
// security:
25+
// - ApiKeyAuth: []
26+
// responses:
27+
// '200':
28+
// description: Successfully passed token to worker
29+
// schema:
30+
// type: string
31+
// '401':
32+
// description: No token was passed
33+
// schema:
34+
// "$ref": "#/definitions/Error"
35+
// '500':
36+
// description: Unable to pass token to worker
37+
// schema:
38+
// "$ref": "#/definitions/Error"
39+
40+
// Register will pass the token given in the request header to the register token
41+
// channel of the worker. This will unblock operation if the worker has not been
42+
// registered and the provided registration token is valid.
43+
func Register(c *gin.Context) {
44+
// extract the worker hostname that was packed into gin context
45+
w, ok := c.Get("worker-hostname")
46+
if !ok {
47+
c.JSON(http.StatusInternalServerError, "no worker hostname in the context")
48+
return
49+
}
50+
51+
// extract the register token channel that was packed into gin context
52+
v, ok := c.Get("register-token")
53+
if !ok {
54+
c.JSON(http.StatusInternalServerError, "no register token channel in the context")
55+
return
56+
}
57+
58+
// make sure we configured the channel properly
59+
rChan, ok := v.(chan string)
60+
if !ok {
61+
c.JSON(http.StatusInternalServerError, "register token channel in the context is the wrong type")
62+
return
63+
}
64+
65+
// if token is present in the channel, deny registration
66+
// this will likely never happen as the channel is offloaded immediately
67+
if len(rChan) > 0 {
68+
c.JSON(http.StatusOK, "worker already registered")
69+
return
70+
}
71+
72+
// retrieve auth token from header
73+
token, err := token.Retrieve(c.Request)
74+
if err != nil {
75+
// an error occurs when no token was passed
76+
c.JSON(http.StatusUnauthorized, err)
77+
return
78+
}
79+
80+
// extract the subject from the token
81+
sub, err := getSubjectFromToken(token)
82+
if err != nil {
83+
c.JSON(http.StatusUnauthorized, err)
84+
return
85+
}
86+
87+
// make sure we configured the hostname properly
88+
hostname, ok := w.(string)
89+
if !ok {
90+
c.JSON(http.StatusInternalServerError, "worker hostname in the context is the wrong type")
91+
return
92+
}
93+
94+
// if the subject doesn't match the worker hostname return an error
95+
if sub != hostname {
96+
c.JSON(http.StatusUnauthorized, "worker hostname is invalid")
97+
return
98+
}
99+
100+
// write registration token to auth token channel
101+
rChan <- token
102+
103+
c.JSON(http.StatusOK, "successfully passed token to worker")
104+
}
105+
106+
// getSubjectFromToken is a helper function to extract
107+
// the subject from the token claims.
108+
func getSubjectFromToken(token string) (string, error) {
109+
// create a new JWT parser
110+
j := jwt.NewParser()
111+
112+
// parse the payload
113+
t, _, err := j.ParseUnverified(token, jwt.MapClaims{})
114+
if err != nil {
115+
return "", fmt.Errorf("unable to parse token")
116+
}
117+
118+
sub, err := t.Claims.GetSubject()
119+
if err != nil {
120+
return "", fmt.Errorf("unable to get subject from token")
121+
}
122+
123+
// make sure there was a subject defined
124+
if len(sub) == 0 {
125+
return "", fmt.Errorf("no subject defined in token")
126+
}
127+
128+
return sub, nil
129+
}

0 commit comments

Comments
 (0)