- Notifications
You must be signed in to change notification settings - Fork673
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsdocs/api-objects.rst
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
28 changes: 28 additions & 0 deletionsdocs/gl_objects/keys.rst
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 | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
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") |
1 change: 1 addition & 0 deletionsgitlab/client.py
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
1 change: 1 addition & 0 deletionsgitlab/v4/objects/__init__.py
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
26 changes: 26 additions & 0 deletionsgitlab/v4/objects/keys.py
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) |
42 changes: 42 additions & 0 deletionstests/functional/api/test_keys.py
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() |
54 changes: 54 additions & 0 deletionstests/unit/objects/test_keys.py
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.