Skip to content

Commit 0732826

Browse files
author
Gauvain Pocentek
committed
Add support for oauth and anonymous auth in config/CLI
1 parent e9b1583 commit 0732826

File tree

5 files changed

+58
-11
lines changed

5 files changed

+58
-11
lines changed

docs/cli.rst

+8-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ parameters. You can override the values in each GitLab server section.
7070
- Integer
7171
- Number of seconds to wait for an answer before failing.
7272

73-
You must define the ``url`` and ``private_token`` in each GitLab server
74-
section.
73+
You must define the ``url`` in each GitLab server section.
74+
75+
Only one of ``private_token`` or ``oauth_token`` should be defined. If neither
76+
are defined an anonymous request will be sent to the Gitlab server, with very
77+
limited permissions.
7578

7679
.. list-table:: GitLab server options
7780
:header-rows: 1
@@ -83,6 +86,9 @@ section.
8386
* - ``private_token``
8487
- Your user token. Login/password is not supported. Refer to `the official
8588
documentation`__ to learn how to obtain a token.
89+
* - ``oauth_token``
90+
- An Oauth token for authentication. The Gitlab server must be configured
91+
to support this authentication method.
8692
* - ``api_version``
8793
- GitLab API version to use (``3`` or ``4``). Defaults to ``3`` for now,
8894
but will switch to ``4`` eventually.

gitlab/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def from_config(gitlab_id=None, config_files=None):
182182
"""
183183
config = gitlab.config.GitlabConfigParser(gitlab_id=gitlab_id,
184184
config_files=config_files)
185-
return Gitlab(config.url, private_token=config.token,
185+
return Gitlab(config.url, private_token=config.private_token,
186+
oauth_token=config.oauth_token,
186187
ssl_verify=config.ssl_verify, timeout=config.timeout,
187188
http_username=config.http_username,
188189
http_password=config.http_password,

gitlab/cli.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _get_base_parser():
8686
help="Verbose mode (legacy format only)",
8787
action="store_true")
8888
parser.add_argument("-d", "--debug",
89-
help="Debug mode (display HTTP requests",
89+
help="Debug mode (display HTTP requests)",
9090
action="store_true")
9191
parser.add_argument("-c", "--config-file", action='append',
9292
help=("Configuration file to use. Can be used "
@@ -147,7 +147,8 @@ def main():
147147

148148
try:
149149
gl = gitlab.Gitlab.from_config(gitlab_id, config_files)
150-
gl.auth()
150+
if gl.private_token or gl.oauth_token:
151+
gl.auth()
151152
except Exception as e:
152153
die(str(e))
153154

gitlab/config.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def __init__(self, gitlab_id=None, config_files=None):
5353

5454
try:
5555
self.url = self._config.get(self.gitlab_id, 'url')
56-
self.token = self._config.get(self.gitlab_id, 'private_token')
5756
except Exception:
5857
raise GitlabDataError("Impossible to get gitlab informations from "
5958
"configuration (%s)" % self.gitlab_id)
@@ -96,6 +95,29 @@ def __init__(self, gitlab_id=None, config_files=None):
9695
except Exception:
9796
pass
9897

98+
self.private_token = None
99+
try:
100+
self.private_token = self._config.get(self.gitlab_id,
101+
'private_token')
102+
except Exception:
103+
pass
104+
105+
self.oauth_token = None
106+
try:
107+
self.oauth_token = self._config.get(self.gitlab_id, 'oauth_token')
108+
except Exception:
109+
pass
110+
111+
self.http_username = None
112+
self.http_password = None
113+
try:
114+
self.http_username = self._config.get(self.gitlab_id,
115+
'http_username')
116+
self.http_password = self._config.get(self.gitlab_id,
117+
'http_password')
118+
except Exception:
119+
pass
120+
99121
self.http_username = None
100122
self.http_password = None
101123
try:

gitlab/tests/test_config.py

+22-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
url = https://three.url
4646
private_token = MNOPQR
4747
ssl_verify = /path/to/CA/bundle.crt
48+
49+
[four]
50+
url = https://four.url
51+
oauth_token = STUV
4852
"""
4953

5054
no_default_config = u"""[global]
@@ -85,8 +89,7 @@ def test_invalid_data(self, m_open):
8589
fd = six.StringIO(missing_attr_config)
8690
fd.close = mock.Mock(return_value=None)
8791
m_open.return_value = fd
88-
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
89-
gitlab_id='one')
92+
config.GitlabConfigParser('one')
9093
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
9194
gitlab_id='two')
9295
self.assertRaises(config.GitlabDataError, config.GitlabConfigParser,
@@ -101,7 +104,8 @@ def test_valid_data(self, m_open):
101104
cp = config.GitlabConfigParser()
102105
self.assertEqual("one", cp.gitlab_id)
103106
self.assertEqual("http://one.url", cp.url)
104-
self.assertEqual("ABCDEF", cp.token)
107+
self.assertEqual("ABCDEF", cp.private_token)
108+
self.assertEqual(None, cp.oauth_token)
105109
self.assertEqual(2, cp.timeout)
106110
self.assertEqual(True, cp.ssl_verify)
107111

@@ -111,7 +115,8 @@ def test_valid_data(self, m_open):
111115
cp = config.GitlabConfigParser(gitlab_id="two")
112116
self.assertEqual("two", cp.gitlab_id)
113117
self.assertEqual("https://two.url", cp.url)
114-
self.assertEqual("GHIJKL", cp.token)
118+
self.assertEqual("GHIJKL", cp.private_token)
119+
self.assertEqual(None, cp.oauth_token)
115120
self.assertEqual(10, cp.timeout)
116121
self.assertEqual(False, cp.ssl_verify)
117122

@@ -121,6 +126,18 @@ def test_valid_data(self, m_open):
121126
cp = config.GitlabConfigParser(gitlab_id="three")
122127
self.assertEqual("three", cp.gitlab_id)
123128
self.assertEqual("https://three.url", cp.url)
124-
self.assertEqual("MNOPQR", cp.token)
129+
self.assertEqual("MNOPQR", cp.private_token)
130+
self.assertEqual(None, cp.oauth_token)
125131
self.assertEqual(2, cp.timeout)
126132
self.assertEqual("/path/to/CA/bundle.crt", cp.ssl_verify)
133+
134+
fd = six.StringIO(valid_config)
135+
fd.close = mock.Mock(return_value=None)
136+
m_open.return_value = fd
137+
cp = config.GitlabConfigParser(gitlab_id="four")
138+
self.assertEqual("four", cp.gitlab_id)
139+
self.assertEqual("https://four.url", cp.url)
140+
self.assertEqual(None, cp.private_token)
141+
self.assertEqual("STUV", cp.oauth_token)
142+
self.assertEqual(2, cp.timeout)
143+
self.assertEqual(True, cp.ssl_verify)

0 commit comments

Comments
 (0)