Skip to content

Commit 5c4acbb

Browse files
authored
Generate test GPG keys when running the tests (#1538)
1 parent f9a1c90 commit 5c4acbb

File tree

5 files changed

+86
-30
lines changed

5 files changed

+86
-30
lines changed

scripts/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gpg-private.asc
2+
gpg-public.asc

scripts/gpg-private.asc

Lines changed: 0 additions & 17 deletions
This file was deleted.

scripts/gpg-public.asc

Lines changed: 0 additions & 13 deletions
This file was deleted.

scripts/gpgkey/main.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package main
6+
7+
import (
8+
"log"
9+
"os"
10+
"strings"
11+
12+
"github.com/ProtonMail/gopenpgp/v2/crypto"
13+
)
14+
15+
const (
16+
signerPassphraseEnv = "ELASTIC_PACKAGE_SIGNER_PASSPHRASE"
17+
privateKeyPathEnv = "ELASTIC_PACKAGE_SIGNER_PRIVATE_KEYFILE"
18+
)
19+
20+
func main() {
21+
passphrase := readPassphrase()
22+
privateKeyPath, publicKeyPath := getKeyPaths()
23+
rsaKeyArmor, rsaPublicKeyArmor := genKey(passphrase)
24+
25+
err := os.WriteFile(privateKeyPath, rsaKeyArmor, 0644)
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
err = os.WriteFile(publicKeyPath, rsaPublicKeyArmor, 0644)
30+
if err != nil {
31+
log.Fatal(err)
32+
}
33+
}
34+
35+
func genKey(passphrase []byte) ([]byte, []byte) {
36+
const (
37+
name = "Elastic Package Test"
38+
rsaBits = 2048
39+
)
40+
41+
rsaKey, err := crypto.GenerateKey(name, "", "rsa", rsaBits)
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
rsaKey, err = rsaKey.Lock(passphrase)
46+
if err != nil {
47+
log.Fatal(err)
48+
}
49+
50+
rsaKeyArmor, err := rsaKey.Armor()
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
rsaPublicKeyArmor, err := rsaKey.GetArmoredPublicKey()
55+
if err != nil {
56+
log.Fatal(err)
57+
}
58+
59+
return []byte(rsaKeyArmor), []byte(rsaPublicKeyArmor)
60+
}
61+
62+
func readPassphrase() []byte {
63+
passphrase := os.Getenv(signerPassphraseEnv)
64+
if passphrase == "" {
65+
log.Fatalf("Environment variable %s empty or not set", signerPassphraseEnv)
66+
}
67+
return []byte(passphrase)
68+
}
69+
70+
func getKeyPaths() (string, string) {
71+
privateKeyPath := os.Getenv(privateKeyPathEnv)
72+
if privateKeyPath == "" {
73+
log.Fatalf("Environment variable %s empty or not set", privateKeyPathEnv)
74+
}
75+
76+
publicKeyPath := strings.ReplaceAll(privateKeyPath, "private", "public")
77+
if privateKeyPath == publicKeyPath {
78+
log.Fatalf("The path indicated in %s is expected to contain \"private\", found: %s", privateKeyPathEnv, privateKeyPath)
79+
}
80+
81+
return privateKeyPath, publicKeyPath
82+
}

scripts/test-build-zip.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export ELASTIC_PACKAGE_SIGNER_PRIVATE_KEYFILE="$OLDPWD/scripts/gpg-private.asc"
3434
export ELASTIC_PACKAGE_SIGNER_PASSPHRASE=$(cat "$OLDPWD/scripts/gpg-pass.txt")
3535
export ELASTIC_PACKAGE_LINKS_FILE_PATH="$(pwd)/scripts/links_table.yml"
3636

37+
go run ./scripts/gpgkey
38+
3739
for d in test/packages/*/*/; do
3840
# Packages in false_positives can have issues.
3941
if [ "$(testype $d)" == "false_positives" ]; then

0 commit comments

Comments
 (0)