UserSSHKey
c.UserSSHKey calls the API's UserSSHKey group. The authorized SSH keys of a
user.
Methods
| Method | Returns | Permission | Description |
|---|---|---|---|
List(ctx, hostname, name) | *Response[Collection[UserSshkeyKey]] | user:read | List the authorized SSH keys of a user. |
Create(ctx, hostname, input) | *Response[Collection[UserSshkeyResult]] | user:write | Add an authorized SSH key to a user. |
Delete(ctx, hostname, input) | *Response[Collection[UserSshkeyResult]] | user:write | Remove an authorized SSH key of a user. |
List
func (s *UserSSHKeyService) List(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[UserSshkeyKey]], error)
List the authorized SSH keys of a user.
Reads ~/.ssh/authorized_keys of the user through gohai, with the type, the SHA256 fingerprint, the comment and the options of each key. A user with no file has no keys. A user that the host does not have is not found.
It sends GET /api/node/{hostname}/user/{name}/sshkey, and needs the permission
user:read.
The examples of the method's definition:
A user with keys, go run user_ssh_key.go list:
resp, err := c.UserSSHKey.List(ctx, target, "osapitest")
Create
func (s *UserSSHKeyService) Create(
ctx context.Context,
hostname string,
input UserSSHKeyCreateInput,
) (*Response[Collection[UserSshkeyResult]], error)
Add an authorized SSH key to a user.
Adds the key line to the end of ~/.ssh/authorized_keys of the user, and makes the directory and the file if they are missing, with modes 700 and 600, owned by the user. A key that the file has already, by its fingerprint, does not change it. A line with no public key is refused. A user that the host does not have is not found. It runs on the Debian family only, and elsewhere reports skipped.
It sends POST /api/node/{hostname}/user/{name}/sshkey, and needs the
permission user:write.
The examples of the method's definition:
Add a key, go run user_ssh_key.go create:
resp, err := c.UserSSHKey.Create(ctx, target, client.UserSSHKeyCreateInput{
Key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl85dcLw7lIdX+IyXEXT00CTCezv/2E1jvolleleo6Z osapitest@example",
Name: "osapitest",
})
Add a second key, go run user_ssh_key.go create-2:
resp, err := c.UserSSHKey.Create(ctx, target, client.UserSSHKeyCreateInput{
Key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGLPeEZQDbhOrSIx6gISjxXIcREV3rwL517pX6m9G5jc other@example",
Name: "osapitest",
})
Delete
func (s *UserSSHKeyService) Delete(
ctx context.Context,
hostname string,
input UserSSHKeyDeleteInput,
) (*Response[Collection[UserSshkeyResult]], error)
Remove an authorized SSH key of a user.
Removes each line of the key with the fingerprint from ~/.ssh/authorized_keys of the user, and keeps the other lines as they are. A key that the file does not have, a user with no file, or a user that the host does not have, changes nothing. It runs on the Debian family only, and elsewhere reports skipped.
It sends DELETE /api/node/{hostname}/user/{name}/sshkey, and needs the
permission user:write.
The examples of the method's definition:
Remove a key, go run user_ssh_key.go delete:
resp, err := c.UserSSHKey.Delete(ctx, target, client.UserSSHKeyDeleteInput{
Fingerprint: "SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk",
Name: "osapitest",
})
A key the user does not have, go run user_ssh_key.go delete-2:
resp, err := c.UserSSHKey.Delete(ctx, target, client.UserSSHKeyDeleteInput{
Fingerprint: "SHA256:a5CeY6Lh3ZXkJOqGyuwb24kQeVjgag1gc9ujzv++nhE",
Name: "osapitest",
})
A user that the host does not have, go run user_ssh_key.go delete-3:
resp, err := c.UserSSHKey.Delete(ctx, target, client.UserSSHKeyDeleteInput{
Fingerprint: "SHA256:mnpIO1El26FsWnOeLzppADcNXgXRNHh9MPNPLPvSCwk",
Name: "nobody",
})
Types
The fields of each type are the properties of its schema in the API's spec.
RowStatus
What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.
A string, one of ok, failed, skipped, timeout.
UserSSHKeyCreateInput
| Field | Type | Description |
|---|---|---|
Name | string | The name of the path. The user, such as alice. |
Key | string | The public key line, such as ssh-ed25519 AAAA... alice@laptop, with options before it if any. |
UserSSHKeyDeleteInput
| Field | Type | Description |
|---|---|---|
Name | string | The name of the path. The user, such as alice. |
Fingerprint | string | The SHA256 fingerprint of the key, as list shows it. |
UserSshkeyKey
| Field | Type | Description |
|---|---|---|
Comment | string | The comment after the key, such as alice@laptop. |
Error | string | Error message if the agent failed. |
Fingerprint | string | The SHA256 fingerprint of the key. |
Hostname | string | Hostname of the agent that produced this result. |
Options | string | The options before the key, such as from="10.0.0.0/8". |
Status | RowStatus | What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran. |
Type | string | The key type, such as ssh-ed25519. |
UserSshkeyResult
| Field | Type | Description |
|---|---|---|
Changed | bool | Whether the host changed. |
Error | string | Error message if the agent failed. |
Fingerprint | string | The SHA256 fingerprint of the key. |
Hostname | string | Hostname of the agent that produced this result. |
Status | RowStatus | What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran. |
Example
examples/sdk/client/user_ssh_key.go
runs each example of this page. With no argument, it lists them.