'How does one retrieve an arbritrary user's ssh public keys from GitLab?

How does one retrieve an arbritrary user's ssh public keys from GitLab?

GitHub provides this feature. For example: https://github.com/winny-.keys

The GitLab API exposes public keys, however it looks like it requires:

  1. Separate authentication
  2. Query a given user name for its UID
  3. Finally get the public keys


Solution 1:[1]

GitHub style ssh public key access was added in GitLab 6.6.0 using the following scheme: http://__HOST__/__USERNAME__.keys (thanks @bastelflp).

Currently we are running 6.2.3, and we will upgrade.

Solution 2:[2]

You also have, with GitLab 14.9 (March 2022):

New API endpoints for keys and tokens

GitLab 14.9 delivers new REST API endpoints:

  • Return a single SSH key for a specified user. This is useful to enable GitLab SSH keys to be a Terraform-managed resource.
  • Return a single project’s deploy token by ID. This allows for a simple request to return a deploy token instead of returning and sorting through pages deploy tokens with the API.
  • Return a single group access token or project access token.

Thank you Timo Furrer for your contribution!

See Documentation, Issue 354889, Issue 355778 and Issue 355893.

Example:

GET /users/:id/keys/:key_id
{
  "id": 1,
  "title": "Public key",
  "key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=",
  "created_at": "2014-08-01T14:47:39.080Z"
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 VonC