Skip to content
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

Add "remove" feature to ssh-key #6136

Closed
zwhitchcox opened this issue Aug 24, 2022 · 9 comments · Fixed by #6273
Closed

Add "remove" feature to ssh-key #6136

zwhitchcox opened this issue Aug 24, 2022 · 9 comments · Fixed by #6273
Labels
enhancement a request to improve CLI help wanted Contributions welcome

Comments

@zwhitchcox
Copy link

Describe the feature or problem you’d like to solve

I'd like to be able to remove ssh key from account from the command line. It's hard to believe there are no requests for this, so I must just be missing them, but is there no way to remove a key from the command line.

You can add with:

gh ssh-key add [file]

But there is no way to remove.

In the original request (#2990) there was a proposal to remove by hash, but you could also remove by title.

@zwhitchcox zwhitchcox added the enhancement a request to improve CLI label Aug 24, 2022
@cliAutomation cliAutomation added the needs-triage needs to be reviewed label Aug 24, 2022
@zwhitchcox
Copy link
Author

zwhitchcox commented Aug 24, 2022

Wrote some scripts in case other people need this functionality

# get list of keys
gh_list_keys() {
  curl \
    -H "Accept: application/vnd.github+json" \
    -H "Authorization: token $GH_TOKEN" \
    https://api.github.com/user/keys
}

# get key ids by title
gh_key_by_title() {
  list_keys | jq -r '.[] | "\(.id) \(.title)"'
  # ^ combine with grep to filter the title you need
}

# delete ssh key
gh_key_del() {
  local KEY_ID=$1
  curl \
    -X DELETE \
    -H "Accept: application/vnd.github+json" \
    -H "Authorization: token $GH_TOKEN" \
    https://api.github.com/user/keys/$KEY_ID
}

@vilmibm
Copy link
Contributor

vilmibm commented Aug 24, 2022

I'm in favor of this assuming there isn't some big reason we didn't do it to begin with.

@vilmibm vilmibm added help wanted Contributions welcome and removed needs-triage needs to be reviewed labels Aug 24, 2022
@nsmag
Copy link
Contributor

nsmag commented Sep 11, 2022

I would like to pick this up but need to check my idea first. GitHub API provides a way to get/delete by key id. And key names are not unique. I can create two keys with the same name here:

$ gh ssh-key list
Test Key  ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEuZpG/RQ20C4NxoyhuS4D6+LmSB+ma9eVbeauBaf68l
Test Key  ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA1dxiSc/vhYD7QRAxTL67BguPioiDRYPtptoSy9NCHd

My idea is

  1. Add key id to the output of gh ssh-key list
$ gh ssh-key list
1 Test Key  ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEuZpG/RQ20C4NxoyhuS4D6+LmSB+ma9eVbeauBaf68l
2 Test Key  ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA1dxiSc/vhYD7QRAxTL67BguPioiDRYPtptoSy9NCHd
  1. Add gh ssh-key delete [KEY_ID]

If this makes sense, I can open the PR for this.

@samcoe
Copy link
Contributor

samcoe commented Sep 13, 2022

@nsmag That seems like a reasonable approach to me. The biggest concern is where we will add the id to the output. Generally we consider changing the output format a breaking change as some peoples scripts may be relying on the order of the columns. Due to this I would be in favor of adding the id column to the end of the output instead of the beginning.

cc/ @vilmibm For any input here.

@nsmag
Copy link
Contributor

nsmag commented Sep 13, 2022

@samcoe I also concern about changing ssh-key list output. Adding id column at the beginning or the end without the header seems unintuitive to me. If we can add the header, id column can be anywhere in the output. Reference run list command:

$ gh run list
STATUS  NAME               WORKFLOW        BRANCH          EVENT           ID          ELAPSED  AGE
✓       Add StateReaso...  PR Automation   issue-state...  pull_reques...  3042998197  16s      24m
✓       Add StateReaso...  Code Scanning   issue-state...  pull_request    3042968543  4m59s    30m
✓       Add StateReaso...  Tests           issue-state...  pull_request    3042968542  7m47s    30m

But I think adding the header is a breaking change as well.

@samcoe
Copy link
Contributor

samcoe commented Sep 14, 2022

@nsmag I would be open to also adding in a header. I don't view the header itself as a breaking change as we would not output it in non-tty mode, so from a scripts point of view nothing has changed.

@nsmag
Copy link
Contributor

nsmag commented Sep 14, 2022

@samcoe If adding header or appending columns to the output is okay. Should we change the output of ssh-key list to be more detailed and consistent with its corresponding REST API response:

$ gh ssh-key list
TITLE     KEY                                                                               ID
Test Key  ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEuZpG/RQ20C4NxoyhuS4D6+LmSB+ma9eVbeauBaf68l  70929690
Test Key  ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA1dxiSc/vhYD7QRAxTL67BguPioiDRYPtptoSy9NCHd  70929824
$ gh api \
  -H "Accept: application/vnd.github+json" \
  /user/keys
[
  {
    "id": 70929690,
    "key": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEuZpG/RQ20C4NxoyhuS4D6+LmSB+ma9eVbeauBaf68l",
    "url": "https://api.github.com/user/keys/70929690",
    "title": "Test Key",
    "verified": true,
    "created_at": "2022-09-11T03:09:50Z",
    "read_only": false
  },
  {
    "id": 70929824,
    "key": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIA1dxiSc/vhYD7QRAxTL67BguPioiDRYPtptoSy9NCHd",
    "url": "https://api.github.com/user/keys/70929824",
    "title": "Test Key",
    "verified": true,
    "created_at": "2022-09-11T03:20:27Z",
    "read_only": false
  }
]

To delete a key, use id as an argument:

$ gh ssh-key delete 70929690

If this approach is good to go, I plan to open two PRs:

  1. Add header and id column to the gh ssh-key list output
  2. Add gh ssh-key delete command

@samcoe
Copy link
Contributor

samcoe commented Sep 14, 2022

@nsmag That sounds good to me 👍

@kbcz1989
Copy link

@samcoe
Seems like "signing keys" cannot be deleted:

image
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement a request to improve CLI help wanted Contributions welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants