Files
terminarandClaude Opus 5.5 0d062f559f feat(yaiam): fetch an SSH certificate for a public key (yaiam ADR-0046)
Client.SSHCertificate trades an access token with the scope ssh and a
public key for a short-lived OpenSSH certificate at
<issuer>/ssh/certificate; SSHCAKeys reads the keys servers trust
(ca.pub, none on 404); SSHAudience names the audience yaiam wants.
Same rules as the other calls: https (http only to loopback), no
redirects, the token only in the Authorization header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:42:02 +01:00

87 lines
3.1 KiB
Go

package yaiam
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// TestSSHCertificate: the public key and the bearer token go to
// <issuer>/ssh/certificate, the answer comes back; a refusal is an
// *Error with yaiam's code; no token is ErrLoginRequired; ca.pub lists
// the CA keys, none on 404; a plain-http issuer elsewhere than loopback
// is refused before anything is sent.
func TestSSHCertificate(t *testing.T) {
var gotAuth, gotKey string
refuse := ""
caKeys := "ssh-ed25519 AAAA-one\nssh-ed25519 AAAA-two\n"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodPost && r.URL.Path == "/t/default/ssh/certificate":
gotAuth = r.Header.Get("Authorization")
var in map[string]string
json.NewDecoder(r.Body).Decode(&in)
gotKey = in["public_key"]
if refuse != "" {
w.WriteHeader(http.StatusForbidden)
json.NewEncoder(w).Encode(map[string]string{"error": refuse, "error_description": "no SSH certificate for this token"})
return
}
now := time.Now().UTC()
json.NewEncoder(w).Encode(map[string]any{"certificate": "ssh-ed25519-cert-v01@openssh.com AAAA", "serial": "42",
"principals": []string{"user:alice"}, "valid_after": now.Add(-5 * time.Minute), "valid_before": now.Add(8 * time.Hour)})
case r.Method == http.MethodGet && r.URL.Path == "/t/default/ssh/ca.pub":
if caKeys == "" {
http.NotFound(w, r)
return
}
w.Write([]byte(caKeys))
default:
http.NotFound(w, r)
}
}))
defer ts.Close()
c := &Client{Issuer: ts.URL + "/t/default", ClientID: "ssh"}
ctx := context.Background()
tok := &Token{AccessToken: "the-token"}
cert, err := c.SSHCertificate(ctx, tok, " ssh-ed25519 AAAA-user alice@laptop\n")
if err != nil {
t.Fatal(err)
}
if gotAuth != "Bearer the-token" || gotKey != "ssh-ed25519 AAAA-user alice@laptop" || cert.Serial != "42" ||
len(cert.Principals) != 1 || !strings.HasPrefix(cert.Certificate, "ssh-ed25519-cert") {
t.Errorf("sent %q %q, got %+v", gotAuth, gotKey, cert)
}
refuse = "access_denied"
var e *Error
if _, err := c.SSHCertificate(ctx, tok, "ssh-ed25519 AAAA"); !errors.As(err, &e) || e.Code != "access_denied" || e.Status != http.StatusForbidden {
t.Errorf("refused: %v", err)
}
if _, err := c.SSHCertificate(ctx, nil, "ssh-ed25519 AAAA"); !errors.Is(err, ErrLoginRequired) {
t.Errorf("no token: %v", err)
}
if SSHAudience(c.Issuer) != ts.URL+"/t/default/ssh" || SSHAudience(c.Issuer+"/") != ts.URL+"/t/default/ssh" {
t.Errorf("audience = %q", SSHAudience(c.Issuer))
}
keys, err := c.SSHCAKeys(ctx)
if err != nil || len(keys) != 2 || keys[1] != "ssh-ed25519 AAAA-two" {
t.Errorf("CA keys = %v, %v", keys, err)
}
caKeys = ""
if keys, err := c.SSHCAKeys(ctx); err != nil || keys != nil {
t.Errorf("no CA = %v, %v", keys, err)
}
far := &Client{Issuer: "http://login.example.org/t/default", ClientID: "ssh"}
if _, err := far.SSHCertificate(ctx, tok, "ssh-ed25519 AAAA"); err == nil || !strings.Contains(err.Error(), "not https") {
t.Errorf("plain http elsewhere: %v", err)
}
}