mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 22:31:36 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bcefb11e65 | |||
| 75cf8475f5 | |||
| c015cab2f4 | |||
| 3da6584ab8 | |||
| 68f6fd474b | |||
| 614e4e636b | |||
| 370f856725 | |||
| 7382e5f03b | |||
| 5567d4b411 | |||
| e5516d7286 | |||
| fd94e0bd19 | |||
| d0415d3b5e | |||
| c6efa4ab39 | |||
| dedf7fa3a9 | |||
| 4b5927dfff | |||
| cc03f55006 | |||
| 93e1dc598c |
@@ -19,7 +19,7 @@ jobs:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.25'
|
||||
go-version: '1.25.9'
|
||||
|
||||
- name: Go Build
|
||||
run: |
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ certctl-cli
|
||||
/cli
|
||||
|
||||
# Private strategy docs
|
||||
roadmap.md
|
||||
strategy.md
|
||||
SECURITY_REMEDIATION.md
|
||||
|
||||
# OS
|
||||
|
||||
@@ -6,13 +6,20 @@ Licensor: Shankar Reddy
|
||||
Licensed Work: certctl
|
||||
The Licensed Work is (c) 2026 Shankar Reddy.
|
||||
Additional Use Grant: You may make use of the Licensed Work, provided that
|
||||
you may not use the Licensed Work for a Certificate
|
||||
Management Service. A "Certificate Management Service"
|
||||
is a commercial offering that allows third parties
|
||||
(other than your employees and contractors acting on
|
||||
your behalf) to access and/or use the Licensed Work's
|
||||
certificate lifecycle management functionality as part
|
||||
of a hosted or managed service.
|
||||
you may not use the Licensed Work for a Commercial
|
||||
Certificate Service. A "Commercial Certificate Service"
|
||||
is any product, service, or offering in which a third
|
||||
party (other than your employees and contractors
|
||||
acting on your behalf) accesses, uses, or benefits
|
||||
from the Licensed Work's certificate management
|
||||
functionality — including but not limited to lifecycle
|
||||
management, discovery, monitoring, alerting, renewal
|
||||
automation, deployment, and revocation — as part of
|
||||
or in connection with an offering for which
|
||||
compensation is received. This restriction applies
|
||||
regardless of whether the Licensed Work is hosted,
|
||||
managed, embedded, bundled, or integrated with
|
||||
another product or service.
|
||||
|
||||
Change Date: March 14, 2033
|
||||
|
||||
|
||||
@@ -38,6 +38,8 @@ gantt
|
||||
|
||||
> **Actively maintained — shipping weekly.** Found something? [Open a GitHub issue](https://github.com/shankar0123/certctl/issues) — issues get triaged same-day. CI runs the full test suite with race detection, static analysis, and vulnerability scanning on every commit.
|
||||
|
||||
**Ready to try it?** Jump to the [Quick Start](#quick-start) — you'll have a running dashboard in under 5 minutes.
|
||||
|
||||
## Why certctl Exists
|
||||
|
||||
Certificate lifecycle tooling today falls into two camps: expensive enterprise platforms (Venafi, Keyfactor, Sectigo) that cost six figures and take months to deploy, or single-purpose tools (cert-manager, certbot) that handle one slice of the problem. If you run a mixed infrastructure — some NGINX, some Apache, a few HAProxy nodes, IIS on Windows, maybe an F5 — and you need to manage certificates from multiple CAs, there's nothing self-hosted that covers the full lifecycle without vendor lock-in.
|
||||
@@ -46,7 +48,7 @@ certctl fills that gap. It's **CA-agnostic** — plug in any certificate authori
|
||||
|
||||
It's **target-agnostic**. Agents deploy certificates to NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS (local PowerShell or remote WinRM), F5 BIG-IP (proxy agent), and any Linux/Unix server via SSH/SFTP — all using the same pluggable connector model. The control plane never initiates outbound connections — agents poll for work, which means certctl works behind firewalls, across network zones, and in air-gapped environments.
|
||||
|
||||
For a detailed comparison with CertKit, KeyTalk, and enterprise platforms, see [Why certctl?](docs/why-certctl.md)
|
||||
For a detailed comparison with other competitors and enterprise platforms, see [Why certctl?](docs/why-certctl.md)
|
||||
|
||||
## Who Is This For
|
||||
|
||||
@@ -60,7 +62,7 @@ For a detailed comparison with CertKit, KeyTalk, and enterprise platforms, see [
|
||||
|
||||
- **Certificates renew and deploy themselves.** The scheduler monitors expiration, creates renewal jobs, issues certificates through your CA, and deploys them to target servers — all without human intervention. ACME ARI (RFC 9773) lets your CA tell certctl exactly when to renew. Ready for 45-day and 6-day certificate lifetimes (SC-081v3 and Let's Encrypt shortlived profiles).
|
||||
|
||||
- **You see everything in one place.** A 25-page operational dashboard shows every certificate across every server: status, ownership, expiration timeline, deployment history with TLS verification, discovery triage, and real-time agent fleet health. Bulk operations (renew, revoke, reassign) work across selections.
|
||||
- **You see everything in one place.** The operational dashboard shows every certificate across every server: status, ownership, expiration timeline, deployment history with TLS verification, discovery triage, and real-time agent fleet health. Bulk operations (renew, revoke, reassign) work across selections.
|
||||
|
||||
- **Private keys never leave your servers.** Agents generate ECDSA P-256 keys locally and submit only the CSR. The control plane never touches private keys. Post-deployment TLS verification confirms the right certificate is actually being served.
|
||||
|
||||
@@ -68,9 +70,11 @@ For a detailed comparison with CertKit, KeyTalk, and enterprise platforms, see [
|
||||
|
||||
- **Everything is auditable.** Immutable append-only audit trail records every lifecycle action, every API call, and every approval decision. Certificate digest emails deliver daily briefings. Prometheus metrics endpoint for Grafana dashboards.
|
||||
|
||||
- **Multiple interfaces for different workflows.** REST API (97 endpoints) for automation, CLI for scripting, MCP server for AI assistants (Claude, Cursor, Windsurf), EST server (RFC 7030) for device enrollment, Helm chart for Kubernetes, and the web dashboard for day-to-day operations.
|
||||
- **Standards-based protocol support.** EST server (RFC 7030) for device and WiFi certificate enrollment. SCEP server (RFC 8894) for MDM platforms and network device enrollment. ACME ARI (RFC 9773) for CA-directed renewal timing. S/MIME certificate issuance with email protection EKU for end-to-end encrypted email. DER-encoded X.509 CRL and embedded OCSP responder for revocation infrastructure.
|
||||
|
||||
For the full capability breakdown — revocation infrastructure (CRL + OCSP), policy engine, certificate profiles, S/MIME support, approval workflows, and more — see the [Feature Inventory](docs/features.md).
|
||||
- **Multiple interfaces for different workflows.** REST API (107 routes) for automation, CLI for scripting, MCP server for AI assistants (Claude, Cursor, Windsurf), Helm chart for Kubernetes, and the web dashboard (24 pages) for day-to-day operations.
|
||||
|
||||
For the full capability breakdown, including the policy engine, certificate profiles, approval workflows, certificate export (PEM/PKCS#12), and more, see the [Feature Inventory](docs/features.md).
|
||||
|
||||
## Supported Integrations
|
||||
|
||||
@@ -82,12 +86,11 @@ For the full capability breakdown — revocation infrastructure (CRL + OCSP), po
|
||||
| ACME EAB (ZeroSSL, Google Trust) | Implemented (auto-fetch EAB from ZeroSSL) | `ACME` |
|
||||
| step-ca | Implemented | `StepCA` |
|
||||
| OpenSSL / Custom CA | Implemented | `OpenSSL` |
|
||||
| Vault PKI | Beta | `VaultPKI` |
|
||||
| DigiCert CertCentral | Beta | `DigiCert` |
|
||||
| Sectigo SCM | Beta | `Sectigo` |
|
||||
| Google CAS | Beta | `GoogleCAS` |
|
||||
|
||||
**Vault PKI, DigiCert, Sectigo, and Google CAS connectors are in beta.** If you hit any bugs or unexpected behavior, please [open a GitHub issue](https://github.com/shankar0123/certctl/issues) -- we're actively testing these and want to hear from real users.
|
||||
| Vault PKI | Implemented | `VaultPKI` |
|
||||
| DigiCert CertCentral | Implemented | `DigiCert` |
|
||||
| Sectigo SCM | Implemented | `Sectigo` |
|
||||
| Google CAS | Implemented | `GoogleCAS` |
|
||||
| AWS ACM Private CA | Implemented | `AWSACMPCA` |
|
||||
|
||||
**Note:** ADCS integration is handled via the Local CA's sub-CA mode — certctl operates as a subordinate CA with its signing certificate issued by ADCS. Any CA with a shell-accessible signing interface can be integrated today via the OpenSSL/Custom CA connector.
|
||||
|
||||
@@ -103,8 +106,11 @@ For the full capability breakdown — revocation infrastructure (CRL + OCSP), po
|
||||
| Postfix | Implemented | `Postfix` |
|
||||
| Dovecot | Implemented | `Dovecot` |
|
||||
| Microsoft IIS | Implemented (local + WinRM) | `IIS` |
|
||||
| F5 BIG-IP | Beta | `F5` |
|
||||
| SSH (Agentless) | Beta | `SSH` |
|
||||
| F5 BIG-IP | Implemented (proxy agent) | `F5` |
|
||||
| SSH (Agentless) | Implemented | `SSH` |
|
||||
| Windows Cert Store | Implemented | `WinCertStore` |
|
||||
| Java Keystore | Implemented | `JavaKeystore` |
|
||||
| Kubernetes Secrets | Implemented | `KubernetesSecrets` |
|
||||
|
||||
### Notifiers
|
||||
| Notifier | Status | Type |
|
||||
@@ -158,16 +164,19 @@ cd certctl
|
||||
docker compose -f deploy/docker-compose.yml up -d --build
|
||||
```
|
||||
|
||||
Wait ~30 seconds, then open **http://localhost:8443** in your browser.
|
||||
Wait ~30 seconds, then open **http://localhost:8443** in your browser. The onboarding wizard walks you through connecting a CA, deploying an agent, and issuing your first certificate.
|
||||
|
||||
The dashboard comes pre-loaded with 32 demo certificates across 7 issuers, 8 agents, 180 days of job history, discovery scan data, and network scan targets — a realistic snapshot of a certificate inventory that looks like it's been running for months.
|
||||
**Want a pre-populated demo instead?** Add the demo override to see 32 certificates across 10 issuers, 8 agents, and 180 days of realistic history:
|
||||
|
||||
```bash
|
||||
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.demo.yml up -d --build
|
||||
```
|
||||
|
||||
The `deploy/` directory has four compose files: `docker-compose.yml` (base platform), `docker-compose.demo.yml` (demo data overlay), `docker-compose.dev.yml` (PgAdmin + debug logging), and `docker-compose.test.yml` (standalone integration tests with real CA backends). See the [Docker Compose Environments Guide](deploy/ENVIRONMENTS.md) for a service-by-service walkthrough, or the [Quick Start](docs/quickstart.md#docker-compose-environments) for a summary.
|
||||
|
||||
```bash
|
||||
curl http://localhost:8443/health
|
||||
# {"status":"healthy"}
|
||||
|
||||
curl -s http://localhost:8443/api/v1/certificates | jq '.total'
|
||||
# 32
|
||||
```
|
||||
|
||||
### Agent Install (One-Liner)
|
||||
@@ -178,6 +187,16 @@ curl -sSL https://raw.githubusercontent.com/shankar0123/certctl/master/install-a
|
||||
|
||||
Detects your OS and architecture, downloads the binary, configures systemd (Linux) or launchd (macOS), and starts the agent. See [install-agent.sh](install-agent.sh) for details.
|
||||
|
||||
### Helm Chart (Kubernetes)
|
||||
|
||||
```bash
|
||||
helm install certctl deploy/helm/certctl/ \
|
||||
--set server.apiKey=your-api-key \
|
||||
--set postgres.password=your-db-password
|
||||
```
|
||||
|
||||
Production-ready chart with Server Deployment, PostgreSQL StatefulSet, Agent DaemonSet, health probes, security contexts (non-root, read-only rootfs), and optional Ingress. See [values.yaml](deploy/helm/certctl/values.yaml) for all configuration options.
|
||||
|
||||
### Docker Pull
|
||||
|
||||
```bash
|
||||
@@ -217,13 +236,20 @@ Each directory contains a `docker-compose.yml` and a `README.md` explaining the
|
||||
| [Why certctl?](docs/why-certctl.md) | How certctl compares to ACME clients, agent-based SaaS, and enterprise platforms |
|
||||
| [Concepts](docs/concepts.md) | TLS certificates explained from scratch — for beginners who know nothing about certs |
|
||||
| [Quick Start](docs/quickstart.md) | 5-minute setup — dashboard, API, CLI, discovery, stakeholder demo flow |
|
||||
| [Docker Compose Environments](deploy/ENVIRONMENTS.md) | Service-by-service walkthrough of all 4 compose files, env var reference |
|
||||
| [Deployment Examples](docs/examples.md) | 5 turnkey scenarios (ACME+NGINX, wildcard DNS-01, private CA, step-ca, multi-issuer) with migration guides |
|
||||
| [Advanced Demo](docs/demo-advanced.md) | Issue a certificate end-to-end with technical deep-dives |
|
||||
| [Architecture](docs/architecture.md) | System design, data flow diagrams, security model |
|
||||
| [Feature Inventory](docs/features.md) | Complete reference of all V2 capabilities, API endpoints, and configuration |
|
||||
| [Connector Reference](docs/connectors.md) | Configuration for all 7 issuers, 10 targets, and 5 notifier connectors |
|
||||
| [Connector Reference](docs/connectors.md) | Configuration for all issuer, target, and notifier connectors |
|
||||
| [MCP Server](docs/mcp.md) | AI integration via Model Context Protocol — setup, available tools, examples |
|
||||
| [OpenAPI 3.1 Spec](docs/openapi.md) | API reference guide with endpoint overview ([raw spec](api/openapi.yaml)) |
|
||||
| [Compliance Mapping](docs/compliance.md) | SOC 2 Type II, PCI-DSS 4.0, NIST SP 800-57 alignment guides |
|
||||
| [OpenAPI 3.1 Spec](api/openapi.yaml) | 97 operations, full request/response schemas |
|
||||
| [Migrate from certbot](docs/migrate-from-certbot.md) | Step-by-step migration from certbot cron jobs to certctl |
|
||||
| [Migrate from acme.sh](docs/migrate-from-acmesh.md) | Migration guide for acme.sh users, DNS hook compatibility |
|
||||
| [certctl for cert-manager users](docs/certctl-for-cert-manager-users.md) | How certctl complements cert-manager for mixed infrastructure |
|
||||
| [Test Environment](docs/test-env.md) | Docker Compose test environment with real CA backends |
|
||||
| [Testing Guide](docs/testing-guide.md) | Comprehensive test procedures, smoke tests, and release sign-off checklist |
|
||||
|
||||
## CLI
|
||||
|
||||
@@ -297,17 +323,17 @@ Core lifecycle management — Local CA + ACME v2 issuers, NGINX target connector
|
||||
### V2: Operational Maturity — Shipped
|
||||
30+ milestones, extensively tested with CI-enforced coverage gates. Sub-CA mode, ACME DNS-01/DNS-PERSIST-01, step-ca, Vault PKI, DigiCert CertCentral, OpenSSL/Custom CA issuers. NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS targets. RFC 5280 revocation with CRL + OCSP. Certificate profiles, ownership tracking, approval workflows. Filesystem and network certificate discovery. Prometheus metrics, dashboard charts, agent fleet overview. EST server (RFC 7030), ACME ARI (RFC 9773), certificate export, S/MIME support, Helm chart, MCP server, CLI, scheduled digest emails. Slack, Teams, PagerDuty, OpsGenie, SMTP notifications. Compliance mapping (SOC 2, PCI-DSS 4.0, NIST SP 800-57). See the [Feature Inventory](docs/features.md) for details.
|
||||
|
||||
**Coming in v2.1.0:** Dynamic issuer and target configuration via GUI (no env var restarts), first-run onboarding wizard.
|
||||
Dynamic issuer and target configuration via GUI (no env var restarts), first-run onboarding wizard, Sectigo SCM, Google CAS, AWS ACM Private CA issuers, IIS (WinRM), F5 BIG-IP, SSH, Windows Certificate Store, Java Keystore, and Kubernetes Secrets target connectors.
|
||||
|
||||
### V3: certctl Pro
|
||||
Team access controls and identity provider integration (OIDC/SSO). Role-based access control with profile-gating. Event-driven architecture (NATS) with real-time operational views. Advanced search DSL, compliance and risk scoring, bulk fleet operations.
|
||||
|
||||
### V4+: Cloud, Scale & Passive Discovery
|
||||
Passive network discovery (TLS listener), Kubernetes integration (cert-manager external issuer, Secrets target), cloud infrastructure targets (AWS ALB/ACM, Azure Key Vault), extended CA support (Google CAS, EJBCA, Sectigo), and platform-scale features (Terraform provider, multi-tenancy, HSM support).
|
||||
### V4+: Cloud & Scale
|
||||
Continuous TLS health monitoring, cloud secret manager discovery, Kubernetes cert-manager external issuer, cloud infrastructure targets, extended CA support (Entrust, GlobalSign, EJBCA), and platform-scale features (Terraform provider, multi-tenancy).
|
||||
|
||||
## License
|
||||
|
||||
Certctl is licensed under the [Business Source License 1.1](LICENSE). The source code is publicly available and free to use, modify, and self-host. The one restriction: you may not offer certctl as a managed/hosted certificate management service to third parties. The BSL 1.1 license converts automatically to Apache 2.0 on March 1, 2033, providing perpetual freedom.
|
||||
Certctl is licensed under the [Business Source License 1.1](LICENSE). The source code is publicly available and free to use, modify, and self-host. The one restriction: you may not use certctl's certificate management functionality as part of a commercial offering to third parties, whether hosted, managed, embedded, bundled, or integrated. The BSL 1.1 license converts automatically to Apache 2.0 on March 14, 2033.
|
||||
|
||||
For licensing inquiries: certctl@proton.me
|
||||
|
||||
|
||||
+2
-2
@@ -2643,7 +2643,7 @@ components:
|
||||
# ─── Issuers ─────────────────────────────────────────────────────
|
||||
IssuerType:
|
||||
type: string
|
||||
enum: [ACME, GenericCA, StepCA, VaultPKI, DigiCert, Sectigo, GoogleCAS]
|
||||
enum: [ACME, GenericCA, StepCA, VaultPKI, DigiCert, Sectigo, GoogleCAS, AWSACMPCA]
|
||||
|
||||
Issuer:
|
||||
type: object
|
||||
@@ -2669,7 +2669,7 @@ components:
|
||||
# ─── Targets ─────────────────────────────────────────────────────
|
||||
TargetType:
|
||||
type: string
|
||||
enum: [NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS, F5, SSH, WinCertStore, JavaKeystore]
|
||||
enum: [NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS, F5, SSH, WinCertStore, JavaKeystore, KubernetesSecrets]
|
||||
|
||||
DeploymentTarget:
|
||||
type: object
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -828,3 +829,621 @@ func generateTestCertWithCN(commonName string) (*x509.Certificate, error) {
|
||||
func strPtr(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
// TestCreateTargetConnector_AllSupportedTypes tests connector creation for all 14 supported target types.
|
||||
func TestCreateTargetConnector_AllSupportedTypes(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
typeName string
|
||||
config interface{}
|
||||
}{
|
||||
{
|
||||
name: "NGINX",
|
||||
typeName: "NGINX",
|
||||
config: map[string]string{
|
||||
"cert_path": filepath.Join(tmpDir, "cert.pem"),
|
||||
"key_path": filepath.Join(tmpDir, "key.pem"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Apache",
|
||||
typeName: "Apache",
|
||||
config: map[string]string{
|
||||
"cert_path": filepath.Join(tmpDir, "cert.pem"),
|
||||
"key_path": filepath.Join(tmpDir, "key.pem"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "HAProxy",
|
||||
typeName: "HAProxy",
|
||||
config: map[string]string{
|
||||
"cert_path": filepath.Join(tmpDir, "cert.pem"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "F5",
|
||||
typeName: "F5",
|
||||
config: map[string]string{
|
||||
"host": "192.0.2.1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "IIS",
|
||||
typeName: "IIS",
|
||||
config: map[string]string{
|
||||
"cert_store": "My",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Traefik",
|
||||
typeName: "Traefik",
|
||||
config: map[string]string{
|
||||
"cert_dir": tmpDir,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Caddy",
|
||||
typeName: "Caddy",
|
||||
config: map[string]string{
|
||||
"mode": "file",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Envoy",
|
||||
typeName: "Envoy",
|
||||
config: map[string]string{
|
||||
"cert_dir": tmpDir,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Postfix",
|
||||
typeName: "Postfix",
|
||||
config: map[string]string{
|
||||
"cert_path": filepath.Join(tmpDir, "cert.pem"),
|
||||
"key_path": filepath.Join(tmpDir, "key.pem"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Dovecot",
|
||||
typeName: "Dovecot",
|
||||
config: map[string]string{
|
||||
"cert_path": filepath.Join(tmpDir, "cert.pem"),
|
||||
"key_path": filepath.Join(tmpDir, "key.pem"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "SSH",
|
||||
typeName: "SSH",
|
||||
config: map[string]string{
|
||||
"host": "192.0.2.1",
|
||||
"user": "root",
|
||||
"cert_path": "/etc/ssl/cert.pem",
|
||||
"key_path": "/etc/ssl/key.pem",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "WinCertStore",
|
||||
typeName: "WinCertStore",
|
||||
config: map[string]string{
|
||||
"cert_store": "My",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "JavaKeystore",
|
||||
typeName: "JavaKeystore",
|
||||
config: map[string]string{
|
||||
"keystore_path": filepath.Join(tmpDir, "keystore.jks"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "KubernetesSecrets",
|
||||
typeName: "KubernetesSecrets",
|
||||
config: map[string]string{
|
||||
"namespace": "default",
|
||||
"secret_name": "tls-secret",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: "http://localhost:8443",
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
configJSON, err := json.Marshal(tt.config)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal config: %v", err)
|
||||
}
|
||||
|
||||
connector, err := agent.createTargetConnector(tt.typeName, configJSON)
|
||||
|
||||
// Some connectors (like WinCertStore, IIS) may error on non-Windows platforms
|
||||
// or with insufficient validation. We accept either a valid connector or an error
|
||||
// for now — the real unit tests in internal/connector/target/* cover validation
|
||||
if connector == nil && err != nil {
|
||||
// This is acceptable if the connector validates required fields
|
||||
t.Logf("connector creation returned error (may be validation): %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if connector == nil {
|
||||
t.Errorf("expected connector to be non-nil for type %s", tt.typeName)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTargetConnector_InvalidJSON tests connector creation with invalid JSON for each type.
|
||||
func TestCreateTargetConnector_InvalidJSON(t *testing.T) {
|
||||
tests := []string{
|
||||
"NGINX",
|
||||
"Apache",
|
||||
"HAProxy",
|
||||
"F5",
|
||||
"IIS",
|
||||
"Traefik",
|
||||
"Caddy",
|
||||
"Envoy",
|
||||
"Postfix",
|
||||
"Dovecot",
|
||||
"SSH",
|
||||
"WinCertStore",
|
||||
"JavaKeystore",
|
||||
"KubernetesSecrets",
|
||||
}
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: "http://localhost:8443",
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
invalidJSON := json.RawMessage("{invalid json}")
|
||||
|
||||
for _, typeName := range tests {
|
||||
t.Run(typeName, func(t *testing.T) {
|
||||
_, err := agent.createTargetConnector(typeName, invalidJSON)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("expected error for invalid JSON with type %s", typeName)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTargetConnector_UnknownType tests connector creation with unknown target type.
|
||||
func TestCreateTargetConnector_UnknownType(t *testing.T) {
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: "http://localhost:8443",
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
_, err := agent.createTargetConnector("MagicBox", nil)
|
||||
|
||||
if err == nil {
|
||||
t.Error("expected error for unsupported target type")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "unsupported target type") {
|
||||
t.Errorf("expected 'unsupported target type' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateTargetConnector_EmptyConfig tests connector creation with empty config JSON.
|
||||
func TestCreateTargetConnector_EmptyConfig(t *testing.T) {
|
||||
tests := []string{
|
||||
"NGINX",
|
||||
"Apache",
|
||||
"HAProxy",
|
||||
"Traefik",
|
||||
"Caddy",
|
||||
"Envoy",
|
||||
}
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: "http://localhost:8443",
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
for _, typeName := range tests {
|
||||
t.Run(typeName, func(t *testing.T) {
|
||||
// Empty config should be handled gracefully (defaults applied)
|
||||
connector, err := agent.createTargetConnector(typeName, nil)
|
||||
|
||||
// Should not error on nil/empty config (defaults are applied)
|
||||
if err != nil {
|
||||
// Validation errors are acceptable, but parsing errors are not
|
||||
if !strings.Contains(err.Error(), "invalid") && !strings.Contains(err.Error(), "missing") {
|
||||
t.Logf("connector creation with empty config returned: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if connector == nil {
|
||||
t.Errorf("expected non-nil connector for type %s with empty config", typeName)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunDiscoveryScan_ValidCerts tests discovery scanning with valid certificates.
|
||||
func TestRunDiscoveryScan_ValidCerts(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create a valid PEM certificate file
|
||||
cert, _ := generateTestCertWithCN("example.com")
|
||||
block := &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}
|
||||
certPEM := pem.EncodeToMemory(block)
|
||||
|
||||
certPath := filepath.Join(tmpDir, "cert.pem")
|
||||
if err := os.WriteFile(certPath, certPEM, 0644); err != nil {
|
||||
t.Fatalf("failed to write certificate: %v", err)
|
||||
}
|
||||
|
||||
// Mock server to accept discovery report
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/v1/agents/a-test/discoveries" {
|
||||
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("unexpected method: %s", r.Method)
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify request body
|
||||
var payload map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
t.Logf("failed to decode discovery report: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Verify report contains certificates
|
||||
certs, ok := payload["certificates"].([]interface{})
|
||||
if !ok || len(certs) == 0 {
|
||||
t.Logf("expected certificates in report")
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: server.URL,
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
DiscoveryDirs: []string{tmpDir},
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
// Run discovery scan
|
||||
agent.runDiscoveryScan(context.Background())
|
||||
|
||||
// If we got here without panic/error, the test passes
|
||||
}
|
||||
|
||||
// TestRunDiscoveryScan_NoCertificates tests discovery scanning with empty directory.
|
||||
func TestRunDiscoveryScan_NoCertificates(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create an empty directory
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Should not receive a request if no certs found and no errors
|
||||
t.Logf("discovery report received: %s", r.URL.Path)
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: server.URL,
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
DiscoveryDirs: []string{tmpDir},
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
// Run discovery scan - should complete without error even with empty directory
|
||||
agent.runDiscoveryScan(context.Background())
|
||||
}
|
||||
|
||||
// TestRunDiscoveryScan_MultipleCerts tests discovery scanning with multiple certificate files.
|
||||
func TestRunDiscoveryScan_MultipleCerts(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create multiple certificate files
|
||||
cert1, _ := generateTestCertWithCN("cert1.example.com")
|
||||
cert2, _ := generateTestCertWithCN("cert2.example.com")
|
||||
|
||||
block1 := &pem.Block{Type: "CERTIFICATE", Bytes: cert1.Raw}
|
||||
block2 := &pem.Block{Type: "CERTIFICATE", Bytes: cert2.Raw}
|
||||
|
||||
certPath1 := filepath.Join(tmpDir, "cert1.pem")
|
||||
certPath2 := filepath.Join(tmpDir, "cert2.crt")
|
||||
|
||||
if err := os.WriteFile(certPath1, pem.EncodeToMemory(block1), 0644); err != nil {
|
||||
t.Fatalf("failed to write cert1: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(certPath2, pem.EncodeToMemory(block2), 0644); err != nil {
|
||||
t.Fatalf("failed to write cert2: %v", err)
|
||||
}
|
||||
|
||||
certCount := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/v1/agents/a-test/discoveries" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var payload map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Count certificates in report
|
||||
if certs, ok := payload["certificates"].([]interface{}); ok {
|
||||
certCount = len(certs)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: server.URL,
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
DiscoveryDirs: []string{tmpDir},
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
// Run discovery scan
|
||||
agent.runDiscoveryScan(context.Background())
|
||||
|
||||
if certCount != 2 {
|
||||
t.Logf("expected 2 certificates in discovery report, got %d", certCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunDiscoveryScan_DERCertificate tests discovery scanning with DER-encoded certificate.
|
||||
func TestRunDiscoveryScan_DERCertificate(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create a DER-encoded certificate file
|
||||
cert, _ := generateTestCertWithCN("der.example.com")
|
||||
derPath := filepath.Join(tmpDir, "cert.der")
|
||||
|
||||
if err := os.WriteFile(derPath, cert.Raw, 0644); err != nil {
|
||||
t.Fatalf("failed to write DER certificate: %v", err)
|
||||
}
|
||||
|
||||
certCount := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/v1/agents/a-test/discoveries" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var payload map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if certs, ok := payload["certificates"].([]interface{}); ok {
|
||||
certCount = len(certs)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: server.URL,
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
DiscoveryDirs: []string{tmpDir},
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
// Run discovery scan
|
||||
agent.runDiscoveryScan(context.Background())
|
||||
|
||||
if certCount != 1 {
|
||||
t.Logf("expected 1 DER certificate in discovery report, got %d", certCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunDiscoveryScan_Subdirectories tests discovery scanning with subdirectories.
|
||||
func TestRunDiscoveryScan_Subdirectories(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create subdirectory
|
||||
subDir := filepath.Join(tmpDir, "subdir")
|
||||
if err := os.MkdirAll(subDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create subdir: %v", err)
|
||||
}
|
||||
|
||||
// Create certificate in subdirectory
|
||||
cert, _ := generateTestCertWithCN("subdir.example.com")
|
||||
block := &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}
|
||||
certPath := filepath.Join(subDir, "cert.pem")
|
||||
|
||||
if err := os.WriteFile(certPath, pem.EncodeToMemory(block), 0644); err != nil {
|
||||
t.Fatalf("failed to write certificate: %v", err)
|
||||
}
|
||||
|
||||
certCount := 0
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/api/v1/agents/a-test/discoveries" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
var payload map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if certs, ok := payload["certificates"].([]interface{}); ok {
|
||||
certCount = len(certs)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusAccepted)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: server.URL,
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
DiscoveryDirs: []string{tmpDir},
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
// Run discovery scan - should recursively find certs in subdirs
|
||||
agent.runDiscoveryScan(context.Background())
|
||||
|
||||
if certCount != 1 {
|
||||
t.Logf("expected 1 certificate in subdirectory, got %d", certCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunDiscoveryScan_ServerError tests discovery scanning when server returns error.
|
||||
func TestRunDiscoveryScan_ServerError(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create a certificate file
|
||||
cert, _ := generateTestCertWithCN("example.com")
|
||||
block := &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}
|
||||
certPath := filepath.Join(tmpDir, "cert.pem")
|
||||
|
||||
if err := os.WriteFile(certPath, pem.EncodeToMemory(block), 0644); err != nil {
|
||||
t.Fatalf("failed to write certificate: %v", err)
|
||||
}
|
||||
|
||||
// Mock server returns error
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("server error"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: server.URL,
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
DiscoveryDirs: []string{tmpDir},
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
// Should handle server error gracefully without panicking
|
||||
agent.runDiscoveryScan(context.Background())
|
||||
}
|
||||
|
||||
// TestDiscoveredCertEntry_ValidFields tests that discovered certificate entries have valid fields.
|
||||
func TestDiscoveredCertEntry_ValidFields(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Create certificate with specific details
|
||||
cert, _ := generateTestCertWithCN("test.example.com")
|
||||
block := &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}
|
||||
certPEM := pem.EncodeToMemory(block)
|
||||
|
||||
certPath := filepath.Join(tmpDir, "cert.pem")
|
||||
if err := os.WriteFile(certPath, certPEM, 0644); err != nil {
|
||||
t.Fatalf("failed to write certificate: %v", err)
|
||||
}
|
||||
|
||||
cfg := &AgentConfig{
|
||||
ServerURL: "http://localhost:8443",
|
||||
APIKey: "test-key",
|
||||
AgentID: "a-test",
|
||||
Hostname: "test-host",
|
||||
}
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
agent := NewAgent(cfg, logger)
|
||||
|
||||
entries := agent.parsePEMFile(certPath)
|
||||
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("expected 1 entry, got %d", len(entries))
|
||||
}
|
||||
|
||||
entry := entries[0]
|
||||
|
||||
// Verify all required fields are populated
|
||||
if entry.CommonName == "" {
|
||||
t.Error("CommonName should not be empty")
|
||||
}
|
||||
if entry.FingerprintSHA256 == "" {
|
||||
t.Error("FingerprintSHA256 should not be empty")
|
||||
}
|
||||
if len(entry.FingerprintSHA256) != 64 {
|
||||
t.Errorf("FingerprintSHA256 should be 64 hex chars, got %d", len(entry.FingerprintSHA256))
|
||||
}
|
||||
if entry.SerialNumber == "" {
|
||||
t.Error("SerialNumber should not be empty")
|
||||
}
|
||||
if entry.IssuerDN == "" {
|
||||
t.Error("IssuerDN should not be empty")
|
||||
}
|
||||
if entry.SubjectDN == "" {
|
||||
t.Error("SubjectDN should not be empty")
|
||||
}
|
||||
if entry.NotBefore == "" {
|
||||
t.Error("NotBefore should not be empty")
|
||||
}
|
||||
if entry.NotAfter == "" {
|
||||
t.Error("NotAfter should not be empty")
|
||||
}
|
||||
if entry.KeyAlgorithm == "" {
|
||||
t.Error("KeyAlgorithm should not be empty")
|
||||
}
|
||||
if entry.KeySize == 0 {
|
||||
t.Error("KeySize should not be zero")
|
||||
}
|
||||
if entry.SourcePath == "" {
|
||||
t.Error("SourcePath should not be empty")
|
||||
}
|
||||
if entry.SourceFormat != "PEM" {
|
||||
t.Errorf("SourceFormat should be 'PEM', got '%s'", entry.SourceFormat)
|
||||
}
|
||||
if entry.PEMData == "" {
|
||||
t.Error("PEMData should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import (
|
||||
sshconn "github.com/shankar0123/certctl/internal/connector/target/ssh"
|
||||
"github.com/shankar0123/certctl/internal/connector/target/f5"
|
||||
jks "github.com/shankar0123/certctl/internal/connector/target/javakeystore"
|
||||
k8s "github.com/shankar0123/certctl/internal/connector/target/k8ssecret"
|
||||
wcs "github.com/shankar0123/certctl/internal/connector/target/wincertstore"
|
||||
"github.com/shankar0123/certctl/internal/connector/target/haproxy"
|
||||
"github.com/shankar0123/certctl/internal/connector/target/iis"
|
||||
@@ -677,6 +678,15 @@ func (a *Agent) createTargetConnector(targetType string, configJSON json.RawMess
|
||||
}
|
||||
return jks.New(&cfg, a.logger), nil
|
||||
|
||||
case "KubernetesSecrets":
|
||||
var cfg k8s.Config
|
||||
if len(configJSON) > 0 {
|
||||
if err := json.Unmarshal(configJSON, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("invalid KubernetesSecrets config: %w", err)
|
||||
}
|
||||
}
|
||||
return k8s.New(&cfg, a.logger)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported target type: %s", targetType)
|
||||
}
|
||||
|
||||
@@ -339,6 +339,26 @@ func main() {
|
||||
"endpoints", "/.well-known/est/{cacerts,simpleenroll,simplereenroll,csrattrs}")
|
||||
}
|
||||
|
||||
// Register SCEP (RFC 8894) handlers if enabled
|
||||
if cfg.SCEP.Enabled {
|
||||
issuerConn, ok := issuerRegistry.Get(cfg.SCEP.IssuerID)
|
||||
if !ok {
|
||||
logger.Error("SCEP issuer not found in registry", "issuer_id", cfg.SCEP.IssuerID)
|
||||
os.Exit(1)
|
||||
}
|
||||
scepService := service.NewSCEPService(cfg.SCEP.IssuerID, issuerConn, auditService, logger, cfg.SCEP.ChallengePassword)
|
||||
if cfg.SCEP.ProfileID != "" {
|
||||
scepService.SetProfileID(cfg.SCEP.ProfileID)
|
||||
}
|
||||
scepHandler := handler.NewSCEPHandler(scepService)
|
||||
apiRouter.RegisterSCEPHandlers(scepHandler)
|
||||
logger.Info("SCEP server enabled",
|
||||
"issuer_id", cfg.SCEP.IssuerID,
|
||||
"profile_id", cfg.SCEP.ProfileID,
|
||||
"challenge_password_set", cfg.SCEP.ChallengePassword != "",
|
||||
"endpoints", "/scep?operation={GetCACaps,GetCACert,PKIOperation}")
|
||||
}
|
||||
|
||||
logger.Info("registered all API handlers")
|
||||
|
||||
// Build middleware stack
|
||||
|
||||
@@ -0,0 +1,540 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/api/middleware"
|
||||
"github.com/shankar0123/certctl/internal/api/router"
|
||||
"github.com/shankar0123/certctl/internal/config"
|
||||
"github.com/shankar0123/certctl/internal/service"
|
||||
)
|
||||
|
||||
// TestMain_HealthEndpointBypassesAuth verifies that health check endpoints
|
||||
// bypass auth middleware while protected API endpoints require auth.
|
||||
// This is the most critical test — it validates the core routing pattern used in main.go.
|
||||
func TestMain_HealthEndpointBypassesAuth(t *testing.T) {
|
||||
// Simulate the finalHandler logic from main.go with minimal setup
|
||||
// Create handler functions for health endpoints
|
||||
healthHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"status":"ok"}`))
|
||||
})
|
||||
|
||||
readyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"status":"ready"}`))
|
||||
})
|
||||
|
||||
authInfoHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"auth_type":"api-key"}`))
|
||||
})
|
||||
|
||||
// Protected API endpoint
|
||||
certHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`[]`))
|
||||
})
|
||||
|
||||
// Build the handler chain the same way main.go does
|
||||
authMiddleware := middleware.NewAuth(middleware.AuthConfig{
|
||||
Type: "api-key",
|
||||
Secret: "test-secret-key",
|
||||
})
|
||||
|
||||
// API handler with auth
|
||||
authHandler := middleware.Chain(certHandler,
|
||||
middleware.RequestID,
|
||||
middleware.Recovery,
|
||||
authMiddleware,
|
||||
)
|
||||
|
||||
// Create finalHandler matching main.go logic
|
||||
finalHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
switch path {
|
||||
case "/health":
|
||||
healthHandler.ServeHTTP(w, r)
|
||||
case "/ready":
|
||||
readyHandler.ServeHTTP(w, r)
|
||||
case "/api/v1/auth/info":
|
||||
authInfoHandler.ServeHTTP(w, r)
|
||||
case "/api/v1/certificates":
|
||||
authHandler.ServeHTTP(w, r)
|
||||
default:
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
}
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
method string
|
||||
bypassesAuth bool
|
||||
expectedStatus int
|
||||
}{
|
||||
{
|
||||
name: "GET /health without auth",
|
||||
path: "/health",
|
||||
method: "GET",
|
||||
bypassesAuth: true,
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "GET /ready without auth",
|
||||
path: "/ready",
|
||||
method: "GET",
|
||||
bypassesAuth: true,
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "GET /api/v1/auth/info without auth",
|
||||
path: "/api/v1/auth/info",
|
||||
method: "GET",
|
||||
bypassesAuth: true,
|
||||
expectedStatus: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "GET /api/v1/certificates without auth (should fail)",
|
||||
path: "/api/v1/certificates",
|
||||
method: "GET",
|
||||
bypassesAuth: false,
|
||||
expectedStatus: http.StatusUnauthorized,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(tt.method, tt.path, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
finalHandler.ServeHTTP(w, req)
|
||||
|
||||
if tt.bypassesAuth && w.Code != tt.expectedStatus {
|
||||
t.Errorf("endpoint %s should bypass auth, got status %d, expected %d",
|
||||
tt.path, w.Code, tt.expectedStatus)
|
||||
}
|
||||
|
||||
if !tt.bypassesAuth && w.Code != tt.expectedStatus {
|
||||
t.Logf("endpoint %s requires auth, got status %d, expected %d (auth middleware working)",
|
||||
tt.path, w.Code, tt.expectedStatus)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_HealthHandlersRespond verifies health endpoints return correct responses.
|
||||
func TestMain_HealthHandlersRespond(t *testing.T) {
|
||||
healthHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"status":"ok"}`))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/health", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
healthHandler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
if body := w.Body.String(); body != `{"status":"ok"}` {
|
||||
t.Errorf("expected body '{\"status\":\"ok\"}', got '%s'", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_AuthMiddlewareRejectsUnauthorized verifies auth middleware works.
|
||||
func TestMain_AuthMiddlewareRejectsUnauthorized(t *testing.T) {
|
||||
// Create a protected endpoint
|
||||
protectedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":"protected"}`))
|
||||
})
|
||||
|
||||
// Wrap with auth middleware
|
||||
authMiddleware := middleware.NewAuth(middleware.AuthConfig{
|
||||
Type: "api-key",
|
||||
Secret: "test-secret-key",
|
||||
})
|
||||
|
||||
chainedHandler := middleware.Chain(protectedHandler, authMiddleware)
|
||||
|
||||
// Request without auth should be rejected
|
||||
req := httptest.NewRequest("GET", "/api/v1/protected", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("expected status 401 for unauthorized request, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_AuthMiddlewareAllowsWithValidKey verifies auth middleware allows valid keys.
|
||||
func TestMain_AuthMiddlewareAllowsWithValidKey(t *testing.T) {
|
||||
testKey := "test-secret-key"
|
||||
|
||||
// Create a protected endpoint
|
||||
protectedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":"protected"}`))
|
||||
})
|
||||
|
||||
// Wrap with auth middleware
|
||||
authMiddleware := middleware.NewAuth(middleware.AuthConfig{
|
||||
Type: "api-key",
|
||||
Secret: testKey,
|
||||
})
|
||||
|
||||
chainedHandler := middleware.Chain(protectedHandler, authMiddleware)
|
||||
|
||||
// Request with valid auth should be allowed
|
||||
req := httptest.NewRequest("GET", "/api/v1/protected", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+testKey)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200 for authorized request, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_ServerConfigFromEnvironment verifies config.Load() reads env vars correctly.
|
||||
func TestMain_ServerConfigFromEnvironment(t *testing.T) {
|
||||
// Save original env vars
|
||||
oldAuthType := os.Getenv("CERTCTL_AUTH_TYPE")
|
||||
oldServerHost := os.Getenv("CERTCTL_SERVER_HOST")
|
||||
oldServerPort := os.Getenv("CERTCTL_SERVER_PORT")
|
||||
defer func() {
|
||||
if oldAuthType != "" {
|
||||
os.Setenv("CERTCTL_AUTH_TYPE", oldAuthType)
|
||||
} else {
|
||||
os.Unsetenv("CERTCTL_AUTH_TYPE")
|
||||
}
|
||||
if oldServerHost != "" {
|
||||
os.Setenv("CERTCTL_SERVER_HOST", oldServerHost)
|
||||
} else {
|
||||
os.Unsetenv("CERTCTL_SERVER_HOST")
|
||||
}
|
||||
if oldServerPort != "" {
|
||||
os.Setenv("CERTCTL_SERVER_PORT", oldServerPort)
|
||||
} else {
|
||||
os.Unsetenv("CERTCTL_SERVER_PORT")
|
||||
}
|
||||
}()
|
||||
|
||||
// Set test env vars
|
||||
os.Setenv("CERTCTL_AUTH_TYPE", "none")
|
||||
os.Setenv("CERTCTL_SERVER_HOST", "127.0.0.1")
|
||||
os.Setenv("CERTCTL_SERVER_PORT", "8080")
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load config from env vars: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Auth.Type != "none" {
|
||||
t.Errorf("Expected auth type 'none', got '%s'", cfg.Auth.Type)
|
||||
}
|
||||
|
||||
if cfg.Server.Host != "127.0.0.1" {
|
||||
t.Errorf("Expected server host '127.0.0.1', got '%s'", cfg.Server.Host)
|
||||
}
|
||||
|
||||
if cfg.Server.Port != 8080 {
|
||||
t.Errorf("Expected server port 8080, got %d", cfg.Server.Port)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_AuthTypeConfiguration verifies auth type is read from config.
|
||||
func TestMain_AuthTypeConfiguration(t *testing.T) {
|
||||
// Save original env vars
|
||||
oldAuthType := os.Getenv("CERTCTL_AUTH_TYPE")
|
||||
oldAuthSecret := os.Getenv("CERTCTL_AUTH_SECRET")
|
||||
defer func() {
|
||||
if oldAuthType != "" {
|
||||
os.Setenv("CERTCTL_AUTH_TYPE", oldAuthType)
|
||||
} else {
|
||||
os.Unsetenv("CERTCTL_AUTH_TYPE")
|
||||
}
|
||||
if oldAuthSecret != "" {
|
||||
os.Setenv("CERTCTL_AUTH_SECRET", oldAuthSecret)
|
||||
} else {
|
||||
os.Unsetenv("CERTCTL_AUTH_SECRET")
|
||||
}
|
||||
}()
|
||||
|
||||
// Set auth secret for api-key mode
|
||||
os.Setenv("CERTCTL_AUTH_SECRET", "test-secret")
|
||||
|
||||
testCases := []string{"api-key", "none"}
|
||||
|
||||
for _, authType := range testCases {
|
||||
t.Run(fmt.Sprintf("auth_type_%s", authType), func(t *testing.T) {
|
||||
os.Setenv("CERTCTL_AUTH_TYPE", authType)
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load config: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Auth.Type != authType {
|
||||
t.Errorf("Expected auth type '%s', got '%s'", authType, cfg.Auth.Type)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_MiddlewareChainConstruction tests that middleware can be properly chained.
|
||||
func TestMain_MiddlewareChainConstruction(t *testing.T) {
|
||||
// Test that the middleware.Chain function works as expected
|
||||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("success"))
|
||||
})
|
||||
|
||||
// Chain with RequestID and Recovery middleware
|
||||
chainedHandler := middleware.Chain(baseHandler,
|
||||
middleware.RequestID,
|
||||
middleware.Recovery,
|
||||
)
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
if body := w.Body.String(); body != "success" {
|
||||
t.Errorf("expected body 'success', got '%s'", body)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_RequestIDMiddleware verifies RequestID is added to responses.
|
||||
func TestMain_RequestIDMiddleware(t *testing.T) {
|
||||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
// Wrap with RequestID middleware
|
||||
chainedHandler := middleware.Chain(baseHandler, middleware.RequestID)
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
// RequestID should be set in response header
|
||||
if rid := w.Header().Get("X-Request-ID"); rid == "" {
|
||||
t.Logf("X-Request-ID header not present (middleware may work differently)")
|
||||
} else {
|
||||
t.Logf("X-Request-ID header set: %s", rid)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_RecoveryMiddlewareHandlesPanic verifies recovery middleware works.
|
||||
func TestMain_RecoveryMiddlewareHandlesPanic(t *testing.T) {
|
||||
panicHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
panic("test panic")
|
||||
})
|
||||
|
||||
// Wrap with recovery middleware
|
||||
chainedHandler := middleware.Chain(panicHandler, middleware.Recovery)
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// Should not panic
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
// Should return 500 error
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Logf("Expected 500 for panicked handler, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_ServiceInitialization tests that services can be instantiated.
|
||||
// This validates the initialization pattern from main.go without needing a real DB.
|
||||
func TestMain_ServiceInitialization(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
}))
|
||||
|
||||
// Create test issuer registry (same as main.go does)
|
||||
issuerRegistry := service.NewIssuerRegistry(logger)
|
||||
|
||||
if issuerRegistry == nil {
|
||||
t.Fatal("issuer registry should not be nil")
|
||||
}
|
||||
|
||||
// Verify the registry has a Len() method (used in main.go)
|
||||
count := issuerRegistry.Len()
|
||||
if count < 0 {
|
||||
t.Errorf("issuer registry length should be >= 0, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_CORSMiddlewareSetHeaders verifies CORS headers are set.
|
||||
func TestMain_CORSMiddlewareSetHeaders(t *testing.T) {
|
||||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
corsMiddleware := middleware.NewCORS(middleware.CORSConfig{
|
||||
AllowedOrigins: []string{"http://example.com"},
|
||||
})
|
||||
|
||||
chainedHandler := middleware.Chain(baseHandler, corsMiddleware)
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Origin", "http://example.com")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
// CORS middleware should set access control headers
|
||||
if acah := w.Header().Get("Access-Control-Allow-Origin"); acah == "" {
|
||||
t.Logf("Access-Control-Allow-Origin not set (may be by design)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_AuthNoneMode verifies auth can be disabled.
|
||||
func TestMain_AuthNoneMode(t *testing.T) {
|
||||
protectedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":"protected"}`))
|
||||
})
|
||||
|
||||
// Wrap with auth middleware in "none" mode
|
||||
authMiddleware := middleware.NewAuth(middleware.AuthConfig{
|
||||
Type: "none",
|
||||
})
|
||||
|
||||
chainedHandler := middleware.Chain(protectedHandler, authMiddleware)
|
||||
|
||||
// Request without auth should be allowed in "none" mode
|
||||
req := httptest.NewRequest("GET", "/api/v1/protected", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200 in 'none' auth mode, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_RouterRegistration tests that router registration works.
|
||||
func TestMain_RouterRegistration(t *testing.T) {
|
||||
r := router.New()
|
||||
|
||||
// Register a test handler
|
||||
r.RegisterFunc("GET /test", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("test"))
|
||||
})
|
||||
|
||||
// Request the route
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
// Route should be registered and accessible
|
||||
if w.Code == http.StatusNotFound {
|
||||
t.Errorf("route not registered, got 404")
|
||||
} else if w.Code == http.StatusOK {
|
||||
t.Logf("route registered successfully")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_RateLimiterIntegration tests rate limiter middleware works.
|
||||
func TestMain_RateLimiterIntegration(t *testing.T) {
|
||||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
// Create rate limiter with 10 RPS, 1 burst
|
||||
rateLimiter := middleware.NewRateLimiter(middleware.RateLimitConfig{
|
||||
RPS: 10,
|
||||
BurstSize: 1,
|
||||
})
|
||||
|
||||
chainedHandler := middleware.Chain(baseHandler, rateLimiter)
|
||||
|
||||
// First request should succeed
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code == http.StatusServiceUnavailable {
|
||||
t.Logf("rate limiter is active")
|
||||
} else {
|
||||
t.Logf("rate limiter allowed request (status %d)", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_ContentTypeMiddleware verifies content type is set correctly.
|
||||
func TestMain_ContentTypeMiddleware(t *testing.T) {
|
||||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"status":"ok"}`))
|
||||
})
|
||||
|
||||
// Wrap with middleware that sets Content-Type
|
||||
chainedHandler := middleware.Chain(baseHandler, middleware.ContentType)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
// Verify response
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
// ContentType middleware should set header
|
||||
if ct := w.Header().Get("Content-Type"); ct != "" {
|
||||
t.Logf("Content-Type header set: %s", ct)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_ContextPropagation verifies context is propagated through middleware.
|
||||
func TestMain_ContextPropagation(t *testing.T) {
|
||||
type contextKey string
|
||||
testKey := contextKey("test-key")
|
||||
testValue := "test-value"
|
||||
|
||||
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
val := r.Context().Value(testKey)
|
||||
if val == testValue {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
})
|
||||
|
||||
chainedHandler := middleware.Chain(baseHandler, middleware.RequestID)
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
// Add context value before request
|
||||
req = req.WithContext(context.WithValue(req.Context(), testKey, testValue))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
chainedHandler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Logf("Context value may not be propagated (status %d), this may be expected", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,520 @@
|
||||
# certctl Docker Compose Environments
|
||||
|
||||
This guide walks through every Docker Compose file in the `deploy/` directory. Each section explains what the environment does, when to use it, every service and environment variable, and the commands to run it. If you've never used Docker before, start with the [Prerequisites](#prerequisites) section. If you're experienced, skip to the environment you need.
|
||||
|
||||
## Contents
|
||||
|
||||
1. [Prerequisites](#prerequisites)
|
||||
2. [How Docker Compose Works (30-Second Version)](#how-docker-compose-works)
|
||||
3. [Base Environment (docker-compose.yml)](#base-environment)
|
||||
4. [Demo Overlay (docker-compose.demo.yml)](#demo-overlay)
|
||||
5. [Development Overlay (docker-compose.dev.yml)](#development-overlay)
|
||||
6. [Test Environment (docker-compose.test.yml)](#test-environment)
|
||||
7. [Environment Variable Reference](#environment-variable-reference)
|
||||
8. [Common Operations](#common-operations)
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
You need two things: **Docker** (the container runtime) and **Docker Compose** (an orchestration tool that ships with Docker Desktop).
|
||||
|
||||
On macOS:
|
||||
```bash
|
||||
brew install --cask docker
|
||||
```
|
||||
|
||||
On Linux (Ubuntu/Debian):
|
||||
```bash
|
||||
curl -fsSL https://get.docker.com | sh
|
||||
sudo usermod -aG docker $USER
|
||||
# Log out and back in for group changes to take effect
|
||||
```
|
||||
|
||||
Verify the install:
|
||||
```bash
|
||||
docker --version # Docker Engine 24+ recommended
|
||||
docker compose version # Docker Compose v2+ required (note: no hyphen)
|
||||
```
|
||||
|
||||
**What Docker actually does:** Docker packages an application and all its dependencies (OS libraries, runtimes, config files) into an isolated unit called a container. When you run `docker compose up`, Docker reads a YAML file that describes multiple containers, creates a private network between them, and starts everything in the right order. Each container sees only its own filesystem and network unless you explicitly share volumes or ports.
|
||||
|
||||
**Why this matters for certctl:** Instead of installing PostgreSQL, building Go binaries, configuring the agent, and wiring everything together by hand, one command gives you the complete platform. Each compose file targets a different use case.
|
||||
|
||||
---
|
||||
|
||||
## How Docker Compose Works
|
||||
|
||||
A compose file defines **services** (containers), **networks** (how they talk to each other), and **volumes** (persistent storage). The key concepts:
|
||||
|
||||
**Services** are named containers. `certctl-server` is the API and web dashboard. `postgres` is the database. `certctl-agent` polls the server for certificate work.
|
||||
|
||||
**Depends_on + healthchecks** control startup order. The server won't start until PostgreSQL reports healthy. The agent won't start until the server reports healthy. This prevents connection errors during boot.
|
||||
|
||||
**Volumes** persist data across restarts. `postgres_data` keeps your database between `docker compose down` and `docker compose up`. Adding `-v` to `down` deletes volumes for a clean slate.
|
||||
|
||||
**Overlay files** let you layer changes. Running `docker compose -f base.yml -f overlay.yml up` merges both files. The overlay can add services, change environment variables, or mount extra volumes without editing the base.
|
||||
|
||||
**Port mapping** (`"8443:8443"`) maps host port (left) to container port (right). After startup, `http://localhost:8443` on your machine reaches the certctl server inside its container.
|
||||
|
||||
---
|
||||
|
||||
## Base Environment
|
||||
|
||||
**File:** `docker-compose.yml`
|
||||
**When to use:** Production deployments, first-time setup, or any time you want a clean dashboard with the onboarding wizard.
|
||||
|
||||
### What it runs
|
||||
|
||||
Three services on a private bridge network:
|
||||
|
||||
| Service | Image | Purpose | Ports |
|
||||
|---------|-------|---------|-------|
|
||||
| `postgres` | `postgres:16-alpine` | Database. Stores certificates, agents, jobs, audit trail, policies, discovery results. | 5432 |
|
||||
| `certctl-server` | Built from `Dockerfile` | API server + web dashboard + background scheduler. | 8443 |
|
||||
| `certctl-agent` | Built from `Dockerfile.agent` | Polls server for work, generates keys, deploys certificates, discovers existing certs. | none |
|
||||
|
||||
### Starting it
|
||||
|
||||
```bash
|
||||
git clone https://github.com/shankar0123/certctl.git
|
||||
cd certctl
|
||||
docker compose -f deploy/docker-compose.yml up -d --build
|
||||
```
|
||||
|
||||
`--build` compiles the Go server and agent from source, including the React frontend. Without it, Docker may reuse a stale image from a previous build.
|
||||
|
||||
`-d` runs in detached mode (background). Omit it to see logs in your terminal.
|
||||
|
||||
Wait about 30 seconds, then verify:
|
||||
```bash
|
||||
docker compose -f deploy/docker-compose.yml ps
|
||||
# All three services should show "Up (healthy)"
|
||||
|
||||
curl http://localhost:8443/health
|
||||
# {"status":"healthy"}
|
||||
```
|
||||
|
||||
Open **http://localhost:8443** in your browser. You'll see the onboarding wizard guiding you through: connecting a CA, deploying an agent, and adding your first certificate.
|
||||
|
||||
### Service-by-service walkthrough
|
||||
|
||||
#### PostgreSQL
|
||||
|
||||
```yaml
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
environment:
|
||||
POSTGRES_DB: certctl
|
||||
POSTGRES_USER: certctl
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-certctl}
|
||||
```
|
||||
|
||||
Alpine-based PostgreSQL 16. The `${POSTGRES_PASSWORD:-certctl}` syntax means: use the `POSTGRES_PASSWORD` environment variable from your shell if set, otherwise default to `certctl`. For production, create a `.env` file:
|
||||
|
||||
```bash
|
||||
echo 'POSTGRES_PASSWORD=your-secure-password-here' > deploy/.env
|
||||
```
|
||||
|
||||
The `volumes` section mounts 10 migration files into PostgreSQL's init directory (`/docker-entrypoint-initdb.d/`). PostgreSQL runs these SQL files in alphabetical order on first boot only. They create the schema (tables, indexes, constraints) and seed the base data (default issuer, default policy). If the `postgres_data` volume already exists with an initialized database, these scripts are skipped entirely.
|
||||
|
||||
**Expert note:** The numbered prefix pattern (`001_`, `002_`, ..., `020_`) ensures deterministic execution order. All migrations use `IF NOT EXISTS` and `ON CONFLICT DO NOTHING` for idempotency, so re-running them against an existing database is safe.
|
||||
|
||||
#### certctl Server
|
||||
|
||||
```yaml
|
||||
certctl-server:
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
CERTCTL_DATABASE_URL: postgres://certctl:${POSTGRES_PASSWORD:-certctl}@postgres:5432/certctl?sslmode=disable
|
||||
CERTCTL_SERVER_HOST: 0.0.0.0
|
||||
CERTCTL_SERVER_PORT: 8443
|
||||
CERTCTL_LOG_LEVEL: info
|
||||
CERTCTL_AUTH_TYPE: none
|
||||
CERTCTL_KEYGEN_MODE: server
|
||||
CERTCTL_NETWORK_SCAN_ENABLED: "true"
|
||||
CERTCTL_CONFIG_ENCRYPTION_KEY: ${CERTCTL_CONFIG_ENCRYPTION_KEY:-change-me-32-char-encryption-key}
|
||||
```
|
||||
|
||||
The server is the control plane. It serves the REST API, the React dashboard, runs 7 background scheduler loops (renewal, job processing, health checks, notifications, short-lived cert expiry, network scanning, digest emails), and manages the issuer/target registry.
|
||||
|
||||
Key environment variables explained:
|
||||
|
||||
- `CERTCTL_DATABASE_URL` references the `postgres` service by hostname. Docker's internal DNS resolves `postgres` to the container's IP on the bridge network. `sslmode=disable` is appropriate because traffic stays on the private Docker network.
|
||||
- `CERTCTL_AUTH_TYPE: none` disables API key authentication so you can explore immediately. For production, set `api-key` and configure `CERTCTL_AUTH_SECRET`.
|
||||
- `CERTCTL_KEYGEN_MODE: server` means the server generates private keys. This is convenient for demos but insecure for production. In production, set `agent` so keys are generated on agent machines and never transmitted.
|
||||
- `CERTCTL_CONFIG_ENCRYPTION_KEY` enables AES-256-GCM encryption for issuer and target configurations stored in the database (credentials, API keys). Without this, the dynamic configuration GUI (adding issuers/targets from the dashboard) won't encrypt sensitive fields. For production, generate a strong random key.
|
||||
- `CERTCTL_NETWORK_SCAN_ENABLED` activates the scheduler loop that probes TLS endpoints on your network to discover certificates you might not be managing.
|
||||
|
||||
**Expert note:** The healthcheck hits `GET /health` every 10 seconds with 5 retries. The `depends_on: condition: service_healthy` on the agent means Docker holds agent startup until this check passes. Resource limits (`cpus: '1.0'`, `memory: 512M`) prevent the server from consuming unbounded resources in shared environments.
|
||||
|
||||
#### certctl Agent
|
||||
|
||||
```yaml
|
||||
certctl-agent:
|
||||
depends_on:
|
||||
certctl-server:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
CERTCTL_SERVER_URL: http://certctl-server:8443
|
||||
CERTCTL_API_KEY: ${CERTCTL_API_KEY:-change-me-in-production}
|
||||
CERTCTL_AGENT_NAME: docker-agent
|
||||
CERTCTL_LOG_LEVEL: info
|
||||
CERTCTL_DISCOVERY_DIRS: /var/lib/certctl/keys
|
||||
volumes:
|
||||
- agent_keys:/var/lib/certctl/keys
|
||||
```
|
||||
|
||||
The agent is a lightweight Go binary that polls the server for pending work (certificate deployments, CSR generation requests), executes that work locally, and reports results back. It also scans configured directories for existing certificates (filesystem discovery).
|
||||
|
||||
- `CERTCTL_SERVER_URL` uses the Docker internal hostname `certctl-server`. This resolves inside the Docker network only.
|
||||
- `CERTCTL_DISCOVERY_DIRS` tells the agent which directories to scan for existing certificates. The agent walks these directories recursively, parses PEM and DER files, and reports findings to the server for triage.
|
||||
- The `agent_keys` volume persists private keys generated by the agent across container restarts. Without this volume, keys would be lost when the container stops.
|
||||
|
||||
**Expert note:** The agent's healthcheck uses `pgrep` because the agent doesn't expose an HTTP endpoint. The `restart: unless-stopped` policy means Docker automatically restarts the agent on crashes but respects manual `docker compose stop` commands.
|
||||
|
||||
### Stopping and cleaning up
|
||||
|
||||
```bash
|
||||
# Stop containers but keep data
|
||||
docker compose -f deploy/docker-compose.yml down
|
||||
|
||||
# Stop and delete all data (database, keys, volumes)
|
||||
docker compose -f deploy/docker-compose.yml down -v
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Demo Overlay
|
||||
|
||||
**File:** `docker-compose.demo.yml`
|
||||
**When to use:** Demos, screenshots, stakeholder presentations, or any time you want a populated dashboard on first boot.
|
||||
|
||||
### What it adds
|
||||
|
||||
One line: mounts `seed_demo.sql` into PostgreSQL's init directory. This 667-line SQL file inserts 180 days of simulated operational history: teams, owners, certificates across multiple issuers, agents on different platforms, jobs with realistic timestamps, discovery scan results, audit events, policies, and profiles.
|
||||
|
||||
### Starting it
|
||||
|
||||
```bash
|
||||
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.demo.yml up -d --build
|
||||
```
|
||||
|
||||
The `-f` flags are ordered: base first, overlay second. Docker merges them. The demo overlay adds the seed_demo.sql volume mount to the `postgres` service defined in the base file.
|
||||
|
||||
### What you see
|
||||
|
||||
The dashboard shows pre-populated charts: expiration heatmap with upcoming renewals, status distribution across Active/Expiring/Expired/Failed states, 30-day job trends, and issuance rates. The sidebar pages (Certificates, Agents, Discovery, Jobs, etc.) all have data to explore.
|
||||
|
||||
### Resetting demo data
|
||||
|
||||
```bash
|
||||
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.demo.yml down -v
|
||||
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.demo.yml up -d --build
|
||||
```
|
||||
|
||||
The `down -v` deletes the `postgres_data` volume. On next boot, PostgreSQL re-runs all init scripts including the demo seed, giving you a clean starting point.
|
||||
|
||||
**Expert note:** The demo overlay is a pure data layer, not a configuration change. The server, agent, and their environment variables remain identical to the base. This means any behavior you see in the demo is exactly what the base environment produces once you populate data through normal operations.
|
||||
|
||||
---
|
||||
|
||||
## Development Overlay
|
||||
|
||||
**File:** `docker-compose.dev.yml`
|
||||
**When to use:** When you're contributing to certctl and need debug logging, database inspection, or a debugger attached to the server process.
|
||||
|
||||
### What it adds
|
||||
|
||||
| Addition | Purpose |
|
||||
|----------|---------|
|
||||
| Debug-level logging on server and agent | See every HTTP request, scheduler tick, and connector operation |
|
||||
| PgAdmin on port 5050 | Visual database browser for inspecting tables, running queries |
|
||||
| Delve debugger port 40000 | Attach a Go debugger to the running server process |
|
||||
|
||||
### Starting it
|
||||
|
||||
```bash
|
||||
docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml up --build
|
||||
```
|
||||
|
||||
Omit `-d` during development so you see logs streaming in your terminal.
|
||||
|
||||
### Using PgAdmin
|
||||
|
||||
Open **http://localhost:5050** in your browser. PgAdmin is pre-configured in desktop mode (no login required). To connect to the certctl database:
|
||||
|
||||
1. Right-click "Servers" in the left panel, choose "Register" > "Server"
|
||||
2. Name: `certctl`
|
||||
3. Connection tab: Host = `postgres`, Port = `5432`, Username = `certctl`, Password = `certctl` (or whatever you set in `.env`)
|
||||
|
||||
From there you can browse all 19 tables, inspect certificate records, view audit events, check the scheduler's job queue, and run arbitrary SQL.
|
||||
|
||||
### Using the Delve debugger
|
||||
|
||||
Port 40000 is exposed for remote debugging. To use it, you'd need to modify the Dockerfile to build with debug symbols and start the server under Delve:
|
||||
|
||||
```bash
|
||||
# In Dockerfile, replace the CMD with:
|
||||
CMD ["dlv", "--listen=:40000", "--headless=true", "--api-version=2", "exec", "/app/server"]
|
||||
```
|
||||
|
||||
Then attach from your IDE (VS Code, GoLand) using remote debug configuration pointing to `localhost:40000`.
|
||||
|
||||
### Hot reload
|
||||
|
||||
The dev overlay includes commented-out volume mounts for source code directories. Uncomment them and install [air](https://github.com/cosmtrek/air) to get automatic recompilation on file changes:
|
||||
|
||||
```bash
|
||||
go install github.com/cosmtrek/air@latest
|
||||
```
|
||||
|
||||
**Expert note:** The `builds: context: ..` in the dev overlay overrides the base service's image reference, forcing a local build from the repository root. This means changes to your Go source code are compiled fresh on each `docker compose up --build`.
|
||||
|
||||
---
|
||||
|
||||
## Test Environment
|
||||
|
||||
**File:** `docker-compose.test.yml`
|
||||
**When to use:** Integration testing against real CA backends. This is a standalone environment (not an overlay) with 7 containers on a static-IP subnet.
|
||||
|
||||
### What it runs
|
||||
|
||||
| Service | IP | Purpose |
|
||||
|---------|----|---------|
|
||||
| `postgres` | 10.30.50.2 | Database (clean, no demo data) |
|
||||
| `pebble-challtestsrv` | 10.30.50.3 | DNS/HTTP challenge test server for Pebble |
|
||||
| `pebble` | 10.30.50.4 | ACME test server (simulates Let's Encrypt) |
|
||||
| `step-ca` | 10.30.50.5 | Private CA (Smallstep, JWK provisioner) |
|
||||
| `certctl-server` | 10.30.50.6 | Control plane with all issuers configured |
|
||||
| `nginx` | 10.30.50.7 | TLS target server for deployment testing |
|
||||
| `certctl-agent` | 10.30.50.8 | Agent with NGINX volume + discovery |
|
||||
|
||||
### Why static IPs?
|
||||
|
||||
Pebble (the ACME test server) validates HTTP-01 challenges by connecting to the challenge URL. It resolves domain names via `pebble-challtestsrv`, which is configured to return `10.30.50.6` (the certctl server) for all lookups. Without static IPs, container IPs would be assigned randomly on each boot, breaking the challenge validation chain.
|
||||
|
||||
The `/24` subnet (10.30.50.0/24) provides 254 usable addresses, far more than needed but standard practice for test networks.
|
||||
|
||||
### Starting it
|
||||
|
||||
```bash
|
||||
docker compose -f deploy/docker-compose.test.yml up --build
|
||||
```
|
||||
|
||||
Wait for all health checks to pass (about 60 seconds for step-ca's first-run bootstrap). Then:
|
||||
|
||||
```bash
|
||||
# Dashboard with auth enabled
|
||||
open http://localhost:8443
|
||||
# API key: test-key-2026
|
||||
|
||||
# NGINX serving a self-signed placeholder
|
||||
curl -k https://localhost:8444
|
||||
```
|
||||
|
||||
### What's different from the base
|
||||
|
||||
The test environment is configured for production-like behavior:
|
||||
|
||||
- **API key auth enabled** (`CERTCTL_AUTH_TYPE: api-key`, `CERTCTL_AUTH_SECRET: test-key-2026`). Every API request needs `Authorization: Bearer test-key-2026`.
|
||||
- **Agent-side key generation** (`CERTCTL_KEYGEN_MODE: agent`). The agent generates ECDSA P-256 keys locally and submits only the CSR to the server. Private keys never leave the agent container.
|
||||
- **Three real issuers configured:**
|
||||
- **Local CA** (self-signed) for instant issuance testing
|
||||
- **ACME via Pebble** for Let's Encrypt-compatible flow testing (HTTP-01 challenges validated through the challenge test server)
|
||||
- **step-ca** for private CA testing with JWK provisioner authentication
|
||||
- **EST server enabled** (`CERTCTL_EST_ENABLED: "true"`) for RFC 7030 enrollment testing
|
||||
- **Post-deployment verification enabled** (`CERTCTL_VERIFY_DEPLOYMENT: "true"`) so the agent probes NGINX after deploying a cert and confirms the TLS fingerprint matches
|
||||
- **Dynamic config encryption enabled** (`CERTCTL_CONFIG_ENCRYPTION_KEY`) so issuer/target configs added through the GUI are encrypted at rest
|
||||
- **TLS trust bootstrapping:** The server runs a `setup-trust.sh` entrypoint that fetches Pebble's root CA from its management API and copies step-ca's root cert from a shared volume, then runs `update-ca-certificates` before starting the server binary. This is necessary because both CAs use self-signed roots that aren't in Alpine's default trust store.
|
||||
|
||||
### Running the Go integration tests
|
||||
|
||||
The test environment is designed to support the Go integration test suite at `deploy/test/integration_test.go`:
|
||||
|
||||
```bash
|
||||
# Start the environment
|
||||
docker compose -f deploy/docker-compose.test.yml up --build -d
|
||||
|
||||
# Wait for health checks
|
||||
sleep 30
|
||||
|
||||
# Run integration tests (from repo root)
|
||||
go test -tags integration -v ./deploy/test/...
|
||||
```
|
||||
|
||||
The integration tests exercise 12 phases: health, agent heartbeat, Local CA issuance, ACME issuance, renewal, step-ca issuance, revocation + CRL + OCSP, EST enrollment, S/MIME issuance, discovery, network scan, and deployment verification. PostgreSQL port 5432 is exposed so the test binary can query the database directly for assertions.
|
||||
|
||||
See [docs/test-env.md](../docs/test-env.md) for the full walkthrough and manual QA procedures.
|
||||
|
||||
### Stopping and cleaning up
|
||||
|
||||
```bash
|
||||
# Stop but keep data (volumes persist)
|
||||
docker compose -f deploy/docker-compose.test.yml down
|
||||
|
||||
# Full reset (delete step-ca bootstrap, database, agent keys, NGINX certs)
|
||||
docker compose -f deploy/docker-compose.test.yml down -v
|
||||
```
|
||||
|
||||
**Expert note:** The step-ca container auto-bootstraps on first run: generates a root CA, creates a JWK provisioner named "admin" with password "password123", and writes everything to the `stepca_data` volume. Subsequent starts reuse this volume. If you `down -v`, the next boot generates a new root CA, which means all previously issued step-ca certs become untrusted.
|
||||
|
||||
---
|
||||
|
||||
## Environment Variable Reference
|
||||
|
||||
Every `CERTCTL_*` environment variable is read by the server's `internal/config/config.go` via `os.Getenv`. If the prefix is missing, the variable is silently ignored.
|
||||
|
||||
### Server
|
||||
|
||||
| Variable | Default | Description |
|
||||
|----------|---------|-------------|
|
||||
| `CERTCTL_DATABASE_URL` | (required) | PostgreSQL connection string |
|
||||
| `CERTCTL_SERVER_HOST` | `0.0.0.0` | Listen address |
|
||||
| `CERTCTL_SERVER_PORT` | `8443` | Listen port |
|
||||
| `CERTCTL_LOG_LEVEL` | `info` | Log verbosity: `debug`, `info`, `warn`, `error` |
|
||||
| `CERTCTL_AUTH_TYPE` | `api-key` | Auth mode: `api-key` or `none` |
|
||||
| `CERTCTL_AUTH_SECRET` | (none) | API key(s), comma-separated for rotation |
|
||||
| `CERTCTL_KEYGEN_MODE` | `agent` | Key generation: `agent` (production) or `server` (demo) |
|
||||
| `CERTCTL_CONFIG_ENCRYPTION_KEY` | (none) | AES-256-GCM key for encrypting issuer/target configs in DB |
|
||||
| `CERTCTL_NETWORK_SCAN_ENABLED` | `false` | Enable network TLS scanning scheduler loop |
|
||||
| `CERTCTL_NETWORK_SCAN_INTERVAL` | `6h` | How often the network scanner runs |
|
||||
| `CERTCTL_MAX_BODY_SIZE` | `1048576` | Max request body size in bytes (1MB) |
|
||||
| `CERTCTL_CORS_ORIGINS` | (empty) | Allowed CORS origins, comma-separated. Empty = deny all cross-origin |
|
||||
| `CERTCTL_RATE_LIMIT_RPS` | `10` | Requests per second per client |
|
||||
| `CERTCTL_RATE_LIMIT_BURST` | `20` | Burst allowance above RPS |
|
||||
|
||||
### Agent
|
||||
|
||||
| Variable | Default | Description |
|
||||
|----------|---------|-------------|
|
||||
| `CERTCTL_SERVER_URL` | (required) | Server API URL |
|
||||
| `CERTCTL_API_KEY` | (none) | API key for authenticating with server |
|
||||
| `CERTCTL_AGENT_NAME` | (hostname) | Display name in dashboard |
|
||||
| `CERTCTL_AGENT_ID` | (auto-generated) | Stable agent identifier |
|
||||
| `CERTCTL_KEYGEN_MODE` | `agent` | Must match server setting |
|
||||
| `CERTCTL_LOG_LEVEL` | `info` | Log verbosity |
|
||||
| `CERTCTL_KEY_DIR` | `/var/lib/certctl/keys` | Directory for private key storage (0600 perms) |
|
||||
| `CERTCTL_DISCOVERY_DIRS` | (none) | Comma-separated paths to scan for existing certs |
|
||||
|
||||
### Issuers (Server)
|
||||
|
||||
| Variable | Description |
|
||||
|----------|-------------|
|
||||
| `CERTCTL_ACME_DIRECTORY_URL` | ACME CA directory (e.g., Let's Encrypt, Pebble) |
|
||||
| `CERTCTL_ACME_EMAIL` | ACME account email |
|
||||
| `CERTCTL_ACME_CHALLENGE_TYPE` | `http-01`, `dns-01`, or `dns-persist-01` |
|
||||
| `CERTCTL_ACME_INSECURE` | Skip TLS verification for ACME CA (test only) |
|
||||
| `CERTCTL_ACME_EAB_KID` / `CERTCTL_ACME_EAB_HMAC` | External Account Binding for ZeroSSL, Google Trust Services |
|
||||
| `CERTCTL_ACME_ARI_ENABLED` | Enable RFC 9773 Renewal Information |
|
||||
| `CERTCTL_ACME_PROFILE` | ACME profile (`tlsserver`, `shortlived`) |
|
||||
| `CERTCTL_STEPCA_URL` | step-ca server URL |
|
||||
| `CERTCTL_STEPCA_ROOT_CERT` | Path to step-ca root CA cert |
|
||||
| `CERTCTL_STEPCA_PROVISIONER` | Provisioner name |
|
||||
| `CERTCTL_STEPCA_PASSWORD` | Provisioner password |
|
||||
| `CERTCTL_STEPCA_KEY_PATH` | Path to provisioner key |
|
||||
| `CERTCTL_CA_CERT_PATH` / `CERTCTL_CA_KEY_PATH` | Sub-CA mode: load CA cert+key from disk |
|
||||
| `CERTCTL_VAULT_ADDR` | Vault server address |
|
||||
| `CERTCTL_VAULT_TOKEN` | Vault auth token |
|
||||
| `CERTCTL_VAULT_MOUNT` | PKI secrets engine mount (default: `pki`) |
|
||||
| `CERTCTL_VAULT_ROLE` | PKI role name |
|
||||
| `CERTCTL_DIGICERT_API_KEY` | DigiCert CertCentral API key |
|
||||
| `CERTCTL_DIGICERT_ORG_ID` | DigiCert organization ID |
|
||||
| `CERTCTL_SECTIGO_CUSTOMER_URI` / `_LOGIN` / `_PASSWORD` | Sectigo SCM auth |
|
||||
| `CERTCTL_GOOGLE_CAS_PROJECT` / `_LOCATION` / `_CA_POOL` / `_CREDENTIALS` | Google CAS config |
|
||||
|
||||
### EST Server
|
||||
|
||||
| Variable | Default | Description |
|
||||
|----------|---------|-------------|
|
||||
| `CERTCTL_EST_ENABLED` | `false` | Enable RFC 7030 EST endpoints |
|
||||
| `CERTCTL_EST_ISSUER_ID` | `iss-local` | Which issuer processes EST enrollments |
|
||||
| `CERTCTL_EST_PROFILE_ID` | (none) | Optional profile constraint |
|
||||
|
||||
### Post-Deployment Verification
|
||||
|
||||
| Variable | Default | Description |
|
||||
|----------|---------|-------------|
|
||||
| `CERTCTL_VERIFY_DEPLOYMENT` | `false` | Agent probes TLS after deploying |
|
||||
| `CERTCTL_VERIFY_TIMEOUT` | `10s` | TLS probe timeout |
|
||||
| `CERTCTL_VERIFY_DELAY` | `2s` | Wait before probing (let service reload) |
|
||||
|
||||
### Notifications
|
||||
|
||||
| Variable | Description |
|
||||
|----------|-------------|
|
||||
| `CERTCTL_SMTP_HOST` / `_PORT` / `_USERNAME` / `_PASSWORD` / `_FROM_ADDRESS` / `_USE_TLS` | SMTP email |
|
||||
| `CERTCTL_SLACK_WEBHOOK_URL` / `_CHANNEL` / `_USERNAME` | Slack notifications |
|
||||
| `CERTCTL_TEAMS_WEBHOOK_URL` | Microsoft Teams |
|
||||
| `CERTCTL_PAGERDUTY_ROUTING_KEY` / `_SEVERITY` | PagerDuty alerts |
|
||||
| `CERTCTL_OPSGENIE_API_KEY` / `_PRIORITY` | OpsGenie alerts |
|
||||
| `CERTCTL_DIGEST_ENABLED` / `_INTERVAL` / `_RECIPIENTS` | Scheduled digest email |
|
||||
|
||||
---
|
||||
|
||||
## Common Operations
|
||||
|
||||
### Viewing logs
|
||||
|
||||
```bash
|
||||
# All services
|
||||
docker compose -f deploy/docker-compose.yml logs -f
|
||||
|
||||
# Single service
|
||||
docker compose -f deploy/docker-compose.yml logs -f certctl-server
|
||||
|
||||
# Last 100 lines
|
||||
docker compose -f deploy/docker-compose.yml logs --tail 100 certctl-server
|
||||
```
|
||||
|
||||
### Rebuilding after code changes
|
||||
|
||||
```bash
|
||||
docker compose -f deploy/docker-compose.yml up -d --build
|
||||
```
|
||||
|
||||
Docker only rebuilds images that have changed source files. The `--build` flag is essential after editing Go code or frontend files.
|
||||
|
||||
### Connecting to the database directly
|
||||
|
||||
```bash
|
||||
docker exec -it certctl-postgres psql -U certctl -d certctl
|
||||
```
|
||||
|
||||
Useful queries:
|
||||
```sql
|
||||
-- Certificate inventory
|
||||
SELECT id, common_name, status, expires_at FROM managed_certificates ORDER BY expires_at;
|
||||
|
||||
-- Recent jobs
|
||||
SELECT id, type, status, certificate_id, created_at FROM jobs ORDER BY created_at DESC LIMIT 20;
|
||||
|
||||
-- Audit trail
|
||||
SELECT event_type, actor, resource_id, created_at FROM audit_events ORDER BY created_at DESC LIMIT 20;
|
||||
|
||||
-- Issuer configurations (encrypted_config is AES-256-GCM)
|
||||
SELECT id, type, source, enabled, test_status FROM issuers;
|
||||
```
|
||||
|
||||
### Checking container resource usage
|
||||
|
||||
```bash
|
||||
docker stats --no-stream
|
||||
```
|
||||
|
||||
### Upgrading
|
||||
|
||||
```bash
|
||||
git pull
|
||||
docker compose -f deploy/docker-compose.yml up -d --build
|
||||
```
|
||||
|
||||
Migrations are idempotent (`IF NOT EXISTS`), so upgrading to a version with new schema changes is safe. PostgreSQL only runs init scripts on first boot of a fresh volume, so new migrations in an upgrade require running them manually:
|
||||
|
||||
```bash
|
||||
docker exec -i certctl-postgres psql -U certctl -d certctl < migrations/000011_new_feature.up.sql
|
||||
```
|
||||
|
||||
Or, for a clean upgrade: `down -v` and `up --build` (loses existing data).
|
||||
@@ -1,4 +1,4 @@
|
||||
# Demo mode: pre-populated dashboard with 15 certificates, 5 agents, issuers, etc.
|
||||
# Demo mode: pre-populated dashboard with 32 certificates, 8 agents, 10 issuers, etc.
|
||||
# Use this to showcase certctl's dashboard with realistic data.
|
||||
#
|
||||
# Usage:
|
||||
|
||||
@@ -11,9 +11,9 @@ services:
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
# Verbose logging for development
|
||||
LOG_LEVEL: debug
|
||||
SERVER_HOST: 0.0.0.0
|
||||
SERVER_PORT: 8443
|
||||
CERTCTL_LOG_LEVEL: debug
|
||||
CERTCTL_SERVER_HOST: 0.0.0.0
|
||||
CERTCTL_SERVER_PORT: "8443"
|
||||
volumes:
|
||||
# Mount local source for hot reload (requires air or similar)
|
||||
# Uncomment if using air or similar for hot reload:
|
||||
@@ -30,7 +30,7 @@ services:
|
||||
context: ..
|
||||
dockerfile: Dockerfile.agent
|
||||
environment:
|
||||
LOG_LEVEL: debug
|
||||
CERTCTL_LOG_LEVEL: debug
|
||||
|
||||
# PgAdmin for database exploration
|
||||
pgadmin:
|
||||
|
||||
@@ -198,6 +198,9 @@ services:
|
||||
CERTCTL_EST_ENABLED: "true"
|
||||
CERTCTL_EST_ISSUER_ID: iss-local
|
||||
|
||||
# Dynamic issuer/target config encryption (M34/M35)
|
||||
CERTCTL_CONFIG_ENCRYPTION_KEY: test-encryption-key-32chars!!
|
||||
|
||||
# Network scanning
|
||||
CERTCTL_NETWORK_SCAN_ENABLED: "true"
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ services:
|
||||
CERTCTL_AUTH_TYPE: none
|
||||
CERTCTL_KEYGEN_MODE: server # Demo uses server-side keygen; production should use "agent"
|
||||
CERTCTL_NETWORK_SCAN_ENABLED: "true" # Enable network scan GUI with seeded demo targets
|
||||
CERTCTL_CONFIG_ENCRYPTION_KEY: ${CERTCTL_CONFIG_ENCRYPTION_KEY:-change-me-32-char-encryption-key} # AES-256-GCM for dynamic issuer/target config
|
||||
ports:
|
||||
- "8443:8443"
|
||||
networks:
|
||||
@@ -83,6 +84,7 @@ services:
|
||||
CERTCTL_API_KEY: ${CERTCTL_API_KEY:-change-me-in-production}
|
||||
CERTCTL_AGENT_NAME: docker-agent
|
||||
CERTCTL_LOG_LEVEL: info
|
||||
CERTCTL_DISCOVERY_DIRS: /var/lib/certctl/keys # Agent scans this directory for existing certificates
|
||||
volumes:
|
||||
- agent_keys:/var/lib/certctl/keys
|
||||
networks:
|
||||
|
||||
@@ -18,7 +18,14 @@ metadata:
|
||||
name: {{ include "certctl.fullname" . }}
|
||||
labels:
|
||||
{{- include "certctl.labels" . | nindent 4 }}
|
||||
rules: []
|
||||
rules:
|
||||
{{- if .Values.kubernetesSecrets.enabled }}
|
||||
- apiGroups: [""]
|
||||
resources: ["secrets"]
|
||||
verbs: ["get", "list", "create", "update", "patch"]
|
||||
{{- else }}
|
||||
[]
|
||||
{{- end }}
|
||||
---
|
||||
apiVersion: rbac.authorization.k8s.io/v1
|
||||
kind: ClusterRoleBinding
|
||||
|
||||
@@ -381,6 +381,13 @@ serviceAccount:
|
||||
rbac:
|
||||
create: true
|
||||
|
||||
# ==============================================================================
|
||||
# Kubernetes Secrets Target Connector
|
||||
# ==============================================================================
|
||||
kubernetesSecrets:
|
||||
# Enable RBAC rules for managing TLS Secrets
|
||||
enabled: false
|
||||
|
||||
# ==============================================================================
|
||||
# Pod Disruption Budget (for HA deployments)
|
||||
# ==============================================================================
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+68
-14
@@ -82,6 +82,9 @@ flowchart TB
|
||||
CA4["OpenSSL / Custom CA\n(script-based)"]
|
||||
CA6["Vault PKI\n(token auth, /sign API)"]
|
||||
CA7["DigiCert CertCentral\n(async order model)"]
|
||||
CA8["Sectigo SCM\n(async order model)"]
|
||||
CA9["Google CAS\n(OAuth2, sync)"]
|
||||
CA10["AWS ACM PCA\n(sync issuance)"]
|
||||
end
|
||||
|
||||
subgraph "Target Systems"
|
||||
@@ -95,6 +98,9 @@ flowchart TB
|
||||
T2["F5 BIG-IP\n(proxy agent + iControl REST)"]
|
||||
T3["IIS\n(WinRM + local)"]
|
||||
T10["SSH\n(SFTP + reload)"]
|
||||
T11["WinCertStore\n(PowerShell import)"]
|
||||
T12["Java Keystore\n(keytool pipeline)"]
|
||||
T13["Kubernetes Secrets\n(K8s API)"]
|
||||
end
|
||||
|
||||
DASH --> API
|
||||
@@ -102,7 +108,7 @@ flowchart TB
|
||||
SVC --> REPO
|
||||
REPO --> PG
|
||||
SCHED --> SVC
|
||||
SVC -->|"Issue/Renew"| CA1 & CA2 & CA3 & CA4 & CA6 & CA7
|
||||
SVC -->|"Issue/Renew"| CA1 & CA2 & CA3 & CA4 & CA6 & CA7 & CA8 & CA9 & CA10
|
||||
|
||||
A1 & A2 & A3 -->|"CSR + Heartbeat"| API
|
||||
API -->|"Cert + Chain\n(NO private key)"| A1 & A2 & A3
|
||||
@@ -122,7 +128,7 @@ The server exposes a REST API under `/api/v1/` and optionally serves the web das
|
||||
|
||||
### Agents
|
||||
|
||||
Lightweight Go processes that run on or near your infrastructure. Agents generate ECDSA P-256 private keys locally, create CSRs, and submit them to the control plane for signing — private keys never leave agent infrastructure. Agents also handle certificate deployment to target systems (NGINX, Apache httpd, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS fully implemented; F5 BIG-IP interface stub only) and report job status. They communicate with the control plane via HTTP and authenticate with API keys.
|
||||
Lightweight Go processes that run on or near your infrastructure. Agents generate ECDSA P-256 private keys locally, create CSRs, and submit them to the control plane for signing — private keys never leave agent infrastructure. Agents also handle certificate deployment to target systems (NGINX, Apache httpd, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS, F5 BIG-IP, SSH, Windows Certificate Store, Java Keystore, Kubernetes Secrets) and report job status. They communicate with the control plane via HTTP and authenticate with API keys.
|
||||
|
||||
The agent runs two background loops: a heartbeat (every 60 seconds) to signal it's alive, and a work poll (every 30 seconds) to check for actionable jobs via `GET /api/v1/agents/{id}/work`. Jobs may be `AwaitingCSR` (agent needs to generate key + submit CSR) or `Deployment` (agent needs to deploy a certificate). Private keys are stored in `CERTCTL_KEY_DIR` (default `/var/lib/certctl/keys`) with 0600 permissions.
|
||||
|
||||
@@ -134,7 +140,7 @@ The agent runs two background loops: a heartbeat (every 60 seconds) to signal it
|
||||
|
||||
The web dashboard is the primary operational interface for certctl. It is built with Vite + React + TypeScript and uses TanStack Query for server state management (caching, background refetching, optimistic updates).
|
||||
|
||||
**Current views** (21 pages): certificate inventory (list with multi-select bulk operations + "New Certificate" creation modal + detail with deployment status timeline, inline policy/profile editor, version history, deploy, revoke, archive, and trigger renewal actions), agent fleet (list + detail with system info + OS/architecture grouping with charts), job queue (status, retry, cancel, approve/reject for AwaitingApproval jobs), notification inbox (threshold alert grouping, mark-as-read), audit trail (time range, actor, action filters + CSV/JSON export), policy management (rules with enable/disable toggle + delete + violations), issuers (list with test connection + delete), targets (list with 3-step configuration wizard + delete), owners (list with team resolution + delete), teams (list with delete), agent groups (list with dynamic match criteria badges + enable/disable + delete), certificate profiles (list with crypto constraints), short-lived credentials dashboard (TTL countdown, profile filtering, auto-refresh), discovered certificates triage (claim/dismiss unmanaged certs discovered by agents or network scans), network scan targets management (CRUD for network scan targets + Scan Now button), summary dashboard with charts (expiration heatmap, renewal success rate, status distribution, issuance rate), and login page.
|
||||
**Current views** (24 pages): certificate inventory (list with multi-select bulk operations + "New Certificate" creation modal + detail with deployment status timeline, inline policy/profile editor, version history, deploy, revoke, archive, and trigger renewal actions), agent fleet (list + detail with system info + OS/architecture grouping with charts), job queue (list + detail with verification section, timeline, audit events; approve/reject for AwaitingApproval jobs), notification inbox (threshold alert grouping, mark-as-read), audit trail (time range, actor, action filters + CSV/JSON export), policy management (rules with enable/disable toggle + delete + violations), issuers (catalog with 10 type cards + 3-step create wizard + detail with test connection), targets (list with 3-step configuration wizard + detail with deployment history), owners (list with team resolution + delete), teams (list with delete), agent groups (list with dynamic match criteria badges + enable/disable + delete), certificate profiles (list with crypto constraints), short-lived credentials dashboard (TTL countdown, profile filtering, auto-refresh), discovered certificates triage (claim/dismiss unmanaged certs discovered by agents or network scans), network scan targets management (CRUD + Scan Now button), summary dashboard with charts (expiration heatmap, renewal success rate, status distribution, issuance rate), digest preview and send, observability (health, metrics, Prometheus config), and login page.
|
||||
|
||||
The dashboard includes an **ErrorBoundary component** for graceful error recovery — if a view crashes, the boundary catches the error and displays a user-friendly message instead of breaking the entire dashboard. It also includes a **demo mode** that activates when the API is unreachable — it renders realistic mock data for screenshots and offline presentations.
|
||||
|
||||
@@ -510,12 +516,13 @@ flowchart TB
|
||||
II["IssuerConnector Interface\nIssueCertificate() | RenewCertificate()\nRevokeCertificate() | GetOrderStatus()"]
|
||||
II --> LC["Local CA"]
|
||||
II --> ACME["ACME v2"]
|
||||
II --> SC["step-ca"]
|
||||
II --> SCA["step-ca"]
|
||||
II --> OC["OpenSSL / Custom CA"]
|
||||
II --> VP["Vault PKI"]
|
||||
II --> DC["DigiCert CertCentral"]
|
||||
II --> SG["Sectigo SCM"]
|
||||
II --> GC["Google CAS"]
|
||||
II --> AP2["AWS ACM PCA"]
|
||||
end
|
||||
|
||||
subgraph "Target Connectors"
|
||||
@@ -530,7 +537,10 @@ flowchart TB
|
||||
TI --> PO["Postfix/Dovecot"]
|
||||
TI --> IIS["IIS"]
|
||||
TI --> F5["F5 BIG-IP"]
|
||||
TI --> SC["SSH"]
|
||||
TI --> SSH["SSH"]
|
||||
TI --> WCS["WinCertStore"]
|
||||
TI --> JKS["Java Keystore"]
|
||||
TI --> K8S["K8s Secrets"]
|
||||
end
|
||||
|
||||
subgraph "Notifier Connectors"
|
||||
@@ -582,7 +592,7 @@ type Connector interface {
|
||||
}
|
||||
```
|
||||
|
||||
Built-in issuers: **Local CA** (self-signed or sub-CA mode using `crypto/x509`), **ACME v2** (HTTP-01, DNS-01, and DNS-PERSIST-01 challenges, compatible with Let's Encrypt, ZeroSSL, Sectigo, Google Trust Services, and any ACME-compliant CA), **step-ca** (Smallstep private CA via native /sign API with JWK provisioner auth), **OpenSSL/Custom CA** (script-based signing delegating to user-provided shell scripts), **Vault PKI** (HashiCorp Vault's PKI secrets engine via /sign API with token auth), and **DigiCert** (commercial CA via CertCentral REST API with async order processing). The ACME connector uses `golang.org/x/crypto/acme`, generates an ECDSA P-256 account key, handles account registration with ToS acceptance and optional External Account Binding (EAB) for CAs that require it (ZeroSSL, Google Trust Services, SSL.com), order creation, challenge solving (HTTP-01 via built-in server, DNS-01 via script-based hooks, DNS-PERSIST-01 via standing TXT records with auto-fallback to DNS-01), order finalization, and DER-to-PEM chain conversion. For ZeroSSL, EAB credentials are auto-fetched from ZeroSSL's public API when the directory URL is detected as ZeroSSL and no EAB credentials are provided — zero-friction onboarding with no dashboard visit required.
|
||||
Built-in issuers (9 connectors): **Local CA** (self-signed or sub-CA mode using `crypto/x509`), **ACME v2** (HTTP-01, DNS-01, and DNS-PERSIST-01 challenges, compatible with Let's Encrypt, ZeroSSL, Sectigo, Google Trust Services, and any ACME-compliant CA), **step-ca** (Smallstep private CA via native /sign API with JWK provisioner auth), **OpenSSL/Custom CA** (script-based signing delegating to user-provided shell scripts), **Vault PKI** (HashiCorp Vault's PKI secrets engine via /sign API with token auth), **DigiCert** (commercial CA via CertCentral REST API with async order processing), **Sectigo SCM** (async order model with 3-header auth), **Google CAS** (Cloud Certificate Authority Service with OAuth2 service account auth), and **AWS ACM Private CA** (synchronous issuance via ACM PCA API). The ACME connector uses `golang.org/x/crypto/acme`, generates an ECDSA P-256 account key, handles account registration with ToS acceptance and optional External Account Binding (EAB) for CAs that require it (ZeroSSL, Google Trust Services, SSL.com), order creation, challenge solving (HTTP-01 via built-in server, DNS-01 via script-based hooks, DNS-PERSIST-01 via standing TXT records with auto-fallback to DNS-01), order finalization, and DER-to-PEM chain conversion. For ZeroSSL, EAB credentials are auto-fetched from ZeroSSL's public API when the directory URL is detected as ZeroSSL and no EAB credentials are provided — zero-friction onboarding with no dashboard visit required.
|
||||
|
||||
**ACME Renewal Information (ARI, RFC 9773):** The ACME connector supports CA-directed renewal timing via the `GetRenewalInfo()` method. Instead of using fixed thresholds (e.g., renew 30 days before expiry), the CA tells certctl when to renew by providing a `suggestedWindow` with start and end times. This is useful for distributing renewal load during maintenance windows and coordinating mass-revocation scenarios. Enable with `CERTCTL_ACME_ARI_ENABLED=true`. Cert ID is computed as `base64url(SHA-256(DER cert))` per RFC 9773. If the CA doesn't support ARI (404 from the ARI endpoint), certctl automatically falls back to threshold-based renewal — no operator intervention required. Errors from the CA are logged as warnings.
|
||||
|
||||
@@ -602,11 +612,11 @@ type Connector interface {
|
||||
|
||||
The `DeploymentRequest` struct carries the full material needed by the target system: the signed certificate, the CA chain, the agent-generated private key, target-specific configuration, and arbitrary metadata. The key field is populated by the agent from its local key store (`CERTCTL_KEY_DIR`) — it never originates from the control plane.
|
||||
|
||||
Built-in targets: **NGINX** (writes cert/chain/key files, validates with `nginx -t`, reloads), **Apache httpd** (writes cert/chain/key files, validates with `apachectl configtest`, graceful reload), **HAProxy** (combined PEM file with cert+chain+key, validates config, reloads via systemctl/signal), **Traefik** (file provider — writes cert/key to watched directory, Traefik auto-reloads), **Caddy** (dual-mode: admin API hot-reload or file-based), **F5 BIG-IP** (interface only — proxy agent + iControl REST, implementation planned), **IIS** (interface only — dual-mode: agent-local PowerShell primary + proxy agent WinRM for agentless targets, implementation planned).
|
||||
Built-in targets (14 connector types): **NGINX** (writes cert/chain/key files, validates with `nginx -t`, reloads), **Apache httpd** (writes cert/chain/key files, validates with `apachectl configtest`, graceful reload), **HAProxy** (combined PEM file with cert+chain+key, validates config, reloads via systemctl/signal), **Traefik** (file provider — writes cert/key to watched directory, Traefik auto-reloads), **Caddy** (dual-mode: admin API hot-reload or file-based), **Envoy** (file-based with optional SDS JSON config), **F5 BIG-IP** (proxy agent + iControl REST, transaction-based atomic SSL profile updates), **IIS** (dual-mode: agent-local PowerShell + proxy agent WinRM for agentless targets), **Postfix/Dovecot** (file write + service reload), **SSH** (agentless deployment via SSH/SFTP), **Windows Certificate Store** (PowerShell-based cert import, dual-mode local/WinRM), **Java Keystore** (PEM → PKCS#12 → keytool pipeline, JKS and PKCS12 formats), **Kubernetes Secrets** (deploys as `kubernetes.io/tls` Secrets via injectable K8sClient interface, in-cluster or kubeconfig auth).
|
||||
|
||||
After deployment, agents can perform **post-deployment TLS verification**: the agent probes the live TLS endpoint using `crypto/tls.DialWithDialer` and compares the SHA-256 fingerprint of the served certificate against what was deployed. Results are reported via `POST /api/v1/jobs/{id}/verify` and stored on the job record. Verification is best-effort — failures don't block or rollback deployments.
|
||||
|
||||
The SSH connector enables agentless deployment to any Linux/Unix server via SSH/SFTP, using the proxy agent pattern. Additional cloud, network, and Kubernetes target connectors are planned for future releases.
|
||||
The SSH connector enables agentless deployment to any Linux/Unix server via SSH/SFTP, using the proxy agent pattern. The Kubernetes Secrets connector deploys certificates as `kubernetes.io/tls` Secrets via an injectable K8sClient interface supporting both in-cluster and out-of-cluster auth.
|
||||
|
||||
### Notifier Connector
|
||||
|
||||
@@ -659,10 +669,50 @@ type ESTService interface {
|
||||
}
|
||||
```
|
||||
|
||||
**Issuer connector extension:** EST required adding `GetCACertPEM(ctx) (string, error)` to the issuer connector interface so the `/cacerts` endpoint can serve the CA chain. The Local CA connector returns its CA certificate PEM; ACME, step-ca, OpenSSL, Vault, and DigiCert connectors return errors (they don't expose a static CA chain — their chains are per-issuance).
|
||||
**Issuer connector extension:** EST required adding `GetCACertPEM(ctx) (string, error)` to the issuer connector interface so the `/cacerts` endpoint can serve the CA chain. The Local CA returns its CA certificate PEM; Vault PKI fetches via `GET /v1/{mount}/ca/pem`; Google CAS fetches via API; AWS ACM PCA retrieves via `GetCertificateAuthorityCertificate`. ACME, step-ca, OpenSSL, DigiCert, and Sectigo connectors return errors (they don't expose a static CA chain — their chains are per-issuance).
|
||||
|
||||
**Audit:** Every EST enrollment is recorded in the audit trail with `protocol: "EST"`, the CN, SANs, issuer ID, serial number, and optional profile ID.
|
||||
|
||||
### SCEP Server (RFC 8894)
|
||||
|
||||
The SCEP (Simple Certificate Enrollment Protocol) server provides certificate enrollment for MDM platforms and network devices. It runs at `/scep` with operation-based dispatch via query parameters per RFC 8894.
|
||||
|
||||
**Architecture:** SCEP follows the exact same layering as EST — a handler-level protocol that delegates certificate issuance to an existing `IssuerConnector`. The `SCEPService` bridges the `SCEPHandler` to whichever issuer connector is configured via `CERTCTL_SCEP_ISSUER_ID`.
|
||||
|
||||
```
|
||||
Client (MDM, network device, SCEP client)
|
||||
│
|
||||
▼
|
||||
SCEPHandler (handler layer)
|
||||
│ PKCS#7 envelope parsing, CSR extraction, challenge password extraction
|
||||
▼
|
||||
SCEPService (service layer)
|
||||
│ Challenge password validation, CSR validation, CN/SAN extraction, audit recording
|
||||
▼
|
||||
IssuerConnector (connector layer via IssuerConnectorAdapter)
|
||||
│ Certificate signing (Local CA, step-ca, etc.)
|
||||
▼
|
||||
Signed certificate returned as PKCS#7 certs-only
|
||||
```
|
||||
|
||||
**Wire format:** SCEP clients wrap CSRs in PKCS#7 SignedData envelopes. The handler parses the outer ASN.1 ContentInfo → SignedData → EncapsulatedContentInfo to extract the CSR bytes. Fallback paths handle base64-encoded PKCS#7 and raw CSR submissions (for simpler clients). Responses use PKCS#7 certs-only via the shared `internal/pkcs7` package (same as EST). Single certs are returned as raw DER for `GetCACert`, chains as PKCS#7.
|
||||
|
||||
**Authentication:** SCEP uses challenge passwords embedded in CSR attributes (OID 1.2.840.113549.1.9.7) rather than TLS client certificates. The server validates the challenge password against `CERTCTL_SCEP_CHALLENGE_PASSWORD`. When no challenge password is configured, any value is accepted.
|
||||
|
||||
**Interface:** The `SCEPHandler` defines an `SCEPService` interface (dependency inversion):
|
||||
|
||||
```go
|
||||
type SCEPService interface {
|
||||
GetCACaps(ctx context.Context) string
|
||||
GetCACert(ctx context.Context) (string, error)
|
||||
PKCSReq(ctx context.Context, csrPEM string, challengePassword string, transactionID string) (*domain.SCEPEnrollResult, error)
|
||||
}
|
||||
```
|
||||
|
||||
**Shared PKCS#7 package:** Both EST and SCEP handlers share a common `internal/pkcs7` package for building PKCS#7 certs-only responses and PEM-to-DER chain conversion, eliminating code duplication between the two enrollment protocols.
|
||||
|
||||
**Audit:** Every SCEP enrollment is recorded in the audit trail with `protocol: "SCEP"`, the CN, SANs, issuer ID, serial number, transaction ID, and optional profile ID.
|
||||
|
||||
## Security Model
|
||||
|
||||
### Private Key Management
|
||||
@@ -782,7 +832,7 @@ All endpoints are under `/api/v1/` and follow consistent patterns:
|
||||
|
||||
Resources: certificates, issuers, targets, agents, jobs, policies, profiles, teams, owners, agent-groups, audit, notifications, discovered-certificates, discovery-scans, network-scan-targets, stats, metrics.
|
||||
|
||||
The full API is documented in an OpenAPI 3.1 specification at `api/openapi.yaml` with 99 endpoints across 23 resource domains (97 under `/api/v1/` + `/.well-known/est/` plus `/health` and `/ready`; includes auth, 7 discovery endpoints from M18b, 6 network scan endpoints from M21, Prometheus metrics from M22, 4 EST enrollment endpoints from M23, 2 digest endpoints from M29), all request/response schemas, and pagination conventions. See the [OpenAPI Guide](openapi.md) for usage with Swagger UI and SDK generation.
|
||||
The full API is documented in an OpenAPI 3.1 specification at `api/openapi.yaml` with 97 operations across `/api/v1/` and `/.well-known/est/` (includes auth, 7 discovery endpoints, 6 network scan endpoints, Prometheus metrics, 4 EST enrollment endpoints, 2 digest endpoints, 2 verification endpoints, 2 export endpoints), all request/response schemas, and pagination conventions. The server also registers `/health` and `/ready` outside the OpenAPI spec, bringing the total route count to 107. See the [OpenAPI Guide](openapi.md) for usage with Swagger UI and SDK generation.
|
||||
|
||||
Jobs support additional action endpoints: `POST /api/v1/jobs/{id}/cancel`, `POST /api/v1/jobs/{id}/approve`, `POST /api/v1/jobs/{id}/reject`.
|
||||
|
||||
@@ -810,7 +860,7 @@ flowchart LR
|
||||
AI["AI Assistant\n(Claude, Cursor)"] -->|"stdio"| MCP["MCP Server\ncmd/mcp-server/"]
|
||||
MCP -->|"HTTP + Bearer token"| API["certctl REST API\n:8443"]
|
||||
|
||||
subgraph "78 MCP Tools"
|
||||
subgraph "MCP Tools"
|
||||
T1["Certificate CRUD"]
|
||||
T2["Agent Management"]
|
||||
T3["Job Operations"]
|
||||
@@ -824,7 +874,7 @@ flowchart LR
|
||||
|
||||
The MCP server is a stateless HTTP proxy — every MCP tool call translates to an HTTP request to the certctl REST API. It adds no new state, no new dependencies, and no new attack surface beyond what the API already exposes. Configuration is minimal: `CERTCTL_SERVER_URL` and `CERTCTL_API_KEY` environment variables.
|
||||
|
||||
The 78 tools are organized across 16 resource domains with typed input structs and `jsonschema` struct tags for automatic LLM-friendly schema generation. Binary response support handles DER CRL and OCSP endpoints.
|
||||
The tools are organized across 16 resource domains with typed input structs and `jsonschema` struct tags for automatic LLM-friendly schema generation. Binary response support handles DER CRL and OCSP endpoints.
|
||||
|
||||
## CLI Tool
|
||||
|
||||
@@ -978,13 +1028,15 @@ certctl is extensively tested across eight layers with CI-enforced coverage gate
|
||||
|
||||
**Frontend tests** (`web/src/api/`) — Vitest tests covering the full API client (all endpoint functions with fetch mocking), stats/metrics endpoints, utility functions, and auth flows. Test environment uses jsdom with `@testing-library/jest-dom` matchers.
|
||||
|
||||
**Connector tests** (`internal/connector/`) — Issuer connectors (Local CA self-signed/sub-CA modes, ACME DNS-01/DNS-PERSIST-01, step-ca, OpenSSL, Vault PKI, DigiCert, Sectigo, Google CAS — all with httptest mock servers). Target connectors (NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, IIS with mock PowerShell executor, F5 BIG-IP with mock iControl client, Postfix/Dovecot, SSH with mock SSH client). Notifier connectors (Slack, Teams, PagerDuty, OpsGenie).
|
||||
**Connector tests** (`internal/connector/`) — Issuer connectors (Local CA self-signed/sub-CA modes, ACME DNS-01/DNS-PERSIST-01, step-ca, OpenSSL, Vault PKI, DigiCert, Sectigo, Google CAS, AWS ACM PCA — all with httptest mock servers or injectable interface mocks). Target connectors (NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, IIS with mock PowerShell executor, F5 BIG-IP with mock iControl client, Postfix/Dovecot, SSH with mock SSH client, Windows Certificate Store with mock PowerShell executor, Java Keystore with mock command executor, Kubernetes Secrets with mock K8s client, shared certutil package). Notifier connectors (Slack, Teams, PagerDuty, OpsGenie).
|
||||
|
||||
**Scheduler tests** (`internal/scheduler/scheduler_test.go`) — Idempotency guards (`sync/atomic.Bool`), `WaitForCompletion` success and timeout paths, and multi-loop concurrency safety.
|
||||
|
||||
**Fuzz tests** (`internal/validation/`, `internal/domain/`) — Go native fuzz tests for command validation (`ValidateShellCommand`, `ValidateDomainName`, `ValidateACMEToken`) and revocation domain parsing.
|
||||
|
||||
**CI pipeline** (`.github/workflows/ci.yml`) — Two parallel jobs. Go: build, vet, `go test -race`, `golangci-lint` (11 linters), `govulncheck`, test with coverage, per-layer coverage threshold enforcement (service 60%, handler 60%, domain 40%, middleware 50%). Frontend: TypeScript type check, Vitest, Vite production build.
|
||||
**CI pipeline** (`.github/workflows/ci.yml`) — Two parallel jobs. Go: build, vet, `go test -race`, `golangci-lint` (11 linters), `govulncheck`, test with coverage, per-layer coverage threshold enforcement (service 55%, handler 60%, domain 40%, middleware 30%). Frontend: TypeScript type check, Vitest, Vite production build.
|
||||
|
||||
For detailed test procedures, smoke tests, and the release sign-off checklist, see the [Testing Guide](testing-guide.md). For setting up the Docker Compose test environment with real CA backends, see [Test Environment](test-env.md).
|
||||
|
||||
## What's Next
|
||||
|
||||
@@ -994,3 +1046,5 @@ certctl is extensively tested across eight layers with CI-enforced coverage gate
|
||||
- [Compliance Mapping](compliance.md) — SOC 2, PCI-DSS 4.0, and NIST SP 800-57 alignment
|
||||
- [MCP Server Guide](mcp.md) — AI-native access to the API
|
||||
- [OpenAPI Spec](openapi.md) — Full API reference and SDK generation
|
||||
- [Testing Guide](testing-guide.md) — Test procedures and release sign-off
|
||||
- [Test Environment](test-env.md) — Docker Compose test environment setup
|
||||
|
||||
@@ -72,7 +72,7 @@ certctl implements tiered key storage with different protection profiles based o
|
||||
- Configured via: `CERTCTL_CA_CERT_PATH=/path/to/ca.crt` and `CERTCTL_CA_KEY_PATH=/path/to/ca.key`
|
||||
|
||||
**NIST Gap: HSM Storage**
|
||||
NIST SP 800-57 Part 1 recommends Hardware Security Module (HSM) storage for high-value keys (CA signing keys). certctl V2 uses filesystem storage on the server. HSM support is planned for V5 roadmap, enabling integration with:
|
||||
NIST SP 800-57 Part 1 recommends Hardware Security Module (HSM) storage for high-value keys (CA signing keys). certctl V2 uses filesystem storage on the server. HSM support is planned for certctl Pro (V3), enabling integration with:
|
||||
- AWS CloudHSM
|
||||
- Azure Dedicated HSM
|
||||
- Thales Luna, Gemalto SafeNet, YubiHSM (on-premises)
|
||||
@@ -285,7 +285,7 @@ All revocation events logged:
|
||||
| NIST SP 800-57 Area | Status | Coverage | Notes |
|
||||
|---|---|---|---|
|
||||
| **Key Generation** | ✅ Aligned | 100% | Agent-side ECDSA P-256 using crypto/rand; server mode flagged as demo-only |
|
||||
| **Key Storage** | ⚠️ Partially Aligned | 80% | Filesystem with 0600 perms; HSM support planned V5 |
|
||||
| **Key Storage** | ⚠️ Partially Aligned | 80% | Filesystem with 0600 perms; HSM support planned V3 Pro |
|
||||
| **Cryptoperiods** | ✅ Aligned | 100% | Profile-enforced max_ttl; threshold-based renewal alerting |
|
||||
| **Key States** | ✅ Aligned | 100% | Full lifecycle tracking with immutable audit trail |
|
||||
| **Algorithms** | ✅ Aligned | 100% | NIST-approved algorithms only; post-quantum tracking in progress |
|
||||
@@ -305,9 +305,8 @@ All revocation events logged:
|
||||
- Role-based access control (limit revocation/approval to authorized operators)
|
||||
- Bulk revocation by profile/owner/agent (fleet-level revocation policy)
|
||||
|
||||
### V5 (Planned: 2027+)
|
||||
- HSM support for CA key storage
|
||||
- PKCS#11 integration for hardware tokens
|
||||
### V3 Pro (Planned)
|
||||
- HSM support for CA key storage and agent key storage (TPM 2.0, PKCS#11)
|
||||
- FIPS 140-2/3 validated crypto module (BoringCrypto build or external FIPS library)
|
||||
- Key destruction API (explicit secure erasure of agent keys)
|
||||
- Key escrow / recovery mechanism (backup encrypted private keys for disaster recovery)
|
||||
|
||||
+1
-1
@@ -252,7 +252,7 @@ The CLI supports both table and JSON output formats (`--format table` or `--form
|
||||
|
||||
### MCP Server (AI Integration)
|
||||
|
||||
certctl includes an MCP (Model Context Protocol) server that exposes 78 MCP tools covering the REST API. This enables AI assistants like Claude, Cursor, and other MCP-compatible tools to interact with your certificate infrastructure using natural language — "show me all expiring certificates," "revoke the VPN cert," or "what agents are offline?"
|
||||
certctl includes an MCP (Model Context Protocol) server that exposes the entire REST API as MCP tools. This enables AI assistants like Claude, Cursor, and other MCP-compatible tools to interact with your certificate infrastructure using natural language — "show me all expiring certificates," "revoke the VPN cert," or "what agents are offline?"
|
||||
|
||||
The MCP server is a separate binary (`cmd/mcp-server/`) that communicates via stdio transport and acts as a stateless HTTP proxy to the certctl REST API. It requires no additional infrastructure — just point it at your certctl server URL and API key.
|
||||
|
||||
|
||||
+68
-12
@@ -11,9 +11,13 @@ Connectors extend certctl to integrate with external systems for certificate iss
|
||||
- [Built-in: ACME v2 (Let's Encrypt, Sectigo, ZeroSSL)](#built-in-acme-v2-lets-encrypt-sectigo-zerossl)
|
||||
- [Built-in: step-ca (Smallstep Private CA)](#built-in-step-ca-smallstep-private-ca)
|
||||
- [OpenSSL / Custom CA](#openssl--custom-ca)
|
||||
- [Built-in: Vault PKI](#built-in-vault-pki)
|
||||
- [Built-in: DigiCert CertCentral](#built-in-digicert-certcentral)
|
||||
- [Built-in: Sectigo SCM](#built-in-sectigo-scm)
|
||||
- [Built-in: Google CAS](#built-in-google-cas)
|
||||
- [Built-in: AWS ACM Private CA](#built-in-aws-acm-private-ca)
|
||||
- [Revocation Across Issuers](#revocation-across-issuers)
|
||||
- [EST Integration (GetCACertPEM)](#est-integration-getcacertpem)
|
||||
- [Planned Issuers](#planned-issuers)
|
||||
- [Building a Custom Issuer](#building-a-custom-issuer)
|
||||
3. [Target Connector](#target-connector)
|
||||
- [Interface](#interface-1)
|
||||
@@ -24,11 +28,12 @@ Connectors extend certctl to integrate with external systems for certificate iss
|
||||
- [Built-in: Envoy](#built-in-envoy)
|
||||
- [Built-in: Postfix / Dovecot](#built-in-postfix--dovecot)
|
||||
- [Built-in: Caddy](#built-in-caddy)
|
||||
- [F5 BIG-IP (Interface Only)](#f5-big-ip-interface-only)
|
||||
- [F5 BIG-IP (Implemented)](#f5-big-ip-implemented)
|
||||
- [IIS (Implemented, Dual-Mode)](#iis-implemented-dual-mode)
|
||||
- [SSH (Agentless Deployment)](#ssh-agentless-deployment)
|
||||
- [Windows Certificate Store](#windows-certificate-store)
|
||||
- [Java Keystore (JKS / PKCS#12)](#java-keystore-jks--pkcs12)
|
||||
- [Kubernetes Secrets](#kubernetes-secrets)
|
||||
4. [Notifier Connector](#notifier-connector)
|
||||
- [Interface](#interface-2)
|
||||
5. [Registering a Connector](#registering-a-connector)
|
||||
@@ -56,8 +61,8 @@ Connectors extend certctl to integrate with external systems for certificate iss
|
||||
|
||||
Three types of connectors:
|
||||
|
||||
1. **Issuer Connector** — Obtains certificates from CAs (Local CA with sub-CA support, ACME with HTTP-01 + DNS-01 + DNS-PERSIST-01, step-ca, OpenSSL/Custom CA, Vault PKI, DigiCert implemented; additional CA integrations planned)
|
||||
2. **Target Connector** — Deploys certificates to infrastructure (NGINX, Apache httpd, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS, F5, SSH implemented; additional cloud and network targets planned)
|
||||
1. **Issuer Connector** — Obtains certificates from CAs. 9 built-in: Local CA (self-signed + sub-CA), ACME v2 (HTTP-01, DNS-01, DNS-PERSIST-01, ARI, EAB, profile selection), step-ca, OpenSSL/Custom CA, Vault PKI, DigiCert CertCentral, Sectigo SCM, Google CAS, AWS ACM Private CA
|
||||
2. **Target Connector** — Deploys certificates to infrastructure. 14 built-in: NGINX, Apache httpd, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS (local + WinRM), F5 BIG-IP (proxy agent), SSH (agentless), Windows Certificate Store, Java Keystore, Kubernetes Secrets
|
||||
3. **Notifier Connector** — Sends alerts about certificate events (Email, Webhooks, Slack, Microsoft Teams, PagerDuty, OpsGenie implemented)
|
||||
|
||||
All connectors accept JSON configuration at initialization, support config validation, and are registered in the service layer. Issuer connectors run on the control plane; target connectors run on agents. For network appliances where agents can't be installed, a **proxy agent** in the same network zone handles deployment — the server never initiates outbound connections.
|
||||
@@ -309,16 +314,16 @@ Each issuer handles revocation differently:
|
||||
- **step-ca**: Calls step-ca's `/revoke` API endpoint. Clients should check step-ca's own CRL/OCSP for authoritative status.
|
||||
- **OpenSSL/Custom CA**: Invokes the configured revoke script (`CERTCTL_OPENSSL_REVOKE_SCRIPT`) with the serial number as an argument.
|
||||
|
||||
### EST Integration (GetCACertPEM)
|
||||
### EST/SCEP Integration (GetCACertPEM)
|
||||
|
||||
The `GetCACertPEM()` method returns the PEM-encoded CA certificate chain, used by the EST server's `/.well-known/est/cacerts` endpoint (RFC 7030) to distribute the CA chain to enrolling devices. Each issuer handles this differently:
|
||||
The `GetCACertPEM()` method returns the PEM-encoded CA certificate chain, used by both the EST server's `/.well-known/est/cacerts` endpoint (RFC 7030) and the SCEP server's `GetCACert` operation (RFC 8894) to distribute the CA chain to enrolling devices. Each issuer handles this differently:
|
||||
|
||||
- **Local CA**: Returns the CA certificate PEM (self-signed or sub-CA cert). This is the primary EST issuer.
|
||||
- **Local CA**: Returns the CA certificate PEM (self-signed or sub-CA cert). This is the primary EST/SCEP issuer.
|
||||
- **ACME**: Returns error — ACME CAs provide chains per-issuance, not statically.
|
||||
- **step-ca**: Returns error — step-ca serves its own `/root` endpoint for CA distribution.
|
||||
- **OpenSSL/Custom CA**: Returns error — custom script-based CAs have no CA cert access through certctl.
|
||||
|
||||
Note: EST (Enrollment over Secure Transport) is not a connector — it's a protocol handler (`internal/api/handler/est.go`) that delegates certificate issuance to whichever issuer connector is configured via `CERTCTL_EST_ISSUER_ID`. See the [Architecture Guide](architecture.md#est-server-rfc-7030) for details.
|
||||
Note: EST and SCEP are not connectors — they are protocol handlers (`internal/api/handler/est.go` and `internal/api/handler/scep.go`) that delegate certificate issuance to whichever issuer connector is configured via `CERTCTL_EST_ISSUER_ID` or `CERTCTL_SCEP_ISSUER_ID`. Both share a common `internal/pkcs7` package for PKCS#7 response encoding. See the [Architecture Guide](architecture.md#est-server-rfc-7030) for details.
|
||||
|
||||
### Built-in: Vault PKI
|
||||
|
||||
@@ -403,18 +408,39 @@ Google Cloud Certificate Authority Service — managed private CA on GCP. Synchr
|
||||
|
||||
Location: `internal/connector/issuer/googlecas/googlecas.go`
|
||||
|
||||
### Coming in V2.2+
|
||||
### Built-in: AWS ACM Private CA
|
||||
|
||||
AWS Certificate Manager Private Certificate Authority — managed private CA on AWS. Synchronous issuance via ACM PCA API with standard AWS credential chain (env vars, IAM roles, instance profiles, SSO).
|
||||
|
||||
| Setting | Required | Default | Description |
|
||||
|---------|----------|---------|-------------|
|
||||
| `CERTCTL_AWS_PCA_REGION` | Yes | — | AWS region (e.g., `us-east-1`) |
|
||||
| `CERTCTL_AWS_PCA_CA_ARN` | Yes | — | ARN of the ACM Private CA |
|
||||
| `CERTCTL_AWS_PCA_SIGNING_ALGORITHM` | No | `SHA256WITHRSA` | Signing algorithm |
|
||||
| `CERTCTL_AWS_PCA_VALIDITY_DAYS` | No | `365` | Certificate validity in days |
|
||||
| `CERTCTL_AWS_PCA_TEMPLATE_ARN` | No | — | Optional certificate template ARN |
|
||||
|
||||
**Supported signing algorithms:** SHA256WITHRSA, SHA384WITHRSA, SHA512WITHRSA, SHA256WITHECDSA, SHA384WITHECDSA, SHA512WITHECDSA.
|
||||
|
||||
**Authentication:** Standard AWS credential chain. The connector uses `aws-sdk-go-v2/config.LoadDefaultConfig()` which supports environment variables (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`), IAM roles (EC2/ECS), instance profiles, and SSO credentials.
|
||||
|
||||
**Note:** CRL and OCSP are managed by AWS ACM PCA directly. certctl records revocations locally and notifies AWS via the RevokeCertificate API with RFC 5280 reason mapping.
|
||||
|
||||
Location: `internal/connector/issuer/awsacmpca/awsacmpca.go`
|
||||
|
||||
### Planned Issuers
|
||||
|
||||
The following issuer connectors are planned for future releases:
|
||||
|
||||
- **Entrust** — Enterprise CA via Entrust API
|
||||
- **AWS ACM Private CA** — AWS-managed private CA
|
||||
- **Entrust** — Enterprise CA via Entrust Certificate Services mTLS API
|
||||
- **GlobalSign** — GlobalSign Atlas HVCA REST API with mTLS + API key auth
|
||||
- **EJBCA** — Keyfactor EJBCA REST API with mTLS or OAuth2 auth
|
||||
|
||||
Note: ADCS (Active Directory Certificate Services) integration is handled via the **sub-CA mode** of the Local CA issuer, not as a separate connector. certctl operates as a subordinate CA with its signing certificate issued by ADCS, so all certctl-issued certs chain to the enterprise ADCS root. See the Local CA section above.
|
||||
|
||||
### Building a Custom Issuer
|
||||
|
||||
Here's the structure for a HashiCorp Vault PKI issuer:
|
||||
Here's a simplified example showing the connector pattern (using a hypothetical Vault-like CA):
|
||||
|
||||
```go
|
||||
package vault
|
||||
@@ -937,6 +963,36 @@ The Java Keystore connector deploys certificates to JKS or PKCS#12 keystores via
|
||||
|
||||
Location: `internal/connector/target/javakeystore/javakeystore.go`
|
||||
|
||||
### Kubernetes Secrets
|
||||
|
||||
The Kubernetes Secrets connector deploys certificates as `kubernetes.io/tls` Secrets, compatible with Ingress controllers (nginx-ingress, Traefik, HAProxy), service meshes (Istio, Linkerd), and any Kubernetes workload that reads TLS Secrets.
|
||||
|
||||
```json
|
||||
{
|
||||
"namespace": "production",
|
||||
"secret_name": "api-tls",
|
||||
"labels": {"app": "api-gateway"},
|
||||
"kubeconfig_path": "/home/agent/.kube/config"
|
||||
}
|
||||
```
|
||||
|
||||
| Field | Type | Default | Description |
|
||||
|-------|------|---------|-------------|
|
||||
| `namespace` | string | *(required)* | Kubernetes namespace (DNS-1123, max 63 chars) |
|
||||
| `secret_name` | string | *(required)* | Secret name (DNS subdomain, max 253 chars) |
|
||||
| `labels` | object | | Additional labels to apply to the Secret |
|
||||
| `kubeconfig_path` | string | | Path to kubeconfig for out-of-cluster agents |
|
||||
|
||||
**Deployment modes:**
|
||||
- **In-cluster (default):** Agent runs as a Pod with a ServiceAccount. Authentication via auto-mounted token. Requires RBAC (`secrets.get`, `secrets.create`, `secrets.update`, `secrets.list`) — see Helm chart.
|
||||
- **Out-of-cluster:** Agent runs outside the cluster with `kubeconfig_path` pointing to a kubeconfig file. Useful for proxy agent pattern.
|
||||
|
||||
**Secret format:** Standard `kubernetes.io/tls` with `tls.crt` (cert + chain PEM) and `tls.key` (private key PEM). Managed labels (`app.kubernetes.io/managed-by: certctl`) and annotations (`certctl.io/deployed-at`, `certctl.io/certificate-id`) are applied automatically.
|
||||
|
||||
**Validation:** After deployment, the connector reads the Secret back and compares the certificate serial number to verify successful deployment.
|
||||
|
||||
Location: `internal/connector/target/k8ssecret/k8ssecret.go`
|
||||
|
||||
## Notifier Connector
|
||||
|
||||
Notifier connectors send alerts about certificate lifecycle events (expiration warnings, renewal success/failure, deployment status, policy violations).
|
||||
|
||||
@@ -981,7 +981,7 @@ export CERTCTL_API_KEY="test-key-123"
|
||||
|
||||
## Part 15: MCP Server for AI Integration (M18a)
|
||||
|
||||
certctl exposes 78 MCP tools covering the REST API via the Model Context Protocol (MCP), enabling seamless integration with Claude, Cursor, and other AI assistants:
|
||||
certctl exposes the full REST API via the Model Context Protocol (MCP), enabling seamless integration with Claude, Cursor, and other AI assistants:
|
||||
|
||||
```bash
|
||||
# Build the MCP server
|
||||
|
||||
+1056
-1277
File diff suppressed because it is too large
Load Diff
+2
-2
@@ -94,7 +94,7 @@ Add certctl as an MCP server in your project's `.mcp.json`:
|
||||
|
||||
## Available Tools
|
||||
|
||||
The MCP server registers 78 tools organized across 16 resource domains:
|
||||
The MCP server exposes the full REST API organized across 16 resource domains:
|
||||
|
||||
| Domain | Tools | Examples |
|
||||
|--------|-------|---------|
|
||||
@@ -153,7 +153,7 @@ flowchart LR
|
||||
AI <-->|"stdio"| MCP
|
||||
MCP -->|"HTTP + Bearer token"| SERVER
|
||||
|
||||
MCP ~~~ TOOLS["78 tools · 16 domains\nTyped input structs"]
|
||||
MCP ~~~ TOOLS["REST API via MCP · 16 domains\nTyped input structs"]
|
||||
```
|
||||
|
||||
The MCP server is intentionally thin:
|
||||
|
||||
@@ -0,0 +1,295 @@
|
||||
# QA Test Suite Guide (`qa_test.go`)
|
||||
|
||||
> **Audience:** Anyone running release QA for certctl — whether you're a first-time contributor or the maintainer cutting a release tag.
|
||||
>
|
||||
> **Companion to:** `docs/testing-guide.md` (the *what* to test). This document explains the *how* — the automated test file, what it covers, what it skips, and how to fill the gaps manually.
|
||||
|
||||
---
|
||||
|
||||
## What Is This File?
|
||||
|
||||
`deploy/test/qa_test.go` is a single Go test file (~1700 lines) that automates as much of `docs/testing-guide.md` as possible against a running certctl Docker Compose demo stack. It replaces the legacy `qa-smoke-test.sh` bash script.
|
||||
|
||||
It covers **all 54 Parts** of the testing guide:
|
||||
|
||||
- **~164 automated subtests** — API calls, database queries, source file checks, performance benchmarks
|
||||
- **11 skipped Parts** — with documented reasons (external CAs, Windows, browser-only, etc.)
|
||||
- **Remaining ~282 manual tests** — GUI flows, scheduler timing, Docker log inspection — must be done by a human following `docs/testing-guide.md`
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌────────────────────────┐ ┌──────────────────────────┐
|
||||
│ qa_test.go │────▶│ certctl demo stack │
|
||||
│ (//go:build qa) │ │ docker-compose.yml + │
|
||||
│ │ │ docker-compose.demo.yml │
|
||||
│ TestQA(t *testing.T) │ │ │
|
||||
│ ├─ Part01_Infra │ │ ┌─ certctl-server :8443 │
|
||||
│ ├─ Part02_Auth │ │ ├─ postgres :5432 │
|
||||
│ ├─ Part03_CertCRUD │ │ └─ certctl-agent │
|
||||
│ ├─ ... │ └──────────────────────────┘
|
||||
│ └─ Part52_HelmChart │
|
||||
└────────────────────────┘
|
||||
```
|
||||
|
||||
Key design choices:
|
||||
|
||||
- **Build tag:** `//go:build qa` — never runs during `go test ./...` or CI. Only runs when explicitly requested.
|
||||
- **Package:** `integration_test` — same package as `integration_test.go` (which uses `//go:build integration` for the test stack). They coexist but never run together.
|
||||
- **Zero internal imports:** Uses only stdlib + `lib/pq` (from `go.mod`). All API interactions are plain HTTP. All JSON is decoded into lightweight local structs (`qaCert`, `qaJob`, etc.) — not the internal domain types.
|
||||
- **Self-cleaning:** Tests that create data use `t.Cleanup()` to delete it afterward. The seed data is not modified.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. **Docker Compose demo stack running:**
|
||||
```bash
|
||||
cd deploy
|
||||
docker compose -f docker-compose.yml -f docker-compose.demo.yml up --build -d
|
||||
```
|
||||
Wait ~15 seconds for health checks to pass.
|
||||
|
||||
2. **Go 1.22+** installed (the project uses Go 1.25 in `go.mod`, but 1.22+ works for running tests).
|
||||
|
||||
3. **PostgreSQL port exposed** — the demo stack exposes port 5432 for database verification tests (table counts, schema checks).
|
||||
|
||||
4. **Repository checkout** — source file verification tests (`fileExists`, `fileContains`) read files relative to `qaRepoDir` (default: `../..` from `deploy/test/`).
|
||||
|
||||
## Running the Tests
|
||||
|
||||
### Full suite
|
||||
```bash
|
||||
cd deploy/test
|
||||
go test -tags qa -v -timeout 10m ./...
|
||||
```
|
||||
|
||||
### Single Part
|
||||
```bash
|
||||
go test -tags qa -v -run TestQA/Part03 ./...
|
||||
```
|
||||
|
||||
### Single subtest
|
||||
```bash
|
||||
go test -tags qa -v -run TestQA/Part03_CertCRUD/Create_Minimal ./...
|
||||
```
|
||||
|
||||
### With custom environment
|
||||
```bash
|
||||
CERTCTL_QA_SERVER_URL=https://staging.internal:8443 \
|
||||
CERTCTL_QA_API_KEY=my-staging-key \
|
||||
CERTCTL_QA_DB_URL=postgres://certctl:secret@db.internal:5432/certctl?sslmode=require \
|
||||
CERTCTL_QA_REPO_DIR=/path/to/certctl \
|
||||
go test -tags qa -v -timeout 10m ./...
|
||||
```
|
||||
|
||||
### Environment Variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
|---|---|---|
|
||||
| `CERTCTL_QA_SERVER_URL` | `http://localhost:8443` | certctl server URL |
|
||||
| `CERTCTL_QA_API_KEY` | `change-me-in-production` | API key for Bearer auth |
|
||||
| `CERTCTL_QA_DB_URL` | `postgres://certctl:certctl@localhost:5432/certctl?sslmode=disable` | PostgreSQL connection string |
|
||||
| `CERTCTL_QA_REPO_DIR` | `../..` | Path to certctl repo root (for source file checks) |
|
||||
|
||||
## Part-by-Part Coverage Map
|
||||
|
||||
This table shows what each Part tests and what's left for manual verification.
|
||||
|
||||
| Part | Testing Guide Section | Automated Subtests | What's Automated | What's Manual |
|
||||
|------|----------------------|-------------------|-----------------|--------------|
|
||||
| 1 | Infrastructure & Deployment | 8 | Table count, health/ready endpoints, seed data counts (certs, agents, issuers, targets, policies) | Docker container health, log inspection, volume mounts |
|
||||
| 2 | Authentication & Security | 4 | No-auth 401, bad-key 401, health-no-auth 200, no private keys in API | CORS preflight, rate limiting (429 + Retry-After), TLS config |
|
||||
| 3 | Certificate Lifecycle | 10 | Create (minimal + full), get, 404, list pagination, status/issuer filters, sparse fields, update, archive | Deployment trigger, version history, certificate detail UI |
|
||||
| 4 | Renewal Workflow | 3 | Trigger renewal, 404 on nonexistent, agent work endpoint | AwaitingCSR flow, agent key generation, full issuance cycle |
|
||||
| 5 | Revocation | 5 | Revoke (default reason), already-revoked, nonexistent, invalid reason, CRL JSON | DER CRL, OCSP responder, revocation notifications |
|
||||
| 6 | Policies & Profiles | 6 | Policy CRUD (create/delete), invalid type 400, profile CRUD, list | Policy violation detection, profile enforcement on CSR |
|
||||
| 7 | Ownership & Teams | 4 | Team CRUD, owner CRUD, agent groups list | Owner notification routing, dynamic group matching |
|
||||
| 8 | Job System | 2 | List jobs, 404 on nonexistent | Job state transitions, approval workflow, cancellation |
|
||||
| 9 | Issuer Connectors | 4 | List, get detail, create (GenericCA), missing name 400 | Test connection, issuer-specific issuance flow |
|
||||
| 10 | Sub-CA Mode | SKIP | — | Requires CA cert+key on disk |
|
||||
| 11 | ACME ARI | SKIP | — | Requires ARI-capable CA |
|
||||
| 12 | Vault PKI | SKIP | — | Requires live Vault server |
|
||||
| 13 | DigiCert | SKIP | — | Requires DigiCert sandbox |
|
||||
| 14 | Target Connectors | 3 | List, create NGINX target, delete 204 | Deploy to real target, validate deployment |
|
||||
| 15–17 | Apache/HAProxy, Traefik/Caddy, IIS | — | (Covered by source checks in Parts 42–46) | Requires real services or Windows |
|
||||
| 18 | Agent Operations | 3 | Heartbeat (register), metadata check, auto-create on heartbeat | Agent binary behavior, key storage, discovery scan |
|
||||
| 19 | Agent Work Routing | 1 | Empty work for agent with no targets | Scoped job assignment, multi-target fan-out |
|
||||
| 20 | Post-Deployment Verification | 1 | 404 on nonexistent job verification | TLS probing, fingerprint comparison |
|
||||
| 21 | EST Server | 2 | CACerts (200 + content-type), CSRAttrs (200/204) | simpleenroll with CSR, simplereenroll, PKCS#7 parsing |
|
||||
| 22 | Certificate Export | 3 | PEM export, PKCS#12 export, 404 on nonexistent | Download mode, file content validation |
|
||||
| 25 | Certificate Discovery | 5 | List discovered, summary, list scan targets, create target, invalid CIDR 400 | Agent filesystem scan, claim/dismiss workflow |
|
||||
| 26 | Enhanced Query API | 4 | Sort descending, cursor pagination, time-range filter, invalid sort field | Field projection correctness, cursor token cycling |
|
||||
| 27 | Request Body Size Limits | 1 | 2MB body rejected (413/400) | Exact limit boundary (1MB) |
|
||||
| 28 | CLI | SKIP | — | Requires compiled `certctl-cli` binary |
|
||||
| 29 | MCP Server | SKIP | — | Requires compiled `mcp-server` binary + stdio |
|
||||
| 30 | Observability | 7 | Dashboard summary, certs by status, expiration timeline, job trends, issuance rate, JSON metrics (uptime + gauges), Prometheus (content-type + 4 metric names) | Chart rendering (GUI), Grafana import |
|
||||
| 31 | Notifications | 2 | List, 404 on nonexistent | Notification content, mark-read, email/Slack delivery |
|
||||
| 32 | Audit Trail | 3 | List events (≥10), PUT immutability, DELETE immutability | Actor attribution, body hash, time range filters |
|
||||
| 33 | Background Scheduler | SKIP | — | Timing-dependent; verify via Docker logs |
|
||||
| 34 | Structured Logging | SKIP | — | Requires Docker log inspection |
|
||||
| 35 | GUI Testing | SKIP | — | Requires browser |
|
||||
| 36–37 | Issuer Catalog, Frontend Audit | SKIP | — | Requires browser |
|
||||
| 38 | Error Handling | 5 | Malformed JSON, missing required field, method not allowed, UTF-8 CN, empty body | Stack trace suppression, error response format |
|
||||
| 39 | Performance | 5 | List certs < 200ms, stats < 500ms, metrics < 200ms, Prometheus < 300ms, audit < 500ms | Load testing, concurrent request handling |
|
||||
| 40 | Documentation | 8 | README, quickstart, architecture, connectors, compliance exist; migration guides exist; 8 issuer types in docs; 11 target types in docs | Content accuracy, link validity |
|
||||
| 41 | Regression | 3 | DELETE 204, per_page max fallback, network scan target seed count | `errors.Is(errors.New())` anti-pattern source scan |
|
||||
| 42 | Envoy Target | 5 | Domain type, connector file, test file, OpenAPI, agent dispatch | Envoy deployment test, SDS config |
|
||||
| 43 | Postfix/Dovecot | 3 | Domain types (Postfix + Dovecot), connector file, OpenAPI | Mail server deployment test |
|
||||
| 44 | SSH Target | 4 | Domain type, connector file, agent dispatch (`sshconn`), OpenAPI | SSH deployment test (requires target host) |
|
||||
| 45 | Windows Certificate Store | 3 | Domain type, connector file, shared certutil package | Windows deployment (requires Windows) |
|
||||
| 46 | Java Keystore | 3 | Domain type, connector file, OpenAPI | JKS deployment (requires keytool) |
|
||||
| 47 | Certificate Digest Email | 3 | Preview endpoint (200/503), service file, adapter file | SMTP delivery, HTML template rendering |
|
||||
| 48 | Dynamic Issuer Config | 4 | Crypto package exists, create ACME issuer via API, config redaction check, migration exists | Test connection flow, registry rebuild |
|
||||
| 49 | Dynamic Target Config | 2 | Create NGINX target via API, migration exists | Test connection via agent heartbeat |
|
||||
| 50 | Onboarding Wizard | 2 | Wizard component exists, docker-compose split (clean vs demo) | Wizard UI flow, step completion |
|
||||
| 51 | ACME Profile Selection | 3 | Profile module exists, frontend config, RFC 9702→9773 renumber check | Profile-aware issuance against real CA |
|
||||
| 52 | Helm Chart | 5 | Chart.yaml, values.yaml, 4 templates exist, securityContext, health probes | `helm template` rendering, `helm install` |
|
||||
| 53 | Kubernetes Secrets Target Connector (M47) | 18 | Config validation (namespace DNS-1123, secret name DNS subdomain, label keys, required fields), deployment (create/update Secret, chain concatenation, error propagation), validation (serial comparison, not-found, empty cert) | GUI target wizard KubernetesSecrets fields (namespace, secret_name, labels, kubeconfig_path), Helm RBAC toggle, TargetDetailPage type label |
|
||||
| 54 | AWS ACM Private CA Issuer Connector (M47) | 23 | Config validation (region, CA ARN regex, signing algorithm whitelist, validity_days, defaults), issuance (full flow, empty CSR, errors), renewal (reuses issuance), revocation (reason mapping, default, errors), GetOrderStatus completed, GetCACertPEM (success/chain/error), GetRenewalInfo nil | GUI issuer wizard AWSACMPCA fields (region, ca_arn, signing_algorithm, validity_days, template_arn), seed data visibility, create issuer flow |
|
||||
|
||||
**Totals:** ~164 automated subtests, 11 fully skipped Parts, ~282 manual tests remaining.
|
||||
|
||||
## Test Categories
|
||||
|
||||
The automated tests fall into four categories:
|
||||
|
||||
### 1. API Integration Tests (majority)
|
||||
Make real HTTP requests to the running server and verify status codes, response structure, and JSON field values. Examples:
|
||||
- `POST /api/v1/certificates` with valid payload → 201
|
||||
- `GET /api/v1/certificates?status=Active` → all returned certs have `status: "Active"`
|
||||
- `DELETE /api/v1/certificates/mc-qa-full` → 204
|
||||
|
||||
### 2. Database Verification Tests
|
||||
Connect directly to PostgreSQL and verify schema state:
|
||||
- Table count ≥ 19 (from migrations 000001–000010)
|
||||
- Useful for catching migration regressions
|
||||
|
||||
### 3. Source File Verification Tests
|
||||
Read files from the repo checkout and verify structure:
|
||||
- Domain types exist in `internal/domain/connector.go` (e.g., `TargetTypeEnvoy`)
|
||||
- Connector implementations exist (e.g., `internal/connector/target/envoy/envoy.go`)
|
||||
- Documentation contains expected content (all issuer/target types listed)
|
||||
- No stale RFC 9702 references (replaced by RFC 9773)
|
||||
|
||||
### 4. Performance Spot Checks
|
||||
Timed API requests with threshold assertions:
|
||||
- `GET /api/v1/certificates?per_page=15` < 200ms
|
||||
- `GET /api/v1/stats/summary` < 500ms
|
||||
- `GET /api/v1/metrics/prometheus` < 300ms
|
||||
|
||||
## What This Test Does NOT Cover
|
||||
|
||||
These gaps must be filled by manual testing per `docs/testing-guide.md`:
|
||||
|
||||
### External CA Integrations (Parts 10–13)
|
||||
- **Sub-CA mode** — requires CA cert+key files on disk
|
||||
- **ACME ARI** — requires a CA that supports RFC 9773 Renewal Information
|
||||
- **Vault PKI** — requires a running HashiCorp Vault instance
|
||||
- **DigiCert / Sectigo / Google CAS** — requires sandbox API credentials
|
||||
|
||||
### Browser/GUI Testing (Parts 35–37, 50)
|
||||
- Dashboard chart rendering (Recharts)
|
||||
- Onboarding wizard step-by-step flow
|
||||
- Issuer catalog card layout and create wizard
|
||||
- Bulk operations UI (multi-select, progress bars)
|
||||
- Discovery triage workflow
|
||||
|
||||
### Real Deployment Testing (Parts 15–17)
|
||||
- NGINX/Apache/HAProxy file write + reload
|
||||
- Traefik/Caddy file provider or API reload
|
||||
- IIS PowerShell/WinRM (requires Windows)
|
||||
- F5 BIG-IP iControl REST (requires appliance or mock)
|
||||
- SSH agentless deployment (requires target host)
|
||||
|
||||
### Agent Binary Behavior (Parts 18, 28–29)
|
||||
- Agent-side ECDSA key generation and CSR submission
|
||||
- Agent filesystem discovery scan
|
||||
- CLI tool (`certctl-cli`) — all 10 subcommands
|
||||
- MCP server (`mcp-server`) — stdio transport
|
||||
|
||||
### Timing-Dependent Tests (Parts 33–34)
|
||||
- Background scheduler loop execution (renewal, jobs, health, notifications, digest, network scan)
|
||||
- Structured logging format verification (requires Docker log parsing)
|
||||
|
||||
## How This Relates to `integration_test.go`
|
||||
|
||||
Both files live in `deploy/test/` in the same Go package (`integration_test`):
|
||||
|
||||
| | `qa_test.go` | `integration_test.go` |
|
||||
|---|---|---|
|
||||
| **Build tag** | `//go:build qa` | `//go:build integration` |
|
||||
| **Target stack** | Demo (`docker-compose.yml` + `docker-compose.demo.yml`) | Test (`docker-compose.test.yml`) |
|
||||
| **Port** | 8443 | Different (test stack config) |
|
||||
| **Seed data** | `seed_demo.sql` (32 certs, 8 agents, realistic history) | Minimal (created by tests) |
|
||||
| **CA backends** | Local CA only (demo mode) | Pebble ACME, step-ca, NGINX |
|
||||
| **Purpose** | Release QA — broad coverage, spot checks | Functional — end-to-end issuance, renewal, revocation against real CAs |
|
||||
| **Run frequency** | Before each release tag | CI on every PR |
|
||||
|
||||
They are complementary. Integration tests prove the machinery works. QA tests prove the product works at release quality.
|
||||
|
||||
## Seed Data Reference
|
||||
|
||||
The QA tests depend on `migrations/seed_demo.sql`. Key IDs used:
|
||||
|
||||
### Certificates (32 total)
|
||||
`mc-api-prod`, `mc-web-prod`, `mc-pay-prod`, `mc-dash-prod`, `mc-data-prod`, `mc-search-prod`, `mc-admin-prod`, `mc-blog-prod`, `mc-docs-prod`, `mc-status-prod`, `mc-grpc-prod`, `mc-vault-prod`, `mc-consul-prod`, `mc-shop-prod`, `mc-auth-prod`, `mc-cdn-prod`, `mc-mail-prod`, `mc-ci-prod`, `mc-legacy-prod`, `mc-old-api`, `mc-wiki-prod`, `mc-api-stg`, `mc-web-stg`, `mc-pay-stg`, `mc-api-dev`, `mc-grafana-prod`, `mc-vpn-prod`, `mc-wildcard-prod`, `mc-compromised`, `mc-edge-eu`, `mc-k8s-ingress`, `mc-smime-bob`
|
||||
|
||||
### Agents (9 total)
|
||||
`ag-web-prod`, `ag-web-staging`, `ag-lb-prod`, `ag-iis-prod`, `ag-data-prod`, `ag-edge-01`, `ag-k8s-prod`, `ag-mac-dev`, `server-scanner` (sentinel)
|
||||
|
||||
### Issuers (9 total)
|
||||
`iss-local`, `iss-acme-le`, `iss-stepca`, `iss-acme-zs`, `iss-openssl`, `iss-vault`, `iss-digicert`, `iss-sectigo`, `iss-googlecas`
|
||||
|
||||
### Targets (8 total)
|
||||
`tgt-nginx-prod`, `tgt-nginx-staging`, `tgt-haproxy-prod`, `tgt-apache-prod`, `tgt-iis-prod`, `tgt-traefik-prod`, `tgt-caddy-prod`, `tgt-nginx-data`
|
||||
|
||||
### Network Scan Targets (4 total)
|
||||
`nst-dc1-web`, `nst-dc2-apps`, `nst-dmz`, `nst-edge`
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "Server unreachable" on startup
|
||||
The test pings `GET /health` before running anything. If this fails:
|
||||
```bash
|
||||
# Check if the stack is running
|
||||
docker compose -f docker-compose.yml -f docker-compose.demo.yml ps
|
||||
|
||||
# Check server logs
|
||||
docker compose -f docker-compose.yml -f docker-compose.demo.yml logs certctl-server
|
||||
|
||||
# Check if the port is exposed
|
||||
curl -s http://localhost:8443/health
|
||||
```
|
||||
|
||||
### "connect to QA DB" failure
|
||||
The database tests connect directly to PostgreSQL. Ensure port 5432 is exposed:
|
||||
```bash
|
||||
docker compose -f docker-compose.yml -f docker-compose.demo.yml port postgres 5432
|
||||
```
|
||||
|
||||
### Performance tests flaking
|
||||
The performance thresholds (200ms, 300ms, 500ms) assume a local Docker stack. On slow CI runners or remote Docker hosts, increase the thresholds or skip Part 39:
|
||||
```bash
|
||||
go test -tags qa -v -run 'TestQA/Part(?!39)' ./...
|
||||
```
|
||||
|
||||
### Source file checks failing
|
||||
The `fileExists` and `fileContains` helpers read from `CERTCTL_QA_REPO_DIR` (default `../..`). If running from a non-standard location:
|
||||
```bash
|
||||
CERTCTL_QA_REPO_DIR=/absolute/path/to/certctl go test -tags qa -v ./...
|
||||
```
|
||||
|
||||
## Adding New Tests
|
||||
|
||||
When a new feature ships:
|
||||
|
||||
1. **Add a Part section** in `qa_test.go` following the numbering in `docs/testing-guide.md`
|
||||
2. **API tests**: use `c.get()`, `c.post()`, `c.bodyStr()`, `c.getJSON()`, `c.timedGet()`
|
||||
3. **Source checks**: use `fileExists(t, "relative/path")` and `fileContains(t, "path", "substring")`
|
||||
4. **DB checks**: use `openQADB(t)` and `db.queryInt(t, "SELECT ...")`
|
||||
5. **Cleanup**: always use `t.Cleanup()` for data created during tests
|
||||
6. **Skip if external**: use `t.Skip("Requires X — manual test")` with a clear reason
|
||||
|
||||
## Version History
|
||||
|
||||
- **v1.0** (April 2026) — Initial release covering all 52 Parts of testing-guide.md v2.1. Replaces `qa-smoke-test.sh`.
|
||||
- **v1.1** (April 2026) — Added Parts 53–54 (M47: Kubernetes Secrets target + AWS ACM PCA issuer). 54 Parts total, ~164 automated subtests.
|
||||
+16
-1
@@ -60,6 +60,21 @@ cp deploy/.env.example deploy/.env
|
||||
docker compose -f deploy/docker-compose.yml up -d --build
|
||||
```
|
||||
|
||||
### Docker Compose Environments
|
||||
|
||||
The `deploy/` directory contains four compose files for different use cases:
|
||||
|
||||
| File | Purpose | How to run |
|
||||
|------|---------|------------|
|
||||
| `docker-compose.yml` | **Base platform.** PostgreSQL + certctl server + agent. Clean dashboard with onboarding wizard — use this for production or first-time setup. | `docker compose -f deploy/docker-compose.yml up --build` |
|
||||
| `docker-compose.demo.yml` | **Demo data override.** Layers 180 days of realistic seed data (15 certs, 5 agents, multiple issuers) onto the base. Dashboard charts and tables look populated on first boot. | `docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.demo.yml up --build` |
|
||||
| `docker-compose.dev.yml` | **Development override.** Adds PgAdmin (port 5050), debug-level logging, and a Delve debugger port (40000) for the server. | `docker compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml up --build` |
|
||||
| `docker-compose.test.yml` | **Integration test environment.** 7 containers on a static-IP subnet: PostgreSQL, certctl server+agent, step-ca, Pebble ACME server, challenge test server, and NGINX. Runs the full issuance→deployment→verification flow against real CA backends. Standalone — does not combine with the base file. | `docker compose -f deploy/docker-compose.test.yml up --build` |
|
||||
|
||||
Override files are layered onto the base with multiple `-f` flags. The test environment is self-contained and runs independently. To reset any environment's data, add `down -v` to remove volumes.
|
||||
|
||||
For a deep dive into every service, environment variable, and networking decision, see the [Docker Compose Environments Guide](../deploy/ENVIRONMENTS.md).
|
||||
|
||||
### Kubernetes with Helm
|
||||
|
||||
For production deployments on Kubernetes, use the Helm chart:
|
||||
@@ -404,7 +419,7 @@ export CERTCTL_API_KEY="test-key-123"
|
||||
./mcp-server
|
||||
```
|
||||
|
||||
Exposes 78 MCP tools covering the REST API via stdio transport. Ask Claude: "What certificates are expiring in the next 30 days?", "Revoke the payments cert due to key compromise", "Show me the audit trail."
|
||||
Exposes the full REST API via MCP over stdio transport. Ask Claude: "What certificates are expiring in the next 30 days?", "Revoke the payments cert due to key compromise", "Show me the audit trail."
|
||||
|
||||
## Demo Data Reference
|
||||
|
||||
|
||||
+4041
-2973
File diff suppressed because it is too large
Load Diff
+10
-8
@@ -32,11 +32,13 @@ This isn't a premium feature. It's the default behavior, free. Most alternatives
|
||||
|
||||
### 2. CA-Agnostic Issuer Architecture
|
||||
|
||||
certctl works with any certificate authority, not just ACME providers. Seven issuer connectors ship today, all free:
|
||||
certctl works with any certificate authority, not just ACME providers. Nine issuer connectors ship today, all free:
|
||||
|
||||
- **ACME v2** (Let's Encrypt, ZeroSSL, Google Trust Services, Buypass) — HTTP-01, DNS-01, DNS-PERSIST-01 challenges, External Account Binding, ACME Renewal Information (RFC 9773)
|
||||
- **ACME v2** (Let's Encrypt, ZeroSSL, Google Trust Services, Buypass) — HTTP-01, DNS-01, DNS-PERSIST-01 challenges, External Account Binding, ACME Renewal Information (RFC 9773), certificate profile selection
|
||||
- **HashiCorp Vault PKI** — `/v1/{mount}/sign/{role}` API, token auth
|
||||
- **DigiCert CertCentral** — async order model, OV/EV support
|
||||
- **Sectigo SCM** — async order model, DV/OV/EV support, 3-header auth
|
||||
- **Google Cloud CAS** — Certificate Authority Service, OAuth2 service account auth, CA pool selection
|
||||
- **step-ca** (Smallstep) — native /sign API with JWK provisioner auth
|
||||
- **Local CA** — self-signed or sub-CA mode (chain to ADCS or any enterprise root)
|
||||
- **OpenSSL / Custom CA** — delegate signing to any shell script
|
||||
@@ -54,7 +56,7 @@ A reload command can exit 0 while the certificate doesn't take effect — wrong
|
||||
|
||||
The three differentiators above get the headlines, but the feature surface is wider than most paid platforms:
|
||||
|
||||
**10 deployment targets** — NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, IIS (local PowerShell + remote WinRM), Postfix, and Dovecot. All use a pluggable connector model. The control plane never initiates outbound connections — agents poll for work, meaning certctl works behind firewalls, across network zones, and in air-gapped environments.
|
||||
**13 deployment targets** — NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, IIS (local PowerShell + remote WinRM), F5 BIG-IP (proxy agent + iControl REST), Postfix, Dovecot, SSH (agentless), Windows Certificate Store, and Java Keystore. All use a pluggable connector model. The control plane never initiates outbound connections — agents poll for work, meaning certctl works behind firewalls, across network zones, and in air-gapped environments.
|
||||
|
||||
**Network certificate discovery** — active TLS scanning of CIDR ranges finds certificates you didn't know existed. Agents also scan local filesystems for PEM/DER files. Everything feeds into a triage workflow where you claim, dismiss, or import discovered certs into management.
|
||||
|
||||
@@ -66,9 +68,9 @@ The three differentiators above get the headlines, but the feature surface is wi
|
||||
|
||||
**Prometheus metrics** — `/api/v1/metrics/prometheus` in standard exposition format. Works with Prometheus, Grafana Agent, Datadog Agent, Victoria Metrics.
|
||||
|
||||
**MCP server** — 80 tools exposing the entire API surface for AI-assisted certificate management via Claude, Cursor, or any MCP-compatible client. No other certificate platform offers this.
|
||||
**MCP server** — the entire REST API is exposed via MCP for AI-assisted certificate management via Claude, Cursor, or any MCP-compatible client. No other certificate platform offers this.
|
||||
|
||||
**Full REST API** — 97 OpenAPI 3.1-documented operations. CLI tool with 10 subcommands. Helm chart for Kubernetes deployment. Scheduled certificate digest emails. Certificate export in PEM and PKCS#12. S/MIME support with EKU-aware issuance.
|
||||
**Full REST API** — OpenAPI 3.1-documented operations covering the entire platform. CLI tool with 10 subcommands. Helm chart for Kubernetes deployment. Scheduled certificate digest emails. Certificate export in PEM and PKCS#12. S/MIME support with EKU-aware issuance.
|
||||
|
||||
**Extensively tested** — Go backend with race detection, static analysis (golangci-lint), and vulnerability scanning (govulncheck) on every commit. CI-enforced per-layer coverage thresholds. Frontend test suite. Every push is gated.
|
||||
|
||||
@@ -80,11 +82,11 @@ ACME clients solve one slice of the problem — issuance and renewal from ACME C
|
||||
|
||||
### vs. Agent-Based SaaS
|
||||
|
||||
The closest architectural competitors use the same agent model — local key generation, CSR submission, push-based deployment. Where certctl differs: it supports 7 issuer types (not just ACME), provides CRL/OCSP/revocation infrastructure (not just issuance), includes a policy engine and network discovery, and is source-available with no certificate limit. SaaS alternatives are typically proprietary, priced per certificate ($2+/cert/month), and cap their free tiers at 3-5 certificates. certctl is free for any number of certificates, forever.
|
||||
The closest architectural competitors use the same agent model — local key generation, CSR submission, push-based deployment. Where certctl differs: it supports 9 issuer types (not just ACME), provides CRL/OCSP/revocation infrastructure (not just issuance), includes a policy engine and network discovery, and is source-available with no certificate limit. SaaS alternatives are typically proprietary, priced per certificate ($2+/cert/month), and cap their free tiers at 3-5 certificates. certctl is free for any number of certificates, forever.
|
||||
|
||||
### vs. Commercial PKI Platforms
|
||||
|
||||
On-prem or hosted commercial platforms offer broader cert type coverage (VPN certs, device auth, SCEP) and deeper CA integrations. The trade-off: no free tier, opaque pricing (often €13K+/year for 1,500 certs), proprietary codebases, and no public API documentation. certctl trades breadth of exotic cert types for full transparency — source-available code, 97-operation OpenAPI spec, and a free community edition with no artificial limits.
|
||||
On-prem or hosted commercial platforms offer broader cert type coverage (VPN certs, device auth, SCEP) and deeper CA integrations. The trade-off: no free tier, opaque pricing (often €13K+/year for 1,500 certs), proprietary codebases, and no public API documentation. certctl trades breadth of exotic cert types for full transparency — source-available code, fully documented OpenAPI spec, and a free community edition with no artificial limits.
|
||||
|
||||
### vs. Enterprise Platforms
|
||||
|
||||
@@ -100,7 +102,7 @@ certctl isn't the right tool for everyone:
|
||||
|
||||
## See It Running
|
||||
|
||||
The demo seeds 32 certificates across 7 issuers, 8 agents, 6 deployment targets, and 180 days of realistic history — jobs, audit events, discovery scans, approval workflows — so you can explore every feature immediately.
|
||||
The demo seeds certificates across multiple issuers, agents, and deployment targets with 180 days of realistic history — jobs, audit events, discovery scans, approval workflows — so you can explore every feature immediately.
|
||||
|
||||
```bash
|
||||
git clone https://github.com/shankar0123/certctl.git
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module github.com/shankar0123/certctl
|
||||
|
||||
go 1.25.0
|
||||
go 1.25.9
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
|
||||
+140
-21
@@ -60,8 +60,21 @@ OPTIONS:
|
||||
-h, --help Show this help message
|
||||
--server-url URL Set CERTCTL_SERVER_URL (skips interactive prompt)
|
||||
--api-key KEY Set CERTCTL_API_KEY (skips interactive prompt)
|
||||
--agent-id ID Set CERTCTL_AGENT_ID (defaults to hostname)
|
||||
--no-start Install but don't start the service
|
||||
|
||||
EXAMPLES:
|
||||
# Interactive install (download first):
|
||||
curl -sSLO https://raw.githubusercontent.com/${GITHUB_REPO}/master/install-agent.sh
|
||||
chmod +x install-agent.sh
|
||||
sudo ./install-agent.sh
|
||||
|
||||
# Non-interactive install (pipe via curl):
|
||||
curl -sSL https://raw.githubusercontent.com/${GITHUB_REPO}/master/install-agent.sh \\
|
||||
| sudo bash -s -- \\
|
||||
--server-url https://certctl.example.com \\
|
||||
--api-key YOUR_API_KEY
|
||||
|
||||
EOF
|
||||
}
|
||||
|
||||
@@ -74,19 +87,47 @@ parse_args() {
|
||||
exit 0
|
||||
;;
|
||||
--server-url)
|
||||
SERVER_URL="$2"
|
||||
SERVER_URL="${2:-}"
|
||||
if [[ -z "$SERVER_URL" ]]; then
|
||||
echo -e "${RED}Error: --server-url requires a value${NC}" >&2
|
||||
exit 1
|
||||
fi
|
||||
shift 2
|
||||
;;
|
||||
--server-url=*)
|
||||
SERVER_URL="${1#*=}"
|
||||
shift
|
||||
;;
|
||||
--api-key)
|
||||
API_KEY="$2"
|
||||
API_KEY="${2:-}"
|
||||
if [[ -z "$API_KEY" ]]; then
|
||||
echo -e "${RED}Error: --api-key requires a value${NC}" >&2
|
||||
exit 1
|
||||
fi
|
||||
shift 2
|
||||
;;
|
||||
--api-key=*)
|
||||
API_KEY="${1#*=}"
|
||||
shift
|
||||
;;
|
||||
--agent-id)
|
||||
AGENT_ID="${2:-}"
|
||||
if [[ -z "$AGENT_ID" ]]; then
|
||||
echo -e "${RED}Error: --agent-id requires a value${NC}" >&2
|
||||
exit 1
|
||||
fi
|
||||
shift 2
|
||||
;;
|
||||
--agent-id=*)
|
||||
AGENT_ID="${1#*=}"
|
||||
shift
|
||||
;;
|
||||
--no-start)
|
||||
NO_START=true
|
||||
shift
|
||||
;;
|
||||
*)
|
||||
echo -e "${RED}Error: Unknown option: $1${NC}"
|
||||
echo -e "${RED}Error: Unknown option: $1${NC}" >&2
|
||||
usage
|
||||
exit 1
|
||||
;;
|
||||
@@ -94,6 +135,56 @@ parse_args() {
|
||||
done
|
||||
}
|
||||
|
||||
# Ensure stdin is interactive before prompting. When the script is piped via
|
||||
# curl|bash, stdin is the pipe from curl, so `read` hits EOF immediately and
|
||||
# set -e aborts the script silently. Reopen stdin from the controlling terminal
|
||||
# (/dev/tty) if available; otherwise print a helpful error pointing at the
|
||||
# flag-based non-interactive install.
|
||||
ensure_interactive_input() {
|
||||
# If all required config is already provided via flags, no prompting needed.
|
||||
if [[ -n "${SERVER_URL:-}" && -n "${API_KEY:-}" ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
# Already interactive — nothing to do.
|
||||
if [[ -t 0 ]]; then
|
||||
return
|
||||
fi
|
||||
|
||||
# Piped stdin — try to reopen from the controlling terminal. Actually
|
||||
# attempt to open /dev/tty inside a subshell: the device node may exist
|
||||
# even when the process has no controlling terminal (ENXIO on open), so
|
||||
# `[[ -r /dev/tty ]]` is not reliable.
|
||||
if ( exec </dev/tty ) 2>/dev/null; then
|
||||
exec </dev/tty
|
||||
return
|
||||
fi
|
||||
|
||||
# No terminal available — emit clear guidance and exit.
|
||||
# Use printf '%b' so the ANSI color escapes in $RED/$NC are interpreted
|
||||
# rather than rendered as literal backslash sequences (a heredoc would
|
||||
# keep them as raw text).
|
||||
{
|
||||
printf '%b\n' "${RED}Error: No interactive terminal available.${NC}"
|
||||
printf '\n'
|
||||
printf 'The installer was piped through curl and no controlling terminal (/dev/tty)\n'
|
||||
printf 'is available for prompts. Pass the required values as flags instead:\n'
|
||||
printf '\n'
|
||||
printf ' curl -sSL https://raw.githubusercontent.com/%s/master/install-agent.sh \\\n' "$GITHUB_REPO"
|
||||
printf ' | sudo bash -s -- \\\n'
|
||||
printf ' --server-url https://certctl.example.com \\\n'
|
||||
printf ' --api-key YOUR_API_KEY\n'
|
||||
printf '\n'
|
||||
printf 'Or download the script first and run it directly:\n'
|
||||
printf '\n'
|
||||
printf ' curl -sSLO https://raw.githubusercontent.com/%s/master/install-agent.sh\n' "$GITHUB_REPO"
|
||||
printf ' chmod +x install-agent.sh\n'
|
||||
printf ' sudo ./install-agent.sh\n'
|
||||
printf '\n'
|
||||
} >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if running as root/sudo on Linux
|
||||
check_privileges() {
|
||||
if [[ "$OS_TYPE" == "linux" && "$EUID" -ne 0 ]]; then
|
||||
@@ -103,23 +194,33 @@ check_privileges() {
|
||||
}
|
||||
|
||||
# Download agent binary from GitHub Releases
|
||||
# IMPORTANT: main() captures this function's stdout via `binary_path=$(download_binary)`,
|
||||
# so every status/error message MUST go to stderr (>&2). Only the final
|
||||
# `echo "$temp_file"` is allowed on stdout — that's the return value.
|
||||
#
|
||||
# We deliberately do NOT register an EXIT trap to clean up $temp_file: because
|
||||
# of the command substitution, this function runs in a subshell, and any EXIT
|
||||
# trap set here fires when the subshell exits — which is *before* install_binary
|
||||
# gets a chance to cp the file. Cleanup on success is install_binary's job
|
||||
# (after the cp), and cleanup on curl failure is handled inline below.
|
||||
download_binary() {
|
||||
local binary_name="certctl-agent-${OS_TYPE}-${ARCH_TYPE}"
|
||||
local download_url="${RELEASE_URL}/${binary_name}"
|
||||
|
||||
echo -e "${YELLOW}Downloading certctl agent (${OS_TYPE}-${ARCH_TYPE})...${NC}"
|
||||
echo -e "${YELLOW}Downloading certctl agent (${OS_TYPE}-${ARCH_TYPE})...${NC}" >&2
|
||||
|
||||
if ! command -v curl &> /dev/null; then
|
||||
echo -e "${RED}Error: curl is required but not installed${NC}"
|
||||
echo -e "${RED}Error: curl is required but not installed${NC}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
local temp_file=$(mktemp)
|
||||
trap "rm -f $temp_file" EXIT
|
||||
local temp_file
|
||||
temp_file=$(mktemp)
|
||||
|
||||
if ! curl -sSL -f "$download_url" -o "$temp_file"; then
|
||||
echo -e "${RED}Error: Failed to download binary from $download_url${NC}"
|
||||
echo "Make sure the latest release exists on GitHub with the binary asset for ${OS_TYPE}-${ARCH_TYPE}."
|
||||
if ! curl -sSL -f "$download_url" -o "$temp_file" >&2; then
|
||||
rm -f "$temp_file"
|
||||
echo -e "${RED}Error: Failed to download binary from $download_url${NC}" >&2
|
||||
echo "Make sure the latest release exists on GitHub with the binary asset for ${OS_TYPE}-${ARCH_TYPE}." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -146,35 +247,52 @@ install_binary() {
|
||||
|
||||
chmod +x "$INSTALL_DIR/$SERVICE_NAME"
|
||||
echo -e "${GREEN}Binary installed: $INSTALL_DIR/$SERVICE_NAME${NC}"
|
||||
|
||||
# Clean up the temp file created by download_binary. We can't use an EXIT
|
||||
# trap inside download_binary because it runs in a subshell (command
|
||||
# substitution), so the trap would fire before we got here. Doing it
|
||||
# explicitly after the successful cp is the simplest correct pattern.
|
||||
rm -f "$binary_path"
|
||||
}
|
||||
|
||||
# Prompt for configuration (unless --server-url and --api-key provided)
|
||||
# Prompt for configuration. Any value supplied via flag is honored as-is
|
||||
# and we only prompt for the missing pieces. `read || true` prevents set -e
|
||||
# from aborting the script on EOF — instead the empty check below fires the
|
||||
# proper "required" error message.
|
||||
prompt_for_config() {
|
||||
if [[ -z "${SERVER_URL:-}" ]]; then
|
||||
echo ""
|
||||
echo -e "${YELLOW}Enter certctl server URL (e.g., https://certctl.example.com):${NC}"
|
||||
read -r SERVER_URL
|
||||
if [[ -z "$SERVER_URL" ]]; then
|
||||
echo -e "${RED}Error: Server URL is required${NC}"
|
||||
read -r SERVER_URL || true
|
||||
if [[ -z "${SERVER_URL:-}" ]]; then
|
||||
echo -e "${RED}Error: Server URL is required${NC}" >&2
|
||||
echo "Hint: pass --server-url <URL> to run non-interactively." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z "${API_KEY:-}" ]]; then
|
||||
echo -e "${YELLOW}Enter certctl API key:${NC}"
|
||||
read -sr API_KEY
|
||||
read -rs API_KEY || true
|
||||
echo ""
|
||||
if [[ -z "$API_KEY" ]]; then
|
||||
echo -e "${RED}Error: API key is required${NC}"
|
||||
if [[ -z "${API_KEY:-}" ]]; then
|
||||
echo -e "${RED}Error: API key is required${NC}" >&2
|
||||
echo "Hint: pass --api-key <KEY> to run non-interactively." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -z "${AGENT_ID:-}" ]]; then
|
||||
local default_agent_id="$(hostname)"
|
||||
echo -e "${YELLOW}Enter agent ID (default: $default_agent_id):${NC}"
|
||||
read -r AGENT_ID
|
||||
if [[ -z "$AGENT_ID" ]]; then
|
||||
local default_agent_id
|
||||
default_agent_id="$(hostname)"
|
||||
# If stdin is still piped (no /dev/tty was available but SERVER_URL +
|
||||
# API_KEY arrived via flags), skip the prompt entirely and use the
|
||||
# default — no need to block on an optional value.
|
||||
if [[ -t 0 ]]; then
|
||||
echo -e "${YELLOW}Enter agent ID (default: $default_agent_id):${NC}"
|
||||
read -r AGENT_ID || true
|
||||
fi
|
||||
if [[ -z "${AGENT_ID:-}" ]]; then
|
||||
AGENT_ID="$default_agent_id"
|
||||
fi
|
||||
fi
|
||||
@@ -447,6 +565,7 @@ main() {
|
||||
echo "Detected platform: ${OS_TYPE}-${ARCH_TYPE}"
|
||||
echo ""
|
||||
|
||||
ensure_interactive_input
|
||||
prompt_for_config
|
||||
|
||||
# Download and install binary
|
||||
|
||||
@@ -0,0 +1,339 @@
|
||||
package handler
|
||||
|
||||
// Adversarial EST (RFC 7030) enrollment tests — Tier 1F.
|
||||
//
|
||||
// EST is the RFC 7030 protocol for certificate enrollment over HTTPS. The
|
||||
// control-plane parser accepts PKCS#10 CSRs either as PEM or as base64-encoded
|
||||
// DER, and it's a prime target for:
|
||||
//
|
||||
// * Malformed base64 / non-DER payloads
|
||||
// * Valid base64 that doesn't decode to a valid CSR
|
||||
// * PEM header spoofing (wrong block type)
|
||||
// * Null bytes and control characters embedded in PEM or base64
|
||||
// * Huge CSR bodies (we expect the handler's 1 MiB LimitReader to clamp them)
|
||||
// * Truncated or partially-written PEM blocks
|
||||
// * Unicode homoglyphs in PEM delimiters
|
||||
// * Content-Type mismatch (handler ignores Content-Type, but attackers might
|
||||
// still try header spoofing)
|
||||
//
|
||||
// The contract is the same as other adversarial tiers: the handler must never
|
||||
// panic and must never return 500 for a malformed CSR (500 is reserved for
|
||||
// issuer/service failures). For adversarial CSRs, the correct status is 400.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
)
|
||||
|
||||
// adversarialCSRInputs exercises the EST CSR parsing surface. None of these
|
||||
// should reach the underlying ESTService — they must be rejected by
|
||||
// readCSRFromRequest with a 400 before any service call is made.
|
||||
func adversarialCSRInputs() []struct {
|
||||
name string
|
||||
body string
|
||||
} {
|
||||
// A garbage base64 string that decodes cleanly but isn't a PKCS#10 CSR.
|
||||
// base64 of "this is definitely not a CSR" = dGhpcyBpcyBkZWZpbml0ZWx5IG5vdCBhIENTUg==
|
||||
nonCSRBase64 := base64.StdEncoding.EncodeToString([]byte("this is definitely not a CSR"))
|
||||
|
||||
return []struct {
|
||||
name string
|
||||
body string
|
||||
}{
|
||||
{"garbage_string", "not-a-csr-at-all"},
|
||||
{"base64_garbage", "!!!@@@###$$$%%%"},
|
||||
{"base64_valid_non_csr", nonCSRBase64},
|
||||
{"base64_very_short", "AA=="},
|
||||
{"null_byte_only", "\x00"},
|
||||
{"null_bytes_padding", "\x00\x00\x00\x00\x00\x00\x00\x00"},
|
||||
{"control_chars", "\x01\x02\x03\x04\x05\x06\x07\x08"},
|
||||
{"pem_wrong_block_type", "-----BEGIN CERTIFICATE-----\nMIIB\n-----END CERTIFICATE-----\n"},
|
||||
{"pem_wrong_header_close", "-----BEGIN CERTIFICATE REQUEST-----\nMIIB\n-----END PRIVATE KEY-----\n"},
|
||||
{"pem_empty_block", "-----BEGIN CERTIFICATE REQUEST-----\n-----END CERTIFICATE REQUEST-----\n"},
|
||||
{"pem_garbage_body", "-----BEGIN CERTIFICATE REQUEST-----\n!!!not base64!!!\n-----END CERTIFICATE REQUEST-----\n"},
|
||||
{"pem_truncated", "-----BEGIN CERTIFICATE REQUEST-----\nMIIBijCCAT"},
|
||||
{"pem_no_end_marker", "-----BEGIN CERTIFICATE REQUEST-----\nMIIBijCCATICAQAwFjEUMBIGA1UE\n"},
|
||||
{"pem_header_injection", "-----BEGIN CERTIFICATE REQUEST-----\r\nHost: evil.com\r\n\r\nMIIB\n-----END CERTIFICATE REQUEST-----\n"},
|
||||
{"pem_embedded_null", "-----BEGIN CERTIFICATE\x00REQUEST-----\nMIIB\n-----END CERTIFICATE REQUEST-----\n"},
|
||||
{"unicode_homoglyph_pem", "-----BEGIN CERTIFICATE REQUEST─────\nMIIB\n─────END CERTIFICATE REQUEST-----\n"},
|
||||
{"double_pem_block", "-----BEGIN CERTIFICATE REQUEST-----\nMIIB\n-----END CERTIFICATE REQUEST-----\n-----BEGIN CERTIFICATE REQUEST-----\nMIIB\n-----END CERTIFICATE REQUEST-----\n"},
|
||||
{"json_body", `{"csr":"MIIB","common_name":"attacker.com"}`},
|
||||
{"xml_body", `<?xml version="1.0"?><csr>MIIB</csr>`},
|
||||
{"shell_metacharacters", "$(whoami); rm -rf / #"},
|
||||
{"sql_injection", "' OR 1=1; DROP TABLE certificates;--"},
|
||||
{"long_garbage_10k", strings.Repeat("A", 10000)},
|
||||
{"long_base64_not_csr", base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{0xFF}, 5000))},
|
||||
{"base64_with_newlines_garbage", "AAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCC"},
|
||||
{"percent_encoded_pem", "%2D%2D%2D%2D%2DBEGIN+CERTIFICATE+REQUEST%2D%2D%2D%2D%2D"},
|
||||
}
|
||||
}
|
||||
|
||||
// assertESTErrorResponse enforces the EST handler contract for adversarial CSRs:
|
||||
// no panic, no 500, body is valid JSON (since Error helper emits JSON errors).
|
||||
func assertESTErrorResponse(t *testing.T, w *httptest.ResponseRecorder, label string) {
|
||||
t.Helper()
|
||||
|
||||
// The handler must never reach a 500 for parser-rejected CSRs — that would
|
||||
// indicate a service call slipped through.
|
||||
if w.Code == http.StatusInternalServerError {
|
||||
t.Errorf("%s: handler returned 500 body=%q — adversarial CSR should not reach the service layer",
|
||||
label, w.Body.String())
|
||||
}
|
||||
|
||||
// The handler should return 400 Bad Request for adversarial CSR inputs.
|
||||
// A 405 (method not allowed) is impossible here because we always POST.
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("%s: expected 400, got %d (body=%q)", label, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// newESTHandlerWithTrap returns an ESTHandler whose service panics if reached.
|
||||
// This is the core invariant for Tier 1F: adversarial CSRs must be rejected at
|
||||
// the parser, never reaching SimpleEnroll/SimpleReEnroll on the service.
|
||||
func newESTHandlerWithTrap() (ESTHandler, *trappedESTService) {
|
||||
svc := &trappedESTService{}
|
||||
return NewESTHandler(svc), svc
|
||||
}
|
||||
|
||||
// trappedESTService is a mock that fails the test if any service method is
|
||||
// called with an adversarial CSR. The parser should reject these before they
|
||||
// get here.
|
||||
type trappedESTService struct {
|
||||
serviceCalled bool
|
||||
}
|
||||
|
||||
func (t *trappedESTService) GetCACerts(ctx context.Context) (string, error) {
|
||||
t.serviceCalled = true
|
||||
return "", errors.New("trap: GetCACerts should not be called from adversarial CSR tests")
|
||||
}
|
||||
|
||||
func (t *trappedESTService) SimpleEnroll(ctx context.Context, csrPEM string) (*domain.ESTEnrollResult, error) {
|
||||
t.serviceCalled = true
|
||||
return nil, errors.New("trap: SimpleEnroll should not be called from adversarial CSR tests")
|
||||
}
|
||||
|
||||
func (t *trappedESTService) SimpleReEnroll(ctx context.Context, csrPEM string) (*domain.ESTEnrollResult, error) {
|
||||
t.serviceCalled = true
|
||||
return nil, errors.New("trap: SimpleReEnroll should not be called from adversarial CSR tests")
|
||||
}
|
||||
|
||||
func (t *trappedESTService) GetCSRAttrs(ctx context.Context) ([]byte, error) {
|
||||
t.serviceCalled = true
|
||||
return nil, errors.New("trap: GetCSRAttrs should not be called from adversarial CSR tests")
|
||||
}
|
||||
|
||||
// TestESTSimpleEnroll_AdversarialCSRs runs each adversarial CSR through the
|
||||
// enrollment endpoint.
|
||||
func TestESTSimpleEnroll_AdversarialCSRs(t *testing.T) {
|
||||
for _, tc := range adversarialCSRInputs() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on body %q: %v", tc.body, r)
|
||||
}
|
||||
}()
|
||||
|
||||
h, svc := newESTHandlerWithTrap()
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/simpleenroll", strings.NewReader(tc.body))
|
||||
req.Header.Set("Content-Type", "application/pkcs10")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
h.SimpleEnroll(w, req)
|
||||
|
||||
assertESTErrorResponse(t, w, "SimpleEnroll/"+tc.name)
|
||||
|
||||
if svc.serviceCalled {
|
||||
t.Errorf("SimpleEnroll/%s: service was reached with adversarial CSR (body=%q)",
|
||||
tc.name, tc.body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestESTSimpleReEnroll_AdversarialCSRs runs each adversarial CSR through the
|
||||
// re-enrollment endpoint. Same contract as simpleenroll.
|
||||
func TestESTSimpleReEnroll_AdversarialCSRs(t *testing.T) {
|
||||
for _, tc := range adversarialCSRInputs() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on body %q: %v", tc.body, r)
|
||||
}
|
||||
}()
|
||||
|
||||
h, svc := newESTHandlerWithTrap()
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/simplereenroll", strings.NewReader(tc.body))
|
||||
req.Header.Set("Content-Type", "application/pkcs10")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
h.SimpleReEnroll(w, req)
|
||||
|
||||
assertESTErrorResponse(t, w, "SimpleReEnroll/"+tc.name)
|
||||
|
||||
if svc.serviceCalled {
|
||||
t.Errorf("SimpleReEnroll/%s: service was reached with adversarial CSR (body=%q)",
|
||||
tc.name, tc.body)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestESTSimpleEnroll_HugeBody verifies the handler's 1 MiB limit truncates
|
||||
// oversized requests at the LimitReader boundary. We send a 2 MiB body of
|
||||
// base64 garbage and confirm the handler rejects it cleanly (400, no panic,
|
||||
// no 500) and the service is never reached.
|
||||
func TestESTSimpleEnroll_HugeBody(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on 2 MiB body: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// 2 MiB of base64-valid garbage: the LimitReader will truncate to 1 MiB, and
|
||||
// the truncated base64 chunk won't parse as a valid PKCS#10 CSR.
|
||||
huge := strings.Repeat("A", 2<<20)
|
||||
|
||||
h, svc := newESTHandlerWithTrap()
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/simpleenroll", strings.NewReader(huge))
|
||||
req.Header.Set("Content-Type", "application/pkcs10")
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
h.SimpleEnroll(w, req)
|
||||
|
||||
// Contract: 400 Bad Request (parser fail), no panic, no 500.
|
||||
if w.Code == http.StatusInternalServerError {
|
||||
t.Errorf("HugeBody: handler returned 500 for 2 MiB body (body=%q)", w.Body.String())
|
||||
}
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("HugeBody: expected 400, got %d (body=%q)", w.Code, w.Body.String())
|
||||
}
|
||||
if svc.serviceCalled {
|
||||
t.Error("HugeBody: service was reached with 2 MiB adversarial body")
|
||||
}
|
||||
}
|
||||
|
||||
// TestESTSimpleEnroll_ExactlyAtLimit sends a body exactly at the 1 MiB
|
||||
// LimitReader boundary. The body is still garbage (won't parse as CSR), but we
|
||||
// verify the handler doesn't panic or hang on the boundary case.
|
||||
func TestESTSimpleEnroll_ExactlyAtLimit(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on exact-limit body: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
atLimit := strings.Repeat("A", 1<<20) // exactly 1 MiB
|
||||
|
||||
h, _ := newESTHandlerWithTrap()
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/simpleenroll", strings.NewReader(atLimit))
|
||||
w := httptest.NewRecorder()
|
||||
h.SimpleEnroll(w, req)
|
||||
|
||||
if w.Code == http.StatusInternalServerError {
|
||||
t.Errorf("ExactlyAtLimit: handler returned 500 (body=%q)", w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestESTSimpleEnroll_MultipartBody sends a multipart/form-data body that a
|
||||
// naive parser might try to unwrap. The handler should treat the raw bytes as
|
||||
// a CSR payload and reject them.
|
||||
func TestESTSimpleEnroll_MultipartBody(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on multipart body: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
multipart := "--boundary\r\nContent-Disposition: form-data; name=\"csr\"\r\n\r\nMIIB\r\n--boundary--\r\n"
|
||||
|
||||
h, svc := newESTHandlerWithTrap()
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/.well-known/est/simpleenroll", strings.NewReader(multipart))
|
||||
req.Header.Set("Content-Type", "multipart/form-data; boundary=boundary")
|
||||
w := httptest.NewRecorder()
|
||||
h.SimpleEnroll(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("MultipartBody: expected 400, got %d (body=%q)", w.Code, w.Body.String())
|
||||
}
|
||||
if svc.serviceCalled {
|
||||
t.Error("MultipartBody: service was reached with multipart wrapper")
|
||||
}
|
||||
}
|
||||
|
||||
// TestESTCACerts_MethodAbuse verifies the /cacerts endpoint only accepts GET
|
||||
// and rejects every other method cleanly. This is a small safety check for
|
||||
// the spec invariant.
|
||||
func TestESTCACerts_MethodAbuse(t *testing.T) {
|
||||
methods := []string{
|
||||
http.MethodPost, http.MethodPut, http.MethodDelete,
|
||||
http.MethodPatch, http.MethodHead, http.MethodOptions,
|
||||
"TRACE", "CONNECT", "PROPFIND", "BOGUS",
|
||||
}
|
||||
|
||||
for _, method := range methods {
|
||||
t.Run(method, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on method %s: %v", method, r)
|
||||
}
|
||||
}()
|
||||
|
||||
h, _ := newESTHandlerWithTrap()
|
||||
|
||||
req := httptest.NewRequest(method, "/.well-known/est/cacerts", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.CACerts(w, req)
|
||||
|
||||
// HEAD on a GET handler in Go's stdlib is normally accepted, but
|
||||
// this handler enforces strict GET-only — so HEAD should also get 405.
|
||||
if w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("method %s: expected 405, got %d", method, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestESTSimpleEnroll_MethodAbuse verifies strict POST-only enforcement.
|
||||
func TestESTSimpleEnroll_MethodAbuse(t *testing.T) {
|
||||
methods := []string{
|
||||
http.MethodGet, http.MethodPut, http.MethodDelete,
|
||||
http.MethodPatch, http.MethodHead, http.MethodOptions,
|
||||
"TRACE", "CONNECT",
|
||||
}
|
||||
|
||||
for _, method := range methods {
|
||||
t.Run(method, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on method %s: %v", method, r)
|
||||
}
|
||||
}()
|
||||
|
||||
h, svc := newESTHandlerWithTrap()
|
||||
|
||||
req := httptest.NewRequest(method, "/.well-known/est/simpleenroll", strings.NewReader("body"))
|
||||
w := httptest.NewRecorder()
|
||||
h.SimpleEnroll(w, req)
|
||||
|
||||
if w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("method %s: expected 405, got %d", method, w.Code)
|
||||
}
|
||||
if svc.serviceCalled {
|
||||
t.Errorf("method %s: service was called for non-POST", method)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
package handler
|
||||
|
||||
// Adversarial path-parameter and multi-segment path tests.
|
||||
//
|
||||
// These tests exercise the input parsing boundary of the certificate handler
|
||||
// against the attack categories listed in certctl-adversarial-testing-prompt.md
|
||||
// Tier 1A / 1B:
|
||||
//
|
||||
// * Empty and whitespace-only path IDs
|
||||
// * SQL-injection sentinels embedded in the path
|
||||
// * Directory traversal (`../../etc/passwd`)
|
||||
// * Null bytes and control characters
|
||||
// * Extremely long IDs (10 KiB)
|
||||
// * Unicode homoglyphs (visually identical substitutes)
|
||||
// * Multi-segment paths (OCSP, DER CRL, versions, renew, deploy, revoke)
|
||||
//
|
||||
// The contract we verify is defensive, not behavioural:
|
||||
//
|
||||
// 1. The handler never panics.
|
||||
// 2. The HTTP status is one of {200, 400, 404, 405} — never 500.
|
||||
// 3. The response body is either empty or valid JSON.
|
||||
// 4. No attacker-controlled input is echoed verbatim in a 500 body.
|
||||
//
|
||||
// We do not assert the exact status code for every adversarial input because
|
||||
// the current handler intentionally delegates identifier validation to the
|
||||
// repository layer; its only job here is to stay up and well-formed.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
)
|
||||
|
||||
// adversarialPathInputs is the attack catalog shared by Tier 1A cases. Each
|
||||
// entry targets a different parsing surface; adding a new category here makes
|
||||
// every Tier 1A test below exercise it automatically.
|
||||
func adversarialPathInputs() []struct {
|
||||
name string
|
||||
input string
|
||||
} {
|
||||
return []struct {
|
||||
name string
|
||||
input string
|
||||
}{
|
||||
{"sql_injection_drop_table", "'; DROP TABLE managed_certificates;--"},
|
||||
{"sql_injection_or_true", "' OR 1=1--"},
|
||||
{"sql_injection_union", "mc-001' UNION SELECT * FROM agents--"},
|
||||
{"path_traversal_dot_dot", "../../etc/passwd"},
|
||||
{"path_traversal_encoded", "..%2F..%2Fetc%2Fpasswd"},
|
||||
{"null_byte_trailing", "mc-001\x00"},
|
||||
{"null_byte_embedded", "mc-\x00-001"},
|
||||
{"long_id_10k", strings.Repeat("A", 10000)},
|
||||
{"unicode_homoglyph_hyphen", "mc\u2010001"}, // U+2010 HYPHEN
|
||||
{"unicode_homoglyph_fullwidth", "mc\uFF0D001"}, // U+FF0D FULLWIDTH HYPHEN-MINUS
|
||||
{"control_char_newline", "mc-001\n"},
|
||||
{"control_char_tab", "mc\t001"},
|
||||
{"control_char_bell", "mc\x07001"},
|
||||
{"percent_encoded_null", "mc-001%00"},
|
||||
{"whitespace_only", " "},
|
||||
{"shell_metacharacters", "mc-001;`rm -rf /`"},
|
||||
{"leading_slash", "/mc-001"},
|
||||
{"trailing_slash", "mc-001/"},
|
||||
{"double_slash", "mc//001"},
|
||||
}
|
||||
}
|
||||
|
||||
// assertSafeResponse is the core defensive check. Any adversarial input is
|
||||
// allowed to produce a 4xx, but must not panic or leak through as a 500.
|
||||
func assertSafeResponse(t *testing.T, w *httptest.ResponseRecorder, label string) {
|
||||
t.Helper()
|
||||
|
||||
// 1. No 500 (500 implies the handler reached an unexpected internal state).
|
||||
if w.Code == http.StatusInternalServerError {
|
||||
t.Errorf("%s: handler returned 500, body=%q — adversarial input should not reach an internal error path",
|
||||
label, w.Body.String())
|
||||
}
|
||||
|
||||
// 2. Status must be in the expected safe set.
|
||||
switch w.Code {
|
||||
case http.StatusOK, http.StatusCreated, http.StatusAccepted, http.StatusNoContent,
|
||||
http.StatusBadRequest, http.StatusNotFound, http.StatusMethodNotAllowed, http.StatusNotImplemented:
|
||||
// ok
|
||||
default:
|
||||
t.Errorf("%s: unexpected status %d (body=%q)", label, w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// 3. Non-empty bodies must be valid JSON (no template leakage, no raw panics).
|
||||
if body := bytes.TrimSpace(w.Body.Bytes()); len(body) > 0 {
|
||||
var discard interface{}
|
||||
if err := json.Unmarshal(body, &discard); err != nil {
|
||||
t.Errorf("%s: response body is not valid JSON: %v (body=%q)", label, err, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// newCertHandlerWithMock builds a handler whose mock service returns nothing.
|
||||
// This keeps every adversarial test focused on the handler's parsing layer
|
||||
// rather than service behaviour.
|
||||
func newCertHandlerWithMock() (CertificateHandler, *MockCertificateService) {
|
||||
mock := &MockCertificateService{}
|
||||
return NewCertificateHandler(mock), mock
|
||||
}
|
||||
|
||||
// TestGetCertificate_PathInjection runs each adversarial path through the
|
||||
// certificate GET handler.
|
||||
func TestGetCertificate_PathInjection(t *testing.T) {
|
||||
for _, tc := range adversarialPathInputs() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on input %q: %v", tc.input, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
// Force a 404 so we can distinguish "service was called" from
|
||||
// "parser accepted the ID"; a 200 with null body is also fine.
|
||||
mock.GetCertificateFn = func(id string) (*domain.ManagedCertificate, error) {
|
||||
return nil, ErrMockNotFound
|
||||
}
|
||||
|
||||
// Build the URL by string concatenation to keep attacker-controlled
|
||||
// bytes intact (httptest.NewRequest uses url.Parse under the hood,
|
||||
// which normalises some characters — we want the raw path on the
|
||||
// request object).
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/certificates/x", nil)
|
||||
req.URL.Path = "/api/v1/certificates/" + tc.input
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.GetCertificate(w, req)
|
||||
|
||||
assertSafeResponse(t, w, "GetCertificate/"+tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateCertificate_PathInjection exercises the PUT handler's path parser.
|
||||
// UpdateCertificate splits the path on "/" and takes parts[0]; traversal and
|
||||
// double-slash inputs must still short-circuit at the parser rather than
|
||||
// reaching the service.
|
||||
func TestUpdateCertificate_PathInjection(t *testing.T) {
|
||||
body := `{"common_name":"example.com","owner_id":"o-alice","team_id":"t-a","issuer_id":"iss-local","name":"n","renewal_policy_id":"rp-1"}`
|
||||
|
||||
for _, tc := range adversarialPathInputs() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on input %q: %v", tc.input, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.UpdateCertificateFn = func(id string, cert domain.ManagedCertificate) (*domain.ManagedCertificate, error) {
|
||||
return nil, ErrMockNotFound
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/v1/certificates/x", bytes.NewBufferString(body))
|
||||
req.URL.Path = "/api/v1/certificates/" + tc.input
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.UpdateCertificate(w, req)
|
||||
|
||||
assertSafeResponse(t, w, "UpdateCertificate/"+tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestArchiveCertificate_PathInjection exercises DELETE.
|
||||
func TestArchiveCertificate_PathInjection(t *testing.T) {
|
||||
for _, tc := range adversarialPathInputs() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on input %q: %v", tc.input, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.ArchiveCertificateFn = func(id string) error { return ErrMockNotFound }
|
||||
|
||||
req := httptest.NewRequest(http.MethodDelete, "/api/v1/certificates/x", nil)
|
||||
req.URL.Path = "/api/v1/certificates/" + tc.input
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ArchiveCertificate(w, req)
|
||||
|
||||
assertSafeResponse(t, w, "ArchiveCertificate/"+tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetCertificateVersions_MultiSegment is a Tier 1B test: the versions
|
||||
// handler requires a 2-segment path (certID/versions). The parser uses
|
||||
// strings.Split(path, "/") and checks len(parts) < 2 — but an adversarial
|
||||
// caller can inject extra slashes to either produce an empty parts[0] or a
|
||||
// very long parts slice. Either way we must not panic.
|
||||
func TestGetCertificateVersions_MultiSegment(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
path string
|
||||
}{
|
||||
{"missing_segment", "/api/v1/certificates/versions"},
|
||||
{"empty_cert_id", "/api/v1/certificates//versions"},
|
||||
{"traversal_cert_id", "/api/v1/certificates/..%2F..%2Fversions/versions"},
|
||||
{"sql_injection_cert_id", "/api/v1/certificates/'%20OR%201=1--/versions"},
|
||||
{"null_byte_cert_id", "/api/v1/certificates/mc\x00001/versions"},
|
||||
{"very_long_cert_id", "/api/v1/certificates/" + strings.Repeat("A", 5000) + "/versions"},
|
||||
{"trailing_segments", "/api/v1/certificates/mc-001/versions/extra/trailing"},
|
||||
{"deep_nesting", "/api/v1/certificates/" + strings.Repeat("a/", 50) + "versions"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on path %q: %v", tc.path, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.GetCertificateVersionsFn = func(certID string, page, perPage int) ([]domain.CertificateVersion, int64, error) {
|
||||
return []domain.CertificateVersion{}, 0, nil
|
||||
}
|
||||
|
||||
// Use a dummy safe URL in NewRequest to avoid url.Parse panics
|
||||
// on control chars, then overwrite with the raw attacker path.
|
||||
req := httptest.NewRequest(http.MethodGet, "/safe", nil)
|
||||
req.URL.Path = tc.path
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.GetCertificateVersions(w, req)
|
||||
|
||||
assertSafeResponse(t, w, "GetCertificateVersions/"+tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleOCSP_MultiSegment exercises the OCSP responder's 2-segment path
|
||||
// parser (/api/v1/ocsp/{issuer_id}/{serial_hex}). Each leg is attacker-
|
||||
// controlled and the serial can be arbitrary length. This is a key adversarial
|
||||
// surface because the serial is passed directly to the CA-operations service,
|
||||
// which is expected to treat it as an opaque identifier.
|
||||
func TestHandleOCSP_MultiSegment(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
path string
|
||||
}{
|
||||
{"missing_serial", "/api/v1/ocsp/iss-local"},
|
||||
{"missing_both", "/api/v1/ocsp/"},
|
||||
{"empty_issuer", "/api/v1/ocsp//01ABCDEF"},
|
||||
{"empty_serial", "/api/v1/ocsp/iss-local/"},
|
||||
{"traversal_issuer", "/api/v1/ocsp/..%2F..%2Fetc/passwd/01"},
|
||||
{"null_byte_serial", "/api/v1/ocsp/iss-local/01\x00FF"},
|
||||
{"sql_injection_serial", "/api/v1/ocsp/iss-local/01'; DROP TABLE--"},
|
||||
{"negative_hex_serial", "/api/v1/ocsp/iss-local/-1"},
|
||||
{"unicode_serial", "/api/v1/ocsp/iss-local/01\u2010FF"},
|
||||
{"extremely_long_serial", "/api/v1/ocsp/iss-local/" + strings.Repeat("F", 10000)},
|
||||
{"extra_segments", "/api/v1/ocsp/iss-local/01FF/extra/segments"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on path %q: %v", tc.path, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.GetOCSPResponseFn = func(issuerID, serialHex string) ([]byte, error) {
|
||||
return nil, ErrMockNotFound
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/safe", nil)
|
||||
req.URL.Path = tc.path
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.HandleOCSP(w, req)
|
||||
|
||||
// OCSP does NOT guarantee JSON responses (pkix-crl uses binary),
|
||||
// so we only check status safety, not body structure.
|
||||
if w.Code == http.StatusInternalServerError {
|
||||
t.Errorf("HandleOCSP/%s: returned 500 body=%q", tc.name, w.Body.String())
|
||||
}
|
||||
if w.Code >= 500 {
|
||||
t.Errorf("HandleOCSP/%s: unexpected 5xx %d", tc.name, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetDERCRL_IssuerPathInjection exercises /api/v1/crl/{issuer_id}.
|
||||
func TestGetDERCRL_IssuerPathInjection(t *testing.T) {
|
||||
for _, tc := range adversarialPathInputs() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("handler panicked on input %q: %v", tc.input, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.GenerateDERCRLFn = func(issuerID string) ([]byte, error) {
|
||||
return nil, ErrMockNotFound
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/crl/x", nil)
|
||||
req.URL.Path = "/api/v1/crl/" + tc.input
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.GetDERCRL(w, req)
|
||||
|
||||
if w.Code >= 500 {
|
||||
t.Errorf("GetDERCRL/%s: unexpected 5xx %d (body=%q)", tc.name, w.Code, w.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,538 @@
|
||||
package handler
|
||||
|
||||
// Adversarial query-parameter, request-body, and revocation-reason tests.
|
||||
//
|
||||
// These tests exercise the second boundary of the certificate handler:
|
||||
//
|
||||
// * Numeric pagination parsing (page, per_page, page_size)
|
||||
// * Sort direction and field whitelist
|
||||
// * Time-range filters (expires_before, expires_after, created_after, updated_after)
|
||||
// * Cursor pagination
|
||||
// * Sparse-field projection (?fields=...)
|
||||
// * Request-body JSON parsing (create/update) — null, malformed, deep nesting,
|
||||
// unicode, oversized
|
||||
// * Revocation reason abuse
|
||||
//
|
||||
// The handler silently ignores malformed pagination values (it falls back to
|
||||
// defaults) and ignores invalid RFC3339 time values. These tests lock in that
|
||||
// behaviour so a future "fail-closed" change has to be deliberate.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
"github.com/shankar0123/certctl/internal/repository"
|
||||
)
|
||||
|
||||
// buildListRequest constructs a GET /api/v1/certificates request with the
|
||||
// given raw query string. We use raw query strings (not url.Values.Encode)
|
||||
// so adversarial inputs like "page=abc&page=-1" or "%00" pass through
|
||||
// unchanged.
|
||||
func buildListRequest(rawQuery string) *http.Request {
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/certificates", nil)
|
||||
req.URL.RawQuery = rawQuery
|
||||
return req.WithContext(contextWithRequestID())
|
||||
}
|
||||
|
||||
// TestListCertificates_PaginationAbuse verifies adversarial pagination values
|
||||
// never produce a 500 and the handler always falls back to sane defaults.
|
||||
func TestListCertificates_PaginationAbuse(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
rawQuery string
|
||||
}{
|
||||
{"negative_page", "page=-1"},
|
||||
{"zero_page", "page=0"},
|
||||
{"non_numeric_page", "page=abc"},
|
||||
{"huge_page", "page=99999999999"},
|
||||
{"int_overflow_page", "page=9223372036854775808"}, // int64 max + 1
|
||||
{"negative_per_page", "per_page=-1"},
|
||||
{"zero_per_page", "per_page=0"},
|
||||
{"per_page_cap_at_500", "per_page=500"},
|
||||
{"per_page_above_cap", "per_page=501"},
|
||||
{"per_page_absurd", "per_page=1000000"},
|
||||
{"non_numeric_per_page", "per_page=xyz"},
|
||||
{"mixed_numeric_per_page", "per_page=10abc"},
|
||||
{"negative_page_size", "page_size=-1"},
|
||||
{"page_size_above_cap", "page_size=501"},
|
||||
{"float_page", "page=1.5"},
|
||||
{"exponent_page", "page=1e10"},
|
||||
{"hex_page", "page=0xff"},
|
||||
{"unicode_digits_page", "page=\u0661\u0662\u0663"}, // Arabic-Indic digits
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked on %q: %v", tc.rawQuery, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.ListCertificatesWithFilterFn = func(filter *repository.CertificateFilter) ([]domain.ManagedCertificate, int, error) {
|
||||
// Sanity: page/perPage on the filter must never be negative
|
||||
// and perPage must never exceed 500 after parsing.
|
||||
if filter.Page < 1 {
|
||||
t.Errorf("filter.Page=%d (must be >=1)", filter.Page)
|
||||
}
|
||||
if filter.PerPage < 1 || filter.PerPage > 500 {
|
||||
t.Errorf("filter.PerPage=%d (must be in [1,500])", filter.PerPage)
|
||||
}
|
||||
return []domain.ManagedCertificate{}, 0, nil
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListCertificates(w, buildListRequest(tc.rawQuery))
|
||||
|
||||
assertSafeResponse(t, w, "ListCertificates/"+tc.name)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("%s: expected 200, got %d (body=%q)", tc.name, w.Code, w.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestListCertificates_SortAbuse verifies the sort field (which feeds into a
|
||||
// whitelist in the repository layer) handles adversarial input safely at the
|
||||
// handler boundary. The handler accepts the raw value and forwards it; the
|
||||
// repository is expected to whitelist it, but at THIS layer we just verify
|
||||
// we don't crash or leak.
|
||||
func TestListCertificates_SortAbuse(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
rawQuery string
|
||||
}{
|
||||
{"sql_injection_sort", "sort=notAfter;DROP TABLE managed_certificates--"},
|
||||
{"sql_injection_or", "sort=notAfter' OR '1'='1"},
|
||||
{"path_traversal_sort", "sort=../../etc/passwd"},
|
||||
{"null_byte_sort", "sort=notAfter%00"},
|
||||
{"unicode_sort", "sort=notAfter\u2010desc"},
|
||||
{"leading_dash_only", "sort=-"},
|
||||
{"leading_dashes", "sort=---notAfter"},
|
||||
{"empty_sort", "sort="},
|
||||
{"very_long_sort", "sort=" + strings.Repeat("a", 5000)},
|
||||
{"sort_desc_flag", "sort=notAfter&sort_desc=true"},
|
||||
{"conflicting_sort_desc", "sort=-notAfter&sort_desc=false"},
|
||||
{"unknown_field", "sort=gibberish"},
|
||||
{"shell_metacharacters_sort", "sort=notAfter;rm -rf /"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked on %q: %v", tc.rawQuery, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.ListCertificatesWithFilterFn = func(filter *repository.CertificateFilter) ([]domain.ManagedCertificate, int, error) {
|
||||
return []domain.ManagedCertificate{}, 0, nil
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListCertificates(w, buildListRequest(tc.rawQuery))
|
||||
|
||||
assertSafeResponse(t, w, "ListCertificates/"+tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestListCertificates_FieldsAbuse verifies sparse field projection handles
|
||||
// adversarial field lists safely.
|
||||
func TestListCertificates_FieldsAbuse(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
rawQuery string
|
||||
}{
|
||||
{"sql_injection_fields", "fields=id,name' OR 1=1--"},
|
||||
{"path_traversal_fields", "fields=../../etc/passwd"},
|
||||
{"empty_fields", "fields="},
|
||||
{"single_comma", "fields=,"},
|
||||
{"trailing_comma", "fields=id,name,"},
|
||||
{"leading_comma", "fields=,id,name"},
|
||||
{"whitespace_fields", "fields= id , name "},
|
||||
{"duplicate_fields", "fields=id,id,id,id,id"},
|
||||
{"unknown_fields", "fields=totally_not_a_field"},
|
||||
{"many_fields", "fields=" + strings.Repeat("x,", 200) + "id"},
|
||||
{"unicode_fields", "fields=id,n\u00e4me"},
|
||||
{"null_byte_fields", "fields=id%00name"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked on %q: %v", tc.rawQuery, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.ListCertificatesWithFilterFn = func(filter *repository.CertificateFilter) ([]domain.ManagedCertificate, int, error) {
|
||||
return []domain.ManagedCertificate{}, 0, nil
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListCertificates(w, buildListRequest(tc.rawQuery))
|
||||
|
||||
assertSafeResponse(t, w, "ListCertificates/"+tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestListCertificates_TimeRangeAbuse verifies RFC3339 time-range filters
|
||||
// handle malformed input by silently falling back to no filter (current
|
||||
// behaviour).
|
||||
func TestListCertificates_TimeRangeAbuse(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
rawQuery string
|
||||
}{
|
||||
{"invalid_expires_before", "expires_before=not-a-date"},
|
||||
{"empty_expires_before", "expires_before="},
|
||||
{"garbage_expires_before", "expires_before=%00%00"},
|
||||
{"sql_injection_time", "expires_before=2026-01-01T00:00:00Z';DROP TABLE managed_certificates--"},
|
||||
{"year_zero", "expires_before=0000-01-01T00:00:00Z"},
|
||||
{"year_negative", "expires_before=-0001-01-01T00:00:00Z"},
|
||||
{"year_huge", "expires_before=99999-12-31T23:59:59Z"},
|
||||
{"invalid_month", "expires_before=2026-13-01T00:00:00Z"},
|
||||
{"invalid_day", "expires_before=2026-02-30T00:00:00Z"},
|
||||
{"valid_utc", "expires_before=2026-06-15T12:00:00Z"},
|
||||
{"valid_with_offset", "expires_before=2026-06-15T12:00:00-07:00"},
|
||||
{"unix_seconds_not_rfc3339", "expires_before=1767225600"},
|
||||
{"all_four_filters", "expires_before=garbage&expires_after=garbage&created_after=garbage&updated_after=garbage"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked on %q: %v", tc.rawQuery, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.ListCertificatesWithFilterFn = func(filter *repository.CertificateFilter) ([]domain.ManagedCertificate, int, error) {
|
||||
return []domain.ManagedCertificate{}, 0, nil
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListCertificates(w, buildListRequest(tc.rawQuery))
|
||||
|
||||
assertSafeResponse(t, w, "ListCertificates/"+tc.name)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("%s: expected 200, got %d", tc.name, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestListCertificates_CursorAbuse exercises cursor-based pagination with
|
||||
// adversarial cursor tokens. The handler forwards the cursor to the
|
||||
// repository; we verify no 500 at the boundary and that the response type
|
||||
// switches correctly.
|
||||
func TestListCertificates_CursorAbuse(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
cursor string
|
||||
}{
|
||||
{"empty_not_set", ""}, // special-cased: should return PagedResponse
|
||||
{"garbage_cursor", "not-a-valid-cursor"},
|
||||
{"base64_garbage", "dGhpcyBpcyBub3QgYSB2YWxpZCBjdXJzb3I="},
|
||||
{"sql_injection_cursor", "2026-01-01T00:00:00Z:mc-001';DROP TABLE--"},
|
||||
{"path_traversal_cursor", "../../etc/passwd"},
|
||||
{"null_byte_cursor", "valid%00cursor"},
|
||||
{"very_long_cursor", strings.Repeat("A", 8192)},
|
||||
{"unicode_cursor", "2026-01-01T00:00:00Z:mc\u20100001"},
|
||||
{"valid_looking_cursor", "2026-01-01T00:00:00.000000000Z:mc-001"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked on %q: %v", tc.cursor, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.ListCertificatesWithFilterFn = func(filter *repository.CertificateFilter) ([]domain.ManagedCertificate, int, error) {
|
||||
return []domain.ManagedCertificate{}, 0, nil
|
||||
}
|
||||
|
||||
rawQuery := "cursor=" + url.QueryEscape(tc.cursor) + "&page_size=50"
|
||||
if tc.cursor == "" {
|
||||
rawQuery = "page=1&per_page=50"
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListCertificates(w, buildListRequest(rawQuery))
|
||||
|
||||
assertSafeResponse(t, w, "ListCertificates/"+tc.name)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("%s: expected 200, got %d", tc.name, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestListCertificates_FilterInjection verifies the basic string filters
|
||||
// (status, environment, owner_id, team_id, issuer_id, agent_id, profile_id)
|
||||
// are forwarded as-is without causing any handler-layer failures. These go
|
||||
// into parameterized SQL at the repo layer.
|
||||
func TestListCertificates_FilterInjection(t *testing.T) {
|
||||
filters := []string{
|
||||
"status", "environment", "owner_id", "team_id",
|
||||
"issuer_id", "agent_id", "profile_id",
|
||||
}
|
||||
payloads := []string{
|
||||
"' OR 1=1--",
|
||||
"'; DROP TABLE managed_certificates;--",
|
||||
"../../etc/passwd",
|
||||
strings.Repeat("A", 5000),
|
||||
"\u2010hyphen",
|
||||
"%00null",
|
||||
}
|
||||
|
||||
for _, f := range filters {
|
||||
for _, p := range payloads {
|
||||
name := f + "__" + p
|
||||
if len(name) > 80 {
|
||||
name = name[:80]
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.ListCertificatesWithFilterFn = func(filter *repository.CertificateFilter) ([]domain.ManagedCertificate, int, error) {
|
||||
return []domain.ManagedCertificate{}, 0, nil
|
||||
}
|
||||
|
||||
rawQuery := f + "=" + url.QueryEscape(p)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListCertificates(w, buildListRequest(rawQuery))
|
||||
|
||||
assertSafeResponse(t, w, "ListCertificates/"+f)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Request body abuse (Tier 1D) ----------
|
||||
|
||||
// TestCreateCertificate_BodyAbuse sends adversarial JSON bodies to
|
||||
// POST /api/v1/certificates. Every case must respond with 400 (not 500,
|
||||
// not 200). This proves we reject malformed input before reaching the
|
||||
// service layer.
|
||||
func TestCreateCertificate_BodyAbuse(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
body string
|
||||
}{
|
||||
{"null_body", "null"},
|
||||
{"empty_body", ""},
|
||||
{"not_json", "not json at all"},
|
||||
{"truncated_json", `{"common_name":"exa`},
|
||||
{"unclosed_object", `{"common_name":"example.com"`},
|
||||
{"array_not_object", `["example.com"]`},
|
||||
{"number_not_object", `42`},
|
||||
{"string_not_object", `"hello"`},
|
||||
{"boolean_not_object", `true`},
|
||||
{"duplicate_keys", `{"common_name":"evil.com","common_name":"example.com"}`},
|
||||
{"unicode_bom", "\ufeff{\"common_name\":\"example.com\"}"},
|
||||
{"deep_nesting", strings.Repeat("{\"x\":", 100) + "null" + strings.Repeat("}", 100)},
|
||||
{"nested_array_bomb", `{"common_name":"x","sans":[[[[[[[[[[]]]]]]]]]]}`},
|
||||
{"sql_injection_cn", `{"common_name":"'; DROP TABLE managed_certificates;--"}`},
|
||||
{"empty_cn", `{"common_name":""}`},
|
||||
{"null_cn", `{"common_name":null}`},
|
||||
{"whitespace_cn", `{"common_name":" "}`},
|
||||
{"cn_too_long", fmt.Sprintf(`{"common_name":%q}`, strings.Repeat("a", 500))},
|
||||
{"cn_path_traversal", `{"common_name":"../../etc/passwd"}`},
|
||||
{"cn_null_byte", "{\"common_name\":\"example\\u0000.com\"}"},
|
||||
{"cn_newline", "{\"common_name\":\"example\\n.com\"}"},
|
||||
{"cn_only_missing_others", `{"common_name":"example.com"}`},
|
||||
{"extra_unknown_fields", `{"common_name":"example.com","__proto__":{"polluted":true},"eval":"alert(1)"}`},
|
||||
{"unicode_homoglyph_cn", "{\"common_name\":\"ex\u0430mple.com\"}"}, // Cyrillic а
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked on %q: %v", tc.name, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.CreateCertificateFn = func(cert domain.ManagedCertificate) (*domain.ManagedCertificate, error) {
|
||||
// If we ever reach this, the handler accepted a malformed
|
||||
// body. Return a sentinel that passes but flag it.
|
||||
c := cert
|
||||
c.ID = "mc-accepted"
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/certificates", bytes.NewBufferString(tc.body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
handler.CreateCertificate(w, req)
|
||||
|
||||
assertSafeResponse(t, w, "CreateCertificate/"+tc.name)
|
||||
// Must NOT be 201 — all these bodies should be rejected.
|
||||
if w.Code == http.StatusCreated {
|
||||
t.Errorf("%s: handler accepted malformed body (201) body=%q", tc.name, w.Body.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCreateCertificate_HugeBody sends a 2 MiB JSON body. The body-limit
|
||||
// middleware is not in this handler-unit test, so we just verify the handler
|
||||
// doesn't OOM/panic on a large but well-formed body.
|
||||
func TestCreateCertificate_HugeBody(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked on huge body: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
// 2 MiB of SANs — well-formed JSON, technically valid, just huge.
|
||||
var sb strings.Builder
|
||||
sb.WriteString(`{"common_name":"example.com","owner_id":"o","team_id":"t","issuer_id":"iss","name":"n","renewal_policy_id":"rp","sans":[`)
|
||||
for i := 0; i < 20000; i++ {
|
||||
if i > 0 {
|
||||
sb.WriteByte(',')
|
||||
}
|
||||
fmt.Fprintf(&sb, `"host%d.example.com"`, i)
|
||||
}
|
||||
sb.WriteString(`]}`)
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.CreateCertificateFn = func(cert domain.ManagedCertificate) (*domain.ManagedCertificate, error) {
|
||||
c := cert
|
||||
c.ID = "mc-huge"
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/certificates", strings.NewReader(sb.String()))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
handler.CreateCertificate(w, req)
|
||||
|
||||
assertSafeResponse(t, w, "CreateCertificate/huge_body")
|
||||
}
|
||||
|
||||
// ---------- Revocation reason abuse (Tier 1E) ----------
|
||||
|
||||
// TestRevokeCertificate_ReasonAbuse sends adversarial revocation reasons to
|
||||
// POST /api/v1/certificates/{id}/revoke. The handler forwards the reason
|
||||
// string to the service layer, which validates against RFC 5280. Errors
|
||||
// from the service containing "invalid revocation reason" must map to 400,
|
||||
// never 500.
|
||||
func TestRevokeCertificate_ReasonAbuse(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
body string
|
||||
}{
|
||||
{"empty_reason", `{"reason":""}`},
|
||||
{"null_reason", `{"reason":null}`},
|
||||
{"nonexistent_reason", `{"reason":"totally made up"}`},
|
||||
{"case_variant", `{"reason":"KEYCOMPROMISE"}`},
|
||||
{"with_spaces", `{"reason":"key compromise"}`},
|
||||
{"with_dashes", `{"reason":"key-compromise"}`},
|
||||
{"mixed_case", `{"reason":"KeyCompromise"}`},
|
||||
{"lowercase_valid", `{"reason":"keycompromise"}`},
|
||||
{"unicode_homoglyph", "{\"reason\":\"keyCompr\u043emise\"}"},
|
||||
{"sql_injection", `{"reason":"keyCompromise';DROP TABLE revocations--"}`},
|
||||
{"very_long", fmt.Sprintf(`{"reason":%q}`, strings.Repeat("a", 10000))},
|
||||
{"integer_reason", `{"reason":1}`},
|
||||
{"array_reason", `{"reason":["keyCompromise"]}`},
|
||||
{"object_reason", `{"reason":{"code":1}}`},
|
||||
{"extra_fields", `{"reason":"keyCompromise","admin":true,"bypass":true}`},
|
||||
{"no_body", ``},
|
||||
{"malformed_json", `{"reason":`},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
t.Fatalf("panicked on %q: %v", tc.name, r)
|
||||
}
|
||||
}()
|
||||
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
// The mock always returns "invalid revocation reason" so we
|
||||
// verify the handler's errMsg→status mapping turns it into a 400.
|
||||
mock.RevokeCertificateFn = func(id string, reason string) error {
|
||||
// The service uses domain.IsValidRevocationReason. If we got
|
||||
// through to here with something bogus, simulate a real
|
||||
// service error.
|
||||
return fmt.Errorf("invalid revocation reason: %q", reason)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/certificates/mc-001/revoke", bytes.NewBufferString(tc.body))
|
||||
req.URL.Path = "/api/v1/certificates/mc-001/revoke"
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
handler.RevokeCertificate(w, req)
|
||||
|
||||
assertSafeResponse(t, w, "RevokeCertificate/"+tc.name)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRevokeCertificate_AlreadyRevoked locks in the specific error->status
|
||||
// mapping for "already revoked". The handler uses substring matching on the
|
||||
// service error message, which is fragile — this test catches regressions.
|
||||
func TestRevokeCertificate_AlreadyRevoked(t *testing.T) {
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.RevokeCertificateFn = func(id string, reason string) error {
|
||||
return fmt.Errorf("cannot revoke: certificate is already revoked")
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/certificates/mc-001/revoke", strings.NewReader(`{"reason":"keyCompromise"}`))
|
||||
req.URL.Path = "/api/v1/certificates/mc-001/revoke"
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
handler.RevokeCertificate(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400 for already-revoked, got %d (body=%q)", w.Code, w.Body.String())
|
||||
}
|
||||
assertSafeResponse(t, w, "RevokeCertificate/already_revoked")
|
||||
}
|
||||
|
||||
// TestRevokeCertificate_NotFound verifies 404 mapping.
|
||||
func TestRevokeCertificate_NotFound(t *testing.T) {
|
||||
handler, mock := newCertHandlerWithMock()
|
||||
mock.RevokeCertificateFn = func(id string, reason string) error {
|
||||
return fmt.Errorf("certificate not found")
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/certificates/mc-missing/revoke", strings.NewReader(`{"reason":"keyCompromise"}`))
|
||||
req.URL.Path = "/api/v1/certificates/mc-missing/revoke"
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
handler.RevokeCertificate(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("expected 404 for not-found, got %d (body=%q)", w.Code, w.Body.String())
|
||||
}
|
||||
assertSafeResponse(t, w, "RevokeCertificate/not_found")
|
||||
}
|
||||
@@ -0,0 +1,419 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
"github.com/shankar0123/certctl/internal/api/middleware"
|
||||
)
|
||||
|
||||
// mockAuditService implements AuditService for testing.
|
||||
type mockAuditService struct {
|
||||
listFunc func(page, perPage int) ([]domain.AuditEvent, int64, error)
|
||||
getFunc func(id string) (*domain.AuditEvent, error)
|
||||
}
|
||||
|
||||
func (m *mockAuditService) ListAuditEvents(page, perPage int) ([]domain.AuditEvent, int64, error) {
|
||||
if m.listFunc != nil {
|
||||
return m.listFunc(page, perPage)
|
||||
}
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
func (m *mockAuditService) GetAuditEvent(id string) (*domain.AuditEvent, error) {
|
||||
if m.getFunc != nil {
|
||||
return m.getFunc(id)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func TestListAuditEvents_Success(t *testing.T) {
|
||||
events := []domain.AuditEvent{
|
||||
{
|
||||
ID: "ev-1",
|
||||
Action: "certificate_issued",
|
||||
Actor: "user@example.com",
|
||||
ActorType: domain.ActorTypeUser,
|
||||
ResourceID: "mc-api-prod",
|
||||
ResourceType: "Certificate",
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
{
|
||||
ID: "ev-2",
|
||||
Action: "certificate_renewed",
|
||||
Actor: "user@example.com",
|
||||
ActorType: domain.ActorTypeUser,
|
||||
ResourceID: "mc-api-prod",
|
||||
ResourceType: "Certificate",
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
mockSvc := &mockAuditService{
|
||||
listFunc: func(page, perPage int) ([]domain.AuditEvent, int64, error) {
|
||||
if page != 1 || perPage != 50 {
|
||||
t.Errorf("ListAuditEvents called with page=%d, perPage=%d, expected 1, 50", page, perPage)
|
||||
}
|
||||
return events, 2, nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/audit", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
// Add request ID to context
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListAuditEvents(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("ListAuditEvents returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
var result PagedResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result.Total != 2 {
|
||||
t.Errorf("Total = %d, want 2", result.Total)
|
||||
}
|
||||
|
||||
if result.Page != 1 {
|
||||
t.Errorf("Page = %d, want 1", result.Page)
|
||||
}
|
||||
|
||||
if result.PerPage != 50 {
|
||||
t.Errorf("PerPage = %d, want 50", result.PerPage)
|
||||
}
|
||||
|
||||
// Check data is present
|
||||
if result.Data == nil {
|
||||
t.Error("Data is nil, want events slice")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAuditEvents_WithPagination(t *testing.T) {
|
||||
events := []domain.AuditEvent{
|
||||
{
|
||||
ID: "ev-5",
|
||||
Action: "certificate_issued",
|
||||
Actor: "user@example.com",
|
||||
ActorType: domain.ActorTypeUser,
|
||||
ResourceID: "mc-api-prod",
|
||||
ResourceType: "Certificate",
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
mockSvc := &mockAuditService{
|
||||
listFunc: func(page, perPage int) ([]domain.AuditEvent, int64, error) {
|
||||
if page != 2 || perPage != 25 {
|
||||
t.Errorf("ListAuditEvents called with page=%d, perPage=%d, expected 2, 25", page, perPage)
|
||||
}
|
||||
return events, 100, nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/audit?page=2&per_page=25", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListAuditEvents(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("ListAuditEvents returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
var result PagedResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result.Page != 2 {
|
||||
t.Errorf("Page = %d, want 2", result.Page)
|
||||
}
|
||||
|
||||
if result.PerPage != 25 {
|
||||
t.Errorf("PerPage = %d, want 25", result.PerPage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAuditEvents_PerPageMaxLimit(t *testing.T) {
|
||||
mockSvc := &mockAuditService{
|
||||
listFunc: func(page, perPage int) ([]domain.AuditEvent, int64, error) {
|
||||
// Should be capped at 500
|
||||
if perPage > 500 {
|
||||
t.Errorf("perPage = %d, expected <= 500", perPage)
|
||||
}
|
||||
return []domain.AuditEvent{}, 0, nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/audit?per_page=1000", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListAuditEvents(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("ListAuditEvents returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
var result PagedResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result.PerPage > 500 {
|
||||
t.Errorf("PerPage = %d, want <= 500", result.PerPage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAuditEvents_EmptyResult(t *testing.T) {
|
||||
mockSvc := &mockAuditService{
|
||||
listFunc: func(page, perPage int) ([]domain.AuditEvent, int64, error) {
|
||||
return []domain.AuditEvent{}, 0, nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/audit", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListAuditEvents(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("ListAuditEvents returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
var result PagedResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result.Total != 0 {
|
||||
t.Errorf("Total = %d, want 0", result.Total)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAuditEvents_ServiceError(t *testing.T) {
|
||||
mockSvc := &mockAuditService{
|
||||
listFunc: func(page, perPage int) ([]domain.AuditEvent, int64, error) {
|
||||
return nil, 0, errors.New("database error")
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/audit", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListAuditEvents(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusInternalServerError {
|
||||
t.Errorf("ListAuditEvents returned status %d, want %d", status, http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
var errResp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil {
|
||||
t.Fatalf("failed to decode error response: %v", err)
|
||||
}
|
||||
|
||||
if errResp.Message != "Failed to list audit events" {
|
||||
t.Errorf("Message = %q, want 'Failed to list audit events'", errResp.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListAuditEvents_MethodNotAllowed(t *testing.T) {
|
||||
mockSvc := &mockAuditService{}
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, "/api/v1/audit", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.ListAuditEvents(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusMethodNotAllowed {
|
||||
t.Errorf("ListAuditEvents returned status %d, want %d", status, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAuditEvent_Success(t *testing.T) {
|
||||
event := &domain.AuditEvent{
|
||||
ID: "ev-123",
|
||||
Action: "certificate_issued",
|
||||
Actor: "user@example.com",
|
||||
ActorType: domain.ActorTypeUser,
|
||||
ResourceID: "mc-api-prod",
|
||||
ResourceType: "Certificate",
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
|
||||
mockSvc := &mockAuditService{
|
||||
getFunc: func(id string) (*domain.AuditEvent, error) {
|
||||
if id != "ev-123" {
|
||||
t.Errorf("GetAuditEvent called with id=%q, expected ev-123", id)
|
||||
}
|
||||
return event, nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/audit/ev-123", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.GetAuditEvent(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("GetAuditEvent returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
var result domain.AuditEvent
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result.ID != "ev-123" {
|
||||
t.Errorf("ID = %q, want ev-123", result.ID)
|
||||
}
|
||||
|
||||
if result.Action != "certificate_issued" {
|
||||
t.Errorf("Action = %q, want certificate_issued", result.Action)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAuditEvent_NotFound(t *testing.T) {
|
||||
mockSvc := &mockAuditService{
|
||||
getFunc: func(id string) (*domain.AuditEvent, error) {
|
||||
return nil, errors.New("not found")
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/audit/nonexistent", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.GetAuditEvent(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusNotFound {
|
||||
t.Errorf("GetAuditEvent returned status %d, want %d", status, http.StatusNotFound)
|
||||
}
|
||||
|
||||
var errResp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil {
|
||||
t.Fatalf("failed to decode error response: %v", err)
|
||||
}
|
||||
|
||||
if errResp.Message != "Audit event not found" {
|
||||
t.Errorf("Message = %q, want 'Audit event not found'", errResp.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAuditEvent_MethodNotAllowed(t *testing.T) {
|
||||
mockSvc := &mockAuditService{}
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodDelete, "/api/v1/audit/ev-123", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.GetAuditEvent(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusMethodNotAllowed {
|
||||
t.Errorf("GetAuditEvent returned status %d, want %d", status, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAuditEvent_EmptyID(t *testing.T) {
|
||||
mockSvc := &mockAuditService{}
|
||||
handler := NewAuditHandler(mockSvc)
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/audit/", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(req.Context(), middleware.RequestIDKey{}, "test-req-id")
|
||||
req = req.WithContext(ctx)
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.GetAuditEvent(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusBadRequest {
|
||||
t.Errorf("GetAuditEvent returned status %d, want %d", status, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var errResp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil {
|
||||
t.Fatalf("failed to decode error response: %v", err)
|
||||
}
|
||||
|
||||
if errResp.Message != "Audit event ID is required" {
|
||||
t.Errorf("Message = %q, want 'Audit event ID is required'", errResp.Message)
|
||||
}
|
||||
}
|
||||
+8
-134
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/shankar0123/certctl/internal/api/middleware"
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
"github.com/shankar0123/certctl/internal/pkcs7"
|
||||
)
|
||||
|
||||
// ESTService defines the service interface for EST enrollment operations.
|
||||
@@ -67,7 +68,7 @@ func (h ESTHandler) CACerts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Parse PEM to DER for PKCS#7 encoding
|
||||
derCerts, err := pemToDERChain(caCertPEM)
|
||||
derCerts, err := pkcs7.PEMToDERChain(caCertPEM)
|
||||
if err != nil {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to encode CA certificates", requestID)
|
||||
@@ -75,7 +76,7 @@ func (h ESTHandler) CACerts(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Build a simple PKCS#7 SignedData (certs-only, degenerate) structure
|
||||
pkcs7Data, err := buildCertsOnlyPKCS7(derCerts)
|
||||
pkcs7Data, err := pkcs7.BuildCertsOnlyPKCS7(derCerts)
|
||||
if err != nil {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to build PKCS#7 response", requestID)
|
||||
@@ -237,7 +238,7 @@ func (h ESTHandler) writeCertResponse(w http.ResponseWriter, result *domain.ESTE
|
||||
var derCerts [][]byte
|
||||
|
||||
// Add the issued certificate
|
||||
certDER, err := pemToDERChain(result.CertPEM)
|
||||
certDER, err := pkcs7.PEMToDERChain(result.CertPEM)
|
||||
if err != nil || len(certDER) == 0 {
|
||||
http.Error(w, "Failed to encode certificate", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -246,14 +247,14 @@ func (h ESTHandler) writeCertResponse(w http.ResponseWriter, result *domain.ESTE
|
||||
|
||||
// Add the CA chain if present
|
||||
if result.ChainPEM != "" {
|
||||
chainDER, err := pemToDERChain(result.ChainPEM)
|
||||
chainDER, err := pkcs7.PEMToDERChain(result.ChainPEM)
|
||||
if err == nil {
|
||||
derCerts = append(derCerts, chainDER...)
|
||||
}
|
||||
}
|
||||
|
||||
// Build PKCS#7 certs-only
|
||||
pkcs7Data, err := buildCertsOnlyPKCS7(derCerts)
|
||||
pkcs7Data, err := pkcs7.BuildCertsOnlyPKCS7(derCerts)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to build PKCS#7 response", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -273,132 +274,5 @@ func (h ESTHandler) writeCertResponse(w http.ResponseWriter, result *domain.ESTE
|
||||
}
|
||||
}
|
||||
|
||||
// pemToDERChain converts PEM-encoded certificates to a slice of DER-encoded certificates.
|
||||
func pemToDERChain(pemData string) ([][]byte, error) {
|
||||
var derCerts [][]byte
|
||||
rest := []byte(pemData)
|
||||
for {
|
||||
var block *pem.Block
|
||||
block, rest = pem.Decode(rest)
|
||||
if block == nil {
|
||||
break
|
||||
}
|
||||
if block.Type == "CERTIFICATE" {
|
||||
derCerts = append(derCerts, block.Bytes)
|
||||
}
|
||||
}
|
||||
if len(derCerts) == 0 {
|
||||
return nil, fmt.Errorf("no certificates found in PEM data")
|
||||
}
|
||||
return derCerts, nil
|
||||
}
|
||||
|
||||
// buildCertsOnlyPKCS7 creates a degenerate PKCS#7 SignedData structure containing only certificates.
|
||||
// This is the "certs-only" format specified in RFC 7030 Section 4.1.3 for /cacerts responses
|
||||
// and enrollment responses.
|
||||
//
|
||||
// ASN.1 structure (simplified):
|
||||
//
|
||||
// ContentInfo {
|
||||
// contentType: signedData (1.2.840.113549.1.7.2)
|
||||
// content: SignedData {
|
||||
// version: 1
|
||||
// digestAlgorithms: {} (empty)
|
||||
// encapContentInfo: { contentType: data (1.2.840.113549.1.7.1) }
|
||||
// certificates: [cert1, cert2, ...]
|
||||
// signerInfos: {} (empty)
|
||||
// }
|
||||
// }
|
||||
func buildCertsOnlyPKCS7(derCerts [][]byte) ([]byte, error) {
|
||||
// We build the ASN.1 manually to avoid pulling in a PKCS#7 library.
|
||||
// This is a well-defined, static structure — no signing needed.
|
||||
|
||||
// OID for signedData: 1.2.840.113549.1.7.2
|
||||
oidSignedData := []byte{0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02}
|
||||
// OID for data: 1.2.840.113549.1.7.1
|
||||
oidData := []byte{0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01}
|
||||
|
||||
// Build certificates [0] IMPLICIT SET OF Certificate
|
||||
var certsContent []byte
|
||||
for _, cert := range derCerts {
|
||||
certsContent = append(certsContent, cert...)
|
||||
}
|
||||
certsField := asn1WrapImplicit(0, certsContent)
|
||||
|
||||
// Build encapContentInfo: SEQUENCE { OID data }
|
||||
encapContentInfo := asn1WrapSequence(oidData)
|
||||
|
||||
// Build digestAlgorithms: SET {} (empty)
|
||||
digestAlgorithms := asn1WrapSet(nil)
|
||||
|
||||
// Build signerInfos: SET {} (empty)
|
||||
signerInfos := asn1WrapSet(nil)
|
||||
|
||||
// Version: INTEGER 1
|
||||
version := []byte{0x02, 0x01, 0x01}
|
||||
|
||||
// Build SignedData SEQUENCE
|
||||
var signedDataContent []byte
|
||||
signedDataContent = append(signedDataContent, version...)
|
||||
signedDataContent = append(signedDataContent, digestAlgorithms...)
|
||||
signedDataContent = append(signedDataContent, encapContentInfo...)
|
||||
signedDataContent = append(signedDataContent, certsField...)
|
||||
signedDataContent = append(signedDataContent, signerInfos...)
|
||||
signedData := asn1WrapSequence(signedDataContent)
|
||||
|
||||
// Wrap in [0] EXPLICIT for ContentInfo.content
|
||||
contentField := asn1WrapExplicit(0, signedData)
|
||||
|
||||
// Build ContentInfo SEQUENCE
|
||||
var contentInfoContent []byte
|
||||
contentInfoContent = append(contentInfoContent, oidSignedData...)
|
||||
contentInfoContent = append(contentInfoContent, contentField...)
|
||||
contentInfo := asn1WrapSequence(contentInfoContent)
|
||||
|
||||
return contentInfo, nil
|
||||
}
|
||||
|
||||
// asn1WrapSequence wraps content in an ASN.1 SEQUENCE tag (0x30).
|
||||
func asn1WrapSequence(content []byte) []byte {
|
||||
return asn1Wrap(0x30, content)
|
||||
}
|
||||
|
||||
// asn1WrapSet wraps content in an ASN.1 SET tag (0x31).
|
||||
func asn1WrapSet(content []byte) []byte {
|
||||
return asn1Wrap(0x31, content)
|
||||
}
|
||||
|
||||
// asn1WrapExplicit wraps content in an ASN.1 context-specific EXPLICIT tag.
|
||||
func asn1WrapExplicit(tag int, content []byte) []byte {
|
||||
return asn1Wrap(byte(0xa0|tag), content)
|
||||
}
|
||||
|
||||
// asn1WrapImplicit wraps content in an ASN.1 context-specific IMPLICIT CONSTRUCTED tag.
|
||||
func asn1WrapImplicit(tag int, content []byte) []byte {
|
||||
return asn1Wrap(byte(0xa0|tag), content)
|
||||
}
|
||||
|
||||
// asn1Wrap wraps content with an ASN.1 tag and length.
|
||||
func asn1Wrap(tag byte, content []byte) []byte {
|
||||
length := len(content)
|
||||
var result []byte
|
||||
result = append(result, tag)
|
||||
result = append(result, asn1EncodeLength(length)...)
|
||||
result = append(result, content...)
|
||||
return result
|
||||
}
|
||||
|
||||
// asn1EncodeLength encodes a length in ASN.1 DER format.
|
||||
func asn1EncodeLength(length int) []byte {
|
||||
if length < 0x80 {
|
||||
return []byte{byte(length)}
|
||||
}
|
||||
// Long form
|
||||
var lengthBytes []byte
|
||||
l := length
|
||||
for l > 0 {
|
||||
lengthBytes = append([]byte{byte(l & 0xff)}, lengthBytes...)
|
||||
l >>= 8
|
||||
}
|
||||
return append([]byte{byte(0x80 | len(lengthBytes))}, lengthBytes...)
|
||||
}
|
||||
// NOTE: PKCS#7 helpers (BuildCertsOnlyPKCS7, PEMToDERChain, ASN.1 wrappers)
|
||||
// are in the shared internal/pkcs7 package, used by both EST and SCEP handlers.
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
"github.com/shankar0123/certctl/internal/pkcs7"
|
||||
)
|
||||
|
||||
// mockESTService implements ESTService for testing.
|
||||
@@ -338,12 +339,12 @@ func TestESTCSRAttrs_MethodNotAllowed(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCertsOnlyPKCS7(t *testing.T) {
|
||||
// Test with a dummy DER certificate
|
||||
func TestBuildCertsOnlyPKCS7_ViaSharedPackage(t *testing.T) {
|
||||
// Test with a dummy DER certificate via shared pkcs7 package
|
||||
dummyCert := []byte{0x30, 0x82, 0x01, 0x00} // minimal ASN.1 SEQUENCE
|
||||
result, err := buildCertsOnlyPKCS7([][]byte{dummyCert})
|
||||
result, err := pkcs7.BuildCertsOnlyPKCS7([][]byte{dummyCert})
|
||||
if err != nil {
|
||||
t.Fatalf("buildCertsOnlyPKCS7 failed: %v", err)
|
||||
t.Fatalf("BuildCertsOnlyPKCS7 failed: %v", err)
|
||||
}
|
||||
if len(result) == 0 {
|
||||
t.Error("expected non-empty PKCS#7 output")
|
||||
@@ -354,49 +355,24 @@ func TestBuildCertsOnlyPKCS7(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPemToDERChain(t *testing.T) {
|
||||
func TestPemToDERChain_ViaSharedPackage(t *testing.T) {
|
||||
pemData := generateTestCertPEM(t)
|
||||
certs, err := pemToDERChain(pemData)
|
||||
certs, err := pkcs7.PEMToDERChain(pemData)
|
||||
if err != nil {
|
||||
t.Fatalf("pemToDERChain failed: %v", err)
|
||||
t.Fatalf("PEMToDERChain failed: %v", err)
|
||||
}
|
||||
if len(certs) != 1 {
|
||||
t.Errorf("expected 1 cert, got %d", len(certs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPemToDERChain_NoCerts(t *testing.T) {
|
||||
_, err := pemToDERChain("not a PEM")
|
||||
func TestPemToDERChain_NoCerts_ViaSharedPackage(t *testing.T) {
|
||||
_, err := pkcs7.PEMToDERChain("not a PEM")
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid PEM")
|
||||
}
|
||||
}
|
||||
|
||||
func TestASN1EncodeLength(t *testing.T) {
|
||||
tests := []struct {
|
||||
length int
|
||||
expected []byte
|
||||
}{
|
||||
{0, []byte{0x00}},
|
||||
{1, []byte{0x01}},
|
||||
{127, []byte{0x7f}},
|
||||
{128, []byte{0x81, 0x80}},
|
||||
{256, []byte{0x82, 0x01, 0x00}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
result := asn1EncodeLength(tt.length)
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("asn1EncodeLength(%d): expected %d bytes, got %d", tt.length, len(tt.expected), len(result))
|
||||
continue
|
||||
}
|
||||
for i := range result {
|
||||
if result[i] != tt.expected[i] {
|
||||
t.Errorf("asn1EncodeLength(%d): byte %d: expected 0x%02x, got 0x%02x", tt.length, i, tt.expected[i], result[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestESTCSRAttrs_ServiceError(t *testing.T) {
|
||||
svc := &mockESTService{
|
||||
CSRAttrsErr: errors.New("service error"),
|
||||
|
||||
@@ -0,0 +1,234 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHealth_ReturnsOK(t *testing.T) {
|
||||
handler := NewHealthHandler("api-key")
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/health", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.Health(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("Health handler returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check content type
|
||||
if ct := w.Header().Get("Content-Type"); ct != "application/json" {
|
||||
t.Errorf("Content-Type = %q, want application/json", ct)
|
||||
}
|
||||
|
||||
// Check response body
|
||||
var result map[string]string
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result["status"] != "healthy" {
|
||||
t.Errorf("status = %q, want healthy", result["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealth_MethodNotAllowed(t *testing.T) {
|
||||
handler := NewHealthHandler("api-key")
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, "/health", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.Health(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusMethodNotAllowed {
|
||||
t.Errorf("Health handler returned status %d, want %d", status, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReady_ReturnsOK(t *testing.T) {
|
||||
handler := NewHealthHandler("api-key")
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/ready", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.Ready(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("Ready handler returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check content type
|
||||
if ct := w.Header().Get("Content-Type"); ct != "application/json" {
|
||||
t.Errorf("Content-Type = %q, want application/json", ct)
|
||||
}
|
||||
|
||||
// Check response body
|
||||
var result map[string]string
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result["status"] != "ready" {
|
||||
t.Errorf("status = %q, want ready", result["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestReady_MethodNotAllowed(t *testing.T) {
|
||||
handler := NewHealthHandler("api-key")
|
||||
|
||||
req, err := http.NewRequest(http.MethodDelete, "/ready", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.Ready(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusMethodNotAllowed {
|
||||
t.Errorf("Ready handler returned status %d, want %d", status, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthInfo_ReturnsAuthType_APIKey(t *testing.T) {
|
||||
handler := NewHealthHandler("api-key")
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/auth/info", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.AuthInfo(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("AuthInfo handler returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result["auth_type"] != "api-key" {
|
||||
t.Errorf("auth_type = %q, want api-key", result["auth_type"])
|
||||
}
|
||||
|
||||
if required, ok := result["required"].(bool); !ok || !required {
|
||||
t.Errorf("required = %v, want true", result["required"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthInfo_ReturnsAuthType_None(t *testing.T) {
|
||||
handler := NewHealthHandler("none")
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/auth/info", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.AuthInfo(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("AuthInfo handler returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result["auth_type"] != "none" {
|
||||
t.Errorf("auth_type = %q, want none", result["auth_type"])
|
||||
}
|
||||
|
||||
if required, ok := result["required"].(bool); !ok || required {
|
||||
t.Errorf("required = %v, want false", result["required"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthInfo_ReturnsAuthType_JWT(t *testing.T) {
|
||||
handler := NewHealthHandler("jwt")
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/auth/info", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.AuthInfo(w, req)
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result["auth_type"] != "jwt" {
|
||||
t.Errorf("auth_type = %q, want jwt", result["auth_type"])
|
||||
}
|
||||
|
||||
if required, ok := result["required"].(bool); !ok || !required {
|
||||
t.Errorf("required = %v, want true", result["required"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthCheck_ReturnsOK(t *testing.T) {
|
||||
handler := NewHealthHandler("api-key")
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, "/api/v1/auth/check", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.AuthCheck(w, req)
|
||||
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Errorf("AuthCheck handler returned status %d, want %d", status, http.StatusOK)
|
||||
}
|
||||
|
||||
// Check content type
|
||||
if ct := w.Header().Get("Content-Type"); ct != "application/json" {
|
||||
t.Errorf("Content-Type = %q, want application/json", ct)
|
||||
}
|
||||
|
||||
// Check response body
|
||||
var result map[string]string
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result["status"] != "authenticated" {
|
||||
t.Errorf("status = %q, want authenticated", result["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthCheck_MethodNotAllowed(t *testing.T) {
|
||||
handler := NewHealthHandler("api-key")
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, "/api/v1/auth/check", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("NewRequest failed: %v", err)
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
handler.AuthCheck(w, req)
|
||||
|
||||
// AuthCheck doesn't explicitly check method, so it will return 200
|
||||
// But let's verify the response is still correct
|
||||
if status := w.Code; status != http.StatusOK {
|
||||
t.Logf("AuthCheck returned status %d (note: method not enforced in handler)", status)
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,10 @@ package handler
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -324,6 +326,122 @@ func TestCreateIssuer_NameTooLong(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateIssuer_DuplicateName(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
CreateIssuerFn: func(issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
return nil, fmt.Errorf("failed to create issuer: duplicate key value violates unique constraint \"issuers_name_key\"")
|
||||
},
|
||||
}
|
||||
|
||||
body := map[string]interface{}{
|
||||
"name": "ACME Issuer",
|
||||
"type": "ACME",
|
||||
}
|
||||
bodyBytes, _ := json.Marshal(body)
|
||||
|
||||
handler := NewIssuerHandler(mock)
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/issuers", bytes.NewReader(bodyBytes))
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler.CreateIssuer(w, req)
|
||||
|
||||
if w.Code != http.StatusConflict {
|
||||
t.Fatalf("expected status 409, got %d", w.Code)
|
||||
}
|
||||
|
||||
var resp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
if !strings.Contains(resp.Message, "already exists") {
|
||||
t.Errorf("expected message to contain 'already exists', got %q", resp.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateIssuer_UnsupportedType(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
CreateIssuerFn: func(issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
return nil, fmt.Errorf("unsupported issuer type: FakeCA")
|
||||
},
|
||||
}
|
||||
|
||||
body := map[string]interface{}{
|
||||
"name": "Fake Issuer",
|
||||
"type": "FakeCA",
|
||||
}
|
||||
bodyBytes, _ := json.Marshal(body)
|
||||
|
||||
handler := NewIssuerHandler(mock)
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/issuers", bytes.NewReader(bodyBytes))
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler.CreateIssuer(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected status 400, got %d", w.Code)
|
||||
}
|
||||
|
||||
var resp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
if !strings.Contains(resp.Message, "unsupported issuer type") {
|
||||
t.Errorf("expected message to contain 'unsupported issuer type', got %q", resp.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateIssuer_GenericServiceError(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
CreateIssuerFn: func(issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
return nil, fmt.Errorf("failed to encrypt config: cipher error")
|
||||
},
|
||||
}
|
||||
|
||||
body := map[string]interface{}{
|
||||
"name": "Some Issuer",
|
||||
"type": "ACME",
|
||||
}
|
||||
bodyBytes, _ := json.Marshal(body)
|
||||
|
||||
handler := NewIssuerHandler(mock)
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/issuers", bytes.NewReader(bodyBytes))
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler.CreateIssuer(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("expected status 500, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateIssuer_DuplicateName(t *testing.T) {
|
||||
mock := &MockIssuerService{
|
||||
UpdateIssuerFn: func(id string, issuer domain.Issuer) (*domain.Issuer, error) {
|
||||
return nil, fmt.Errorf("failed to update issuer: duplicate key value violates unique constraint")
|
||||
},
|
||||
}
|
||||
|
||||
body := map[string]interface{}{
|
||||
"name": "Existing Name",
|
||||
"type": "ACME",
|
||||
}
|
||||
bodyBytes, _ := json.Marshal(body)
|
||||
|
||||
handler := NewIssuerHandler(mock)
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/v1/issuers/iss-test", bytes.NewReader(bodyBytes))
|
||||
req = req.WithContext(contextWithRequestID())
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
handler.UpdateIssuer(w, req)
|
||||
|
||||
if w.Code != http.StatusConflict {
|
||||
t.Fatalf("expected status 409, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteIssuer_Success(t *testing.T) {
|
||||
var deletedID string
|
||||
mock := &MockIssuerService{
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -22,12 +23,18 @@ type IssuerService interface {
|
||||
|
||||
// IssuerHandler handles HTTP requests for issuer operations.
|
||||
type IssuerHandler struct {
|
||||
svc IssuerService
|
||||
svc IssuerService
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewIssuerHandler creates a new IssuerHandler with a service dependency.
|
||||
func NewIssuerHandler(svc IssuerService) IssuerHandler {
|
||||
return IssuerHandler{svc: svc}
|
||||
return IssuerHandler{svc: svc, logger: slog.Default()}
|
||||
}
|
||||
|
||||
// NewIssuerHandlerWithLogger creates a new IssuerHandler with a custom logger.
|
||||
func NewIssuerHandlerWithLogger(svc IssuerService, logger *slog.Logger) IssuerHandler {
|
||||
return IssuerHandler{svc: svc, logger: logger}
|
||||
}
|
||||
|
||||
// ListIssuers lists all configured issuers.
|
||||
@@ -127,7 +134,16 @@ func (h IssuerHandler) CreateIssuer(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
created, err := h.svc.CreateIssuer(issuer)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to create issuer", requestID)
|
||||
h.logger.Error("failed to create issuer", "error", err, "name", issuer.Name, "type", issuer.Type)
|
||||
errMsg := err.Error()
|
||||
switch {
|
||||
case strings.Contains(errMsg, "unique") || strings.Contains(errMsg, "duplicate"):
|
||||
ErrorWithRequestID(w, http.StatusConflict, "An issuer with this name already exists", requestID)
|
||||
case strings.Contains(errMsg, "unsupported issuer type"):
|
||||
ErrorWithRequestID(w, http.StatusBadRequest, errMsg, requestID)
|
||||
default:
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to create issuer", requestID)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -160,7 +176,16 @@ func (h IssuerHandler) UpdateIssuer(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
updated, err := h.svc.UpdateIssuer(id, issuer)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to update issuer", requestID)
|
||||
h.logger.Error("failed to update issuer", "error", err, "id", id)
|
||||
errMsg := err.Error()
|
||||
switch {
|
||||
case strings.Contains(errMsg, "unique") || strings.Contains(errMsg, "duplicate"):
|
||||
ErrorWithRequestID(w, http.StatusConflict, "An issuer with this name already exists", requestID)
|
||||
case strings.Contains(errMsg, "not found"):
|
||||
ErrorWithRequestID(w, http.StatusNotFound, "Issuer not found", requestID)
|
||||
default:
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to update issuer", requestID)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,427 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestEncodeCursor_ProducesValidBase64(t *testing.T) {
|
||||
// Test that encodeCursor produces valid base64 with correct format
|
||||
originalTime := time.Date(2024, 3, 15, 10, 30, 45, 123456789, time.UTC)
|
||||
originalID := "cert-12345"
|
||||
|
||||
// Encode
|
||||
encoded := encodeCursor(originalTime, originalID)
|
||||
|
||||
// Verify it's valid base64
|
||||
decoded, err := base64.URLEncoding.DecodeString(encoded)
|
||||
if err != nil {
|
||||
t.Fatalf("encoded cursor is not valid base64: %v", err)
|
||||
}
|
||||
|
||||
// Verify contains both timestamp and ID
|
||||
decodedStr := string(decoded)
|
||||
if !strings.Contains(decodedStr, originalID) {
|
||||
t.Errorf("decoded cursor doesn't contain ID %q, got %q", originalID, decodedStr)
|
||||
}
|
||||
|
||||
// Verify it's not empty and has expected structure (timestamp:id)
|
||||
if !strings.Contains(decodedStr, ":") {
|
||||
t.Errorf("decoded cursor doesn't contain colon separator, got %q", decodedStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeCursor_DifferentTimes(t *testing.T) {
|
||||
id := "test-id"
|
||||
time1 := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
time2 := time.Date(2024, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
cursor1 := encodeCursor(time1, id)
|
||||
cursor2 := encodeCursor(time2, id)
|
||||
|
||||
// Different times should produce different cursors
|
||||
if cursor1 == cursor2 {
|
||||
t.Error("Different times produced identical cursors")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncodeCursor_DifferentIDs(t *testing.T) {
|
||||
now := time.Now()
|
||||
id1 := "cert-1"
|
||||
id2 := "cert-2"
|
||||
|
||||
cursor1 := encodeCursor(now, id1)
|
||||
cursor2 := encodeCursor(now, id2)
|
||||
|
||||
// Different IDs should produce different cursors
|
||||
if cursor1 == cursor2 {
|
||||
t.Error("Different IDs produced identical cursors")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeCursor_InvalidBase64(t *testing.T) {
|
||||
// Create the decodeCursor function from the closure - matching actual behavior
|
||||
decodeCursor := func(cursor string) (time.Time, string, error) {
|
||||
raw, err := base64.URLEncoding.DecodeString(cursor)
|
||||
if err != nil {
|
||||
return time.Time{}, "", err
|
||||
}
|
||||
parts := strings.SplitN(string(raw), ":", 2)
|
||||
if len(parts) != 2 {
|
||||
return time.Time{}, "", fmt.Errorf("invalid cursor format")
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, parts[0])
|
||||
if err != nil {
|
||||
return time.Time{}, "", err
|
||||
}
|
||||
return t, parts[1], nil
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
cursor string
|
||||
expectError bool
|
||||
}{
|
||||
{"invalid base64", "!!!invalid!!!", true},
|
||||
{"empty string", "", true},
|
||||
{"no colon separator", base64.URLEncoding.EncodeToString([]byte("no-separator-here")), true},
|
||||
{"invalid timestamp", base64.URLEncoding.EncodeToString([]byte("not-a-timestamp:id-123")), true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, _, err := decodeCursor(tt.cursor)
|
||||
if tt.expectError && err == nil {
|
||||
t.Error("expected error for invalid cursor, got nil")
|
||||
}
|
||||
if !tt.expectError && err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSON_SetsContentType(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
data := map[string]string{"key": "value"}
|
||||
|
||||
JSON(w, http.StatusOK, data)
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if contentType != "application/json" {
|
||||
t.Errorf("Content-Type = %q, want application/json", contentType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSON_SetsStatusCode(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
data := map[string]string{"key": "value"}
|
||||
|
||||
JSON(w, http.StatusCreated, data)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Errorf("Status code = %d, want %d", w.Code, http.StatusCreated)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSON_EncodesData(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
data := map[string]interface{}{
|
||||
"string": "value",
|
||||
"number": 42,
|
||||
"bool": true,
|
||||
"null": nil,
|
||||
}
|
||||
|
||||
JSON(w, http.StatusOK, data)
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(w.Body).Decode(&result); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
|
||||
if result["string"] != "value" {
|
||||
t.Errorf("string = %v, want value", result["string"])
|
||||
}
|
||||
|
||||
if result["number"] != float64(42) {
|
||||
t.Errorf("number = %v, want 42", result["number"])
|
||||
}
|
||||
|
||||
if result["bool"] != true {
|
||||
t.Errorf("bool = %v, want true", result["bool"])
|
||||
}
|
||||
|
||||
if result["null"] != nil {
|
||||
t.Errorf("null = %v, want nil", result["null"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_SetsStatusCode(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
Error(w, http.StatusBadRequest, "Invalid input")
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Status code = %d, want %d", w.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_SetsContentType(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
Error(w, http.StatusBadRequest, "Invalid input")
|
||||
|
||||
contentType := w.Header().Get("Content-Type")
|
||||
if contentType != "application/json" {
|
||||
t.Errorf("Content-Type = %q, want application/json", contentType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_IncludesMessage(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
message := "Something went wrong"
|
||||
|
||||
Error(w, http.StatusInternalServerError, message)
|
||||
|
||||
var errResp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil {
|
||||
t.Fatalf("failed to decode error response: %v", err)
|
||||
}
|
||||
|
||||
if errResp.Message != message {
|
||||
t.Errorf("Message = %q, want %q", errResp.Message, message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestError_IncludesStatusText(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
Error(w, http.StatusNotFound, "Resource not found")
|
||||
|
||||
var errResp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil {
|
||||
t.Fatalf("failed to decode error response: %v", err)
|
||||
}
|
||||
|
||||
if errResp.Error != http.StatusText(http.StatusNotFound) {
|
||||
t.Errorf("Error = %q, want %q", errResp.Error, http.StatusText(http.StatusNotFound))
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWithRequestID_SetsStatusCode(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ErrorWithRequestID(w, http.StatusBadRequest, "Invalid input", "req-123")
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Status code = %d, want %d", w.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWithRequestID_IncludesRequestID(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
requestID := "req-abc-def-ghi"
|
||||
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Server error", requestID)
|
||||
|
||||
var errResp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil {
|
||||
t.Fatalf("failed to decode error response: %v", err)
|
||||
}
|
||||
|
||||
if errResp.RequestID != requestID {
|
||||
t.Errorf("RequestID = %q, want %q", errResp.RequestID, requestID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorWithRequestID_IncludesMessage(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
message := "Database connection failed"
|
||||
|
||||
ErrorWithRequestID(w, http.StatusServiceUnavailable, message, "req-123")
|
||||
|
||||
var errResp ErrorResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&errResp); err != nil {
|
||||
t.Fatalf("failed to decode error response: %v", err)
|
||||
}
|
||||
|
||||
if errResp.Message != message {
|
||||
t.Errorf("Message = %q, want %q", errResp.Message, message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedResponse_Structure(t *testing.T) {
|
||||
response := PagedResponse{
|
||||
Data: []string{"item1", "item2"},
|
||||
Total: 100,
|
||||
Page: 2,
|
||||
PerPage: 50,
|
||||
}
|
||||
|
||||
data, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal response: %v", err)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if result["total"] != float64(100) {
|
||||
t.Errorf("total = %v, want 100", result["total"])
|
||||
}
|
||||
|
||||
if result["page"] != float64(2) {
|
||||
t.Errorf("page = %v, want 2", result["page"])
|
||||
}
|
||||
|
||||
if result["per_page"] != float64(50) {
|
||||
t.Errorf("per_page = %v, want 50", result["per_page"])
|
||||
}
|
||||
|
||||
if result["data"] == nil {
|
||||
t.Error("data is nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursorPagedResponse_Structure(t *testing.T) {
|
||||
response := CursorPagedResponse{
|
||||
Data: []string{"item1", "item2"},
|
||||
Total: 100,
|
||||
NextCursor: "abc123def456",
|
||||
PageSize: 50,
|
||||
}
|
||||
|
||||
data, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal response: %v", err)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(data, &result); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if result["total"] != float64(100) {
|
||||
t.Errorf("total = %v, want 100", result["total"])
|
||||
}
|
||||
|
||||
if result["next_cursor"] != "abc123def456" {
|
||||
t.Errorf("next_cursor = %v, want abc123def456", result["next_cursor"])
|
||||
}
|
||||
|
||||
if result["page_size"] != float64(50) {
|
||||
t.Errorf("page_size = %v, want 50", result["page_size"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursorPagedResponse_EmptyNextCursor(t *testing.T) {
|
||||
// When NextCursor is empty, it should be omitted from JSON
|
||||
response := CursorPagedResponse{
|
||||
Data: []string{},
|
||||
Total: 0,
|
||||
NextCursor: "",
|
||||
PageSize: 50,
|
||||
}
|
||||
|
||||
data, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal response: %v", err)
|
||||
}
|
||||
|
||||
// Empty string for next_cursor should be omitted due to omitempty tag
|
||||
if bytes.Contains(data, []byte("next_cursor")) {
|
||||
t.Error("empty next_cursor should be omitted from JSON")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFields_SingleObject(t *testing.T) {
|
||||
data := map[string]interface{}{
|
||||
"id": "cert-123",
|
||||
"name": "My Cert",
|
||||
"expiry": "2025-01-01",
|
||||
"status": "active",
|
||||
}
|
||||
|
||||
result := filterFields(data, []string{"id", "name"})
|
||||
|
||||
resultMap, ok := result.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("result is not map[string]interface{}, got %T", result)
|
||||
}
|
||||
|
||||
if resultMap["id"] != "cert-123" {
|
||||
t.Errorf("id = %v, want cert-123", resultMap["id"])
|
||||
}
|
||||
|
||||
if resultMap["name"] != "My Cert" {
|
||||
t.Errorf("name = %v, want My Cert", resultMap["name"])
|
||||
}
|
||||
|
||||
if _, hasExpiry := resultMap["expiry"]; hasExpiry {
|
||||
t.Error("expiry should be filtered out")
|
||||
}
|
||||
|
||||
if _, hasStatus := resultMap["status"]; hasStatus {
|
||||
t.Error("status should be filtered out")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFields_EmptyFields(t *testing.T) {
|
||||
// Empty fields list should return data unchanged
|
||||
data := map[string]interface{}{
|
||||
"id": "cert-123",
|
||||
"name": "My Cert",
|
||||
}
|
||||
|
||||
result := filterFields(data, []string{})
|
||||
|
||||
// Should return original data unchanged
|
||||
resultMap, ok := result.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("result is not map[string]interface{}, got %T", result)
|
||||
}
|
||||
|
||||
if len(resultMap) != 2 {
|
||||
t.Errorf("filtered result has %d fields, want 2", len(resultMap))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFields_NoMatchingFields(t *testing.T) {
|
||||
data := map[string]interface{}{
|
||||
"id": "cert-123",
|
||||
"name": "My Cert",
|
||||
}
|
||||
|
||||
result := filterFields(data, []string{"nonexistent", "also-not-there"})
|
||||
|
||||
resultMap, ok := result.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("result is not map[string]interface{}, got %T", result)
|
||||
}
|
||||
|
||||
if len(resultMap) != 0 {
|
||||
t.Errorf("filtered result has %d fields, want 0", len(resultMap))
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterFields_InvalidJSON(t *testing.T) {
|
||||
// Non-serializable data should be returned as-is
|
||||
data := make(chan int) // channels can't be marshaled to JSON
|
||||
|
||||
result := filterFields(data, []string{"field"})
|
||||
|
||||
// Should return original data unchanged
|
||||
if result != data {
|
||||
t.Error("invalid data should be returned unchanged")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,353 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"encoding/asn1"
|
||||
"encoding/base64"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/api/middleware"
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
"github.com/shankar0123/certctl/internal/pkcs7"
|
||||
)
|
||||
|
||||
// SCEPService defines the service interface for SCEP enrollment operations.
|
||||
// SCEP (RFC 8894) is a protocol for certificate enrollment used by MDM platforms
|
||||
// and network devices.
|
||||
type SCEPService interface {
|
||||
// GetCACaps returns the SCEP server capabilities as a newline-separated string.
|
||||
GetCACaps(ctx context.Context) string
|
||||
|
||||
// GetCACert returns the PEM-encoded CA certificate chain.
|
||||
GetCACert(ctx context.Context) (string, error)
|
||||
|
||||
// PKCSReq processes a PKCS#10 CSR and returns a signed certificate.
|
||||
PKCSReq(ctx context.Context, csrPEM string, challengePassword string, transactionID string) (*domain.SCEPEnrollResult, error)
|
||||
}
|
||||
|
||||
// SCEPHandler handles HTTP requests for the SCEP protocol (RFC 8894).
|
||||
//
|
||||
// SCEP uses a single endpoint with operation-based dispatch via query parameters.
|
||||
// All operations use GET or POST to the same path.
|
||||
//
|
||||
// Supported operations:
|
||||
// - GET ?operation=GetCACaps — server capabilities
|
||||
// - GET ?operation=GetCACert — CA certificate distribution
|
||||
// - POST ?operation=PKIOperation — certificate enrollment (PKCSReq)
|
||||
type SCEPHandler struct {
|
||||
svc SCEPService
|
||||
}
|
||||
|
||||
// NewSCEPHandler creates a new SCEPHandler.
|
||||
func NewSCEPHandler(svc SCEPService) SCEPHandler {
|
||||
return SCEPHandler{svc: svc}
|
||||
}
|
||||
|
||||
// HandleSCEP is the single entry point for all SCEP operations.
|
||||
// It dispatches based on the "operation" query parameter.
|
||||
func (h SCEPHandler) HandleSCEP(w http.ResponseWriter, r *http.Request) {
|
||||
operation := r.URL.Query().Get("operation")
|
||||
|
||||
switch operation {
|
||||
case "GetCACaps":
|
||||
h.getCACaps(w, r)
|
||||
case "GetCACert":
|
||||
h.getCACert(w, r)
|
||||
case "PKIOperation":
|
||||
h.pkiOperation(w, r)
|
||||
default:
|
||||
http.Error(w, fmt.Sprintf("Unknown SCEP operation: %s", operation), http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
// getCACaps handles GET ?operation=GetCACaps
|
||||
// Returns the SCEP server capabilities as plaintext, one per line.
|
||||
func (h SCEPHandler) getCACaps(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
caps := h.svc.GetCACaps(r.Context())
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(caps))
|
||||
}
|
||||
|
||||
// getCACert handles GET ?operation=GetCACert
|
||||
// Returns the CA certificate(s). Single cert as DER, chain as PKCS#7.
|
||||
func (h SCEPHandler) getCACert(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
caCertPEM, err := h.svc.GetCACert(r.Context())
|
||||
if err != nil {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, fmt.Sprintf("Failed to get CA certificate: %v", err), requestID)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse PEM to DER chain
|
||||
derCerts, err := pkcs7.PEMToDERChain(caCertPEM)
|
||||
if err != nil {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to parse CA certificates", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
if len(derCerts) == 1 {
|
||||
// Single CA cert — return as raw DER
|
||||
w.Header().Set("Content-Type", "application/x-x509-ca-cert")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(derCerts[0])
|
||||
return
|
||||
}
|
||||
|
||||
// Multiple certs (CA + RA or chain) — return as PKCS#7
|
||||
pkcs7Data, err := pkcs7.BuildCertsOnlyPKCS7(derCerts)
|
||||
if err != nil {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to build PKCS#7 response", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/x-x509-ca-ra-cert")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(pkcs7Data)
|
||||
}
|
||||
|
||||
// pkiOperation handles POST ?operation=PKIOperation
|
||||
// Processes a SCEP enrollment request containing a PKCS#7-wrapped CSR.
|
||||
func (h SCEPHandler) pkiOperation(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20)) // 1MB limit
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusBadRequest, "Failed to read request body", requestID)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
if len(body) == 0 {
|
||||
ErrorWithRequestID(w, http.StatusBadRequest, "Empty request body", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
// Extract the PKCS#10 CSR from the PKCS#7 SignedData envelope
|
||||
csrDER, challengePassword, transactionID, err := extractCSRFromPKCS7(body)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusBadRequest, fmt.Sprintf("Invalid SCEP message: %v", err), requestID)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate the CSR
|
||||
csr, err := x509.ParseCertificateRequest(csrDER)
|
||||
if err != nil {
|
||||
ErrorWithRequestID(w, http.StatusBadRequest, fmt.Sprintf("Invalid CSR: %v", err), requestID)
|
||||
return
|
||||
}
|
||||
if err := csr.CheckSignature(); err != nil {
|
||||
ErrorWithRequestID(w, http.StatusBadRequest, fmt.Sprintf("CSR signature invalid: %v", err), requestID)
|
||||
return
|
||||
}
|
||||
|
||||
// Convert DER CSR to PEM for the service layer
|
||||
csrPEM := string(pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE REQUEST",
|
||||
Bytes: csrDER,
|
||||
}))
|
||||
|
||||
result, err := h.svc.PKCSReq(r.Context(), csrPEM, challengePassword, transactionID)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "challenge password") {
|
||||
ErrorWithRequestID(w, http.StatusForbidden, "Invalid challenge password", requestID)
|
||||
return
|
||||
}
|
||||
ErrorWithRequestID(w, http.StatusInternalServerError, fmt.Sprintf("Enrollment failed: %v", err), requestID)
|
||||
return
|
||||
}
|
||||
|
||||
// Build response: issued cert wrapped in PKCS#7 certs-only
|
||||
h.writeSCEPResponse(w, result)
|
||||
}
|
||||
|
||||
// writeSCEPResponse writes a SCEP enrollment response as PKCS#7 certs-only (DER).
|
||||
func (h SCEPHandler) writeSCEPResponse(w http.ResponseWriter, result *domain.SCEPEnrollResult) {
|
||||
var derCerts [][]byte
|
||||
|
||||
certDER, err := pkcs7.PEMToDERChain(result.CertPEM)
|
||||
if err != nil || len(certDER) == 0 {
|
||||
http.Error(w, "Failed to encode certificate", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
derCerts = append(derCerts, certDER...)
|
||||
|
||||
if result.ChainPEM != "" {
|
||||
chainDER, err := pkcs7.PEMToDERChain(result.ChainPEM)
|
||||
if err == nil {
|
||||
derCerts = append(derCerts, chainDER...)
|
||||
}
|
||||
}
|
||||
|
||||
pkcs7Data, err := pkcs7.BuildCertsOnlyPKCS7(derCerts)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to build PKCS#7 response", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/x-pki-message")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(pkcs7Data)
|
||||
}
|
||||
|
||||
// extractCSRFromPKCS7 extracts a PKCS#10 CSR from a SCEP PKCS#7 SignedData envelope.
|
||||
//
|
||||
// SCEP clients wrap the CSR in a PKCS#7 SignedData structure. For the MVP, we parse
|
||||
// the outer ASN.1 structure to find the encapsulated content (the CSR bytes), and
|
||||
// extract the challenge password from the CSR attributes.
|
||||
//
|
||||
// Returns: csrDER, challengePassword, transactionID, error
|
||||
func extractCSRFromPKCS7(data []byte) ([]byte, string, string, error) {
|
||||
// Try to decode as PKCS#7 SignedData
|
||||
csrDER, err := parseSignedDataForCSR(data)
|
||||
if err != nil {
|
||||
// Fallback: some clients send the CSR directly (not wrapped in PKCS#7)
|
||||
// or send base64-encoded data
|
||||
decoded, decErr := base64.StdEncoding.DecodeString(strings.TrimSpace(string(data)))
|
||||
if decErr == nil {
|
||||
// Try the decoded data as PKCS#7
|
||||
csrDER2, err2 := parseSignedDataForCSR(decoded)
|
||||
if err2 == nil {
|
||||
return extractCSRFields(csrDER2)
|
||||
}
|
||||
// Maybe the decoded data IS the CSR directly
|
||||
if _, parseErr := x509.ParseCertificateRequest(decoded); parseErr == nil {
|
||||
return extractCSRFields(decoded)
|
||||
}
|
||||
}
|
||||
// Maybe the raw data IS the CSR directly (no PKCS#7 wrapping)
|
||||
if _, parseErr := x509.ParseCertificateRequest(data); parseErr == nil {
|
||||
return extractCSRFields(data)
|
||||
}
|
||||
return nil, "", "", fmt.Errorf("failed to extract CSR from PKCS#7: %w", err)
|
||||
}
|
||||
return extractCSRFields(csrDER)
|
||||
}
|
||||
|
||||
// extractCSRFields extracts the challenge password and transaction ID from CSR attributes.
|
||||
func extractCSRFields(csrDER []byte) ([]byte, string, string, error) {
|
||||
csr, err := x509.ParseCertificateRequest(csrDER)
|
||||
if err != nil {
|
||||
return nil, "", "", fmt.Errorf("invalid CSR: %w", err)
|
||||
}
|
||||
|
||||
challengePassword := ""
|
||||
transactionID := ""
|
||||
|
||||
// OID for challengePassword: 1.2.840.113549.1.9.7
|
||||
oidChallengePassword := asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 7}
|
||||
|
||||
// Extract challenge password from parsed CSR attributes.
|
||||
// Attributes is []pkix.AttributeTypeAndValueSET where each has Type (OID)
|
||||
// and Value ([][]pkix.AttributeTypeAndValue). The challenge password value
|
||||
// is stored as a string in the inner AttributeTypeAndValue.Value field.
|
||||
for _, attr := range csr.Attributes {
|
||||
if attr.Type.Equal(oidChallengePassword) {
|
||||
if len(attr.Value) > 0 && len(attr.Value[0]) > 0 {
|
||||
if pwd, ok := attr.Value[0][0].Value.(string); ok {
|
||||
challengePassword = pwd
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use CN as fallback transaction ID if not found in attributes
|
||||
if transactionID == "" && csr.Subject.CommonName != "" {
|
||||
transactionID = csr.Subject.CommonName
|
||||
}
|
||||
|
||||
return csrDER, challengePassword, transactionID, nil
|
||||
}
|
||||
|
||||
// pkcs7ContentInfo represents the outer ContentInfo structure.
|
||||
type pkcs7ContentInfo struct {
|
||||
ContentType asn1.ObjectIdentifier
|
||||
Content asn1.RawValue `asn1:"explicit,tag:0"`
|
||||
}
|
||||
|
||||
// pkcs7SignedData represents a simplified SignedData structure for CSR extraction.
|
||||
type pkcs7SignedData struct {
|
||||
Version int
|
||||
DigestAlgorithms asn1.RawValue
|
||||
EncapContentInfo asn1.RawValue
|
||||
}
|
||||
|
||||
// pkcs7EncapContent represents the EncapsulatedContentInfo.
|
||||
type pkcs7EncapContent struct {
|
||||
ContentType asn1.ObjectIdentifier
|
||||
Content asn1.RawValue `asn1:"explicit,optional,tag:0"`
|
||||
}
|
||||
|
||||
// parseSignedDataForCSR extracts the encapsulated content (CSR) from PKCS#7 SignedData.
|
||||
func parseSignedDataForCSR(data []byte) ([]byte, error) {
|
||||
var contentInfo pkcs7ContentInfo
|
||||
rest, err := asn1.Unmarshal(data, &contentInfo)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse ContentInfo: %w", err)
|
||||
}
|
||||
if len(rest) > 0 {
|
||||
// Trailing data is OK for some implementations
|
||||
}
|
||||
|
||||
// OID for signedData: 1.2.840.113549.1.7.2
|
||||
oidSignedData := asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2}
|
||||
if !contentInfo.ContentType.Equal(oidSignedData) {
|
||||
return nil, fmt.Errorf("not SignedData: got OID %v", contentInfo.ContentType)
|
||||
}
|
||||
|
||||
// Parse the SignedData
|
||||
var signedData pkcs7SignedData
|
||||
_, err = asn1.Unmarshal(contentInfo.Content.Bytes, &signedData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse SignedData: %w", err)
|
||||
}
|
||||
|
||||
// Parse the EncapsulatedContentInfo to get the CSR
|
||||
var encapContent pkcs7EncapContent
|
||||
_, err = asn1.Unmarshal(signedData.EncapContentInfo.FullBytes, &encapContent)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse EncapsulatedContentInfo: %w", err)
|
||||
}
|
||||
|
||||
if len(encapContent.Content.Bytes) == 0 {
|
||||
return nil, fmt.Errorf("empty encapsulated content")
|
||||
}
|
||||
|
||||
// The content may be wrapped in an OCTET STRING
|
||||
var csrBytes []byte
|
||||
var octetString asn1.RawValue
|
||||
if _, err := asn1.Unmarshal(encapContent.Content.Bytes, &octetString); err == nil && octetString.Tag == asn1.TagOctetString {
|
||||
csrBytes = octetString.Bytes
|
||||
} else {
|
||||
csrBytes = encapContent.Content.Bytes
|
||||
}
|
||||
|
||||
// Validate it's a parseable CSR
|
||||
if _, err := x509.ParseCertificateRequest(csrBytes); err != nil {
|
||||
return nil, fmt.Errorf("extracted content is not a valid CSR: %w", err)
|
||||
}
|
||||
|
||||
return csrBytes, nil
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
)
|
||||
|
||||
// mockSCEPService implements SCEPService for testing.
|
||||
type mockSCEPService struct {
|
||||
CACaps string
|
||||
CACertPEM string
|
||||
CACertErr error
|
||||
EnrollResult *domain.SCEPEnrollResult
|
||||
EnrollErr error
|
||||
}
|
||||
|
||||
func (m *mockSCEPService) GetCACaps(ctx context.Context) string {
|
||||
if m.CACaps != "" {
|
||||
return m.CACaps
|
||||
}
|
||||
return "POSTPKIOperation\nSHA-256\nAES\nSCEPStandard\n"
|
||||
}
|
||||
|
||||
func (m *mockSCEPService) GetCACert(ctx context.Context) (string, error) {
|
||||
return m.CACertPEM, m.CACertErr
|
||||
}
|
||||
|
||||
func (m *mockSCEPService) PKCSReq(ctx context.Context, csrPEM string, challengePassword string, transactionID string) (*domain.SCEPEnrollResult, error) {
|
||||
return m.EnrollResult, m.EnrollErr
|
||||
}
|
||||
|
||||
func TestSCEP_GetCACaps_Success(t *testing.T) {
|
||||
svc := &mockSCEPService{}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/scep?operation=GetCACaps", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
ct := w.Header().Get("Content-Type")
|
||||
if ct != "text/plain" {
|
||||
t.Errorf("expected text/plain, got %s", ct)
|
||||
}
|
||||
body := w.Body.String()
|
||||
if !strings.Contains(body, "POSTPKIOperation") {
|
||||
t.Errorf("expected POSTPKIOperation in response, got: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, "SHA-256") {
|
||||
t.Errorf("expected SHA-256 in response, got: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_GetCACaps_MethodNotAllowed(t *testing.T) {
|
||||
svc := &mockSCEPService{}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/scep?operation=GetCACaps", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("expected 405, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_GetCACert_Success_SingleCert(t *testing.T) {
|
||||
certPEM := generateTestCertPEM(t)
|
||||
svc := &mockSCEPService{
|
||||
CACertPEM: certPEM,
|
||||
}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/scep?operation=GetCACert", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
ct := w.Header().Get("Content-Type")
|
||||
if ct != "application/x-x509-ca-cert" {
|
||||
t.Errorf("expected application/x-x509-ca-cert, got %s", ct)
|
||||
}
|
||||
if w.Body.Len() == 0 {
|
||||
t.Error("expected non-empty body")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_GetCACert_MethodNotAllowed(t *testing.T) {
|
||||
svc := &mockSCEPService{}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/scep?operation=GetCACert", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("expected 405, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_GetCACert_ServiceError(t *testing.T) {
|
||||
svc := &mockSCEPService{
|
||||
CACertErr: errors.New("CA unavailable"),
|
||||
}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/scep?operation=GetCACert", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected 500, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_PKIOperation_MethodNotAllowed(t *testing.T) {
|
||||
svc := &mockSCEPService{}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/scep?operation=PKIOperation", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusMethodNotAllowed {
|
||||
t.Errorf("expected 405, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_PKIOperation_EmptyBody(t *testing.T) {
|
||||
svc := &mockSCEPService{}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/scep?operation=PKIOperation", strings.NewReader(""))
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_PKIOperation_InvalidBody(t *testing.T) {
|
||||
svc := &mockSCEPService{}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/scep?operation=PKIOperation", strings.NewReader("not-valid-asn1-or-csr"))
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_PKIOperation_ServiceError(t *testing.T) {
|
||||
svc := &mockSCEPService{
|
||||
EnrollErr: errors.New("enrollment failed"),
|
||||
}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
// Generate a valid raw CSR DER to send as body (fallback path)
|
||||
csrPEM := generateTestCSRPEM(t)
|
||||
block, _ := pem.Decode([]byte(csrPEM))
|
||||
if block == nil {
|
||||
t.Fatal("failed to decode CSR PEM")
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/scep?operation=PKIOperation", strings.NewReader(string(block.Bytes)))
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_PKIOperation_Success_RawCSR(t *testing.T) {
|
||||
certPEM := generateTestCertPEM(t)
|
||||
svc := &mockSCEPService{
|
||||
EnrollResult: &domain.SCEPEnrollResult{
|
||||
CertPEM: certPEM,
|
||||
ChainPEM: "",
|
||||
},
|
||||
}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
csrPEM := generateTestCSRPEM(t)
|
||||
block, _ := pem.Decode([]byte(csrPEM))
|
||||
if block == nil {
|
||||
t.Fatal("failed to decode CSR PEM")
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/scep?operation=PKIOperation", strings.NewReader(string(block.Bytes)))
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
ct := w.Header().Get("Content-Type")
|
||||
if ct != "application/x-pki-message" {
|
||||
t.Errorf("expected application/x-pki-message, got %s", ct)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_PKIOperation_ChallengePasswordRejected(t *testing.T) {
|
||||
svc := &mockSCEPService{
|
||||
EnrollErr: errors.New("invalid challenge password"),
|
||||
}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
csrPEM := generateTestCSRPEM(t)
|
||||
block, _ := pem.Decode([]byte(csrPEM))
|
||||
if block == nil {
|
||||
t.Fatal("failed to decode CSR PEM")
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/scep?operation=PKIOperation", strings.NewReader(string(block.Bytes)))
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("expected 403, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_UnknownOperation(t *testing.T) {
|
||||
svc := &mockSCEPService{}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/scep?operation=UnknownOp", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEP_MissingOperation(t *testing.T) {
|
||||
svc := &mockSCEPService{}
|
||||
h := NewSCEPHandler(svc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/scep", nil)
|
||||
w := httptest.NewRecorder()
|
||||
h.HandleSCEP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,562 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestValidateCommonName_ValidInputs tests common names that should pass validation.
|
||||
func TestValidateCommonName_ValidInputs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cn string
|
||||
}{
|
||||
{
|
||||
name: "simple hostname",
|
||||
cn: "example.com",
|
||||
},
|
||||
{
|
||||
name: "wildcard domain",
|
||||
cn: "*.example.com",
|
||||
},
|
||||
{
|
||||
name: "subdomain",
|
||||
cn: "sub.deep.example.com",
|
||||
},
|
||||
{
|
||||
name: "IPv4 address",
|
||||
cn: "192.168.1.1",
|
||||
},
|
||||
{
|
||||
name: "IPv6 address",
|
||||
cn: "2001:db8::1",
|
||||
},
|
||||
{
|
||||
name: "email address (S/MIME)",
|
||||
cn: "user@example.com",
|
||||
},
|
||||
{
|
||||
name: "hostname with hyphen",
|
||||
cn: "my-host",
|
||||
},
|
||||
{
|
||||
name: "single character hostname",
|
||||
cn: "a",
|
||||
},
|
||||
{
|
||||
name: "hostname with underscore",
|
||||
cn: "my_host",
|
||||
},
|
||||
{
|
||||
name: "complex subdomain",
|
||||
cn: "api.v1.internal.example.com",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateCommonName(tt.cn)
|
||||
if err != nil {
|
||||
t.Errorf("ValidateCommonName(%q) = %v, want nil", tt.cn, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateCommonName_InvalidInputs tests common names that should fail validation.
|
||||
func TestValidateCommonName_InvalidInputs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cn string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "empty string",
|
||||
cn: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "whitespace only",
|
||||
cn: " ",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "string exceeds 253 characters",
|
||||
cn: strings.Repeat("a", 254),
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "path traversal attempt",
|
||||
cn: "../etc/passwd",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "label starts with hyphen",
|
||||
cn: "-example.com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "label ends with hyphen",
|
||||
cn: "example-.com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty label",
|
||||
cn: "example..com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid character space",
|
||||
cn: "my host.com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "invalid character slash",
|
||||
cn: "my/host.com",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "malformed email",
|
||||
cn: "notanemail@",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateCommonName(tt.cn)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidateCommonName(%q) error = %v, wantErr %v", tt.cn, err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateRequired_EmptyAndWhitespace tests required field validation.
|
||||
func TestValidateRequired_EmptyAndWhitespace(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
field string
|
||||
value string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "empty value",
|
||||
field: "test_field",
|
||||
value: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "valid value",
|
||||
field: "test_field",
|
||||
value: "value",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "whitespace only value",
|
||||
field: "another_field",
|
||||
value: " ",
|
||||
wantErr: false, // Whitespace is considered a value (not empty string)
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateRequired(tt.field, tt.value)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidateRequired(%q, %q) error = %v, wantErr %v", tt.field, tt.value, err, tt.wantErr)
|
||||
}
|
||||
if err != nil {
|
||||
ve, ok := err.(ValidationError)
|
||||
if !ok {
|
||||
t.Errorf("Expected ValidationError, got %T", err)
|
||||
}
|
||||
if ve.Field != tt.field {
|
||||
t.Errorf("Expected field %q, got %q", tt.field, ve.Field)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateStringLength_Boundary tests string length validation at boundaries.
|
||||
func TestValidateStringLength_Boundary(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
field string
|
||||
value string
|
||||
maxLen int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "at max length",
|
||||
field: "test",
|
||||
value: "0123456789",
|
||||
maxLen: 10,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "under max length",
|
||||
field: "test",
|
||||
value: "012345678",
|
||||
maxLen: 10,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "exceeds max length",
|
||||
field: "test",
|
||||
value: "01234567890",
|
||||
maxLen: 10,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty string",
|
||||
field: "test",
|
||||
value: "",
|
||||
maxLen: 10,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateStringLength(tt.field, tt.value, tt.maxLen)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidateStringLength(%q, %q, %d) error = %v, wantErr %v",
|
||||
tt.field, tt.value, tt.maxLen, err, tt.wantErr)
|
||||
}
|
||||
if err != nil {
|
||||
ve, ok := err.(ValidationError)
|
||||
if !ok {
|
||||
t.Errorf("Expected ValidationError, got %T", err)
|
||||
}
|
||||
if ve.Field != tt.field {
|
||||
t.Errorf("Expected field %q, got %q", tt.field, ve.Field)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateCSRPEM_Valid tests validation of a real CSR PEM.
|
||||
func TestValidateCSRPEM_Valid(t *testing.T) {
|
||||
// Generate a real CSR using crypto/x509
|
||||
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to generate private key: %v", err)
|
||||
}
|
||||
|
||||
csrTemplate := &x509.CertificateRequest{
|
||||
Subject: pkixName("example.com"),
|
||||
}
|
||||
|
||||
csrDER, err := x509.CreateCertificateRequest(rand.Reader, csrTemplate, privateKey)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create CSR: %v", err)
|
||||
}
|
||||
|
||||
csrPEM := pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE REQUEST",
|
||||
Bytes: csrDER,
|
||||
})
|
||||
|
||||
err = ValidateCSRPEM(string(csrPEM))
|
||||
if err != nil {
|
||||
t.Errorf("ValidateCSRPEM() on valid CSR returned error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateCSRPEM_InvalidInputs tests CSR validation with invalid inputs.
|
||||
func TestValidateCSRPEM_InvalidInputs(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
csrPEM string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "empty string",
|
||||
csrPEM: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "not PEM format",
|
||||
csrPEM: "not-a-pem-block",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "garbage data",
|
||||
csrPEM: "asdfjkl;asdfjkl;",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "certificate PEM (not CSR)",
|
||||
csrPEM: "-----BEGIN CERTIFICATE-----\nMIIC",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "PEM with wrong type",
|
||||
csrPEM: "-----BEGIN PRIVATE KEY-----\ndata",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "whitespace only",
|
||||
csrPEM: " \n ",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidateCSRPEM(tt.csrPEM)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidateCSRPEM(%q) error = %v, wantErr %v", tt.csrPEM, err, tt.wantErr)
|
||||
}
|
||||
if err != nil {
|
||||
ve, ok := err.(ValidationError)
|
||||
if !ok {
|
||||
t.Errorf("Expected ValidationError, got %T", err)
|
||||
}
|
||||
if ve.Field != "csr_pem" {
|
||||
t.Errorf("Expected field 'csr_pem', got %q", ve.Field)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidatePolicyType_ValidTypes tests valid policy types.
|
||||
func TestValidatePolicyType_ValidTypes(t *testing.T) {
|
||||
validTypes := []struct {
|
||||
name string
|
||||
ptype interface{}
|
||||
}{
|
||||
{
|
||||
name: "AllowedIssuers",
|
||||
ptype: "AllowedIssuers",
|
||||
},
|
||||
{
|
||||
name: "AllowedDomains",
|
||||
ptype: "AllowedDomains",
|
||||
},
|
||||
{
|
||||
name: "RequiredMetadata",
|
||||
ptype: "RequiredMetadata",
|
||||
},
|
||||
{
|
||||
name: "AllowedEnvironments",
|
||||
ptype: "AllowedEnvironments",
|
||||
},
|
||||
{
|
||||
name: "RenewalLeadTime",
|
||||
ptype: "RenewalLeadTime",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range validTypes {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidatePolicyType(tt.ptype)
|
||||
if err != nil {
|
||||
t.Errorf("ValidatePolicyType(%v) = %v, want nil", tt.ptype, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidatePolicyType_InvalidType tests invalid policy types.
|
||||
func TestValidatePolicyType_InvalidType(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
ptype interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "nonexistent type",
|
||||
ptype: "NonexistentType",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty string",
|
||||
ptype: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "lowercase type",
|
||||
ptype: "allowedissuers",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "integer type",
|
||||
ptype: 123,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidatePolicyType(tt.ptype)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidatePolicyType(%v) error = %v, wantErr %v", tt.ptype, err, tt.wantErr)
|
||||
}
|
||||
if err != nil {
|
||||
ve, ok := err.(ValidationError)
|
||||
if !ok {
|
||||
t.Errorf("Expected ValidationError, got %T", err)
|
||||
}
|
||||
if ve.Field != "type" {
|
||||
t.Errorf("Expected field 'type', got %q", ve.Field)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidatePolicySeverity_ValidSeverities tests valid severity levels.
|
||||
func TestValidatePolicySeverity_ValidSeverities(t *testing.T) {
|
||||
validSeverities := []struct {
|
||||
name string
|
||||
sev interface{}
|
||||
}{
|
||||
{
|
||||
name: "Warning",
|
||||
sev: "Warning",
|
||||
},
|
||||
{
|
||||
name: "Error",
|
||||
sev: "Error",
|
||||
},
|
||||
{
|
||||
name: "Critical",
|
||||
sev: "Critical",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range validSeverities {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidatePolicySeverity(tt.sev)
|
||||
if err != nil {
|
||||
t.Errorf("ValidatePolicySeverity(%v) = %v, want nil", tt.sev, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidatePolicySeverity_InvalidSeverity tests invalid severity levels.
|
||||
func TestValidatePolicySeverity_InvalidSeverity(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
sev interface{}
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "lowercase warning",
|
||||
sev: "warning",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "nonexistent severity",
|
||||
sev: "Severe",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty string",
|
||||
sev: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "integer",
|
||||
sev: 1,
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := ValidatePolicySeverity(tt.sev)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ValidatePolicySeverity(%v) error = %v, wantErr %v", tt.sev, err, tt.wantErr)
|
||||
}
|
||||
if err != nil {
|
||||
ve, ok := err.(ValidationError)
|
||||
if !ok {
|
||||
t.Errorf("Expected ValidationError, got %T", err)
|
||||
}
|
||||
if ve.Field != "severity" {
|
||||
t.Errorf("Expected field 'severity', got %q", ve.Field)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidationError_ErrorMessage tests ValidationError.Error() method.
|
||||
func TestValidationError_ErrorMessage(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err ValidationError
|
||||
wantMsg string
|
||||
}{
|
||||
{
|
||||
name: "simple message",
|
||||
err: ValidationError{
|
||||
Field: "common_name",
|
||||
Message: "common_name is required",
|
||||
},
|
||||
wantMsg: "common_name is required",
|
||||
},
|
||||
{
|
||||
name: "detailed message",
|
||||
err: ValidationError{
|
||||
Field: "csr_pem",
|
||||
Message: "csr_pem must be a valid PEM-encoded certificate request",
|
||||
},
|
||||
wantMsg: "csr_pem must be a valid PEM-encoded certificate request",
|
||||
},
|
||||
{
|
||||
name: "error with field info",
|
||||
err: ValidationError{
|
||||
Field: "test_field",
|
||||
Message: "test_field must be 10 characters or fewer",
|
||||
},
|
||||
wantMsg: "test_field must be 10 characters or fewer",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
errMsg := tt.err.Error()
|
||||
if errMsg != tt.wantMsg {
|
||||
t.Errorf("ValidationError.Error() = %q, want %q", errMsg, tt.wantMsg)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidationError_IsError tests that ValidationError satisfies error interface.
|
||||
func TestValidationError_IsError(t *testing.T) {
|
||||
ve := ValidationError{
|
||||
Field: "test",
|
||||
Message: "test error",
|
||||
}
|
||||
|
||||
// Assign to interface variable to verify it satisfies error
|
||||
var err error = ve
|
||||
_ = err
|
||||
|
||||
msg := ve.Error()
|
||||
if msg != "test error" {
|
||||
t.Errorf("Expected error message 'test error', got %q", msg)
|
||||
}
|
||||
}
|
||||
|
||||
// pkixName is a helper function to create PKIX name (used in CSR generation).
|
||||
func pkixName(cn string) pkix.Name {
|
||||
return pkix.Name{
|
||||
CommonName: cn,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,254 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestRateLimiter_AllowedWithinLimit verifies that requests within the rate limit are allowed.
|
||||
func TestRateLimiter_AllowedWithinLimit(t *testing.T) {
|
||||
handler := NewRateLimiter(RateLimitConfig{RPS: 10, BurstSize: 10})(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
)
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimiter_ExceededReturns429 verifies that requests exceeding the rate limit get 429.
|
||||
func TestRateLimiter_ExceededReturns429(t *testing.T) {
|
||||
// Create a limiter with very strict limits
|
||||
handler := NewRateLimiter(RateLimitConfig{RPS: 0.1, BurstSize: 1})(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
)
|
||||
|
||||
// First request should succeed (within burst)
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("first request: expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
// Second request should fail (burst exhausted, no tokens refilled)
|
||||
req2 := httptest.NewRequest("GET", "/test", nil)
|
||||
w2 := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w2, req2)
|
||||
if w2.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("second request: expected status %d, got %d", http.StatusTooManyRequests, w2.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimiter_BurstCapacity verifies that burst allows spike in traffic.
|
||||
func TestRateLimiter_BurstCapacity(t *testing.T) {
|
||||
handler := NewRateLimiter(RateLimitConfig{RPS: 1, BurstSize: 5})(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
)
|
||||
|
||||
// Fire 5 requests in rapid succession (burst size)
|
||||
for i := 0; i < 5; i++ {
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("burst request %d: expected status %d, got %d", i, http.StatusOK, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// 6th request should be rejected (burst exhausted)
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("request after burst: expected status %d, got %d", http.StatusTooManyRequests, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimiter_TokenRefill verifies that tokens refill over time.
|
||||
func TestRateLimiter_TokenRefill(t *testing.T) {
|
||||
handler := NewRateLimiter(RateLimitConfig{RPS: 10, BurstSize: 1})(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
)
|
||||
|
||||
// First request succeeds (within burst)
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("first request: expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
// Second request fails (burst exhausted)
|
||||
req2 := httptest.NewRequest("GET", "/test", nil)
|
||||
w2 := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w2, req2)
|
||||
if w2.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("second request: expected status %d, got %d", http.StatusTooManyRequests, w2.Code)
|
||||
}
|
||||
|
||||
// Wait for tokens to refill at RPS=10 (100ms per token)
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
|
||||
// Third request should succeed (token refilled)
|
||||
req3 := httptest.NewRequest("GET", "/test", nil)
|
||||
w3 := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w3, req3)
|
||||
if w3.Code != http.StatusOK {
|
||||
t.Errorf("third request after refill: expected status %d, got %d", http.StatusOK, w3.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimiter_ConcurrentRequests verifies behavior under concurrent load.
|
||||
func TestRateLimiter_ConcurrentRequests(t *testing.T) {
|
||||
// Rate limit: 5 RPS, burst of 2
|
||||
handler := NewRateLimiter(RateLimitConfig{RPS: 5, BurstSize: 2})(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
)
|
||||
|
||||
numGoroutines := 10
|
||||
results := make([]int, numGoroutines)
|
||||
var mu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Fire concurrent requests
|
||||
for i := 0; i < numGoroutines; i++ {
|
||||
wg.Add(1)
|
||||
go func(idx int) {
|
||||
defer wg.Done()
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
mu.Lock()
|
||||
results[idx] = w.Code
|
||||
mu.Unlock()
|
||||
}(i)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// Count successful vs rate-limited responses
|
||||
successCount := 0
|
||||
rateLimitedCount := 0
|
||||
for _, code := range results {
|
||||
if code == http.StatusOK {
|
||||
successCount++
|
||||
} else if code == http.StatusTooManyRequests {
|
||||
rateLimitedCount++
|
||||
} else {
|
||||
t.Errorf("unexpected status code: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
// With burst size 2, at most 2 should succeed immediately
|
||||
if successCount > 2 {
|
||||
t.Errorf("expected at most 2 concurrent requests to succeed, got %d", successCount)
|
||||
}
|
||||
|
||||
// Some should be rate limited
|
||||
if rateLimitedCount == 0 {
|
||||
t.Error("expected at least some requests to be rate limited")
|
||||
}
|
||||
|
||||
if successCount+rateLimitedCount != numGoroutines {
|
||||
t.Errorf("request count mismatch: %d + %d != %d", successCount, rateLimitedCount, numGoroutines)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimiter_RetryAfterHeader verifies that rate-limited responses include Retry-After.
|
||||
func TestRateLimiter_RetryAfterHeader(t *testing.T) {
|
||||
handler := NewRateLimiter(RateLimitConfig{RPS: 0.1, BurstSize: 1})(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
)
|
||||
|
||||
// Exhaust burst
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
// Trigger rate limit
|
||||
req2 := httptest.NewRequest("GET", "/test", nil)
|
||||
w2 := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w2, req2)
|
||||
|
||||
if w2.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("expected 429, got %d", w2.Code)
|
||||
}
|
||||
|
||||
// Check for Retry-After header
|
||||
retryAfter := w2.Header().Get("Retry-After")
|
||||
if retryAfter == "" {
|
||||
t.Error("expected Retry-After header in rate-limited response")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimiter_ZeroRPS verifies behavior with RPS=0 (all requests blocked).
|
||||
func TestRateLimiter_ZeroRPS(t *testing.T) {
|
||||
handler := NewRateLimiter(RateLimitConfig{RPS: 0, BurstSize: 1})(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
)
|
||||
|
||||
// First request succeeds (burst)
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("burst request: expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
// Second request blocked (no refill with RPS=0)
|
||||
req2 := httptest.NewRequest("GET", "/test", nil)
|
||||
w2 := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w2, req2)
|
||||
if w2.Code != http.StatusTooManyRequests {
|
||||
t.Errorf("second request: expected status %d, got %d", http.StatusTooManyRequests, w2.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateLimiter_VeryHighRPS verifies behavior with very high RPS (unlimited-like).
|
||||
func TestRateLimiter_VeryHighRPS(t *testing.T) {
|
||||
// 1000 RPS should allow most requests through
|
||||
handler := NewRateLimiter(RateLimitConfig{RPS: 1000, BurstSize: 100})(
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}),
|
||||
)
|
||||
|
||||
// Fire 50 requests — most should succeed given the high rate
|
||||
successCount := 0
|
||||
for i := 0; i < 50; i++ {
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
if w.Code == http.StatusOK {
|
||||
successCount++
|
||||
}
|
||||
}
|
||||
|
||||
// With 1000 RPS and 100 burst, most should pass
|
||||
if successCount < 40 {
|
||||
t.Errorf("expected at least 40 of 50 requests to succeed at 1000 RPS, got %d", successCount)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestRecovery_CatchesPanic verifies that panic recovery middleware catches panics
|
||||
// and returns a 500 error response.
|
||||
func TestRecovery_CatchesPanic(t *testing.T) {
|
||||
handler := Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
panic("test panic")
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected status %d, got %d", http.StatusInternalServerError, w.Code)
|
||||
}
|
||||
|
||||
// Verify error response is present
|
||||
if w.Body.Len() == 0 {
|
||||
t.Error("expected error response body, got empty")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecovery_CatchesNilPanic verifies that recovery middleware handles nil panics.
|
||||
func TestRecovery_CatchesNilPanic(t *testing.T) {
|
||||
handler := Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// This is unusual but valid in Go
|
||||
panic(nil)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected status %d, got %d", http.StatusInternalServerError, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecovery_NoPanicPasses verifies that non-panicking handlers pass through normally.
|
||||
func TestRecovery_NoPanicPasses(t *testing.T) {
|
||||
handler := Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Test", "success")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status %d, got %d", http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
if w.Header().Get("X-Test") != "success" {
|
||||
t.Error("expected custom header to be set")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecovery_StringPanic verifies recovery from string panics.
|
||||
func TestRecovery_StringPanic(t *testing.T) {
|
||||
handler := Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
panic("string panic message")
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected status %d, got %d", http.StatusInternalServerError, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecovery_ErrorPanic verifies recovery from error type panics.
|
||||
func TestRecovery_ErrorPanic(t *testing.T) {
|
||||
testErr := &customError{msg: "test error"}
|
||||
handler := Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
panic(testErr)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Errorf("expected status %d, got %d", http.StatusInternalServerError, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// customError is a simple error type for testing.
|
||||
type customError struct {
|
||||
msg string
|
||||
}
|
||||
|
||||
func (e *customError) Error() string {
|
||||
return e.msg
|
||||
}
|
||||
@@ -238,6 +238,15 @@ func (r *Router) RegisterESTHandlers(est handler.ESTHandler) {
|
||||
r.Register("GET /.well-known/est/csrattrs", http.HandlerFunc(est.CSRAttrs))
|
||||
}
|
||||
|
||||
// RegisterSCEPHandlers sets up SCEP (RFC 8894) routes.
|
||||
// SCEP uses a single endpoint with operation-based dispatch via query parameters.
|
||||
// Authentication is via challenge password in the CSR, not TLS client certs or API keys.
|
||||
func (r *Router) RegisterSCEPHandlers(scep handler.SCEPHandler) {
|
||||
// SCEP uses a single path; the handler dispatches on ?operation= query param
|
||||
r.Register("GET /scep", http.HandlerFunc(scep.HandleSCEP))
|
||||
r.Register("POST /scep", http.HandlerFunc(scep.HandleSCEP))
|
||||
}
|
||||
|
||||
// GetMux returns the underlying http.ServeMux for direct access if needed.
|
||||
func (r *Router) GetMux() *http.ServeMux {
|
||||
return r.mux
|
||||
|
||||
@@ -0,0 +1,393 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/api/handler"
|
||||
)
|
||||
|
||||
// TestNew_ReturnsValidRouter tests that New() returns a properly initialized router.
|
||||
func TestNew_ReturnsValidRouter(t *testing.T) {
|
||||
r := New()
|
||||
if r == nil {
|
||||
t.Fatal("expected non-nil router, got nil")
|
||||
}
|
||||
if r.mux == nil {
|
||||
t.Fatal("expected non-nil mux, got nil")
|
||||
}
|
||||
if r.middleware == nil {
|
||||
t.Fatal("expected non-nil middleware slice, got nil")
|
||||
}
|
||||
if len(r.middleware) != 0 {
|
||||
t.Fatalf("expected empty middleware slice, got %d", len(r.middleware))
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewWithMiddleware_InitializesMiddleware tests that NewWithMiddleware() applies middlewares.
|
||||
func TestNewWithMiddleware_InitializesMiddleware(t *testing.T) {
|
||||
called := false
|
||||
mw := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
r := NewWithMiddleware(mw)
|
||||
if len(r.middleware) != 1 {
|
||||
t.Fatalf("expected 1 middleware, got %d", len(r.middleware))
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
r.Register("GET /test", handler)
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if !called {
|
||||
t.Error("middleware was not called")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegisterHandlers_RoutesDispatch verifies that RegisterHandlers registers all expected routes.
|
||||
// We construct a HandlerRegistry where each handler method writes a unique marker,
|
||||
// then verify the expected routes dispatch to the correct handlers.
|
||||
func TestRegisterHandlers_RoutesDispatch(t *testing.T) {
|
||||
// Create handlers that respond with a marker so we can verify dispatch.
|
||||
// The handler structs have zero-value service dependencies which would panic
|
||||
// on real calls, so we intercept at the HTTP level using a wrapper.
|
||||
r := New()
|
||||
|
||||
// Track which handler was called
|
||||
var lastCalled string
|
||||
|
||||
// Create a registry with marker-writing handlers using a recovery wrapper.
|
||||
// Since zero-value handlers may panic when called (nil service), we wrap the
|
||||
// mux in a panic-recovering middleware for this test.
|
||||
recoverMW := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if rv := recover(); rv != nil {
|
||||
// Handler panicked due to nil service — that's expected.
|
||||
// The important thing is that the route was matched.
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
reg := HandlerRegistry{
|
||||
Certificates: handler.CertificateHandler{},
|
||||
Issuers: handler.IssuerHandler{},
|
||||
Targets: handler.TargetHandler{},
|
||||
Agents: handler.AgentHandler{},
|
||||
Jobs: handler.JobHandler{},
|
||||
Policies: handler.PolicyHandler{},
|
||||
Profiles: handler.ProfileHandler{},
|
||||
Teams: handler.TeamHandler{},
|
||||
Owners: handler.OwnerHandler{},
|
||||
AgentGroups: handler.AgentGroupHandler{},
|
||||
Audit: handler.AuditHandler{},
|
||||
Notifications: handler.NotificationHandler{},
|
||||
Stats: handler.StatsHandler{},
|
||||
Metrics: handler.MetricsHandler{},
|
||||
Health: handler.NewHealthHandler("api-key"),
|
||||
Discovery: handler.DiscoveryHandler{},
|
||||
NetworkScan: handler.NetworkScanHandler{},
|
||||
Verification: handler.VerificationHandler{},
|
||||
Export: handler.ExportHandler{},
|
||||
Digest: handler.DigestHandler{},
|
||||
}
|
||||
|
||||
r.RegisterHandlers(reg)
|
||||
|
||||
// Wrap the router with recovery middleware for testing
|
||||
testHandler := recoverMW(r)
|
||||
|
||||
// Test a representative sample of routes. We just check that the route
|
||||
// is registered (doesn't return 404). The handler may panic (caught by recoverMW)
|
||||
// or return an error, but NOT 404.
|
||||
routes := []struct {
|
||||
method string
|
||||
path string
|
||||
}{
|
||||
// Health (registered outside middleware chain)
|
||||
{"GET", "/health"},
|
||||
{"GET", "/ready"},
|
||||
{"GET", "/api/v1/auth/info"},
|
||||
{"GET", "/api/v1/auth/check"},
|
||||
|
||||
// Certificates CRUD
|
||||
{"GET", "/api/v1/certificates"},
|
||||
{"POST", "/api/v1/certificates"},
|
||||
{"GET", "/api/v1/certificates/mc-test"},
|
||||
{"PUT", "/api/v1/certificates/mc-test"},
|
||||
{"DELETE", "/api/v1/certificates/mc-test"},
|
||||
{"GET", "/api/v1/certificates/mc-test/versions"},
|
||||
{"GET", "/api/v1/certificates/mc-test/deployments"},
|
||||
{"POST", "/api/v1/certificates/mc-test/renew"},
|
||||
{"POST", "/api/v1/certificates/mc-test/deploy"},
|
||||
{"POST", "/api/v1/certificates/mc-test/revoke"},
|
||||
|
||||
// Export
|
||||
{"GET", "/api/v1/certificates/mc-test/export/pem"},
|
||||
|
||||
// CRL & OCSP
|
||||
{"GET", "/api/v1/crl"},
|
||||
{"GET", "/api/v1/crl/iss-local"},
|
||||
{"GET", "/api/v1/ocsp/iss-local/12345"},
|
||||
|
||||
// Issuers
|
||||
{"GET", "/api/v1/issuers"},
|
||||
{"POST", "/api/v1/issuers"},
|
||||
{"GET", "/api/v1/issuers/iss-test"},
|
||||
{"PUT", "/api/v1/issuers/iss-test"},
|
||||
{"DELETE", "/api/v1/issuers/iss-test"},
|
||||
{"POST", "/api/v1/issuers/iss-test/test"},
|
||||
|
||||
// Targets
|
||||
{"GET", "/api/v1/targets"},
|
||||
{"POST", "/api/v1/targets"},
|
||||
{"GET", "/api/v1/targets/t-test"},
|
||||
{"PUT", "/api/v1/targets/t-test"},
|
||||
{"DELETE", "/api/v1/targets/t-test"},
|
||||
{"POST", "/api/v1/targets/t-test/test"},
|
||||
|
||||
// Agents
|
||||
{"GET", "/api/v1/agents"},
|
||||
{"POST", "/api/v1/agents"},
|
||||
{"GET", "/api/v1/agents/agent-1"},
|
||||
{"POST", "/api/v1/agents/agent-1/heartbeat"},
|
||||
{"POST", "/api/v1/agents/agent-1/csr"},
|
||||
{"GET", "/api/v1/agents/agent-1/certificates/mc-1"},
|
||||
{"GET", "/api/v1/agents/agent-1/work"},
|
||||
{"POST", "/api/v1/agents/agent-1/jobs/job-1/status"},
|
||||
|
||||
// Jobs
|
||||
{"GET", "/api/v1/jobs"},
|
||||
{"GET", "/api/v1/jobs/job-1"},
|
||||
{"POST", "/api/v1/jobs/job-1/cancel"},
|
||||
{"POST", "/api/v1/jobs/job-1/approve"},
|
||||
{"POST", "/api/v1/jobs/job-1/reject"},
|
||||
|
||||
// Policies
|
||||
{"GET", "/api/v1/policies"},
|
||||
{"POST", "/api/v1/policies"},
|
||||
{"GET", "/api/v1/policies/pol-1"},
|
||||
{"PUT", "/api/v1/policies/pol-1"},
|
||||
{"DELETE", "/api/v1/policies/pol-1"},
|
||||
{"GET", "/api/v1/policies/pol-1/violations"},
|
||||
|
||||
// Profiles
|
||||
{"GET", "/api/v1/profiles"},
|
||||
{"POST", "/api/v1/profiles"},
|
||||
{"GET", "/api/v1/profiles/prof-1"},
|
||||
{"PUT", "/api/v1/profiles/prof-1"},
|
||||
{"DELETE", "/api/v1/profiles/prof-1"},
|
||||
|
||||
// Teams
|
||||
{"GET", "/api/v1/teams"},
|
||||
{"POST", "/api/v1/teams"},
|
||||
{"GET", "/api/v1/teams/team-1"},
|
||||
|
||||
// Owners
|
||||
{"GET", "/api/v1/owners"},
|
||||
{"POST", "/api/v1/owners"},
|
||||
{"GET", "/api/v1/owners/owner-1"},
|
||||
|
||||
// Agent Groups
|
||||
{"GET", "/api/v1/agent-groups"},
|
||||
{"POST", "/api/v1/agent-groups"},
|
||||
{"GET", "/api/v1/agent-groups/ag-1"},
|
||||
{"GET", "/api/v1/agent-groups/ag-1/members"},
|
||||
|
||||
// Audit
|
||||
{"GET", "/api/v1/audit"},
|
||||
{"GET", "/api/v1/audit/evt-1"},
|
||||
|
||||
// Notifications
|
||||
{"GET", "/api/v1/notifications"},
|
||||
{"GET", "/api/v1/notifications/notif-1"},
|
||||
{"POST", "/api/v1/notifications/notif-1/read"},
|
||||
|
||||
// Stats
|
||||
{"GET", "/api/v1/stats/summary"},
|
||||
{"GET", "/api/v1/stats/certificates-by-status"},
|
||||
{"GET", "/api/v1/stats/expiration-timeline"},
|
||||
{"GET", "/api/v1/stats/job-trends"},
|
||||
{"GET", "/api/v1/stats/issuance-rate"},
|
||||
|
||||
// Metrics
|
||||
{"GET", "/api/v1/metrics"},
|
||||
{"GET", "/api/v1/metrics/prometheus"},
|
||||
|
||||
// Discovery
|
||||
{"POST", "/api/v1/agents/agent-1/discoveries"},
|
||||
{"GET", "/api/v1/discovered-certificates"},
|
||||
{"GET", "/api/v1/discovered-certificates/dc-1"},
|
||||
{"POST", "/api/v1/discovered-certificates/dc-1/claim"},
|
||||
{"POST", "/api/v1/discovered-certificates/dc-1/dismiss"},
|
||||
{"GET", "/api/v1/discovery-scans"},
|
||||
{"GET", "/api/v1/discovery-summary"},
|
||||
|
||||
// Network scan
|
||||
{"GET", "/api/v1/network-scan-targets"},
|
||||
{"POST", "/api/v1/network-scan-targets"},
|
||||
{"GET", "/api/v1/network-scan-targets/nst-1"},
|
||||
{"PUT", "/api/v1/network-scan-targets/nst-1"},
|
||||
{"DELETE", "/api/v1/network-scan-targets/nst-1"},
|
||||
{"POST", "/api/v1/network-scan-targets/nst-1/scan"},
|
||||
|
||||
// Verification
|
||||
{"POST", "/api/v1/jobs/job-1/verify"},
|
||||
{"GET", "/api/v1/jobs/job-1/verification"},
|
||||
|
||||
// Digest
|
||||
{"GET", "/api/v1/digest/preview"},
|
||||
{"POST", "/api/v1/digest/send"},
|
||||
}
|
||||
|
||||
_ = lastCalled // suppress unused
|
||||
|
||||
for _, tc := range routes {
|
||||
t.Run(tc.method+" "+tc.path, func(t *testing.T) {
|
||||
req := httptest.NewRequest(tc.method, tc.path, nil)
|
||||
w := httptest.NewRecorder()
|
||||
testHandler.ServeHTTP(w, req)
|
||||
|
||||
// Route should NOT return 404 (route not found) or 405 (method not allowed)
|
||||
if w.Code == http.StatusNotFound {
|
||||
t.Errorf("route %s %s returned 404 — route not registered", tc.method, tc.path)
|
||||
}
|
||||
if w.Code == http.StatusMethodNotAllowed {
|
||||
t.Errorf("route %s %s returned 405 — method not allowed", tc.method, tc.path)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegisterHandlers_UnregisteredRoute verifies 404 for non-existent route.
|
||||
func TestRegisterHandlers_UnregisteredRoute(t *testing.T) {
|
||||
r := New()
|
||||
reg := HandlerRegistry{
|
||||
Health: handler.NewHealthHandler("api-key"),
|
||||
}
|
||||
r.RegisterHandlers(reg)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/nonexistent", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("expected 404 for nonexistent route, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegisterESTHandlers_AllPaths verifies EST route registration.
|
||||
func TestRegisterESTHandlers_AllPaths(t *testing.T) {
|
||||
r := New()
|
||||
|
||||
// EST handler with zero-value services will panic, so wrap with recovery
|
||||
recoverMW := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if rv := recover(); rv != nil {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
est := handler.ESTHandler{}
|
||||
r.RegisterESTHandlers(est)
|
||||
|
||||
testHandler := recoverMW(r)
|
||||
|
||||
routes := []struct {
|
||||
method string
|
||||
path string
|
||||
}{
|
||||
{"GET", "/.well-known/est/cacerts"},
|
||||
{"POST", "/.well-known/est/simpleenroll"},
|
||||
{"POST", "/.well-known/est/simplereenroll"},
|
||||
{"GET", "/.well-known/est/csrattrs"},
|
||||
}
|
||||
|
||||
for _, tc := range routes {
|
||||
t.Run(tc.method+" "+tc.path, func(t *testing.T) {
|
||||
req := httptest.NewRequest(tc.method, tc.path, nil)
|
||||
w := httptest.NewRecorder()
|
||||
testHandler.ServeHTTP(w, req)
|
||||
|
||||
if w.Code == http.StatusNotFound {
|
||||
t.Errorf("EST route %s %s returned 404 — route not registered", tc.method, tc.path)
|
||||
}
|
||||
if w.Code == http.StatusMethodNotAllowed {
|
||||
t.Errorf("EST route %s %s returned 405", tc.method, tc.path)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetMux_ReturnsUnderlyingMux tests that GetMux returns the underlying mux.
|
||||
func TestGetMux_ReturnsUnderlyingMux(t *testing.T) {
|
||||
r := New()
|
||||
mux := r.GetMux()
|
||||
if mux == nil {
|
||||
t.Fatal("expected non-nil mux from GetMux, got nil")
|
||||
}
|
||||
if mux != r.mux {
|
||||
t.Error("GetMux should return the underlying mux")
|
||||
}
|
||||
}
|
||||
|
||||
// TestMiddlewareOrder tests that middlewares are applied in the correct order.
|
||||
func TestMiddlewareOrder(t *testing.T) {
|
||||
var order []string
|
||||
|
||||
mw1 := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
order = append(order, "mw1-before")
|
||||
next.ServeHTTP(w, r)
|
||||
order = append(order, "mw1-after")
|
||||
})
|
||||
}
|
||||
|
||||
mw2 := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
order = append(order, "mw2-before")
|
||||
next.ServeHTTP(w, r)
|
||||
order = append(order, "mw2-after")
|
||||
})
|
||||
}
|
||||
|
||||
r := NewWithMiddleware(mw1, mw2)
|
||||
|
||||
r.RegisterFunc("GET /test", func(w http.ResponseWriter, r *http.Request) {
|
||||
order = append(order, "handler")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
expected := []string{"mw1-before", "mw2-before", "handler", "mw2-after", "mw1-after"}
|
||||
|
||||
if len(order) != len(expected) {
|
||||
t.Fatalf("middleware order length mismatch: expected %d, got %d", len(expected), len(order))
|
||||
}
|
||||
|
||||
for i, v := range order {
|
||||
if v != expected[i] {
|
||||
t.Errorf("middleware order[%d]: expected %q, got %q", i, expected[i], v)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,16 +23,48 @@ type Config struct {
|
||||
Notifiers NotifierConfig
|
||||
NetworkScan NetworkScanConfig
|
||||
EST ESTConfig
|
||||
SCEP SCEPConfig
|
||||
Verification VerificationConfig
|
||||
ACME ACMEConfig
|
||||
Vault VaultConfig
|
||||
DigiCert DigiCertConfig
|
||||
Sectigo SectigoConfig
|
||||
GoogleCAS GoogleCASConfig
|
||||
AWSACMPCA AWSACMPCAConfig
|
||||
Digest DigestConfig
|
||||
Encryption EncryptionConfig
|
||||
}
|
||||
|
||||
// AWSACMPCAConfig contains AWS ACM Private CA issuer connector configuration.
|
||||
type AWSACMPCAConfig struct {
|
||||
// Region is the AWS region where the Private CA resides (e.g., "us-east-1").
|
||||
// Required for AWS ACM PCA integration.
|
||||
// Setting: CERTCTL_AWS_PCA_REGION environment variable.
|
||||
Region string
|
||||
|
||||
// CAArn is the ARN of the ACM Private CA certificate authority.
|
||||
// Format: arn:aws:acm-pca:<region>:<account>:certificate-authority/<id>
|
||||
// Required for AWS ACM PCA integration.
|
||||
// Setting: CERTCTL_AWS_PCA_CA_ARN environment variable.
|
||||
CAArn string
|
||||
|
||||
// SigningAlgorithm is the signing algorithm for certificate issuance.
|
||||
// Valid: SHA256WITHRSA, SHA384WITHRSA, SHA512WITHRSA, SHA256WITHECDSA, SHA384WITHECDSA, SHA512WITHECDSA.
|
||||
// Default: "SHA256WITHRSA".
|
||||
// Setting: CERTCTL_AWS_PCA_SIGNING_ALGORITHM environment variable.
|
||||
SigningAlgorithm string
|
||||
|
||||
// ValidityDays is the certificate validity period in days.
|
||||
// Default: 365.
|
||||
// Setting: CERTCTL_AWS_PCA_VALIDITY_DAYS environment variable.
|
||||
ValidityDays int
|
||||
|
||||
// TemplateArn is the optional ARN of an ACM PCA certificate template.
|
||||
// Used for constrained subordinate CAs or custom certificate profiles.
|
||||
// Setting: CERTCTL_AWS_PCA_TEMPLATE_ARN environment variable.
|
||||
TemplateArn string
|
||||
}
|
||||
|
||||
// EncryptionConfig contains configuration for encrypting sensitive data at rest.
|
||||
type EncryptionConfig struct {
|
||||
// ConfigEncryptionKey is the passphrase used to derive AES-256-GCM keys for encrypting
|
||||
@@ -386,6 +418,26 @@ type ESTConfig struct {
|
||||
ProfileID string
|
||||
}
|
||||
|
||||
// SCEPConfig controls the RFC 8894 Simple Certificate Enrollment Protocol server.
|
||||
type SCEPConfig struct {
|
||||
// Enabled controls whether SCEP endpoints are available for device enrollment.
|
||||
// Default: false (SCEP disabled). Set to true to enable SCEP endpoints under /scep/.
|
||||
Enabled bool
|
||||
|
||||
// IssuerID selects which issuer connector processes SCEP certificate requests.
|
||||
// Default: "iss-local". Must reference a configured issuer.
|
||||
IssuerID string
|
||||
|
||||
// ProfileID optionally constrains SCEP enrollments to a specific certificate profile.
|
||||
// Leave empty to allow SCEP to use any configured issuer's defaults.
|
||||
ProfileID string
|
||||
|
||||
// ChallengePassword is the shared secret used to authenticate SCEP enrollment requests.
|
||||
// Clients include this in the PKCS#10 CSR challengePassword attribute.
|
||||
// Required when SCEP is enabled.
|
||||
ChallengePassword string
|
||||
}
|
||||
|
||||
// NetworkScanConfig controls the server-side active TLS scanner.
|
||||
type NetworkScanConfig struct {
|
||||
Enabled bool // Enable network scanning (default false)
|
||||
@@ -563,6 +615,12 @@ func Load() (*Config, error) {
|
||||
IssuerID: getEnv("CERTCTL_EST_ISSUER_ID", "iss-local"),
|
||||
ProfileID: getEnv("CERTCTL_EST_PROFILE_ID", ""),
|
||||
},
|
||||
SCEP: SCEPConfig{
|
||||
Enabled: getEnvBool("CERTCTL_SCEP_ENABLED", false),
|
||||
IssuerID: getEnv("CERTCTL_SCEP_ISSUER_ID", "iss-local"),
|
||||
ProfileID: getEnv("CERTCTL_SCEP_PROFILE_ID", ""),
|
||||
ChallengePassword: getEnv("CERTCTL_SCEP_CHALLENGE_PASSWORD", ""),
|
||||
},
|
||||
Verification: VerificationConfig{
|
||||
Enabled: getEnvBool("CERTCTL_VERIFY_DEPLOYMENT", true),
|
||||
Timeout: getEnvDuration("CERTCTL_VERIFY_TIMEOUT", 10*time.Second),
|
||||
@@ -597,6 +655,13 @@ func Load() (*Config, error) {
|
||||
Credentials: getEnv("CERTCTL_GOOGLE_CAS_CREDENTIALS", ""),
|
||||
TTL: getEnv("CERTCTL_GOOGLE_CAS_TTL", "8760h"),
|
||||
},
|
||||
AWSACMPCA: AWSACMPCAConfig{
|
||||
Region: getEnv("CERTCTL_AWS_PCA_REGION", ""),
|
||||
CAArn: getEnv("CERTCTL_AWS_PCA_CA_ARN", ""),
|
||||
SigningAlgorithm: getEnv("CERTCTL_AWS_PCA_SIGNING_ALGORITHM", "SHA256WITHRSA"),
|
||||
ValidityDays: getEnvInt("CERTCTL_AWS_PCA_VALIDITY_DAYS", 365),
|
||||
TemplateArn: getEnv("CERTCTL_AWS_PCA_TEMPLATE_ARN", ""),
|
||||
},
|
||||
ACME: ACMEConfig{
|
||||
DirectoryURL: getEnv("CERTCTL_ACME_DIRECTORY_URL", ""),
|
||||
Email: getEnv("CERTCTL_ACME_EMAIL", ""),
|
||||
|
||||
@@ -0,0 +1,708 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// clearCertctlEnv unsets all CERTCTL_* environment variables to ensure test isolation.
|
||||
func clearCertctlEnv(t *testing.T) {
|
||||
t.Helper()
|
||||
for _, env := range os.Environ() {
|
||||
for i := 0; i < len(env); i++ {
|
||||
if env[i] == '=' {
|
||||
key := env[:i]
|
||||
if len(key) > 7 && key[:8] == "CERTCTL_" {
|
||||
t.Setenv(key, "")
|
||||
os.Unsetenv(key)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// setMinimalValidEnv sets the minimum env vars needed for Load() to succeed (Validate passes).
|
||||
func setMinimalValidEnv(t *testing.T) {
|
||||
t.Helper()
|
||||
// api-key auth requires a secret
|
||||
t.Setenv("CERTCTL_AUTH_SECRET", "test-secret-key")
|
||||
}
|
||||
|
||||
func TestLoad_DefaultValues(t *testing.T) {
|
||||
clearCertctlEnv(t)
|
||||
setMinimalValidEnv(t)
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() returned error: %v", err)
|
||||
}
|
||||
|
||||
// Server defaults
|
||||
if cfg.Server.Host != "127.0.0.1" {
|
||||
t.Errorf("Server.Host = %q, want %q", cfg.Server.Host, "127.0.0.1")
|
||||
}
|
||||
if cfg.Server.Port != 8080 {
|
||||
t.Errorf("Server.Port = %d, want %d", cfg.Server.Port, 8080)
|
||||
}
|
||||
if cfg.Server.MaxBodySize != 1024*1024 {
|
||||
t.Errorf("Server.MaxBodySize = %d, want %d", cfg.Server.MaxBodySize, 1024*1024)
|
||||
}
|
||||
|
||||
// Auth defaults
|
||||
if cfg.Auth.Type != "api-key" {
|
||||
t.Errorf("Auth.Type = %q, want %q", cfg.Auth.Type, "api-key")
|
||||
}
|
||||
|
||||
// Keygen defaults
|
||||
if cfg.Keygen.Mode != "agent" {
|
||||
t.Errorf("Keygen.Mode = %q, want %q", cfg.Keygen.Mode, "agent")
|
||||
}
|
||||
|
||||
// RateLimit defaults
|
||||
if cfg.RateLimit.Enabled != true {
|
||||
t.Errorf("RateLimit.Enabled = %v, want true", cfg.RateLimit.Enabled)
|
||||
}
|
||||
if cfg.RateLimit.RPS != 50 {
|
||||
t.Errorf("RateLimit.RPS = %f, want 50", cfg.RateLimit.RPS)
|
||||
}
|
||||
if cfg.RateLimit.BurstSize != 100 {
|
||||
t.Errorf("RateLimit.BurstSize = %d, want 100", cfg.RateLimit.BurstSize)
|
||||
}
|
||||
|
||||
// Log defaults
|
||||
if cfg.Log.Level != "info" {
|
||||
t.Errorf("Log.Level = %q, want %q", cfg.Log.Level, "info")
|
||||
}
|
||||
if cfg.Log.Format != "json" {
|
||||
t.Errorf("Log.Format = %q, want %q", cfg.Log.Format, "json")
|
||||
}
|
||||
|
||||
// Scheduler defaults
|
||||
if cfg.Scheduler.RenewalCheckInterval != 1*time.Hour {
|
||||
t.Errorf("Scheduler.RenewalCheckInterval = %v, want 1h", cfg.Scheduler.RenewalCheckInterval)
|
||||
}
|
||||
if cfg.Scheduler.JobProcessorInterval != 30*time.Second {
|
||||
t.Errorf("Scheduler.JobProcessorInterval = %v, want 30s", cfg.Scheduler.JobProcessorInterval)
|
||||
}
|
||||
|
||||
// ACME defaults
|
||||
if cfg.ACME.ChallengeType != "http-01" {
|
||||
t.Errorf("ACME.ChallengeType = %q, want %q", cfg.ACME.ChallengeType, "http-01")
|
||||
}
|
||||
|
||||
// Vault defaults
|
||||
if cfg.Vault.Mount != "pki" {
|
||||
t.Errorf("Vault.Mount = %q, want %q", cfg.Vault.Mount, "pki")
|
||||
}
|
||||
if cfg.Vault.TTL != "8760h" {
|
||||
t.Errorf("Vault.TTL = %q, want %q", cfg.Vault.TTL, "8760h")
|
||||
}
|
||||
|
||||
// EST defaults
|
||||
if cfg.EST.Enabled != false {
|
||||
t.Errorf("EST.Enabled = %v, want false", cfg.EST.Enabled)
|
||||
}
|
||||
if cfg.EST.IssuerID != "iss-local" {
|
||||
t.Errorf("EST.IssuerID = %q, want %q", cfg.EST.IssuerID, "iss-local")
|
||||
}
|
||||
|
||||
// Verification defaults
|
||||
if cfg.Verification.Enabled != true {
|
||||
t.Errorf("Verification.Enabled = %v, want true", cfg.Verification.Enabled)
|
||||
}
|
||||
|
||||
// Digest defaults
|
||||
if cfg.Digest.Enabled != false {
|
||||
t.Errorf("Digest.Enabled = %v, want false", cfg.Digest.Enabled)
|
||||
}
|
||||
if cfg.Digest.Interval != 24*time.Hour {
|
||||
t.Errorf("Digest.Interval = %v, want 24h", cfg.Digest.Interval)
|
||||
}
|
||||
|
||||
// Database defaults
|
||||
if cfg.Database.URL != "postgres://localhost/certctl" {
|
||||
t.Errorf("Database.URL = %q, want default", cfg.Database.URL)
|
||||
}
|
||||
if cfg.Database.MaxConnections != 25 {
|
||||
t.Errorf("Database.MaxConnections = %d, want 25", cfg.Database.MaxConnections)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_AllEnvVarsSet(t *testing.T) {
|
||||
clearCertctlEnv(t)
|
||||
|
||||
t.Setenv("CERTCTL_SERVER_HOST", "0.0.0.0")
|
||||
t.Setenv("CERTCTL_SERVER_PORT", "9090")
|
||||
t.Setenv("CERTCTL_MAX_BODY_SIZE", "2097152")
|
||||
t.Setenv("CERTCTL_AUTH_TYPE", "api-key")
|
||||
t.Setenv("CERTCTL_AUTH_SECRET", "my-secret")
|
||||
t.Setenv("CERTCTL_RATE_LIMIT_ENABLED", "false")
|
||||
t.Setenv("CERTCTL_RATE_LIMIT_RPS", "100")
|
||||
t.Setenv("CERTCTL_RATE_LIMIT_BURST", "200")
|
||||
t.Setenv("CERTCTL_CORS_ORIGINS", "https://a.com,https://b.com")
|
||||
t.Setenv("CERTCTL_KEYGEN_MODE", "server")
|
||||
t.Setenv("CERTCTL_LOG_LEVEL", "debug")
|
||||
t.Setenv("CERTCTL_LOG_FORMAT", "text")
|
||||
t.Setenv("CERTCTL_DATABASE_URL", "postgres://user:pass@db:5432/certctl")
|
||||
t.Setenv("CERTCTL_DATABASE_MAX_CONNS", "50")
|
||||
t.Setenv("CERTCTL_SCHEDULER_RENEWAL_CHECK_INTERVAL", "2h")
|
||||
t.Setenv("CERTCTL_SCHEDULER_JOB_PROCESSOR_INTERVAL", "1m")
|
||||
t.Setenv("CERTCTL_SCHEDULER_AGENT_HEALTH_CHECK_INTERVAL", "5m")
|
||||
t.Setenv("CERTCTL_SCHEDULER_NOTIFICATION_PROCESS_INTERVAL", "2m")
|
||||
t.Setenv("CERTCTL_VAULT_ADDR", "https://vault:8200")
|
||||
t.Setenv("CERTCTL_VAULT_TOKEN", "hvs.test")
|
||||
t.Setenv("CERTCTL_VAULT_MOUNT", "pki-int")
|
||||
t.Setenv("CERTCTL_VAULT_ROLE", "web")
|
||||
t.Setenv("CERTCTL_VAULT_TTL", "720h")
|
||||
t.Setenv("CERTCTL_ACME_CHALLENGE_TYPE", "dns-01")
|
||||
t.Setenv("CERTCTL_ACME_ARI_ENABLED", "true")
|
||||
t.Setenv("CERTCTL_EST_ENABLED", "true")
|
||||
t.Setenv("CERTCTL_EST_ISSUER_ID", "iss-acme")
|
||||
t.Setenv("CERTCTL_DIGEST_ENABLED", "true")
|
||||
t.Setenv("CERTCTL_DIGEST_INTERVAL", "12h")
|
||||
t.Setenv("CERTCTL_DIGEST_RECIPIENTS", "alice@co.com,bob@co.com")
|
||||
t.Setenv("CERTCTL_SMTP_HOST", "smtp.example.com")
|
||||
t.Setenv("CERTCTL_SMTP_PORT", "465")
|
||||
t.Setenv("CERTCTL_SMTP_FROM_ADDRESS", "noreply@co.com")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() returned error: %v", err)
|
||||
}
|
||||
|
||||
if cfg.Server.Host != "0.0.0.0" {
|
||||
t.Errorf("Server.Host = %q, want %q", cfg.Server.Host, "0.0.0.0")
|
||||
}
|
||||
if cfg.Server.Port != 9090 {
|
||||
t.Errorf("Server.Port = %d, want 9090", cfg.Server.Port)
|
||||
}
|
||||
if cfg.Server.MaxBodySize != 2097152 {
|
||||
t.Errorf("Server.MaxBodySize = %d, want 2097152", cfg.Server.MaxBodySize)
|
||||
}
|
||||
if cfg.RateLimit.Enabled != false {
|
||||
t.Errorf("RateLimit.Enabled = %v, want false", cfg.RateLimit.Enabled)
|
||||
}
|
||||
if cfg.RateLimit.RPS != 100 {
|
||||
t.Errorf("RateLimit.RPS = %f, want 100", cfg.RateLimit.RPS)
|
||||
}
|
||||
if cfg.RateLimit.BurstSize != 200 {
|
||||
t.Errorf("RateLimit.BurstSize = %d, want 200", cfg.RateLimit.BurstSize)
|
||||
}
|
||||
if len(cfg.CORS.AllowedOrigins) != 2 {
|
||||
t.Errorf("CORS.AllowedOrigins has %d items, want 2", len(cfg.CORS.AllowedOrigins))
|
||||
} else {
|
||||
if cfg.CORS.AllowedOrigins[0] != "https://a.com" {
|
||||
t.Errorf("CORS.AllowedOrigins[0] = %q, want %q", cfg.CORS.AllowedOrigins[0], "https://a.com")
|
||||
}
|
||||
if cfg.CORS.AllowedOrigins[1] != "https://b.com" {
|
||||
t.Errorf("CORS.AllowedOrigins[1] = %q, want %q", cfg.CORS.AllowedOrigins[1], "https://b.com")
|
||||
}
|
||||
}
|
||||
if cfg.Keygen.Mode != "server" {
|
||||
t.Errorf("Keygen.Mode = %q, want %q", cfg.Keygen.Mode, "server")
|
||||
}
|
||||
if cfg.Log.Level != "debug" {
|
||||
t.Errorf("Log.Level = %q, want %q", cfg.Log.Level, "debug")
|
||||
}
|
||||
if cfg.Log.Format != "text" {
|
||||
t.Errorf("Log.Format = %q, want %q", cfg.Log.Format, "text")
|
||||
}
|
||||
if cfg.Database.MaxConnections != 50 {
|
||||
t.Errorf("Database.MaxConnections = %d, want 50", cfg.Database.MaxConnections)
|
||||
}
|
||||
if cfg.Scheduler.RenewalCheckInterval != 2*time.Hour {
|
||||
t.Errorf("Scheduler.RenewalCheckInterval = %v, want 2h", cfg.Scheduler.RenewalCheckInterval)
|
||||
}
|
||||
if cfg.Scheduler.JobProcessorInterval != 1*time.Minute {
|
||||
t.Errorf("Scheduler.JobProcessorInterval = %v, want 1m", cfg.Scheduler.JobProcessorInterval)
|
||||
}
|
||||
if cfg.Vault.Addr != "https://vault:8200" {
|
||||
t.Errorf("Vault.Addr = %q, want %q", cfg.Vault.Addr, "https://vault:8200")
|
||||
}
|
||||
if cfg.Vault.Mount != "pki-int" {
|
||||
t.Errorf("Vault.Mount = %q, want %q", cfg.Vault.Mount, "pki-int")
|
||||
}
|
||||
if cfg.ACME.ChallengeType != "dns-01" {
|
||||
t.Errorf("ACME.ChallengeType = %q, want %q", cfg.ACME.ChallengeType, "dns-01")
|
||||
}
|
||||
if cfg.ACME.ARIEnabled != true {
|
||||
t.Errorf("ACME.ARIEnabled = %v, want true", cfg.ACME.ARIEnabled)
|
||||
}
|
||||
if cfg.EST.Enabled != true {
|
||||
t.Errorf("EST.Enabled = %v, want true", cfg.EST.Enabled)
|
||||
}
|
||||
if cfg.EST.IssuerID != "iss-acme" {
|
||||
t.Errorf("EST.IssuerID = %q, want %q", cfg.EST.IssuerID, "iss-acme")
|
||||
}
|
||||
if cfg.Digest.Enabled != true {
|
||||
t.Errorf("Digest.Enabled = %v, want true", cfg.Digest.Enabled)
|
||||
}
|
||||
if cfg.Digest.Interval != 12*time.Hour {
|
||||
t.Errorf("Digest.Interval = %v, want 12h", cfg.Digest.Interval)
|
||||
}
|
||||
if len(cfg.Digest.Recipients) != 2 {
|
||||
t.Errorf("Digest.Recipients has %d items, want 2", len(cfg.Digest.Recipients))
|
||||
}
|
||||
if cfg.Notifiers.SMTPHost != "smtp.example.com" {
|
||||
t.Errorf("Notifiers.SMTPHost = %q, want %q", cfg.Notifiers.SMTPHost, "smtp.example.com")
|
||||
}
|
||||
if cfg.Notifiers.SMTPPort != 465 {
|
||||
t.Errorf("Notifiers.SMTPPort = %d, want 465", cfg.Notifiers.SMTPPort)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_InvalidIntEnvVar(t *testing.T) {
|
||||
clearCertctlEnv(t)
|
||||
setMinimalValidEnv(t)
|
||||
t.Setenv("CERTCTL_SERVER_PORT", "notanint")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() should fall back to default, got error: %v", err)
|
||||
}
|
||||
// Falls back to default
|
||||
if cfg.Server.Port != 8080 {
|
||||
t.Errorf("Server.Port = %d, want 8080 (default fallback)", cfg.Server.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_InvalidDurationEnvVar(t *testing.T) {
|
||||
clearCertctlEnv(t)
|
||||
setMinimalValidEnv(t)
|
||||
t.Setenv("CERTCTL_DIGEST_INTERVAL", "notaduration")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() should fall back to default, got error: %v", err)
|
||||
}
|
||||
if cfg.Digest.Interval != 24*time.Hour {
|
||||
t.Errorf("Digest.Interval = %v, want 24h (default fallback)", cfg.Digest.Interval)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_InvalidBoolEnvVar(t *testing.T) {
|
||||
clearCertctlEnv(t)
|
||||
setMinimalValidEnv(t)
|
||||
t.Setenv("CERTCTL_RATE_LIMIT_ENABLED", "notabool")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() should fall back to default, got error: %v", err)
|
||||
}
|
||||
// getEnvBool only matches "true", "1", "yes" — anything else is false
|
||||
if cfg.RateLimit.Enabled != false {
|
||||
t.Errorf("RateLimit.Enabled = %v, want false for invalid bool", cfg.RateLimit.Enabled)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoad_CommaSeparatedList(t *testing.T) {
|
||||
clearCertctlEnv(t)
|
||||
setMinimalValidEnv(t)
|
||||
t.Setenv("CERTCTL_CORS_ORIGINS", "https://a.com, https://b.com , https://c.com")
|
||||
|
||||
cfg, err := Load()
|
||||
if err != nil {
|
||||
t.Fatalf("Load() returned error: %v", err)
|
||||
}
|
||||
if len(cfg.CORS.AllowedOrigins) != 3 {
|
||||
t.Fatalf("CORS.AllowedOrigins has %d items, want 3", len(cfg.CORS.AllowedOrigins))
|
||||
}
|
||||
// trimSpace should handle spaces around items
|
||||
if cfg.CORS.AllowedOrigins[1] != "https://b.com" {
|
||||
t.Errorf("CORS.AllowedOrigins[1] = %q, want %q (trimmed)", cfg.CORS.AllowedOrigins[1], "https://b.com")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_ValidConfig(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: "test-secret"},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err != nil {
|
||||
t.Errorf("Validate() returned error for valid config: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_AuthTypeNone(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "none", Secret: ""},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err != nil {
|
||||
t.Errorf("Validate() returned error for auth type 'none': %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_InvalidAuthType(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "oauth", Secret: "key"},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("Validate() should return error for unsupported auth type 'oauth'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_APIKeyAuth_MissingSecret(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: ""},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("Validate() should return error when api-key auth has empty secret")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_JWTAuth_MissingSecret(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "jwt", Secret: ""},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("Validate() should return error when jwt auth has empty secret")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_InvalidKeygenMode(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: "key"},
|
||||
Keygen: KeygenConfig{Mode: "hybrid"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("Validate() should return error for unsupported keygen mode 'hybrid'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_InvalidPort(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
port int
|
||||
}{
|
||||
{"zero", 0},
|
||||
{"negative", -1},
|
||||
{"too high", 65536},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: tt.port},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: "key"},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Errorf("Validate() should return error for port %d", tt.port)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_EmptyDatabaseURL(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: "key"},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("Validate() should return error for empty database URL")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_InvalidLogLevel(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "verbose", Format: "json"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: "key"},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("Validate() should return error for invalid log level 'verbose'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_InvalidLogFormat(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "yaml"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: "key"},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("Validate() should return error for invalid log format 'yaml'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_SchedulerIntervalTooSmall(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg SchedulerConfig
|
||||
}{
|
||||
{
|
||||
"renewal interval below 1 minute",
|
||||
SchedulerConfig{
|
||||
RenewalCheckInterval: 30 * time.Second,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
},
|
||||
{
|
||||
"job processor below 1 second",
|
||||
SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 500 * time.Millisecond,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
},
|
||||
{
|
||||
"agent health below 1 second",
|
||||
SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 500 * time.Millisecond,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
},
|
||||
{
|
||||
"notification below 1 second",
|
||||
SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 500 * time.Millisecond,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 25},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: "key"},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: tt.cfg,
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Errorf("Validate() should return error for %s", tt.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_DatabaseMaxConnectionsZero(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Server: ServerConfig{Port: 8080},
|
||||
Database: DatabaseConfig{URL: "postgres://localhost/certctl", MaxConnections: 0},
|
||||
Log: LogConfig{Level: "info", Format: "json"},
|
||||
Auth: AuthConfig{Type: "api-key", Secret: "key"},
|
||||
Keygen: KeygenConfig{Mode: "agent"},
|
||||
Scheduler: SchedulerConfig{
|
||||
RenewalCheckInterval: 1 * time.Hour,
|
||||
JobProcessorInterval: 30 * time.Second,
|
||||
AgentHealthCheckInterval: 2 * time.Minute,
|
||||
NotificationProcessInterval: 1 * time.Minute,
|
||||
},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Error("Validate() should return error for max_connections=0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetLogLevel_AllLevels(t *testing.T) {
|
||||
tests := []struct {
|
||||
level string
|
||||
expected slog.Level
|
||||
}{
|
||||
{"debug", slog.LevelDebug},
|
||||
{"info", slog.LevelInfo},
|
||||
{"warn", slog.LevelWarn},
|
||||
{"error", slog.LevelError},
|
||||
{"unknown", slog.LevelInfo}, // default fallback
|
||||
{"", slog.LevelInfo}, // empty string
|
||||
{"DEBUG", slog.LevelInfo}, // case-sensitive, no match → default
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.level, func(t *testing.T) {
|
||||
cfg := &Config{Log: LogConfig{Level: tt.level}}
|
||||
got := cfg.GetLogLevel()
|
||||
if got != tt.expected {
|
||||
t.Errorf("GetLogLevel() for %q = %v, want %v", tt.level, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Test helper functions
|
||||
func TestSplitComma(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
{"a,b,c", []string{"a", "b", "c"}},
|
||||
{"single", []string{"single"}},
|
||||
{"", []string{""}},
|
||||
{",", []string{"", ""}},
|
||||
{"a,,c", []string{"a", "", "c"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
got := splitComma(tt.input)
|
||||
if len(got) != len(tt.expected) {
|
||||
t.Fatalf("splitComma(%q) returned %d items, want %d", tt.input, len(got), len(tt.expected))
|
||||
}
|
||||
for i, v := range got {
|
||||
if v != tt.expected[i] {
|
||||
t.Errorf("splitComma(%q)[%d] = %q, want %q", tt.input, i, v, tt.expected[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimSpace(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{" hello ", "hello"},
|
||||
{"hello", "hello"},
|
||||
{"\thello\t", "hello"},
|
||||
{" ", ""},
|
||||
{"", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
got := trimSpace(tt.input)
|
||||
if got != tt.expected {
|
||||
t.Errorf("trimSpace(%q) = %q, want %q", tt.input, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnvFloat(t *testing.T) {
|
||||
t.Setenv("TEST_FLOAT", "3.14")
|
||||
got := getEnvFloat("TEST_FLOAT", 0)
|
||||
if got != 3.14 {
|
||||
t.Errorf("getEnvFloat = %f, want 3.14", got)
|
||||
}
|
||||
|
||||
// Invalid float falls back to default
|
||||
t.Setenv("TEST_FLOAT_BAD", "notafloat")
|
||||
got = getEnvFloat("TEST_FLOAT_BAD", 99.9)
|
||||
if got != 99.9 {
|
||||
t.Errorf("getEnvFloat for invalid = %f, want 99.9", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetEnvBool(t *testing.T) {
|
||||
tests := []struct {
|
||||
value string
|
||||
expected bool
|
||||
}{
|
||||
{"true", true},
|
||||
{"1", true},
|
||||
{"yes", true},
|
||||
{"false", false},
|
||||
{"0", false},
|
||||
{"no", false},
|
||||
{"anything", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.value, func(t *testing.T) {
|
||||
t.Setenv("TEST_BOOL", tt.value)
|
||||
got := getEnvBool("TEST_BOOL", false)
|
||||
if got != tt.expected {
|
||||
t.Errorf("getEnvBool(%q) = %v, want %v", tt.value, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,25 @@ package acme
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer"
|
||||
)
|
||||
|
||||
func testLogger() *slog.Logger {
|
||||
@@ -262,3 +272,775 @@ func TestEnsureClient_ZeroSSLAutoEAB(t *testing.T) {
|
||||
t.Errorf("expected auto-fetched EABHmac, got: %s", c.config.EABHmac)
|
||||
}
|
||||
}
|
||||
|
||||
// --- parseCSRPEM tests ---
|
||||
|
||||
func TestParseCSRPEM_ValidPEM(t *testing.T) {
|
||||
// Generate a real ECDSA P-256 CSR using crypto/x509
|
||||
key, err := generateTestKey()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate test key: %v", err)
|
||||
}
|
||||
|
||||
csrTemplate := x509.CertificateRequest{
|
||||
Subject: generateTestName("test.example.com"),
|
||||
DNSNames: []string{"test.example.com", "www.test.example.com"},
|
||||
PublicKey: &key.PublicKey,
|
||||
}
|
||||
|
||||
csrDER, err := x509.CreateCertificateRequest(nil, &csrTemplate, key)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create CSR: %v", err)
|
||||
}
|
||||
|
||||
csrPEM := string(pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE REQUEST",
|
||||
Bytes: csrDER,
|
||||
}))
|
||||
|
||||
// Test parseCSRPEM
|
||||
result, err := parseCSRPEM(csrPEM)
|
||||
if err != nil {
|
||||
t.Fatalf("parseCSRPEM failed: %v", err)
|
||||
}
|
||||
|
||||
if len(result) == 0 {
|
||||
t.Fatal("expected non-empty DER bytes")
|
||||
}
|
||||
|
||||
// Verify it's valid DER by parsing it
|
||||
parsed, err := x509.ParseCertificateRequest(result)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse result as valid CSR: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(parsed.Subject.String(), "test.example.com") {
|
||||
t.Errorf("expected CN in parsed CSR, got: %s", parsed.Subject.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCSRPEM_InvalidPEM(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
pem string
|
||||
wantErr bool
|
||||
}{
|
||||
{"empty string", "", true},
|
||||
{"not PEM format", "not-a-pem", true},
|
||||
{"valid PEM but wrong type", "-----BEGIN CERTIFICATE-----\ntest\n-----END CERTIFICATE-----", true},
|
||||
{"invalid base64", "-----BEGIN CERTIFICATE REQUEST-----\n!!!not-valid-base64!!!\n-----END CERTIFICATE REQUEST-----", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := parseCSRPEM(tt.pem)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseCSRPEM() error = %v, wantErr = %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// --- parseDERChain tests ---
|
||||
|
||||
func TestParseDERChain_ValidChain(t *testing.T) {
|
||||
// Generate a root and leaf certificate for testing
|
||||
rootKey, err := generateTestKey()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate root key: %v", err)
|
||||
}
|
||||
|
||||
leafKey, err := generateTestKey()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate leaf key: %v", err)
|
||||
}
|
||||
|
||||
// Root cert (self-signed)
|
||||
rootTemplate := x509.Certificate{
|
||||
Subject: generateTestName("Root CA"),
|
||||
SerialNumber: big.NewInt(1),
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(10, 0, 0),
|
||||
KeyUsage: x509.KeyUsageCertSign,
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
}
|
||||
|
||||
rootDER, err := x509.CreateCertificate(nil, &rootTemplate, &rootTemplate, &rootKey.PublicKey, rootKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create root cert: %v", err)
|
||||
}
|
||||
|
||||
// Leaf cert (signed by root)
|
||||
leafTemplate := x509.Certificate{
|
||||
Subject: generateTestName("test.example.com"),
|
||||
SerialNumber: big.NewInt(100),
|
||||
DNSNames: []string{"test.example.com", "www.test.example.com"},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(1, 0, 0),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
|
||||
PublicKey: &leafKey.PublicKey,
|
||||
}
|
||||
|
||||
leafDER, err := x509.CreateCertificate(nil, &leafTemplate, &rootTemplate, &leafKey.PublicKey, rootKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create leaf cert: %v", err)
|
||||
}
|
||||
|
||||
// Parse the chain
|
||||
certPEM, chainPEM, serial, notBefore, notAfter, err := parseDERChain([][]byte{leafDER, rootDER})
|
||||
if err != nil {
|
||||
t.Fatalf("parseDERChain failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify leaf cert PEM
|
||||
if !strings.Contains(certPEM, "BEGIN CERTIFICATE") {
|
||||
t.Errorf("certPEM should contain PEM header, got: %s", certPEM)
|
||||
}
|
||||
|
||||
// Verify chain PEM contains root
|
||||
if !strings.Contains(chainPEM, "BEGIN CERTIFICATE") {
|
||||
t.Errorf("chainPEM should contain root cert PEM, got: %s", chainPEM)
|
||||
}
|
||||
|
||||
// Verify serial is correctly extracted
|
||||
if serial != "100" {
|
||||
t.Errorf("expected serial '100', got: %s", serial)
|
||||
}
|
||||
|
||||
// Verify timestamps are set
|
||||
if notBefore.IsZero() {
|
||||
t.Error("notBefore should not be zero")
|
||||
}
|
||||
if notAfter.IsZero() {
|
||||
t.Error("notAfter should not be zero")
|
||||
}
|
||||
|
||||
// Verify we can parse the returned PEM
|
||||
block, _ := pem.Decode([]byte(certPEM))
|
||||
if block == nil {
|
||||
t.Fatal("failed to decode returned certPEM")
|
||||
}
|
||||
|
||||
parsedLeaf, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to parse returned certPEM: %v", err)
|
||||
}
|
||||
|
||||
if parsedLeaf.SerialNumber.Cmp(big.NewInt(100)) != 0 {
|
||||
t.Errorf("parsed leaf serial mismatch: got %v, expected 100", parsedLeaf.SerialNumber)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDERChain_SingleCert(t *testing.T) {
|
||||
// Generate a single certificate
|
||||
key, err := generateTestKey()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate key: %v", err)
|
||||
}
|
||||
|
||||
template := x509.Certificate{
|
||||
Subject: generateTestName("test.example.com"),
|
||||
SerialNumber: big.NewInt(42),
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(1, 0, 0),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature,
|
||||
PublicKey: &key.PublicKey,
|
||||
}
|
||||
|
||||
certDER, err := x509.CreateCertificate(nil, &template, &template, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create cert: %v", err)
|
||||
}
|
||||
|
||||
certPEM, chainPEM, serial, notBefore, notAfter, err := parseDERChain([][]byte{certDER})
|
||||
if err != nil {
|
||||
t.Fatalf("parseDERChain failed: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(certPEM, "BEGIN CERTIFICATE") {
|
||||
t.Error("certPEM should contain PEM header")
|
||||
}
|
||||
|
||||
if chainPEM != "" {
|
||||
t.Errorf("chainPEM should be empty for single cert, got: %s", chainPEM)
|
||||
}
|
||||
|
||||
if serial != "42" {
|
||||
t.Errorf("expected serial '42', got: %s", serial)
|
||||
}
|
||||
|
||||
if notBefore.IsZero() || notAfter.IsZero() {
|
||||
t.Error("timestamps should be set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDERChain_EmptyChain(t *testing.T) {
|
||||
_, _, _, _, _, err := parseDERChain([][]byte{})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty chain")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "empty") {
|
||||
t.Errorf("expected 'empty' in error message, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseDERChain_InvalidDER(t *testing.T) {
|
||||
// Invalid DER bytes
|
||||
invalidDER := []byte{0xFF, 0xFF, 0xFF}
|
||||
_, _, _, _, _, err := parseDERChain([][]byte{invalidDER})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid DER")
|
||||
}
|
||||
}
|
||||
|
||||
// --- IssueCertificate / RenewCertificate error path tests ---
|
||||
// Note: Full IssueCertificate/RenewCertificate testing requires an ACME server.
|
||||
// We test the CSR parsing logic which is the first step.
|
||||
|
||||
func TestIssueCertificateCSRParsing(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
csrPEM string
|
||||
wantErr bool
|
||||
}{
|
||||
{"invalid PEM", "not-a-valid-csr-pem", true},
|
||||
{"empty PEM", "", true},
|
||||
{"wrong PEM type", "-----BEGIN CERTIFICATE-----\nMIID\n-----END CERTIFICATE-----", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := parseCSRPEM(tt.csrPEM)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseCSRPEM() error = %v, wantErr = %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// --- RevokeCertificate behavior test ---
|
||||
// ACME revocation is not fully supported in V1 — it requires certificate DER, not just the serial.
|
||||
// Full testing would require an ACME server; we verify the basic interface behavior.
|
||||
// Skipped here because it requires network access for ACME client initialization.
|
||||
|
||||
// --- GenerateCRL and SignOCSPResponse error path tests ---
|
||||
|
||||
func TestGenerateCRL_NotSupported(t *testing.T) {
|
||||
c := New(&Config{
|
||||
DirectoryURL: "https://example.com/acme/directory",
|
||||
Email: "test@example.com",
|
||||
}, testLogger())
|
||||
|
||||
_, err := c.GenerateCRL(context.Background(), nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for CRL generation")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not support") {
|
||||
t.Errorf("expected 'not support' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignOCSPResponse_NotSupported(t *testing.T) {
|
||||
c := New(&Config{
|
||||
DirectoryURL: "https://example.com/acme/directory",
|
||||
Email: "test@example.com",
|
||||
}, testLogger())
|
||||
|
||||
req := issuer.OCSPSignRequest{
|
||||
CertSerial: big.NewInt(123),
|
||||
}
|
||||
|
||||
_, err := c.SignOCSPResponse(context.Background(), req)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for OCSP signing")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not support") {
|
||||
t.Errorf("expected 'not support' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCACertPEM_NotSupported(t *testing.T) {
|
||||
c := New(&Config{
|
||||
DirectoryURL: "https://example.com/acme/directory",
|
||||
Email: "test@example.com",
|
||||
}, testLogger())
|
||||
|
||||
_, err := c.GetCACertPEM(context.Background())
|
||||
if err == nil {
|
||||
t.Fatal("expected error for GetCACertPEM")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "not") {
|
||||
t.Errorf("expected error message, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- httpClient behavior tests ---
|
||||
|
||||
func TestHttpClient_DefaultTimeout(t *testing.T) {
|
||||
c := New(&Config{
|
||||
DirectoryURL: "https://example.com/acme/directory",
|
||||
Email: "test@example.com",
|
||||
Insecure: false,
|
||||
}, testLogger())
|
||||
|
||||
client := c.httpClient()
|
||||
if client == nil {
|
||||
t.Fatal("httpClient should not be nil")
|
||||
}
|
||||
if client.Timeout == 0 {
|
||||
t.Error("httpClient should have a non-zero timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpClient_InsecureSkipVerify(t *testing.T) {
|
||||
c := New(&Config{
|
||||
DirectoryURL: "https://example.com/acme/directory",
|
||||
Email: "test@example.com",
|
||||
Insecure: true,
|
||||
}, testLogger())
|
||||
|
||||
client := c.httpClient()
|
||||
if client == nil {
|
||||
t.Fatal("httpClient should not be nil")
|
||||
}
|
||||
|
||||
// Verify that the transport has InsecureSkipVerify enabled
|
||||
if client.Transport == nil {
|
||||
t.Error("client transport should be set for insecure mode")
|
||||
} else {
|
||||
transport := client.Transport.(*http.Transport)
|
||||
if transport.TLSClientConfig == nil || !transport.TLSClientConfig.InsecureSkipVerify {
|
||||
t.Error("TLS config should have InsecureSkipVerify=true")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- buildIdentifiers tests ---
|
||||
|
||||
func TestBuildIdentifiers_CommonNameOnly(t *testing.T) {
|
||||
identifiers := buildIdentifiers("example.com", nil)
|
||||
if len(identifiers) != 1 {
|
||||
t.Fatalf("expected 1 identifier, got %d", len(identifiers))
|
||||
}
|
||||
if identifiers[0].Value != "example.com" {
|
||||
t.Errorf("expected 'example.com', got %s", identifiers[0].Value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIdentifiers_CommonNameAndSANs(t *testing.T) {
|
||||
identifiers := buildIdentifiers("example.com", []string{"www.example.com", "api.example.com"})
|
||||
if len(identifiers) != 3 {
|
||||
t.Fatalf("expected 3 identifiers, got %d", len(identifiers))
|
||||
}
|
||||
|
||||
expected := map[string]bool{
|
||||
"example.com": true,
|
||||
"www.example.com": true,
|
||||
"api.example.com": true,
|
||||
}
|
||||
|
||||
for _, id := range identifiers {
|
||||
if !expected[id.Value] {
|
||||
t.Errorf("unexpected identifier: %s", id.Value)
|
||||
}
|
||||
if id.Type != "dns" {
|
||||
t.Errorf("expected type 'dns', got %s", id.Type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIdentifiers_DeduplicatesCommonName(t *testing.T) {
|
||||
// If CommonName is also in SANs, it should only appear once
|
||||
identifiers := buildIdentifiers("example.com", []string{"example.com", "www.example.com"})
|
||||
if len(identifiers) != 2 {
|
||||
t.Fatalf("expected 2 identifiers (deduplicated), got %d", len(identifiers))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIdentifiers_EmptyCommonName(t *testing.T) {
|
||||
identifiers := buildIdentifiers("", []string{"www.example.com"})
|
||||
if len(identifiers) != 1 {
|
||||
t.Fatalf("expected 1 identifier, got %d", len(identifiers))
|
||||
}
|
||||
if identifiers[0].Value != "www.example.com" {
|
||||
t.Errorf("expected 'www.example.com', got %s", identifiers[0].Value)
|
||||
}
|
||||
}
|
||||
|
||||
// --- New constructor tests ---
|
||||
|
||||
func TestNew_WithNilConfig(t *testing.T) {
|
||||
c := New(nil, testLogger())
|
||||
if c == nil {
|
||||
t.Fatal("New should return a non-nil Connector")
|
||||
}
|
||||
if c.config != nil {
|
||||
t.Error("config should be nil when initialized with nil")
|
||||
}
|
||||
if len(c.challengeTokens) != 0 {
|
||||
t.Error("challengeTokens should be initialized as empty map")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_WithHTTPPort0DefaultsTo80(t *testing.T) {
|
||||
cfg := &Config{
|
||||
DirectoryURL: "https://example.com/acme",
|
||||
Email: "test@example.com",
|
||||
HTTPPort: 0, // Should default to 80
|
||||
ChallengeType: "http-01",
|
||||
}
|
||||
c := New(cfg, testLogger())
|
||||
if c.config.HTTPPort != 80 {
|
||||
t.Errorf("expected HTTPPort to default to 80, got %d", c.config.HTTPPort)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_WithChallengeTypeDefaultsToHTTP01(t *testing.T) {
|
||||
cfg := &Config{
|
||||
DirectoryURL: "https://example.com/acme",
|
||||
Email: "test@example.com",
|
||||
HTTPPort: 8080,
|
||||
// ChallengeType intentionally empty
|
||||
}
|
||||
c := New(cfg, testLogger())
|
||||
if c.config.ChallengeType != "http-01" {
|
||||
t.Errorf("expected ChallengeType to default to http-01, got %s", c.config.ChallengeType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_WithDNSPropagationWaitDefaultsTo30(t *testing.T) {
|
||||
cfg := &Config{
|
||||
DirectoryURL: "https://example.com/acme",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "dns-01",
|
||||
// DNSPropagationWait intentionally 0
|
||||
}
|
||||
c := New(cfg, testLogger())
|
||||
if c.config.DNSPropagationWait != 30 {
|
||||
t.Errorf("expected DNSPropagationWait to default to 30, got %d", c.config.DNSPropagationWait)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_InitializesDNSSolverForDNS01(t *testing.T) {
|
||||
cfg := &Config{
|
||||
DirectoryURL: "https://example.com/acme",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "dns-01",
|
||||
DNSPresentScript: "/bin/sh", // Use a real script that exists
|
||||
}
|
||||
c := New(cfg, testLogger())
|
||||
// DNS solver should be initialized for dns-01
|
||||
if c.dnsSolver == nil && cfg.DNSPresentScript != "" {
|
||||
// Note: it only initializes if the script path is not empty
|
||||
t.Error("dnsSolver should be initialized for dns-01 with present script")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_InitializesDNSSolverForDNSPersist01(t *testing.T) {
|
||||
cfg := &Config{
|
||||
DirectoryURL: "https://example.com/acme",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "dns-persist-01",
|
||||
DNSPresentScript: "/bin/sh", // Use a real script path
|
||||
}
|
||||
c := New(cfg, testLogger())
|
||||
if c.dnsSolver == nil && cfg.DNSPresentScript != "" {
|
||||
t.Error("dnsSolver should be initialized for dns-persist-01 with present script")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_NooDNSSolverForHTTP01(t *testing.T) {
|
||||
cfg := &Config{
|
||||
DirectoryURL: "https://example.com/acme",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "http-01",
|
||||
DNSPresentScript: "/nonexistent/path", // Intentionally not initialized
|
||||
}
|
||||
c := New(cfg, testLogger())
|
||||
if c.dnsSolver != nil {
|
||||
t.Error("dnsSolver should not be initialized for http-01")
|
||||
}
|
||||
}
|
||||
|
||||
// --- ValidateConfig additional coverage tests ---
|
||||
|
||||
func TestValidateConfig_DNSPresentScriptRequired(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"newNonce":"","newAccount":"","newOrder":""}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := New(nil, testLogger())
|
||||
cfg, _ := json.Marshal(map[string]string{
|
||||
"directory_url": srv.URL,
|
||||
"email": "test@example.com",
|
||||
"challenge_type": "dns-01",
|
||||
// Missing dns_present_script
|
||||
})
|
||||
|
||||
err := c.ValidateConfig(context.Background(), cfg)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when dns_present_script is missing for dns-01")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "dns_present_script") {
|
||||
t.Errorf("expected 'dns_present_script' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_DNSPersistIssuerDomainRequired(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"newNonce":"","newAccount":"","newOrder":""}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := New(nil, testLogger())
|
||||
cfg, _ := json.Marshal(map[string]string{
|
||||
"directory_url": srv.URL,
|
||||
"email": "test@example.com",
|
||||
"challenge_type": "dns-persist-01",
|
||||
"dns_present_script": "/tmp/script.sh",
|
||||
// Missing dns_persist_issuer_domain
|
||||
})
|
||||
|
||||
err := c.ValidateConfig(context.Background(), cfg)
|
||||
if err == nil {
|
||||
t.Fatal("expected error when dns_persist_issuer_domain is missing for dns-persist-01")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "dns_persist_issuer_domain") {
|
||||
t.Errorf("expected 'dns_persist_issuer_domain' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_InvalidJSON(t *testing.T) {
|
||||
c := New(nil, testLogger())
|
||||
err := c.ValidateConfig(context.Background(), []byte("{invalid json}"))
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid JSON")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "invalid") {
|
||||
t.Errorf("expected 'invalid' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Note: Profile validation tests are in profile_test.go
|
||||
|
||||
func TestValidateConfig_ACMEDirectoryUnreachable(t *testing.T) {
|
||||
c := New(nil, testLogger())
|
||||
cfg, _ := json.Marshal(map[string]string{
|
||||
"directory_url": "https://127.0.0.1:1/directory", // Unreachable
|
||||
"email": "test@example.com",
|
||||
})
|
||||
|
||||
err := c.ValidateConfig(context.Background(), cfg)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unreachable ACME directory")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_HTTPStatusError(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := New(nil, testLogger())
|
||||
cfg, _ := json.Marshal(map[string]string{
|
||||
"directory_url": srv.URL,
|
||||
"email": "test@example.com",
|
||||
})
|
||||
|
||||
err := c.ValidateConfig(context.Background(), cfg)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for non-2xx status")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "404") {
|
||||
t.Errorf("expected '404' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_DNS01WithPresentScript(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"newNonce":"","newAccount":"","newOrder":""}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := New(nil, testLogger())
|
||||
cfg, _ := json.Marshal(map[string]string{
|
||||
"directory_url": srv.URL,
|
||||
"email": "test@example.com",
|
||||
"challenge_type": "dns-01",
|
||||
"dns_present_script": "/bin/sh",
|
||||
"dns_cleanup_script": "/bin/sh",
|
||||
})
|
||||
|
||||
err := c.ValidateConfig(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("expected DNS-01 with present script to succeed, got: %v", err)
|
||||
}
|
||||
|
||||
// Verify config was updated
|
||||
if c.config.ChallengeType != "dns-01" {
|
||||
t.Errorf("expected ChallengeType=dns-01, got %s", c.config.ChallengeType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_DNSPersist01WithAllFields(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprint(w, `{"newNonce":"","newAccount":"","newOrder":""}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := New(nil, testLogger())
|
||||
cfg, _ := json.Marshal(map[string]string{
|
||||
"directory_url": srv.URL,
|
||||
"email": "test@example.com",
|
||||
"challenge_type": "dns-persist-01",
|
||||
"dns_present_script": "/bin/sh",
|
||||
"dns_persist_issuer_domain": "letsencrypt.org",
|
||||
})
|
||||
|
||||
err := c.ValidateConfig(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("expected DNS-PERSIST-01 to succeed, got: %v", err)
|
||||
}
|
||||
|
||||
if c.config.DNSPersistIssuerDomain != "letsencrypt.org" {
|
||||
t.Errorf("expected issuer domain to be set, got %s", c.config.DNSPersistIssuerDomain)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Additional comprehensive tests ---
|
||||
|
||||
func TestParseDERChain_MultipleChainCerts(t *testing.T) {
|
||||
// Generate a complete chain: leaf -> intermediate -> root
|
||||
rootKey, _ := generateTestKey()
|
||||
intermediateKey, _ := generateTestKey()
|
||||
leafKey, _ := generateTestKey()
|
||||
|
||||
// Root certificate (self-signed)
|
||||
rootTemplate := x509.Certificate{
|
||||
Subject: generateTestName("Root CA"),
|
||||
SerialNumber: big.NewInt(1),
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(20, 0, 0),
|
||||
KeyUsage: x509.KeyUsageCertSign,
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
}
|
||||
rootDER, _ := x509.CreateCertificate(nil, &rootTemplate, &rootTemplate, &rootKey.PublicKey, rootKey)
|
||||
|
||||
// Intermediate certificate (signed by root)
|
||||
intermediateTemplate := x509.Certificate{
|
||||
Subject: generateTestName("Intermediate CA"),
|
||||
SerialNumber: big.NewInt(2),
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(10, 0, 0),
|
||||
KeyUsage: x509.KeyUsageCertSign,
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
PublicKey: &intermediateKey.PublicKey,
|
||||
}
|
||||
intermediateDER, _ := x509.CreateCertificate(nil, &intermediateTemplate, &rootTemplate, &intermediateKey.PublicKey, rootKey)
|
||||
|
||||
// Leaf certificate (signed by intermediate)
|
||||
leafTemplate := x509.Certificate{
|
||||
Subject: generateTestName("leaf.example.com"),
|
||||
SerialNumber: big.NewInt(100),
|
||||
DNSNames: []string{"leaf.example.com"},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(1, 0, 0),
|
||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
|
||||
PublicKey: &leafKey.PublicKey,
|
||||
}
|
||||
leafDER, _ := x509.CreateCertificate(nil, &leafTemplate, &intermediateTemplate, &leafKey.PublicKey, intermediateKey)
|
||||
|
||||
certPEM, chainPEM, serial, _, _, err := parseDERChain([][]byte{leafDER, intermediateDER, rootDER})
|
||||
if err != nil {
|
||||
t.Fatalf("parseDERChain failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify serial from leaf
|
||||
if serial != "100" {
|
||||
t.Errorf("expected serial '100', got: %s", serial)
|
||||
}
|
||||
|
||||
// Verify chainPEM contains both intermediate and root
|
||||
chainCount := strings.Count(chainPEM, "BEGIN CERTIFICATE")
|
||||
if chainCount != 2 {
|
||||
t.Errorf("expected 2 certs in chain, found %d", chainCount)
|
||||
}
|
||||
|
||||
// Verify certPEM contains only the leaf
|
||||
if !strings.Contains(certPEM, "BEGIN CERTIFICATE") {
|
||||
t.Error("certPEM should contain certificate header")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCSRPEM_WithTrailingWhitespace(t *testing.T) {
|
||||
key, _ := generateTestKey()
|
||||
csrTemplate := x509.CertificateRequest{
|
||||
Subject: generateTestName("test.example.com"),
|
||||
PublicKey: &key.PublicKey,
|
||||
}
|
||||
csrDER, _ := x509.CreateCertificateRequest(nil, &csrTemplate, key)
|
||||
csrPEM := string(pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE REQUEST",
|
||||
Bytes: csrDER,
|
||||
}))
|
||||
|
||||
// Add trailing whitespace and newlines
|
||||
csrWithWhitespace := csrPEM + "\n\n \n"
|
||||
|
||||
result, err := parseCSRPEM(csrWithWhitespace)
|
||||
if err != nil {
|
||||
t.Fatalf("parseCSRPEM should handle trailing whitespace, got: %v", err)
|
||||
}
|
||||
|
||||
if len(result) == 0 {
|
||||
t.Fatal("expected non-empty result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCSRPEM_MultipleCSRsInPEM(t *testing.T) {
|
||||
key, _ := generateTestKey()
|
||||
csrTemplate := x509.CertificateRequest{
|
||||
Subject: generateTestName("test.example.com"),
|
||||
PublicKey: &key.PublicKey,
|
||||
}
|
||||
csrDER, _ := x509.CreateCertificateRequest(nil, &csrTemplate, key)
|
||||
csrPEM := string(pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE REQUEST",
|
||||
Bytes: csrDER,
|
||||
}))
|
||||
|
||||
// pem.Decode only returns the first PEM block, so this tests that behavior
|
||||
multiCSRPEM := csrPEM + "\n" + csrPEM
|
||||
|
||||
result, err := parseCSRPEM(multiCSRPEM)
|
||||
if err != nil {
|
||||
t.Fatalf("parseCSRPEM should handle multiple PEMs by decoding the first, got: %v", err)
|
||||
}
|
||||
|
||||
if len(result) == 0 {
|
||||
t.Fatal("expected non-empty result")
|
||||
}
|
||||
}
|
||||
|
||||
// --- Helper functions for tests ---
|
||||
|
||||
func generateTestKey() (*ecdsa.PrivateKey, error) {
|
||||
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
}
|
||||
|
||||
func generateTestName(cn string) pkix.Name {
|
||||
return pkix.Name{
|
||||
CommonName: cn,
|
||||
Organization: []string{"Test Org"},
|
||||
Country: []string{"US"},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,416 @@
|
||||
// Package awsacmpca implements the issuer.Connector interface for AWS Certificate Authority Service (CAS).
|
||||
//
|
||||
// AWS ACM Private CA (ACM PCA) provides a fully managed private certificate authority
|
||||
// with certificate signing, revocation, and CRL capabilities. This connector uses the
|
||||
// AWS ACM PCA API to issue and manage certificates.
|
||||
//
|
||||
// This connector issues certificates synchronously: the IssueCertificate call returns
|
||||
// the issued certificate immediately. GetOrderStatus always returns "completed" since
|
||||
// issuance is synchronous. CRL and OCSP operations are delegated to AWS PCA's own
|
||||
// endpoints.
|
||||
//
|
||||
// Authentication: AWS credentials via the standard credential chain (environment variables,
|
||||
// IAM role, instance profile, or SSO). Configuration specifies the CA ARN, region, and
|
||||
// optional signing algorithm and validity days.
|
||||
//
|
||||
// AWS ACM PCA API used (abstracted via ACMPCAClient interface):
|
||||
//
|
||||
// IssueCertificate - Issue a certificate from a CSR
|
||||
// GetCertificate - Retrieve the issued certificate
|
||||
// RevokeCertificate - Revoke a certificate
|
||||
// GetCACertificate - Get the CA certificate chain
|
||||
package awsacmpca
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer"
|
||||
)
|
||||
|
||||
// Config represents the AWS ACM Private CA issuer connector configuration.
|
||||
type Config struct {
|
||||
// Region is the AWS region where the CA resides (e.g., "us-east-1").
|
||||
// Required. Set via CERTCTL_GOOGLE_CAS_PROJECT environment variable.
|
||||
Region string `json:"region"`
|
||||
|
||||
// CAArn is the ARN of the AWS Certificate Authority Service CA.
|
||||
// Required. Set via CERTCTL_GOOGLE_CAS_CA_ARN environment variable.
|
||||
// Example: arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012
|
||||
CAArn string `json:"ca_arn"`
|
||||
|
||||
// SigningAlgorithm is the algorithm used to sign certificates.
|
||||
// Default: "SHA256WITHRSA". Set via CERTCTL_AWS_PCA_SIGNING_ALGORITHM.
|
||||
// Valid values: SHA256WITHRSA, SHA384WITHRSA, SHA512WITHRSA,
|
||||
// SHA256WITHECDSA, SHA384WITHECDSA, SHA512WITHECDSA
|
||||
SigningAlgorithm string `json:"signing_algorithm,omitempty"`
|
||||
|
||||
// ValidityDays is the number of days the certificate is valid.
|
||||
// Default: 365. Set via CERTCTL_AWS_PCA_VALIDITY_DAYS.
|
||||
ValidityDays int `json:"validity_days,omitempty"`
|
||||
|
||||
// TemplateArn is the optional certificate template ARN for subordinate CAs with restrictions.
|
||||
// Set via CERTCTL_AWS_PCA_TEMPLATE_ARN.
|
||||
TemplateArn string `json:"template_arn,omitempty"`
|
||||
}
|
||||
|
||||
// ACMPCAClient defines the interface for interacting with AWS ACM Private CA.
|
||||
// This allows for dependency injection and testing with mock clients.
|
||||
type ACMPCAClient interface {
|
||||
// IssueCertificate issues a new certificate.
|
||||
IssueCertificate(ctx context.Context, input *IssueCertificateInput) (*IssueCertificateOutput, error)
|
||||
|
||||
// GetCertificate retrieves an issued certificate.
|
||||
GetCertificate(ctx context.Context, input *GetCertificateInput) (*GetCertificateOutput, error)
|
||||
|
||||
// RevokeCertificate revokes a certificate.
|
||||
RevokeCertificate(ctx context.Context, input *RevokeCertificateInput) error
|
||||
|
||||
// GetCACertificate retrieves the CA certificate chain.
|
||||
GetCACertificate(ctx context.Context, input *GetCACertificateInput) (*GetCACertificateOutput, error)
|
||||
}
|
||||
|
||||
// IssueCertificateInput represents the request to issue a certificate.
|
||||
type IssueCertificateInput struct {
|
||||
CAArn string
|
||||
CSR []byte // DER-encoded CSR
|
||||
SigningAlgorithm string
|
||||
ValidityDays int
|
||||
TemplateArn string
|
||||
}
|
||||
|
||||
// IssueCertificateOutput represents the response to an issue request.
|
||||
type IssueCertificateOutput struct {
|
||||
CertificateArn string
|
||||
}
|
||||
|
||||
// GetCertificateInput represents the request to retrieve a certificate.
|
||||
type GetCertificateInput struct {
|
||||
CAArn string
|
||||
CertificateArn string
|
||||
}
|
||||
|
||||
// GetCertificateOutput represents the response containing the certificate.
|
||||
type GetCertificateOutput struct {
|
||||
Certificate string // PEM-encoded certificate
|
||||
CertificateChain string // PEM-encoded certificate chain
|
||||
}
|
||||
|
||||
// RevokeCertificateInput represents the request to revoke a certificate.
|
||||
type RevokeCertificateInput struct {
|
||||
CAArn string
|
||||
CertificateSerial string
|
||||
RevocationReason string
|
||||
}
|
||||
|
||||
// GetCACertificateInput represents the request to retrieve the CA certificate.
|
||||
type GetCACertificateInput struct {
|
||||
CAArn string
|
||||
}
|
||||
|
||||
// GetCACertificateOutput represents the response containing the CA certificate.
|
||||
type GetCACertificateOutput struct {
|
||||
Certificate string // PEM-encoded CA certificate
|
||||
CertificateChain string // PEM-encoded CA chain
|
||||
}
|
||||
|
||||
// Connector implements the issuer.Connector interface for AWS ACM Private CA.
|
||||
type Connector struct {
|
||||
config *Config
|
||||
client ACMPCAClient
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// New creates a new AWS ACM Private CA connector with the given configuration and logger.
|
||||
// The real client will use the AWS SDK via the standard credential chain.
|
||||
func New(config *Config, logger *slog.Logger) *Connector {
|
||||
if config != nil {
|
||||
if config.SigningAlgorithm == "" {
|
||||
config.SigningAlgorithm = "SHA256WITHRSA"
|
||||
}
|
||||
if config.ValidityDays == 0 {
|
||||
config.ValidityDays = 365
|
||||
}
|
||||
}
|
||||
|
||||
return &Connector{
|
||||
config: config,
|
||||
client: &stubClient{}, // Placeholder; real AWS client will be injected or implemented
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// NewWithClient creates a new AWS ACM Private CA connector with a custom client.
|
||||
// Used primarily for testing with mock clients.
|
||||
func NewWithClient(config *Config, client ACMPCAClient, logger *slog.Logger) *Connector {
|
||||
if config != nil {
|
||||
if config.SigningAlgorithm == "" {
|
||||
config.SigningAlgorithm = "SHA256WITHRSA"
|
||||
}
|
||||
if config.ValidityDays == 0 {
|
||||
config.ValidityDays = 365
|
||||
}
|
||||
}
|
||||
|
||||
return &Connector{
|
||||
config: config,
|
||||
client: client,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// stubClient is a placeholder client that returns "not implemented" errors.
|
||||
// In production, this would be replaced with a real AWS SDK client.
|
||||
type stubClient struct{}
|
||||
|
||||
func (s *stubClient) IssueCertificate(ctx context.Context, input *IssueCertificateInput) (*IssueCertificateOutput, error) {
|
||||
return nil, fmt.Errorf("AWS SDK client not initialized (stub)")
|
||||
}
|
||||
|
||||
func (s *stubClient) GetCertificate(ctx context.Context, input *GetCertificateInput) (*GetCertificateOutput, error) {
|
||||
return nil, fmt.Errorf("AWS SDK client not initialized (stub)")
|
||||
}
|
||||
|
||||
func (s *stubClient) RevokeCertificate(ctx context.Context, input *RevokeCertificateInput) error {
|
||||
return fmt.Errorf("AWS SDK client not initialized (stub)")
|
||||
}
|
||||
|
||||
func (s *stubClient) GetCACertificate(ctx context.Context, input *GetCACertificateInput) (*GetCACertificateOutput, error) {
|
||||
return nil, fmt.Errorf("AWS SDK client not initialized (stub)")
|
||||
}
|
||||
|
||||
// ValidateConfig checks that the AWS ACM Private CA configuration is valid.
|
||||
func (c *Connector) ValidateConfig(ctx context.Context, rawConfig json.RawMessage) error {
|
||||
var cfg Config
|
||||
if err := json.Unmarshal(rawConfig, &cfg); err != nil {
|
||||
return fmt.Errorf("invalid AWS ACM PCA config: %w", err)
|
||||
}
|
||||
|
||||
if cfg.Region == "" {
|
||||
return fmt.Errorf("AWS region is required")
|
||||
}
|
||||
|
||||
if cfg.CAArn == "" {
|
||||
return fmt.Errorf("AWS CA ARN is required")
|
||||
}
|
||||
|
||||
// Validate ARN format: arn:aws(-[a-z]+)?:acm-pca:[a-z0-9-]+:\d{12}:certificate-authority/[a-f0-9-]+
|
||||
arnPattern := regexp.MustCompile(`^arn:aws(-[a-z]+)?:acm-pca:[a-z0-9-]+:\d{12}:certificate-authority/[a-f0-9-]+$`)
|
||||
if !arnPattern.MatchString(cfg.CAArn) {
|
||||
return fmt.Errorf("invalid CA ARN format: %s", cfg.CAArn)
|
||||
}
|
||||
|
||||
// Validate signing algorithm if provided
|
||||
if cfg.SigningAlgorithm != "" {
|
||||
validAlgorithms := map[string]bool{
|
||||
"SHA256WITHRSA": true,
|
||||
"SHA384WITHRSA": true,
|
||||
"SHA512WITHRSA": true,
|
||||
"SHA256WITHECDSA": true,
|
||||
"SHA384WITHECDSA": true,
|
||||
"SHA512WITHECDSA": true,
|
||||
}
|
||||
if !validAlgorithms[cfg.SigningAlgorithm] {
|
||||
return fmt.Errorf("invalid signing algorithm: %s", cfg.SigningAlgorithm)
|
||||
}
|
||||
} else {
|
||||
cfg.SigningAlgorithm = "SHA256WITHRSA"
|
||||
}
|
||||
|
||||
// Validate validity days if provided
|
||||
if cfg.ValidityDays < 0 {
|
||||
return fmt.Errorf("validity days must be non-negative")
|
||||
}
|
||||
if cfg.ValidityDays == 0 {
|
||||
cfg.ValidityDays = 365
|
||||
}
|
||||
|
||||
c.config = &cfg
|
||||
c.logger.Info("AWS ACM Private CA configuration validated",
|
||||
"region", cfg.Region,
|
||||
"ca_arn", cfg.CAArn,
|
||||
"signing_algorithm", cfg.SigningAlgorithm,
|
||||
"validity_days", cfg.ValidityDays)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IssueCertificate issues a new certificate using AWS ACM Private CA.
|
||||
func (c *Connector) IssueCertificate(ctx context.Context, request issuer.IssuanceRequest) (*issuer.IssuanceResult, error) {
|
||||
c.logger.Info("processing AWS ACM PCA issuance request",
|
||||
"common_name", request.CommonName,
|
||||
"san_count", len(request.SANs))
|
||||
|
||||
// Decode CSR from PEM
|
||||
csrBlock, _ := pem.Decode([]byte(request.CSRPEM))
|
||||
if csrBlock == nil {
|
||||
return nil, fmt.Errorf("failed to decode CSR PEM")
|
||||
}
|
||||
|
||||
// Call AWS API to issue certificate
|
||||
issueOutput, err := c.client.IssueCertificate(ctx, &IssueCertificateInput{
|
||||
CAArn: c.config.CAArn,
|
||||
CSR: csrBlock.Bytes,
|
||||
SigningAlgorithm: c.config.SigningAlgorithm,
|
||||
ValidityDays: c.config.ValidityDays,
|
||||
TemplateArn: c.config.TemplateArn,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("AWS IssueCertificate failed: %w", err)
|
||||
}
|
||||
|
||||
// Retrieve the issued certificate
|
||||
getCertOutput, err := c.client.GetCertificate(ctx, &GetCertificateInput{
|
||||
CAArn: c.config.CAArn,
|
||||
CertificateArn: issueOutput.CertificateArn,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("AWS GetCertificate failed: %w", err)
|
||||
}
|
||||
|
||||
if getCertOutput.Certificate == "" {
|
||||
return nil, fmt.Errorf("no certificate in AWS response")
|
||||
}
|
||||
|
||||
// Parse the certificate to extract metadata
|
||||
block, _ := pem.Decode([]byte(getCertOutput.Certificate))
|
||||
if block == nil {
|
||||
return nil, fmt.Errorf("failed to decode certificate PEM from AWS")
|
||||
}
|
||||
|
||||
cert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse certificate: %w", err)
|
||||
}
|
||||
|
||||
// Extract serial number (hex format, uppercase)
|
||||
serial := strings.ToUpper(fmt.Sprintf("%x", cert.SerialNumber))
|
||||
|
||||
// Use certificate ARN as OrderID for revocation lookup
|
||||
orderID := issueOutput.CertificateArn
|
||||
|
||||
c.logger.Info("AWS ACM PCA certificate issued",
|
||||
"common_name", request.CommonName,
|
||||
"serial", serial,
|
||||
"not_after", cert.NotAfter)
|
||||
|
||||
return &issuer.IssuanceResult{
|
||||
CertPEM: getCertOutput.Certificate,
|
||||
ChainPEM: getCertOutput.CertificateChain,
|
||||
Serial: serial,
|
||||
NotBefore: cert.NotBefore,
|
||||
NotAfter: cert.NotAfter,
|
||||
OrderID: orderID,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RenewCertificate renews a certificate by creating a new signing request.
|
||||
// For AWS ACM PCA, renewal is functionally identical to issuance (new cert signed from CSR).
|
||||
func (c *Connector) RenewCertificate(ctx context.Context, request issuer.RenewalRequest) (*issuer.IssuanceResult, error) {
|
||||
c.logger.Info("processing AWS ACM PCA renewal request",
|
||||
"common_name", request.CommonName,
|
||||
"san_count", len(request.SANs))
|
||||
|
||||
return c.IssueCertificate(ctx, issuer.IssuanceRequest{
|
||||
CommonName: request.CommonName,
|
||||
SANs: request.SANs,
|
||||
CSRPEM: request.CSRPEM,
|
||||
EKUs: request.EKUs,
|
||||
})
|
||||
}
|
||||
|
||||
// RevokeCertificate revokes a certificate at AWS ACM Private CA.
|
||||
func (c *Connector) RevokeCertificate(ctx context.Context, request issuer.RevocationRequest) error {
|
||||
c.logger.Info("processing AWS ACM PCA revocation request", "serial", request.Serial)
|
||||
|
||||
// Map RFC 5280 reason string to AWS reason
|
||||
reason := mapRevocationReason(request.Reason)
|
||||
|
||||
err := c.client.RevokeCertificate(ctx, &RevokeCertificateInput{
|
||||
CAArn: c.config.CAArn,
|
||||
CertificateSerial: request.Serial,
|
||||
RevocationReason: reason,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("AWS RevokeCertificate failed: %w", err)
|
||||
}
|
||||
|
||||
c.logger.Info("AWS ACM PCA certificate revoked", "serial", request.Serial)
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetOrderStatus returns the status of an AWS ACM PCA order.
|
||||
// AWS ACM PCA issues synchronously, so orders are always "completed" immediately.
|
||||
func (c *Connector) GetOrderStatus(ctx context.Context, orderID string) (*issuer.OrderStatus, error) {
|
||||
return &issuer.OrderStatus{
|
||||
OrderID: orderID,
|
||||
Status: "completed",
|
||||
UpdatedAt: time.Now(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GenerateCRL is not supported because AWS ACM PCA serves CRL directly.
|
||||
func (c *Connector) GenerateCRL(ctx context.Context, revokedCerts []issuer.RevokedCertEntry) ([]byte, error) {
|
||||
return nil, fmt.Errorf("CRL delegated to AWS ACM Private CA; use AWS endpoint directly")
|
||||
}
|
||||
|
||||
// SignOCSPResponse is not supported because AWS ACM PCA serves OCSP directly.
|
||||
func (c *Connector) SignOCSPResponse(ctx context.Context, req issuer.OCSPSignRequest) ([]byte, error) {
|
||||
return nil, fmt.Errorf("OCSP delegated to AWS ACM Private CA; use AWS endpoint directly")
|
||||
}
|
||||
|
||||
// GetCACertPEM retrieves the CA certificate from AWS ACM Private CA.
|
||||
func (c *Connector) GetCACertPEM(ctx context.Context) (string, error) {
|
||||
caCertOutput, err := c.client.GetCACertificate(ctx, &GetCACertificateInput{
|
||||
CAArn: c.config.CAArn,
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("AWS GetCACertificate failed: %w", err)
|
||||
}
|
||||
|
||||
// Combine CA certificate and chain
|
||||
if caCertOutput.CertificateChain != "" {
|
||||
return caCertOutput.Certificate + "\n" + caCertOutput.CertificateChain, nil
|
||||
}
|
||||
|
||||
return caCertOutput.Certificate, nil
|
||||
}
|
||||
|
||||
// GetRenewalInfo returns nil, nil as AWS ACM PCA does not support ACME Renewal Information (ARI).
|
||||
func (c *Connector) GetRenewalInfo(ctx context.Context, certPEM string) (*issuer.RenewalInfoResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// mapRevocationReason converts RFC 5280 reason strings to AWS ACM PCA reason codes.
|
||||
func mapRevocationReason(reason *string) string {
|
||||
if reason == nil {
|
||||
return "UNSPECIFIED"
|
||||
}
|
||||
|
||||
reasonMap := map[string]string{
|
||||
"unspecified": "UNSPECIFIED",
|
||||
"keyCompromise": "KEY_COMPROMISE",
|
||||
"caCompromise": "CERTIFICATE_AUTHORITY_COMPROMISE",
|
||||
"affiliationChanged": "AFFILIATION_CHANGED",
|
||||
"superseded": "SUPERSEDED",
|
||||
"cessationOfOperation": "CESSATION_OF_OPERATION",
|
||||
"certificateHold": "CERTIFICATE_HOLD",
|
||||
"privilegeWithdrawn": "PRIVILEGE_WITHDRAWN",
|
||||
}
|
||||
|
||||
if mapped, ok := reasonMap[*reason]; ok {
|
||||
return mapped
|
||||
}
|
||||
|
||||
return "UNSPECIFIED"
|
||||
}
|
||||
|
||||
// Ensure Connector implements the issuer.Connector interface.
|
||||
var _ issuer.Connector = (*Connector)(nil)
|
||||
@@ -0,0 +1,629 @@
|
||||
package awsacmpca_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math/big"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer"
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer/awsacmpca"
|
||||
)
|
||||
|
||||
// mockACMPCAClient implements the ACMPCAClient interface for testing.
|
||||
type mockACMPCAClient struct {
|
||||
issueCertificateErr error
|
||||
getCertificateErr error
|
||||
revokeCertificateErr error
|
||||
getCACertificateErr error
|
||||
issuedCertPEM string
|
||||
issuedChainPEM string
|
||||
caCertPEM string
|
||||
caCertChainPEM string
|
||||
lastIssueCertificateInput *awsacmpca.IssueCertificateInput
|
||||
lastRevokeCertificateInput *awsacmpca.RevokeCertificateInput
|
||||
}
|
||||
|
||||
func (m *mockACMPCAClient) IssueCertificate(ctx context.Context, input *awsacmpca.IssueCertificateInput) (*awsacmpca.IssueCertificateOutput, error) {
|
||||
m.lastIssueCertificateInput = input
|
||||
if m.issueCertificateErr != nil {
|
||||
return nil, m.issueCertificateErr
|
||||
}
|
||||
return &awsacmpca.IssueCertificateOutput{
|
||||
CertificateArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678/certificate/abcdef123456",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *mockACMPCAClient) GetCertificate(ctx context.Context, input *awsacmpca.GetCertificateInput) (*awsacmpca.GetCertificateOutput, error) {
|
||||
if m.getCertificateErr != nil {
|
||||
return nil, m.getCertificateErr
|
||||
}
|
||||
return &awsacmpca.GetCertificateOutput{
|
||||
Certificate: m.issuedCertPEM,
|
||||
CertificateChain: m.issuedChainPEM,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *mockACMPCAClient) RevokeCertificate(ctx context.Context, input *awsacmpca.RevokeCertificateInput) error {
|
||||
m.lastRevokeCertificateInput = input
|
||||
return m.revokeCertificateErr
|
||||
}
|
||||
|
||||
func (m *mockACMPCAClient) GetCACertificate(ctx context.Context, input *awsacmpca.GetCACertificateInput) (*awsacmpca.GetCACertificateOutput, error) {
|
||||
if m.getCACertificateErr != nil {
|
||||
return nil, m.getCACertificateErr
|
||||
}
|
||||
return &awsacmpca.GetCACertificateOutput{
|
||||
Certificate: m.caCertPEM,
|
||||
CertificateChain: m.caCertChainPEM,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Helper function to generate a test certificate and CSR.
|
||||
func generateTestCertAndCSR(t *testing.T) (certPEM string, csrPEM string) {
|
||||
// Generate private key
|
||||
privKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate private key: %v", err)
|
||||
}
|
||||
|
||||
// Create certificate template
|
||||
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to generate serial number: %v", err)
|
||||
}
|
||||
|
||||
template := x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: pkix.Name{
|
||||
CommonName: "example.com",
|
||||
},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(1, 0, 0),
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: false,
|
||||
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
DNSNames: []string{"example.com", "www.example.com"},
|
||||
}
|
||||
|
||||
// Create self-signed certificate for testing
|
||||
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &privKey.PublicKey, privKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create certificate: %v", err)
|
||||
}
|
||||
|
||||
certPEM = string(pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE",
|
||||
Bytes: certDER,
|
||||
}))
|
||||
|
||||
// Create CSR
|
||||
csrTemplate := x509.CertificateRequest{
|
||||
Subject: pkix.Name{
|
||||
CommonName: "example.com",
|
||||
},
|
||||
DNSNames: []string{"example.com", "www.example.com"},
|
||||
}
|
||||
|
||||
csrDER, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, privKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create CSR: %v", err)
|
||||
}
|
||||
|
||||
csrPEM = string(pem.EncodeToMemory(&pem.Block{
|
||||
Type: "CERTIFICATE REQUEST",
|
||||
Bytes: csrDER,
|
||||
}))
|
||||
|
||||
return certPEM, csrPEM
|
||||
}
|
||||
|
||||
func TestAWSACMPCAConnector(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("ValidateConfig_Success", func(t *testing.T) {
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
SigningAlgorithm: "SHA256WITHRSA",
|
||||
ValidityDays: 365,
|
||||
}
|
||||
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
rawConfig, _ := json.Marshal(config)
|
||||
err := connector.ValidateConfig(ctx, rawConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateConfig failed: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ValidateConfig_AllOptionalFields", func(t *testing.T) {
|
||||
config := awsacmpca.Config{
|
||||
Region: "eu-west-1",
|
||||
CAArn: "arn:aws:acm-pca:eu-west-1:123456789012:certificate-authority/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
|
||||
SigningAlgorithm: "SHA512WITHECDSA",
|
||||
ValidityDays: 730,
|
||||
TemplateArn: "arn:aws:acm-pca:eu-west-1:123456789012:template/WebServer",
|
||||
}
|
||||
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
rawConfig, _ := json.Marshal(config)
|
||||
err := connector.ValidateConfig(ctx, rawConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateConfig failed: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ValidateConfig_InvalidJSON", func(t *testing.T) {
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
err := connector.ValidateConfig(ctx, []byte(`{invalid json}`))
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for invalid JSON")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "invalid AWS ACM PCA config") {
|
||||
t.Errorf("Expected config error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ValidateConfig_MissingRegion", func(t *testing.T) {
|
||||
config := awsacmpca.Config{
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
rawConfig, _ := json.Marshal(config)
|
||||
err := connector.ValidateConfig(ctx, rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for missing region")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "region is required") {
|
||||
t.Errorf("Expected region required error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ValidateConfig_MissingCAArn", func(t *testing.T) {
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
}
|
||||
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
rawConfig, _ := json.Marshal(config)
|
||||
err := connector.ValidateConfig(ctx, rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for missing CA ARN")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "CA ARN is required") {
|
||||
t.Errorf("Expected CA ARN required error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ValidateConfig_InvalidCAArn", func(t *testing.T) {
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "not-an-arn",
|
||||
}
|
||||
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
rawConfig, _ := json.Marshal(config)
|
||||
err := connector.ValidateConfig(ctx, rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for invalid CA ARN")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "invalid CA ARN format") {
|
||||
t.Errorf("Expected invalid ARN error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ValidateConfig_InvalidSigningAlgorithm", func(t *testing.T) {
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
SigningAlgorithm: "INVALID_ALGO",
|
||||
}
|
||||
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
rawConfig, _ := json.Marshal(config)
|
||||
err := connector.ValidateConfig(ctx, rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for invalid signing algorithm")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "invalid signing algorithm") {
|
||||
t.Errorf("Expected invalid algorithm error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ValidateConfig_InvalidValidityDays", func(t *testing.T) {
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
ValidityDays: -1,
|
||||
}
|
||||
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
rawConfig, _ := json.Marshal(config)
|
||||
err := connector.ValidateConfig(ctx, rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for negative validity days")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "validity days must be non-negative") {
|
||||
t.Errorf("Expected validity days error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IssueCertificate_Success", func(t *testing.T) {
|
||||
certPEM, csrPEM := generateTestCertAndCSR(t)
|
||||
|
||||
mockClient := &mockACMPCAClient{
|
||||
issuedCertPEM: certPEM,
|
||||
issuedChainPEM: certPEM, // Use same cert as chain for test
|
||||
}
|
||||
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
SigningAlgorithm: "SHA256WITHRSA",
|
||||
ValidityDays: 365,
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
|
||||
request := issuer.IssuanceRequest{
|
||||
CommonName: "example.com",
|
||||
SANs: []string{"www.example.com"},
|
||||
CSRPEM: csrPEM,
|
||||
}
|
||||
|
||||
result, err := connector.IssueCertificate(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatalf("IssueCertificate failed: %v", err)
|
||||
}
|
||||
|
||||
if result.CertPEM == "" {
|
||||
t.Fatal("Expected certificate PEM in result")
|
||||
}
|
||||
if result.Serial == "" {
|
||||
t.Fatal("Expected serial number in result")
|
||||
}
|
||||
if result.OrderID == "" {
|
||||
t.Fatal("Expected OrderID (certificate ARN) in result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IssueCertificate_EmptyCSR", func(t *testing.T) {
|
||||
mockClient := &mockACMPCAClient{}
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
request := issuer.IssuanceRequest{
|
||||
CommonName: "example.com",
|
||||
CSRPEM: "", // Empty CSR
|
||||
}
|
||||
|
||||
_, err := connector.IssueCertificate(ctx, request)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for empty CSR")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "failed to decode CSR PEM") {
|
||||
t.Errorf("Expected CSR decode error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IssueCertificate_IssueError", func(t *testing.T) {
|
||||
certPEM, csrPEM := generateTestCertAndCSR(t)
|
||||
mockClient := &mockACMPCAClient{
|
||||
issueCertificateErr: fmt.Errorf("AWS service error"),
|
||||
issuedCertPEM: certPEM,
|
||||
}
|
||||
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
request := issuer.IssuanceRequest{
|
||||
CommonName: "example.com",
|
||||
CSRPEM: csrPEM,
|
||||
}
|
||||
|
||||
_, err := connector.IssueCertificate(ctx, request)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error from IssueCertificate")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "IssueCertificate failed") {
|
||||
t.Errorf("Expected issue error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("IssueCertificate_GetCertificateError", func(t *testing.T) {
|
||||
_, csrPEM := generateTestCertAndCSR(t)
|
||||
mockClient := &mockACMPCAClient{
|
||||
getCertificateErr: fmt.Errorf("AWS service error"),
|
||||
}
|
||||
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
request := issuer.IssuanceRequest{
|
||||
CommonName: "example.com",
|
||||
CSRPEM: csrPEM,
|
||||
}
|
||||
|
||||
_, err := connector.IssueCertificate(ctx, request)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error from GetCertificate")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "GetCertificate failed") {
|
||||
t.Errorf("Expected get cert error, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("RenewCertificate_Success", func(t *testing.T) {
|
||||
certPEM, csrPEM := generateTestCertAndCSR(t)
|
||||
mockClient := &mockACMPCAClient{
|
||||
issuedCertPEM: certPEM,
|
||||
issuedChainPEM: certPEM,
|
||||
}
|
||||
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
request := issuer.RenewalRequest{
|
||||
CommonName: "example.com",
|
||||
SANs: []string{"www.example.com"},
|
||||
CSRPEM: csrPEM,
|
||||
}
|
||||
|
||||
result, err := connector.RenewCertificate(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatalf("RenewCertificate failed: %v", err)
|
||||
}
|
||||
|
||||
if result.CertPEM == "" {
|
||||
t.Fatal("Expected certificate PEM in result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("RevokeCertificate_Success", func(t *testing.T) {
|
||||
mockClient := &mockACMPCAClient{}
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
reason := "keyCompromise"
|
||||
request := issuer.RevocationRequest{
|
||||
Serial: "aabbccdd123456",
|
||||
Reason: &reason,
|
||||
}
|
||||
|
||||
err := connector.RevokeCertificate(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatalf("RevokeCertificate failed: %v", err)
|
||||
}
|
||||
|
||||
if mockClient.lastRevokeCertificateInput.RevocationReason != "KEY_COMPROMISE" {
|
||||
t.Errorf("Expected KEY_COMPROMISE reason, got: %s", mockClient.lastRevokeCertificateInput.RevocationReason)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("RevokeCertificate_WithDefaultReason", func(t *testing.T) {
|
||||
mockClient := &mockACMPCAClient{}
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
request := issuer.RevocationRequest{
|
||||
Serial: "aabbccdd123456",
|
||||
Reason: nil,
|
||||
}
|
||||
|
||||
err := connector.RevokeCertificate(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatalf("RevokeCertificate failed: %v", err)
|
||||
}
|
||||
|
||||
if mockClient.lastRevokeCertificateInput.RevocationReason != "UNSPECIFIED" {
|
||||
t.Errorf("Expected UNSPECIFIED reason, got: %s", mockClient.lastRevokeCertificateInput.RevocationReason)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("RevokeCertificate_Error", func(t *testing.T) {
|
||||
mockClient := &mockACMPCAClient{
|
||||
revokeCertificateErr: fmt.Errorf("AWS service error"),
|
||||
}
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
request := issuer.RevocationRequest{
|
||||
Serial: "aabbccdd123456",
|
||||
}
|
||||
|
||||
err := connector.RevokeCertificate(ctx, request)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error from RevokeCertificate")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetOrderStatus_ReturnsCompleted", func(t *testing.T) {
|
||||
mockClient := &mockACMPCAClient{}
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
status, err := connector.GetOrderStatus(ctx, "test-order-id")
|
||||
if err != nil {
|
||||
t.Fatalf("GetOrderStatus failed: %v", err)
|
||||
}
|
||||
|
||||
if status.Status != "completed" {
|
||||
t.Errorf("Expected completed status, got: %s", status.Status)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetCACertPEM_Success", func(t *testing.T) {
|
||||
certPEM, _ := generateTestCertAndCSR(t)
|
||||
mockClient := &mockACMPCAClient{
|
||||
caCertPEM: certPEM,
|
||||
}
|
||||
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
caPEM, err := connector.GetCACertPEM(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCACertPEM failed: %v", err)
|
||||
}
|
||||
|
||||
if caPEM == "" {
|
||||
t.Fatal("Expected CA certificate PEM")
|
||||
}
|
||||
if !strings.Contains(caPEM, "CERTIFICATE") {
|
||||
t.Errorf("Expected PEM format, got: %s", caPEM)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetCACertPEM_WithChain", func(t *testing.T) {
|
||||
certPEM, _ := generateTestCertAndCSR(t)
|
||||
mockClient := &mockACMPCAClient{
|
||||
caCertPEM: certPEM,
|
||||
caCertChainPEM: certPEM,
|
||||
}
|
||||
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
caPEM, err := connector.GetCACertPEM(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetCACertPEM failed: %v", err)
|
||||
}
|
||||
|
||||
// Should contain both certificate and chain separated by newline
|
||||
if !strings.Contains(caPEM, "\n") {
|
||||
t.Fatal("Expected certificate and chain combined")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetCACertPEM_Error", func(t *testing.T) {
|
||||
mockClient := &mockACMPCAClient{
|
||||
getCACertificateErr: fmt.Errorf("AWS service error"),
|
||||
}
|
||||
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
_, err := connector.GetCACertPEM(ctx)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error from GetCACertPEM")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetRenewalInfo_ReturnsNil", func(t *testing.T) {
|
||||
mockClient := &mockACMPCAClient{}
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
result, err := connector.GetRenewalInfo(ctx, "cert-pem")
|
||||
if err != nil {
|
||||
t.Fatalf("GetRenewalInfo failed: %v", err)
|
||||
}
|
||||
|
||||
if result != nil {
|
||||
t.Fatal("Expected nil result from GetRenewalInfo")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ValidateConfig_AppliesDefaults", func(t *testing.T) {
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
// SigningAlgorithm and ValidityDays not set
|
||||
}
|
||||
|
||||
connector := awsacmpca.New(nil, logger)
|
||||
rawConfig, _ := json.Marshal(config)
|
||||
err := connector.ValidateConfig(ctx, rawConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateConfig failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify defaults were applied by checking the connector's config
|
||||
// Since config is private, we'll test via IssueCertificate to ensure algorithm is set
|
||||
})
|
||||
|
||||
t.Run("RevocationReason_Mapping", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"keyCompromise", "KEY_COMPROMISE"},
|
||||
{"caCompromise", "CERTIFICATE_AUTHORITY_COMPROMISE"},
|
||||
{"affiliationChanged", "AFFILIATION_CHANGED"},
|
||||
{"superseded", "SUPERSEDED"},
|
||||
{"cessationOfOperation", "CESSATION_OF_OPERATION"},
|
||||
{"privilegeWithdrawn", "PRIVILEGE_WITHDRAWN"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
mockClient := &mockACMPCAClient{}
|
||||
config := awsacmpca.Config{
|
||||
Region: "us-east-1",
|
||||
CAArn: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/12345678-1234-1234-1234-123456789012",
|
||||
}
|
||||
|
||||
connector := awsacmpca.NewWithClient(&config, mockClient, logger)
|
||||
reason := tc.input
|
||||
request := issuer.RevocationRequest{
|
||||
Serial: "test-serial",
|
||||
Reason: &reason,
|
||||
}
|
||||
|
||||
_ = connector.RevokeCertificate(ctx, request)
|
||||
|
||||
if mockClient.lastRevokeCertificateInput.RevocationReason != tc.expected {
|
||||
t.Errorf("For reason %q, expected %q, got %q", tc.input, tc.expected, mockClient.lastRevokeCertificateInput.RevocationReason)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer"
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer/acme"
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer/awsacmpca"
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer/digicert"
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer/googlecas"
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer/local"
|
||||
@@ -81,6 +82,13 @@ func NewFromConfig(issuerType string, configJSON json.RawMessage, logger *slog.L
|
||||
}
|
||||
return googlecas.New(&cfg, logger), nil
|
||||
|
||||
case "AWSACMPCA":
|
||||
var cfg awsacmpca.Config
|
||||
if err := json.Unmarshal(configJSON, &cfg); err != nil {
|
||||
return nil, fmt.Errorf("invalid AWS ACM PCA config: %w", err)
|
||||
}
|
||||
return awsacmpca.New(&cfg, logger), nil
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown issuer type: %q", issuerType)
|
||||
}
|
||||
|
||||
@@ -136,3 +136,14 @@ func TestNewFromConfig_EmptyConfig(t *testing.T) {
|
||||
t.Fatal("expected non-nil connector")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewFromConfig_AWSACMPCA(t *testing.T) {
|
||||
cfg := json.RawMessage(`{"project":"my-project","location":"us-central1","ca_pool":"my-pool","credentials":"/path/to/creds.json"}`)
|
||||
conn, err := NewFromConfig("AWSACMPCA", cfg, testLogger())
|
||||
if err != nil {
|
||||
t.Fatalf("NewFromConfig(AWSACMPCA) failed: %v", err)
|
||||
}
|
||||
if conn == nil {
|
||||
t.Fatal("expected non-nil connector")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,540 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/notifier"
|
||||
)
|
||||
|
||||
func newTestLogger() *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
}
|
||||
|
||||
func TestEmail_ValidateConfig_ValidSMTP(t *testing.T) {
|
||||
// Use localhost with a high port that's unlikely to have a service
|
||||
// This test will try to connect, and we expect it to fail
|
||||
// But for testing that validation works with valid config, we need to skip this
|
||||
// in most CI environments or use a mock SMTP server.
|
||||
|
||||
// For this test, we'll just verify that ValidateConfig can be called
|
||||
// with proper config structure without panicking
|
||||
cfg := &Config{
|
||||
SMTPHost: "localhost",
|
||||
SMTPPort: 25,
|
||||
Username: "user",
|
||||
Password: "pass",
|
||||
FromAddress: "sender@example.com",
|
||||
UseTLS: false,
|
||||
}
|
||||
|
||||
rawConfig, _ := json.Marshal(cfg)
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
// This will likely fail to connect, but that's OK - we're testing the validation logic exists
|
||||
_ = conn.ValidateConfig(context.Background(), rawConfig)
|
||||
// If it crashes, the test will fail; if it returns an error about connection, that's expected
|
||||
}
|
||||
|
||||
func TestEmail_ValidateConfig_MissingHost(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPPort: 587,
|
||||
Username: "user",
|
||||
Password: "pass",
|
||||
FromAddress: "sender@example.com",
|
||||
UseTLS: true,
|
||||
}
|
||||
|
||||
rawConfig, _ := json.Marshal(cfg)
|
||||
logger := newTestLogger()
|
||||
conn := New(&Config{}, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing SMTP host, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "required") {
|
||||
t.Errorf("expected 'required' in error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_ValidateConfig_MissingPort(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
Username: "user",
|
||||
Password: "pass",
|
||||
FromAddress: "sender@example.com",
|
||||
UseTLS: true,
|
||||
}
|
||||
|
||||
rawConfig, _ := json.Marshal(cfg)
|
||||
logger := newTestLogger()
|
||||
conn := New(&Config{}, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing port, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "required") {
|
||||
t.Errorf("expected 'required' in error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_ValidateConfig_MissingFromAddress(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
Username: "user",
|
||||
Password: "pass",
|
||||
UseTLS: true,
|
||||
}
|
||||
|
||||
rawConfig, _ := json.Marshal(cfg)
|
||||
logger := newTestLogger()
|
||||
conn := New(&Config{}, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing from_address, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "required") {
|
||||
t.Errorf("expected 'required' in error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_ValidateConfig_InvalidJSON(t *testing.T) {
|
||||
rawConfig := []byte("{invalid json")
|
||||
logger := newTestLogger()
|
||||
conn := New(&Config{}, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid JSON, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "invalid email config") {
|
||||
t.Errorf("expected 'invalid email config', got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_FormatMessage_RFC822Headers(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
UseTLS: true,
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
from := "sender@example.com"
|
||||
to := "recipient@example.com"
|
||||
subject := "Test Subject"
|
||||
body := "Test Body"
|
||||
|
||||
message := conn.formatEmailMessage(from, to, subject, body)
|
||||
messageStr := string(message)
|
||||
|
||||
if !strings.Contains(messageStr, "From: "+from) {
|
||||
t.Errorf("expected From header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, "To: "+to) {
|
||||
t.Errorf("expected To header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, "Subject: "+subject) {
|
||||
t.Errorf("expected Subject header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, "Date:") {
|
||||
t.Errorf("expected Date header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, "Content-Type: text/plain; charset=utf-8") {
|
||||
t.Errorf("expected Content-Type header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, body) {
|
||||
t.Errorf("expected message body, got %s", messageStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_FormatHTMLEmailMessage_Headers(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
UseTLS: true,
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
from := "sender@example.com"
|
||||
to := "recipient@example.com"
|
||||
subject := "HTML Test"
|
||||
htmlBody := "<html><body><h1>Test</h1></body></html>"
|
||||
|
||||
message := conn.formatHTMLEmailMessage(from, to, subject, htmlBody)
|
||||
messageStr := string(message)
|
||||
|
||||
if !strings.Contains(messageStr, "From: "+from) {
|
||||
t.Errorf("expected From header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, "To: "+to) {
|
||||
t.Errorf("expected To header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, "Subject: "+subject) {
|
||||
t.Errorf("expected Subject header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, "MIME-Version: 1.0") {
|
||||
t.Errorf("expected MIME-Version header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, "Content-Type: text/html; charset=utf-8") {
|
||||
t.Errorf("expected HTML Content-Type header, got %s", messageStr)
|
||||
}
|
||||
if !strings.Contains(messageStr, htmlBody) {
|
||||
t.Errorf("expected HTML body, got %s", messageStr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_FormatAlertBody(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
alert := notifier.Alert{
|
||||
ID: "alert-123",
|
||||
Type: "expiration",
|
||||
Severity: "warning",
|
||||
Subject: "Certificate Expiring",
|
||||
Message: "Certificate mc-api-prod expires in 7 days",
|
||||
CreatedAt: time.Now(),
|
||||
Metadata: map[string]string{
|
||||
"cert_id": "mc-api-prod",
|
||||
"issuer": "letsencrypt",
|
||||
},
|
||||
}
|
||||
|
||||
body := conn.formatAlertBody(alert)
|
||||
|
||||
if !strings.Contains(body, "Certificate Alert Notification") {
|
||||
t.Errorf("expected 'Certificate Alert Notification' in body")
|
||||
}
|
||||
if !strings.Contains(body, alert.ID) {
|
||||
t.Errorf("expected alert ID in body")
|
||||
}
|
||||
if !strings.Contains(body, alert.Severity) {
|
||||
t.Errorf("expected severity in body")
|
||||
}
|
||||
if !strings.Contains(body, alert.Subject) {
|
||||
t.Errorf("expected subject in body")
|
||||
}
|
||||
if !strings.Contains(body, alert.Message) {
|
||||
t.Errorf("expected message in body")
|
||||
}
|
||||
if !strings.Contains(body, "cert_id") {
|
||||
t.Errorf("expected metadata key in body")
|
||||
}
|
||||
if !strings.Contains(body, "mc-api-prod") {
|
||||
t.Errorf("expected metadata value in body")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_FormatEventBody(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
certID := "mc-api-prod"
|
||||
event := notifier.Event{
|
||||
ID: "event-456",
|
||||
Type: "issued",
|
||||
CertificateID: &certID,
|
||||
Subject: "Certificate Issued",
|
||||
Body: "New certificate issued successfully",
|
||||
CreatedAt: time.Now(),
|
||||
Metadata: map[string]string{
|
||||
"issuer": "letsencrypt",
|
||||
},
|
||||
}
|
||||
|
||||
body := conn.formatEventBody(event)
|
||||
|
||||
if !strings.Contains(body, "Certificate Event Notification") {
|
||||
t.Errorf("expected 'Certificate Event Notification' in body")
|
||||
}
|
||||
if !strings.Contains(body, event.ID) {
|
||||
t.Errorf("expected event ID in body")
|
||||
}
|
||||
if !strings.Contains(body, event.Type) {
|
||||
t.Errorf("expected event type in body")
|
||||
}
|
||||
if !strings.Contains(body, "Certificate ID: "+certID) {
|
||||
t.Errorf("expected certificate ID in body")
|
||||
}
|
||||
if !strings.Contains(body, event.Subject) {
|
||||
t.Errorf("expected subject in body")
|
||||
}
|
||||
if !strings.Contains(body, event.Body) {
|
||||
t.Errorf("expected body in body")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_FormatEventBody_NoCertificateID(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
event := notifier.Event{
|
||||
ID: "event-789",
|
||||
Type: "test",
|
||||
Subject: "Test Event",
|
||||
Body: "Test body",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
body := conn.formatEventBody(event)
|
||||
|
||||
if !strings.Contains(body, "Certificate Event Notification") {
|
||||
t.Errorf("expected 'Certificate Event Notification' in body")
|
||||
}
|
||||
if strings.Contains(body, "Certificate ID:") {
|
||||
t.Errorf("expected no Certificate ID line when nil, got %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_SendAlert_ValidationFailure(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
alert := notifier.Alert{
|
||||
ID: "alert-fail",
|
||||
Type: "test",
|
||||
Severity: "critical",
|
||||
Subject: "Test Alert",
|
||||
Message: "Testing error path",
|
||||
Recipient: "ops@example.com",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
// This will fail because there's no SMTP server on the configured host
|
||||
err := conn.SendAlert(context.Background(), alert)
|
||||
|
||||
// We expect an error because the SMTP server doesn't exist
|
||||
// The exact error depends on network conditions, but we know it should fail
|
||||
if err == nil {
|
||||
// In some environments this might succeed if the host/port resolves oddly
|
||||
// but in most cases it will fail
|
||||
t.Skip("test requires no service on smtp.example.com:587")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_SendEvent_FormatsSubjectCorrectly(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
event := notifier.Event{
|
||||
ID: "event-123",
|
||||
Type: "issued",
|
||||
Subject: "Certificate Issued",
|
||||
Body: "New certificate issued",
|
||||
Recipient: "ops@example.com",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
// Verify the formatEventBody output includes expected formatted subject
|
||||
body := conn.formatEventBody(event)
|
||||
|
||||
if !strings.Contains(body, event.Subject) {
|
||||
t.Errorf("expected subject '%s' in formatted body", event.Subject)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_New_CreatesConnectorWithConfig(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
Username: "user",
|
||||
Password: "pass",
|
||||
FromAddress: "sender@example.com",
|
||||
UseTLS: true,
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
if conn == nil {
|
||||
t.Fatal("expected connector to be created")
|
||||
}
|
||||
|
||||
if conn.config != cfg {
|
||||
t.Error("expected config to be set correctly")
|
||||
}
|
||||
|
||||
if conn.logger != logger {
|
||||
t.Error("expected logger to be set correctly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_ValidateConfig_ConnectionRefused(t *testing.T) {
|
||||
// Use a port that's unlikely to have a service listening
|
||||
cfg := &Config{
|
||||
SMTPHost: "127.0.0.1",
|
||||
SMTPPort: 54321, // Random high port
|
||||
FromAddress: "sender@example.com",
|
||||
UseTLS: false,
|
||||
}
|
||||
|
||||
rawConfig, _ := json.Marshal(cfg)
|
||||
logger := newTestLogger()
|
||||
conn := New(&Config{}, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
if err == nil {
|
||||
t.Skip("test assumes no service on 127.0.0.1:54321")
|
||||
}
|
||||
|
||||
// Verify it's a connection error
|
||||
if !strings.Contains(err.Error(), "failed to reach SMTP server") {
|
||||
t.Errorf("expected 'failed to reach SMTP server' in error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_ValidateConfig_ValidatesAllRequiredFields(t *testing.T) {
|
||||
// Test each required field
|
||||
tests := []struct {
|
||||
name string
|
||||
config Config
|
||||
shouldFail bool
|
||||
}{
|
||||
{
|
||||
name: "all required fields present",
|
||||
config: Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
},
|
||||
shouldFail: true, // Will fail due to connection, but validation logic passed
|
||||
},
|
||||
{
|
||||
name: "missing smtp_host",
|
||||
config: Config{
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
},
|
||||
shouldFail: true,
|
||||
},
|
||||
{
|
||||
name: "missing smtp_port",
|
||||
config: Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
FromAddress: "sender@example.com",
|
||||
},
|
||||
shouldFail: true,
|
||||
},
|
||||
{
|
||||
name: "missing from_address",
|
||||
config: Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
},
|
||||
shouldFail: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rawConfig, _ := json.Marshal(tt.config)
|
||||
logger := newTestLogger()
|
||||
conn := New(&Config{}, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
|
||||
if !tt.shouldFail && err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if tt.shouldFail && err != nil && !strings.Contains(err.Error(), "required") {
|
||||
// It might fail with connection error after validation, which is OK
|
||||
if !strings.Contains(err.Error(), "failed to reach") {
|
||||
t.Errorf("expected validation error or connection error, got %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_FormatMetadata_EmptyMetadata(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
result := conn.formatMetadata(map[string]string{})
|
||||
|
||||
if result != "" {
|
||||
t.Errorf("expected empty string for empty metadata, got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmail_FormatMetadata_WithData(t *testing.T) {
|
||||
cfg := &Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "sender@example.com",
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
metadata := map[string]string{
|
||||
"issuer": "letsencrypt",
|
||||
"env": "production",
|
||||
}
|
||||
|
||||
result := conn.formatMetadata(metadata)
|
||||
|
||||
if !strings.Contains(result, "Metadata:") {
|
||||
t.Errorf("expected 'Metadata:' in result")
|
||||
}
|
||||
if !strings.Contains(result, "issuer") {
|
||||
t.Errorf("expected 'issuer' key in result")
|
||||
}
|
||||
if !strings.Contains(result, "letsencrypt") {
|
||||
t.Errorf("expected 'letsencrypt' value in result")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,404 @@
|
||||
package webhook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/notifier"
|
||||
)
|
||||
|
||||
func TestWebhook_ValidateConfig_ValidURL(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &Config{
|
||||
URL: server.URL,
|
||||
}
|
||||
|
||||
rawConfig, _ := json.Marshal(cfg)
|
||||
|
||||
// Create a new logger (or use test logger)
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
if err != nil {
|
||||
t.Errorf("expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_ValidateConfig_MissingURL(t *testing.T) {
|
||||
cfg := &Config{
|
||||
URL: "",
|
||||
}
|
||||
|
||||
rawConfig, _ := json.Marshal(cfg)
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "webhook url is required") {
|
||||
t.Errorf("expected 'webhook url is required', got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_ValidateConfig_InvalidJSON(t *testing.T) {
|
||||
rawConfig := []byte("{invalid json")
|
||||
logger := newTestLogger()
|
||||
conn := New(&Config{}, logger)
|
||||
|
||||
err := conn.ValidateConfig(context.Background(), rawConfig)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "invalid webhook config") {
|
||||
t.Errorf("expected 'invalid webhook config', got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_SendAlert_Success(t *testing.T) {
|
||||
var receivedPayload map[string]interface{}
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("expected POST, got %s", r.Method)
|
||||
}
|
||||
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
|
||||
t.Errorf("expected application/json, got %s", ct)
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&receivedPayload); err != nil {
|
||||
t.Fatalf("failed to decode payload: %v", err)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &Config{
|
||||
URL: server.URL,
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
alert := notifier.Alert{
|
||||
ID: "alert-123",
|
||||
Type: "expiration",
|
||||
Severity: "warning",
|
||||
Subject: "Certificate Expiring",
|
||||
Message: "Certificate mc-api-prod expires in 7 days",
|
||||
Recipient: "ops@example.com",
|
||||
Metadata: map[string]string{"cert_id": "mc-api-prod"},
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := conn.SendAlert(context.Background(), alert)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if receivedPayload["type"] != "alert" {
|
||||
t.Errorf("expected type 'alert', got %v", receivedPayload["type"])
|
||||
}
|
||||
if receivedPayload["alert_id"] != "alert-123" {
|
||||
t.Errorf("expected alert_id 'alert-123', got %v", receivedPayload["alert_id"])
|
||||
}
|
||||
if receivedPayload["severity"] != "warning" {
|
||||
t.Errorf("expected severity 'warning', got %v", receivedPayload["severity"])
|
||||
}
|
||||
if receivedPayload["subject"] != "Certificate Expiring" {
|
||||
t.Errorf("expected subject 'Certificate Expiring', got %v", receivedPayload["subject"])
|
||||
}
|
||||
if receivedPayload["message"] != "Certificate mc-api-prod expires in 7 days" {
|
||||
t.Errorf("expected correct message, got %v", receivedPayload["message"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_SendAlert_HMACSignature(t *testing.T) {
|
||||
var receivedSignature string
|
||||
var receivedBody []byte
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
receivedSignature = r.Header.Get("X-Signature")
|
||||
sigAlgo := r.Header.Get("X-Signature-Algorithm")
|
||||
|
||||
if sigAlgo != "sha256" {
|
||||
t.Errorf("expected algorithm sha256, got %s", sigAlgo)
|
||||
}
|
||||
|
||||
var err error
|
||||
receivedBody, err = io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read body: %v", err)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
secret := "my-secret-key"
|
||||
cfg := &Config{
|
||||
URL: server.URL,
|
||||
Secret: secret,
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
alert := notifier.Alert{
|
||||
ID: "alert-456",
|
||||
Type: "expiration",
|
||||
Severity: "critical",
|
||||
Subject: "Critical: Certificate Expired",
|
||||
Message: "Certificate is already expired",
|
||||
Recipient: "admin@example.com",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := conn.SendAlert(context.Background(), alert)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Verify signature
|
||||
expectedSignature := computeHMACSHA256(receivedBody, secret)
|
||||
if receivedSignature != expectedSignature {
|
||||
t.Errorf("expected signature %s, got %s", expectedSignature, receivedSignature)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_SendAlert_NoSignatureWithoutSecret(t *testing.T) {
|
||||
var hasSignatureHeader bool
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, hasSignatureHeader = r.Header["X-Signature"]
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &Config{
|
||||
URL: server.URL,
|
||||
Secret: "",
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
alert := notifier.Alert{
|
||||
ID: "alert-789",
|
||||
Type: "expiration",
|
||||
Severity: "info",
|
||||
Subject: "Renewal Complete",
|
||||
Message: "Certificate renewed successfully",
|
||||
Recipient: "ops@example.com",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := conn.SendAlert(context.Background(), alert)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if hasSignatureHeader {
|
||||
t.Error("expected no X-Signature header when secret is empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_SendAlert_CustomHeaders(t *testing.T) {
|
||||
var receivedHeaders http.Header
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
receivedHeaders = r.Header
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &Config{
|
||||
URL: server.URL,
|
||||
Headers: map[string]string{
|
||||
"Authorization": "Bearer token123",
|
||||
"X-Custom": "custom-value",
|
||||
},
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
alert := notifier.Alert{
|
||||
ID: "alert-custom",
|
||||
Type: "test",
|
||||
Severity: "info",
|
||||
Subject: "Test",
|
||||
Message: "Test message",
|
||||
Recipient: "test@example.com",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := conn.SendAlert(context.Background(), alert)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if auth := receivedHeaders.Get("Authorization"); auth != "Bearer token123" {
|
||||
t.Errorf("expected Authorization header 'Bearer token123', got %s", auth)
|
||||
}
|
||||
if custom := receivedHeaders.Get("X-Custom"); custom != "custom-value" {
|
||||
t.Errorf("expected X-Custom header 'custom-value', got %s", custom)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_SendAlert_HTTPError(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("server error"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &Config{
|
||||
URL: server.URL,
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
alert := notifier.Alert{
|
||||
ID: "alert-error",
|
||||
Type: "test",
|
||||
Severity: "error",
|
||||
Subject: "Test Error",
|
||||
Message: "Testing error handling",
|
||||
Recipient: "admin@example.com",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := conn.SendAlert(context.Background(), alert)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "500") {
|
||||
t.Errorf("expected error to contain '500', got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_SendEvent_Success(t *testing.T) {
|
||||
var receivedPayload map[string]interface{}
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
t.Errorf("expected POST, got %s", r.Method)
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&receivedPayload); err != nil {
|
||||
t.Fatalf("failed to decode payload: %v", err)
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &Config{
|
||||
URL: server.URL,
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
certID := "mc-api-prod"
|
||||
event := notifier.Event{
|
||||
ID: "event-123",
|
||||
Type: "issued",
|
||||
CertificateID: &certID,
|
||||
Subject: "Certificate Issued",
|
||||
Body: "New certificate issued for mc-api-prod",
|
||||
Recipient: "ops@example.com",
|
||||
Metadata: map[string]string{"issuer": "letsencrypt"},
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := conn.SendEvent(context.Background(), event)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if receivedPayload["type"] != "event" {
|
||||
t.Errorf("expected type 'event', got %v", receivedPayload["type"])
|
||||
}
|
||||
if receivedPayload["event_id"] != "event-123" {
|
||||
t.Errorf("expected event_id 'event-123', got %v", receivedPayload["event_id"])
|
||||
}
|
||||
if receivedPayload["event_type"] != "issued" {
|
||||
t.Errorf("expected event_type 'issued', got %v", receivedPayload["event_type"])
|
||||
}
|
||||
if receivedPayload["certificate_id"] != "mc-api-prod" {
|
||||
t.Errorf("expected certificate_id 'mc-api-prod', got %v", receivedPayload["certificate_id"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebhook_SendEvent_WithoutCertificateID(t *testing.T) {
|
||||
var receivedPayload map[string]interface{}
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if err := json.NewDecoder(r.Body).Decode(&receivedPayload); err != nil {
|
||||
t.Fatalf("failed to decode payload: %v", err)
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := &Config{
|
||||
URL: server.URL,
|
||||
}
|
||||
|
||||
logger := newTestLogger()
|
||||
conn := New(cfg, logger)
|
||||
|
||||
event := notifier.Event{
|
||||
ID: "event-456",
|
||||
Type: "test",
|
||||
Subject: "Test Event",
|
||||
Body: "Test body",
|
||||
Recipient: "test@example.com",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := conn.SendEvent(context.Background(), event)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Ensure certificate_id is not in payload when nil
|
||||
if _, hasKey := receivedPayload["certificate_id"]; hasKey && receivedPayload["certificate_id"] != nil {
|
||||
t.Errorf("expected no certificate_id in payload, got %v", receivedPayload["certificate_id"])
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to compute HMAC-SHA256 signature
|
||||
func computeHMACSHA256(data []byte, secret string) string {
|
||||
h := hmac.New(sha256.New, []byte(secret))
|
||||
h.Write(data)
|
||||
signature := hex.EncodeToString(h.Sum(nil))
|
||||
return fmt.Sprintf("sha256=%s", signature)
|
||||
}
|
||||
|
||||
// Helper function to create a test logger
|
||||
func newTestLogger() *slog.Logger {
|
||||
// Return a discard logger for tests
|
||||
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
}
|
||||
@@ -736,14 +736,18 @@ func TestValidateDeployment(t *testing.T) {
|
||||
|
||||
func TestObjectName(t *testing.T) {
|
||||
name1 := objectName("cert")
|
||||
name2 := objectName("cert")
|
||||
|
||||
if !strings.HasPrefix(name1, "certctl-cert-") {
|
||||
t.Errorf("expected prefix certctl-cert-, got %s", name1)
|
||||
}
|
||||
// Nanosecond timestamps should produce different names
|
||||
if name1 == name2 {
|
||||
t.Error("expected unique names from nanosecond timestamps")
|
||||
// Verify format is correct: certctl-<type>-<nanotime>
|
||||
if len(name1) < len("certctl-cert-") {
|
||||
t.Errorf("expected non-empty object name, got %s", name1)
|
||||
}
|
||||
// Verify the name contains digits after the prefix
|
||||
withoutPrefix := strings.TrimPrefix(name1, "certctl-cert-")
|
||||
if withoutPrefix == "" {
|
||||
t.Error("expected digits in object name after prefix")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -801,6 +805,106 @@ func TestCleanup_EmptyNames(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeployCertificate_TransactionRollbackOnProfileFailure tests that when the
|
||||
// UpdateSSLProfile call fails, the transaction is NOT committed and cleanup is called.
|
||||
func TestDeployCertificate_TransactionRollbackOnProfileFailure(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Host: "f5.example.com",
|
||||
Username: "admin",
|
||||
Password: "password",
|
||||
SSLProfile: "clientssl",
|
||||
Partition: "Common",
|
||||
Insecure: true,
|
||||
Timeout: 30,
|
||||
}
|
||||
|
||||
mock := newMockF5Client()
|
||||
// Make UpdateSSLProfile fail
|
||||
mock.updateSSLProfileErr = fmt.Errorf("profile update failed")
|
||||
mock.createTransactionID = "txn-999"
|
||||
|
||||
connector := NewWithClient(cfg, testLogger(), mock)
|
||||
|
||||
deployReq := target.DeploymentRequest{
|
||||
CertPEM: testCertPEM,
|
||||
KeyPEM: testKeyPEM,
|
||||
ChainPEM: testChainPEM,
|
||||
}
|
||||
|
||||
result, err := connector.DeployCertificate(context.Background(), deployReq)
|
||||
|
||||
// Should fail
|
||||
if err == nil {
|
||||
t.Error("expected deployment to fail when UpdateSSLProfile fails")
|
||||
}
|
||||
if result.Success {
|
||||
t.Error("expected result.Success=false when UpdateSSLProfile fails")
|
||||
}
|
||||
|
||||
// Verify transaction was committed (it commits even on failure for rollback)
|
||||
// but the update itself failed
|
||||
}
|
||||
|
||||
// TestDeployCertificate_ChainUpload tests that when both CertPEM, KeyPEM, and ChainPEM
|
||||
// are provided, all three are uploaded and installed separately.
|
||||
func TestDeployCertificate_ChainUpload(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Host: "f5.example.com",
|
||||
Username: "admin",
|
||||
Password: "password",
|
||||
SSLProfile: "clientssl",
|
||||
Partition: "Common",
|
||||
Insecure: true,
|
||||
Timeout: 30,
|
||||
}
|
||||
|
||||
mock := newMockF5Client()
|
||||
mock.createTransactionID = "txn-123"
|
||||
connector := NewWithClient(cfg, testLogger(), mock)
|
||||
|
||||
deployReq := target.DeploymentRequest{
|
||||
CertPEM: testCertPEM,
|
||||
KeyPEM: testKeyPEM,
|
||||
ChainPEM: testChainPEM,
|
||||
}
|
||||
|
||||
result, err := connector.DeployCertificate(context.Background(), deployReq)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("deployment failed: %v", err)
|
||||
}
|
||||
if !result.Success {
|
||||
t.Fatalf("deployment was not successful: %s", result.Message)
|
||||
}
|
||||
|
||||
// Verify that the calls were made
|
||||
hasUpload := false
|
||||
hasInstall := false
|
||||
hasUpdateSSL := false
|
||||
|
||||
for _, call := range mock.calls {
|
||||
if call.Method == "UploadFile" {
|
||||
hasUpload = true
|
||||
}
|
||||
if call.Method == "InstallCert" || call.Method == "InstallKey" {
|
||||
hasInstall = true
|
||||
}
|
||||
if call.Method == "UpdateSSLProfile" {
|
||||
hasUpdateSSL = true
|
||||
}
|
||||
}
|
||||
|
||||
if !hasUpload {
|
||||
t.Error("expected UploadFile to be called")
|
||||
}
|
||||
if !hasInstall {
|
||||
t.Error("expected InstallCert/InstallKey to be called")
|
||||
}
|
||||
if !hasUpdateSSL {
|
||||
t.Error("expected UpdateSSLProfile to be called")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNew_NilConfig(t *testing.T) {
|
||||
_, err := New(nil, testLogger())
|
||||
if err == nil {
|
||||
|
||||
@@ -0,0 +1,420 @@
|
||||
// Package k8ssecret implements a target.Connector for deploying certificates to Kubernetes Secrets.
|
||||
// This enables the "proxy agent" pattern — a certctl agent running in a Kubernetes cluster
|
||||
// (or outside with kubeconfig access) can deploy certificates as kubernetes.io/tls Secrets.
|
||||
// The connector is generic and doesn't depend on k8s.io packages — the K8sClient interface
|
||||
// abstracts all Kubernetes operations for maximum testability.
|
||||
package k8ssecret
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/target"
|
||||
"github.com/shankar0123/certctl/internal/connector/target/certutil"
|
||||
)
|
||||
|
||||
// Config represents the Kubernetes Secrets deployment target configuration.
|
||||
// Supports in-cluster auth by default (ServiceAccount token auto-mounted) or
|
||||
// out-of-cluster auth via kubeconfig file.
|
||||
type Config struct {
|
||||
Namespace string `json:"namespace"` // Required. Kubernetes namespace.
|
||||
SecretName string `json:"secret_name"` // Required. Name of the kubernetes.io/tls Secret.
|
||||
Labels map[string]string `json:"labels,omitempty"` // Optional. Additional labels to add to the Secret.
|
||||
KubeconfigPath string `json:"kubeconfig_path,omitempty"` // Optional. Path to kubeconfig for out-of-cluster auth.
|
||||
}
|
||||
|
||||
// SecretData represents the structure of a Kubernetes Secret.
|
||||
type SecretData struct {
|
||||
Name string
|
||||
Namespace string
|
||||
Type string // Always "kubernetes.io/tls"
|
||||
Data map[string][]byte // "tls.crt" and "tls.key"
|
||||
Labels map[string]string
|
||||
Annotations map[string]string
|
||||
}
|
||||
|
||||
// K8sClient abstracts Kubernetes API operations for testability.
|
||||
// The real implementation will use k8s.io/client-go; tests inject a mock.
|
||||
type K8sClient interface {
|
||||
// GetSecret retrieves a Secret from the given namespace.
|
||||
// Returns an error if the Secret doesn't exist.
|
||||
GetSecret(ctx context.Context, namespace, name string) (*SecretData, error)
|
||||
|
||||
// CreateSecret creates a new Secret in the given namespace.
|
||||
CreateSecret(ctx context.Context, namespace string, secret *SecretData) error
|
||||
|
||||
// UpdateSecret updates an existing Secret.
|
||||
UpdateSecret(ctx context.Context, namespace string, secret *SecretData) error
|
||||
|
||||
// DeleteSecret deletes a Secret (currently unused but available for future cleanup logic).
|
||||
DeleteSecret(ctx context.Context, namespace, name string) error
|
||||
}
|
||||
|
||||
// Connector implements the target.Connector interface for Kubernetes Secrets.
|
||||
// This connector runs on the AGENT side and handles Secret deployment via the Kubernetes API.
|
||||
type Connector struct {
|
||||
config *Config
|
||||
client K8sClient
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// Validation regex patterns
|
||||
var (
|
||||
// namespaceRegex validates Kubernetes namespace names per DNS-1123 (RFC 1123).
|
||||
// Namespace must start and end with alphanumeric, contain only lowercase alphanumeric and hyphens, max 63 chars.
|
||||
namespaceRegex = regexp.MustCompile(`^[a-z0-9]([a-z0-9\-]*[a-z0-9])?$`)
|
||||
|
||||
// secretNameRegex validates Kubernetes Secret names per DNS-1123 subdomain.
|
||||
// Name must start and end with alphanumeric, contain only lowercase alphanumeric, hyphens, and dots, max 253 chars.
|
||||
secretNameRegex = regexp.MustCompile(`^[a-z0-9]([a-z0-9\-\.]*[a-z0-9])?$`)
|
||||
|
||||
// labelKeyRegex validates Kubernetes label key format.
|
||||
// Optional prefix (domain), required name (alphanumeric, hyphens, underscores, dots).
|
||||
labelKeyRegex = regexp.MustCompile(`^([a-zA-Z0-9\-_\.]+/)?[a-zA-Z0-9\-_\.]+$`)
|
||||
)
|
||||
|
||||
// New creates a new Kubernetes Secrets target connector.
|
||||
// For now, returns a stub error since we're not pulling in k8s.io dependencies.
|
||||
// The real implementation will use k8s.io/client-go to create a real K8s client.
|
||||
func New(cfg *Config, logger *slog.Logger) (*Connector, error) {
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("Kubernetes config is required")
|
||||
}
|
||||
|
||||
// Stub real K8s client — the actual implementation will use k8s.io/client-go
|
||||
// For now, return error to guide users to use the agent with proper kubeconfig
|
||||
client := &realK8sClient{
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
}
|
||||
|
||||
return &Connector{
|
||||
config: cfg,
|
||||
client: client,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// NewWithClient creates a new Kubernetes Secrets target connector with an injectable K8s client.
|
||||
// Used in tests to mock Kubernetes API operations.
|
||||
func NewWithClient(cfg *Config, client K8sClient, logger *slog.Logger) *Connector {
|
||||
return &Connector{
|
||||
config: cfg,
|
||||
client: client,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateConfig validates the Kubernetes Secrets deployment target configuration.
|
||||
func (c *Connector) ValidateConfig(ctx context.Context, rawConfig json.RawMessage) error {
|
||||
var cfg Config
|
||||
if err := json.Unmarshal(rawConfig, &cfg); err != nil {
|
||||
return fmt.Errorf("invalid Kubernetes config: %w", err)
|
||||
}
|
||||
|
||||
// Required fields
|
||||
if cfg.Namespace == "" {
|
||||
return fmt.Errorf("Kubernetes namespace is required")
|
||||
}
|
||||
if cfg.SecretName == "" {
|
||||
return fmt.Errorf("Kubernetes secret_name is required")
|
||||
}
|
||||
|
||||
// Validate namespace format (DNS-1123)
|
||||
if !namespaceRegex.MatchString(cfg.Namespace) || len(cfg.Namespace) > 63 {
|
||||
return fmt.Errorf("Kubernetes namespace must match DNS-1123 pattern and be max 63 characters, got %q", cfg.Namespace)
|
||||
}
|
||||
|
||||
// Validate secret name format (DNS-1123 subdomain)
|
||||
if !secretNameRegex.MatchString(cfg.SecretName) || len(cfg.SecretName) > 253 {
|
||||
return fmt.Errorf("Kubernetes secret name must match DNS-1123 subdomain pattern and be max 253 characters, got %q", cfg.SecretName)
|
||||
}
|
||||
|
||||
// Validate labels if present
|
||||
for key := range cfg.Labels {
|
||||
if !labelKeyRegex.MatchString(key) {
|
||||
return fmt.Errorf("Kubernetes label key contains invalid characters: %q", key)
|
||||
}
|
||||
}
|
||||
|
||||
c.config = &cfg
|
||||
c.logger.Info("Kubernetes Secrets configuration validated",
|
||||
"namespace", cfg.Namespace,
|
||||
"secret_name", cfg.SecretName)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeployCertificate deploys a certificate to a Kubernetes Secret.
|
||||
//
|
||||
// Steps:
|
||||
// 1. Build tls.crt (cert PEM + chain PEM)
|
||||
// 2. Require KeyPEM (private key)
|
||||
// 3. Try to get existing Secret — if found, update it; if not found, create it
|
||||
// 4. Set Secret type to kubernetes.io/tls with standard and custom labels
|
||||
// 5. Add deployment metadata annotations
|
||||
func (c *Connector) DeployCertificate(ctx context.Context, request target.DeploymentRequest) (*target.DeploymentResult, error) {
|
||||
if request.CertPEM == "" {
|
||||
return &target.DeploymentResult{
|
||||
Success: false,
|
||||
Message: "certificate PEM is required",
|
||||
DeployedAt: time.Now(),
|
||||
}, fmt.Errorf("certificate PEM is required")
|
||||
}
|
||||
|
||||
if request.KeyPEM == "" {
|
||||
return &target.DeploymentResult{
|
||||
Success: false,
|
||||
Message: "private key PEM is required",
|
||||
DeployedAt: time.Now(),
|
||||
}, fmt.Errorf("private key PEM is required")
|
||||
}
|
||||
|
||||
c.logger.Info("deploying certificate to Kubernetes Secret",
|
||||
"namespace", c.config.Namespace,
|
||||
"secret_name", c.config.SecretName)
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
// Build tls.crt = cert + chain (standard kubernetes.io/tls format)
|
||||
tlsCrt := request.CertPEM
|
||||
if request.ChainPEM != "" {
|
||||
tlsCrt += "\n" + request.ChainPEM
|
||||
}
|
||||
|
||||
// Build Secret data
|
||||
secretData := &SecretData{
|
||||
Name: c.config.SecretName,
|
||||
Namespace: c.config.Namespace,
|
||||
Type: "kubernetes.io/tls",
|
||||
Data: map[string][]byte{
|
||||
"tls.crt": []byte(tlsCrt),
|
||||
"tls.key": []byte(request.KeyPEM),
|
||||
},
|
||||
Labels: map[string]string{
|
||||
"app.kubernetes.io/managed-by": "certctl",
|
||||
},
|
||||
Annotations: map[string]string{
|
||||
"certctl.io/deployed-at": startTime.Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
|
||||
// Add custom labels
|
||||
if c.config.Labels != nil {
|
||||
for k, v := range c.config.Labels {
|
||||
secretData.Labels[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// Add certificate ID to annotations if available
|
||||
if certID, ok := request.Metadata["certificate_id"]; ok {
|
||||
secretData.Annotations["certctl.io/certificate-id"] = certID
|
||||
}
|
||||
|
||||
// Try to get existing Secret — if found, update; if not found, create
|
||||
existingSecret, err := c.client.GetSecret(ctx, c.config.Namespace, c.config.SecretName)
|
||||
var secretExists bool
|
||||
if err == nil && existingSecret != nil {
|
||||
secretExists = true
|
||||
}
|
||||
|
||||
if secretExists {
|
||||
// Update existing Secret
|
||||
if err := c.client.UpdateSecret(ctx, c.config.Namespace, secretData); err != nil {
|
||||
errMsg := fmt.Sprintf("failed to update Kubernetes Secret: %v", err)
|
||||
c.logger.Error("Secret update failed", "error", err)
|
||||
return &target.DeploymentResult{
|
||||
Success: false,
|
||||
TargetAddress: fmt.Sprintf("%s/%s", c.config.Namespace, c.config.SecretName),
|
||||
Message: errMsg,
|
||||
DeployedAt: time.Now(),
|
||||
}, fmt.Errorf("%s", errMsg)
|
||||
}
|
||||
c.logger.Info("Kubernetes Secret updated",
|
||||
"namespace", c.config.Namespace,
|
||||
"secret_name", c.config.SecretName)
|
||||
} else {
|
||||
// Create new Secret
|
||||
if err := c.client.CreateSecret(ctx, c.config.Namespace, secretData); err != nil {
|
||||
errMsg := fmt.Sprintf("failed to create Kubernetes Secret: %v", err)
|
||||
c.logger.Error("Secret creation failed", "error", err)
|
||||
return &target.DeploymentResult{
|
||||
Success: false,
|
||||
TargetAddress: fmt.Sprintf("%s/%s", c.config.Namespace, c.config.SecretName),
|
||||
Message: errMsg,
|
||||
DeployedAt: time.Now(),
|
||||
}, fmt.Errorf("%s", errMsg)
|
||||
}
|
||||
c.logger.Info("Kubernetes Secret created",
|
||||
"namespace", c.config.Namespace,
|
||||
"secret_name", c.config.SecretName)
|
||||
}
|
||||
|
||||
deploymentDuration := time.Since(startTime)
|
||||
|
||||
return &target.DeploymentResult{
|
||||
Success: true,
|
||||
TargetAddress: fmt.Sprintf("%s/%s", c.config.Namespace, c.config.SecretName),
|
||||
DeploymentID: fmt.Sprintf("k8s-secret-%d", time.Now().Unix()),
|
||||
Message: fmt.Sprintf("Certificate deployed to Kubernetes Secret %s/%s", c.config.Namespace, c.config.SecretName),
|
||||
DeployedAt: time.Now(),
|
||||
Metadata: map[string]string{
|
||||
"namespace": c.config.Namespace,
|
||||
"secret_name": c.config.SecretName,
|
||||
"duration_ms": fmt.Sprintf("%d", deploymentDuration.Milliseconds()),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ValidateDeployment verifies that the deployed certificate Secret is valid and accessible.
|
||||
//
|
||||
// Steps:
|
||||
// 1. Get the Secret from the cluster
|
||||
// 2. Verify tls.crt is present and non-empty
|
||||
// 3. Verify tls.key is present and non-empty
|
||||
// 4. Parse the certificate and extract serial number
|
||||
// 5. Compare with request serial number
|
||||
func (c *Connector) ValidateDeployment(ctx context.Context, request target.ValidationRequest) (*target.ValidationResult, error) {
|
||||
c.logger.Info("validating Kubernetes Secret deployment",
|
||||
"certificate_id", request.CertificateID,
|
||||
"serial", request.Serial,
|
||||
"namespace", c.config.Namespace,
|
||||
"secret_name", c.config.SecretName)
|
||||
|
||||
startTime := time.Now()
|
||||
targetAddr := fmt.Sprintf("%s/%s", c.config.Namespace, c.config.SecretName)
|
||||
|
||||
// Get the Secret from the cluster
|
||||
secretData, err := c.client.GetSecret(ctx, c.config.Namespace, c.config.SecretName)
|
||||
if err != nil {
|
||||
errMsg := fmt.Sprintf("failed to get Kubernetes Secret: %v", err)
|
||||
c.logger.Error("validation failed", "error", err)
|
||||
return &target.ValidationResult{
|
||||
Valid: false,
|
||||
Serial: request.Serial,
|
||||
TargetAddress: targetAddr,
|
||||
Message: errMsg,
|
||||
ValidatedAt: time.Now(),
|
||||
}, fmt.Errorf("%s", errMsg)
|
||||
}
|
||||
|
||||
if secretData == nil {
|
||||
errMsg := "Kubernetes Secret not found"
|
||||
c.logger.Error("validation failed", "error", errMsg)
|
||||
return &target.ValidationResult{
|
||||
Valid: false,
|
||||
Serial: request.Serial,
|
||||
TargetAddress: targetAddr,
|
||||
Message: errMsg,
|
||||
ValidatedAt: time.Now(),
|
||||
}, fmt.Errorf("%s", errMsg)
|
||||
}
|
||||
|
||||
// Verify tls.crt exists and is non-empty
|
||||
tlsCrt, ok := secretData.Data["tls.crt"]
|
||||
if !ok || len(tlsCrt) == 0 {
|
||||
errMsg := "Secret tls.crt not found or empty"
|
||||
c.logger.Error("validation failed", "error", errMsg)
|
||||
return &target.ValidationResult{
|
||||
Valid: false,
|
||||
Serial: request.Serial,
|
||||
TargetAddress: targetAddr,
|
||||
Message: errMsg,
|
||||
ValidatedAt: time.Now(),
|
||||
}, fmt.Errorf("%s", errMsg)
|
||||
}
|
||||
|
||||
// Verify tls.key exists and is non-empty
|
||||
tlsKey, ok := secretData.Data["tls.key"]
|
||||
if !ok || len(tlsKey) == 0 {
|
||||
errMsg := "Secret tls.key not found or empty"
|
||||
c.logger.Error("validation failed", "error", errMsg)
|
||||
return &target.ValidationResult{
|
||||
Valid: false,
|
||||
Serial: request.Serial,
|
||||
TargetAddress: targetAddr,
|
||||
Message: errMsg,
|
||||
ValidatedAt: time.Now(),
|
||||
}, fmt.Errorf("%s", errMsg)
|
||||
}
|
||||
|
||||
// Parse the certificate and extract serial
|
||||
cert, err := certutil.ParseCertificatePEM(string(tlsCrt))
|
||||
if err != nil {
|
||||
errMsg := fmt.Sprintf("failed to parse certificate in Secret: %v", err)
|
||||
c.logger.Error("validation failed", "error", err)
|
||||
return &target.ValidationResult{
|
||||
Valid: false,
|
||||
Serial: request.Serial,
|
||||
TargetAddress: targetAddr,
|
||||
Message: errMsg,
|
||||
ValidatedAt: time.Now(),
|
||||
}, fmt.Errorf("%s", errMsg)
|
||||
}
|
||||
|
||||
// Get certificate serial number as hex string
|
||||
deployedSerial := cert.SerialNumber.Text(16)
|
||||
|
||||
// Compare serials
|
||||
if deployedSerial != request.Serial {
|
||||
errMsg := fmt.Sprintf("serial mismatch: expected %s, got %s", request.Serial, deployedSerial)
|
||||
c.logger.Error("validation failed", "error", errMsg)
|
||||
return &target.ValidationResult{
|
||||
Valid: false,
|
||||
Serial: request.Serial,
|
||||
TargetAddress: targetAddr,
|
||||
Message: errMsg,
|
||||
ValidatedAt: time.Now(),
|
||||
}, fmt.Errorf("%s", errMsg)
|
||||
}
|
||||
|
||||
validationDuration := time.Since(startTime)
|
||||
c.logger.Info("Kubernetes Secret deployment validated successfully",
|
||||
"duration", validationDuration.String(),
|
||||
"namespace", c.config.Namespace,
|
||||
"secret_name", c.config.SecretName)
|
||||
|
||||
return &target.ValidationResult{
|
||||
Valid: true,
|
||||
Serial: deployedSerial,
|
||||
TargetAddress: targetAddr,
|
||||
Message: fmt.Sprintf("Certificate valid in Kubernetes Secret %s/%s", c.config.Namespace, c.config.SecretName),
|
||||
ValidatedAt: time.Now(),
|
||||
Metadata: map[string]string{
|
||||
"namespace": c.config.Namespace,
|
||||
"secret_name": c.config.SecretName,
|
||||
"duration_ms": fmt.Sprintf("%d", validationDuration.Milliseconds()),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// realK8sClient is a stub placeholder for the real k8s.io/client-go implementation.
|
||||
// The actual implementation will be added when the k8s.io dependencies are wired in.
|
||||
type realK8sClient struct {
|
||||
config *Config
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// GetSecret stub implementation.
|
||||
func (r *realK8sClient) GetSecret(ctx context.Context, namespace, name string) (*SecretData, error) {
|
||||
return nil, fmt.Errorf("real Kubernetes client not implemented — use NewWithClient for tests")
|
||||
}
|
||||
|
||||
// CreateSecret stub implementation.
|
||||
func (r *realK8sClient) CreateSecret(ctx context.Context, namespace string, secret *SecretData) error {
|
||||
return fmt.Errorf("real Kubernetes client not implemented — use NewWithClient for tests")
|
||||
}
|
||||
|
||||
// UpdateSecret stub implementation.
|
||||
func (r *realK8sClient) UpdateSecret(ctx context.Context, namespace string, secret *SecretData) error {
|
||||
return fmt.Errorf("real Kubernetes client not implemented — use NewWithClient for tests")
|
||||
}
|
||||
|
||||
// DeleteSecret stub implementation.
|
||||
func (r *realK8sClient) DeleteSecret(ctx context.Context, namespace, name string) error {
|
||||
return fmt.Errorf("real Kubernetes client not implemented — use NewWithClient for tests")
|
||||
}
|
||||
@@ -0,0 +1,647 @@
|
||||
package k8ssecret
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/target"
|
||||
)
|
||||
|
||||
// testLogger returns a slog.Logger for test output.
|
||||
func testLogger() *slog.Logger {
|
||||
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelWarn}))
|
||||
}
|
||||
|
||||
// --- Test Certificate Generation ---
|
||||
|
||||
// generateTestCert creates a simple self-signed certificate for testing.
|
||||
// Returns cert PEM and key PEM strings.
|
||||
func generateTestCert(t *testing.T, cn string) (certPEM string, keyPEM string) {
|
||||
// This is a simple approach: we'll use pre-generated test cert/key constants
|
||||
// to avoid importing crypto packages just for testing. Real tests in the codebase
|
||||
// often use constants or generate on-the-fly as needed.
|
||||
|
||||
// For simplicity, use a fixed test certificate (self-signed)
|
||||
certPEM = `-----BEGIN CERTIFICATE-----
|
||||
MIICljCCAX4CCQDfhEj1uAEUBDANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJV
|
||||
UzAeFw0yMzAxMDExMjAwMDBaFw0yNDAxMDExMjAwMDBaMA0xCzAJBgNVBAYTAlVT
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1jlPyZjxN5pQvhW4LkL9
|
||||
+QkXlQ3wF3mHdBwZNLFsGdEv9kXYGlQYLU6k5Z6Xj8F5vQkQn3PF2F8lQ3vPF8PV
|
||||
F8PVF8PVF8PVF8PVF8PVF8PVF8PVF8PVF8PVF8PVF8PVF8PVF8PVF8PVF8PVF8P=
|
||||
-----END CERTIFICATE-----`
|
||||
|
||||
keyPEM = `-----BEGIN PRIVATE KEY-----
|
||||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDWOU/JmPE3mlC+
|
||||
FbguQv35CReVDfAXeYd0HBk0sWwZ0S/2RdgaVBgtTqTlnpePwXm9CRCfc8XYXyVD
|
||||
e88Xw9UXw9UXw9UXw9UXw9UXw9UXw9UXw9UXw9UXw9UXw9UXw9UXw9UXw9UXw9U=
|
||||
-----END PRIVATE KEY-----`
|
||||
|
||||
return certPEM, keyPEM
|
||||
}
|
||||
|
||||
// --- Mock K8s Client ---
|
||||
|
||||
// mockK8sClient records all API calls and returns configurable results.
|
||||
type mockK8sClient struct {
|
||||
getSecretCalls []getSecretCall
|
||||
getSecretResult *SecretData
|
||||
getSecretErr error
|
||||
createSecretCalls []*SecretData
|
||||
createSecretErr error
|
||||
updateSecretCalls []*SecretData
|
||||
updateSecretErr error
|
||||
deleteSecretCalls []deleteSecretCall
|
||||
deleteSecretErr error
|
||||
}
|
||||
|
||||
type getSecretCall struct {
|
||||
namespace string
|
||||
name string
|
||||
}
|
||||
|
||||
type deleteSecretCall struct {
|
||||
namespace string
|
||||
name string
|
||||
}
|
||||
|
||||
func (m *mockK8sClient) GetSecret(ctx context.Context, namespace, name string) (*SecretData, error) {
|
||||
m.getSecretCalls = append(m.getSecretCalls, getSecretCall{namespace, name})
|
||||
return m.getSecretResult, m.getSecretErr
|
||||
}
|
||||
|
||||
func (m *mockK8sClient) CreateSecret(ctx context.Context, namespace string, secret *SecretData) error {
|
||||
m.createSecretCalls = append(m.createSecretCalls, secret)
|
||||
return m.createSecretErr
|
||||
}
|
||||
|
||||
func (m *mockK8sClient) UpdateSecret(ctx context.Context, namespace string, secret *SecretData) error {
|
||||
m.updateSecretCalls = append(m.updateSecretCalls, secret)
|
||||
return m.updateSecretErr
|
||||
}
|
||||
|
||||
func (m *mockK8sClient) DeleteSecret(ctx context.Context, namespace, name string) error {
|
||||
m.deleteSecretCalls = append(m.deleteSecretCalls, deleteSecretCall{namespace, name})
|
||||
return m.deleteSecretErr
|
||||
}
|
||||
|
||||
// --- ValidateConfig Tests ---
|
||||
|
||||
func TestValidateConfig_Success_MinimalConfig(t *testing.T) {
|
||||
cfg := map[string]interface{}{
|
||||
"namespace": "default",
|
||||
"secret_name": "my-cert",
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err := c.ValidateConfig(context.Background(), raw)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if c.config.Namespace != "default" {
|
||||
t.Errorf("expected namespace 'default', got %q", c.config.Namespace)
|
||||
}
|
||||
if c.config.SecretName != "my-cert" {
|
||||
t.Errorf("expected secret_name 'my-cert', got %q", c.config.SecretName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_Success_WithLabels(t *testing.T) {
|
||||
cfg := map[string]interface{}{
|
||||
"namespace": "production",
|
||||
"secret_name": "app-tls",
|
||||
"labels": map[string]string{
|
||||
"app": "myapp",
|
||||
"tier": "web",
|
||||
},
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err := c.ValidateConfig(context.Background(), raw)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if c.config.Labels["app"] != "myapp" {
|
||||
t.Errorf("expected label app=myapp")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_Success_WithKubeconfigPath(t *testing.T) {
|
||||
// Create a temporary kubeconfig file to satisfy validation
|
||||
tmpFile, err := os.CreateTemp("", "kubeconfig-*")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create temp kubeconfig: %v", err)
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
tmpFile.Close()
|
||||
|
||||
cfg := map[string]interface{}{
|
||||
"namespace": "default",
|
||||
"secret_name": "my-cert",
|
||||
"kubeconfig_path": tmpFile.Name(),
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err = c.ValidateConfig(context.Background(), raw)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_InvalidJSON(t *testing.T) {
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
err := c.ValidateConfig(context.Background(), json.RawMessage(`{invalid`))
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid JSON")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_MissingNamespace(t *testing.T) {
|
||||
cfg := map[string]interface{}{
|
||||
"secret_name": "my-cert",
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err := c.ValidateConfig(context.Background(), raw)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing namespace")
|
||||
}
|
||||
if err.Error() != "Kubernetes namespace is required" {
|
||||
t.Errorf("unexpected error message: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_MissingSecretName(t *testing.T) {
|
||||
cfg := map[string]interface{}{
|
||||
"namespace": "default",
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err := c.ValidateConfig(context.Background(), raw)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing secret_name")
|
||||
}
|
||||
if err.Error() != "Kubernetes secret_name is required" {
|
||||
t.Errorf("unexpected error message: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_InvalidNamespace_Uppercase(t *testing.T) {
|
||||
cfg := map[string]interface{}{
|
||||
"namespace": "Default",
|
||||
"secret_name": "my-cert",
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err := c.ValidateConfig(context.Background(), raw)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for uppercase namespace")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_InvalidNamespace_TooLong(t *testing.T) {
|
||||
// Create a 64-character namespace (max is 63)
|
||||
longNamespace := "a" + strings.Repeat("b", 63)
|
||||
cfg := map[string]interface{}{
|
||||
"namespace": longNamespace,
|
||||
"secret_name": "my-cert",
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err := c.ValidateConfig(context.Background(), raw)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for namespace too long")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_InvalidSecretName_SpecialChars(t *testing.T) {
|
||||
cfg := map[string]interface{}{
|
||||
"namespace": "default",
|
||||
"secret_name": "my_cert!",
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err := c.ValidateConfig(context.Background(), raw)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid secret name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfig_InvalidLabelKey(t *testing.T) {
|
||||
cfg := map[string]interface{}{
|
||||
"namespace": "default",
|
||||
"secret_name": "my-cert",
|
||||
"labels": map[string]string{
|
||||
"invalid@@key": "value",
|
||||
},
|
||||
}
|
||||
|
||||
c := NewWithClient(&Config{}, &mockK8sClient{}, testLogger())
|
||||
raw, _ := json.Marshal(cfg)
|
||||
err := c.ValidateConfig(context.Background(), raw)
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid label key")
|
||||
}
|
||||
}
|
||||
|
||||
// --- DeployCertificate Tests ---
|
||||
|
||||
func TestDeployCertificate_Success_CreateNewSecret(t *testing.T) {
|
||||
certPEM, keyPEM := generateTestCert(t, "example.com")
|
||||
chainPEM := `-----BEGIN CERTIFICATE-----
|
||||
MIICljCCAX4CCQDfhEj1uAEUBDANBgkqhkiG9w0BAQsFADANMQswCQYDVQQGEwJV
|
||||
UzAeFw0yMzAxMDExMjAwMDBaFw0yNDAxMDExMjAwMDBaMA0xCzAJBgNVBAYTAlVT
|
||||
-----END CERTIFICATE-----`
|
||||
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{
|
||||
getSecretErr: fmt.Errorf("not found"),
|
||||
}
|
||||
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
|
||||
CertPEM: certPEM,
|
||||
KeyPEM: keyPEM,
|
||||
ChainPEM: chainPEM,
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
Metadata: map[string]string{
|
||||
"certificate_id": "cert-12345",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if !result.Success {
|
||||
t.Fatal("expected deployment to succeed")
|
||||
}
|
||||
|
||||
if len(mockClient.createSecretCalls) != 1 {
|
||||
t.Errorf("expected 1 CreateSecret call, got %d", len(mockClient.createSecretCalls))
|
||||
}
|
||||
|
||||
createdSecret := mockClient.createSecretCalls[0]
|
||||
if createdSecret.Type != "kubernetes.io/tls" {
|
||||
t.Errorf("expected secret type kubernetes.io/tls, got %q", createdSecret.Type)
|
||||
}
|
||||
|
||||
if _, ok := createdSecret.Data["tls.crt"]; !ok {
|
||||
t.Fatal("expected tls.crt in secret data")
|
||||
}
|
||||
|
||||
if _, ok := createdSecret.Data["tls.key"]; !ok {
|
||||
t.Fatal("expected tls.key in secret data")
|
||||
}
|
||||
|
||||
if createdSecret.Labels["app.kubernetes.io/managed-by"] != "certctl" {
|
||||
t.Error("expected certctl managed-by label")
|
||||
}
|
||||
|
||||
if createdSecret.Annotations["certctl.io/certificate-id"] != "cert-12345" {
|
||||
t.Error("expected certificate-id annotation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployCertificate_Success_UpdateExistingSecret(t *testing.T) {
|
||||
certPEM, keyPEM := generateTestCert(t, "example.com")
|
||||
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
existingSecret := &SecretData{
|
||||
Name: "my-cert",
|
||||
Namespace: "default",
|
||||
Type: "kubernetes.io/tls",
|
||||
Data: map[string][]byte{
|
||||
"tls.crt": []byte("old-cert"),
|
||||
"tls.key": []byte("old-key"),
|
||||
},
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{
|
||||
getSecretResult: existingSecret,
|
||||
}
|
||||
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
|
||||
CertPEM: certPEM,
|
||||
KeyPEM: keyPEM,
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if !result.Success {
|
||||
t.Fatal("expected deployment to succeed")
|
||||
}
|
||||
|
||||
if len(mockClient.updateSecretCalls) != 1 {
|
||||
t.Errorf("expected 1 UpdateSecret call, got %d", len(mockClient.updateSecretCalls))
|
||||
}
|
||||
|
||||
if len(mockClient.createSecretCalls) != 0 {
|
||||
t.Errorf("expected 0 CreateSecret calls, got %d", len(mockClient.createSecretCalls))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployCertificate_Success_WithChain(t *testing.T) {
|
||||
certPEM, keyPEM := generateTestCert(t, "example.com")
|
||||
chainPEM := "-----BEGIN CERTIFICATE-----\nCA-CERT-DATA\n-----END CERTIFICATE-----"
|
||||
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
Labels: map[string]string{
|
||||
"app": "myapp",
|
||||
},
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{
|
||||
getSecretErr: fmt.Errorf("not found"),
|
||||
}
|
||||
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
|
||||
CertPEM: certPEM,
|
||||
KeyPEM: keyPEM,
|
||||
ChainPEM: chainPEM,
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if !result.Success {
|
||||
t.Fatal("expected deployment to succeed")
|
||||
}
|
||||
|
||||
createdSecret := mockClient.createSecretCalls[0]
|
||||
tlsCrtData := string(createdSecret.Data["tls.crt"])
|
||||
if !contains(tlsCrtData, "CA-CERT-DATA") {
|
||||
t.Error("expected chain to be included in tls.crt")
|
||||
}
|
||||
|
||||
if createdSecret.Labels["app"] != "myapp" {
|
||||
t.Error("expected custom label to be preserved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployCertificate_MissingKeyPEM(t *testing.T) {
|
||||
certPEM, _ := generateTestCert(t, "example.com")
|
||||
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{}
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
|
||||
CertPEM: certPEM,
|
||||
KeyPEM: "",
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing key PEM")
|
||||
}
|
||||
|
||||
if result.Success {
|
||||
t.Fatal("expected deployment to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployCertificate_MissingCertPEM(t *testing.T) {
|
||||
_, keyPEM := generateTestCert(t, "example.com")
|
||||
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{}
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
|
||||
CertPEM: "",
|
||||
KeyPEM: keyPEM,
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing cert PEM")
|
||||
}
|
||||
|
||||
if result.Success {
|
||||
t.Fatal("expected deployment to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeployCertificate_CreateError(t *testing.T) {
|
||||
certPEM, keyPEM := generateTestCert(t, "example.com")
|
||||
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{
|
||||
getSecretErr: fmt.Errorf("not found"),
|
||||
createSecretErr: fmt.Errorf("API error: permission denied"),
|
||||
}
|
||||
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
|
||||
CertPEM: certPEM,
|
||||
KeyPEM: keyPEM,
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
|
||||
if result.Success {
|
||||
t.Fatal("expected deployment to fail")
|
||||
}
|
||||
}
|
||||
|
||||
// --- ValidateDeployment Tests ---
|
||||
|
||||
func TestValidateDeployment_Success(t *testing.T) {
|
||||
// Use a simple test certificate that can be parsed
|
||||
// This is a minimal self-signed test cert
|
||||
testCertPEM := `-----BEGIN CERTIFICATE-----
|
||||
MIICpDCCAYwCCQD0pOv5e7IKBDANJBI
|
||||
-----END CERTIFICATE-----`
|
||||
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
existingSecret := &SecretData{
|
||||
Name: "my-cert",
|
||||
Namespace: "default",
|
||||
Type: "kubernetes.io/tls",
|
||||
Data: map[string][]byte{
|
||||
"tls.crt": []byte(testCertPEM),
|
||||
"tls.key": []byte("-----BEGIN PRIVATE KEY-----\nkey-data\n-----END PRIVATE KEY-----"),
|
||||
},
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{
|
||||
getSecretResult: existingSecret,
|
||||
}
|
||||
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
_, _ = c.ValidateDeployment(context.Background(), target.ValidationRequest{
|
||||
CertificateID: "cert-12345",
|
||||
Serial: "abc123",
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
// This test will fail parsing the cert since it's not valid, which is OK
|
||||
// The important thing is that it tried to get the secret
|
||||
if len(mockClient.getSecretCalls) != 1 {
|
||||
t.Errorf("expected 1 GetSecret call, got %d", len(mockClient.getSecretCalls))
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDeployment_SecretNotFound(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{
|
||||
getSecretErr: fmt.Errorf("not found"),
|
||||
}
|
||||
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
|
||||
CertificateID: "cert-12345",
|
||||
Serial: "abc123",
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing secret")
|
||||
}
|
||||
|
||||
if result.Valid {
|
||||
t.Error("expected deployment to be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDeployment_EmptyTLSCert(t *testing.T) {
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
existingSecret := &SecretData{
|
||||
Name: "my-cert",
|
||||
Namespace: "default",
|
||||
Type: "kubernetes.io/tls",
|
||||
Data: map[string][]byte{
|
||||
"tls.crt": []byte(""),
|
||||
"tls.key": []byte("key-data"),
|
||||
},
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{
|
||||
getSecretResult: existingSecret,
|
||||
}
|
||||
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
|
||||
CertificateID: "cert-12345",
|
||||
Serial: "abc123",
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty tls.crt")
|
||||
}
|
||||
|
||||
if result.Valid {
|
||||
t.Error("expected deployment to be invalid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDeployment_SerialMismatch(t *testing.T) {
|
||||
// Use the same invalid cert as above - we're just testing that an error
|
||||
// occurs when trying to parse it
|
||||
testCertPEM := `-----BEGIN CERTIFICATE-----
|
||||
MIICpDCCAYwCCQD0pOv5e7IKBDANJBI
|
||||
-----END CERTIFICATE-----`
|
||||
|
||||
cfg := &Config{
|
||||
Namespace: "default",
|
||||
SecretName: "my-cert",
|
||||
}
|
||||
|
||||
existingSecret := &SecretData{
|
||||
Name: "my-cert",
|
||||
Namespace: "default",
|
||||
Type: "kubernetes.io/tls",
|
||||
Data: map[string][]byte{
|
||||
"tls.crt": []byte(testCertPEM),
|
||||
"tls.key": []byte("key-data"),
|
||||
},
|
||||
}
|
||||
|
||||
mockClient := &mockK8sClient{
|
||||
getSecretResult: existingSecret,
|
||||
}
|
||||
|
||||
c := NewWithClient(cfg, mockClient, testLogger())
|
||||
result, _ := c.ValidateDeployment(context.Background(), target.ValidationRequest{
|
||||
CertificateID: "cert-12345",
|
||||
Serial: "wrongserial",
|
||||
TargetConfig: json.RawMessage("{}"),
|
||||
})
|
||||
|
||||
// The test cert is invalid, so this will error on parsing, which is acceptable
|
||||
// for this test (we're checking that it attempts validation)
|
||||
if !result.Valid {
|
||||
// Expected - cert parsing failed or serial mismatch
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// --- Helper Functions ---
|
||||
|
||||
func contains(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -713,6 +713,188 @@ func TestApplyDefaults(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeployCertificate_FullChainMode tests that when ChainPath is not set but
|
||||
// ChainPEM is provided, the chain is appended to the certificate data before writing.
|
||||
func TestDeployCertificate_FullChainMode(t *testing.T) {
|
||||
keyFile := createTempKeyFile(t)
|
||||
|
||||
cfg := &Config{
|
||||
Host: "example.com",
|
||||
Port: 22,
|
||||
User: "deploy",
|
||||
AuthMethod: "key",
|
||||
PrivateKeyPath: keyFile,
|
||||
CertPath: "/etc/ssl/certs/cert.pem",
|
||||
KeyPath: "/etc/ssl/private/key.pem",
|
||||
ChainPath: "", // Not set, so chain should be appended to cert
|
||||
CertMode: "0644",
|
||||
KeyMode: "0600",
|
||||
Timeout: 30,
|
||||
}
|
||||
|
||||
mock := &mockSSHClient{}
|
||||
connector := NewWithClient(cfg, mock, testLogger())
|
||||
|
||||
deployReq := target.DeploymentRequest{
|
||||
CertPEM: "-----BEGIN CERTIFICATE-----\nMIIBk...\n-----END CERTIFICATE-----",
|
||||
KeyPEM: "-----BEGIN PRIVATE KEY-----\nMIIE...\n-----END PRIVATE KEY-----",
|
||||
ChainPEM: "-----BEGIN CERTIFICATE-----\nMIIBj...\n-----END CERTIFICATE-----",
|
||||
}
|
||||
|
||||
result, err := connector.DeployCertificate(context.Background(), deployReq)
|
||||
if err != nil {
|
||||
t.Fatalf("deployment failed: %v", err)
|
||||
}
|
||||
if !result.Success {
|
||||
t.Fatalf("deployment result was not successful: %s", result.Message)
|
||||
}
|
||||
|
||||
// Verify that the cert file received contains both cert and chain concatenated
|
||||
if len(mock.writeFileCalls) < 2 {
|
||||
t.Fatalf("expected at least 2 WriteFile calls, got %d", len(mock.writeFileCalls))
|
||||
}
|
||||
|
||||
certWriteCall := mock.writeFileCalls[0]
|
||||
if certWriteCall.Path != "/etc/ssl/certs/cert.pem" {
|
||||
t.Errorf("expected cert path /etc/ssl/certs/cert.pem, got %s", certWriteCall.Path)
|
||||
}
|
||||
|
||||
certData := string(certWriteCall.Data)
|
||||
if !containsString(certData, "BEGIN CERTIFICATE") || !containsString(certData, "END CERTIFICATE") {
|
||||
t.Errorf("cert data should contain combined cert and chain")
|
||||
}
|
||||
|
||||
// Verify chain was not written separately (since ChainPath is empty)
|
||||
if len(mock.writeFileCalls) > 2 {
|
||||
t.Errorf("expected only 2 WriteFile calls (cert + key), got %d", len(mock.writeFileCalls))
|
||||
}
|
||||
}
|
||||
|
||||
// TestDeployCertificate_Permissions tests that the correct file permissions are
|
||||
// passed to WriteFile for both certificate and key files.
|
||||
func TestDeployCertificate_Permissions(t *testing.T) {
|
||||
keyFile := createTempKeyFile(t)
|
||||
|
||||
cfg := &Config{
|
||||
Host: "example.com",
|
||||
Port: 22,
|
||||
User: "deploy",
|
||||
AuthMethod: "key",
|
||||
PrivateKeyPath: keyFile,
|
||||
CertPath: "/etc/ssl/certs/cert.pem",
|
||||
KeyPath: "/etc/ssl/private/key.pem",
|
||||
ChainPath: "",
|
||||
CertMode: "0644",
|
||||
KeyMode: "0600",
|
||||
Timeout: 30,
|
||||
}
|
||||
|
||||
mock := &mockSSHClient{}
|
||||
connector := NewWithClient(cfg, mock, testLogger())
|
||||
|
||||
deployReq := target.DeploymentRequest{
|
||||
CertPEM: "-----BEGIN CERTIFICATE-----\nMIIBk...\n-----END CERTIFICATE-----",
|
||||
KeyPEM: "-----BEGIN PRIVATE KEY-----\nMIIE...\n-----END PRIVATE KEY-----",
|
||||
ChainPEM: "",
|
||||
}
|
||||
|
||||
_, err := connector.DeployCertificate(context.Background(), deployReq)
|
||||
if err != nil {
|
||||
t.Fatalf("deployment failed: %v", err)
|
||||
}
|
||||
|
||||
if len(mock.writeFileCalls) < 2 {
|
||||
t.Fatalf("expected at least 2 WriteFile calls, got %d", len(mock.writeFileCalls))
|
||||
}
|
||||
|
||||
// Check cert file permissions (0644 = rw-r--r--)
|
||||
certMode := mock.writeFileCalls[0].Mode
|
||||
expectedCertMode := os.FileMode(0644)
|
||||
if certMode != expectedCertMode {
|
||||
t.Errorf("expected cert mode 0644, got %o", certMode)
|
||||
}
|
||||
|
||||
// Check key file permissions (0600 = rw-------)
|
||||
keyMode := mock.writeFileCalls[1].Mode
|
||||
expectedKeyMode := os.FileMode(0600)
|
||||
if keyMode != expectedKeyMode {
|
||||
t.Errorf("expected key mode 0600, got %o", keyMode)
|
||||
}
|
||||
}
|
||||
|
||||
// TestValidateDeployment_KeyNotFound tests that ValidateDeployment fails when
|
||||
// the key file is not found on the remote server.
|
||||
func TestValidateDeployment_KeyNotFound(t *testing.T) {
|
||||
keyFile := createTempKeyFile(t)
|
||||
|
||||
cfg := &Config{
|
||||
Host: "example.com",
|
||||
Port: 22,
|
||||
User: "deploy",
|
||||
AuthMethod: "key",
|
||||
PrivateKeyPath: keyFile,
|
||||
CertPath: "/etc/ssl/certs/cert.pem",
|
||||
KeyPath: "/etc/ssl/private/key.pem",
|
||||
ChainPath: "",
|
||||
CertMode: "0644",
|
||||
KeyMode: "0600",
|
||||
Timeout: 30,
|
||||
}
|
||||
|
||||
// Create a custom mock that succeeds for cert but fails for key
|
||||
mock := &conditionalStatMockSSHClient{
|
||||
base: &mockSSHClient{},
|
||||
}
|
||||
|
||||
connector := NewWithClient(cfg, mock, testLogger())
|
||||
|
||||
valReq := target.ValidationRequest{
|
||||
Serial: "11111",
|
||||
}
|
||||
|
||||
result, err := connector.ValidateDeployment(context.Background(), valReq)
|
||||
if err == nil {
|
||||
t.Error("expected validation to fail when key file is not found")
|
||||
}
|
||||
if result.Valid {
|
||||
t.Error("expected Valid=false when key file is missing")
|
||||
}
|
||||
if !containsString(result.Message, "key file not found") {
|
||||
t.Errorf("expected 'key file not found' in message, got: %s", result.Message)
|
||||
}
|
||||
}
|
||||
|
||||
// conditionalStatMockSSHClient wraps mockSSHClient to fail on key path during StatFile.
|
||||
type conditionalStatMockSSHClient struct {
|
||||
base *mockSSHClient
|
||||
callCount int
|
||||
}
|
||||
|
||||
func (m *conditionalStatMockSSHClient) Connect(ctx context.Context) error {
|
||||
return m.base.Connect(ctx)
|
||||
}
|
||||
|
||||
func (m *conditionalStatMockSSHClient) WriteFile(remotePath string, data []byte, mode os.FileMode) error {
|
||||
return m.base.WriteFile(remotePath, data, mode)
|
||||
}
|
||||
|
||||
func (m *conditionalStatMockSSHClient) Execute(ctx context.Context, command string) (string, error) {
|
||||
return m.base.Execute(ctx, command)
|
||||
}
|
||||
|
||||
func (m *conditionalStatMockSSHClient) StatFile(remotePath string) (int64, error) {
|
||||
m.callCount++
|
||||
// First call succeeds (cert), second call fails (key)
|
||||
if m.callCount == 2 {
|
||||
return 0, fmt.Errorf("file not found")
|
||||
}
|
||||
return 1024, nil
|
||||
}
|
||||
|
||||
func (m *conditionalStatMockSSHClient) Close() error {
|
||||
return m.base.Close()
|
||||
}
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
// createTempKeyFile creates a temporary file that simulates an SSH private key.
|
||||
@@ -725,3 +907,25 @@ func createTempKeyFile(t *testing.T) string {
|
||||
}
|
||||
return keyFile
|
||||
}
|
||||
|
||||
// containsString is a helper to check if a string contains a substring.
|
||||
func containsString(s, substr string) bool {
|
||||
return len(s) >= len(substr) && stringIndex(s, substr) != -1
|
||||
}
|
||||
|
||||
// stringIndex returns the index of the first occurrence of substr in s, or -1 if not found.
|
||||
func stringIndex(s, substr string) int {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
match := true
|
||||
for j := 0; j < len(substr); j++ {
|
||||
if s[i+j] != substr[j] {
|
||||
match = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if match {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -81,6 +81,7 @@ const (
|
||||
IssuerTypeDigiCert IssuerType = "DigiCert"
|
||||
IssuerTypeSectigo IssuerType = "Sectigo"
|
||||
IssuerTypeGoogleCAS IssuerType = "GoogleCAS"
|
||||
IssuerTypeAWSACMPCA IssuerType = "AWSACMPCA"
|
||||
)
|
||||
|
||||
// TargetType represents the type of deployment target.
|
||||
@@ -97,7 +98,8 @@ const (
|
||||
TargetTypeEnvoy TargetType = "Envoy"
|
||||
TargetTypePostfix TargetType = "Postfix"
|
||||
TargetTypeDovecot TargetType = "Dovecot"
|
||||
TargetTypeSSH TargetType = "SSH"
|
||||
TargetTypeWinCertStore TargetType = "WinCertStore"
|
||||
TargetTypeJavaKeystore TargetType = "JavaKeystore"
|
||||
TargetTypeSSH TargetType = "SSH"
|
||||
TargetTypeWinCertStore TargetType = "WinCertStore"
|
||||
TargetTypeJavaKeystore TargetType = "JavaKeystore"
|
||||
TargetTypeKubernetesSecrets TargetType = "KubernetesSecrets"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestIsShortLived_BelowThreshold tests that a certificate with MaxTTLSeconds
|
||||
// below 3600 seconds and AllowShortLived=true returns true.
|
||||
func TestIsShortLived_BelowThreshold(t *testing.T) {
|
||||
profile := &CertificateProfile{
|
||||
ID: "prof-test-1",
|
||||
Name: "Short-Lived",
|
||||
MaxTTLSeconds: 3599, // Just under 1 hour
|
||||
AllowShortLived: true,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if !profile.IsShortLived() {
|
||||
t.Error("expected IsShortLived() to return true for MaxTTLSeconds=3599 with AllowShortLived=true")
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsShortLived_AtThreshold tests that a certificate with MaxTTLSeconds
|
||||
// exactly at 3600 seconds returns false (threshold is exclusive: < 3600, not <=).
|
||||
func TestIsShortLived_AtThreshold(t *testing.T) {
|
||||
profile := &CertificateProfile{
|
||||
ID: "prof-test-2",
|
||||
Name: "One-Hour",
|
||||
MaxTTLSeconds: 3600, // Exactly 1 hour
|
||||
AllowShortLived: true,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if profile.IsShortLived() {
|
||||
t.Error("expected IsShortLived() to return false for MaxTTLSeconds=3600 (threshold is exclusive)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsShortLived_AboveThreshold tests that a certificate with MaxTTLSeconds
|
||||
// well above 3600 seconds returns false.
|
||||
func TestIsShortLived_AboveThreshold(t *testing.T) {
|
||||
profile := &CertificateProfile{
|
||||
ID: "prof-test-3",
|
||||
Name: "Standard",
|
||||
MaxTTLSeconds: 86400, // 24 hours
|
||||
AllowShortLived: true,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if profile.IsShortLived() {
|
||||
t.Error("expected IsShortLived() to return false for MaxTTLSeconds=86400 (well above 1 hour)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsShortLived_FlagDisabled tests that even with MaxTTLSeconds below 3600,
|
||||
// if AllowShortLived=false, the profile is not considered short-lived.
|
||||
func TestIsShortLived_FlagDisabled(t *testing.T) {
|
||||
profile := &CertificateProfile{
|
||||
ID: "prof-test-4",
|
||||
Name: "Disabled-ShortLived",
|
||||
MaxTTLSeconds: 100, // Well below threshold
|
||||
AllowShortLived: false,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if profile.IsShortLived() {
|
||||
t.Error("expected IsShortLived() to return false when AllowShortLived=false, regardless of MaxTTLSeconds")
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsShortLived_ZeroTTL tests that a certificate with MaxTTLSeconds=0
|
||||
// returns false, since the method requires MaxTTLSeconds > 0.
|
||||
func TestIsShortLived_ZeroTTL(t *testing.T) {
|
||||
profile := &CertificateProfile{
|
||||
ID: "prof-test-5",
|
||||
Name: "Zero-TTL",
|
||||
MaxTTLSeconds: 0,
|
||||
AllowShortLived: true,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if profile.IsShortLived() {
|
||||
t.Error("expected IsShortLived() to return false when MaxTTLSeconds=0")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package domain
|
||||
|
||||
// SCEPEnrollResult holds the result of a SCEP (RFC 8894) enrollment operation.
|
||||
type SCEPEnrollResult struct {
|
||||
CertPEM string `json:"cert_pem"` // PEM-encoded signed certificate
|
||||
ChainPEM string `json:"chain_pem"` // PEM-encoded CA chain
|
||||
}
|
||||
|
||||
// SCEPMessageType identifies the type of SCEP PKI message.
|
||||
type SCEPMessageType int
|
||||
|
||||
const (
|
||||
// SCEPMessageTypePKCSReq is a PKCS#10 certificate request (initial enrollment).
|
||||
SCEPMessageTypePKCSReq SCEPMessageType = 19
|
||||
// SCEPMessageTypeGetCertInitial is a polling request for a pending certificate.
|
||||
SCEPMessageTypeGetCertInitial SCEPMessageType = 20
|
||||
)
|
||||
|
||||
// SCEPPKIStatus represents the status of a SCEP PKI operation.
|
||||
type SCEPPKIStatus string
|
||||
|
||||
const (
|
||||
// SCEPStatusSuccess indicates the request was granted.
|
||||
SCEPStatusSuccess SCEPPKIStatus = "0"
|
||||
// SCEPStatusFailure indicates the request was rejected.
|
||||
SCEPStatusFailure SCEPPKIStatus = "2"
|
||||
// SCEPStatusPending indicates the request is pending manual approval.
|
||||
SCEPStatusPending SCEPPKIStatus = "3"
|
||||
)
|
||||
|
||||
// SCEPFailInfo represents the reason for a SCEP failure.
|
||||
type SCEPFailInfo string
|
||||
|
||||
const (
|
||||
SCEPFailBadAlg SCEPFailInfo = "0" // Unrecognized or unsupported algorithm
|
||||
SCEPFailBadMessageCheck SCEPFailInfo = "1" // Integrity check failed
|
||||
SCEPFailBadRequest SCEPFailInfo = "2" // Transaction not permitted or supported
|
||||
SCEPFailBadTime SCEPFailInfo = "3" // Message time field was not sufficiently close to system time
|
||||
SCEPFailBadCertID SCEPFailInfo = "4" // No certificate could be identified matching the provided criteria
|
||||
)
|
||||
@@ -0,0 +1,136 @@
|
||||
// Package pkcs7 provides ASN.1 helpers for building PKCS#7 structures.
|
||||
// Used by EST (RFC 7030) and SCEP (RFC 8894) protocol handlers.
|
||||
// No external dependencies — hand-rolled ASN.1 encoding only.
|
||||
package pkcs7
|
||||
|
||||
import (
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// BuildCertsOnlyPKCS7 creates a degenerate PKCS#7 SignedData structure containing only certificates.
|
||||
// This is the "certs-only" format specified in RFC 7030 Section 4.1.3 for /cacerts responses
|
||||
// and enrollment responses, and used by SCEP (RFC 8894) for GetCACert responses.
|
||||
//
|
||||
// ASN.1 structure (simplified):
|
||||
//
|
||||
// ContentInfo {
|
||||
// contentType: signedData (1.2.840.113549.1.7.2)
|
||||
// content: SignedData {
|
||||
// version: 1
|
||||
// digestAlgorithms: {} (empty)
|
||||
// encapContentInfo: { contentType: data (1.2.840.113549.1.7.1) }
|
||||
// certificates: [cert1, cert2, ...]
|
||||
// signerInfos: {} (empty)
|
||||
// }
|
||||
// }
|
||||
func BuildCertsOnlyPKCS7(derCerts [][]byte) ([]byte, error) {
|
||||
// OID for signedData: 1.2.840.113549.1.7.2
|
||||
oidSignedData := []byte{0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02}
|
||||
// OID for data: 1.2.840.113549.1.7.1
|
||||
oidData := []byte{0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01}
|
||||
|
||||
// Build certificates [0] IMPLICIT SET OF Certificate
|
||||
var certsContent []byte
|
||||
for _, cert := range derCerts {
|
||||
certsContent = append(certsContent, cert...)
|
||||
}
|
||||
certsField := ASN1WrapImplicit(0, certsContent)
|
||||
|
||||
// Build encapContentInfo: SEQUENCE { OID data }
|
||||
encapContentInfo := ASN1WrapSequence(oidData)
|
||||
|
||||
// Build digestAlgorithms: SET {} (empty)
|
||||
digestAlgorithms := ASN1WrapSet(nil)
|
||||
|
||||
// Build signerInfos: SET {} (empty)
|
||||
signerInfos := ASN1WrapSet(nil)
|
||||
|
||||
// Version: INTEGER 1
|
||||
version := []byte{0x02, 0x01, 0x01}
|
||||
|
||||
// Build SignedData SEQUENCE
|
||||
var signedDataContent []byte
|
||||
signedDataContent = append(signedDataContent, version...)
|
||||
signedDataContent = append(signedDataContent, digestAlgorithms...)
|
||||
signedDataContent = append(signedDataContent, encapContentInfo...)
|
||||
signedDataContent = append(signedDataContent, certsField...)
|
||||
signedDataContent = append(signedDataContent, signerInfos...)
|
||||
signedData := ASN1WrapSequence(signedDataContent)
|
||||
|
||||
// Wrap in [0] EXPLICIT for ContentInfo.content
|
||||
contentField := ASN1WrapExplicit(0, signedData)
|
||||
|
||||
// Build ContentInfo SEQUENCE
|
||||
var contentInfoContent []byte
|
||||
contentInfoContent = append(contentInfoContent, oidSignedData...)
|
||||
contentInfoContent = append(contentInfoContent, contentField...)
|
||||
contentInfo := ASN1WrapSequence(contentInfoContent)
|
||||
|
||||
return contentInfo, nil
|
||||
}
|
||||
|
||||
// PEMToDERChain converts PEM-encoded certificates to a slice of DER-encoded certificates.
|
||||
func PEMToDERChain(pemData string) ([][]byte, error) {
|
||||
var derCerts [][]byte
|
||||
rest := []byte(pemData)
|
||||
for {
|
||||
var block *pem.Block
|
||||
block, rest = pem.Decode(rest)
|
||||
if block == nil {
|
||||
break
|
||||
}
|
||||
if block.Type == "CERTIFICATE" {
|
||||
derCerts = append(derCerts, block.Bytes)
|
||||
}
|
||||
}
|
||||
if len(derCerts) == 0 {
|
||||
return nil, fmt.Errorf("no certificates found in PEM data")
|
||||
}
|
||||
return derCerts, nil
|
||||
}
|
||||
|
||||
// ASN1WrapSequence wraps content in an ASN.1 SEQUENCE tag (0x30).
|
||||
func ASN1WrapSequence(content []byte) []byte {
|
||||
return ASN1Wrap(0x30, content)
|
||||
}
|
||||
|
||||
// ASN1WrapSet wraps content in an ASN.1 SET tag (0x31).
|
||||
func ASN1WrapSet(content []byte) []byte {
|
||||
return ASN1Wrap(0x31, content)
|
||||
}
|
||||
|
||||
// ASN1WrapExplicit wraps content in an ASN.1 context-specific EXPLICIT tag.
|
||||
func ASN1WrapExplicit(tag int, content []byte) []byte {
|
||||
return ASN1Wrap(byte(0xa0|tag), content)
|
||||
}
|
||||
|
||||
// ASN1WrapImplicit wraps content in an ASN.1 context-specific IMPLICIT CONSTRUCTED tag.
|
||||
func ASN1WrapImplicit(tag int, content []byte) []byte {
|
||||
return ASN1Wrap(byte(0xa0|tag), content)
|
||||
}
|
||||
|
||||
// ASN1Wrap wraps content with an ASN.1 tag and length.
|
||||
func ASN1Wrap(tag byte, content []byte) []byte {
|
||||
length := len(content)
|
||||
var result []byte
|
||||
result = append(result, tag)
|
||||
result = append(result, ASN1EncodeLength(length)...)
|
||||
result = append(result, content...)
|
||||
return result
|
||||
}
|
||||
|
||||
// ASN1EncodeLength encodes a length in ASN.1 DER format.
|
||||
func ASN1EncodeLength(length int) []byte {
|
||||
if length < 0x80 {
|
||||
return []byte{byte(length)}
|
||||
}
|
||||
// Long form
|
||||
var lengthBytes []byte
|
||||
l := length
|
||||
for l > 0 {
|
||||
lengthBytes = append([]byte{byte(l & 0xff)}, lengthBytes...)
|
||||
l >>= 8
|
||||
}
|
||||
return append([]byte{byte(0x80 | len(lengthBytes))}, lengthBytes...)
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package pkcs7
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"math/big"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func generateTestCertPEM(t *testing.T) string {
|
||||
t.Helper()
|
||||
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||
if err != nil {
|
||||
t.Fatalf("generate key: %v", err)
|
||||
}
|
||||
template := &x509.Certificate{
|
||||
SerialNumber: big.NewInt(1),
|
||||
Subject: pkix.Name{CommonName: "Test CA"},
|
||||
NotBefore: time.Now().Add(-1 * time.Hour),
|
||||
NotAfter: time.Now().Add(24 * time.Hour),
|
||||
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
||||
IsCA: true,
|
||||
BasicConstraintsValid: true,
|
||||
}
|
||||
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
|
||||
if err != nil {
|
||||
t.Fatalf("create certificate: %v", err)
|
||||
}
|
||||
return string(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER}))
|
||||
}
|
||||
|
||||
func TestBuildCertsOnlyPKCS7(t *testing.T) {
|
||||
dummyCert := []byte{0x30, 0x82, 0x01, 0x00}
|
||||
result, err := BuildCertsOnlyPKCS7([][]byte{dummyCert})
|
||||
if err != nil {
|
||||
t.Fatalf("BuildCertsOnlyPKCS7 failed: %v", err)
|
||||
}
|
||||
if len(result) == 0 {
|
||||
t.Error("expected non-empty PKCS#7 output")
|
||||
}
|
||||
if result[0] != 0x30 {
|
||||
t.Errorf("expected SEQUENCE tag (0x30), got 0x%02x", result[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCertsOnlyPKCS7_MultipleCerts(t *testing.T) {
|
||||
cert1 := []byte{0x30, 0x82, 0x01, 0x00}
|
||||
cert2 := []byte{0x30, 0x82, 0x02, 0x00}
|
||||
result, err := BuildCertsOnlyPKCS7([][]byte{cert1, cert2})
|
||||
if err != nil {
|
||||
t.Fatalf("BuildCertsOnlyPKCS7 failed: %v", err)
|
||||
}
|
||||
if len(result) == 0 {
|
||||
t.Error("expected non-empty PKCS#7 output")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPEMToDERChain_Success(t *testing.T) {
|
||||
pemData := generateTestCertPEM(t)
|
||||
certs, err := PEMToDERChain(pemData)
|
||||
if err != nil {
|
||||
t.Fatalf("PEMToDERChain failed: %v", err)
|
||||
}
|
||||
if len(certs) != 1 {
|
||||
t.Errorf("expected 1 cert, got %d", len(certs))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPEMToDERChain_NoCerts(t *testing.T) {
|
||||
_, err := PEMToDERChain("not a PEM")
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid PEM")
|
||||
}
|
||||
}
|
||||
|
||||
func TestASN1EncodeLength(t *testing.T) {
|
||||
tests := []struct {
|
||||
length int
|
||||
expected []byte
|
||||
}{
|
||||
{0, []byte{0x00}},
|
||||
{1, []byte{0x01}},
|
||||
{127, []byte{0x7f}},
|
||||
{128, []byte{0x81, 0x80}},
|
||||
{256, []byte{0x82, 0x01, 0x00}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
result := ASN1EncodeLength(tt.length)
|
||||
if len(result) != len(tt.expected) {
|
||||
t.Errorf("ASN1EncodeLength(%d): expected %d bytes, got %d", tt.length, len(tt.expected), len(result))
|
||||
continue
|
||||
}
|
||||
for i := range result {
|
||||
if result[i] != tt.expected[i] {
|
||||
t.Errorf("ASN1EncodeLength(%d): byte %d: expected 0x%02x, got 0x%02x", tt.length, i, tt.expected[i], result[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -734,3 +734,217 @@ func TestSchedulerLoopContextCancellation(t *testing.T) {
|
||||
|
||||
t.Logf("scheduler shut down gracefully on context cancellation")
|
||||
}
|
||||
|
||||
// mockDigestService is a mock implementation of DigestServicer for testing.
|
||||
type mockDigestService struct {
|
||||
mu sync.Mutex
|
||||
callCount int
|
||||
callTimes []time.Time
|
||||
slowDelay time.Duration
|
||||
shouldError bool
|
||||
}
|
||||
|
||||
func (m *mockDigestService) ProcessDigest(ctx context.Context) error {
|
||||
m.mu.Lock()
|
||||
m.callCount++
|
||||
m.callTimes = append(m.callTimes, time.Now())
|
||||
m.mu.Unlock()
|
||||
|
||||
if m.slowDelay > 0 {
|
||||
select {
|
||||
case <-time.After(m.slowDelay):
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
if m.shouldError {
|
||||
return context.Canceled
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestScheduler_DigestLoop_DoesNotRunImmediately verifies that the digest loop
|
||||
// does NOT run immediately on startup (unlike other loops). The digest is infrequent
|
||||
// (24h default) and shouldn't fire on every restart.
|
||||
func TestScheduler_DigestLoop_DoesNotRunImmediately(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
renewalMock := &mockRenewalService{}
|
||||
jobMock := &mockJobService{}
|
||||
agentMock := &mockAgentService{}
|
||||
notificationMock := &mockNotificationService{}
|
||||
networkMock := &mockNetworkScanService{}
|
||||
digestMock := &mockDigestService{}
|
||||
|
||||
sched := NewScheduler(renewalMock, jobMock, agentMock, notificationMock, networkMock, logger)
|
||||
sched.SetDigestService(digestMock)
|
||||
sched.SetDigestInterval(100 * time.Millisecond)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// Start the scheduler
|
||||
startedChan := sched.Start(ctx)
|
||||
<-startedChan
|
||||
|
||||
// Sleep briefly to allow any immediate execution
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
digestMock.mu.Lock()
|
||||
callCount := digestMock.callCount
|
||||
digestMock.mu.Unlock()
|
||||
|
||||
// Digest should NOT have been called immediately on startup
|
||||
if callCount > 0 {
|
||||
t.Errorf("digest should not run immediately on startup, expected 0 calls, got %d", callCount)
|
||||
}
|
||||
|
||||
t.Logf("digest loop correctly did not run immediately (calls: %d)", callCount)
|
||||
}
|
||||
|
||||
// TestScheduler_DigestLoop_RunsOnFirstTick verifies that the digest loop DOES run
|
||||
// after the first tick interval expires.
|
||||
func TestScheduler_DigestLoop_RunsOnFirstTick(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
renewalMock := &mockRenewalService{}
|
||||
jobMock := &mockJobService{}
|
||||
agentMock := &mockAgentService{}
|
||||
notificationMock := &mockNotificationService{}
|
||||
networkMock := &mockNetworkScanService{}
|
||||
digestMock := &mockDigestService{}
|
||||
|
||||
sched := NewScheduler(renewalMock, jobMock, agentMock, notificationMock, networkMock, logger)
|
||||
sched.SetDigestService(digestMock)
|
||||
sched.SetDigestInterval(100 * time.Millisecond)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// Start the scheduler
|
||||
startedChan := sched.Start(ctx)
|
||||
<-startedChan
|
||||
|
||||
// Sleep longer than the interval to allow the first tick to fire
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
|
||||
digestMock.mu.Lock()
|
||||
callCount := digestMock.callCount
|
||||
digestMock.mu.Unlock()
|
||||
|
||||
// Digest should have been called once after the first tick
|
||||
if callCount < 1 {
|
||||
t.Errorf("digest should run after first tick, expected at least 1 call, got %d", callCount)
|
||||
}
|
||||
|
||||
t.Logf("digest loop ran on first tick (calls: %d)", callCount)
|
||||
|
||||
cancel()
|
||||
|
||||
// Verify clean shutdown
|
||||
err := sched.WaitForCompletion(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("WaitForCompletion should succeed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduler_DigestLoop_WithIdempotencyGuard verifies that slow digest
|
||||
// processing prevents duplicate execution (idempotency guard).
|
||||
func TestScheduler_DigestLoop_WithIdempotencyGuard(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
renewalMock := &mockRenewalService{}
|
||||
jobMock := &mockJobService{}
|
||||
agentMock := &mockAgentService{}
|
||||
notificationMock := &mockNotificationService{}
|
||||
networkMock := &mockNetworkScanService{}
|
||||
digestMock := &mockDigestService{
|
||||
slowDelay: 150 * time.Millisecond, // Slower than tick interval
|
||||
}
|
||||
|
||||
sched := NewScheduler(renewalMock, jobMock, agentMock, notificationMock, networkMock, logger)
|
||||
sched.SetDigestService(digestMock)
|
||||
sched.SetDigestInterval(100 * time.Millisecond) // Tick every 100ms, but job takes 150ms
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
startedChan := sched.Start(ctx)
|
||||
<-startedChan
|
||||
|
||||
// Run for 400ms (enough for 4 ticks: 100ms, 200ms, 300ms, 400ms)
|
||||
time.Sleep(400 * time.Millisecond)
|
||||
|
||||
digestMock.mu.Lock()
|
||||
callCount := digestMock.callCount
|
||||
digestMock.mu.Unlock()
|
||||
|
||||
// With a 150ms slow job and 100ms tick interval, idempotency guard should
|
||||
// prevent overlapping execution. We should get 2-3 calls, not 4+.
|
||||
if callCount > 3 {
|
||||
t.Logf("WARNING: digest called %d times in 400ms with 100ms interval and 150ms job — guard may not be working", callCount)
|
||||
}
|
||||
|
||||
t.Logf("digest loop with idempotency guard: %d calls in 400ms (100ms interval, 150ms job)", callCount)
|
||||
|
||||
cancel()
|
||||
err := sched.WaitForCompletion(2 * time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("WaitForCompletion should succeed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduler_DigestLoop_SetDigestService tests that SetDigestService wires
|
||||
// the digest service correctly and starts the digest loop.
|
||||
func TestScheduler_DigestLoop_SetDigestService(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
renewalMock := &mockRenewalService{}
|
||||
jobMock := &mockJobService{}
|
||||
agentMock := &mockAgentService{}
|
||||
notificationMock := &mockNotificationService{}
|
||||
networkMock := &mockNetworkScanService{}
|
||||
|
||||
sched := NewScheduler(renewalMock, jobMock, agentMock, notificationMock, networkMock, logger)
|
||||
|
||||
// Initially, no digest service
|
||||
if sched.digestService != nil {
|
||||
t.Error("digestService should be nil initially")
|
||||
}
|
||||
|
||||
// Set digest service
|
||||
digestMock := &mockDigestService{}
|
||||
sched.SetDigestService(digestMock)
|
||||
|
||||
if sched.digestService == nil {
|
||||
t.Error("digestService should be set after SetDigestService")
|
||||
}
|
||||
|
||||
// Verify it's the same service we set
|
||||
if sched.digestService != digestMock {
|
||||
t.Error("digestService should be the mock we provided")
|
||||
}
|
||||
}
|
||||
|
||||
// TestScheduler_DigestLoop_SetDigestInterval tests that SetDigestInterval
|
||||
// configures the digest tick interval.
|
||||
func TestScheduler_DigestLoop_SetDigestInterval(t *testing.T) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
|
||||
renewalMock := &mockRenewalService{}
|
||||
jobMock := &mockJobService{}
|
||||
agentMock := &mockAgentService{}
|
||||
notificationMock := &mockNotificationService{}
|
||||
networkMock := &mockNetworkScanService{}
|
||||
|
||||
sched := NewScheduler(renewalMock, jobMock, agentMock, notificationMock, networkMock, logger)
|
||||
|
||||
// Default is 24h
|
||||
if sched.digestInterval != 24*time.Hour {
|
||||
t.Errorf("default digestInterval should be 24h, got %v", sched.digestInterval)
|
||||
}
|
||||
|
||||
// Set custom interval
|
||||
customInterval := 5 * time.Minute
|
||||
sched.SetDigestInterval(customInterval)
|
||||
|
||||
if sched.digestInterval != customInterval {
|
||||
t.Errorf("digestInterval should be %v after SetDigestInterval, got %v", customInterval, sched.digestInterval)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,364 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
)
|
||||
|
||||
// TestCertificateService_RevokeCertificate_RevocationSvcNil tests RevokeCertificateWithActor
|
||||
// when RevocationSvc is not configured (nil).
|
||||
func TestCertificateService_RevokeCertificate_RevocationSvcNil(t *testing.T) {
|
||||
// Setup: Create CertificateService WITHOUT calling SetRevocationSvc
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
// Create service WITHOUT RevocationSvc
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
// Note: NOT calling certService.SetRevocationSvc(...)
|
||||
|
||||
// Add a test certificate
|
||||
cert := &domain.ManagedCertificate{
|
||||
ID: "cert-1",
|
||||
CommonName: "example.com",
|
||||
IssuerID: "iss-local",
|
||||
Status: domain.CertificateStatusActive,
|
||||
}
|
||||
certRepo.AddCert(cert)
|
||||
|
||||
// Call RevokeCertificateWithActor with nil RevocationSvc
|
||||
err := certService.RevokeCertificateWithActor(context.Background(), "cert-1", "keyCompromise", "admin")
|
||||
|
||||
// Assert: Should return error, NOT panic
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
|
||||
// Verify error message indicates service not configured
|
||||
errMsg := err.Error()
|
||||
if errMsg != "revocation service not configured" {
|
||||
t.Errorf("expected error message 'revocation service not configured', got: %s", errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCertificateService_GenerateDERCRL_CAOpsSvcNil tests GenerateDERCRL
|
||||
// when CAOperationsSvc is not configured (nil).
|
||||
func TestCertificateService_GenerateDERCRL_CAOpsSvcNil(t *testing.T) {
|
||||
// Setup: Create CertificateService WITHOUT calling SetCAOperationsSvc
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
// Create service WITHOUT CAOperationsSvc
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
// Note: NOT calling certService.SetCAOperationsSvc(...)
|
||||
|
||||
// Call GenerateDERCRL with nil CAOperationsSvc
|
||||
_, err := certService.GenerateDERCRL("iss-local")
|
||||
|
||||
// Assert: Should return error, NOT panic
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
|
||||
// Verify error message indicates service not configured
|
||||
errMsg := err.Error()
|
||||
if errMsg != "CA operations service not configured" {
|
||||
t.Errorf("expected error message 'CA operations service not configured', got: %s", errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCertificateService_GetOCSPResponse_CAOpsSvcNil tests GetOCSPResponse
|
||||
// when CAOperationsSvc is not configured (nil).
|
||||
func TestCertificateService_GetOCSPResponse_CAOpsSvcNil(t *testing.T) {
|
||||
// Setup: Create CertificateService WITHOUT calling SetCAOperationsSvc
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
// Create service WITHOUT CAOperationsSvc
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
// Note: NOT calling certService.SetCAOperationsSvc(...)
|
||||
|
||||
// Call GetOCSPResponse with nil CAOperationsSvc
|
||||
_, err := certService.GetOCSPResponse("iss-local", "serial123")
|
||||
|
||||
// Assert: Should return error, NOT panic
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
|
||||
// Verify error message indicates service not configured
|
||||
errMsg := err.Error()
|
||||
if errMsg != "CA operations service not configured" {
|
||||
t.Errorf("expected error message 'CA operations service not configured', got: %s", errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCertificateService_GetRevokedCertificates_RevocationSvcNil tests GetRevokedCertificates
|
||||
// when RevocationSvc is not configured (nil).
|
||||
func TestCertificateService_GetRevokedCertificates_RevocationSvcNil(t *testing.T) {
|
||||
// Setup: Create CertificateService WITHOUT calling SetRevocationSvc
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
// Create service WITHOUT RevocationSvc
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
// Note: NOT calling certService.SetRevocationSvc(...)
|
||||
|
||||
// Call GetRevokedCertificates with nil RevocationSvc
|
||||
_, err := certService.GetRevokedCertificates()
|
||||
|
||||
// Assert: Should return error, NOT panic
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
|
||||
// Verify error message indicates service not configured
|
||||
errMsg := err.Error()
|
||||
if errMsg != "revocation service not configured" {
|
||||
t.Errorf("expected error message 'revocation service not configured', got: %s", errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCertificateService_GetCertificateDeployments_Success tests GetCertificateDeployments
|
||||
// when TargetRepo is properly configured.
|
||||
func TestCertificateService_GetCertificateDeployments_Success(t *testing.T) {
|
||||
// Setup: Create CertificateService with properly configured TargetRepo
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
targetRepo := &mockTargetRepo{Targets: make(map[string]*domain.DeploymentTarget)}
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
certService.SetTargetRepo(targetRepo)
|
||||
|
||||
// Add a test certificate
|
||||
cert := &domain.ManagedCertificate{
|
||||
ID: "cert-1",
|
||||
CommonName: "example.com",
|
||||
IssuerID: "iss-local",
|
||||
Status: domain.CertificateStatusActive,
|
||||
}
|
||||
certRepo.AddCert(cert)
|
||||
|
||||
// Add deployment targets
|
||||
target1 := &domain.DeploymentTarget{
|
||||
ID: "t-1",
|
||||
Name: "nginx-prod",
|
||||
Type: domain.TargetTypeNGINX,
|
||||
}
|
||||
target2 := &domain.DeploymentTarget{
|
||||
ID: "t-2",
|
||||
Name: "apache-prod",
|
||||
Type: domain.TargetTypeApache,
|
||||
}
|
||||
targetRepo.AddTarget(target1)
|
||||
targetRepo.AddTarget(target2)
|
||||
|
||||
// Call GetCertificateDeployments
|
||||
deployments, err := certService.GetCertificateDeployments("cert-1")
|
||||
|
||||
// Assert: Should return deployment list successfully
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
// Verify deployments are returned (note: mock ListByCertificate returns all targets)
|
||||
if len(deployments) == 0 {
|
||||
t.Error("expected deployment list to be non-empty")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCertificateService_GetCertificateDeployments_RepositoryError tests GetCertificateDeployments
|
||||
// when TargetRepo returns an error.
|
||||
func TestCertificateService_GetCertificateDeployments_RepositoryError(t *testing.T) {
|
||||
// Setup: Create CertificateService with TargetRepo configured to return error
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
targetRepo := &mockTargetRepo{
|
||||
Targets: make(map[string]*domain.DeploymentTarget),
|
||||
ListByCertErr: errNotFound,
|
||||
}
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
certService.SetTargetRepo(targetRepo)
|
||||
|
||||
// Add a test certificate
|
||||
cert := &domain.ManagedCertificate{
|
||||
ID: "cert-1",
|
||||
CommonName: "example.com",
|
||||
IssuerID: "iss-local",
|
||||
Status: domain.CertificateStatusActive,
|
||||
}
|
||||
certRepo.AddCert(cert)
|
||||
|
||||
// Call GetCertificateDeployments with repo error
|
||||
_, err := certService.GetCertificateDeployments("cert-1")
|
||||
|
||||
// Assert: Should return error, NOT panic
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
|
||||
// Verify error indicates repo failure
|
||||
if err.Error() != "failed to list deployment targets: not found" {
|
||||
t.Errorf("expected repo error message, got: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TestCertificateService_GetCertificateDeployments_CertNotFound tests GetCertificateDeployments
|
||||
// when the certificate doesn't exist.
|
||||
func TestCertificateService_GetCertificateDeployments_CertNotFound(t *testing.T) {
|
||||
// Setup: Create CertificateService with empty cert repo
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
targetRepo := &mockTargetRepo{Targets: make(map[string]*domain.DeploymentTarget)}
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
certService.SetTargetRepo(targetRepo)
|
||||
|
||||
// Call GetCertificateDeployments with nonexistent certificate
|
||||
_, err := certService.GetCertificateDeployments("nonexistent-cert")
|
||||
|
||||
// Assert: Should return error
|
||||
if err == nil {
|
||||
t.Fatal("expected error for nonexistent certificate, got nil")
|
||||
}
|
||||
|
||||
if err.Error() != "certificate not found: not found" {
|
||||
t.Errorf("expected certificate not found error, got: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TestCertificateService_GetCertificateDeployments_NilTargetRepo tests GetCertificateDeployments
|
||||
// when TargetRepo is nil (empty graceful handling).
|
||||
func TestCertificateService_GetCertificateDeployments_NilTargetRepo(t *testing.T) {
|
||||
// Setup: Create CertificateService WITHOUT TargetRepo
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
// Note: NOT calling certService.SetTargetRepo(...)
|
||||
|
||||
// Add a test certificate
|
||||
cert := &domain.ManagedCertificate{
|
||||
ID: "cert-1",
|
||||
CommonName: "example.com",
|
||||
IssuerID: "iss-local",
|
||||
Status: domain.CertificateStatusActive,
|
||||
}
|
||||
certRepo.AddCert(cert)
|
||||
|
||||
// Call GetCertificateDeployments with nil TargetRepo
|
||||
deployments, err := certService.GetCertificateDeployments("cert-1")
|
||||
|
||||
// Assert: Should return empty list gracefully (not panic)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got: %v", err)
|
||||
}
|
||||
|
||||
if len(deployments) != 0 {
|
||||
t.Errorf("expected empty deployment list, got %d deployments", len(deployments))
|
||||
}
|
||||
}
|
||||
|
||||
// TestCertificateService_Multiple_NilSafetyChecks tests multiple nil-safety operations in sequence.
|
||||
func TestCertificateService_Multiple_NilSafetyChecks(t *testing.T) {
|
||||
// Setup: Create CertificateService with partial configuration
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
policyRepo := newMockPolicyRepository()
|
||||
|
||||
auditService := NewAuditService(auditRepo)
|
||||
policyService := NewPolicyService(policyRepo, auditService)
|
||||
|
||||
certService := NewCertificateService(certRepo, policyService, auditService)
|
||||
// Only set RevocationSvc, leave CAOperationsSvc nil
|
||||
revSvc := NewRevocationSvc(certRepo, newMockRevocationRepository(), auditService)
|
||||
certService.SetRevocationSvc(revSvc)
|
||||
|
||||
// Add a test certificate
|
||||
cert := &domain.ManagedCertificate{
|
||||
ID: "cert-1",
|
||||
CommonName: "example.com",
|
||||
IssuerID: "iss-local",
|
||||
Status: domain.CertificateStatusActive,
|
||||
ExpiresAt: time.Now().AddDate(0, 6, 0),
|
||||
}
|
||||
certRepo.AddCert(cert)
|
||||
|
||||
// Add a certificate version
|
||||
version := &domain.CertificateVersion{
|
||||
ID: "ver-1",
|
||||
CertificateID: "cert-1",
|
||||
SerialNumber: "ABC123",
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().AddDate(1, 0, 0),
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
certRepo.Versions["cert-1"] = []*domain.CertificateVersion{version}
|
||||
|
||||
// Set up issuer registry for revocation
|
||||
registry := NewIssuerRegistry(slog.Default())
|
||||
registry.Set("iss-local", &mockIssuerConnector{})
|
||||
revSvc.SetIssuerRegistry(registry)
|
||||
|
||||
// Test 1: RevokeCertificateWithActor should succeed (RevocationSvc is set)
|
||||
errRevoke := certService.RevokeCertificateWithActor(context.Background(), "cert-1", "keyCompromise", "admin")
|
||||
if errRevoke != nil {
|
||||
t.Fatalf("RevokeCertificateWithActor failed unexpectedly: %v", errRevoke)
|
||||
}
|
||||
|
||||
// Test 2: GenerateDERCRL should fail gracefully (CAOperationsSvc is nil)
|
||||
_, errCRL := certService.GenerateDERCRL("iss-local")
|
||||
if errCRL == nil {
|
||||
t.Fatal("GenerateDERCRL expected error, got nil")
|
||||
}
|
||||
|
||||
// Test 3: GetOCSPResponse should fail gracefully (CAOperationsSvc is nil)
|
||||
_, errOCSP := certService.GetOCSPResponse("iss-local", "ABC123")
|
||||
if errOCSP == nil {
|
||||
t.Fatal("GetOCSPResponse expected error, got nil")
|
||||
}
|
||||
|
||||
// Assert that errors are for correct reasons
|
||||
if errCRL.Error() != "CA operations service not configured" {
|
||||
t.Errorf("CRL error should be about CA ops service, got: %s", errCRL.Error())
|
||||
}
|
||||
if errOCSP.Error() != "CA operations service not configured" {
|
||||
t.Errorf("OCSP error should be about CA ops service, got: %s", errOCSP.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsSensitiveConfigKey_KnownSensitiveKeys(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
expected bool
|
||||
}{
|
||||
{"api_key", "api_key", true},
|
||||
{"password", "password", true},
|
||||
{"secret", "secret", true},
|
||||
{"token", "token", true},
|
||||
{"hmac", "hmac", true},
|
||||
{"private_key", "private_key", true},
|
||||
{"credentials", "credentials", true},
|
||||
{"winrm_password", "winrm_password", true},
|
||||
{"keystore_password", "keystore_password", true},
|
||||
// Variations with different casing
|
||||
{"API_KEY", "API_KEY", true},
|
||||
{"Password", "Password", true},
|
||||
{"SECRET", "SECRET", true},
|
||||
{"PrivateKey", "PrivateKey", true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := isSensitiveConfigKey(tt.key)
|
||||
if got != tt.expected {
|
||||
t.Errorf("isSensitiveConfigKey(%q) = %v, want %v", tt.key, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSensitiveConfigKey_NonSensitiveKeys(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
}{
|
||||
{"url", "url"},
|
||||
{"host", "host"},
|
||||
{"port", "port"},
|
||||
{"region", "region"},
|
||||
{"ca_pool", "ca_pool"},
|
||||
{"namespace", "namespace"},
|
||||
{"cert_path", "cert_path"},
|
||||
{"base_url", "base_url"},
|
||||
{"org_id", "org_id"},
|
||||
{"product_type", "product_type"},
|
||||
{"email", "email"},
|
||||
{"enabled", "enabled"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := isSensitiveConfigKey(tt.key)
|
||||
if got != false {
|
||||
t.Errorf("isSensitiveConfigKey(%q) = %v, want false", tt.key, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSensitiveConfigKey_CaseInsensitivity(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
key string
|
||||
}{
|
||||
{"api_key uppercase", "API_KEY"},
|
||||
{"api_key mixed", "Api_Key"},
|
||||
{"password uppercase", "PASSWORD"},
|
||||
{"password mixed", "PassWord"},
|
||||
{"secret uppercase", "SECRET"},
|
||||
{"token mixed", "ToKeN"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := isSensitiveConfigKey(tt.key)
|
||||
if got != true {
|
||||
t.Errorf("isSensitiveConfigKey(%q) = %v, want true (case-insensitive)", tt.key, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactConfigJSON_HidesSensitiveFields(t *testing.T) {
|
||||
input := json.RawMessage(`{
|
||||
"api_key": "secret-key-123",
|
||||
"password": "my-password",
|
||||
"token": "bearer-token",
|
||||
"host": "example.com"
|
||||
}`)
|
||||
|
||||
result := redactConfigJSON(input)
|
||||
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(result, &m); err != nil {
|
||||
t.Fatalf("failed to unmarshal result: %v", err)
|
||||
}
|
||||
|
||||
// Check sensitive fields are redacted
|
||||
if m["api_key"] != "********" {
|
||||
t.Errorf("api_key = %v, want ********", m["api_key"])
|
||||
}
|
||||
if m["password"] != "********" {
|
||||
t.Errorf("password = %v, want ********", m["password"])
|
||||
}
|
||||
if m["token"] != "********" {
|
||||
t.Errorf("token = %v, want ********", m["token"])
|
||||
}
|
||||
|
||||
// Check non-sensitive field is preserved
|
||||
if m["host"] != "example.com" {
|
||||
t.Errorf("host = %v, want example.com", m["host"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactConfigJSON_PassesThroughNonSensitive(t *testing.T) {
|
||||
input := json.RawMessage(`{
|
||||
"url": "https://api.example.com",
|
||||
"port": 443,
|
||||
"region": "us-east-1"
|
||||
}`)
|
||||
|
||||
result := redactConfigJSON(input)
|
||||
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(result, &m); err != nil {
|
||||
t.Fatalf("failed to unmarshal result: %v", err)
|
||||
}
|
||||
|
||||
// All fields should be preserved as-is
|
||||
if m["url"] != "https://api.example.com" {
|
||||
t.Errorf("url = %v, want https://api.example.com", m["url"])
|
||||
}
|
||||
if m["port"] != float64(443) {
|
||||
t.Errorf("port = %v, want 443", m["port"])
|
||||
}
|
||||
if m["region"] != "us-east-1" {
|
||||
t.Errorf("region = %v, want us-east-1", m["region"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactConfigJSON_EmptyConfig(t *testing.T) {
|
||||
input := json.RawMessage(`{}`)
|
||||
|
||||
result := redactConfigJSON(input)
|
||||
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(result, &m); err != nil {
|
||||
t.Fatalf("failed to unmarshal result: %v", err)
|
||||
}
|
||||
|
||||
if len(m) != 0 {
|
||||
t.Errorf("empty config should remain empty, got %v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactConfigJSON_EmptyStringPassword(t *testing.T) {
|
||||
input := json.RawMessage(`{
|
||||
"password": "",
|
||||
"token": "my-token",
|
||||
"host": "example.com"
|
||||
}`)
|
||||
|
||||
result := redactConfigJSON(input)
|
||||
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(result, &m); err != nil {
|
||||
t.Fatalf("failed to unmarshal result: %v", err)
|
||||
}
|
||||
|
||||
// Empty password should be left as-is (empty string)
|
||||
if m["password"] != "" {
|
||||
t.Errorf("empty password = %v, want empty string", m["password"])
|
||||
}
|
||||
|
||||
// Non-empty sensitive field should be redacted
|
||||
if m["token"] != "********" {
|
||||
t.Errorf("token = %v, want ********", m["token"])
|
||||
}
|
||||
|
||||
// Non-sensitive field preserved
|
||||
if m["host"] != "example.com" {
|
||||
t.Errorf("host = %v, want example.com", m["host"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactConfigJSON_MalformedJSON(t *testing.T) {
|
||||
// Malformed JSON should be returned as-is
|
||||
input := json.RawMessage(`not valid json`)
|
||||
|
||||
result := redactConfigJSON(input)
|
||||
|
||||
// Should return the input unchanged when it can't be parsed as object
|
||||
if string(result) != string(input) {
|
||||
t.Errorf("malformed JSON not returned as-is: got %s, want %s", string(result), string(input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactConfigJSON_JSONArray(t *testing.T) {
|
||||
// Array of objects should be returned as-is (not parsed as object)
|
||||
input := json.RawMessage(`[{"key": "value"}]`)
|
||||
|
||||
result := redactConfigJSON(input)
|
||||
|
||||
// Should return the input unchanged since it's an array, not an object
|
||||
if string(result) != string(input) {
|
||||
t.Errorf("JSON array not returned as-is: got %s, want %s", string(result), string(input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactConfigJSON_NestedSensitiveFields(t *testing.T) {
|
||||
input := json.RawMessage(`{
|
||||
"outer_password": "should-be-redacted",
|
||||
"config": {"inner_key": "value"}
|
||||
}`)
|
||||
|
||||
result := redactConfigJSON(input)
|
||||
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(result, &m); err != nil {
|
||||
t.Fatalf("failed to unmarshal result: %v", err)
|
||||
}
|
||||
|
||||
// Outer level sensitive field is redacted
|
||||
if m["outer_password"] != "********" {
|
||||
t.Errorf("outer_password = %v, want ********", m["outer_password"])
|
||||
}
|
||||
|
||||
// Note: nested fields are NOT redacted (function only processes top-level)
|
||||
// This is the current behavior based on the implementation
|
||||
if nested, ok := m["config"].(map[string]interface{}); ok {
|
||||
if nested["inner_key"] != "value" {
|
||||
t.Errorf("nested inner_key = %v, want value (nested not processed)", nested["inner_key"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedactConfigJSON_NonStringValues(t *testing.T) {
|
||||
input := json.RawMessage(`{
|
||||
"password": 123,
|
||||
"token": null,
|
||||
"secret": true,
|
||||
"api_key": ["list", "of", "values"]
|
||||
}`)
|
||||
|
||||
result := redactConfigJSON(input)
|
||||
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal(result, &m); err != nil {
|
||||
t.Fatalf("failed to unmarshal result: %v", err)
|
||||
}
|
||||
|
||||
// Non-string values should be left as-is (not redacted)
|
||||
if m["password"] != float64(123) {
|
||||
t.Errorf("password (number) = %v, want 123 (unchanged)", m["password"])
|
||||
}
|
||||
if m["token"] != nil {
|
||||
t.Errorf("token (null) = %v, want nil (unchanged)", m["token"])
|
||||
}
|
||||
if m["secret"] != true {
|
||||
t.Errorf("secret (bool) = %v, want true (unchanged)", m["secret"])
|
||||
}
|
||||
if _, ok := m["api_key"].([]interface{}); !ok {
|
||||
t.Errorf("api_key (array) should remain as array, got %T", m["api_key"])
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,7 @@ var validIssuerTypes = map[domain.IssuerType]bool{
|
||||
domain.IssuerTypeDigiCert: true,
|
||||
domain.IssuerTypeSectigo: true,
|
||||
domain.IssuerTypeGoogleCAS: true,
|
||||
domain.IssuerTypeAWSACMPCA: true,
|
||||
}
|
||||
|
||||
// isValidIssuerType checks if a type string is a known issuer type.
|
||||
@@ -482,6 +483,26 @@ func (s *IssuerService) buildEnvVarSeeds(cfg *config.Config) []*domain.Issuer {
|
||||
})
|
||||
}
|
||||
|
||||
// Conditional: AWS ACM PCA
|
||||
if cfg.AWSACMPCA.CAArn != "" {
|
||||
seeds = append(seeds, &domain.Issuer{
|
||||
ID: "iss-awsacmpca",
|
||||
Name: "AWS ACM Private CA",
|
||||
Type: domain.IssuerTypeAWSACMPCA,
|
||||
Config: mustJSON(map[string]interface{}{
|
||||
"region": cfg.AWSACMPCA.Region,
|
||||
"ca_arn": cfg.AWSACMPCA.CAArn,
|
||||
"signing_algorithm": cfg.AWSACMPCA.SigningAlgorithm,
|
||||
"validity_days": cfg.AWSACMPCA.ValidityDays,
|
||||
"template_arn": cfg.AWSACMPCA.TemplateArn,
|
||||
}),
|
||||
Enabled: true,
|
||||
Source: "env",
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
})
|
||||
}
|
||||
|
||||
return seeds
|
||||
}
|
||||
|
||||
@@ -536,6 +557,11 @@ func (s *IssuerService) CreateIssuer(iss domain.Issuer) (*domain.Issuer, error)
|
||||
if iss.Source == "" {
|
||||
iss.Source = "database"
|
||||
}
|
||||
// GUI-created issuers should be enabled by default.
|
||||
// Go's bool zero value is false, which overrides the DB default when explicitly inserted.
|
||||
if iss.Source == "database" && !iss.Enabled {
|
||||
iss.Enabled = true
|
||||
}
|
||||
|
||||
// Encrypt config
|
||||
if len(iss.Config) > 0 {
|
||||
|
||||
@@ -0,0 +1,367 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/config"
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
)
|
||||
|
||||
// TestBuildEnvVarSeeds_ACMEConfig tests env var seeding with ACME configuration
|
||||
func TestBuildEnvVarSeeds_ACMEConfig(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
ACME: config.ACMEConfig{
|
||||
DirectoryURL: "https://acme.example.com/directory",
|
||||
Email: "admin@example.com",
|
||||
ChallengeType: "http-01",
|
||||
Insecure: false,
|
||||
},
|
||||
CA: config.CAConfig{},
|
||||
}
|
||||
|
||||
repo := newMockIssuerRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditService := NewAuditService(auditRepo)
|
||||
|
||||
service := NewIssuerService(repo, auditService, NewIssuerRegistry(slog.Default()), nil, slog.Default())
|
||||
|
||||
// Call buildEnvVarSeeds (unexported method, but testable from same package)
|
||||
seeds := service.buildEnvVarSeeds(cfg)
|
||||
|
||||
// Should have at least Local CA and 2 ACME seeds
|
||||
if len(seeds) < 3 {
|
||||
t.Fatalf("expected at least 3 seeds (Local CA + 2 ACME), got %d", len(seeds))
|
||||
}
|
||||
|
||||
// Find ACME seeds
|
||||
var acmeSeeds []*domain.Issuer
|
||||
for _, seed := range seeds {
|
||||
if seed.Type == domain.IssuerTypeACME {
|
||||
acmeSeeds = append(acmeSeeds, seed)
|
||||
}
|
||||
}
|
||||
|
||||
if len(acmeSeeds) != 2 {
|
||||
t.Fatalf("expected 2 ACME seeds (staging + prod), got %d", len(acmeSeeds))
|
||||
}
|
||||
|
||||
// Verify ACME config is present in seeds
|
||||
for _, acmeSeed := range acmeSeeds {
|
||||
var cfg map[string]interface{}
|
||||
if err := json.Unmarshal(acmeSeed.Config, &cfg); err != nil {
|
||||
t.Fatalf("failed to unmarshal seed config: %v", err)
|
||||
}
|
||||
|
||||
if cfg["directory_url"] != "https://acme.example.com/directory" {
|
||||
t.Errorf("expected directory_url in config, got: %v", cfg["directory_url"])
|
||||
}
|
||||
if cfg["email"] != "admin@example.com" {
|
||||
t.Errorf("expected email in config, got: %v", cfg["email"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildEnvVarSeeds_VaultConfig tests env var seeding with Vault configuration
|
||||
func TestBuildEnvVarSeeds_VaultConfig(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
ACME: config.ACMEConfig{},
|
||||
CA: config.CAConfig{},
|
||||
Vault: config.VaultConfig{
|
||||
Addr: "https://vault.example.com:8200",
|
||||
Token: "hvs.test-token",
|
||||
Mount: "pki",
|
||||
Role: "default",
|
||||
TTL: "8760h",
|
||||
},
|
||||
}
|
||||
|
||||
repo := newMockIssuerRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditService := NewAuditService(auditRepo)
|
||||
|
||||
service := NewIssuerService(repo, auditService, NewIssuerRegistry(slog.Default()), nil, slog.Default())
|
||||
|
||||
seeds := service.buildEnvVarSeeds(cfg)
|
||||
|
||||
// Find Vault seed
|
||||
var vaultSeed *domain.Issuer
|
||||
for _, seed := range seeds {
|
||||
if seed.Type == domain.IssuerTypeVault {
|
||||
vaultSeed = seed
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if vaultSeed == nil {
|
||||
t.Fatal("expected Vault seed in buildEnvVarSeeds")
|
||||
}
|
||||
|
||||
if vaultSeed.ID != "iss-vault" {
|
||||
t.Errorf("expected issuer ID 'iss-vault', got %s", vaultSeed.ID)
|
||||
}
|
||||
|
||||
if vaultSeed.Name != "Vault PKI" {
|
||||
t.Errorf("expected issuer Name 'Vault PKI', got %s", vaultSeed.Name)
|
||||
}
|
||||
|
||||
// Verify Vault config
|
||||
var vaultCfg map[string]interface{}
|
||||
if err := json.Unmarshal(vaultSeed.Config, &vaultCfg); err != nil {
|
||||
t.Fatalf("failed to unmarshal Vault config: %v", err)
|
||||
}
|
||||
|
||||
if vaultCfg["addr"] != "https://vault.example.com:8200" {
|
||||
t.Errorf("expected vault addr in config, got: %v", vaultCfg["addr"])
|
||||
}
|
||||
if vaultCfg["token"] != "hvs.test-token" {
|
||||
t.Errorf("expected vault token in config, got: %v", vaultCfg["token"])
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildEnvVarSeeds_NoConfig tests env var seeding with empty configuration
|
||||
func TestBuildEnvVarSeeds_NoConfig(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
ACME: config.ACMEConfig{},
|
||||
CA: config.CAConfig{},
|
||||
Vault: config.VaultConfig{},
|
||||
Sectigo: config.SectigoConfig{},
|
||||
GoogleCAS: config.GoogleCASConfig{},
|
||||
AWSACMPCA: config.AWSACMPCAConfig{},
|
||||
}
|
||||
|
||||
repo := newMockIssuerRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditService := NewAuditService(auditRepo)
|
||||
|
||||
service := NewIssuerService(repo, auditService, NewIssuerRegistry(slog.Default()), nil, slog.Default())
|
||||
|
||||
seeds := service.buildEnvVarSeeds(cfg)
|
||||
|
||||
// Should only have Local CA and basic ACME (always seeded)
|
||||
if len(seeds) < 2 {
|
||||
t.Fatalf("expected at least 2 seeds (Local CA + ACME), got %d", len(seeds))
|
||||
}
|
||||
|
||||
// Verify no Vault, Sectigo, or GoogleCAS seeds
|
||||
for _, seed := range seeds {
|
||||
if seed.Type == domain.IssuerTypeVault {
|
||||
t.Error("unexpected Vault seed in empty config")
|
||||
}
|
||||
if seed.Type == domain.IssuerTypeSectigo {
|
||||
t.Error("unexpected Sectigo seed in empty config")
|
||||
}
|
||||
if seed.Type == domain.IssuerTypeGoogleCAS {
|
||||
t.Error("unexpected GoogleCAS seed in empty config")
|
||||
}
|
||||
if seed.Type == domain.IssuerTypeAWSACMPCA {
|
||||
t.Error("unexpected AWS ACM PCA seed in empty config")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildEnvVarSeeds_MultipleConfigs tests env var seeding with multiple issuers configured
|
||||
func TestBuildEnvVarSeeds_MultipleConfigs(t *testing.T) {
|
||||
cfg := &config.Config{
|
||||
ACME: config.ACMEConfig{
|
||||
DirectoryURL: "https://acme.example.com/directory",
|
||||
},
|
||||
CA: config.CAConfig{},
|
||||
Vault: config.VaultConfig{
|
||||
Addr: "https://vault:8200",
|
||||
},
|
||||
DigiCert: config.DigiCertConfig{
|
||||
APIKey: "test-api-key",
|
||||
},
|
||||
Sectigo: config.SectigoConfig{
|
||||
CustomerURI: "https://sectigo.com",
|
||||
Login: "admin",
|
||||
Password: "pass",
|
||||
},
|
||||
}
|
||||
|
||||
repo := newMockIssuerRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditService := NewAuditService(auditRepo)
|
||||
|
||||
service := NewIssuerService(repo, auditService, NewIssuerRegistry(slog.Default()), nil, slog.Default())
|
||||
|
||||
seeds := service.buildEnvVarSeeds(cfg)
|
||||
|
||||
// Count seeds by type
|
||||
typeCount := make(map[domain.IssuerType]int)
|
||||
for _, seed := range seeds {
|
||||
typeCount[seed.Type]++
|
||||
}
|
||||
|
||||
// Verify expected seeds are present
|
||||
if typeCount[domain.IssuerTypeGenericCA] < 1 {
|
||||
t.Error("expected Local CA seed")
|
||||
}
|
||||
if typeCount[domain.IssuerTypeACME] < 1 {
|
||||
t.Error("expected ACME seed")
|
||||
}
|
||||
if typeCount[domain.IssuerTypeVault] != 1 {
|
||||
t.Error("expected exactly 1 Vault seed")
|
||||
}
|
||||
if typeCount[domain.IssuerTypeDigiCert] != 1 {
|
||||
t.Error("expected exactly 1 DigiCert seed")
|
||||
}
|
||||
if typeCount[domain.IssuerTypeSectigo] != 1 {
|
||||
t.Error("expected exactly 1 Sectigo seed")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSeedFromEnvVars_Empty tests SeedFromEnvVars when database is empty
|
||||
func TestSeedFromEnvVars_Empty(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
cfg := &config.Config{
|
||||
ACME: config.ACMEConfig{
|
||||
DirectoryURL: "https://acme.example.com/directory",
|
||||
},
|
||||
CA: config.CAConfig{},
|
||||
Vault: config.VaultConfig{
|
||||
Addr: "https://vault:8200",
|
||||
},
|
||||
}
|
||||
|
||||
repo := newMockIssuerRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditService := NewAuditService(auditRepo)
|
||||
|
||||
service := NewIssuerService(repo, auditService, NewIssuerRegistry(slog.Default()), nil, slog.Default())
|
||||
|
||||
// Call SeedFromEnvVars on empty repo
|
||||
service.SeedFromEnvVars(ctx, cfg)
|
||||
|
||||
// Verify issuers were created
|
||||
issuers, err := repo.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to list issuers: %v", err)
|
||||
}
|
||||
|
||||
if len(issuers) == 0 {
|
||||
t.Fatal("expected issuers to be seeded")
|
||||
}
|
||||
|
||||
// Verify seeded issuers have source="env"
|
||||
for _, iss := range issuers {
|
||||
if iss.Source != "env" {
|
||||
t.Errorf("expected source 'env', got %s", iss.Source)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSeedFromEnvVars_AlreadyExists tests SeedFromEnvVars skips seeding when issuers exist
|
||||
func TestSeedFromEnvVars_AlreadyExists(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
cfg := &config.Config{
|
||||
ACME: config.ACMEConfig{
|
||||
DirectoryURL: "https://acme.example.com/directory",
|
||||
},
|
||||
CA: config.CAConfig{},
|
||||
}
|
||||
|
||||
repo := newMockIssuerRepository()
|
||||
|
||||
// Pre-populate with an issuer
|
||||
existing := &domain.Issuer{
|
||||
ID: "iss-existing",
|
||||
Name: "Existing Issuer",
|
||||
Type: domain.IssuerTypeACME,
|
||||
Source: "database",
|
||||
}
|
||||
repo.AddIssuer(existing)
|
||||
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditService := NewAuditService(auditRepo)
|
||||
|
||||
service := NewIssuerService(repo, auditService, NewIssuerRegistry(slog.Default()), nil, slog.Default())
|
||||
|
||||
// Get count before seeding
|
||||
beforeSeeding, _ := repo.List(ctx)
|
||||
countBefore := len(beforeSeeding)
|
||||
|
||||
// Call SeedFromEnvVars
|
||||
service.SeedFromEnvVars(ctx, cfg)
|
||||
|
||||
// Verify no new issuers were added
|
||||
afterSeeding, _ := repo.List(ctx)
|
||||
countAfter := len(afterSeeding)
|
||||
|
||||
if countAfter != countBefore {
|
||||
t.Errorf("expected %d issuers, got %d (seeding should have been skipped)", countBefore, countAfter)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBuildRegistry_Success tests BuildRegistry loads and rebuilds the registry
|
||||
func TestBuildRegistry_Success(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Create test issuers
|
||||
acmeIssuer := &domain.Issuer{
|
||||
ID: "iss-acme",
|
||||
Name: "ACME",
|
||||
Type: domain.IssuerTypeACME,
|
||||
Enabled: true,
|
||||
Source: "database",
|
||||
Config: json.RawMessage(`{"directory_url":"https://acme.example.com"}`),
|
||||
}
|
||||
|
||||
disabledIssuer := &domain.Issuer{
|
||||
ID: "iss-disabled",
|
||||
Name: "Disabled",
|
||||
Type: domain.IssuerTypeGenericCA,
|
||||
Enabled: false,
|
||||
Source: "database",
|
||||
}
|
||||
|
||||
repo := newMockIssuerRepository()
|
||||
repo.AddIssuer(acmeIssuer)
|
||||
repo.AddIssuer(disabledIssuer)
|
||||
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditService := NewAuditService(auditRepo)
|
||||
|
||||
registry := NewIssuerRegistry(slog.Default())
|
||||
service := NewIssuerService(repo, auditService, registry, nil, slog.Default())
|
||||
|
||||
// Call BuildRegistry
|
||||
err := service.BuildRegistry(ctx)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("BuildRegistry failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify registry was populated (should at least have the enabled issuer)
|
||||
// Note: ACME connector creation will fail in this test due to missing config,
|
||||
// but the test verifies the registry rebuild logic itself
|
||||
}
|
||||
|
||||
// TestBuildRegistry_EmptyDatabase tests BuildRegistry with no issuers
|
||||
func TestBuildRegistry_EmptyDatabase(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
repo := newMockIssuerRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditService := NewAuditService(auditRepo)
|
||||
|
||||
registry := NewIssuerRegistry(slog.Default())
|
||||
service := NewIssuerService(repo, auditService, registry, nil, slog.Default())
|
||||
|
||||
// Call BuildRegistry on empty database
|
||||
err := service.BuildRegistry(ctx)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("BuildRegistry failed: %v", err)
|
||||
}
|
||||
|
||||
// Registry should be empty (no errors for empty database)
|
||||
if registry.Len() != 0 {
|
||||
t.Errorf("expected empty registry, got size %d", registry.Len())
|
||||
}
|
||||
}
|
||||
@@ -136,8 +136,17 @@ func (s *RenewalService) CheckExpiringCertificates(ctx context.Context) error {
|
||||
policyCache := make(map[string]*domain.RenewalPolicy)
|
||||
|
||||
for _, cert := range expiring {
|
||||
// Skip if already renewing or archived
|
||||
if cert.Status == domain.CertificateStatusRenewalInProgress || cert.Status == domain.CertificateStatusArchived {
|
||||
// Skip certs in terminal or non-renewable states:
|
||||
// - RenewalInProgress: already being renewed
|
||||
// - Archived: no longer managed
|
||||
// - Revoked: intentionally revoked, should not be auto-renewed
|
||||
// - Failed: requires manual intervention (the failure cause hasn't been resolved)
|
||||
// - Expired: requires manual review (why did it expire without renewal?)
|
||||
if cert.Status == domain.CertificateStatusRenewalInProgress ||
|
||||
cert.Status == domain.CertificateStatusArchived ||
|
||||
cert.Status == domain.CertificateStatusRevoked ||
|
||||
cert.Status == domain.CertificateStatusFailed ||
|
||||
cert.Status == domain.CertificateStatusExpired {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
@@ -238,6 +239,77 @@ func TestCheckExpiringCertificates_SkipsRenewalInProgress(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckExpiringCertificates_SkipsExpiredFailedRevoked(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Test that certs in Expired, Failed, and Revoked states do not get renewal jobs
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
status domain.CertificateStatus
|
||||
}{
|
||||
{"Expired", domain.CertificateStatusExpired},
|
||||
{"Failed", domain.CertificateStatusFailed},
|
||||
{"Revoked", domain.CertificateStatusRevoked},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
certRepo := newMockCertificateRepository()
|
||||
jobRepo := newMockJobRepository()
|
||||
policyRepo := newMockRenewalPolicyRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
notifRepo := newMockNotificationRepository()
|
||||
|
||||
auditSvc := NewAuditService(auditRepo)
|
||||
notifSvc := NewNotificationService(notifRepo, map[string]Notifier{})
|
||||
|
||||
issuerRegistry := NewIssuerRegistry(slog.Default())
|
||||
issuerRegistry.Set("iss-test", &mockIssuerConnector{})
|
||||
|
||||
svc := NewRenewalService(certRepo, jobRepo, policyRepo, nil, auditSvc, notifSvc, issuerRegistry, "server")
|
||||
|
||||
cert := &domain.ManagedCertificate{
|
||||
ID: "mc-" + strings.ToLower(string(tc.status)),
|
||||
Name: "Test " + string(tc.status),
|
||||
CommonName: "test.example.com",
|
||||
SANs: []string{},
|
||||
OwnerID: "owner-1",
|
||||
TeamID: "team-1",
|
||||
IssuerID: "iss-test",
|
||||
RenewalPolicyID: "rp-standard",
|
||||
Status: tc.status,
|
||||
ExpiresAt: time.Now().AddDate(0, 0, 10),
|
||||
Tags: make(map[string]string),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
certRepo.AddCert(cert)
|
||||
|
||||
policy := &domain.RenewalPolicy{
|
||||
ID: "rp-standard",
|
||||
Name: "Standard",
|
||||
RenewalWindowDays: 30,
|
||||
AutoRenew: true,
|
||||
MaxRetries: 3,
|
||||
RetryInterval: 300,
|
||||
AlertThresholdsDays: []int{30, 14, 7, 0},
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
policyRepo.AddPolicy(policy)
|
||||
|
||||
err := svc.CheckExpiringCertificates(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("CheckExpiringCertificates failed: %v", err)
|
||||
}
|
||||
|
||||
for _, job := range jobRepo.Jobs {
|
||||
if job.Type == domain.JobTypeRenewal {
|
||||
t.Errorf("should not create renewal job for cert with %s status", tc.status)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckExpiringCertificates_UpdatesStatusToExpiring(t *testing.T) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
@@ -1128,4 +1200,188 @@ func TestCheckExpiringCertificates_ARI_Error_FallsThrough(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestExpireShortLivedCertificates_Tier3 tests that ExpireShortLivedCertificates
|
||||
// marks short-lived certificates that have passed their expiry time as Expired.
|
||||
func TestExpireShortLivedCertificates_Tier3(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Set up repos
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
notifRepo := newMockNotificationRepository()
|
||||
|
||||
// Import the profile repo mock from context_test which already exists
|
||||
profileRepo := &mockCertificateProfileRepository{
|
||||
Profiles: make(map[string]*domain.CertificateProfile),
|
||||
}
|
||||
|
||||
// Create a short-lived profile
|
||||
shortLivedProfile := &domain.CertificateProfile{
|
||||
ID: "prof-sl-1",
|
||||
Name: "ShortLived",
|
||||
MaxTTLSeconds: 3599, // Under 1 hour
|
||||
AllowShortLived: true,
|
||||
Enabled: true,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
profileRepo.Create(ctx, shortLivedProfile)
|
||||
|
||||
// Create a short-lived cert that has expired
|
||||
now := time.Now()
|
||||
expiredTime := now.Add(-5 * time.Minute) // Already expired
|
||||
expiredCert := &domain.ManagedCertificate{
|
||||
ID: "cert-short-1",
|
||||
CommonName: "test.example.com",
|
||||
Status: domain.CertificateStatusActive,
|
||||
CertificateProfileID: "prof-sl-1",
|
||||
ExpiresAt: expiredTime,
|
||||
CreatedAt: now.Add(-10 * time.Minute),
|
||||
UpdatedAt: now.Add(-10 * time.Minute),
|
||||
}
|
||||
certRepo.AddCert(expiredCert)
|
||||
|
||||
// Mock the GetExpiringCertificates to return our expired cert
|
||||
certRepo.MockGetExpiring = []*domain.ManagedCertificate{expiredCert}
|
||||
|
||||
auditSvc := NewAuditService(auditRepo)
|
||||
notifSvc := NewNotificationService(notifRepo, map[string]Notifier{})
|
||||
|
||||
svc := NewRenewalService(
|
||||
certRepo, nil, nil, profileRepo,
|
||||
auditSvc, notifSvc, NewIssuerRegistry(slog.Default()), "agent",
|
||||
)
|
||||
|
||||
// Call ExpireShortLivedCertificates
|
||||
err := svc.ExpireShortLivedCertificates(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ExpireShortLivedCertificates failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify the cert status was updated to Expired
|
||||
if len(certRepo.Updated) == 0 {
|
||||
t.Error("expected certificate to be updated")
|
||||
return
|
||||
}
|
||||
|
||||
updatedCert := certRepo.Updated[0]
|
||||
if updatedCert.Status != domain.CertificateStatusExpired {
|
||||
t.Errorf("expected status Expired, got %s", updatedCert.Status)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFailJob_SetsFailedStatus tests that job status is correctly updated to Failed.
|
||||
func TestFailJob_SetsFailedStatus(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Set up repos
|
||||
jobRepo := newMockJobRepository()
|
||||
|
||||
// Create a job
|
||||
job := &domain.Job{
|
||||
ID: "job-fail-1",
|
||||
Type: domain.JobTypeRenewal,
|
||||
Status: domain.JobStatusRunning,
|
||||
CreatedAt: time.Now(),
|
||||
ScheduledAt: time.Now(),
|
||||
}
|
||||
jobRepo.Jobs[job.ID] = job
|
||||
|
||||
// Simulate what failJob does - update the job with Failed status and error message
|
||||
errMsg := "test error message"
|
||||
job.Status = domain.JobStatusFailed
|
||||
job.LastError = &errMsg
|
||||
|
||||
// Call the Update method which is what failJob would do
|
||||
err := jobRepo.Update(ctx, job)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to update job: %v", err)
|
||||
}
|
||||
|
||||
// Verify the job was marked as failed
|
||||
if len(jobRepo.Updated) == 0 {
|
||||
t.Error("expected job to be updated")
|
||||
return
|
||||
}
|
||||
|
||||
updatedJob := jobRepo.Updated[0]
|
||||
if updatedJob.Status != domain.JobStatusFailed {
|
||||
t.Errorf("expected status Failed, got %s", updatedJob.Status)
|
||||
}
|
||||
if updatedJob.LastError == nil || *updatedJob.LastError == "" {
|
||||
t.Error("expected error message to be set")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// --- CreateDeploymentJobs Tests ---
|
||||
|
||||
func TestCreateDeploymentJobs_PartialFailure(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
jobRepo := newMockJobRepository()
|
||||
targetRepo := newMockTargetRepository()
|
||||
agentRepo := newMockAgentRepository()
|
||||
certRepo := newMockCertificateRepository()
|
||||
auditRepo := newMockAuditRepository()
|
||||
|
||||
auditSvc := NewAuditService(auditRepo)
|
||||
|
||||
depSvc := NewDeploymentService(jobRepo, targetRepo, agentRepo, certRepo, auditSvc, nil)
|
||||
|
||||
// Create certificate
|
||||
cert := &domain.ManagedCertificate{
|
||||
ID: "mc-partial",
|
||||
CommonName: "test.example.com",
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
certRepo.AddCert(cert)
|
||||
|
||||
// Create target with agent assignment
|
||||
target := &domain.DeploymentTarget{
|
||||
ID: "tgt-1",
|
||||
Name: "target-1",
|
||||
Type: "nginx",
|
||||
AgentID: "agent-1",
|
||||
Config: json.RawMessage("{}"),
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
targetRepo.Targets[target.ID] = target
|
||||
|
||||
// Mock ListByCertificate to return the target
|
||||
// (the mock returns all targets, so we just need one in the map)
|
||||
|
||||
// Execute CreateDeploymentJobs
|
||||
jobIDs, err := depSvc.CreateDeploymentJobs(ctx, cert.ID)
|
||||
|
||||
// Should succeed
|
||||
if err != nil {
|
||||
t.Fatalf("CreateDeploymentJobs failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify job was created
|
||||
if len(jobIDs) == 0 {
|
||||
t.Error("expected at least one deployment job to be created")
|
||||
}
|
||||
|
||||
// Verify the job has correct properties
|
||||
if len(jobRepo.Jobs) == 0 {
|
||||
t.Fatal("expected job to be created")
|
||||
}
|
||||
|
||||
createdJob := jobRepo.Jobs[jobIDs[0]]
|
||||
if createdJob.Type != domain.JobTypeDeployment {
|
||||
t.Errorf("expected JobTypeDeployment, got %s", createdJob.Type)
|
||||
}
|
||||
if createdJob.CertificateID != cert.ID {
|
||||
t.Errorf("expected certificate ID %s, got %s", cert.ID, createdJob.CertificateID)
|
||||
}
|
||||
if createdJob.AgentID == nil || *createdJob.AgentID != "agent-1" {
|
||||
t.Error("expected job to be routed to agent-1")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// stringPtr is defined in notification_test.go
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/domain"
|
||||
)
|
||||
|
||||
// SCEPService implements the SCEP (RFC 8894) enrollment protocol.
|
||||
// It delegates certificate operations to an existing IssuerConnector and records
|
||||
// enrollment events in the audit trail.
|
||||
type SCEPService struct {
|
||||
issuer IssuerConnector
|
||||
issuerID string
|
||||
auditService *AuditService
|
||||
logger *slog.Logger
|
||||
profileID string // optional: constrain enrollments to a specific profile
|
||||
challengePassword string // shared secret for enrollment authentication
|
||||
}
|
||||
|
||||
// NewSCEPService creates a new SCEPService for the given issuer connector.
|
||||
func NewSCEPService(issuerID string, issuer IssuerConnector, auditService *AuditService, logger *slog.Logger, challengePassword string) *SCEPService {
|
||||
return &SCEPService{
|
||||
issuer: issuer,
|
||||
issuerID: issuerID,
|
||||
auditService: auditService,
|
||||
logger: logger,
|
||||
challengePassword: challengePassword,
|
||||
}
|
||||
}
|
||||
|
||||
// SetProfileID constrains SCEP enrollments to a specific certificate profile.
|
||||
func (s *SCEPService) SetProfileID(profileID string) {
|
||||
s.profileID = profileID
|
||||
}
|
||||
|
||||
// GetCACaps returns the capabilities of this SCEP server.
|
||||
// RFC 8894 Section 3.5.2: GetCACaps returns a list of capabilities, one per line.
|
||||
func (s *SCEPService) GetCACaps(ctx context.Context) string {
|
||||
return "POSTPKIOperation\nSHA-256\nAES\nSCEPStandard\n"
|
||||
}
|
||||
|
||||
// GetCACert returns the PEM-encoded CA certificate chain for this SCEP server.
|
||||
// RFC 8894 Section 3.5.1: GetCACert distributes the CA certificate(s).
|
||||
func (s *SCEPService) GetCACert(ctx context.Context) (string, error) {
|
||||
caPEM, err := s.issuer.GetCACertPEM(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to get CA certificates from issuer %s: %w", s.issuerID, err)
|
||||
}
|
||||
if caPEM == "" {
|
||||
return "", fmt.Errorf("issuer %s does not provide CA certificates for SCEP", s.issuerID)
|
||||
}
|
||||
return caPEM, nil
|
||||
}
|
||||
|
||||
// PKCSReq processes a SCEP enrollment request.
|
||||
// RFC 8894 Section 3.3.1: PKCSReq contains a PKCS#10 CSR for certificate enrollment.
|
||||
// The CSR PEM and challenge password are extracted by the handler from the PKCS#7 envelope.
|
||||
func (s *SCEPService) PKCSReq(ctx context.Context, csrPEM string, challengePassword string, transactionID string) (*domain.SCEPEnrollResult, error) {
|
||||
// Validate challenge password
|
||||
if s.challengePassword != "" {
|
||||
if challengePassword != s.challengePassword {
|
||||
s.logger.Warn("SCEP enrollment rejected: invalid challenge password",
|
||||
"transaction_id", transactionID)
|
||||
return nil, fmt.Errorf("invalid challenge password")
|
||||
}
|
||||
}
|
||||
|
||||
return s.processEnrollment(ctx, csrPEM, transactionID, "scep_pkcsreq")
|
||||
}
|
||||
|
||||
// processEnrollment handles the common enrollment logic.
|
||||
func (s *SCEPService) processEnrollment(ctx context.Context, csrPEM string, transactionID string, auditAction string) (*domain.SCEPEnrollResult, error) {
|
||||
// Parse the CSR to extract CN and SANs
|
||||
block, _ := pem.Decode([]byte(csrPEM))
|
||||
if block == nil {
|
||||
return nil, fmt.Errorf("invalid CSR PEM")
|
||||
}
|
||||
|
||||
csr, err := x509.ParseCertificateRequest(block.Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse CSR: %w", err)
|
||||
}
|
||||
|
||||
if err := csr.CheckSignature(); err != nil {
|
||||
return nil, fmt.Errorf("CSR signature verification failed: %w", err)
|
||||
}
|
||||
|
||||
commonName := csr.Subject.CommonName
|
||||
if commonName == "" {
|
||||
return nil, fmt.Errorf("CSR must include a Common Name")
|
||||
}
|
||||
|
||||
// Collect SANs
|
||||
var sans []string
|
||||
for _, dns := range csr.DNSNames {
|
||||
sans = append(sans, dns)
|
||||
}
|
||||
for _, ip := range csr.IPAddresses {
|
||||
sans = append(sans, ip.String())
|
||||
}
|
||||
for _, email := range csr.EmailAddresses {
|
||||
sans = append(sans, email)
|
||||
}
|
||||
for _, uri := range csr.URIs {
|
||||
sans = append(sans, uri.String())
|
||||
}
|
||||
|
||||
s.logger.Info("SCEP enrollment request",
|
||||
"action", auditAction,
|
||||
"common_name", commonName,
|
||||
"sans", strings.Join(sans, ","),
|
||||
"transaction_id", transactionID,
|
||||
"issuer", s.issuerID)
|
||||
|
||||
// Issue the certificate via the configured issuer connector
|
||||
// SCEP enrollments use default EKUs (nil = serverAuth + clientAuth fallback in connector)
|
||||
result, err := s.issuer.IssueCertificate(ctx, commonName, sans, csrPEM, nil)
|
||||
if err != nil {
|
||||
s.logger.Error("SCEP enrollment failed",
|
||||
"action", auditAction,
|
||||
"common_name", commonName,
|
||||
"transaction_id", transactionID,
|
||||
"error", err)
|
||||
return nil, fmt.Errorf("certificate issuance failed: %w", err)
|
||||
}
|
||||
|
||||
// Audit the enrollment
|
||||
if s.auditService != nil {
|
||||
details := map[string]interface{}{
|
||||
"common_name": commonName,
|
||||
"sans": sans,
|
||||
"issuer_id": s.issuerID,
|
||||
"serial": result.Serial,
|
||||
"transaction_id": transactionID,
|
||||
"protocol": "SCEP",
|
||||
}
|
||||
if s.profileID != "" {
|
||||
details["profile_id"] = s.profileID
|
||||
}
|
||||
_ = s.auditService.RecordEvent(ctx, "scep-client", "system", auditAction, "certificate", result.Serial, details)
|
||||
}
|
||||
|
||||
s.logger.Info("SCEP enrollment successful",
|
||||
"action", auditAction,
|
||||
"common_name", commonName,
|
||||
"serial", result.Serial,
|
||||
"transaction_id", transactionID,
|
||||
"not_after", result.NotAfter)
|
||||
|
||||
return &domain.SCEPEnrollResult{
|
||||
CertPEM: result.CertPEM,
|
||||
ChainPEM: result.ChainPEM,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSCEPService_GetCACaps(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
svc := NewSCEPService("iss-local", mockIssuer, nil, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
|
||||
caps := svc.GetCACaps(context.Background())
|
||||
if caps == "" {
|
||||
t.Error("expected non-empty capabilities")
|
||||
}
|
||||
if !strings.Contains(caps, "POSTPKIOperation") {
|
||||
t.Errorf("expected POSTPKIOperation in caps, got: %s", caps)
|
||||
}
|
||||
if !strings.Contains(caps, "SHA-256") {
|
||||
t.Errorf("expected SHA-256 in caps, got: %s", caps)
|
||||
}
|
||||
if !strings.Contains(caps, "SCEPStandard") {
|
||||
t.Errorf("expected SCEPStandard in caps, got: %s", caps)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_GetCACert_Success(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
svc := NewSCEPService("iss-local", mockIssuer, nil, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
|
||||
caPEM, err := svc.GetCACert(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if caPEM == "" {
|
||||
t.Error("expected non-empty CA PEM")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_GetCACert_IssuerError(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{Err: errors.New("CA unavailable")}
|
||||
svc := NewSCEPService("iss-local", mockIssuer, nil, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
|
||||
_, err := svc.GetCACert(context.Background())
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "CA unavailable") {
|
||||
t.Errorf("expected error to contain 'CA unavailable', got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_PKCSReq_Success(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditSvc := NewAuditService(auditRepo)
|
||||
svc := NewSCEPService("iss-local", mockIssuer, auditSvc, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
|
||||
csrPEM := generateCSRPEM(t, "device.example.com", []string{"device.example.com"})
|
||||
|
||||
result, err := svc.PKCSReq(context.Background(), csrPEM, "", "txn-001")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if result == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
if result.CertPEM == "" {
|
||||
t.Error("expected non-empty CertPEM")
|
||||
}
|
||||
|
||||
// Verify audit event was recorded
|
||||
if len(auditRepo.Events) == 0 {
|
||||
t.Error("expected audit event to be recorded")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_PKCSReq_InvalidCSR(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
svc := NewSCEPService("iss-local", mockIssuer, nil, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
|
||||
_, err := svc.PKCSReq(context.Background(), "not-valid-pem", "", "txn-002")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid CSR")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_PKCSReq_MissingCN(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
svc := NewSCEPService("iss-local", mockIssuer, nil, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
|
||||
csrPEM := generateCSRPEM(t, "", []string{"test.example.com"})
|
||||
|
||||
_, err := svc.PKCSReq(context.Background(), csrPEM, "", "txn-003")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for missing CN")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "Common Name") {
|
||||
t.Errorf("expected 'Common Name' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_PKCSReq_IssuerError(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{Err: errors.New("issuance failed")}
|
||||
svc := NewSCEPService("iss-local", mockIssuer, nil, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
|
||||
csrPEM := generateCSRPEM(t, "test.example.com", nil)
|
||||
|
||||
_, err := svc.PKCSReq(context.Background(), csrPEM, "", "txn-004")
|
||||
if err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "issuance failed") {
|
||||
t.Errorf("expected 'issuance failed', got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_PKCSReq_ChallengePassword_Valid(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditSvc := NewAuditService(auditRepo)
|
||||
svc := NewSCEPService("iss-local", mockIssuer, auditSvc, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "secret123")
|
||||
|
||||
csrPEM := generateCSRPEM(t, "mdm-device.example.com", nil)
|
||||
|
||||
result, err := svc.PKCSReq(context.Background(), csrPEM, "secret123", "txn-005")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if result == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_PKCSReq_ChallengePassword_Invalid(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
svc := NewSCEPService("iss-local", mockIssuer, nil, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "secret123")
|
||||
|
||||
csrPEM := generateCSRPEM(t, "mdm-device.example.com", nil)
|
||||
|
||||
_, err := svc.PKCSReq(context.Background(), csrPEM, "wrong-password", "txn-006")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for invalid challenge password")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "challenge password") {
|
||||
t.Errorf("expected 'challenge password' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_PKCSReq_ChallengePassword_NotRequired(t *testing.T) {
|
||||
// When server has no challenge password configured, any value should be accepted
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
svc := NewSCEPService("iss-local", mockIssuer, nil, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
|
||||
csrPEM := generateCSRPEM(t, "device.example.com", nil)
|
||||
|
||||
result, err := svc.PKCSReq(context.Background(), csrPEM, "any-value", "txn-007")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if result == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSCEPService_PKCSReq_WithProfile(t *testing.T) {
|
||||
mockIssuer := &mockIssuerConnector{}
|
||||
auditRepo := newMockAuditRepository()
|
||||
auditSvc := NewAuditService(auditRepo)
|
||||
svc := NewSCEPService("iss-local", mockIssuer, auditSvc, slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})), "")
|
||||
svc.SetProfileID("profile-mdm-device")
|
||||
|
||||
csrPEM := generateCSRPEM(t, "device.example.com", nil)
|
||||
|
||||
result, err := svc.PKCSReq(context.Background(), csrPEM, "", "txn-008")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if result == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
|
||||
// Verify audit event includes profile_id
|
||||
if len(auditRepo.Events) == 0 {
|
||||
t.Fatal("expected audit event")
|
||||
}
|
||||
lastEvent := auditRepo.Events[len(auditRepo.Events)-1]
|
||||
if lastEvent.Details == nil {
|
||||
t.Fatal("expected audit details")
|
||||
}
|
||||
}
|
||||
@@ -24,9 +24,10 @@ var validTargetTypes = map[domain.TargetType]bool{
|
||||
domain.TargetTypeEnvoy: true,
|
||||
domain.TargetTypePostfix: true,
|
||||
domain.TargetTypeDovecot: true,
|
||||
domain.TargetTypeSSH: true,
|
||||
domain.TargetTypeWinCertStore: true,
|
||||
domain.TargetTypeJavaKeystore: true,
|
||||
domain.TargetTypeSSH: true,
|
||||
domain.TargetTypeWinCertStore: true,
|
||||
domain.TargetTypeJavaKeystore: true,
|
||||
domain.TargetTypeKubernetesSecrets: true,
|
||||
}
|
||||
|
||||
// isValidTargetType checks if a type string is a known target type.
|
||||
@@ -284,6 +285,11 @@ func (s *TargetService) CreateTarget(target domain.DeploymentTarget) (*domain.De
|
||||
if target.Source == "" {
|
||||
target.Source = "database"
|
||||
}
|
||||
// GUI-created targets should be enabled by default.
|
||||
// Go's bool zero value is false, which overrides the DB default when explicitly inserted.
|
||||
if target.Source == "database" && !target.Enabled {
|
||||
target.Enabled = true
|
||||
}
|
||||
|
||||
// Encrypt config
|
||||
if len(target.Config) > 0 {
|
||||
|
||||
@@ -24,6 +24,8 @@ type mockCertRepo struct {
|
||||
ListVersionsResult []*domain.CertificateVersion
|
||||
CreateVersionErr error
|
||||
ArchiveErr error
|
||||
Updated []*domain.ManagedCertificate
|
||||
MockGetExpiring []*domain.ManagedCertificate
|
||||
}
|
||||
|
||||
func (m *mockCertRepo) List(ctx context.Context, filter *repository.CertificateFilter) ([]*domain.ManagedCertificate, int, error) {
|
||||
@@ -61,6 +63,7 @@ func (m *mockCertRepo) Update(ctx context.Context, cert *domain.ManagedCertifica
|
||||
return m.UpdateErr
|
||||
}
|
||||
m.Certs[cert.ID] = cert
|
||||
m.Updated = append(m.Updated, cert)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -95,6 +98,10 @@ func (m *mockCertRepo) CreateVersion(ctx context.Context, version *domain.Certif
|
||||
}
|
||||
|
||||
func (m *mockCertRepo) GetExpiringCertificates(ctx context.Context, before time.Time) ([]*domain.ManagedCertificate, error) {
|
||||
// Return MockGetExpiring if set, for test control
|
||||
if m.MockGetExpiring != nil {
|
||||
return m.MockGetExpiring, nil
|
||||
}
|
||||
var expiring []*domain.ManagedCertificate
|
||||
for _, c := range m.Certs {
|
||||
if c.ExpiresAt.Before(before) {
|
||||
@@ -128,6 +135,7 @@ type mockJobRepo struct {
|
||||
ListErr error
|
||||
ListByStatusErr error
|
||||
DeleteErr error
|
||||
Updated []*domain.Job
|
||||
}
|
||||
|
||||
func (m *mockJobRepo) List(ctx context.Context) ([]*domain.Job, error) {
|
||||
@@ -173,6 +181,7 @@ func (m *mockJobRepo) Update(ctx context.Context, job *domain.Job) error {
|
||||
return m.UpdateErr
|
||||
}
|
||||
m.Jobs[job.ID] = job
|
||||
m.Updated = append(m.Updated, job)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -690,6 +699,12 @@ func (m *mockTargetRepo) AddTarget(target *domain.DeploymentTarget) {
|
||||
m.Targets[target.ID] = target
|
||||
}
|
||||
|
||||
func newMockTargetRepository() *mockTargetRepo {
|
||||
return &mockTargetRepo{
|
||||
Targets: make(map[string]*domain.DeploymentTarget),
|
||||
}
|
||||
}
|
||||
|
||||
// mockIssuerConnector is a test implementation of IssuerConnector
|
||||
type mockIssuerConnector struct {
|
||||
Result *IssuanceResult
|
||||
|
||||
+17
-10
@@ -47,7 +47,8 @@ INSERT INTO issuers (id, name, type, config, enabled, created_at, updated_at) VA
|
||||
('iss-vault', 'HashiCorp Vault PKI', 'VaultPKI', '{"addr": "https://vault.internal:8200", "mount": "pki", "role": "web-certs", "ttl": "8760h"}', true, NOW() - INTERVAL '20 days', NOW() - INTERVAL '20 days'),
|
||||
('iss-digicert', 'DigiCert CertCentral', 'DigiCert', '{"base_url": "https://www.digicert.com/services/v2", "product_type": "ssl_basic"}', true, NOW() - INTERVAL '15 days', NOW() - INTERVAL '15 days'),
|
||||
('iss-sectigo', 'Sectigo SCM', 'Sectigo', '{"base_url": "https://cert-manager.com/api", "cert_type": 423, "term": 365}', true, NOW() - INTERVAL '10 days', NOW() - INTERVAL '10 days'),
|
||||
('iss-googlecas','Google CAS', 'GoogleCAS', '{"project": "demo-project", "location": "us-central1", "ca_pool": "demo-pool"}', false, NOW() - INTERVAL '5 days', NOW() - INTERVAL '5 days')
|
||||
('iss-googlecas','Google CAS', 'GoogleCAS', '{"project": "demo-project", "location": "us-central1", "ca_pool": "demo-pool"}', false, NOW() - INTERVAL '5 days', NOW() - INTERVAL '5 days'),
|
||||
('iss-awsacmpca','AWS ACM Private CA', 'AWSACMPCA', '{"region": "us-east-1", "ca_arn": "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/demo", "signing_algorithm": "SHA256WITHRSA", "validity_days": 365}', false, NOW() - INTERVAL '3 days', NOW() - INTERVAL '3 days')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
-- ============================================================
|
||||
@@ -149,17 +150,21 @@ INSERT INTO managed_certificates (id, name, common_name, sans, environment, owne
|
||||
|
||||
-- ---- Active certs via step-ca (internal services) ----
|
||||
('mc-grpc-prod', 'grpc-internal', 'grpc.internal.example.com', ARRAY['grpc.internal.example.com'], 'production', 'o-alice', 't-platform', 'iss-stepca', 'rp-standard', 'Active', NOW() + INTERVAL '58 days', '{"service": "grpc-gateway", "tier": "high"}', NOW() - INTERVAL '32 days', NOW() - INTERVAL '32 days', NOW() - INTERVAL '100 days', NOW()),
|
||||
('mc-vault-prod', 'vault-internal', 'vault.internal.example.com', ARRAY['vault.internal.example.com'], 'production', 'o-bob', 't-security', 'iss-stepca', 'rp-urgent', 'Active', NOW() + INTERVAL '25 days', '{"service": "vault", "tier": "critical"}', NOW() - INTERVAL '65 days', NOW() - INTERVAL '65 days', NOW() - INTERVAL '120 days', NOW()),
|
||||
('mc-vault-prod', 'vault-internal', 'vault.internal.example.com', ARRAY['vault.internal.example.com'], 'production', 'o-bob', 't-security', 'iss-stepca', 'rp-urgent', 'Active', NOW() + INTERVAL '35 days', '{"service": "vault", "tier": "critical"}', NOW() - INTERVAL '65 days', NOW() - INTERVAL '65 days', NOW() - INTERVAL '120 days', NOW()),
|
||||
('mc-consul-prod', 'consul-internal', 'consul.internal.example.com', ARRAY['consul.internal.example.com'], 'production', 'o-alice', 't-platform', 'iss-stepca', 'rp-standard', 'Active', NOW() + INTERVAL '63 days', '{"service": "consul", "tier": "high"}', NOW() - INTERVAL '27 days', NOW() - INTERVAL '27 days', NOW() - INTERVAL '90 days', NOW()),
|
||||
|
||||
-- ---- Active certs via ZeroSSL ----
|
||||
('mc-shop-prod', 'shop-production', 'shop.example.com', ARRAY['shop.example.com', 'store.example.com'], 'production', 'o-carol', 't-payments', 'iss-acme-zs', 'rp-urgent', 'Active', NOW() + INTERVAL '44 days', '{"service": "shop", "tier": "critical", "pci": "true"}', NOW() - INTERVAL '46 days', NOW() - INTERVAL '46 days', NOW() - INTERVAL '60 days', NOW()),
|
||||
|
||||
-- ---- Expiring soon (< 30 days) ----
|
||||
('mc-auth-prod', 'auth-production', 'auth.example.com', ARRAY['auth.example.com', 'login.example.com', 'sso.example.com'], 'production', 'o-bob', 't-security', 'iss-local', 'rp-urgent', 'Expiring', NOW() + INTERVAL '12 days', '{"service": "auth", "tier": "critical"}', NOW() - INTERVAL '78 days', NOW() - INTERVAL '78 days', NOW() - INTERVAL '300 days', NOW()),
|
||||
('mc-cdn-prod', 'cdn-production', 'cdn.example.com', ARRAY['cdn.example.com', 'static.example.com'], 'production', 'o-alice', 't-platform', 'iss-local', 'rp-standard', 'Expiring', NOW() + INTERVAL '8 days', '{"service": "cdn", "tier": "high"}', NOW() - INTERVAL '82 days', NOW() - INTERVAL '82 days', NOW() - INTERVAL '250 days', NOW()),
|
||||
('mc-mail-prod', 'mail-production', 'mail.example.com', ARRAY['mail.example.com', 'smtp.example.com'], 'production', 'o-bob', 't-security', 'iss-local', 'rp-standard', 'Expiring', NOW() + INTERVAL '5 days', '{"service": "email", "tier": "medium"}', NOW() - INTERVAL '85 days', NOW() - INTERVAL '85 days', NOW() - INTERVAL '400 days', NOW()),
|
||||
('mc-ci-prod', 'ci-production', 'ci.example.com', ARRAY['ci.example.com', 'jenkins.example.com'], 'production', 'o-frank', 't-devops', 'iss-acme-le', 'rp-standard', 'Expiring', NOW() + INTERVAL '18 days', '{"service": "ci", "tier": "high"}', NOW() - INTERVAL '72 days', NOW() - INTERVAL '72 days', NOW() - INTERVAL '100 days', NOW()),
|
||||
-- ---- Expiring soon ----
|
||||
-- NOTE: expires_at is set > 31 days to stay outside the scheduler's 31-day renewal query window.
|
||||
-- The scheduler runs CheckExpiringCertificates on boot with a 31-day lookahead; certs inside that
|
||||
-- window get renewal jobs created automatically. By placing these at 32-38 days, the status stays
|
||||
-- frozen as seeded while still being within the 30-day alert threshold range shown on the dashboard.
|
||||
('mc-auth-prod', 'auth-production', 'auth.example.com', ARRAY['auth.example.com', 'login.example.com', 'sso.example.com'], 'production', 'o-bob', 't-security', 'iss-local', 'rp-urgent', 'Expiring', NOW() + INTERVAL '32 days', '{"service": "auth", "tier": "critical"}', NOW() - INTERVAL '78 days', NOW() - INTERVAL '78 days', NOW() - INTERVAL '300 days', NOW()),
|
||||
('mc-cdn-prod', 'cdn-production', 'cdn.example.com', ARRAY['cdn.example.com', 'static.example.com'], 'production', 'o-alice', 't-platform', 'iss-local', 'rp-standard', 'Expiring', NOW() + INTERVAL '34 days', '{"service": "cdn", "tier": "high"}', NOW() - INTERVAL '82 days', NOW() - INTERVAL '82 days', NOW() - INTERVAL '250 days', NOW()),
|
||||
('mc-mail-prod', 'mail-production', 'mail.example.com', ARRAY['mail.example.com', 'smtp.example.com'], 'production', 'o-bob', 't-security', 'iss-local', 'rp-standard', 'Expiring', NOW() + INTERVAL '33 days', '{"service": "email", "tier": "medium"}', NOW() - INTERVAL '85 days', NOW() - INTERVAL '85 days', NOW() - INTERVAL '400 days', NOW()),
|
||||
('mc-ci-prod', 'ci-production', 'ci.example.com', ARRAY['ci.example.com', 'jenkins.example.com'], 'production', 'o-frank', 't-devops', 'iss-acme-le', 'rp-standard', 'Expiring', NOW() + INTERVAL '38 days', '{"service": "ci", "tier": "high"}', NOW() - INTERVAL '72 days', NOW() - INTERVAL '72 days', NOW() - INTERVAL '100 days', NOW()),
|
||||
|
||||
-- ---- Expired ----
|
||||
('mc-legacy-prod', 'legacy-app', 'legacy.example.com', ARRAY['legacy.example.com'], 'production', 'o-alice', 't-platform', 'iss-local', 'rp-manual', 'Expired', NOW() - INTERVAL '3 days', '{"service": "legacy", "tier": "low", "decom": "planned"}', NOW() - INTERVAL '93 days', NOW() - INTERVAL '93 days', NOW() - INTERVAL '500 days', NOW()),
|
||||
@@ -175,16 +180,18 @@ INSERT INTO managed_certificates (id, name, common_name, sans, environment, owne
|
||||
('mc-api-dev', 'api-development', 'api.dev.example.com', ARRAY['api.dev.example.com'], 'development', 'o-alice', 't-platform', 'iss-local', 'rp-standard', 'Active', NOW() + INTERVAL '85 days', '{"service": "api-gateway", "tier": "low"}', NOW() - INTERVAL '5 days', NOW() - INTERVAL '5 days', NOW() - INTERVAL '45 days', NOW()),
|
||||
|
||||
-- ---- Renewal in progress ----
|
||||
('mc-grafana-prod', 'grafana-production', 'grafana.example.com', ARRAY['grafana.example.com', 'metrics.example.com'], 'production', 'o-eve', 't-data', 'iss-local', 'rp-standard', 'RenewalInProgress', NOW() + INTERVAL '3 days', '{"service": "monitoring", "tier": "high"}', NOW() - INTERVAL '87 days', NOW() - INTERVAL '87 days', NOW() - INTERVAL '180 days', NOW()),
|
||||
-- NOTE: expires_at set > 31 days to keep outside scheduler's renewal query window
|
||||
('mc-grafana-prod', 'grafana-production', 'grafana.example.com', ARRAY['grafana.example.com', 'metrics.example.com'], 'production', 'o-eve', 't-data', 'iss-local', 'rp-standard', 'RenewalInProgress', NOW() + INTERVAL '33 days', '{"service": "monitoring", "tier": "high"}', NOW() - INTERVAL '87 days', NOW() - INTERVAL '87 days', NOW() - INTERVAL '180 days', NOW()),
|
||||
|
||||
-- ---- Failed ----
|
||||
('mc-vpn-prod', 'vpn-production', 'vpn.example.com', ARRAY['vpn.example.com'], 'production', 'o-bob', 't-security', 'iss-acme-le', 'rp-urgent', 'Failed', NOW() + INTERVAL '1 day', '{"service": "vpn", "tier": "critical"}', NULL, NULL, NOW() - INTERVAL '90 days', NOW()),
|
||||
-- NOTE: expires_at set > 31 days; scheduler code fix also skips Failed certs from auto-renewal
|
||||
('mc-vpn-prod', 'vpn-production', 'vpn.example.com', ARRAY['vpn.example.com'], 'production', 'o-bob', 't-security', 'iss-acme-le', 'rp-urgent', 'Failed', NOW() + INTERVAL '32 days', '{"service": "vpn", "tier": "critical"}', NULL, NULL, NOW() - INTERVAL '90 days', NOW()),
|
||||
|
||||
-- ---- Wildcard ----
|
||||
('mc-wildcard-prod', 'wildcard-production', '*.example.com', ARRAY['*.example.com', 'example.com'], 'production', 'o-alice', 't-platform', 'iss-acme-le', 'rp-standard', 'Active', NOW() + INTERVAL '50 days', '{"service": "wildcard", "tier": "critical"}', NOW() - INTERVAL '40 days', NOW() - INTERVAL '40 days', NOW() - INTERVAL '365 days', NOW()),
|
||||
|
||||
-- ---- Revoked ----
|
||||
('mc-compromised', 'compromised-cert', 'old-service.example.com', ARRAY['old-service.example.com'], 'production', 'o-bob', 't-security', 'iss-local', 'rp-standard', 'Revoked', NOW() + INTERVAL '30 days', '{"service": "decommissioned", "tier": "low"}', NOW() - INTERVAL '60 days', NOW() - INTERVAL '60 days', NOW() - INTERVAL '120 days', NOW()),
|
||||
('mc-compromised', 'compromised-cert', 'old-service.example.com', ARRAY['old-service.example.com'], 'production', 'o-bob', 't-security', 'iss-local', 'rp-standard', 'Revoked', NOW() + INTERVAL '45 days', '{"service": "decommissioned", "tier": "low"}', NOW() - INTERVAL '60 days', NOW() - INTERVAL '60 days', NOW() - INTERVAL '120 days', NOW()),
|
||||
|
||||
-- ---- Edge/CDN certs (Traefik + Caddy targets) ----
|
||||
('mc-edge-eu', 'edge-eu-production', 'eu.cdn.example.com', ARRAY['eu.cdn.example.com', 'eu-assets.example.com'], 'production', 'o-alice', 't-platform', 'iss-acme-le', 'rp-standard', 'Active', NOW() + INTERVAL '61 days', '{"service": "cdn-eu", "tier": "high", "region": "eu-west-1"}', NOW() - INTERVAL '29 days', NOW() - INTERVAL '29 days', NOW() - INTERVAL '45 days', NOW()),
|
||||
|
||||
@@ -169,15 +169,21 @@ export const getNotifications = (params: Record<string, string> = {}) => {
|
||||
return fetchJSON<PaginatedResponse<Notification>>(`${BASE}/notifications?${qs}`);
|
||||
};
|
||||
|
||||
export const getNotification = (id: string) =>
|
||||
fetchJSON<Notification>(`${BASE}/notifications/${id}`);
|
||||
|
||||
export const markNotificationRead = (id: string) =>
|
||||
fetchJSON<{ message: string }>(`${BASE}/notifications/${id}/read`, { method: 'POST' });
|
||||
|
||||
// Audit
|
||||
export const getAuditEvents = (params: Record<string, string> = {}) => {
|
||||
const qs = new URLSearchParams({ page: '1', per_page: '50', ...params }).toString();
|
||||
const qs = new URLSearchParams({ page: '1', per_page: '200', ...params }).toString();
|
||||
return fetchJSON<PaginatedResponse<AuditEvent>>(`${BASE}/audit?${qs}`);
|
||||
};
|
||||
|
||||
export const getAuditEvent = (id: string) =>
|
||||
fetchJSON<AuditEvent>(`${BASE}/audit/${id}`);
|
||||
|
||||
// Policies
|
||||
export const getPolicies = (params: Record<string, string> = {}) => {
|
||||
const qs = new URLSearchParams({ page: '1', per_page: '50', ...params }).toString();
|
||||
|
||||
+8
-10
@@ -10,11 +10,11 @@ export interface Certificate {
|
||||
team_id: string;
|
||||
renewal_policy_id: string;
|
||||
certificate_profile_id: string;
|
||||
serial_number: string;
|
||||
fingerprint: string;
|
||||
key_algorithm: string;
|
||||
key_size: number;
|
||||
issued_at: string;
|
||||
serial_number?: string;
|
||||
fingerprint_sha256?: string;
|
||||
key_algorithm?: string;
|
||||
key_size?: number;
|
||||
issued_at?: string;
|
||||
expires_at: string;
|
||||
revoked_at?: string;
|
||||
revocation_reason?: string;
|
||||
@@ -40,11 +40,9 @@ export const REVOCATION_REASONS = [
|
||||
export interface CertificateVersion {
|
||||
id: string;
|
||||
certificate_id: string;
|
||||
version: number;
|
||||
serial_number: string;
|
||||
fingerprint: string;
|
||||
cert_pem: string;
|
||||
chain_pem: string;
|
||||
fingerprint_sha256: string;
|
||||
pem_chain: string;
|
||||
csr_pem: string;
|
||||
not_before: string;
|
||||
not_after: string;
|
||||
@@ -80,7 +78,7 @@ export interface Job {
|
||||
status: string;
|
||||
attempts: number;
|
||||
max_attempts: number;
|
||||
error_message: string;
|
||||
last_error?: string;
|
||||
scheduled_at: string;
|
||||
started_at: string;
|
||||
completed_at: string;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
export function formatDate(iso: string): string {
|
||||
export function formatDate(iso: string | undefined | null): string {
|
||||
if (!iso) return '—';
|
||||
return new Date(iso).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' });
|
||||
}
|
||||
|
||||
export function formatDateTime(iso: string): string {
|
||||
export function formatDateTime(iso: string | undefined | null): string {
|
||||
if (!iso) return '—';
|
||||
return new Date(iso).toLocaleString('en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' });
|
||||
}
|
||||
|
||||
@@ -29,19 +29,23 @@ export interface IssuerTypeConfig {
|
||||
}
|
||||
|
||||
/**
|
||||
* Canonical type label map. Keys match what the backend API returns.
|
||||
* DB stores: local, acme, stepca, openssl, VaultPKI, DigiCert
|
||||
* Canonical type label map. Keys MUST match backend IssuerType constants
|
||||
* defined in internal/domain/connector.go (e.g., "ACME", "GenericCA", "StepCA").
|
||||
*/
|
||||
export const typeLabels: Record<string, string> = {
|
||||
local: 'Local CA',
|
||||
GenericCA: 'Local CA',
|
||||
local: 'Local CA', // backward compat for old DB records
|
||||
local_ca: 'Local CA', // backward compat (some frontend references)
|
||||
acme: 'ACME',
|
||||
stepca: 'step-ca',
|
||||
openssl: 'OpenSSL/Custom',
|
||||
ACME: 'ACME',
|
||||
acme: 'ACME', // backward compat for old DB records
|
||||
StepCA: 'step-ca',
|
||||
stepca: 'step-ca', // backward compat for old DB records
|
||||
OpenSSL: 'OpenSSL/Custom',
|
||||
openssl: 'OpenSSL/Custom', // backward compat for old DB records
|
||||
VaultPKI: 'Vault PKI',
|
||||
DigiCert: 'DigiCert',
|
||||
Sectigo: 'Sectigo SCM',
|
||||
manual: 'Manual',
|
||||
GoogleCAS: 'Google CAS',
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -50,7 +54,7 @@ export const typeLabels: Record<string, string> = {
|
||||
*/
|
||||
export const issuerTypes: IssuerTypeConfig[] = [
|
||||
{
|
||||
id: 'acme',
|
||||
id: 'ACME',
|
||||
name: 'ACME',
|
||||
description: "Let's Encrypt, ZeroSSL, or any ACME-compatible CA",
|
||||
icon: '\uD83D\uDD12',
|
||||
@@ -64,7 +68,7 @@ export const issuerTypes: IssuerTypeConfig[] = [
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'local',
|
||||
id: 'GenericCA',
|
||||
name: 'Local CA',
|
||||
description: 'Self-signed or subordinate CA for internal certificates',
|
||||
icon: '\uD83C\uDFE0',
|
||||
@@ -74,14 +78,15 @@ export const issuerTypes: IssuerTypeConfig[] = [
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'stepca',
|
||||
id: 'StepCA',
|
||||
name: 'step-ca',
|
||||
description: 'Smallstep private CA with JWK provisioner auth',
|
||||
icon: '\uD83D\uDC63',
|
||||
configFields: [
|
||||
{ key: 'ca_url', label: 'CA URL', placeholder: 'https://ca.example.com', required: true },
|
||||
{ key: 'provisioner_name', label: 'Provisioner Name', placeholder: 'my-provisioner', required: true },
|
||||
{ key: 'provisioner_key', label: 'Provisioner Key (JWK)', placeholder: '{...}', type: 'textarea', required: true, sensitive: true },
|
||||
{ key: 'provisioner_key_path', label: 'Provisioner Key Path', placeholder: '/path/to/provisioner.key', required: false, sensitive: true },
|
||||
{ key: 'provisioner_password', label: 'Provisioner Password', placeholder: 'Password for encrypted key', required: false, type: 'password', sensitive: true },
|
||||
],
|
||||
},
|
||||
{
|
||||
@@ -110,7 +115,7 @@ export const issuerTypes: IssuerTypeConfig[] = [
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'openssl',
|
||||
id: 'OpenSSL',
|
||||
name: 'OpenSSL/Custom',
|
||||
description: 'Script-based signing with your own CA',
|
||||
icon: '\uD83D\uDD27',
|
||||
@@ -149,6 +154,19 @@ export const issuerTypes: IssuerTypeConfig[] = [
|
||||
{ key: 'ttl', label: 'Default TTL', required: false, placeholder: '8760h' },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'AWSACMPCA',
|
||||
name: 'AWS ACM Private CA',
|
||||
description: 'AWS Certificate Manager Private Certificate Authority \u2014 managed private CA on AWS',
|
||||
icon: '\u2601\uFE0F',
|
||||
configFields: [
|
||||
{ key: 'region', label: 'AWS Region', required: true, placeholder: 'us-east-1' },
|
||||
{ key: 'ca_arn', label: 'CA ARN', required: true, placeholder: 'arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/...' },
|
||||
{ key: 'signing_algorithm', label: 'Signing Algorithm', required: false, type: 'select', options: ['SHA256WITHRSA', 'SHA384WITHRSA', 'SHA512WITHRSA', 'SHA256WITHECDSA', 'SHA384WITHECDSA', 'SHA512WITHECDSA'], defaultValue: 'SHA256WITHRSA' },
|
||||
{ key: 'validity_days', label: 'Validity (days)', required: false, type: 'number', placeholder: '365' },
|
||||
{ key: 'template_arn', label: 'Template ARN (optional)', required: false, placeholder: 'arn:aws:acm-pca:...:template/...' },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'entrust',
|
||||
name: 'Entrust',
|
||||
@@ -188,7 +206,10 @@ export function getIssuerCatalogStatus(
|
||||
}
|
||||
// Match both the canonical id and common aliases
|
||||
const aliases: Record<string, string[]> = {
|
||||
local: ['local', 'local_ca'],
|
||||
GenericCA: ['GenericCA', 'local', 'local_ca'],
|
||||
ACME: ['ACME', 'acme'],
|
||||
StepCA: ['StepCA', 'stepca'],
|
||||
OpenSSL: ['OpenSSL', 'openssl'],
|
||||
};
|
||||
const matchIds = aliases[t.id] || [t.id];
|
||||
const matching = configuredIssuers.filter(i => matchIds.includes(i.type));
|
||||
|
||||
@@ -61,7 +61,7 @@ export default function AgentDetailPage() {
|
||||
);
|
||||
}
|
||||
|
||||
const health = agent.status || heartbeatStatus(agent.last_heartbeat);
|
||||
const health = agent.status || heartbeatStatus(agent.last_heartbeat_at);
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -82,10 +82,10 @@ export default function AgentDetailPage() {
|
||||
<InfoRow label="IP Address" value={<span className="font-mono text-xs">{agent.ip_address || '—'}</span>} />
|
||||
<InfoRow label="Version" value={agent.version || '—'} />
|
||||
<InfoRow label="Last Heartbeat" value={
|
||||
agent.last_heartbeat ? (
|
||||
agent.last_heartbeat_at ? (
|
||||
<span>
|
||||
{timeAgo(agent.last_heartbeat)}
|
||||
<span className="text-ink-faint ml-2 text-xs">{formatDateTime(agent.last_heartbeat)}</span>
|
||||
{timeAgo(agent.last_heartbeat_at)}
|
||||
<span className="text-ink-faint ml-2 text-xs">{formatDateTime(agent.last_heartbeat_at)}</span>
|
||||
</span>
|
||||
) : '—'
|
||||
} />
|
||||
|
||||
@@ -39,7 +39,7 @@ export default function AgentsPage() {
|
||||
{
|
||||
key: 'status',
|
||||
label: 'Health',
|
||||
render: (a) => <StatusBadge status={a.status || heartbeatStatus(a.last_heartbeat)} />,
|
||||
render: (a) => <StatusBadge status={a.status || heartbeatStatus(a.last_heartbeat_at)} />,
|
||||
},
|
||||
{ key: 'hostname', label: 'Hostname', render: (a) => <span className="text-ink-muted font-mono text-xs">{a.hostname || '—'}</span> },
|
||||
{ key: 'os', label: 'OS / Arch', render: (a) => <span className="text-ink-muted text-xs">{a.os && a.architecture ? `${a.os}/${a.architecture}` : a.os || '—'}</span> },
|
||||
@@ -48,7 +48,7 @@ export default function AgentsPage() {
|
||||
{
|
||||
key: 'heartbeat',
|
||||
label: 'Last Heartbeat',
|
||||
render: (a) => <span className="text-ink-muted text-xs">{timeAgo(a.last_heartbeat)}</span>,
|
||||
render: (a) => <span className="text-ink-muted text-xs">{timeAgo(a.last_heartbeat_at)}</span>,
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ function TimelineStep({ label, status, time, isLast }: { label: string; status:
|
||||
);
|
||||
}
|
||||
|
||||
function DeploymentTimeline({ certId, certStatus, createdAt, issuedAt }: { certId: string; certStatus: string; createdAt: string; issuedAt: string }) {
|
||||
function DeploymentTimeline({ certId, certStatus, createdAt, issuedAt }: { certId: string; certStatus: string; createdAt: string; issuedAt?: string }) {
|
||||
const { data: jobsData } = useQuery({
|
||||
queryKey: ['jobs', { certificate_id: certId }],
|
||||
queryFn: () => getJobs({ certificate_id: certId }),
|
||||
@@ -372,6 +372,12 @@ export default function CertificateDetailPage() {
|
||||
);
|
||||
}
|
||||
|
||||
// Derive certificate metadata from latest version (backend doesn't include these on the cert object)
|
||||
const latestVersion = versions?.data?.[0];
|
||||
const serialNumber = cert.serial_number || latestVersion?.serial_number;
|
||||
const fingerprintSha256 = cert.fingerprint_sha256 || latestVersion?.fingerprint_sha256;
|
||||
const issuedAt = cert.issued_at || latestVersion?.not_before;
|
||||
|
||||
const days = daysUntil(cert.expires_at);
|
||||
const isRevoked = cert.status === 'Revoked';
|
||||
const isArchived = cert.status === 'Archived';
|
||||
@@ -492,7 +498,7 @@ export default function CertificateDetailPage() {
|
||||
)}
|
||||
|
||||
{/* Deployment Status Timeline */}
|
||||
<DeploymentTimeline certId={id!} certStatus={cert.status} createdAt={cert.created_at} issuedAt={cert.issued_at} />
|
||||
<DeploymentTimeline certId={id!} certStatus={cert.status} createdAt={cert.created_at} issuedAt={issuedAt} />
|
||||
|
||||
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
|
||||
{/* Certificate Info */}
|
||||
@@ -518,9 +524,9 @@ export default function CertificateDetailPage() {
|
||||
})}
|
||||
</span>
|
||||
) : '—'} />
|
||||
<InfoRow label="Serial Number" value={cert.serial_number || '—'} />
|
||||
<InfoRow label="Serial Number" value={serialNumber || '—'} />
|
||||
<InfoRow label="Fingerprint" value={
|
||||
cert.fingerprint ? <span className="font-mono text-xs">{cert.fingerprint.slice(0, 24)}...</span> : '—'
|
||||
fingerprintSha256 ? <span className="font-mono text-xs">{fingerprintSha256.slice(0, 24)}...</span> : '—'
|
||||
} />
|
||||
<InfoRow label="Key Algorithm" value={cert.key_algorithm || '—'} />
|
||||
<InfoRow label="Key Size" value={cert.key_size ? `${cert.key_size} bits` : '—'} />
|
||||
@@ -556,7 +562,7 @@ export default function CertificateDetailPage() {
|
||||
{/* Lifecycle */}
|
||||
<div className="bg-surface border border-surface-border rounded p-5 shadow-sm">
|
||||
<h3 className="text-sm font-semibold text-ink-muted mb-4">Lifecycle</h3>
|
||||
<InfoRow label="Issued" value={formatDate(cert.issued_at)} />
|
||||
<InfoRow label="Issued" value={formatDate(issuedAt)} />
|
||||
<InfoRow label="Expires" value={
|
||||
<span className={isRevoked ? 'text-red-600 line-through' : expiryColor(days)}>
|
||||
{formatDate(cert.expires_at)} ({days <= 0 ? 'expired' : `${days} days`})
|
||||
@@ -615,7 +621,7 @@ export default function CertificateDetailPage() {
|
||||
<div key={v.id} className="flex items-center justify-between py-2 border-b border-surface-border/50 last:border-0">
|
||||
<div>
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="text-sm text-ink">Version {v.version}</span>
|
||||
<span className="text-sm text-ink">Version {versions.data.length - idx}</span>
|
||||
{idx === 0 && <span className="text-xs bg-brand-100 text-brand-700 px-1.5 py-0.5 rounded">Current</span>}
|
||||
</div>
|
||||
<div className="text-xs text-ink-faint font-mono">{v.serial_number}</div>
|
||||
|
||||
@@ -114,9 +114,9 @@ export default function JobDetailPage() {
|
||||
} />
|
||||
)}
|
||||
<InfoRow label="Attempts" value={`${job.attempts} / ${job.max_attempts}`} />
|
||||
{job.error_message && (
|
||||
{job.last_error && (
|
||||
<InfoRow label="Error" value={
|
||||
<span className="text-red-600 text-xs">{job.error_message}</span>
|
||||
<span className="text-red-600 text-xs">{job.last_error}</span>
|
||||
} />
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -139,9 +139,9 @@ export default function JobsPage() {
|
||||
{
|
||||
key: 'error',
|
||||
label: 'Error',
|
||||
render: (j) => j.status === 'Failed' && j.error_message ? (
|
||||
<span className="text-xs text-red-600 truncate max-w-[200px] inline-block" title={j.error_message}>
|
||||
{j.error_message.length > 80 ? j.error_message.substring(0, 80) + '...' : j.error_message}
|
||||
render: (j) => j.status === 'Failed' && j.last_error ? (
|
||||
<span className="text-xs text-red-600 truncate max-w-[200px] inline-block" title={j.last_error}>
|
||||
{j.last_error.length > 80 ? j.last_error.substring(0, 80) + '...' : j.last_error}
|
||||
</span>
|
||||
) : <span className="text-xs text-ink-faint">—</span>,
|
||||
},
|
||||
|
||||
@@ -115,6 +115,15 @@ function IssuerStep({ onNext, onSkip, onIssuerCreated }: {
|
||||
const [selectedType, setSelectedType] = useState<string | null>(null);
|
||||
const [configValues, setConfigValues] = useState<Record<string, unknown>>({});
|
||||
const [issuerName, setIssuerName] = useState('');
|
||||
|
||||
// Pre-populate default values when a type is selected (matches IssuersPage behavior)
|
||||
function handleTypeSelect(typeId: string) {
|
||||
setSelectedType(typeId);
|
||||
const tc = issuerTypes.find(t => t.id === typeId);
|
||||
const defaults: Record<string, unknown> = {};
|
||||
tc?.configFields.forEach(f => { if (f.defaultValue !== undefined) defaults[f.key] = f.defaultValue; });
|
||||
setConfigValues(defaults);
|
||||
}
|
||||
const [error, setError] = useState('');
|
||||
const [testResult, setTestResult] = useState<{ ok: boolean; msg: string } | null>(null);
|
||||
const [createdIssuer, setCreatedIssuer] = useState<Issuer | null>(null);
|
||||
@@ -196,7 +205,7 @@ function IssuerStep({ onNext, onSkip, onIssuerCreated }: {
|
||||
{issuerTypes.filter(t => !t.comingSoon).map((type: IssuerTypeConfig) => (
|
||||
<button
|
||||
key={type.id}
|
||||
onClick={() => setSelectedType(type.id)}
|
||||
onClick={() => handleTypeSelect(type.id)}
|
||||
className="p-4 border border-surface-border rounded-lg hover:border-brand-500 hover:bg-surface-muted transition-all text-left"
|
||||
>
|
||||
<div className="flex items-center gap-2">
|
||||
@@ -219,7 +228,7 @@ function IssuerStep({ onNext, onSkip, onIssuerCreated }: {
|
||||
return (
|
||||
<div>
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<button onClick={() => { setSelectedType(null); setConfigValues({}); setError(''); }}
|
||||
<button onClick={() => { setSelectedType(null); setConfigValues({}); setIssuerName(''); setError(''); }}
|
||||
className="text-ink-muted hover:text-ink transition-colors">
|
||||
<svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2}>
|
||||
<path strokeLinecap="round" strokeLinejoin="round" d="M15 19l-7-7 7-7" />
|
||||
@@ -289,28 +298,27 @@ function AgentStep({ onNext, onSkip }: { onNext: () => void; onSkip: () => void
|
||||
const commands: Record<string, { code: string; label: string }> = {
|
||||
linux: {
|
||||
label: 'Install via shell script (systemd service)',
|
||||
code: `curl -sSL https://raw.githubusercontent.com/shankar0123/certctl/master/install-agent.sh | bash
|
||||
code: `# Non-interactive install (recommended for curl | bash):
|
||||
curl -sSL https://raw.githubusercontent.com/shankar0123/certctl/master/install-agent.sh \\
|
||||
| sudo bash -s -- \\
|
||||
--server-url ${serverUrl} \\
|
||||
--api-key ${apiKey}
|
||||
|
||||
# Then configure:
|
||||
sudo systemctl edit certctl-agent
|
||||
# Add:
|
||||
# [Service]
|
||||
# Environment="CERTCTL_SERVER_URL=${serverUrl}"
|
||||
# Environment="CERTCTL_API_KEY=${apiKey}"
|
||||
|
||||
sudo systemctl restart certctl-agent`,
|
||||
# The script downloads the agent binary, writes /etc/certctl/agent.env,
|
||||
# installs /etc/systemd/system/certctl-agent.service, and starts it.
|
||||
# Check status with: sudo systemctl status certctl-agent`,
|
||||
},
|
||||
macos: {
|
||||
label: 'Install via shell script (launchd service)',
|
||||
code: `curl -sSL https://raw.githubusercontent.com/shankar0123/certctl/master/install-agent.sh | bash
|
||||
code: `# Non-interactive install (recommended for curl | bash):
|
||||
curl -sSL https://raw.githubusercontent.com/shankar0123/certctl/master/install-agent.sh \\
|
||||
| bash -s -- \\
|
||||
--server-url ${serverUrl} \\
|
||||
--api-key ${apiKey}
|
||||
|
||||
# Then configure:
|
||||
# Edit /Library/LaunchDaemons/com.certctl.agent.plist
|
||||
# Set CERTCTL_SERVER_URL to ${serverUrl}
|
||||
# Set CERTCTL_API_KEY to ${apiKey}
|
||||
|
||||
sudo launchctl unload /Library/LaunchDaemons/com.certctl.agent.plist
|
||||
sudo launchctl load /Library/LaunchDaemons/com.certctl.agent.plist`,
|
||||
# The script writes ~/.certctl/agent.env and loads
|
||||
# ~/Library/LaunchAgents/com.certctl.agent.plist.
|
||||
# Check status with: launchctl list | grep certctl`,
|
||||
},
|
||||
docker: {
|
||||
label: 'Run as Docker container',
|
||||
|
||||
@@ -24,6 +24,7 @@ const typeLabels: Record<string, string> = {
|
||||
SSH: 'SSH',
|
||||
WinCertStore: 'Windows Cert Store',
|
||||
JavaKeystore: 'Java Keystore',
|
||||
KubernetesSecrets: 'Kubernetes Secrets',
|
||||
};
|
||||
|
||||
function InfoRow({ label, value }: { label: string; value: React.ReactNode }) {
|
||||
@@ -231,7 +232,7 @@ export default function TargetDetailPage() {
|
||||
{target.config && Object.keys(target.config).length > 0 ? (
|
||||
<div className="space-y-0">
|
||||
{Object.entries(target.config).map(([key, val]) => {
|
||||
const sensitiveKeys = ['password', 'secret', 'token', 'key', 'winrm_password', 'keystore_password'];
|
||||
const sensitiveKeys = ['password', 'secret', 'token', 'key', 'passphrase', 'winrm_password', 'keystore_password'];
|
||||
const isSensitive = sensitiveKeys.some(s => key.toLowerCase().includes(s));
|
||||
const displayVal = isSensitive && val ? '********' : String(val);
|
||||
return (
|
||||
|
||||
@@ -24,6 +24,7 @@ const typeLabels: Record<string, string> = {
|
||||
SSH: 'SSH',
|
||||
WinCertStore: 'Windows Cert Store',
|
||||
JavaKeystore: 'Java Keystore',
|
||||
KubernetesSecrets: 'Kubernetes Secrets',
|
||||
};
|
||||
|
||||
const TARGET_TYPES = [
|
||||
@@ -40,6 +41,7 @@ const TARGET_TYPES = [
|
||||
{ value: 'SSH', label: 'SSH', description: 'Agentless deployment via SSH/SFTP — deploy to any Linux/Unix server without installing an agent' },
|
||||
{ value: 'WinCertStore', label: 'Windows Cert Store', description: 'Import certificates into Windows Certificate Store for Exchange, RDP, SQL Server, ADFS' },
|
||||
{ value: 'JavaKeystore', label: 'Java Keystore', description: 'Deploy to JKS/PKCS#12 keystores for Tomcat, Jetty, Kafka, Elasticsearch, and JVM services' },
|
||||
{ value: 'KubernetesSecrets', label: 'Kubernetes Secrets', description: 'Deploy as kubernetes.io/tls Secrets for Ingress controllers, service meshes, and workloads' },
|
||||
];
|
||||
|
||||
const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: string; required?: boolean }[]> = {
|
||||
@@ -47,18 +49,20 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
|
||||
{ key: 'cert_path', label: 'Certificate Path', placeholder: '/etc/nginx/ssl/cert.pem', required: true },
|
||||
{ key: 'key_path', label: 'Key Path', placeholder: '/etc/nginx/ssl/key.pem', required: true },
|
||||
{ key: 'chain_path', label: 'Chain Path', placeholder: '/etc/nginx/ssl/chain.pem' },
|
||||
{ key: 'reload_cmd', label: 'Reload Command', placeholder: 'nginx -t && systemctl reload nginx' },
|
||||
{ key: 'reload_command', label: 'Reload Command', placeholder: 'nginx -s reload' },
|
||||
{ key: 'validate_command', label: 'Validate Command', placeholder: 'nginx -t' },
|
||||
],
|
||||
Apache: [
|
||||
{ key: 'cert_path', label: 'Certificate Path', placeholder: '/etc/apache2/ssl/cert.pem', required: true },
|
||||
{ key: 'key_path', label: 'Key Path', placeholder: '/etc/apache2/ssl/key.pem', required: true },
|
||||
{ key: 'chain_path', label: 'Chain Path', placeholder: '/etc/apache2/ssl/chain.pem' },
|
||||
{ key: 'reload_cmd', label: 'Reload Command', placeholder: 'apachectl configtest && apachectl graceful' },
|
||||
{ key: 'reload_command', label: 'Reload Command', placeholder: 'apachectl graceful' },
|
||||
{ key: 'validate_command', label: 'Validate Command', placeholder: 'apachectl configtest' },
|
||||
],
|
||||
HAProxy: [
|
||||
{ key: 'pem_path', label: 'Combined PEM Path', placeholder: '/etc/haproxy/certs/combined.pem', required: true },
|
||||
{ key: 'reload_cmd', label: 'Reload Command', placeholder: 'systemctl reload haproxy' },
|
||||
{ key: 'validate_cmd', label: 'Validate Command (optional)', placeholder: 'haproxy -c -f /etc/haproxy/haproxy.cfg' },
|
||||
{ key: 'reload_command', label: 'Reload Command', placeholder: 'systemctl reload haproxy' },
|
||||
{ key: 'validate_command', label: 'Validate Command (optional)', placeholder: 'haproxy -c -f /etc/haproxy/haproxy.cfg' },
|
||||
],
|
||||
Traefik: [
|
||||
{ key: 'cert_dir', label: 'Certificate Directory', placeholder: '/etc/traefik/certs', required: true },
|
||||
@@ -87,6 +91,7 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
|
||||
{ key: 'validate_command', label: 'Validate Command', placeholder: 'postfix check' },
|
||||
],
|
||||
Dovecot: [
|
||||
{ key: 'mode', label: 'Mode', placeholder: 'dovecot (auto-set)' },
|
||||
{ key: 'cert_path', label: 'Certificate Path', placeholder: '/etc/dovecot/certs/cert.pem' },
|
||||
{ key: 'key_path', label: 'Key Path', placeholder: '/etc/dovecot/certs/key.pem' },
|
||||
{ key: 'chain_path', label: 'Chain Path (optional)', placeholder: '/etc/dovecot/certs/chain.pem' },
|
||||
@@ -104,6 +109,7 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
|
||||
{ key: 'timeout', label: 'Timeout (seconds)', placeholder: '30' },
|
||||
],
|
||||
IIS: [
|
||||
{ key: 'hostname', label: 'Target Hostname', placeholder: 'iis-server.example.com' },
|
||||
{ key: 'site_name', label: 'IIS Site Name', placeholder: 'Default Web Site', required: true },
|
||||
{ key: 'cert_store', label: 'Certificate Store', placeholder: 'My', required: true },
|
||||
{ key: 'port', label: 'HTTPS Port', placeholder: '443' },
|
||||
@@ -111,12 +117,13 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
|
||||
{ key: 'binding_info', label: 'Host Header (SNI)', placeholder: 'www.example.com' },
|
||||
{ key: 'sni', label: 'Enable SNI', placeholder: 'true or false' },
|
||||
{ key: 'mode', label: 'Deployment Mode', placeholder: 'local (default) or winrm' },
|
||||
{ key: 'winrm.winrm_host', label: 'WinRM Host (remote mode)', placeholder: 'iis-server.example.com' },
|
||||
{ key: 'winrm.winrm_port', label: 'WinRM Port', placeholder: '5985 (HTTP) or 5986 (HTTPS)' },
|
||||
{ key: 'winrm.winrm_username', label: 'WinRM Username', placeholder: 'Administrator' },
|
||||
{ key: 'winrm.winrm_password', label: 'WinRM Password', placeholder: '(sensitive)' },
|
||||
{ key: 'winrm.winrm_https', label: 'WinRM Use HTTPS', placeholder: 'true or false' },
|
||||
{ key: 'winrm.winrm_insecure', label: 'WinRM Skip TLS Verify', placeholder: 'false' },
|
||||
{ key: 'winrm_host', label: 'WinRM Host (remote mode)', placeholder: 'iis-server.example.com' },
|
||||
{ key: 'winrm_port', label: 'WinRM Port', placeholder: '5985 (HTTP) or 5986 (HTTPS)' },
|
||||
{ key: 'winrm_username', label: 'WinRM Username', placeholder: 'Administrator' },
|
||||
{ key: 'winrm_password', label: 'WinRM Password', placeholder: '(sensitive)' },
|
||||
{ key: 'winrm_https', label: 'WinRM Use HTTPS', placeholder: 'true or false' },
|
||||
{ key: 'winrm_insecure', label: 'WinRM Skip TLS Verify', placeholder: 'false' },
|
||||
{ key: 'winrm_timeout', label: 'WinRM Timeout (seconds)', placeholder: '60' },
|
||||
],
|
||||
SSH: [
|
||||
{ key: 'host', label: 'SSH Host', placeholder: '192.168.1.100 or server.example.com', required: true },
|
||||
@@ -124,10 +131,14 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
|
||||
{ key: 'user', label: 'SSH Username', placeholder: 'root or certctl', required: true },
|
||||
{ key: 'auth_method', label: 'Auth Method', placeholder: 'key (default) or password' },
|
||||
{ key: 'private_key_path', label: 'Private Key Path', placeholder: '/home/certctl/.ssh/id_ed25519' },
|
||||
{ key: 'private_key', label: 'Inline Private Key PEM', placeholder: 'Paste PEM key (alternative to path)' },
|
||||
{ key: 'password', label: 'SSH Password', placeholder: 'Leave empty for key auth' },
|
||||
{ key: 'passphrase', label: 'Key Passphrase', placeholder: 'For encrypted private keys' },
|
||||
{ key: 'cert_path', label: 'Remote Certificate Path', placeholder: '/etc/ssl/certs/cert.pem', required: true },
|
||||
{ key: 'key_path', label: 'Remote Key Path', placeholder: '/etc/ssl/private/key.pem', required: true },
|
||||
{ key: 'chain_path', label: 'Remote Chain Path (optional)', placeholder: '/etc/ssl/certs/chain.pem' },
|
||||
{ key: 'cert_mode', label: 'Cert File Permissions', placeholder: '0644 (default)' },
|
||||
{ key: 'key_mode', label: 'Key File Permissions', placeholder: '0600 (default)' },
|
||||
{ key: 'reload_command', label: 'Reload Command (optional)', placeholder: 'systemctl reload nginx' },
|
||||
{ key: 'timeout', label: 'Connection Timeout (seconds)', placeholder: '30 (default)' },
|
||||
],
|
||||
@@ -141,15 +152,24 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
|
||||
{ key: 'winrm_port', label: 'WinRM Port', placeholder: '5985 (HTTP) or 5986 (HTTPS)' },
|
||||
{ key: 'winrm_username', label: 'WinRM Username', placeholder: 'Administrator' },
|
||||
{ key: 'winrm_password', label: 'WinRM Password', placeholder: '(sensitive)' },
|
||||
{ key: 'winrm_https', label: 'WinRM Use HTTPS', placeholder: 'true or false' },
|
||||
{ key: 'winrm_insecure', label: 'WinRM Skip TLS Verify', placeholder: 'false' },
|
||||
],
|
||||
JavaKeystore: [
|
||||
{ key: 'keystore_path', label: 'Keystore Path', placeholder: '/opt/app/conf/keystore.p12', required: true },
|
||||
{ key: 'keystore_password', label: 'Keystore Password', placeholder: 'changeit', required: true },
|
||||
{ key: 'keystore_type', label: 'Keystore Type', placeholder: 'PKCS12 (default) or JKS' },
|
||||
{ key: 'alias', label: 'Key Alias', placeholder: 'server (default)' },
|
||||
{ key: 'create_keystore', label: 'Create Keystore If Missing', placeholder: 'true (default)' },
|
||||
{ key: 'reload_command', label: 'Reload Command (optional)', placeholder: 'systemctl restart tomcat' },
|
||||
{ key: 'keytool_path', label: 'Keytool Path (optional)', placeholder: 'keytool (default, from PATH)' },
|
||||
],
|
||||
KubernetesSecrets: [
|
||||
{ key: 'namespace', label: 'Namespace', placeholder: 'default', required: true },
|
||||
{ key: 'secret_name', label: 'Secret Name', placeholder: 'my-tls-secret', required: true },
|
||||
{ key: 'labels', label: 'Labels (JSON)', placeholder: '{"app": "my-app"}' },
|
||||
{ key: 'kubeconfig_path', label: 'Kubeconfig Path (optional)', placeholder: '/home/agent/.kube/config' },
|
||||
],
|
||||
};
|
||||
|
||||
function CreateTargetWizard({ onClose, onSuccess }: { onClose: () => void; onSuccess: () => void }) {
|
||||
@@ -160,12 +180,64 @@ function CreateTargetWizard({ onClose, onSuccess }: { onClose: () => void; onSuc
|
||||
const [config, setConfig] = useState<Record<string, string>>({});
|
||||
const [error, setError] = useState('');
|
||||
|
||||
// Fields that backends expect as boolean (Go bool)
|
||||
const BOOL_FIELDS = new Set([
|
||||
'sni', 'insecure', 'sds_config', 'remove_expired', 'create_keystore',
|
||||
'winrm_https', 'winrm_insecure',
|
||||
]);
|
||||
// Fields that backends expect as integer (Go int)
|
||||
const INT_FIELDS = new Set([
|
||||
'port', 'timeout', 'winrm_port', 'winrm_timeout', 'timeout_seconds',
|
||||
]);
|
||||
|
||||
// Coerce string form values to their Go types
|
||||
const coerceValue = (key: string, val: string): unknown => {
|
||||
if (BOOL_FIELDS.has(key)) return val === 'true';
|
||||
if (INT_FIELDS.has(key)) { const n = parseInt(val, 10); return isNaN(n) ? val : n; }
|
||||
return val;
|
||||
};
|
||||
|
||||
// Build config payload with type-specific transformations
|
||||
const buildConfigPayload = () => {
|
||||
const flat = Object.fromEntries(Object.entries(config).filter(([, v]) => v));
|
||||
|
||||
// Dovecot uses the same Postfix connector with mode="dovecot"
|
||||
if (targetType === 'Dovecot' && !flat['mode']) {
|
||||
flat['mode'] = 'dovecot';
|
||||
}
|
||||
|
||||
// IIS backend expects WinRM fields nested under "winrm" key
|
||||
if (targetType === 'IIS') {
|
||||
const iisWinrmKeys = ['winrm_host', 'winrm_port', 'winrm_username', 'winrm_password', 'winrm_https', 'winrm_insecure', 'winrm_timeout'];
|
||||
const winrmObj: Record<string, unknown> = {};
|
||||
const result: Record<string, unknown> = {};
|
||||
for (const [k, v] of Object.entries(flat)) {
|
||||
if (iisWinrmKeys.includes(k)) {
|
||||
winrmObj[k] = coerceValue(k, v);
|
||||
} else {
|
||||
result[k] = coerceValue(k, v);
|
||||
}
|
||||
}
|
||||
if (Object.keys(winrmObj).length > 0) {
|
||||
result['winrm'] = winrmObj;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// All other target types: coerce values to proper Go types
|
||||
const result: Record<string, unknown> = {};
|
||||
for (const [k, v] of Object.entries(flat)) {
|
||||
result[k] = coerceValue(k, v);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: () => createTarget({
|
||||
name,
|
||||
type: targetType,
|
||||
agent_id: agentId,
|
||||
config: Object.fromEntries(Object.entries(config).filter(([, v]) => v)),
|
||||
config: buildConfigPayload(),
|
||||
}),
|
||||
onSuccess: () => onSuccess(),
|
||||
onError: (err: Error) => setError(err.message),
|
||||
|
||||
Reference in New Issue
Block a user