Skip to content

Job token authentication fails #1620

Closed
Closed
@axl89

Description

@axl89

Description of the problem, including code/CLI snippet

The following python3 snippet fails running under Gitlab 13.12.5-ee CI pipeline:

import os, gitlab
gl = gitlab.Gitlab(os.environ['CI_SERVER_URL'], job_token=os.environ['CI_JOB_TOKEN'], user_agent='Whatever/custom-name')
gl.auth()

It's using python-gitlab v2.10.1 and it fails with:

Traceback (most recent call last):
  File "/builds/CbQEQmZ2/0/whatever/tools/project_name/./app.py", line 3, in <module>
    gl.auth()
  File "/usr/local/lib/python3.10/site-packages/gitlab/client.py", line 257, in auth
    self.user = self._objects.CurrentUserManager(self).get()
  File "/usr/local/lib/python3.10/site-packages/gitlab/exceptions.py", line 304, in wrapped_f
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/gitlab/mixins.py", line 146, in get
    server_data = self.gitlab.http_get(self.path, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/gitlab/client.py", line 662, in http_get
    result = self.http_request(
  File "/usr/local/lib/python3.10/site-packages/gitlab/client.py", line 622, in http_request
    raise gitlab.exceptions.GitlabAuthenticationError(
gitlab.exceptions.GitlabAuthenticationError: 401: 401 Unauthorized

The .gitlab-ci.yml config is as follows:

stages:
  - run

run:
  stage: run
  rules:
    - if: '$CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "schedule"'
  image: python:alpine
  before_script:
    - pip3 install -r requirements.txt
  script:
    - python3 app.py # This is where the snippet lives

Expected Behavior

As the documentation says, the following code should be valid:

import gitlab
# job token authentication (to be used in CI)
import os
gl = gitlab.Gitlab('http://10.0.0.1', job_token=os.environ['CI_JOB_TOKEN'])
# make an API request to create the gl.user object. This is mandatory if you
# use the username/password authentication.
gl.auth()

Actual Behavior

The gl.auth() method fails with a GitlabAuthenticationError exception.

Specifications

  • python-gitlab version: v2.10.1
  • API version you are using (v3/v4): v4
  • Gitlab server version (or gitlab.com): 13.12.5-ee

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions