|
| 1 | +package main |
| 2 | + |
| 3 | +/* |
| 4 | +Apache License 2.0 |
| 5 | +
|
| 6 | +Copyright 2026 Shane |
| 7 | +
|
| 8 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +you may not use this file except in compliance with the License. |
| 10 | +You may obtain a copy of the License at |
| 11 | +
|
| 12 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +
|
| 14 | +Unless required by applicable law or agreed to in writing, software |
| 15 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +See the License for the specific language governing permissions and |
| 18 | +limitations under the License. |
| 19 | +*/ |
| 20 | + |
| 21 | +import ( |
| 22 | + "encoding/base64" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | + |
| 28 | + cryptosv1 "github.com/CryptOS-PKI/api/go/cryptos/v1" |
| 29 | +) |
| 30 | + |
| 31 | +func TestHumanStatusShowsProtocols(t *testing.T) { |
| 32 | + s := &cryptosv1.NodeStatus{ |
| 33 | + Role: cryptosv1.NodeRole_NODE_ROLE_ISSUING, |
| 34 | + Protocols: []*cryptosv1.ProtocolStatus{ |
| 35 | + {Protocol: cryptosv1.ServiceProtocol_SERVICE_PROTOCOL_ACME, Configured: true, Running: true}, |
| 36 | + {Protocol: cryptosv1.ServiceProtocol_SERVICE_PROTOCOL_EST, Configured: true, Running: false, RebootPending: true}, |
| 37 | + }, |
| 38 | + ConfigRebootPending: true, |
| 39 | + } |
| 40 | + out := humanStatus(s) |
| 41 | + for _, want := range []string{ |
| 42 | + "Protocols:", "ACME on", "EST on (not running, reboot pending)", |
| 43 | + "Reboot:", "pending", |
| 44 | + } { |
| 45 | + if !strings.Contains(out, want) { |
| 46 | + t.Errorf("humanStatus missing %q in:\n%s", want, out) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + off := humanStatus(&cryptosv1.NodeStatus{ |
| 51 | + Protocols: []*cryptosv1.ProtocolStatus{ |
| 52 | + {Protocol: cryptosv1.ServiceProtocol_SERVICE_PROTOCOL_ACME}, |
| 53 | + {Protocol: cryptosv1.ServiceProtocol_SERVICE_PROTOCOL_EST, Running: true, RebootPending: true}, |
| 54 | + }, |
| 55 | + }) |
| 56 | + for _, want := range []string{"ACME off", "EST off (still running, reboot pending)"} { |
| 57 | + if !strings.Contains(off, want) { |
| 58 | + t.Errorf("humanStatus missing %q in:\n%s", want, off) |
| 59 | + } |
| 60 | + } |
| 61 | + if strings.Contains(off, "Reboot:") { |
| 62 | + t.Errorf("humanStatus flagged a reboot the node did not report:\n%s", off) |
| 63 | + } |
| 64 | + |
| 65 | + // A node that does not report protocols (maintenance mode, an older node) |
| 66 | + // prints no Protocols line rather than an empty one. |
| 67 | + if bare := humanStatus(&cryptosv1.NodeStatus{}); strings.Contains(bare, "Protocols:") { |
| 68 | + t.Errorf("humanStatus printed unreported protocols:\n%s", bare) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +const protocolsConfig = `apiVersion: cryptos.dev/v1alpha1 |
| 73 | +kind: MachineConfig |
| 74 | +metadata: |
| 75 | + name: pki-issuing-test |
| 76 | +role: |
| 77 | + kind: issuing |
| 78 | +network: |
| 79 | + interface: eth0 |
| 80 | + address: 192.0.2.30/24 |
| 81 | + gateway: 192.0.2.1 |
| 82 | +pki: |
| 83 | + root_key_alg: ECDSA-P384 |
| 84 | + root_subject: |
| 85 | + common_name: Protocols Issuing CA |
| 86 | + parent: |
| 87 | + ca_cert_sha256: abababababababababababababababababababababababababababababababab |
| 88 | + profiles: |
| 89 | + - name: leaf-server |
| 90 | + key_alg: ECDSA-P384 |
| 91 | + validity_days: 90 |
| 92 | + key_usage: [digital_signature] |
| 93 | + ext_key_usage: [server_auth] |
| 94 | + acme: |
| 95 | + base_url: https://ca.example.org/acme |
| 96 | + profile: leaf-server |
| 97 | + external_account_keys: |
| 98 | + - key_id: ops |
| 99 | + hmac_key_base64: EAB_KEY |
| 100 | +bootstrap: |
| 101 | + admin_cert_sha256: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 102 | +` |
| 103 | + |
| 104 | +// config get prints the EAB secret blank, and feeding that output back to |
| 105 | +// config apply keeps the secret the node holds. |
| 106 | +func TestConfigGetApply_KeepsWriteOnlySecret(t *testing.T) { |
| 107 | + ts := startTestServer(t) |
| 108 | + dir := t.TempDir() |
| 109 | + secret := base64.RawURLEncoding.EncodeToString([]byte("0123456789abcdef0123456789abcdef")) |
| 110 | + path := filepath.Join(dir, "machine.yaml") |
| 111 | + if err := os.WriteFile(path, []byte(strings.Replace(protocolsConfig, "EAB_KEY", secret, 1)), 0o600); err != nil { |
| 112 | + t.Fatalf("WriteFile: %v", err) |
| 113 | + } |
| 114 | + out, err := ts.run(t, "config", "apply", "-f", path) |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("config apply: %v (out=%s)", err, out) |
| 117 | + } |
| 118 | + if !strings.Contains(out, "requires_reboot=true") { |
| 119 | + t.Errorf("switching ACME on did not report requires_reboot:\n%s", out) |
| 120 | + } |
| 121 | + |
| 122 | + got, err := ts.run(t, "config", "get") |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("config get: %v (out=%s)", err, got) |
| 125 | + } |
| 126 | + if strings.Contains(got, secret) { |
| 127 | + t.Fatalf("config get printed the EAB secret:\n%s", got) |
| 128 | + } |
| 129 | + if !strings.Contains(got, "key_id: ops") { |
| 130 | + t.Errorf("config get dropped the key_id:\n%s", got) |
| 131 | + } |
| 132 | + |
| 133 | + back := filepath.Join(dir, "back.yaml") |
| 134 | + if err := os.WriteFile(back, []byte(got), 0o600); err != nil { |
| 135 | + t.Fatalf("WriteFile: %v", err) |
| 136 | + } |
| 137 | + out, err = ts.run(t, "config", "apply", "-f", back) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("config apply of the config get output: %v (out=%s)", err, out) |
| 140 | + } |
| 141 | + if !strings.Contains(out, "requires_reboot=false") { |
| 142 | + t.Errorf("an unchanged re-apply reported a reboot:\n%s", out) |
| 143 | + } |
| 144 | +} |
0 commit comments