-
Notifications
You must be signed in to change notification settings - Fork 90
feat: enable support for using job token authentication #859
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ export default ( | |
| CI_PROJECT_URL, | ||
| CI_PROJECT_PATH, | ||
| CI_API_V4_URL, | ||
| CI_JOB_TOKEN, | ||
| GL_TOKEN, | ||
| GITLAB_TOKEN, | ||
| GL_URL, | ||
|
|
@@ -52,6 +53,8 @@ export default ( | |
| : "https://gitlab.com"); | ||
| return { | ||
| gitlabToken: GL_TOKEN || GITLAB_TOKEN, | ||
| isJobToken: (!!CI_JOB_TOKEN && (GL_TOKEN || GITLAB_TOKEN) === CI_JOB_TOKEN), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chore: I would create a variable to reuse this logic const isJobToken = (!!CI_JOB_TOKEN && (GL_TOKEN || GITLAB_TOKEN) === CI_JOB_TOKEN);
// ...
return {
// ...
isJobToken,
tokenHeader: isJobToken ? "JOB-TOKEN" : "PRIVATE-TOKEN",
// ... |
||
| tokenHeader: (!!CI_JOB_TOKEN && (GL_TOKEN || GITLAB_TOKEN) === CI_JOB_TOKEN) ? "JOB-TOKEN" : "PRIVATE-TOKEN", | ||
| gitlabUrl: defaultedGitlabUrl, | ||
| gitlabApiUrl: | ||
| userGitlabUrl && userGitlabApiPathPrefix | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import AggregateError from "aggregate-error"; | |
| import resolveConfig from "./resolve-config.js"; | ||
| import getProjectContext from "./get-project-context.js"; | ||
| import getError from "./get-error.js"; | ||
| import urlJoin from "url-join"; | ||
|
|
||
| const isNonEmptyString = (value) => isString(value) && value.trim(); | ||
| const isStringOrStringArray = (value) => | ||
|
|
@@ -30,7 +31,10 @@ export default async (pluginConfig, context) => { | |
| options: { repositoryUrl }, | ||
| logger, | ||
| } = context; | ||
| const { gitlabToken, gitlabUrl, gitlabApiUrl, proxy, ...options } = resolveConfig(pluginConfig, context); | ||
| const { gitlabToken, isJobToken, tokenHeader, successCommentCondition, failCommentCondition, gitlabUrl, gitlabApiUrl, proxy, ...options } = resolveConfig( | ||
| pluginConfig, | ||
| context | ||
| ); | ||
| const { projectPath, projectApiUrl } = getProjectContext(context, gitlabUrl, gitlabApiUrl, repositoryUrl); | ||
|
|
||
| debug("apiUrl: %o", gitlabApiUrl); | ||
|
|
@@ -53,30 +57,38 @@ export default async (pluginConfig, context) => { | |
| errors.push(getError("ENOGLTOKEN", { repositoryUrl })); | ||
| } | ||
|
|
||
| if (isJobToken && !(failCommentCondition === false) && !(successCommentCondition === false)) { | ||
| errors.push(getError("EJOBTOKENCOMMENTCONDITION", { projectPath })) | ||
| } | ||
|
Comment on lines
+60
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @arvest-bjoneson Will all the other endpoints listed at #846 (comment) work with the job token or do we need more checks like this? |
||
|
|
||
| if (gitlabToken && projectPath) { | ||
| let projectAccess; | ||
| let groupAccess; | ||
|
|
||
| logger.log("Verify GitLab authentication (%s)", gitlabApiUrl); | ||
|
|
||
| try { | ||
| ({ | ||
| permissions: { project_access: projectAccess, group_access: groupAccess }, | ||
| } = await got | ||
| .get(projectApiUrl, { | ||
| headers: { "PRIVATE-TOKEN": gitlabToken }, | ||
| ...proxy, | ||
| }) | ||
| .json()); | ||
| if ( | ||
| context.options.dryRun && | ||
| !((projectAccess && projectAccess.access_level >= 10) || (groupAccess && groupAccess.access_level >= 10)) | ||
| ) { | ||
| errors.push(getError("EGLNOPULLPERMISSION", { projectPath })); | ||
| } else if ( | ||
| !((projectAccess && projectAccess.access_level >= 30) || (groupAccess && groupAccess.access_level >= 30)) | ||
| ) { | ||
| errors.push(getError("EGLNOPUSHPERMISSION", { projectPath })); | ||
| if (isJobToken) { | ||
| await got.get(urlJoin(projectApiUrl, "releases"), { headers: { [tokenHeader]: gitlabToken } }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this enough to check whether the job token has enough permissions to push? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @fgreinacher , only reliable way at the moment is a dry run of |
||
| } else { | ||
| ({ | ||
| permissions: { project_access: projectAccess, group_access: groupAccess }, | ||
| } = await got | ||
| .get(projectApiUrl, { | ||
| headers: { [tokenHeader]: gitlabToken }, | ||
| ...proxy, | ||
| }) | ||
| .json()); | ||
| if ( | ||
| context.options.dryRun && | ||
| !((projectAccess && projectAccess.access_level >= 10) || (groupAccess && groupAccess.access_level >= 10)) | ||
| ) { | ||
| errors.push(getError("EGLNOPULLPERMISSION", { projectPath })); | ||
| } else if ( | ||
| !((projectAccess && projectAccess.access_level >= 30) || (groupAccess && groupAccess.access_level >= 30)) | ||
| ) { | ||
| errors.push(getError("EGLNOPUSHPERMISSION", { projectPath })); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| if (error.response && error.response.statusCode === 401) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -112,3 +112,32 @@ test.serial("Verify Github auth and release", async (t) => { | |||||
| t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]); | ||||||
| t.true(gitlab.isDone()); | ||||||
| }); | ||||||
|
|
||||||
| test.serial("Verify GitLab auth and release with Job Token", async (t) => { | ||||||
| const env = { GL_TOKEN: "gitlab_token", CI_JOB_TOKEN: "gitlab_token" }; | ||||||
| const owner = "test_user"; | ||||||
| const repo = "test_repo"; | ||||||
| const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` }; | ||||||
|
||||||
| const options = { repositoryUrl: `https://github.com/${owner}/${repo}.git` }; | |
| const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` }; |
Copilot uses AI. Check for mistakes.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to set the bar a bit higher as long as this is still feature flagged in GitLab. Could you expose a dedicated option to enable this behavior and also mention this more clearly here?