| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b7f3342 commit cef3aa5
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,7 @@ per_page = ${GITLAB_PER_PAGE:-10} | |||
| 14 | 14 | url = ${GITLAB_URL:-https://gitlab.com} | |
| 15 | 15 | private_token = ${GITLAB_PRIVATE_TOKEN} | |
| 16 | 16 | oauth_token = ${GITLAB_OAUTH_TOKEN} | |
| 17 | + job_token = ${GITLAB_JOB_TOKEN} | ||
| 17 | 18 | http_username = ${GITLAB_HTTP_USERNAME} | |
| 18 | 19 | http_password = ${GITLAB_HTTP_PASSWORD} | |
| 19 | 20 | EOF | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,10 @@ To connect to a GitLab server, create a ``gitlab.Gitlab`` object: | |||
| 19 | 19 | # oauth token authentication | |
| 20 | 20 | gl = gitlab.Gitlab('http://10.0.0.1', oauth_token='my_long_token_here') | |
| 21 | 21 | ||
| 22 | + # job token authentication (to be used in CI) | ||
| 23 | + import os | ||
| 24 | + gl = gitlab.Gitlab('http://10.0.0.1', job_token=os.environ['CI_JOB_TOKEN']) | ||
| 25 | + | ||
| 22 | 26 | # username/password authentication (for GitLab << 10.2) | |
| 23 | 27 | gl = gitlab.Gitlab('http://10.0.0.1', email='jdoe', password='s3cr3t') | |
| 24 | 28 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -83,9 +83,9 @@ You must define the ``url`` in each GitLab server section. | |||
| 83 | 83 | If the GitLab server you are using redirects requests from http to https, | |
| 84 | 84 | make sure to use the ``https://`` protocol in the ``url`` definition. | |
| 85 | 85 | ||
| 86 | - Only one of ``private_token`` or ``oauth_token`` should be defined. If neither | ||
| 87 | - are defined an anonymous request will be sent to the Gitlab server, with very | ||
| 88 | - limited permissions. | ||
| 86 | + Only one of ``private_token``, ``oauth_token`` or ``job_token`` should be | ||
| 87 | + defined. If neither are defined an anonymous request will be sent to the Gitlab | ||
| 88 | + server, with very limited permissions. | ||
| 89 | 89 | ||
| 90 | 90 | .. list-table:: GitLab server options | |
| 91 | 91 | :header-rows: 1 | |
@@ -96,10 +96,12 @@ limited permissions. | |||
| 96 | 96 | - URL for the GitLab server | |
| 97 | 97 | * - ``private_token`` | |
| 98 | 98 | - Your user token. Login/password is not supported. Refer to `the official | |
| 99 | - documentation`__ to learn how to obtain a token. | ||
| 99 | + documentation`_pat to learn how to obtain a token. | ||
| 100 | 100 | * - ``oauth_token`` | |
| 101 | 101 | - An Oauth token for authentication. The Gitlab server must be configured | |
| 102 | 102 | to support this authentication method. | |
| 103 | + * - ``job_token`` | ||
| 104 | + - Your job token. See `the official documentation`_job-token to learn how to obtain a token. | ||
| 103 | 105 | * - ``api_version`` | |
| 104 | 106 | - GitLab API version to use (``3`` or ``4``). Defaults to ``4`` since | |
| 105 | 107 | version 1.3.0. | |
@@ -108,7 +110,8 @@ limited permissions. | |||
| 108 | 110 | * - ``http_password`` | |
| 109 | 111 | - Password for optional HTTP authentication | |
| 110 | 112 | ||
| 111 | - __ https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html | ||
| 113 | + .. _pat: https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html | ||
| 114 | + .. _job-token: https://docs.gitlab.com/ce/api/jobs.html#get-job-artifacts | ||
| 112 | 115 | ||
| 113 | 116 | CLI | |
| 114 | 117 | === | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,6 +60,7 @@ class Gitlab(object): | |||
| 60 | 60 | url (str): The URL of the GitLab server. | |
| 61 | 61 | private_token (str): The user private token | |
| 62 | 62 | oauth_token (str): An oauth token | |
| 63 | + job_token (str): A CI job token | ||
| 63 | 64 | email (str): The user email or login. | |
| 64 | 65 | password (str): The user password (associated with email). | |
| 65 | 66 | ssl_verify (bool|str): Whether SSL certificates should be validated. If | |
@@ -76,6 +77,7 @@ def __init__( | |||
| 76 | 77 | url, | |
| 77 | 78 | private_token=None, | |
| 78 | 79 | oauth_token=None, | |
| 80 | + job_token=None, | ||
| 79 | 81 | email=None, | |
| 80 | 82 | password=None, | |
| 81 | 83 | ssl_verify=True, | |
@@ -107,6 +109,7 @@ def __init__( | |||
| 107 | 109 | self.http_username = http_username | |
| 108 | 110 | self.http_password = http_password | |
| 109 | 111 | self.oauth_token = oauth_token | |
| 112 | + self.job_token = job_token | ||
| 110 | 113 | self._set_auth_info() | |
| 111 | 114 | ||
| 112 | 115 | #: Create a session object for requests | |
@@ -195,6 +198,7 @@ def from_config(cls, gitlab_id=None, config_files=None): | |||
| 195 | 198 | config.url, | |
| 196 | 199 | private_token=config.private_token, | |
| 197 | 200 | oauth_token=config.oauth_token, | |
| 201 | + job_token=config.job_token, | ||
| 198 | 202 | ssl_verify=config.ssl_verify, | |
| 199 | 203 | timeout=config.timeout, | |
| 200 | 204 | http_username=config.http_username, | |
@@ -211,7 +215,7 @@ def auth(self): | |||
| 211 | 215 | The `user` attribute will hold a `gitlab.objects.CurrentUser` object on | |
| 212 | 216 | success. | |
| 213 | 217 | """ | |
| 214 | - if self.private_token or self.oauth_token: | ||
| 218 | + if self.private_token or self.oauth_token or self.job_token: | ||
| 215 | 219 | self._token_auth() | |
| 216 | 220 | else: | |
| 217 | 221 | self._credentials_auth() | |
@@ -346,9 +350,16 @@ def _construct_url(self, id_, obj, parameters, action=None): | |||
| 346 | 350 | return url | |
| 347 | 351 | ||
| 348 | 352 | def _set_auth_info(self): | |
| 349 | - if self.private_token and self.oauth_token: | ||
| 353 | + if ( | ||
| 354 | + sum( | ||
| 355 | + bool(arg) | ||
| 356 | + for arg in [self.private_token, self.oauth_token, self.job_token] | ||
| 357 | + ) | ||
| 358 | + != 1 | ||
| 359 | + ): | ||
| 350 | 360 | raise ValueError( | |
| 351 | - "Only one of private_token or oauth_token should " "be defined" | ||
| 361 | + "Only one of private_token, oauth_token or job_token should " | ||
| 362 | + "be defined" | ||
| 352 | 363 | ) | |
| 353 | 364 | if (self.http_username and not self.http_password) or ( | |
| 354 | 365 | not self.http_username and self.http_password | |
@@ -364,12 +375,19 @@ def _set_auth_info(self): | |||
| 364 | 375 | ||
| 365 | 376 | self._http_auth = None | |
| 366 | 377 | if self.private_token: | |
| 367 | - self.headers["PRIVATE-TOKEN"] = self.private_token | ||
| 368 | 378 | self.headers.pop("Authorization", None) | |
| 379 | + self.headers["PRIVATE-TOKEN"] = self.private_token | ||
| 380 | + self.headers.pop("JOB-TOKEN", None) | ||
| 369 | 381 | ||
| 370 | 382 | if self.oauth_token: | |
| 371 | 383 | self.headers["Authorization"] = "Bearer %s" % self.oauth_token | |
| 372 | 384 | self.headers.pop("PRIVATE-TOKEN", None) | |
| 385 | + self.headers.pop("JOB-TOKEN", None) | ||
| 386 | + | ||
| 387 | + if self.job_token: | ||
| 388 | + self.headers.pop("Authorization", None) | ||
| 389 | + self.headers.pop("PRIVATE-TOKEN", None) | ||
| 390 | + self.headers["JOB-TOKEN"] = self.job_token | ||
| 373 | 391 | ||
| 374 | 392 | if self.http_username: | |
| 375 | 393 | self._http_auth = requests.auth.HTTPBasicAuth( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -202,7 +202,7 @@ def main(): | |||
| 202 | 202 | ||
| 203 | 203 | try: | |
| 204 | 204 | gl = gitlab.Gitlab.from_config(gitlab_id, config_files) | |
| 205 | - if gl.private_token or gl.oauth_token: | ||
| 205 | + if gl.private_token or gl.oauth_token or gl.job_token: | ||
| 206 | 206 | gl.auth() | |
| 207 | 207 | except Exception as e: | |
| 208 | 208 | die(str(e)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -122,6 +122,12 @@ def __init__(self, gitlab_id=None, config_files=None): | |||
| 122 | 122 | except Exception: | |
| 123 | 123 | pass | |
| 124 | 124 | ||
| 125 | + self.job_token = None | ||
| 126 | + try: | ||
| 127 | + self.job_token = self._config.get(self.gitlab_id, "job_token") | ||
| 128 | + except Exception: | ||
| 129 | + pass | ||
| 130 | + | ||
| 125 | 131 | self.http_username = None | |
| 126 | 132 | self.http_password = None | |
| 127 | 133 | try: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -403,17 +403,31 @@ def test_private_token_auth(self): | |||
| 403 | 403 | gl = Gitlab("http://localhost", private_token="private_token", api_version="4") | |
| 404 | 404 | self.assertEqual(gl.private_token, "private_token") | |
| 405 | 405 | self.assertEqual(gl.oauth_token, None) | |
| 406 | + self.assertEqual(gl.job_token, None) | ||
| 406 | 407 | self.assertEqual(gl._http_auth, None) | |
| 407 | - self.assertEqual(gl.headers["PRIVATE-TOKEN"], "private_token") | ||
| 408 | 408 | self.assertNotIn("Authorization", gl.headers) | |
| 409 | + self.assertEqual(gl.headers["PRIVATE-TOKEN"], "private_token") | ||
| 410 | + self.assertNotIn("JOB-TOKEN", gl.headers) | ||
| 409 | 411 | ||
| 410 | 412 | def test_oauth_token_auth(self): | |
| 411 | 413 | gl = Gitlab("http://localhost", oauth_token="oauth_token", api_version="4") | |
| 412 | 414 | self.assertEqual(gl.private_token, None) | |
| 413 | 415 | self.assertEqual(gl.oauth_token, "oauth_token") | |
| 416 | + self.assertEqual(gl.job_token, None) | ||
| 414 | 417 | self.assertEqual(gl._http_auth, None) | |
| 415 | 418 | self.assertEqual(gl.headers["Authorization"], "Bearer oauth_token") | |
| 416 | 419 | self.assertNotIn("PRIVATE-TOKEN", gl.headers) | |
| 420 | + self.assertNotIn("JOB-TOKEN", gl.headers) | ||
| 421 | + | ||
| 422 | + def test_job_token_auth(self): | ||
| 423 | + gl = Gitlab("http://localhost", job_token="CI_JOB_TOKEN", api_version="4") | ||
| 424 | + self.assertEqual(gl.private_token, None) | ||
| 425 | + self.assertEqual(gl.oauth_token, None) | ||
| 426 | + self.assertEqual(gl.job_token, "CI_JOB_TOKEN") | ||
| 427 | + self.assertEqual(gl._http_auth, None) | ||
| 428 | + self.assertNotIn("Authorization", gl.headers) | ||
| 429 | + self.assertNotIn("PRIVATE-TOKEN", gl.headers) | ||
| 430 | + self.assertEqual(gl.headers["JOB-TOKEN"], "CI_JOB_TOKEN") | ||
| 417 | 431 | ||
| 418 | 432 | def test_http_auth(self): | |
| 419 | 433 | gl = Gitlab( | |
@@ -425,6 +439,7 @@ def test_http_auth(self): | |||
| 425 | 439 | ) | |
| 426 | 440 | self.assertEqual(gl.private_token, "private_token") | |
| 427 | 441 | self.assertEqual(gl.oauth_token, None) | |
| 442 | + self.assertEqual(gl.job_token, None) | ||
| 428 | 443 | self.assertIsInstance(gl._http_auth, requests.auth.HTTPBasicAuth) | |
| 429 | 444 | self.assertEqual(gl.headers["PRIVATE-TOKEN"], "private_token") | |
| 430 | 445 | self.assertNotIn("Authorization", gl.headers) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments