Serge Bazanski | 9ffa1f9 | 2021-09-01 15:42:23 +0200 | [diff] [blame^] | 1 | package pki |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/tls" |
| 6 | "crypto/x509" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // EphemeralClusterCredentials returns a pair of node and manager |
| 11 | // tls.Certificates signed by a CA certificate. |
| 12 | // |
| 13 | // All of these are ephemeral, ie. not stored anywhere - including the CA |
| 14 | // certificate. This function is for use by tests which want to bring up a |
| 15 | // minimum set of PKI credentials for a fake Metropolis cluster. |
| 16 | func EphemeralClusterCredentials(t *testing.T) (node, manager tls.Certificate, ca *x509.Certificate) { |
| 17 | ctx := context.Background() |
| 18 | |
| 19 | ns := Namespaced("unused") |
| 20 | caCert := Certificate{ |
| 21 | Namespace: &ns, |
| 22 | Issuer: SelfSigned, |
| 23 | Template: CA("test cluster ca"), |
| 24 | Mode: CertificateEphemeral, |
| 25 | } |
| 26 | caBytes, err := caCert.Ensure(ctx, nil) |
| 27 | if err != nil { |
| 28 | t.Fatalf("Could not ensure CA certificate: %v", err) |
| 29 | } |
| 30 | ca, err = x509.ParseCertificate(caBytes) |
| 31 | if err != nil { |
| 32 | t.Fatalf("Could not parse new CA certificate: %v", err) |
| 33 | } |
| 34 | |
| 35 | nodeCert := Certificate{ |
| 36 | Namespace: &ns, |
| 37 | Issuer: &caCert, |
| 38 | Template: Server([]string{"test-server"}, nil), |
| 39 | Mode: CertificateEphemeral, |
| 40 | } |
| 41 | nodeBytes, err := nodeCert.Ensure(ctx, nil) |
| 42 | if err != nil { |
| 43 | t.Fatalf("Could not ensure node certificate: %v", err) |
| 44 | } |
| 45 | node = tls.Certificate{ |
| 46 | Certificate: [][]byte{nodeBytes}, |
| 47 | PrivateKey: nodeCert.PrivateKey, |
| 48 | } |
| 49 | |
| 50 | managerCert := Certificate{ |
| 51 | Namespace: &ns, |
| 52 | Issuer: &caCert, |
| 53 | Template: Client("owner", nil), |
| 54 | Mode: CertificateEphemeral, |
| 55 | } |
| 56 | managerBytes, err := managerCert.Ensure(ctx, nil) |
| 57 | if err != nil { |
| 58 | t.Fatalf("Could not ensure manager certificate: %v", err) |
| 59 | } |
| 60 | manager = tls.Certificate{ |
| 61 | Certificate: [][]byte{managerBytes}, |
| 62 | PrivateKey: managerCert.PrivateKey, |
| 63 | } |
| 64 | return |
| 65 | } |