Skip to content

Commit d04c8df

Browse files
ChristopherHXsilverwind
authored andcommitted
Add artifacts v4 jwt to job message and accept it (go-gitea#28885)
This change allows act_runner / actions_runner to use jwt tokens for `ACTIONS_RUNTIME_TOKEN` that are compatible with actions/upload-artifact@v4. The official Artifact actions are now validating and extracting the jwt claim scp to get the runid and jobid, the old artifact backend also needs to accept the same token jwt. --- Related to go-gitea#28853 I'm not familar with the auth system, maybe you know how to improve this I have tested - the jwt token is a valid token for artifact uploading - the jwt token can be parsed by actions/upload-artifact@v4 and passes their scp claim validation Next steps would be a new artifacts@v4 backend. ~~I'm linking the act_runner change soonish.~~ act_runner change to make the change effective and use jwt tokens <https://gitea.com/gitea/act_runner/pulls/471>
1 parent 9d66903 commit d04c8df

File tree

4 files changed

+166
-6
lines changed

4 files changed

+166
-6
lines changed

routers/api/actions/artifacts.go

+28-6
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import (
7878
"code.gitea.io/gitea/modules/util"
7979
"code.gitea.io/gitea/modules/web"
8080
web_types "code.gitea.io/gitea/modules/web/types"
81+
actions_service "code.gitea.io/gitea/services/actions"
8182
)
8283

8384
const artifactRouteBase = "/_apis/pipelines/workflows/{run_id}/artifacts"
@@ -137,12 +138,33 @@ func ArtifactContexter() func(next http.Handler) http.Handler {
137138
return
138139
}
139140

140-
authToken := strings.TrimPrefix(authHeader, "Bearer ")
141-
task, err := actions.GetRunningTaskByToken(req.Context(), authToken)
142-
if err != nil {
143-
log.Error("Error runner api getting task: %v", err)
144-
ctx.Error(http.StatusInternalServerError, "Error runner api getting task")
145-
return
141+
// New act_runner uses jwt to authenticate
142+
tID, err := actions_service.ParseAuthorizationToken(req)
143+
144+
var task *actions.ActionTask
145+
if err == nil {
146+
147+
task, err = actions.GetTaskByID(req.Context(), tID)
148+
if err != nil {
149+
log.Error("Error runner api getting task by ID: %v", err)
150+
ctx.Error(http.StatusInternalServerError, "Error runner api getting task by ID")
151+
return
152+
}
153+
if task.Status != actions.StatusRunning {
154+
log.Error("Error runner api getting task: task is not running")
155+
ctx.Error(http.StatusInternalServerError, "Error runner api getting task: task is not running")
156+
return
157+
}
158+
} else {
159+
// Old act_runner uses GITEA_TOKEN to authenticate
160+
authToken := strings.TrimPrefix(authHeader, "Bearer ")
161+
162+
task, err = actions.GetRunningTaskByToken(req.Context(), authToken)
163+
if err != nil {
164+
log.Error("Error runner api getting task: %v", err)
165+
ctx.Error(http.StatusInternalServerError, "Error runner api getting task")
166+
return
167+
}
146168
}
147169

148170
if err := task.LoadJob(req.Context()); err != nil {

routers/api/actions/runner/utils.go

+6
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
151151

152152
refName := git.RefName(ref)
153153

154+
giteaRuntimeToken, err := actions.CreateAuthorizationToken(t.ID, t.Job.RunID, t.JobID)
155+
if err != nil {
156+
log.Error("actions.CreateAuthorizationToken failed: %v", err)
157+
}
158+
154159
taskContext, err := structpb.NewStruct(map[string]any{
155160
// standard contexts, see https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
156161
"action": "", // string, The name of the action currently running, or the id of a step. GitHub removes special characters, and uses the name __run when the current step runs a script without an id. If you use the same action more than once in the same job, the name will include a suffix with the sequence number with underscore before it. For example, the first script you run will have the name __run, and the second script will be named __run_2. Similarly, the second invocation of actions/checkout will be actionscheckout2.
@@ -190,6 +195,7 @@ func generateTaskContext(t *actions_model.ActionTask) *structpb.Struct {
190195

191196
// additional contexts
192197
"gitea_default_actions_url": setting.Actions.DefaultActionsURL.URL(),
198+
"gitea_runtime_token": giteaRuntimeToken,
193199
})
194200
if err != nil {
195201
log.Error("structpb.NewStruct failed: %v", err)

services/actions/auth.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"fmt"
8+
"net/http"
9+
"strings"
10+
"time"
11+
12+
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/setting"
14+
15+
"github.com/golang-jwt/jwt/v5"
16+
)
17+
18+
type actionsClaims struct {
19+
jwt.RegisteredClaims
20+
Scp string `json:"scp"`
21+
TaskID int64
22+
RunID int64
23+
JobID int64
24+
}
25+
26+
func CreateAuthorizationToken(taskID, runID, jobID int64) (string, error) {
27+
now := time.Now()
28+
29+
claims := actionsClaims{
30+
RegisteredClaims: jwt.RegisteredClaims{
31+
ExpiresAt: jwt.NewNumericDate(now.Add(24 * time.Hour)),
32+
NotBefore: jwt.NewNumericDate(now),
33+
},
34+
Scp: fmt.Sprintf("Actions.Results:%d:%d", runID, jobID),
35+
TaskID: taskID,
36+
RunID: runID,
37+
JobID: jobID,
38+
}
39+
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
40+
41+
tokenString, err := token.SignedString([]byte(setting.SecretKey))
42+
if err != nil {
43+
return "", err
44+
}
45+
46+
return tokenString, nil
47+
}
48+
49+
func ParseAuthorizationToken(req *http.Request) (int64, error) {
50+
h := req.Header.Get("Authorization")
51+
if h == "" {
52+
return 0, nil
53+
}
54+
55+
parts := strings.SplitN(h, " ", 2)
56+
if len(parts) != 2 {
57+
log.Error("split token failed: %s", h)
58+
return 0, fmt.Errorf("split token failed")
59+
}
60+
61+
token, err := jwt.ParseWithClaims(parts[1], &actionsClaims{}, func(t *jwt.Token) (any, error) {
62+
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
63+
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
64+
}
65+
return []byte(setting.SecretKey), nil
66+
})
67+
if err != nil {
68+
return 0, err
69+
}
70+
71+
c, ok := token.Claims.(*actionsClaims)
72+
if !token.Valid || !ok {
73+
return 0, fmt.Errorf("invalid token claim")
74+
}
75+
76+
return c.TaskID, nil
77+
}

services/actions/auth_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"net/http"
8+
"testing"
9+
10+
"code.gitea.io/gitea/modules/setting"
11+
12+
"github.com/golang-jwt/jwt/v5"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestCreateAuthorizationToken(t *testing.T) {
17+
var taskID int64 = 23
18+
token, err := CreateAuthorizationToken(taskID, 1, 2)
19+
assert.Nil(t, err)
20+
assert.NotEqual(t, "", token)
21+
claims := jwt.MapClaims{}
22+
_, err = jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (interface{}, error) {
23+
return []byte(setting.SecretKey), nil
24+
})
25+
assert.Nil(t, err)
26+
scp, ok := claims["scp"]
27+
assert.True(t, ok, "Has scp claim in jwt token")
28+
assert.Contains(t, scp, "Actions.Results:1:2")
29+
taskIDClaim, ok := claims["TaskID"]
30+
assert.True(t, ok, "Has TaskID claim in jwt token")
31+
assert.Equal(t, float64(taskID), taskIDClaim, "Supplied taskid must match stored one")
32+
}
33+
34+
func TestParseAuthorizationToken(t *testing.T) {
35+
var taskID int64 = 23
36+
token, err := CreateAuthorizationToken(taskID, 1, 2)
37+
assert.Nil(t, err)
38+
assert.NotEqual(t, "", token)
39+
headers := http.Header{}
40+
headers.Set("Authorization", "Bearer "+token)
41+
rTaskID, err := ParseAuthorizationToken(&http.Request{
42+
Header: headers,
43+
})
44+
assert.Nil(t, err)
45+
assert.Equal(t, taskID, rTaskID)
46+
}
47+
48+
func TestParseAuthorizationTokenNoAuthHeader(t *testing.T) {
49+
headers := http.Header{}
50+
rTaskID, err := ParseAuthorizationToken(&http.Request{
51+
Header: headers,
52+
})
53+
assert.Nil(t, err)
54+
assert.Equal(t, int64(0), rTaskID)
55+
}

0 commit comments

Comments
 (0)