Prevent commit forgery with GPG signatures
While Git is cryptographically secure, it is not invulnerable. For instance, a leaked password or a forged commit could allow an attacker to push malicious code to your repository by impersonating a trusted user. To prevent this, use GPG to sign your commits and tags locally. Codeup verifies these GPG signatures to confirm that they originate from a trusted source.
How Codeup handles GPG signatures
Codeup uses its own keychain to verify GPG signatures and does not access any public key servers.
For Codeup to verify a commit:
-
The committer must have a GPG public/private key pair.
-
The committer's public key must be uploaded to their Codeup account.
-
The GPG key must contain an email address that matches a verified email address in the committer's Codeup account.
-
The committer email address in the commit must match the verified email address in the GPG key.
GPG keys
Install GPG tools
By default, GPG is not installed on macOS or Windows. To install the GPG command-line tools, see the GnuPG download page.
Check for an existing key pair
Adding a key generated with an unsupported algorithm will cause an error. Codeup currently supports the following GPG key algorithms:
-
RSA
-
ElGamal
-
DSA
-
Open Terminal (for macOS or Linux) or Git Bash (for Windows).
-
Run the following command to list your existing GPG key pairs.
gpg --list-secret-keys --keyid-format LONG -
Review the command output to see if you have a GPG key pair.
-
If you do not have a GPG key pair or you do not want to use an existing one to sign commits and tags, generate a new GPG key pair.
-
If you already have a GPG key pair and want to use it to sign commits and tags, add the GPG public key to your Codeup account.
-
Generate GPG key pair
If you do not have a GPG key pair, generate one to sign commits and tags. If your operating system has gpg2 installed, replace gpg with gpg2 in the following commands.
-
Open Terminal (for macOS or Linux) or Git Bash (for Windows). Run the appropriate command for your
gpgversion.Because there are multiple versions of GPG, you may need to consult the relevant man page to find the appropriate key generation command. Your key must use RSA.
2.1.17or laterIf you are using version
2.1.17or later, run the following command to generate a GPG key pair.gpg --full-generate-keyVersions earlier than
2.1.17If you are using a version earlier than
2.1.17, run the following command and then skip to Step 4.gpg --default-new-key-algo rsa4096 --gen-key -
At the prompt, select the desired key type, or press
Enterto accept the default (RSA and RSA).Please select what kind of key you want: (1) RSA and RSA (default) (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) Your selection? 1 -
Enter your desired key size. Your key must be at least
4096bits.RSA keys may be between 1024 and 4096 bits long. What keysize do you want? (2048) 4096 Requested keysize is 4096 bits -
Specify the key's expiration period. Press
Enterto accept the default (no expiration).Please specify how long the key should be valid. 0 = key does not expire <n> = key expires in n days <n>w = key expires in n weeks <n>m = key expires in n months <n>y = key expires in n years Key is valid for? (0) 0 Key does not expire at all -
Verify that your selections are correct.
Is this correct? (y/N) y -
Enter your name, an email address that is verified on your Codeup account, and an optional comment (press
Enterto skip).GnuPG needs to construct a user ID to identify your key. Real name: Codeup Email address: <your_email> Comment: You selected this USER-ID: "Codeup <your_email>" Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O -
When prompted, enter a strong passphrase to protect your GPG key and confirm it. This action generates your GPG key pair.
Export GPG public key
-
Run the following command to list the secret key required for signing commits or tags.
Note: Replace
<your_email>with your email address.gpg --list-secret-keys --keyid-format LONG <your_email> -
Copy the GPG key ID from the
secline in the output. In the following example, the key ID is30F2B65B9246B6CA.sec rsa4096/30F2B65B9246B6CA 2017-08-18 [SC] D5E4F29F3275DC0CDA8FFC8730F2B65B9246B6CA uid [ultimate] Codeup <your_email> ssb rsa4096/B7ABC0813E4028C0 2017-08-18 [E] -
Run the following command to export the public key for that ID. The GPG public key starts with
-----BEGIN PGP PUBLIC KEY BLOCK-----and ends with-----END PGP PUBLIC KEY BLOCK-----.Note: Replace the example ID
30F2B65B9246B6CAwith the key ID from the previous step.gpg --armor --export 30F2B65B9246B6CA
Add a GPG key to your account
After you add a key, you cannot edit it; you can only delete it. If you paste an invalid key, you must delete the problematic key and add it again.
-
Log on to the Alibaba Cloud DevOps Workbench, click your profile picture in the upper-right corner, and then click Personal Settings in the menu that appears.
-
On the personal settings page, click GPG Public Keys.
-
Paste your GPG public key into the Public key field and click Add. After you add the key, Codeup displays its fingerprint, associated email, and creation date.
Make sure the email address in the GPG key is verified. If it is not, add the email address to your profile, or generate a new GPG key with an existing, verified email address.
Associate your GPG key with Git
After associating the GPG key with your Codeup account, you must configure Git to use it.
-
Run the following command to list your GPG secret key.
Note: Replace
<your_email>with your email address.gpg --list-secret-keys --keyid-format LONG <your_email> -
Copy the GPG key ID from the sec line in the output. In the following example, the ID is 30F2B65B9246B6CA.
sec rsa4096/30F2B65B9246B6CA 2017-08-18 [SC] D5E4F29F3275DC0CDA8FFC8730F2B65B9246B6CA uid [ultimate] Codeup <your_email> ssb rsa4096/B7ABC0813E4028C0 2017-08-18 [E] -
Configure Git to use this key to sign your commits.
Note: Replace
30F2B65B9246B6CAwith your GPG key ID.git config --global user.signingkey 30F2B65B9246B6CAThe GPG key set with the
--globalflag becomes your default key. If you want to use a different key for a specific repository, you can set it individually within that repository's directory, like this:git config user.signingkey 30F2B65B9246B6CA -
(Optional) If Git is using
gpgand you encounter errors likesecret key not availableorgpg: signing failed: secret key not available, run the following command to switch togpg2.git config --global gpg.program gpg2
Signing
Sign commits
After you create a GPG key and add it to your account, you can start signing your commits.
-
To sign a commit, add the
-Sflag to your git commit command.git commit -S -m "My commit msg"Alternatively, you can specify the key ID, for example, 30F2B65B9246B6CA.
Note: Replace
30F2B65B9246B6CAwith your GPG key ID.git commit -S30F2B65B9246B6CA -m "My commit msg" -
When prompted, enter the passphrase for your GPG key (if any).
-
Push to Codeup and check for the "Verified" status on your commit. If you do not want to type the -S flag for every commit, you can configure Git to sign your commits automatically.
git config --global commit.gpgsign true
Sign tags
In addition to signing commits, you can also sign tags.
-
Add the
-sflag to sign a tag.git tag -s mytagAlternatively, you can specify the key ID, for example, 30F2B65B9246B6CA.
Note: Replace
30F2B65B9246B6CAwith your GPG key ID.git tag -s -u 30F2B65B9246B6CA mytag -
Run the following command to verify a signed tag.
git tag -v mytag
If you do not want to type the -s flag every time you create a tag, you can configure Git to sign tags automatically.
git config --global tag.gpgSign true
Signature verification
Signature verification has two statuses:
-
Verified
-
Unverified
A signature is marked "Unverified" for any of the following reasons:
-
The GPG public key signature failed verification.
-
The GPG public key signature passed verification, but the committer email, which belongs to the current Alibaba Cloud DevOps user, does not match the email in the GPG key.
-
The GPG public key signature passed verification, but the committer email does not match the email in the GPG key.
-
The GPG public key signature passed verification, but the email in the GPG key is not a verified email for the current Alibaba Cloud DevOps user.
-
The GPG public key is not registered with Codeup.
Remove GPG key
Revoke GPG key
Revoking a key changes the status of all past and future commits signed with it to "Unverified". You should use this action if your key has been compromised.
-
Log on to the Alibaba Cloud DevOps Workbench, click your profile picture in the upper-right corner, and then click Personal Settings in the menu that appears.
-
On the personal settings page, click GPG Public Keys.
-
Hover over the public key you want to revoke. Click Revoke, and then click OK in the confirmation dialog box.
After you revoke a key, it can no longer be used for commit signature verification, and previously verified commits will become unverified.
Delete GPG key
Deleting a key does not affect the "Verified" status of previously signed commits and tags. However, any new commits signed with that key will be marked as "Unverified" when pushed to Codeup. To change the status of existing commits to "Unverified", you must revoke the key instead.
-
Log on to the Alibaba Cloud DevOps Workbench, click your profile picture in the upper-right corner, and then click Personal Settings in the menu that appears.
-
On the personal settings page, click GPG Public Keys.
-
Hover over the public key you want to delete. Click Delete, and then click OK in the confirmation dialog box.
After you delete a key, it can no longer be used for commit signature verification. Previously verified commits are not affected.
Reject unsigned commits or tags (Coming soon)
You can configure a push rule in your repository to reject commits that are not signed with GPG.
Related information
-
Git Tools - Signing Your Work: https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work
-
GnuPG: https://www.gnupg.org/