0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Docker】GnuPG で署名鍵ペアをパスフレーズなしで作成する【Alpine ベース】

Last updated at Posted at 2025-02-08

CI やテストなどで使い捨てできる、PEM 形式の OpenPGP 署名鍵を docker で作成したい場合の、Alpine Linux ベースで動作するシェル(ash 対応)スクリプトと実行例です。

作成される鍵は、パスフレーズなしの Ed25519 です。

ちなみに、ローカルで実行したい場合は、以下の通り。詳しくは、下部にある gen_key_pairs.sh を参考にしてください。

  • 鍵のアルゴリズム: default (= Ed25519, gpg のデフォルト)
  • 鍵の使用目的: default (= cert & sign, gpg のデフォルト)
  • 鍵の有効期限: 0 (なし。無期限)
ローカルで実行する場合のコマンド(パスフレーズなし)
gpg --batch --expert --quick-gen-key --passphrase '' "MyName <my_name@example.com>" default default 0
gen_key_pairs.sh のあるディレクトリで実行
docker run --rm \
    -w "/data" \
    -e TEST_ID="YourName <your_name@example.com>" \
    -v "$(pwd)":/data \
    alpine:latest \
    /bin/sh /data/gen_key_pairs.sh
実行例
$ ls
gen_key_pairs.sh

$ docker run --rm -w "/data" -e TEST_ID="YourName <your_name@example.com>" -v "$(pwd)":/data alpine:latest /bin/sh /data/gen_key_pairs.sh
**snip**

$ ls
gen_key_pairs.sh	gpg-ed25519-public.pem	gpg-ed25519-secret.pem

下記 gen_key_pairs.sh スクリプトは、GnuPG(gpg)でパスフレーズなしの Ed25519 署名鍵を作成し、マウントされたディレクトリ(スクリプトと同じ階層)に PEM 形式(OpenPGP 互換)の公開鍵と秘密鍵が作成されます。

gen_key_pairs.sh
#!/bin/sh

# Note: This script is intended to be run inside the Alpine Linux-base Docker
# container.

set -e

# Install GnuPG
apk add --no-cache --no-progress --quiet gpg gpg-agent

# Define arguments
testID="MyName <my_name@example.com>" # default value
[ "${TEST_ID:+defined}" ] && {
    echo "* TEST_ID is defined"
    testID="${TEST_ID}"

    echo "  * User ID: ${testID}"
}

algo=ed25519 # --> equivalent to "default"
usage=sign # --> equivalent to "default". "cert" is by default on, so add "sign" to be "SC"
expire=0
nameFilePubKey="gpg-ed25519-public.pem"
nameFileSecKey="gpg-ed25519-secret.pem"

echo "* Generating a key pair ..."
# Generate a key pair
# Usage:
#   gpg [options] --quick-generate-key USER-ID [ALGO [USAGE [EXPIRE]]]
# --batch : non-interactive
# --expert : extra options
gpg --batch --expert --quick-gen-key --passphrase '' \
    "$testID" \
    $algo \
    $usage \
    $expire

# Print for debugging. Be careful with the output.
echo "* Public Key info"
gpg --list-public-keys "$testID"

echo "* Private Key info"
gpg --list-secret-keys "$testID"

# Export the public key to a PEM file
gpg --armor --export "$testID" > $nameFilePubKey

# Export the private key to a PEM file
gpg --armor --export-secret-keys "$testID" > $nameFileSecKey

併せて読みたい

動作環境

  • OS: macOS 12.7.6, 21H1320
  • Docker version 27.5.1, build 9f9e405
  • Image OS: Alpine Linux v3.21
    • gpg (GnuPG) 2.4.7
    • libgcrypt 1.10.3
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?