oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1//go:build hostedlive
2
3package main
4
5import (
6 "crypto/ecdsa"
7 "crypto/ed25519"
8 "crypto/elliptic"
9 "crypto/rand"
10 "crypto/x509"
11 "crypto/x509/pkix"
12 "encoding/base64"
13 "encoding/json"
14 "encoding/pem"
15 "math/big"
16 "net"
17 "os"
18 "path/filepath"
19 "testing"
20 "time"
21)
22
23// keygenDirEnv names the directory TestLiveKeygen writes to.
24const keygenDirEnv = "EQUIPO_HOSTED_KEYGEN_DIR"
25
26// TestLiveKeygen writes the key and certificate files a Task 17 run needs
27// into the existing directory EQUIPO_HOSTED_KEYGEN_DIR, refusing to replace
28// any file:
29//
30// controller.key the controller's signing_key_file: 32-byte ed25519 seed, standard base64, mode 0600
31// controller.pub its public key, standard base64 (EQUIPO_HOSTED_LIVE_CONTROLLER_KEY_FILE)
32// instance.key instance key seed, as controller.key (EQUIPO_HOSTED_LIVE_KEY_FILE)
33// operator.key operator key seed, as controller.key (EQUIPO_HOSTED_LIVE_OPERATOR_KEY_FILE)
34// verify_keys.json the config's verify_keys: task17-instance (instance), task17-operator (operator)
35// tls.crt, tls.key a self-signed P-256 certificate for 127.0.0.1, valid 7 days; tls.crt is also
36// EQUIPO_HOSTED_LIVE_CA_FILE
37//
38// It checks controller.key and verify_keys.json as the controller's config
39// loader reads them. It makes no network call.
40func TestLiveKeygen(t *testing.T) {
41 dir := os.Getenv(keygenDirEnv)
42 if dir == "" {
43 t.Skip(keygenDirEnv + " is not set")
44 }
45 write := func(name string, data []byte, mode os.FileMode) string {
46 t.Helper()
47 path := filepath.Join(dir, name)
48 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode)
49 if err != nil {
50 t.Fatalf("write %s: %v", path, err)
51 }
52 if _, err := f.Write(data); err != nil {
53 t.Fatalf("write %s: %v", path, err)
54 }
55 if err := f.Close(); err != nil {
56 t.Fatalf("write %s: %v", path, err)
57 }
58 return path
59 }
60 seed := func(name string) ed25519.PrivateKey {
61 t.Helper()
62 s := make([]byte, ed25519.SeedSize)
63 if _, err := rand.Read(s); err != nil {
64 t.Fatal(err)
65 }
66 write(name+".key", []byte(base64.StdEncoding.EncodeToString(s)+"\n"), 0o600)
67 return ed25519.NewKeyFromSeed(s)
68 }
69 pub := func(k ed25519.PrivateKey) string {
70 return base64.StdEncoding.EncodeToString(k.Public().(ed25519.PublicKey))
71 }
72
73 ctl := seed("controller")
74 write("controller.pub", []byte(pub(ctl)+"\n"), 0o644)
75 inst, op := seed("instance"), seed("operator")
76 entries := []keyEntry{
77 {ID: "task17-instance", Role: "instance", PublicKey: pub(inst)},
78 {ID: "task17-operator", Role: "operator", PublicKey: pub(op)},
79 }
80 if _, err := verifyKeys(entries); err != nil {
81 t.Fatalf("the controller would refuse these verify keys: %v", err)
82 }
83 vk, err := json.MarshalIndent(entries, "", " ")
84 if err != nil {
85 t.Fatal(err)
86 }
87 write("verify_keys.json", append(vk, '\n'), 0o644)
88 if got, err := readSigningKey(filepath.Join(dir, "controller.key")); err != nil || !got.Equal(ctl) {
89 t.Fatalf("the controller reads controller.key as %v, %v", got.Public(), err)
90 }
91
92 tlsKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
93 if err != nil {
94 t.Fatal(err)
95 }
96 now := time.Now()
97 tmpl := &x509.Certificate{SerialNumber: big.NewInt(now.UnixNano()), Subject: pkix.Name{CommonName: "equipo-hosted-controller task17"},
98 NotBefore: now.Add(-time.Hour), NotAfter: now.Add(7 * 24 * time.Hour), IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)},
99 KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
100 BasicConstraintsValid: true, IsCA: true}
101 der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &tlsKey.PublicKey, tlsKey)
102 if err != nil {
103 t.Fatal(err)
104 }
105 keyDER, err := x509.MarshalPKCS8PrivateKey(tlsKey)
106 if err != nil {
107 t.Fatal(err)
108 }
109 write("tls.crt", pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), 0o644)
110 write("tls.key", pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER}), 0o600)
111 t.Logf("wrote controller.key, controller.pub, instance.key, operator.key, verify_keys.json, tls.crt and tls.key to %s", dir)
112}
113