This is an automated email from the ASF dual-hosted git repository.

lahirujayathilake pushed a commit to branch ssh-certs
in repository https://gitbox.apache.org/repos/asf/airavata-custos.git

commit 6da88bb8134cfb7c3b704b3dffbf74870ec6cf83
Author: lahiruj <[email protected]>
AuthorDate: Thu Nov 12 17:14:35 2026 -0500

    Load the CA at startup and create it only when allowed
---
 dev-ops/compose/signer/config.yaml                 |  1 +
 extensions/SSH-Certificate-Signer/README.md        |  8 +++---
 .../SSH-Certificate-Signer/config.example.yaml     |  1 +
 .../internal/audit/logger.go                       |  8 ++++++
 .../internal/config/config.go                      |  3 ++-
 .../internal/vault/client.go                       | 14 ++++++++---
 extensions/SSH-Certificate-Signer/main.go          | 29 ++++++++++++++++++++++
 7 files changed, 56 insertions(+), 8 deletions(-)

diff --git a/dev-ops/compose/signer/config.yaml 
b/dev-ops/compose/signer/config.yaml
index 2eb982afa..c4d14f6aa 100644
--- a/dev-ops/compose/signer/config.yaml
+++ b/dev-ops/compose/signer/config.yaml
@@ -43,6 +43,7 @@ signer:
   login_host: localhost
   login_port: 2222
   ca:
+    create_if_missing: true
     rotation:
       period_hours: 2160
       overlap_hours: 2
diff --git a/extensions/SSH-Certificate-Signer/README.md 
b/extensions/SSH-Certificate-Signer/README.md
index 40dd747ae..275fbcc85 100644
--- a/extensions/SSH-Certificate-Signer/README.md
+++ b/extensions/SSH-Certificate-Signer/README.md
@@ -58,7 +58,8 @@ The certificate names who it is for. The CA signature covers 
all of it.
 A request can also set `source_address`, a comma-separated list of IP 
addresses or CIDR blocks. It goes in the
 certificate's `source-address` critical option, and sshd then accepts the 
certificate only from those addresses.
 
-A deployment has one Ed25519 CA. It is generated in Vault by the first request 
that reads it.
+A deployment has one Ed25519 CA, stored in Vault. The signer reads it at 
startup and refuses to start without it,
+unless `signer.ca.create_if_missing` is set, in which case it creates one and 
writes a `ca_created` audit log entry.
 
 ## Prerequisites
 
@@ -85,7 +86,7 @@ The rest of this section is the manual setup.
 
 ```sql
 CREATE
-DATABASE custos_signer;
+    DATABASE custos_signer;
 ```
 
 If using the project's Docker Compose, the `custos_signer` database is created 
automatically by
@@ -118,7 +119,8 @@ The initial migration creates two tables:
 vault secrets enable -path=ssh-ca kv-v2
 ```
 
-The path must match `vault.mount_path`. The signer creates CA keys on demand. 
No manual key setup is required.
+The path must match `vault.mount_path`. Store the CA key before starting the 
signer, or set
+`signer.ca.create_if_missing` to let it create one.
 
 Ensure the configured token has read/write access to `ssh-ca/data/*`.
 
diff --git a/extensions/SSH-Certificate-Signer/config.example.yaml 
b/extensions/SSH-Certificate-Signer/config.example.yaml
index 05bd7791b..7c6fb606e 100644
--- a/extensions/SSH-Certificate-Signer/config.example.yaml
+++ b/extensions/SSH-Certificate-Signer/config.example.yaml
@@ -52,6 +52,7 @@ signer:
   login_host: login.example.org        # returned with each certificate
   login_port: 22
   ca:
+    create_if_missing: false          # true creates a CA when none is stored.
     rotation:
       period_hours: 2160              # 90 days
       overlap_hours: 2
diff --git a/extensions/SSH-Certificate-Signer/internal/audit/logger.go 
b/extensions/SSH-Certificate-Signer/internal/audit/logger.go
index b2b64bfe2..763b6d7a7 100644
--- a/extensions/SSH-Certificate-Signer/internal/audit/logger.go
+++ b/extensions/SSH-Certificate-Signer/internal/audit/logger.go
@@ -94,3 +94,11 @@ func (l *Logger) LogIssuance(ctx context.Context, entry 
*IssuanceEntry) error {
 
        return nil
 }
+
+func (l *Logger) LogCACreated(ctx context.Context, caFingerprint string) {
+       l.slogger.WarnContext(ctx, "CA created",
+               "event_type", "ca_created",
+               "timestamp", time.Now().UTC().Format(time.RFC3339),
+               "ca_fingerprint", caFingerprint,
+       )
+}
diff --git a/extensions/SSH-Certificate-Signer/internal/config/config.go 
b/extensions/SSH-Certificate-Signer/internal/config/config.go
index 46b7553dd..68127c104 100644
--- a/extensions/SSH-Certificate-Signer/internal/config/config.go
+++ b/extensions/SSH-Certificate-Signer/internal/config/config.go
@@ -73,7 +73,8 @@ type SignerConfig struct {
 }
 
 type CAConfig struct {
-       Rotation RotationConfig `yaml:"rotation"`
+       CreateIfMissing bool           `yaml:"create_if_missing"`
+       Rotation        RotationConfig `yaml:"rotation"`
 }
 
 type RotationConfig struct {
diff --git a/extensions/SSH-Certificate-Signer/internal/vault/client.go 
b/extensions/SSH-Certificate-Signer/internal/vault/client.go
index ab07f6346..b51c1d0d6 100644
--- a/extensions/SSH-Certificate-Signer/internal/vault/client.go
+++ b/extensions/SSH-Certificate-Signer/internal/vault/client.go
@@ -37,6 +37,8 @@ import (
 // ErrSerialConflict marks a lost CAS race on the serial counter, not a store 
failure.
 var ErrSerialConflict = errors.New("serial counter conflict")
 
+var ErrCANotFound = errors.New("no CA key in vault")
+
 func isCASMismatch(err error) bool {
        var respErr *vaultapi.ResponseError
        if !errors.As(err, &respErr) {
@@ -109,17 +111,21 @@ func (c *Client) kvPath(parts ...string) string {
        return path
 }
 
-// GetCurrentCAKey returns the current CA key, auto-creating one if it doesn't 
exist.
 func (c *Client) GetCurrentCAKey(ctx context.Context) (*CAKeyPair, error) {
        kp, err := c.readCAKey(ctx, "current")
        if err != nil {
                return nil, err
        }
-       if kp != nil {
-               return kp, nil
+       if kp == nil {
+               return nil, ErrCANotFound
        }
+       return kp, nil
+}
 
-       kp, err = generateEd25519CAKey()
+// GenerateCAKey generates a new CA key and stores it as the current one.
+// TODO - also publish the new public key to the login nodes' trusted CA file.
+func (c *Client) GenerateCAKey(ctx context.Context) (*CAKeyPair, error) {
+       kp, err := generateEd25519CAKey()
        if err != nil {
                return nil, err
        }
diff --git a/extensions/SSH-Certificate-Signer/main.go 
b/extensions/SSH-Certificate-Signer/main.go
index 0d07eea78..60405f626 100644
--- a/extensions/SSH-Certificate-Signer/main.go
+++ b/extensions/SSH-Certificate-Signer/main.go
@@ -16,6 +16,7 @@
 package main
 
 import (
+       "context"
        "errors"
        "fmt"
        "log/slog"
@@ -24,6 +25,7 @@ import (
        "time"
 
        "github.com/apache/airavata-custos/signer/internal/audit"
+       "github.com/apache/airavata-custos/signer/internal/cert"
        "github.com/apache/airavata-custos/signer/internal/config"
        "github.com/apache/airavata-custos/signer/internal/handler"
        "github.com/apache/airavata-custos/signer/internal/policy"
@@ -151,6 +153,13 @@ func runServer(cfg *config.Config, logger *slog.Logger, 
autoMigrate bool) {
        policyEnforcer := policy.NewEnforcer(cfg.Signer.Policy.MaxTTLSeconds, 
cfg.Signer.Policy.AllowedKeyTypes)
        auditLogger := audit.NewLogger(db, logger)
 
+       caFingerprint, err := loadCA(context.Background(), vaultClient, 
cfg.Signer.CA.CreateIfMissing, auditLogger)
+       if err != nil {
+               logger.Error("failed to load CA", "error", err)
+               os.Exit(1)
+       }
+       logger.Info("CA loaded", "ca_fingerprint", caFingerprint)
+
        cacheTTL := time.Duration(cfg.Signer.Validation.CacheTTLSeconds) * 
time.Second
        ldapConnector := validation.NewDefaultLDAPConnector()
        principalValidator := validation.NewValidatorDispatcher(
@@ -190,6 +199,26 @@ func runServer(cfg *config.Config, logger *slog.Logger, 
autoMigrate bool) {
        }
 }
 
+// loadCA makes sure the CA exists before the signer serves requests and 
returns its fingerprint.
+func loadCA(ctx context.Context, vaultClient *vault.Client, createIfMissing 
bool, auditLogger *audit.Logger) (string, error) {
+       kp, err := vaultClient.GetCurrentCAKey(ctx)
+       if errors.Is(err, vault.ErrCANotFound) {
+               if !createIfMissing {
+                       return "", fmt.Errorf("%w: store the CA key, or set 
signer.ca.create_if_missing to create one", err)
+               }
+               if kp, err = vaultClient.GenerateCAKey(ctx); err != nil {
+                       return "", err
+               }
+               fingerprint, err := cert.CAFingerprint(kp.PublicKey)
+               auditLogger.LogCACreated(ctx, fingerprint)
+               return fingerprint, err
+       }
+       if err != nil {
+               return "", err
+       }
+       return cert.CAFingerprint(kp.PublicKey)
+}
+
 func setupLogger(cfg config.LoggingConfig) *slog.Logger {
        var level slog.Level
        switch strings.ToLower(cfg.Level) {

Reply via email to