Guest User

Untitled

a guest
Dec 6th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.41 KB | None | 0 0
  1. [TOC levels=1-3]: # "#### Table of Contents"
  2. #### Table of Contents
  3. - [GPG Intro](#gpg-intro)
  4. - [GPG Usage](#gpg-usage)
  5. - [Create signing key for git](#create-signing-key-for-git)
  6. - [Example](#example)
  7. - [Export and backup of keys](#export-and-backup-of-keys)
  8. - [Git configuration](#git-configuration)
  9. - [GitHub user settings](#github-user-settings)
  10. - [Configure git locally](#configure-git-locally)
  11. - [Sign commits](#sign-commits)
  12. - [Branch protection](#branch-protection)
  13. - [Demo output](#demo-output)
  14. - [Locally validate that a commit is signed](#locally-validate-that-a-commit-is-signed)
  15.  
  16. Quick primer for devs trying to sign commits in github
  17.  
  18. # GPG Intro
  19.  
  20. Gpg stands for GNU Privacy Guard, which is an open source implementation
  21. for OpenPGP encryption.
  22.  
  23. While ssh keypairs are generally used for authenticating to remote
  24. systems, gpg is generally used to sign content. Git enables you to sign
  25. commits and tags with your private gpg key and GitHub allows you to
  26. upload your public gpg key in user settings.
  27.  
  28. When a signed commit is pushed into GitHub, it will be marked by a
  29. 'Verified' tag. You can then apply rules to repo branches to require all
  30. commits to be signed. Unsigned commits on a protected branch will be
  31. rejected during git push.
  32.  
  33. # GPG Usage
  34.  
  35. These commands assume a linux environment - either native, vm or wsl.
  36.  
  37. Git bash will not work here.
  38.  
  39. ## Create signing key for git
  40. ```bash
  41. #create ~/.gnupg by invoking gpg
  42. gpg -k
  43.  
  44. #create master key, which is only for creating subkeys
  45. gpg --full-generate-key #select RSA (sign only) option
  46.  
  47. #list your new key (along with any other public keys loaded)
  48. gpg -k
  49.  
  50. #list your private keys
  51. gpg -K
  52. ```
  53.  
  54. ### Example
  55. ```bash
  56. test@feigas:~$ gpg -K
  57. gpg: /home/test/.gnupg/trustdb.gpg: trustdb created
  58.  
  59. test@feigas:~$ gpg --full-gen
  60. gpg (GnuPG) 2.1.18; Copyright (C) 2017 Free Software Foundation, Inc.
  61. This is free software: you are free to change and redistribute it.
  62. There is NO WARRANTY, to the extent permitted by law.
  63.  
  64. Please select what kind of key you want:
  65. (1) RSA and RSA (default)
  66. (2) DSA and Elgamal
  67. (3) DSA (sign only)
  68. (4) RSA (sign only)
  69. Your selection? 4
  70. RSA keys may be between 1024 and 4096 bits long.
  71. What keysize do you want? (3072)
  72. Requested keysize is 3072 bits
  73. Please specify how long the key should be valid.
  74. 0 = key does not expire
  75. <n> = key expires in n days
  76. <n>w = key expires in n weeks
  77. <n>m = key expires in n months
  78. <n>y = key expires in n years
  79. Key is valid for? (0)
  80. Key does not expire at all
  81. Is this correct? (y/N) y
  82.  
  83. GnuPG needs to construct a user ID to identify your key.
  84.  
  85. Real name: David Gallmeier
  86. Email address: dgallmeier@gmail.com
  87. Comment: gpg key for signing
  88. You selected this USER-ID:
  89. "David Gallmeier (gpg key for signing) <dgallmeier@gmail.com>"
  90.  
  91. Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
  92. We need to generate a lot of random bytes. It is a good idea to perform
  93. some other action (type on the keyboard, move the mouse, utilize the
  94. disks) during the prime generation; this gives the random number
  95. generator a better chance to gain enough entropy.
  96. ```
  97.  
  98. ## Export and backup of keys
  99.  
  100. You need to export your public key to load it into your github settings
  101. and it's a good idea to export the private keys for backup.
  102.  
  103. ```bash
  104. #Export public key (use the email address
  105. gpg --armor --output EMAILADDRESS.public.asc --export EMAILADDRESS
  106.  
  107. #Export private keys (master key + subkeys)
  108. gpg --armor --output EMAILADDRESS.private.asc --export-secret-keys EMAILADDRESS
  109.  
  110. #Export private keys (subkeys only)
  111. gpg --armor --output EMAILADDRESS.subkeys.asc --export-secret-subkeys EMAILADDRESS
  112. ```
  113.  
  114. You will upload the contents of EMAILADDRESS.public.asc in GitHub.
  115.  
  116. # Git configuration
  117.  
  118. ## GitHub user settings
  119.  
  120. Exported keys will be in a base64 format, similar to how ssh keys look.
  121.  
  122. Sample public key:
  123. ```
  124. -----BEGIN PGP PUBLIC KEY BLOCK-----
  125.  
  126. mQENBFwGwSMBCADqS2vmDuG9TWpOMol7mq1d3opRfZvrpYld6iVrWdj/1I4U9uLo
  127. NtFix4N6iN7XSG4vmQ57Lil9Y+QzJTcoSuG0FCRX5VKNkUSLG9GnafLx10BylfE3
  128. yKppAZnQgg2WaW+XW1ZbfCzdVxsDyA0iWRuzmUZJ7XbWMhB10yRtEzREJnakZcjd
  129. vy4lvajDO6Nbdqb3KkRCbPUPX9fW6blM7FeJ0CpSIsanjYGHnbFKquT5DZM06vN8
  130. ZEzx/OQ0xvPGrcTsUSbyOfkPl8zhpkv5fcEXmh19TxvxWHHBLClOF9oKDa3grlOz
  131. XmQ9HlmxgcJgqfrokxR2zGFa9k6dl8IqPj95ABEBAAG0VkRhdmlkIEdhbGxtZWll
  132. ciAoamVua2lucyBkZXBsb3kga2V5IGZvciBibGFja2JveCkgPGplbmtpbnNAamVu
  133. a2lucy5mZWlnYXMueG91bmdlcy5uZXQ+iQFOBBMBCAA4FiEEgV8yCg4wj197o7g1
  134. vIOv0/ZlMRoFAlwGwSMCGwMFCwkIBwIGFQgJCgsCBBYCAwECHgECF4AACgkQvIOv
  135. 0/ZlMRqGUAgAkPK7hO5sOFW1xNONFog3vrL+HMJ52MUsQ1fd5qD5HPZuoGbMKiWf
  136. ClaHGC71euoPtYqr4OEyoeMbOlwPTiNiK+VBb99xP/L5mbExeLGkcpZvSXVUn5D/
  137. nml0sUh2dnNZ1CPdJyyD2iTLWtm+0nMIl0t6FZZoZYGUH6UMHdlgzCaQSTK2Gnu2
  138. do51B8vq+6wnYEF6BFyXjICW2gSX7rFanqhk8bPyYIJT3Qec3S/kcNIsqJUcBux8
  139. XARLICby/0VuhCTk/qiEZ9QugRRV76qgw5B1PMdjK40DVq8pFig5+xUY5PpSM9SD
  140. UnkksTQLNKK6gOaTAPPXJcDA5hbudbpH17kBDQRcBsEjAQgA0LablculL+99NhTa
  141. 0Ittp76TCDsgLeYVDmqitI3I9C/Flxr84OinHmw+WOU5WcIS7zJZjO0gJUThNoW8
  142. W9rAWYiSAd7Nix6sguDmUDF6Iu27cMSO0Bh7HNzFV4YCgAHVEvBcS/pmTCbmeub+
  143. GpXtRBTEiT3BXp86453d4Abh1MDh9z1LIeEYy1sbTMDwU0ZfVZWnwcokmEY3qrAy
  144. krJI1XsqeikzQTo53lH6YHYxrJFPozuYOIv/SAEhNQhKnqLFE792N1HYmLct1XTb
  145. mYbbYAdrSRRDnqX1hqGsKM9Y7fNQIPhhFcwHlnpH3f8cRn/4HE0RKyDYOyoQtyAN
  146. Q5aNdwARAQABiQE2BBgBCAAgFiEEgV8yCg4wj197o7g1vIOv0/ZlMRoFAlwGwSMC
  147. GwwACgkQvIOv0/ZlMRrttwf8C4Nhh9RmV0wmo38yqsl2PDL8YP7IDoDKhRKhhDyw
  148. krfByG/PbRLbAMQDe8ZARsvF1nX8JJG03TLTr59HiLjiIYSg5xc4EpWjsW4zYKDL
  149. M9p6FMUzMrXpzVTlHgEgfKnXrTaBVZ7dJohm38w2hoyZd9etQNMzNCDq1KqNSNuf
  150. 1tKQVYN4K2VQH69YOvwQiNNYrzyKhydPkgqvemX8Zhdiarwd5/VwS68A+taruLQZ
  151. jnVbctNPrSM6gluMXl4WU8QcObeV3VKmvG620PNgdKqzsUviKpI1RtCzeK/rb6Jr
  152. AhO8Gz4XFSwm3ZxIg5n+7k794Or/NQiELcevviV+0yVyaw==
  153. =+s8M
  154. -----END PGP PUBLIC KEY BLOCK-----
  155. ```
  156.  
  157. Add this public key to your GitHub account, under User -> Settings ->
  158. SSH and GPG keys.
  159.  
  160. ### Configure git locally
  161.  
  162. Add signing key to git's config:
  163. ```bash
  164. davidg@DESKTOP-53SN5LI:~/gits/studycast$ gpg -k
  165. /home/davidg/.gnupg/pubring.kbx
  166. -------------------------------
  167. pub rsa2048 2017-10-31 [SC]
  168. EA40D5B54300BAD17CFB73CEE87C032427B77C9F
  169. uid [ unknown] David Gallmeier <dgallmeier@gmail.com>
  170. sub rsa2048 2017-10-31 [E]
  171. sub rsa2048 2017-10-31 [S]
  172. sub rsa2048 2017-11-24 [A]
  173. sub rsa2048 2017-10-31 [A]
  174.  
  175. davidg@DESKTOP-53SN5LI:~/gits/studycast$ git config --global user.signingkey EA40D5B54300BAD17CFB73CEE87C032427B77C9F
  176. davidg@DESKTOP-53SN5LI:~/gits/studycast$
  177. ```
  178.  
  179. ### Sign commits
  180.  
  181. After you've updated GitHub and git locally, add the -S flag to `git commit`:
  182. ```bash
  183. davidg@DESKTOP-53SN5LI:~/gits/studycast$ git commit -m "Signed commit" -S
  184. [master 8d1121a] Signed commit
  185. 1 file changed, 1 insertion(+)
  186. ```
  187.  
  188. __NOTE__ that if your gitconfig email address matches the address on
  189. your gpg key, git may automatically sign commits for you.
  190.  
  191. ## Branch protection
  192.  
  193. 1. In the repository's Settings -> Branches, click Add Rule.
  194. 2. List the branch(es) you want to protect.
  195. * for multiple branches, you can use wildcards, or add multiple rules
  196. 3. Check 'Require signed commits'
  197. 4. Click Create
  198.  
  199. ### Demo output
  200.  
  201. When things are setup properly, unsigned commits will be rejected when
  202. you attempt to push them into github. You will see an error message like
  203. this:
  204. ```bash
  205. test@DESKTOP-53SN5LI:~/studycast$ git push origin master
  206. Counting objects: 6, done.
  207. Delta compression using up to 4 threads.
  208. Compressing objects: 100% (5/5), done.
  209. Writing objects: 100% (6/6), 575 bytes | 0 bytes/s, done.
  210. Total 6 (delta 3), reused 0 (delta 0)
  211. remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
  212. remote: error: GH006: Protected branch update failed for refs/heads/master.
  213. remote: error: Commits must have valid signatures.
  214. To github.com:vector623/studycast.git
  215. ! [remote rejected] master -> master (protected branch hook declined)
  216. error: failed to push some refs to 'git@github.com:vector623/studycast.git'
  217. ```
  218.  
  219. Again, remember that commits may be signed automagically by git, if your
  220. gitconfig email matches your gpg email.
  221.  
  222. ### Locally validate that a commit is signed
  223.  
  224. To check whether or not a commit has been signed, use `git
  225. verify-commit`.
  226.  
  227. If you have the public key for the gpg signature loaded locally, you'll
  228. see that the signature is good.
  229.  
  230. ```bash
  231. davidg@DESKTOP-53SN5LI:/mnt/c/Users/David G/gits/studycast$ git verify-commit 932e890ec92c1da010c1ab50c108fd871df00ccf
  232. gpg: Signature made Thu 06 Dec 2018 11:12:23 AM STD
  233. gpg: using RSA key 4E6AEDB3630B2E7863415C0A39C8649055B0BCC2
  234. gpg: Good signature from "David Gallmeier <dgallmeier@gmail.com>" [unknown]
  235. gpg: WARNING: This key is not certified with a trusted signature!
  236. gpg: There is no indication that the signature belongs to the owner.
  237. Primary key fingerprint: EA40 D5B5 4300 BAD1 7CFB 73CE E87C 0324 27B7 7C9F
  238. Subkey fingerprint: 4E6A EDB3 630B 2E78 6341 5C0A 39C8 6490 55B0 BCC2
  239. ```
  240.  
  241. Don't worry about the warning for now - that is resolved through
  242. assigning trust, which is not necessary for us since we'll be using
  243. github to validate that the signature belongs to the owner:
  244.  
  245. If you do not have the public key loaded, you'll see that the commit has
  246. been signed and the key used, but you won't be able to check the
  247. signature.
  248. ```bash
  249. test@DESKTOP-53SN5LI:~/studycast$ git verify-commit 932e890ec92c1da010c1ab50c108fd871df00ccf
  250. gpg: Signature made Thu Dec 6 11:12:23 2018 STD
  251. gpg: using RSA key 4E6AEDB3630B2E7863415C0A39C8649055B0BCC2
  252. gpg: Can't check signature: No public key
  253. ```
  254.  
  255. If there is no signature, you'll get blank output:
  256. ```bash
  257. test@DESKTOP-53SN5LI:~/studycast$ git verify-commit 8a7c03e959b48acc07d2cb01cef3834733c59a8f
  258. test@DESKTOP-53SN5LI:~/studycast$
  259. ```
Add Comment
Please, Sign In to add comment