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

GitHub authentication #80

Open
phajy opened this issue Dec 5, 2024 · 4 comments
Open

GitHub authentication #80

phajy opened this issue Dec 5, 2024 · 4 comments
Labels
help I need help pending-writeup This issue should be written up as a cookbook entry resolved This issue has been successfuly resolved.

Comments

@phajy
Copy link

phajy commented Dec 5, 2024

Can anyone recommend good practice for securely authenticating with GitHub on the group's Linux system? I've looked at this article but the tools I use on my Mac are not installed (or I can't find) on the group's computers. Should I use a "personal access token" and if so, what's the safest way to do this? Thanks!

@phajy phajy added the help I need help label Dec 5, 2024
@fjebaker
Copy link
Member

fjebaker commented Dec 5, 2024

When you say authenticating with GitHub, do you mean authenticating over git (as in, so that push / pull works), or for the gh GitHub CLI?

@phajy
Copy link
Author

phajy commented Dec 5, 2024

Authenticating over git so that, e.g., git push origin main works. Not using gh because I don't think that is installed on the group's computers (but if it is [or is easy to install] we could use gh). Basically want @DariusMichienzi to be able to easily push changes to a GitHub repository from the group's computers.

@fjebaker
Copy link
Member

fjebaker commented Dec 6, 2024

Probably the most straight forward and secure way is to use an SSH keypair then. There's no limit to how many public keys you can upload to github, so it's probably best to generate a new keypair on typhon, and configure git to use SSH.

That basically means, run ssh-keygen -t ed25519 and follow the interactive prompt. This will create a new key in your ~/.ssh/ directory called id_ed25519 unless you chose something else during creation. Copy the one that ends in .pub and on github, under Settings -> SSH and GPG keys add a new SSH key and paste the .pub key in there.

To configure git to use the new ID, add somethign like this to your ~/.ssh/config file:

Host github.com
    IdentityFile ~/.ssh/id_ed25519 # change this to match your path (NB: without .pub this time)
    IdentitiesOnly yes

Then, on all of your cloned repos, use get remote -v to get the HTTPS url and change it to an SSH one. So in general, if you have a URL like

https://github.com/USER/REPO-NAME

it would become

[email protected]:USER/REPO-NAME

using the command

git remote set-url origin "[email protected]:USER/REPO-NAME"

Now you can push and pull from that repo securely.

As an aside, I have here a script for automatically converting HTTPS urls into SSH ones. Works on ZSH, should work on Bash, just add to your .zshrc or .bash_profile respectively. To use, navigate to the git clone and just do sshremote, where the first argument may optionally be which remote you want to update:

sshremote() {
    local remote="${1:-origin}"
    local url="$(git remote get-url $remote)"
    local alphanumeric="[a-zA-Z0-9\.]"

    if [[ "$url" =~ ^$alphanumeric+://$alphanumeric+/(.*)  ]]; then
        local ext="${match[1]}"
        local new="[email protected]:$ext"
        git remote set-url "$remote" "$new"
        echo "Updating $remote: '$url' -> '$new'"
    else
        echo "Malformed url: '$url'"
    fi
}

@phajy
Copy link
Author

phajy commented Dec 12, 2024

This works - thanks!

@fjebaker fjebaker added resolved This issue has been successfuly resolved. pending-writeup This issue should be written up as a cookbook entry labels Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help I need help pending-writeup This issue should be written up as a cookbook entry resolved This issue has been successfuly resolved.
Projects
None yet
Development

No branches or pull requests

2 participants