-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Backport #32527 We have some actions that leverage the Gitea API that began receiving 401 errors, with a message that the user was not found. These actions use the `ACTIONS_RUNTIME_TOKEN` env var in the actions job to authenticate with the Gitea API. The format of this env var in actions jobs changed with /pull/28885 to be a JWT (with a corresponding update to `act_runner`) Since it was a JWT, the OAuth parsing logic attempted to parse it as an OAuth token, and would return user not found, instead of falling back to look up the running task and assigning it to the actions user. Make ACTIONS_RUNTIME_TOKEN in action runners could be used, attempting to parse Oauth JWTs. The code to parse potential old `ACTION_RUNTIME_TOKEN` was kept in case someone is running an older version of act_runner that doesn't support the Actions JWT.
- Loading branch information
Showing
4 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package auth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"code.gitea.io/gitea/models/unittest" | ||
user_model "code.gitea.io/gitea/models/user" | ||
"code.gitea.io/gitea/modules/web/middleware" | ||
"code.gitea.io/gitea/services/actions" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUserIDFromToken(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
t.Run("Actions JWT", func(t *testing.T) { | ||
const RunningTaskID = 47 | ||
token, err := actions.CreateAuthorizationToken(RunningTaskID, 1, 2) | ||
assert.NoError(t, err) | ||
|
||
ds := make(middleware.ContextData) | ||
|
||
o := OAuth2{} | ||
uid := o.userIDFromToken(context.Background(), token, ds) | ||
assert.Equal(t, int64(user_model.ActionsUserID), uid) | ||
assert.Equal(t, ds["IsActionsToken"], true) | ||
assert.Equal(t, ds["ActionsTaskID"], int64(RunningTaskID)) | ||
}) | ||
} | ||
|
||
func TestCheckTaskIsRunning(t *testing.T) { | ||
assert.NoError(t, unittest.PrepareTestDatabase()) | ||
|
||
cases := map[string]struct { | ||
TaskID int64 | ||
Expected bool | ||
}{ | ||
"Running": {TaskID: 47, Expected: true}, | ||
"Missing": {TaskID: 1, Expected: false}, | ||
"Cancelled": {TaskID: 46, Expected: false}, | ||
} | ||
|
||
for name := range cases { | ||
c := cases[name] | ||
t.Run(name, func(t *testing.T) { | ||
actual := CheckTaskIsRunning(context.Background(), c.TaskID) | ||
assert.Equal(t, c.Expected, actual) | ||
}) | ||
} | ||
} |