-
Notifications
You must be signed in to change notification settings - Fork 675
feat: add keys endpoint #1490
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
Merged
Merged
feat: add keys endpoint #1490
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,28 @@ | ||
| #### | ||
| Keys | ||
| #### | ||
|
|
||
| Keys | ||
| ==== | ||
|
|
||
| Reference | ||
| --------- | ||
|
|
||
| * v4 API | ||
|
|
||
| + :class:`gitlab.v4.objects.Key` | ||
| + :class:`gitlab.v4.objects.KeyManager` | ||
| + :attr:`gitlab.Gitlab.keys` | ||
|
|
||
| * GitLab API: https://docs.gitlab.com/ce/api/keys.html | ||
|
|
||
| Examples | ||
| -------- | ||
|
|
||
| Get an ssh key by its id (requires admin access):: | ||
|
|
||
| key = gl.keys.get(key_id) | ||
|
|
||
| Get an ssh key (requires admin access) or a deploy key by its fingerprint:: | ||
|
|
||
| key = gl.keys.get(fingerprint="SHA256:ERJJ/OweAM6jA8OjJ/gXs4N5fqUaREEJnz/EyfywfXY") | ||
This file contains hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| from gitlab.base import RESTManager, RESTObject | ||
| from gitlab.mixins import GetMixin | ||
|
|
||
| __all__ = [ | ||
| "Key", | ||
| "KeyManager", | ||
| ] | ||
|
|
||
|
|
||
| class Key(RESTObject): | ||
| pass | ||
|
|
||
|
|
||
| class KeyManager(GetMixin, RESTManager): | ||
| _path = "/keys" | ||
| _obj_cls = Key | ||
|
|
||
| def get(self, id=None, **kwargs): | ||
| if id is not None: | ||
| return super(KeyManager, self).get(id, **kwargs) | ||
|
|
||
| if "fingerprint" not in kwargs: | ||
| raise AttributeError("Missing attribute: id or fingerprint") | ||
|
|
||
| server_data = self.gitlab.http_get(self.path, **kwargs) | ||
| return self._obj_cls(self, server_data) |
This file contains hidden or 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,42 @@ | ||
| """ | ||
| GitLab API: | ||
| https://docs.gitlab.com/ce/api/keys.html | ||
| """ | ||
| import base64 | ||
| import hashlib | ||
|
|
||
|
|
||
| def key_fingerprint(key): | ||
| key_part = key.split()[1] | ||
| decoded = base64.b64decode(key_part.encode("ascii")) | ||
| digest = hashlib.sha256(decoded).digest() | ||
| return "SHA256:" + base64.b64encode(digest).rstrip(b"=").decode("utf-8") | ||
|
|
||
|
|
||
| def test_keys_ssh(gl, user, SSH_KEY): | ||
| key = user.keys.create({"title": "foo@bar", "key": SSH_KEY}) | ||
|
|
||
| # Get key by ID (admin only). | ||
| key_by_id = gl.keys.get(key.id) | ||
| assert key_by_id.title == key.title | ||
| assert key_by_id.key == key.key | ||
|
|
||
| fingerprint = key_fingerprint(SSH_KEY) | ||
| # Get key by fingerprint (admin only). | ||
| key_by_fingerprint = gl.keys.get(fingerprint=fingerprint) | ||
| assert key_by_fingerprint.title == key.title | ||
| assert key_by_fingerprint.key == key.key | ||
|
|
||
| key.delete() | ||
|
|
||
|
|
||
| def test_keys_deploy(gl, project, DEPLOY_KEY): | ||
| key = project.keys.create({"title": "foo@bar", "key": DEPLOY_KEY}) | ||
|
|
||
| fingerprint = key_fingerprint(DEPLOY_KEY) | ||
| key_by_fingerprint = gl.keys.get(fingerprint=fingerprint) | ||
| assert key_by_fingerprint.title == key.title | ||
| assert key_by_fingerprint.key == key.key | ||
| assert len(key_by_fingerprint.deploy_keys_projects) == 1 | ||
|
|
||
| key.delete() |
This file contains hidden or 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,54 @@ | ||
| """ | ||
| GitLab API: https://docs.gitlab.com/ce/api/keys.html | ||
| """ | ||
| import pytest | ||
| import responses | ||
|
|
||
| from gitlab.v4.objects import Key | ||
|
|
||
| key_content = {"id": 1, "title": "title", "key": "ssh-keytype AAAAC3Nza/key comment"} | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def resp_get_key_by_id(): | ||
| with responses.RequestsMock() as rsps: | ||
| rsps.add( | ||
| method=responses.GET, | ||
| url="http://localhost/api/v4/keys/1", | ||
| json=key_content, | ||
| content_type="application/json", | ||
| status=200, | ||
| ) | ||
| yield rsps | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def resp_get_key_by_fingerprint(): | ||
| with responses.RequestsMock() as rsps: | ||
| rsps.add( | ||
| method=responses.GET, | ||
| url="http://localhost/api/v4/keys?fingerprint=foo", | ||
| json=key_content, | ||
| content_type="application/json", | ||
| status=200, | ||
| ) | ||
| yield rsps | ||
|
|
||
|
|
||
| def test_get_key_by_id(gl, resp_get_key_by_id): | ||
| key = gl.keys.get(1) | ||
| assert isinstance(key, Key) | ||
| assert key.id == 1 | ||
| assert key.title == "title" | ||
|
|
||
|
|
||
| def test_get_key_by_fingerprint(gl, resp_get_key_by_fingerprint): | ||
| key = gl.keys.get(fingerprint="foo") | ||
| assert isinstance(key, Key) | ||
| assert key.id == 1 | ||
| assert key.title == "title" | ||
|
|
||
|
|
||
| def test_get_key_missing_attrs(gl): | ||
| with pytest.raises(AttributeError): | ||
| gl.keys.get() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.