Skip to content

Instantly share code, notes, and snippets.

@victor-pogor
Created October 3, 2020 17:31
Show Gist options
  • Select an option

  • Save victor-pogor/b406ff813ad2d638c0cda0b1fb9bb814 to your computer and use it in GitHub Desktop.

Select an option

Save victor-pogor/b406ff813ad2d638c0cda0b1fb9bb814 to your computer and use it in GitHub Desktop.
This shell script is used for adding SSH and GPG keys to newly created WSL (Ubuntu) distribution.
#!/usr/bin/env bash
# Sensitive informations
FULLNAME='Name Surname' # Enter your fullname.
EMAIL='name.surname@example.com' # Enter your email.
GITHUB_TOKEN='xxxxx' # Enter your Github token. It can be obtained here: https://github.com/settings/tokens. You need to define the "admin:gpg_key" and "admin:public_key" scopes.
GPG_PASSPHRASE="xxxxx" # Enter GPG passphrase.
# Configure Git
git config --global user.name "$FULLNAME"
git config --global user.email $EMAIL
# Calling the Github API
call_github_api() {
curl --location --request POST "$1" \
--header 'Accept: application/vnd.github.v3+json' \
--header "Authorization: token ${GITHUB_TOKEN}" \
--header 'Content-Type: application/json' \
--data-raw "$2"
}
# Adding a SSH key
add_ssh_key() {
SSH_KEY_LOCATION=$HOME/.ssh/id_github
PUB_KEY=${SSH_KEY_LOCATION}.pub
# Generating a new SSH key
ssh-keygen -t rsa -b 4096 -C $EMAIL -N '' -C 'WSL Key' -f $SSH_KEY_LOCATION
# Add current SSH key to Github
SSH_REQUEST_BODY='{ "title": "WSL", "key": "'$(<$PUB_KEY)'"}'
call_github_api 'https://api.github.com/user/keys' "$SSH_REQUEST_BODY"
}
# Adding a GPG key
add_gpg_key() {
# Preparing a batch file for the GPG key generation
echo "Key-Type: default
Key-Length: 4096
Subkey-Type: default
Subkey-Length: 4096
Name-Real: $FULLNAME
Name-Email: $EMAIL
Expire-Date: 0
Passphrase: $GPG_PASSPHRASE" > ~/genkey
# Generating a GPG key
gpg --full-generate-key --batch ~/genkey
rm ~/genkey
# Extracting GPG key ID
GPG_KEY_ID=$(gpg --list-secret-keys --keyid-format LONG $EMAIL | awk '/sec/{if (length($2) > 0) print $2}');
PARSED_GPG_KEY_ID="${GPG_KEY_ID##*/}"
# GPG public key files
PUB_KEY_FILE=$HOME/$PARSED_GPG_KEY_ID.pub
PUB_KEY_MIN_FILE=$HOME/$PARSED_GPG_KEY_ID.min.pub
# Exporting the GPG public key to file
gpg --output "$PUB_KEY_FILE" --armor --export $PARSED_GPG_KEY_ID
sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' $PUB_KEY_FILE > $PUB_KEY_MIN_FILE
# Adding a new GPG key to your GitHub account
GPG_SSH_REQUEST_BODY='{ "armored_public_key" : "'$(<$PUB_KEY_MIN_FILE)'"}'
call_github_api 'https://api.github.com/user/gpg_keys' "$GPG_SSH_REQUEST_BODY"
rm $PUB_KEY_FILE $PUB_KEY_MIN_FILE
# Telling Git about your GPG key
git config --global commit.gpgsign true
git config --global user.signingkey $PARSED_GPG_KEY_ID
# Adding GPG key to bash profile
test -r ~/.bash_profile && echo 'export GPG_TTY=$(tty)' >> ~/.bash_profile
echo 'export GPG_TTY=$(tty)' >> ~/.profile
}
# Set SSH agent to start on login
set_ssh_to_autostart() {
echo "SSH_ENV=\"\$HOME/.ssh/agent-environment\"
function add_keys {
for possiblekey in \${HOME}/.ssh/id_*; do
if grep -q PRIVATE \"\$possiblekey\"; then
/usr/bin/ssh-add \"\$possiblekey\"
fi
done
}
function start_agent {
echo \"Initialising new SSH agent...\"
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > \"\${SSH_ENV}\"
echo succeeded
chmod 600 \"\${SSH_ENV}\"
. \"\${SSH_ENV}\" > /dev/null
add_keys;
}
# Source SSH settings, if applicable
if [ -f \"\${SSH_ENV}\" ]; then
. \"\${SSH_ENV}\" > /dev/null
#ps \${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep \${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi"
} > $HOME/.bash_profile
# Add SSH key
add_ssh_key
# Add GPG key
add_gpg_key
# Autostart SSH agent
set_ssh_to_autostart
@victor-pogor
Copy link
Author

victor-pogor commented Oct 3, 2020

Additional settings

  1. Install socat in WSL distro.
  2. Add "terminal.integrated.shellArgs.linux": ["-l"] and "terminal.integrated.fontFamily": "Source Code Pro for Powerline" configurations to VS Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment