DEV Community

Cover image for วิธี sign commit ด้วย GPG บน GitHub
Anuchit Prasertsang
Anuchit Prasertsang

Posted on • Updated on

วิธี sign commit ด้วย GPG บน GitHub

ทำไมต้อง sign commit ของเรา

การ sign GPG กับ Git commit เป็นการรับรองว่าโค้ดถูกเปลี่ยนแปลงจากเราจริงๆ (ไม่ใช่คนอื่นแอบอ้างเอาอีเมล์หรือชื่อของเราไป commit โค้ดแทนเรา)ทำให้โค้ดมีความน่าเชื่อถือเพิ่มมากขึ้นและป้องกันการแก้ไขโดยไม่ได้รับอนุญาต พร้อมสามารถตรวจสอบย้อนกลับได้ ทำให้โปรเจคเรามีความน่าเชื่อถือสูงขึ้น อีกทั้งยังเป็นการเพิ่มความปลอดภัยและสร้างความมั่นใจให้คนใช้งานโปรเจคและทีมของเรา

สิ่งที่ต้องเตรียม

วิธี sign commit ด้วย GPG บน GitHub

วิธี sign commit ด้วย GPG บน GitHub

  1. สร้าง GPG key ใหม่
    • gpg --full-generate-key
  2. นำ GPG public key ไปเพิ่มใน GitHub
    • gpg --armor --export EFABXXXXXXXX174
  3. บอก Git ให้ใช้ GPG key ของเรา sign commit
    • git config user.signingkey EFABXXXXXXXX174
  4. ทำการ sign ตอน git commit
    • สั่งsignตอนcommit โดยส่ง -S ตอนcommit git commit -S -m "message"
    • หรือ
    • ตั้งค่าsignอัตโนมัติ git config commit.gpgsign true เพียงครั้งเดียว หลังจากนั้น git commit -m "message" ตามปกติ
  5. push ขึ้น GitHub ตามปกติ และเช็คว่า commit ของเรา sign แล้วหรือยัง บน GitHub
    • git push origin main commit verified

1. สร้าง GPG key ใหม่

gpg --full-generate-key
Enter fullscreen mode Exit fullscreen mode

จากนั้นเลือก

  • ประเภทของ key เป็น ECC (sign and encrypt) *default* (Elliptic Curve Cryptography)
  • เลือก elliptic curve เป็น Curve 25519 *default*
  • กำหนดระยะเวลาในการใช้งาน key หรือเลือก 0 ให้ key ไม่มีวันหมดอายุ
  • กรอกข้อมูลในการสร้าง key เช่น Real name:, Email address:, Comment:
  • ทำการยืนยันการข้อมูลโดยกด O (O)kay
  • กำหนด passphrase ให้กับ key ของเรา (เพื่อป้องกันการเข้าถึง key ของเรา เป็นรหัสผ่าน) แล้วเลือก <OK>

ตัวอย่างผลลัพธ์จากขึ้นตอนด้านบน

gpg (GnuPG) 2.4.5; Copyright (C) 2024 g10 Code GmbH
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Please select what kind of key you want:
   (1) RSA and RSA
   (2) DSA and Elgamal
   (3) DSA (sign only)
   (4) RSA (sign only)
   (9) ECC (sign and encrypt) *default*
  (10) ECC (sign only)
  (14) Existing key from card
Your selection?
Please select which elliptic curve you want:
   (1) Curve 25519 *default*
   (4) NIST P-384
   (6) Brainpool P-256
Your selection?
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)
Key does not expire at all
Is this correct? (y/N) y

GnuPG needs to construct a user ID to identify your key.

Real name: Your Name
Email address: your@email.com
Comment: your comment explanation your key
You selected this USER-ID:
    "Your Name (your comment explanation your key) <your@email.com>"

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O

                  ┌─────────────────────────────────────────────────────────────┐
                  │ Please enter the passphrase to                              │
                  │ protect your new key                                        │
                  │                                                             │
                  │ Passphrases match.                                          │
                  │                                                             │
                  │ Passphrase: _______________________________________________ │
                  │                                                             │
                  │ Repeat: ___________________________________________________ │
                  │ ┌─────────────────────────────────────────────────────────┐ │
                  │ │                                                         │ │
                  │ └─────────────────────────────────────────────────────────┘ │
                  │        <OK>                                   <Cancel>      │
                  └─────────────────────────────────────────────────────────────┘
Enter fullscreen mode Exit fullscreen mode

2. นำ GPG public key ไปเพิ่มใน GitHub

2.1. list GPG keys

gpg --list-secret-keys --keyid-format=long
Enter fullscreen mode Exit fullscreen mode
~/.gnupg/pubring.kbx
----------------------------------
sec   ed25519/EFABA125EBAF174 2024-08-16 [SC]
      1E37ABCEFAEF1F8B6697990BEFABA125EBAF174
uid                 [ultimate] Your Name (your comment explanation your key) <your@email.com>
ssb   cv25519/2A2C03ACFEFDBA9C 2024-08-16 [E]
Enter fullscreen mode Exit fullscreen mode

2.2. export GPG key

gpg --armor --export EFABA125EBAF174 | pbcopy
Enter fullscreen mode Exit fullscreen mode

บน MacOS ใช้ pbcopy คัดลอก public key ไปยัง clipboard ได้เลย
ถ้าไม่มี pbcopy ให้ใช้คำสั่ง gpg --armor --export EFABA125EBAF174 แล้วคัดลอก public key ไปยัง GitHub

  1. นำ public key ที่คัดลอกไว้ไปใส่เพิ่มใน GitHub GPG Keys โดยกดปุ่ม New GPG key -> Add GPG key

3. บอก Git ให้ใช้ GPG key ของเรา sign commit

ทำการบอก Git ให้ใช้ GPG key ของเรา sign commit โดยใช้คำสั่ง

git config --global user.signingkey EFABA125EBAF174
Enter fullscreen mode Exit fullscreen mode

--global คือการบอก Git ให้ใช้ GPG key นี้ sign commit ของเราในทุก repository ในเครื่องของเรา ไม่ว่าจะเป็น GitHub, github หรือ อื่นๆ
ซึ่งอาจจะไม่เหมาะสำหรับเราถ้าเรามีหลาย GPG key หรือต้องการใช้ GPG key ต่างกันใน repository ต่างๆ (บทความนี้จะไม่พูดถึงกรณีนี้)

หากต้องการใช้ GPG key เฉพาะใน repository นั้นๆ ให้ cd เข้าไปใน repository นั้นๆ แล้วใช้คำสั่งเดียวกันแต่ไม่ต้องใส่ --global

git config commit.gpgsign true
git config user.signingkey EFABA125EBAF174
Enter fullscreen mode Exit fullscreen mode

4. ทำการ sign ตอน git commit

หากเรา config ให้ใช้ GPG key ของเรา sign commit แล้ว ในขึ้นตอนก่อนหน้า
เราสามารถสั่ง git commit -m "message" ได้เหมือนเดิม เพียงเท่านี้ commit ของเราจะถูก sign ด้วย GPG key ของเรา

หรือหากเราไม่ได้ config ให้ใช้ GPG key ของเรา sign commit ดังขึ้นตอนก่อนหน้า
เราสามารถใช้คำสั่ง git commit -S -m "message" ในการ sign commit ด้วย GPG key ของเราเช่นกัน

5. push ขึ้น GitHub ตามปกติ และเช็คว่า commit ของเรา sign แล้วหรือยัง บน GitHub

หลังจากที่เรา push commit ขึ้น GitHub แล้ว เราสามารถเช็คว่า commit ของเรา sign แล้วหรือยัง ได้โดยดูที่หน้า commit บน GitHub ว่ามีเครื่องหมาย Verified และ GPG key ของเราอยู่ด้านล่างของ commit หรือไม่
commit verified

FAQ:

หากใช้ gnupg2 และ gpg-agent 2.x อย่าลืมตั้งค่าตัวแปรสภาพแวดล้อม GPG_TTY

export GPG_TTY=$(tty)
Enter fullscreen mode Exit fullscreen mode

อ้างอิง

Top comments (0)