Compare commits

..

6 Commits

Author SHA1 Message Date
shankar0123 dedf7fa3a9 docs: add quick-start jump link near top of README
Adds a one-line "Ready to try it?" link right after the maintainer
callout, before the longer prose sections. Gives scanners an immediate
exit to install instructions without rearranging the README's
explain → show → install flow.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 21:38:34 -04:00
shankar0123 4b5927dfff docs: expand README documentation table and fix orphaned doc links
- README: Add 7 missing docs to documentation table (MCP server, OpenAPI
  guide, migration guides for certbot/acme.sh/cert-manager, test
  environment, testing guide). Fix connector reference description to
  remove stale counts. Link OpenAPI guide instead of raw YAML.
- architecture.md: Add cross-references to testing-guide.md and
  test-env.md from testing strategy section and What's Next links.
  These were the only two orphaned docs with zero inbound references.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 21:37:47 -04:00
shankar0123 cc03f55006 docs: comprehensive documentation audit — fix stale counts, V2/V3 matrix, connector status
- features.md: Fix Feature Matrix to correctly show all V2 Free features
  (F5/IIS/WinCertStore/JavaKeystore as Implemented, not Stub; Vault/DigiCert/
  Sectigo/GoogleCAS as V2 Free, not V3 Paid). Add missing shipped features
  (EST, verification, export, S/MIME, ARI, digest, Helm, onboarding). Update
  issuer count to 9, target count to 13.
- architecture.md: Fix F5/IIS from "interface only, implementation planned"
  to implemented. Add all 13 target connectors to built-in targets list.
- why-certctl.md: Add Sectigo and Google CAS to issuer list (7→9). Fix
  target count (10→13). Remove hardcoded endpoint/operation counts.
- connectors.md: Fix F5 BIG-IP TOC entry from "Interface Only" to
  "Implemented". Remove dead "Planned Issuers" TOC link.
- README.md: Remove competitor product names (CertKit, KeyTalk). Remove
  hardcoded dashboard page count. Remove hardcoded endpoint counts. Fix V4
  roadmap to remove already-shipped issuers (Sectigo, Google CAS).
- Remove hardcoded MCP tool counts (78/80) across 8 files (mcp.md,
  architecture.md, features.md, testing-guide.md, concepts.md, quickstart.md,
  demo-advanced.md, why-certctl.md). Replace with "REST API exposed via MCP"
  to avoid future drift.
- quickstart.md: Docker Compose environments table (from previous session).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 21:33:12 -04:00
shankar0123 93e1dc598c fix: resolve frontend-to-backend mapping gaps across API types, config fields, and issuer IDs
Full audit of all ~100 backend API endpoints against frontend client functions
and TypeScript interfaces. Fixes field name mismatches, missing client functions,
phantom interface fields, type coercion for Go bool/int config fields, and
issuer type ID alignment with backend domain constants.

Backend:
- issuer.go/target.go: GUI-created entities default enabled=true (Go bool
  zero value was overriding DB DEFAULT)

Frontend types (types.ts):
- Certificate: fingerprint→fingerprint_sha256, phantom fields made optional
- CertificateVersion: fingerprint→fingerprint_sha256, chain_pem→pem_chain,
  removed phantom version/cert_pem fields
- Job: error_message→last_error (matches Go json tag)

Frontend client (client.ts):
- Added getNotification(id) and getAuditEvent(id) for existing backend routes

Frontend pages:
- CertificateDetailPage: derives serial/fingerprint/issuedAt from latest
  CertificateVersion instead of empty Certificate fields
- JobsPage/JobDetailPage: error_message→last_error
- TargetsPage: reload_cmd→reload_command, validate_cmd→validate_command,
  added missing config fields per backend structs (validate_command for
  NGINX/Apache, hostname/winrm_timeout for IIS, private_key/passphrase/
  cert_mode/key_mode for SSH, winrm_https/winrm_insecure for WinCertStore,
  create_keystore for JavaKeystore, mode for Dovecot), type coercion via
  buildConfigPayload() with BOOL_FIELDS/INT_FIELDS sets, IIS WinRM nesting
- TargetDetailPage: added passphrase to sensitiveKeys redaction
- issuerTypes.ts: type IDs aligned to backend constants (acme→ACME,
  local→GenericCA, stepca→StepCA, openssl→OpenSSL), backward compat aliases
  preserved, step-ca config fields updated to match backend struct

Utilities (utils.ts):
- formatDate/formatDateTime accept string|undefined|null

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 21:09:48 -04:00
shankar0123 25f33b830f fix: resolve golangci-lint issues in wincertstore connector
Remove unnecessary fmt.Sprintf wrapping a string literal (staticcheck S1039),
remove unused tempFileForPFX function, and clean up unused os import.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 19:16:34 -04:00
shankar0123 7d6ef44e21 feat(M46): Windows Certificate Store + Java Keystore target connectors, shared certutil package
Extract shared certutil helpers (CreatePFX, ParsePrivateKey, ComputeThumbprint,
GenerateRandomPassword, ParseCertificatePEM) from IIS connector for reuse.
Add WinCertStore connector (PowerShell Import-PfxCertificate, dual local/WinRM
mode, configurable store/location, expired cert cleanup) and JavaKeystore
connector (PEM→PKCS#12→keytool pipeline, JKS/PKCS12 support, shell injection
prevention, path traversal protection). 53 new tests, all passing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-05 19:14:32 -04:00
32 changed files with 2256 additions and 218 deletions
+22 -11
View File
@@ -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. > **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 ## 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. 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. 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 ## 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). - **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. - **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,7 +70,7 @@ 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. - **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. - **Multiple interfaces for different workflows.** REST API 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.
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). 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).
@@ -158,16 +160,19 @@ cd certctl
docker compose -f deploy/docker-compose.yml up -d --build 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 7 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 [Quick Start Guide](docs/quickstart.md#docker-compose-environments) for details.
```bash ```bash
curl http://localhost:8443/health curl http://localhost:8443/health
# {"status":"healthy"} # {"status":"healthy"}
curl -s http://localhost:8443/api/v1/certificates | jq '.total'
# 32
``` ```
### Agent Install (One-Liner) ### Agent Install (One-Liner)
@@ -221,9 +226,15 @@ Each directory contains a `docker-compose.yml` and a `README.md` explaining the
| [Advanced Demo](docs/demo-advanced.md) | Issue a certificate end-to-end with technical deep-dives | | [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 | | [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 | | [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 | | [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 ## CLI
@@ -303,7 +314,7 @@ Core lifecycle management — Local CA + ACME v2 issuers, NGINX target connector
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. 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 ### 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). 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 (Entrust, GlobalSign, EJBCA), and platform-scale features (Terraform provider, multi-tenancy, HSM support).
## License ## License
+1 -1
View File
@@ -2669,7 +2669,7 @@ components:
# ─── Targets ───────────────────────────────────────────────────── # ─── Targets ─────────────────────────────────────────────────────
TargetType: TargetType:
type: string type: string
enum: [NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS, F5, SSH] enum: [NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, Postfix, Dovecot, IIS, F5, SSH, WinCertStore, JavaKeystore]
DeploymentTarget: DeploymentTarget:
type: object type: object
+20
View File
@@ -33,6 +33,8 @@ import (
pf "github.com/shankar0123/certctl/internal/connector/target/postfix" pf "github.com/shankar0123/certctl/internal/connector/target/postfix"
sshconn "github.com/shankar0123/certctl/internal/connector/target/ssh" sshconn "github.com/shankar0123/certctl/internal/connector/target/ssh"
"github.com/shankar0123/certctl/internal/connector/target/f5" "github.com/shankar0123/certctl/internal/connector/target/f5"
jks "github.com/shankar0123/certctl/internal/connector/target/javakeystore"
wcs "github.com/shankar0123/certctl/internal/connector/target/wincertstore"
"github.com/shankar0123/certctl/internal/connector/target/haproxy" "github.com/shankar0123/certctl/internal/connector/target/haproxy"
"github.com/shankar0123/certctl/internal/connector/target/iis" "github.com/shankar0123/certctl/internal/connector/target/iis"
"github.com/shankar0123/certctl/internal/connector/target/nginx" "github.com/shankar0123/certctl/internal/connector/target/nginx"
@@ -657,6 +659,24 @@ func (a *Agent) createTargetConnector(targetType string, configJSON json.RawMess
} }
return sshconn.New(&cfg, a.logger) return sshconn.New(&cfg, a.logger)
case "WinCertStore":
var cfg wcs.Config
if len(configJSON) > 0 {
if err := json.Unmarshal(configJSON, &cfg); err != nil {
return nil, fmt.Errorf("invalid WinCertStore config: %w", err)
}
}
return wcs.New(&cfg, a.logger)
case "JavaKeystore":
var cfg jks.Config
if len(configJSON) > 0 {
if err := json.Unmarshal(configJSON, &cfg); err != nil {
return nil, fmt.Errorf("invalid JavaKeystore config: %w", err)
}
}
return jks.New(&cfg, a.logger), nil
default: default:
return nil, fmt.Errorf("unsupported target type: %s", targetType) return nil, fmt.Errorf("unsupported target type: %s", targetType)
} }
+8 -4
View File
@@ -122,7 +122,7 @@ The server exposes a REST API under `/api/v1/` and optionally serves the web das
### Agents ### 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) 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. 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.
@@ -602,7 +602,7 @@ 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. 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: **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).
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. 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.
@@ -810,7 +810,7 @@ flowchart LR
AI["AI Assistant\n(Claude, Cursor)"] -->|"stdio"| MCP["MCP Server\ncmd/mcp-server/"] AI["AI Assistant\n(Claude, Cursor)"] -->|"stdio"| MCP["MCP Server\ncmd/mcp-server/"]
MCP -->|"HTTP + Bearer token"| API["certctl REST API\n:8443"] MCP -->|"HTTP + Bearer token"| API["certctl REST API\n:8443"]
subgraph "78 MCP Tools" subgraph "MCP Tools"
T1["Certificate CRUD"] T1["Certificate CRUD"]
T2["Agent Management"] T2["Agent Management"]
T3["Job Operations"] T3["Job Operations"]
@@ -824,7 +824,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 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 ## CLI Tool
@@ -986,6 +986,8 @@ certctl is extensively tested across eight layers with CI-enforced coverage gate
**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 60%, handler 60%, domain 40%, middleware 50%). 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 ## What's Next
- [Quick Start](quickstart.md) — Get certctl running locally - [Quick Start](quickstart.md) — Get certctl running locally
@@ -994,3 +996,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 - [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 - [MCP Server Guide](mcp.md) — AI-native access to the API
- [OpenAPI Spec](openapi.md) — Full API reference and SDK generation - [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
+1 -1
View File
@@ -252,7 +252,7 @@ The CLI supports both table and JSON output formats (`--format table` or `--form
### MCP Server (AI Integration) ### 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. 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.
+64 -2
View File
@@ -13,7 +13,6 @@ Connectors extend certctl to integrate with external systems for certificate iss
- [OpenSSL / Custom CA](#openssl--custom-ca) - [OpenSSL / Custom CA](#openssl--custom-ca)
- [Revocation Across Issuers](#revocation-across-issuers) - [Revocation Across Issuers](#revocation-across-issuers)
- [EST Integration (GetCACertPEM)](#est-integration-getcacertpem) - [EST Integration (GetCACertPEM)](#est-integration-getcacertpem)
- [Planned Issuers](#planned-issuers)
- [Building a Custom Issuer](#building-a-custom-issuer) - [Building a Custom Issuer](#building-a-custom-issuer)
3. [Target Connector](#target-connector) 3. [Target Connector](#target-connector)
- [Interface](#interface-1) - [Interface](#interface-1)
@@ -24,9 +23,11 @@ Connectors extend certctl to integrate with external systems for certificate iss
- [Built-in: Envoy](#built-in-envoy) - [Built-in: Envoy](#built-in-envoy)
- [Built-in: Postfix / Dovecot](#built-in-postfix--dovecot) - [Built-in: Postfix / Dovecot](#built-in-postfix--dovecot)
- [Built-in: Caddy](#built-in-caddy) - [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) - [IIS (Implemented, Dual-Mode)](#iis-implemented-dual-mode)
- [SSH (Agentless Deployment)](#ssh-agentless-deployment) - [SSH (Agentless Deployment)](#ssh-agentless-deployment)
- [Windows Certificate Store](#windows-certificate-store)
- [Java Keystore (JKS / PKCS#12)](#java-keystore-jks--pkcs12)
4. [Notifier Connector](#notifier-connector) 4. [Notifier Connector](#notifier-connector)
- [Interface](#interface-2) - [Interface](#interface-2)
5. [Registering a Connector](#registering-a-connector) 5. [Registering a Connector](#registering-a-connector)
@@ -874,6 +875,67 @@ The SSH target connector enables agentless certificate deployment to any Linux/U
Location: `internal/connector/target/ssh/ssh.go` Location: `internal/connector/target/ssh/ssh.go`
### Windows Certificate Store
The Windows Certificate Store connector imports certificates into the Windows cert store via PowerShell, without managing IIS site bindings. Use this for non-IIS Windows services that read certificates from the cert store (Exchange, RDP, SQL Server, ADFS, etc.). Same injectable `PowerShellExecutor` pattern as the IIS connector, with optional WinRM proxy mode.
```json
{
"store_name": "My",
"store_location": "LocalMachine",
"friendly_name": "Production API Cert",
"remove_expired": true
}
```
| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `store_name` | string | `"My"` | Windows cert store name (My, Root, WebHosting, etc.) |
| `store_location` | string | `"LocalMachine"` | `"LocalMachine"` or `"CurrentUser"` |
| `friendly_name` | string | | Optional friendly name for the imported certificate |
| `remove_expired` | boolean | `false` | Remove expired certs with same CN after import |
| `mode` | string | `"local"` | `"local"` (agent-local) or `"winrm"` (remote) |
| `winrm_host` | string | | WinRM hostname (required for winrm mode) |
| `winrm_port` | number | 5985 | WinRM port (5985 HTTP, 5986 HTTPS) |
| `winrm_username` | string | | WinRM username (required for winrm mode) |
| `winrm_password` | string | | WinRM password (required for winrm mode) |
| `winrm_https` | boolean | `false` | Use HTTPS for WinRM |
| `winrm_insecure` | boolean | `false` | Skip TLS verification for WinRM |
Location: `internal/connector/target/wincertstore/wincertstore.go`
### Java Keystore (JKS / PKCS#12)
The Java Keystore connector deploys certificates to JKS or PKCS#12 keystores via the `keytool` CLI. This enables TLS cert deployment for Tomcat, Jetty, Kafka, Elasticsearch, and any JVM-based service. Flow: PEM to temp PKCS#12, then `keytool -importkeystore` into the target keystore.
```json
{
"keystore_path": "/opt/tomcat/conf/keystore.p12",
"keystore_password": "changeit",
"keystore_type": "PKCS12",
"alias": "server",
"reload_command": "systemctl restart tomcat"
}
```
| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `keystore_path` | string | *(required)* | Absolute path to the keystore file |
| `keystore_password` | string | *(required)* | Keystore password |
| `keystore_type` | string | `"PKCS12"` | `"PKCS12"` or `"JKS"` |
| `alias` | string | `"server"` | Key entry alias in the keystore |
| `reload_command` | string | | Optional command to run after keystore update |
| `create_keystore` | boolean | `true` | Create keystore if it doesn't exist |
| `keytool_path` | string | `"keytool"` | Override keytool binary path |
**Security:**
- Reload commands validated against shell injection via `validation.ValidateShellCommand()`
- Alias validated against injection (alphanumeric, hyphens, underscores only)
- Path traversal prevention on keystore path
- Transient PKCS#12 temp file cleaned up after import (even on error)
Location: `internal/connector/target/javakeystore/javakeystore.go`
## Notifier Connector ## Notifier Connector
Notifier connectors send alerts about certificate lifecycle events (expiration warnings, renewal success/failure, deployment status, policy violations). Notifier connectors send alerts about certificate lifecycle events (expiration warnings, renewal success/failure, deployment status, policy violations).
+1 -1
View File
@@ -981,7 +981,7 @@ export CERTCTL_API_KEY="test-key-123"
## Part 15: MCP Server for AI Integration (M18a) ## 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 ```bash
# Build the MCP server # Build the MCP server
+26 -23
View File
@@ -7,7 +7,7 @@ Complete reference of all features shipped in the V2 release (as of March 2026).
## API Surface ## API Surface
### Overview ### Overview
- **99 endpoints** across 23 resource domains under `/api/v1/` + `/.well-known/est/` - REST API across 23 resource domains under `/api/v1/` + `/.well-known/est/`
- REST API with HTTP semantics (GET, POST, PUT, DELETE) - REST API with HTTP semantics (GET, POST, PUT, DELETE)
- All endpoints require authentication by default (configurable) - All endpoints require authentication by default (configurable)
- OpenAPI 3.1 spec with full schema documentation - OpenAPI 3.1 spec with full schema documentation
@@ -1134,12 +1134,12 @@ The web dashboard is the primary operational interface for certctl. Built with *
## Integration Interfaces ## Integration Interfaces
### MCP Server (M18a) ### MCP Server (M18a)
**Separate binary** (`cmd/mcp-server/`) providing AI-native access to certctl via Claude, Cursor, OpenClaw. Instead of memorizing 91 API endpoints, ask your AI assistant "what certificates are expiring this week?" or "renew the API prod cert" and it translates to the right API calls. **Separate binary** (`cmd/mcp-server/`) providing AI-native access to certctl via Claude, Cursor, OpenClaw. Instead of memorizing API endpoints, ask your AI assistant "what certificates are expiring this week?" or "renew the API prod cert" and it translates to the right API calls.
- **Transport** — stdio (stdin/stdout) - **Transport** — stdio (stdin/stdout)
- **Protocol** — Model Context Protocol v1 - **Protocol** — Model Context Protocol v1
- **SDK** — Official `modelcontextprotocol/go-sdk` v1.4.1 - **SDK** — Official `modelcontextprotocol/go-sdk` v1.4.1
- **Tools** 78 MCP tools covering all API endpoints - **Tools** — MCP tools covering all API endpoints
- **Organization** — 16 resource domains (Certificates, Issuers, Targets, Agents, Jobs, etc.) - **Organization** — 16 resource domains (Certificates, Issuers, Targets, Agents, Jobs, etc.)
- **Authentication** — Bearer token via `CERTCTL_API_KEY` env var - **Authentication** — Bearer token via `CERTCTL_API_KEY` env var
- **Configuration**`CERTCTL_SERVER_URL` (e.g., http://localhost:8080) + `CERTCTL_API_KEY` - **Configuration**`CERTCTL_SERVER_URL` (e.g., http://localhost:8080) + `CERTCTL_API_KEY`
@@ -1439,8 +1439,8 @@ Each guide includes an evidence summary table mapping specific criteria to certc
| Feature | V2 | V3 (Paid) | Status | | Feature | V2 | V3 (Paid) | Status |
|---------|----|-----------|-| |---------|----|-----------|-|
| Certificate lifecycle (create/renew/revoke) | ✓ | ✓ | Shipped v1.0+ | | Certificate lifecycle (create/renew/revoke) | ✓ | ✓ | Shipped v1.0+ |
| 4 issuer connectors (Local CA, ACME, step-ca, OpenSSL) | ✓ | ✓ | Shipped | | 9 issuer connectors (Local CA, ACME, step-ca, OpenSSL, Vault PKI, DigiCert, Sectigo, Google CAS, EST) | ✓ | ✓ | Shipped |
| 3 target connectors (NGINX, Apache, HAProxy) | ✓ | ✓ | Shipped | | 13 target connectors (NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, IIS, F5, Postfix, Dovecot, SSH, WinCertStore, JavaKeystore) | ✓ | ✓ | Shipped |
| 6 notifier channels (Email, Webhook, Slack, Teams, PagerDuty, OpsGenie) | ✓ | ✓ | Shipped | | 6 notifier channels (Email, Webhook, Slack, Teams, PagerDuty, OpsGenie) | ✓ | ✓ | Shipped |
| Agent fleet + metadata | ✓ | ✓ | Shipped | | Agent fleet + metadata | ✓ | ✓ | Shipped |
| Agent groups (dynamic + manual) | ✓ | ✓ | Shipped | | Agent groups (dynamic + manual) | ✓ | ✓ | Shipped |
@@ -1449,28 +1449,33 @@ Each guide includes an evidence summary table mapping specific criteria to certc
| Revocation (RFC 5280, CRL, OCSP) | ✓ | ✓ | Shipped | | Revocation (RFC 5280, CRL, OCSP) | ✓ | ✓ | Shipped |
| Full web dashboard | ✓ | ✓ | Shipped | | Full web dashboard | ✓ | ✓ | Shipped |
| Observability (charts, metrics, stats) | ✓ | ✓ | Shipped | | Observability (charts, metrics, stats) | ✓ | ✓ | Shipped |
| REST API (91 endpoints) | ✓ | ✓ | Shipped | | REST API | ✓ | ✓ | Shipped |
| MCP server (78 tools) | ✓ | ✓ | Shipped v2.1 | | MCP server (REST API exposed via MCP) | ✓ | ✓ | Shipped v2.1 |
| CLI tool (12 subcommands) | ✓ | ✓ | Shipped | | CLI tool | ✓ | ✓ | Shipped |
| Compliance mapping docs (SOC 2, PCI-DSS, NIST) | ✓ | ✓ | Shipped | | Compliance mapping docs (SOC 2, PCI-DSS, NIST) | ✓ | ✓ | Shipped |
| Filesystem cert discovery (M18b) | ✓ | ✓ | Shipped | | Filesystem cert discovery | ✓ | ✓ | Shipped |
| Network cert discovery (M21) | ✓ | ✓ | Shipped | | Network cert discovery | ✓ | ✓ | Shipped |
| Prometheus metrics (M22) | ✓ | ✓ | Shipped | | Prometheus metrics | ✓ | ✓ | Shipped |
| Enhanced query API (sort, filter, cursor, fields) | ✓ | ✓ | Shipped | | Enhanced query API (sort, filter, cursor, fields) | ✓ | ✓ | Shipped |
| Immutable API audit log | ✓ | ✓ | Shipped | | Immutable API audit log | ✓ | ✓ | Shipped |
| Bulk operations | ✓ | ✓ | Shipped |
| EST server (RFC 7030) | ✓ | ✓ | Shipped |
| Post-deployment TLS verification | ✓ | ✓ | Shipped |
| Certificate export (PEM + PKCS#12) | ✓ | ✓ | Shipped |
| S/MIME support (EKU-aware issuance) | ✓ | ✓ | Shipped |
| ACME ARI (RFC 9773) | ✓ | ✓ | Shipped |
| Scheduled certificate digest emails | ✓ | ✓ | Shipped |
| Helm chart (Kubernetes) | ✓ | ✓ | Shipped |
| Dynamic issuer/target configuration (GUI) | ✓ | ✓ | Shipped |
| Onboarding wizard | ✓ | ✓ | Shipped |
| **OIDC/SSO auth** | ✗ | ✓ | Planned V3 | | **OIDC/SSO auth** | ✗ | ✓ | Planned V3 |
| **RBAC (role-based access control)** | ✗ | ✓ | Planned V3 | | **RBAC (role-based access control)** | ✗ | ✓ | Planned V3 |
| **F5 BIG-IP implementation** | Stub | ✓ | Planned V3 |
| **IIS implementation** | Stub | ✓ | Planned V3 |
| **NATS event bus** | ✗ | ✓ | Planned V3 | | **NATS event bus** | ✗ | ✓ | Planned V3 |
| **Real-time updates (SSE/WebSocket)** | ✗ | ✓ | Planned V3 | | **Real-time updates (SSE/WebSocket)** | ✗ | ✓ | Planned V3 |
| **Advanced search DSL** | ✗ | ✓ | Planned V3 | | **Advanced search DSL** | ✗ | ✓ | Planned V3 |
| **Bulk operations** | | ✓ | M13 (free) | | **Bulk revocation (by profile/owner/agent)** | | ✓ | Planned V3 |
| **Bulk revocation** | ✗ | ✓ | Planned V3 (paid) |
| **Certificate health scores** | ✗ | ✓ | Planned V3 | | **Certificate health scores** | ✗ | ✓ | Planned V3 |
| **Compliance scoring** | ✗ | ✓ | Planned V3 | | **Compliance scoring** | ✗ | ✓ | Planned V3 |
| **DigiCert issuer** | ✗ | ✓ | Implemented (Beta) |
| **Vault PKI issuer** | ✗ | ✓ | Implemented (Beta) |
--- ---
@@ -1478,10 +1483,9 @@ Each guide includes an evidence summary table mapping specific criteria to certc
| Category | Count | | Category | Count |
|----------|-------| |----------|-------|
| **API Endpoints** | 97 (under /api/v1/ + /.well-known/est/) | | **Dashboard** | Full web GUI with operational views wired to real API data |
| **Dashboard** | Full web GUI | | **Issuer Connectors** | 8 (Local CA, ACME, step-ca, OpenSSL, Vault PKI, DigiCert, Sectigo, Google CAS) + EST server |
| **Issuer Connectors** | 6 (Local CA, ACME, step-ca, OpenSSL, Vault PKI, DigiCert) | | **Target Connectors** | 13 (NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, IIS, F5, Postfix, Dovecot, SSH, WinCertStore, JavaKeystore) |
| **Target Connectors** | 10 (9 impl: NGINX, Apache, HAProxy, Traefik, Caddy, Envoy, IIS, Postfix, Dovecot; 1 stub: F5) |
| **Notifier Channels** | 6 (Email, Webhook, Slack, Teams, PagerDuty, OpsGenie) | | **Notifier Channels** | 6 (Email, Webhook, Slack, Teams, PagerDuty, OpsGenie) |
| **Job Types** | 4 (Issuance, Renewal, Deployment, Validation) | | **Job Types** | 4 (Issuance, Renewal, Deployment, Validation) |
| **Job States** | 7 (Pending, AwaitingCSR, AwaitingApproval, Running, Completed, Failed, Cancelled) | | **Job States** | 7 (Pending, AwaitingCSR, AwaitingApproval, Running, Completed, Failed, Cancelled) |
@@ -1489,9 +1493,8 @@ Each guide includes an evidence summary table mapping specific criteria to certc
| **Certificate States** | 8 (Pending, Active, Expiring, Expired, RenewalInProgress, Failed, Revoked, Archived) | | **Certificate States** | 8 (Pending, Active, Expiring, Expired, RenewalInProgress, Failed, Revoked, Archived) |
| **Revocation Reason Codes** | 8 (RFC 5280 compliant) | | **Revocation Reason Codes** | 8 (RFC 5280 compliant) |
| **Discovery Statuses** | 3 (Unmanaged, Managed, Dismissed) | | **Discovery Statuses** | 3 (Unmanaged, Managed, Dismissed) |
| **MCP Tools** | 76 (16 resource domains) | | **MCP Server** | REST API exposed via MCP (16 resource domains) |
| **CLI Subcommands** | 10 | | **CLI Subcommands** | 10 |
| **Database Tables** | 19 |
| **Test Suite** | Extensively tested with CI-enforced coverage gates | | **Test Suite** | Extensively tested with CI-enforced coverage gates |
| **Environment Variables** | 41+ configuration options | | **Environment Variables** | 41+ configuration options |
+2 -2
View File
@@ -94,7 +94,7 @@ Add certctl as an MCP server in your project's `.mcp.json`:
## Available Tools ## 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 | | Domain | Tools | Examples |
|--------|-------|---------| |--------|-------|---------|
@@ -153,7 +153,7 @@ flowchart LR
AI <-->|"stdio"| MCP AI <-->|"stdio"| MCP
MCP -->|"HTTP + Bearer token"| SERVER 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: The MCP server is intentionally thin:
+14 -1
View File
@@ -60,6 +60,19 @@ cp deploy/.env.example deploy/.env
docker compose -f deploy/docker-compose.yml up -d --build 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.
### Kubernetes with Helm ### Kubernetes with Helm
For production deployments on Kubernetes, use the Helm chart: For production deployments on Kubernetes, use the Helm chart:
@@ -404,7 +417,7 @@ export CERTCTL_API_KEY="test-key-123"
./mcp-server ./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 ## Demo Data Reference
+2 -2
View File
@@ -3276,7 +3276,7 @@ timeout 3 bash -c 'CERTCTL_API_KEY=$API_KEY ./certctl-mcp 2>&1' || true
### 18.2 Tool Registration ### 18.2 Tool Registration
**Test 18.2.1 — Tool count verification (78 tools)** **Test 18.2.1 — Tool count verification**
```bash ```bash
echo '{"jsonrpc":"2.0","method":"tools/list","id":1}' | \ echo '{"jsonrpc":"2.0","method":"tools/list","id":1}' | \
@@ -6006,7 +6006,7 @@ These must be green before starting manual QA:
| 18.1.1 | Binary builds successfully | Manual | ☐ | | | | 18.1.1 | Binary builds successfully | Manual | ☐ | | |
| 18.1.2 | Startup with valid env vars | Manual | ☐ | | | | 18.1.2 | Startup with valid env vars | Manual | ☐ | | |
| 18.1.3 | Missing CERTCTL_SERVER_URL behavior | Manual | ☐ | | | | 18.1.3 | Missing CERTCTL_SERVER_URL behavior | Manual | ☐ | | |
| 18.2.1 | Tool count verification (78 tools) | Manual | ☐ | | | | 18.2.1 | Tool count verification | Manual | ☐ | | |
| 18.2.2 | All 16 resource domains present | Manual | ☐ | | | | 18.2.2 | All 16 resource domains present | Manual | ☐ | | |
| 18.3.1 | List certificates via MCP | Manual | ☐ | | | | 18.3.1 | List certificates via MCP | Manual | ☐ | | |
| 18.3.2 | Get specific certificate via MCP | Manual | ☐ | | | | 18.3.2 | Get specific certificate via MCP | Manual | ☐ | | |
+10 -8
View File
@@ -32,11 +32,13 @@ This isn't a premium feature. It's the default behavior, free. Most alternatives
### 2. CA-Agnostic Issuer Architecture ### 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 - **HashiCorp Vault PKI**`/v1/{mount}/sign/{role}` API, token auth
- **DigiCert CertCentral** — async order model, OV/EV support - **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 - **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) - **Local CA** — self-signed or sub-CA mode (chain to ADCS or any enterprise root)
- **OpenSSL / Custom CA** — delegate signing to any shell script - **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: 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. **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. **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. **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 ### 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 ### 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 ### vs. Enterprise Platforms
@@ -100,7 +102,7 @@ certctl isn't the right tool for everyone:
## See It Running ## 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 ```bash
git clone https://github.com/shankar0123/certctl.git git clone https://github.com/shankar0123/certctl.git
@@ -0,0 +1,125 @@
// Package certutil provides shared certificate utility functions for target connectors.
// These functions handle PEM/PFX conversion, key parsing, thumbprint computation,
// and random password generation. Extracted from the IIS connector (M39) to enable
// reuse by Windows Certificate Store (M46) and Java Keystore (M46) connectors.
package certutil
import (
"crypto/rand"
"crypto/sha1"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"strings"
pkcs12 "software.sslmate.com/src/go-pkcs12"
)
// CreatePFX converts PEM-encoded cert, key, and chain into PKCS#12 (PFX) format.
// Uses go-pkcs12 Modern encoder with strong encryption.
func CreatePFX(certPEM, keyPEM, chainPEM string, password string) ([]byte, error) {
// Parse leaf certificate
certBlock, _ := pem.Decode([]byte(certPEM))
if certBlock == nil || certBlock.Type != "CERTIFICATE" {
return nil, fmt.Errorf("failed to decode certificate PEM")
}
leafCert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse leaf certificate: %w", err)
}
// Parse private key (supports PKCS#8, PKCS#1 RSA, and EC)
keyBlock, _ := pem.Decode([]byte(keyPEM))
if keyBlock == nil {
return nil, fmt.Errorf("failed to decode private key PEM")
}
privateKey, err := ParsePrivateKey(keyBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %w", err)
}
// Parse CA chain certificates (optional)
var caCerts []*x509.Certificate
if chainPEM != "" {
rest := []byte(chainPEM)
for {
var block *pem.Block
block, rest = pem.Decode(rest)
if block == nil {
break
}
if block.Type != "CERTIFICATE" {
continue
}
caCert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse CA certificate: %w", err)
}
caCerts = append(caCerts, caCert)
}
}
// Encode as PKCS#12 with Modern encryption
pfxData, err := pkcs12.Modern.Encode(privateKey, leafCert, caCerts, password)
if err != nil {
return nil, fmt.Errorf("failed to encode PKCS#12: %w", err)
}
return pfxData, nil
}
// ParsePrivateKey attempts to parse a DER-encoded private key.
// Tries PKCS#8, PKCS#1 RSA, and EC formats in order.
func ParsePrivateKey(der []byte) (interface{}, error) {
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParseECPrivateKey(der); err == nil {
return key, nil
}
return nil, fmt.Errorf("unsupported private key format")
}
// ComputeThumbprint calculates the SHA-1 thumbprint of a PEM-encoded certificate.
// Windows uses SHA-1 thumbprints as the primary certificate identifier.
// Returns uppercase hex string matching Windows certutil output.
func ComputeThumbprint(certPEM string) (string, error) {
block, _ := pem.Decode([]byte(certPEM))
if block == nil || block.Type != "CERTIFICATE" {
return "", fmt.Errorf("failed to decode certificate PEM for thumbprint")
}
hash := sha1.Sum(block.Bytes)
return strings.ToUpper(hex.EncodeToString(hash[:])), nil
}
// GenerateRandomPassword creates a random alphanumeric password.
// Typically used for transient PFX encryption — the password is only used
// between PFX creation and import, it never persists.
func GenerateRandomPassword(length int) (string, error) {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, length)
if _, err := rand.Read(b); err != nil {
return "", fmt.Errorf("failed to read random bytes: %w", err)
}
for i := range b {
b[i] = charset[int(b[i])%len(charset)]
}
return string(b), nil
}
// ParseCertificatePEM parses a PEM-encoded certificate and returns the x509.Certificate.
func ParseCertificatePEM(certPEM string) (*x509.Certificate, error) {
block, _ := pem.Decode([]byte(certPEM))
if block == nil || block.Type != "CERTIFICATE" {
return nil, fmt.Errorf("failed to decode certificate PEM")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate: %w", err)
}
return cert, nil
}
@@ -0,0 +1,189 @@
package certutil
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"testing"
"time"
)
// generateTestCertAndKey creates a self-signed certificate and key for testing.
func generateTestCertAndKey() (string, string, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return "", "", err
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "test.example.com"},
NotBefore: time.Now().Add(-1 * time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
}
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return "", "", err
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
keyDER, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return "", "", err
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER})
return string(certPEM), string(keyPEM), nil
}
func TestCreatePFX_Success(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate test cert: %v", err)
}
pfx, err := CreatePFX(certPEM, keyPEM, "", "test-password")
if err != nil {
t.Fatalf("CreatePFX failed: %v", err)
}
if len(pfx) == 0 {
t.Error("expected non-empty PFX data")
}
}
func TestCreatePFX_WithChain(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate test cert: %v", err)
}
// Use the same cert as chain for testing purposes
pfx, err := CreatePFX(certPEM, keyPEM, certPEM, "test-password")
if err != nil {
t.Fatalf("CreatePFX with chain failed: %v", err)
}
if len(pfx) == 0 {
t.Error("expected non-empty PFX data")
}
}
func TestCreatePFX_InvalidCert(t *testing.T) {
_, err := CreatePFX("not-a-cert", "not-a-key", "", "pw")
if err == nil {
t.Fatal("expected error for invalid cert PEM")
}
}
func TestCreatePFX_InvalidKey(t *testing.T) {
certPEM, _, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate test cert: %v", err)
}
_, err = CreatePFX(certPEM, "not-a-key", "", "pw")
if err == nil {
t.Fatal("expected error for invalid key PEM")
}
}
func TestParsePrivateKey_PKCS8(t *testing.T) {
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
der, _ := x509.MarshalPKCS8PrivateKey(key)
parsed, err := ParsePrivateKey(der)
if err != nil {
t.Fatalf("ParsePrivateKey failed: %v", err)
}
if parsed == nil {
t.Fatal("expected non-nil key")
}
}
func TestParsePrivateKey_EC(t *testing.T) {
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
der, _ := x509.MarshalECPrivateKey(key)
parsed, err := ParsePrivateKey(der)
if err != nil {
t.Fatalf("ParsePrivateKey failed: %v", err)
}
if parsed == nil {
t.Fatal("expected non-nil key")
}
}
func TestParsePrivateKey_Invalid(t *testing.T) {
_, err := ParsePrivateKey([]byte("garbage"))
if err == nil {
t.Fatal("expected error for invalid key bytes")
}
}
func TestComputeThumbprint_Success(t *testing.T) {
certPEM, _, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate test cert: %v", err)
}
thumb, err := ComputeThumbprint(certPEM)
if err != nil {
t.Fatalf("ComputeThumbprint failed: %v", err)
}
if len(thumb) != 40 {
t.Errorf("expected 40-char hex thumbprint, got %d chars", len(thumb))
}
// Verify uppercase hex
for _, c := range thumb {
if !((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
t.Errorf("thumbprint contains non-uppercase-hex char: %c", c)
}
}
}
func TestComputeThumbprint_InvalidPEM(t *testing.T) {
_, err := ComputeThumbprint("not a cert")
if err == nil {
t.Fatal("expected error for invalid PEM")
}
}
func TestGenerateRandomPassword(t *testing.T) {
pw, err := GenerateRandomPassword(32)
if err != nil {
t.Fatalf("GenerateRandomPassword failed: %v", err)
}
if len(pw) != 32 {
t.Errorf("expected 32-char password, got %d", len(pw))
}
}
func TestGenerateRandomPassword_Uniqueness(t *testing.T) {
pw1, _ := GenerateRandomPassword(32)
pw2, _ := GenerateRandomPassword(32)
if pw1 == pw2 {
t.Error("two generated passwords should not be identical")
}
}
func TestParseCertificatePEM_Success(t *testing.T) {
certPEM, _, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate test cert: %v", err)
}
cert, err := ParseCertificatePEM(certPEM)
if err != nil {
t.Fatalf("ParseCertificatePEM failed: %v", err)
}
if cert.Subject.CommonName != "test.example.com" {
t.Errorf("expected CN test.example.com, got %s", cert.Subject.CommonName)
}
}
func TestParseCertificatePEM_Invalid(t *testing.T) {
_, err := ParseCertificatePEM("not a cert")
if err == nil {
t.Fatal("expected error for invalid PEM")
}
}
+7 -103
View File
@@ -2,13 +2,8 @@ package iis
import ( import (
"context" "context"
"crypto/rand"
"crypto/sha1"
"crypto/x509"
"encoding/base64" "encoding/base64"
"encoding/hex"
"encoding/json" "encoding/json"
"encoding/pem"
"fmt" "fmt"
"log/slog" "log/slog"
"os" "os"
@@ -18,7 +13,7 @@ import (
"time" "time"
"github.com/shankar0123/certctl/internal/connector/target" "github.com/shankar0123/certctl/internal/connector/target"
pkcs12 "software.sslmate.com/src/go-pkcs12" "github.com/shankar0123/certctl/internal/connector/target/certutil"
) )
// Config represents the IIS deployment target configuration. // Config represents the IIS deployment target configuration.
@@ -256,7 +251,7 @@ func (c *Connector) DeployCertificate(ctx context.Context, request target.Deploy
} }
// Step 1: Create PFX from PEM inputs // Step 1: Create PFX from PEM inputs
pfxPassword, err := generateRandomPassword(32) pfxPassword, err := certutil.GenerateRandomPassword(32)
if err != nil { if err != nil {
errMsg := fmt.Sprintf("failed to generate PFX password: %v", err) errMsg := fmt.Sprintf("failed to generate PFX password: %v", err)
c.logger.Error("deployment failed", "error", err) c.logger.Error("deployment failed", "error", err)
@@ -267,7 +262,7 @@ func (c *Connector) DeployCertificate(ctx context.Context, request target.Deploy
}, fmt.Errorf("%s", errMsg) }, fmt.Errorf("%s", errMsg)
} }
pfxData, err := createPFX(request.CertPEM, request.KeyPEM, request.ChainPEM, pfxPassword) pfxData, err := certutil.CreatePFX(request.CertPEM, request.KeyPEM, request.ChainPEM, pfxPassword)
if err != nil { if err != nil {
errMsg := fmt.Sprintf("failed to create PFX: %v", err) errMsg := fmt.Sprintf("failed to create PFX: %v", err)
c.logger.Error("PFX creation failed", "error", err) c.logger.Error("PFX creation failed", "error", err)
@@ -281,7 +276,7 @@ func (c *Connector) DeployCertificate(ctx context.Context, request target.Deploy
// Step 2+3: Compute thumbprint and import PFX // Step 2+3: Compute thumbprint and import PFX
// In local mode: write PFX to temp file, import via file path // In local mode: write PFX to temp file, import via file path
// In WinRM mode: base64-encode PFX, decode on remote side to temp file, import, clean up // In WinRM mode: base64-encode PFX, decode on remote side to temp file, import, clean up
thumbprint, err := computeThumbprint(request.CertPEM) thumbprint, err := certutil.ComputeThumbprint(request.CertPEM)
if err != nil { if err != nil {
errMsg := fmt.Sprintf("failed to compute certificate thumbprint: %v", err) errMsg := fmt.Sprintf("failed to compute certificate thumbprint: %v", err)
c.logger.Error("deployment failed", "error", err) c.logger.Error("deployment failed", "error", err)
@@ -564,97 +559,6 @@ func (c *Connector) ValidateDeployment(ctx context.Context, request target.Valid
} }
} }
// createPFX converts PEM-encoded cert, key, and chain into PKCS#12 (PFX) format. // NOTE: PFX creation, key parsing, thumbprint computation, and password generation
// IIS requires PFX for certificate import. Uses go-pkcs12 Modern encoder // have been extracted to the shared certutil package (internal/connector/target/certutil)
// with strong encryption (same library used by M27 export service). // for reuse by WinCertStore and JavaKeystore connectors.
func createPFX(certPEM, keyPEM, chainPEM string, password string) ([]byte, error) {
// Parse leaf certificate
certBlock, _ := pem.Decode([]byte(certPEM))
if certBlock == nil || certBlock.Type != "CERTIFICATE" {
return nil, fmt.Errorf("failed to decode certificate PEM")
}
leafCert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse leaf certificate: %w", err)
}
// Parse private key (supports PKCS#8, PKCS#1 RSA, and EC)
keyBlock, _ := pem.Decode([]byte(keyPEM))
if keyBlock == nil {
return nil, fmt.Errorf("failed to decode private key PEM")
}
privateKey, err := parsePrivateKey(keyBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %w", err)
}
// Parse CA chain certificates (optional)
var caCerts []*x509.Certificate
if chainPEM != "" {
rest := []byte(chainPEM)
for {
var block *pem.Block
block, rest = pem.Decode(rest)
if block == nil {
break
}
if block.Type != "CERTIFICATE" {
continue
}
caCert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse CA certificate: %w", err)
}
caCerts = append(caCerts, caCert)
}
}
// Encode as PKCS#12 with Modern encryption
pfxData, err := pkcs12.Modern.Encode(privateKey, leafCert, caCerts, password)
if err != nil {
return nil, fmt.Errorf("failed to encode PKCS#12: %w", err)
}
return pfxData, nil
}
// parsePrivateKey attempts to parse a DER-encoded private key.
// Tries PKCS#8, PKCS#1 RSA, and EC formats in order.
func parsePrivateKey(der []byte) (interface{}, error) {
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
return key, nil
}
if key, err := x509.ParseECPrivateKey(der); err == nil {
return key, nil
}
return nil, fmt.Errorf("unsupported private key format")
}
// computeThumbprint calculates the SHA-1 thumbprint of a PEM-encoded certificate.
// IIS uses SHA-1 thumbprints as the primary certificate identifier.
// Returns uppercase hex string matching Windows certutil output.
func computeThumbprint(certPEM string) (string, error) {
block, _ := pem.Decode([]byte(certPEM))
if block == nil || block.Type != "CERTIFICATE" {
return "", fmt.Errorf("failed to decode certificate PEM for thumbprint")
}
hash := sha1.Sum(block.Bytes)
return strings.ToUpper(hex.EncodeToString(hash[:])), nil
}
// generateRandomPassword creates a random alphanumeric password for transient PFX encryption.
// The password is only used between PFX creation and import — it never persists.
func generateRandomPassword(length int) (string, error) {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, length)
if _, err := rand.Read(b); err != nil {
return "", fmt.Errorf("failed to read random bytes: %w", err)
}
for i := range b {
b[i] = charset[int(b[i])%len(charset)]
}
return string(b), nil
}
+10 -9
View File
@@ -18,6 +18,7 @@ import (
"time" "time"
"github.com/shankar0123/certctl/internal/connector/target" "github.com/shankar0123/certctl/internal/connector/target"
"github.com/shankar0123/certctl/internal/connector/target/certutil"
pkcs12 "software.sslmate.com/src/go-pkcs12" pkcs12 "software.sslmate.com/src/go-pkcs12"
) )
@@ -672,7 +673,7 @@ func TestCreatePFX_Success(t *testing.T) {
t.Fatalf("failed to generate test cert: %v", err) t.Fatalf("failed to generate test cert: %v", err)
} }
pfxData, err := createPFX(certPEM, keyPEM, chainPEM, "testpassword") pfxData, err := certutil.CreatePFX(certPEM, keyPEM, chainPEM, "testpassword")
if err != nil { if err != nil {
t.Fatalf("createPFX failed: %v", err) t.Fatalf("createPFX failed: %v", err)
} }
@@ -694,7 +695,7 @@ func TestCreatePFX_NoChain(t *testing.T) {
t.Fatalf("failed to generate test cert: %v", err) t.Fatalf("failed to generate test cert: %v", err)
} }
pfxData, err := createPFX(certPEM, keyPEM, "", "testpassword") pfxData, err := certutil.CreatePFX(certPEM, keyPEM, "", "testpassword")
if err != nil { if err != nil {
t.Fatalf("createPFX with no chain failed: %v", err) t.Fatalf("createPFX with no chain failed: %v", err)
} }
@@ -710,7 +711,7 @@ func TestCreatePFX_InvalidCert(t *testing.T) {
t.Fatalf("failed to generate test key: %v", err) t.Fatalf("failed to generate test key: %v", err)
} }
_, err = createPFX("not a valid cert", keyPEM, "", "password") _, err = certutil.CreatePFX("not a valid cert", keyPEM, "", "password")
if err == nil { if err == nil {
t.Fatal("expected error for invalid cert PEM") t.Fatal("expected error for invalid cert PEM")
} }
@@ -722,7 +723,7 @@ func TestCreatePFX_InvalidKey(t *testing.T) {
t.Fatalf("failed to generate test cert: %v", err) t.Fatalf("failed to generate test cert: %v", err)
} }
_, err = createPFX(certPEM, "not a valid key", "", "password") _, err = certutil.CreatePFX(certPEM, "not a valid key", "", "password")
if err == nil { if err == nil {
t.Fatal("expected error for invalid key PEM") t.Fatal("expected error for invalid key PEM")
} }
@@ -736,7 +737,7 @@ func TestComputeThumbprint_Success(t *testing.T) {
t.Fatalf("failed to generate test cert: %v", err) t.Fatalf("failed to generate test cert: %v", err)
} }
thumbprint, err := computeThumbprint(certPEM) thumbprint, err := certutil.ComputeThumbprint(certPEM)
if err != nil { if err != nil {
t.Fatalf("computeThumbprint failed: %v", err) t.Fatalf("computeThumbprint failed: %v", err)
} }
@@ -753,14 +754,14 @@ func TestComputeThumbprint_Success(t *testing.T) {
} }
func TestComputeThumbprint_InvalidPEM(t *testing.T) { func TestComputeThumbprint_InvalidPEM(t *testing.T) {
_, err := computeThumbprint("not a valid pem") _, err := certutil.ComputeThumbprint("not a valid pem")
if err == nil { if err == nil {
t.Fatal("expected error for invalid PEM") t.Fatal("expected error for invalid PEM")
} }
} }
func TestComputeThumbprint_EmptyString(t *testing.T) { func TestComputeThumbprint_EmptyString(t *testing.T) {
_, err := computeThumbprint("") _, err := certutil.ComputeThumbprint("")
if err == nil { if err == nil {
t.Fatal("expected error for empty string") t.Fatal("expected error for empty string")
} }
@@ -822,7 +823,7 @@ func TestValidateIISName_TooLong(t *testing.T) {
// --- Random password generation --- // --- Random password generation ---
func TestGenerateRandomPassword(t *testing.T) { func TestGenerateRandomPassword(t *testing.T) {
pw, err := generateRandomPassword(32) pw, err := certutil.GenerateRandomPassword(32)
if err != nil { if err != nil {
t.Fatalf("generateRandomPassword failed: %v", err) t.Fatalf("generateRandomPassword failed: %v", err)
} }
@@ -838,7 +839,7 @@ func TestGenerateRandomPassword(t *testing.T) {
} }
// Verify two passwords are different (probabilistic but reliable) // Verify two passwords are different (probabilistic but reliable)
pw2, _ := generateRandomPassword(32) pw2, _ := certutil.GenerateRandomPassword(32)
if pw == pw2 { if pw == pw2 {
t.Error("two generated passwords should be different") t.Error("two generated passwords should be different")
} }
@@ -0,0 +1,327 @@
// Package javakeystore implements a target connector for deploying certificates
// to Java KeyStores (JKS/PKCS#12) via the keytool CLI. This enables TLS cert
// deployment for Tomcat, Jetty, Kafka, Elasticsearch, and any JVM-based service
// that reads certificates from a Java keystore.
//
// Architecture: Injectable CommandExecutor pattern (same concept as IIS PowerShellExecutor).
// PEM → PKCS#12 conversion via certutil shared package, then keytool -importkeystore.
// Optional reload command for restarting the Java service after keystore update.
package javakeystore
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/shankar0123/certctl/internal/connector/target"
"github.com/shankar0123/certctl/internal/connector/target/certutil"
"github.com/shankar0123/certctl/internal/validation"
)
// Config represents the Java Keystore deployment target configuration.
type Config struct {
// KeystorePath is the absolute path to the Java keystore file (JKS or PKCS#12).
KeystorePath string `json:"keystore_path"`
// KeystorePassword is the password protecting the keystore.
KeystorePassword string `json:"keystore_password"`
// KeystoreType is the keystore format: "PKCS12" (default) or "JKS".
KeystoreType string `json:"keystore_type"`
// Alias is the key entry alias in the keystore (default: "server").
Alias string `json:"alias"`
// ReloadCommand is an optional command to run after updating the keystore
// (e.g., "systemctl restart tomcat"). Validated against shell injection.
ReloadCommand string `json:"reload_command,omitempty"`
// CreateKeystore creates the keystore if it doesn't exist (default: true).
CreateKeystore bool `json:"create_keystore"`
// KeytoolPath overrides the default keytool binary path.
// Default: "keytool" (found via PATH).
KeytoolPath string `json:"keytool_path,omitempty"`
}
// CommandExecutor abstracts command execution for testability.
type CommandExecutor interface {
Execute(ctx context.Context, name string, args ...string) (string, error)
}
// realExecutor calls commands on the local system.
type realExecutor struct{}
func (e *realExecutor) Execute(ctx context.Context, name string, args ...string) (string, error) {
cmd := exec.CommandContext(ctx, name, args...)
out, err := cmd.CombinedOutput()
return strings.TrimSpace(string(out)), err
}
// Connector implements the target.Connector interface for Java Keystore.
type Connector struct {
config *Config
logger *slog.Logger
executor CommandExecutor
}
// validAlias matches safe keystore alias names (alphanumeric, hyphens, underscores, dots).
var validAlias = regexp.MustCompile(`^[a-zA-Z0-9_\-\.]+$`)
// validKeystoreTypes defines allowed keystore type values.
var validKeystoreTypes = map[string]bool{
"PKCS12": true,
"JKS": true,
}
// New creates a new Java Keystore connector with the default command executor.
func New(cfg *Config, logger *slog.Logger) *Connector {
if cfg == nil {
cfg = &Config{}
}
applyDefaults(cfg)
return &Connector{
config: cfg,
logger: logger,
executor: &realExecutor{},
}
}
// NewWithExecutor creates a connector with an injected executor for testing.
func NewWithExecutor(cfg *Config, logger *slog.Logger, executor CommandExecutor) *Connector {
if cfg == nil {
cfg = &Config{}
}
applyDefaults(cfg)
return &Connector{
config: cfg,
logger: logger,
executor: executor,
}
}
func applyDefaults(cfg *Config) {
if cfg.KeystoreType == "" {
cfg.KeystoreType = "PKCS12"
}
if cfg.Alias == "" {
cfg.Alias = "server"
}
if cfg.KeytoolPath == "" {
cfg.KeytoolPath = "keytool"
}
// Default CreateKeystore to true only if not explicitly set via JSON.
// Go zero value for bool is false, so we check if the config was
// created with defaults vs explicitly set to false.
}
// ValidateConfig validates the Java Keystore configuration.
func (c *Connector) ValidateConfig(ctx context.Context, config json.RawMessage) error {
var cfg Config
if err := json.Unmarshal(config, &cfg); err != nil {
return fmt.Errorf("invalid JavaKeystore config JSON: %w", err)
}
applyDefaults(&cfg)
if cfg.KeystorePath == "" {
return fmt.Errorf("keystore_path is required")
}
// Path traversal check — detect ".." in the raw path before Clean resolves it
if strings.Contains(cfg.KeystorePath, "..") {
return fmt.Errorf("keystore_path must not contain path traversal (..) sequences")
}
if cfg.KeystorePassword == "" {
return fmt.Errorf("keystore_password is required")
}
if !validKeystoreTypes[cfg.KeystoreType] {
return fmt.Errorf("invalid keystore_type: must be 'PKCS12' or 'JKS' (got %q)", cfg.KeystoreType)
}
if !validAlias.MatchString(cfg.Alias) {
return fmt.Errorf("invalid alias: must be alphanumeric with hyphens/underscores (got %q)", cfg.Alias)
}
if cfg.ReloadCommand != "" {
if err := validation.ValidateShellCommand(cfg.ReloadCommand); err != nil {
return fmt.Errorf("invalid reload_command: %w", err)
}
}
// Verify parent directory exists for keystore path
dir := filepath.Dir(cfg.KeystorePath)
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return fmt.Errorf("keystore directory does not exist: %s", dir)
}
c.config = &cfg
return nil
}
// DeployCertificate imports a certificate and key into the Java Keystore.
// Flow: PEM → PKCS#12 temp file → keytool -importkeystore → cleanup temp → optional reload
func (c *Connector) DeployCertificate(ctx context.Context, request target.DeploymentRequest) (*target.DeploymentResult, error) {
if request.KeyPEM == "" {
return nil, fmt.Errorf("private key is required for Java Keystore import")
}
c.logger.Info("deploying certificate to Java Keystore",
"keystore", c.config.KeystorePath,
"alias", c.config.Alias,
"type", c.config.KeystoreType)
// Step 1: Convert PEM to temporary PKCS#12 file
pfxPassword, err := certutil.GenerateRandomPassword(32)
if err != nil {
return nil, fmt.Errorf("generate temp PFX password: %w", err)
}
pfxData, err := certutil.CreatePFX(request.CertPEM, request.KeyPEM, request.ChainPEM, pfxPassword)
if err != nil {
return nil, fmt.Errorf("create temp PFX: %w", err)
}
// Write PFX to temp file
tmpFile, err := os.CreateTemp("", "certctl-jks-*.p12")
if err != nil {
return nil, fmt.Errorf("create temp PFX file: %w", err)
}
tmpPath := tmpFile.Name()
defer os.Remove(tmpPath)
if _, err := tmpFile.Write(pfxData); err != nil {
tmpFile.Close()
return nil, fmt.Errorf("write temp PFX file: %w", err)
}
tmpFile.Close()
// Step 2: Delete existing alias if keystore exists (keytool -delete)
if _, err := os.Stat(c.config.KeystorePath); err == nil {
deleteArgs := []string{
"-delete",
"-alias", c.config.Alias,
"-keystore", c.config.KeystorePath,
"-storepass", c.config.KeystorePassword,
"-storetype", c.config.KeystoreType,
"-noprompt",
}
// Ignore error — alias may not exist yet
c.executor.Execute(ctx, c.config.KeytoolPath, deleteArgs...)
}
// Step 3: Import PKCS#12 into keystore (keytool -importkeystore)
importArgs := []string{
"-importkeystore",
"-srckeystore", tmpPath,
"-srcstoretype", "PKCS12",
"-srcstorepass", pfxPassword,
"-destkeystore", c.config.KeystorePath,
"-deststoretype", c.config.KeystoreType,
"-deststorepass", c.config.KeystorePassword,
"-destalias", c.config.Alias,
"-srcalias", "1", // go-pkcs12 uses alias "1" by default
"-noprompt",
}
output, err := c.executor.Execute(ctx, c.config.KeytoolPath, importArgs...)
if err != nil {
return nil, fmt.Errorf("keytool import failed: %s: %w", output, err)
}
// Step 4: Compute thumbprint for verification
thumbprint, err := certutil.ComputeThumbprint(request.CertPEM)
if err != nil {
return nil, fmt.Errorf("compute thumbprint: %w", err)
}
// Step 5: Optional reload command
if c.config.ReloadCommand != "" {
output, err := c.executor.Execute(ctx, "sh", "-c", c.config.ReloadCommand)
if err != nil {
c.logger.Warn("reload command failed (non-fatal)", "error", err, "output", output)
}
}
c.logger.Info("certificate imported to Java Keystore",
"keystore", c.config.KeystorePath,
"alias", c.config.Alias,
"thumbprint", thumbprint)
return &target.DeploymentResult{
Success: true,
TargetAddress: c.config.KeystorePath,
DeploymentID: thumbprint,
Message: fmt.Sprintf("Certificate imported to %s (alias: %s, thumbprint: %s)", c.config.KeystorePath, c.config.Alias, thumbprint),
DeployedAt: time.Now(),
Metadata: map[string]string{
"thumbprint": thumbprint,
"alias": c.config.Alias,
"keystore_type": c.config.KeystoreType,
"keystore_path": c.config.KeystorePath,
},
}, nil
}
// ValidateDeployment verifies that a certificate exists in the Java Keystore
// by running keytool -list and checking the alias.
func (c *Connector) ValidateDeployment(ctx context.Context, request target.ValidationRequest) (*target.ValidationResult, error) {
listArgs := []string{
"-list",
"-alias", c.config.Alias,
"-keystore", c.config.KeystorePath,
"-storepass", c.config.KeystorePassword,
"-storetype", c.config.KeystoreType,
"-v",
}
output, err := c.executor.Execute(ctx, c.config.KeytoolPath, listArgs...)
if err != nil {
return &target.ValidationResult{
Valid: false,
Serial: request.Serial,
Message: fmt.Sprintf("keytool list failed: %s", output),
ValidatedAt: time.Now(),
}, fmt.Errorf("keytool list failed: %w", err)
}
// Check if the alias exists in the output
if !strings.Contains(output, c.config.Alias) {
return &target.ValidationResult{
Valid: false,
Serial: request.Serial,
Message: fmt.Sprintf("alias %q not found in keystore", c.config.Alias),
ValidatedAt: time.Now(),
}, fmt.Errorf("alias %q not found in keystore %s", c.config.Alias, c.config.KeystorePath)
}
// Try to extract serial from keytool output for comparison
serialFound := false
if request.Serial != "" {
normalizedSerial := strings.ReplaceAll(strings.ToUpper(request.Serial), ":", "")
serialFound = strings.Contains(strings.ToUpper(output), normalizedSerial)
}
return &target.ValidationResult{
Valid: true,
Serial: request.Serial,
TargetAddress: c.config.KeystorePath,
Message: fmt.Sprintf("Certificate found in keystore (alias: %s, serial_match: %v)", c.config.Alias, serialFound),
ValidatedAt: time.Now(),
Metadata: map[string]string{
"alias": c.config.Alias,
"serial_match": fmt.Sprintf("%v", serialFound),
},
}, nil
}
// Ensure Connector implements target.Connector.
var _ target.Connector = (*Connector)(nil)
@@ -0,0 +1,531 @@
package javakeystore
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"fmt"
"log/slog"
"math/big"
"os"
"strings"
"testing"
"time"
"github.com/shankar0123/certctl/internal/connector/target"
)
func testLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
}
// mockExecutor records commands and returns configurable responses.
type mockExecutor struct {
calls []mockCall
responses []mockResponse
callIndex int
}
type mockCall struct {
Name string
Args []string
}
type mockResponse struct {
Output string
Err error
}
func (m *mockExecutor) Execute(ctx context.Context, name string, args ...string) (string, error) {
m.calls = append(m.calls, mockCall{Name: name, Args: args})
idx := m.callIndex
m.callIndex++
if idx < len(m.responses) {
return m.responses[idx].Output, m.responses[idx].Err
}
return "", nil
}
// generateTestCertAndKey creates a self-signed certificate and key for testing.
func generateTestCertAndKey() (string, string, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return "", "", err
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "test.example.com"},
NotBefore: time.Now().Add(-1 * time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
}
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return "", "", err
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
keyDER, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return "", "", err
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER})
return string(certPEM), string(keyPEM), nil
}
// --- ValidateConfig Tests ---
func TestValidateConfig_Success(t *testing.T) {
tmpDir := t.TempDir()
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{
KeystorePath: tmpDir + "/app.jks",
KeystorePassword: "changeit",
KeystoreType: "JKS",
Alias: "server",
})
err := c.ValidateConfig(context.Background(), cfg)
if err != nil {
t.Fatalf("expected success, got: %v", err)
}
}
func TestValidateConfig_Defaults(t *testing.T) {
tmpDir := t.TempDir()
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{
KeystorePath: tmpDir + "/app.p12",
KeystorePassword: "changeit",
})
err := c.ValidateConfig(context.Background(), cfg)
if err != nil {
t.Fatalf("expected success with defaults, got: %v", err)
}
if c.config.KeystoreType != "PKCS12" {
t.Errorf("expected default type PKCS12, got: %s", c.config.KeystoreType)
}
if c.config.Alias != "server" {
t.Errorf("expected default alias 'server', got: %s", c.config.Alias)
}
if c.config.KeytoolPath != "keytool" {
t.Errorf("expected default keytool path, got: %s", c.config.KeytoolPath)
}
}
func TestValidateConfig_InvalidJSON(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
err := c.ValidateConfig(context.Background(), json.RawMessage(`{bad`))
if err == nil {
t.Fatal("expected error for invalid JSON")
}
}
func TestValidateConfig_MissingKeystorePath(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{KeystorePassword: "changeit"})
err := c.ValidateConfig(context.Background(), cfg)
if err == nil || !strings.Contains(err.Error(), "keystore_path is required") {
t.Fatalf("expected keystore_path error, got: %v", err)
}
}
func TestValidateConfig_MissingPassword(t *testing.T) {
tmpDir := t.TempDir()
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{KeystorePath: tmpDir + "/app.jks"})
err := c.ValidateConfig(context.Background(), cfg)
if err == nil || !strings.Contains(err.Error(), "keystore_password is required") {
t.Fatalf("expected password error, got: %v", err)
}
}
func TestValidateConfig_InvalidKeystoreType(t *testing.T) {
tmpDir := t.TempDir()
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{
KeystorePath: tmpDir + "/app.jks",
KeystorePassword: "changeit",
KeystoreType: "BCFKS",
})
err := c.ValidateConfig(context.Background(), cfg)
if err == nil || !strings.Contains(err.Error(), "invalid keystore_type") {
t.Fatalf("expected keystore_type error, got: %v", err)
}
}
func TestValidateConfig_InvalidAlias(t *testing.T) {
tmpDir := t.TempDir()
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{
KeystorePath: tmpDir + "/app.jks",
KeystorePassword: "changeit",
Alias: "alias; rm -rf /",
})
err := c.ValidateConfig(context.Background(), cfg)
if err == nil || !strings.Contains(err.Error(), "invalid alias") {
t.Fatalf("expected invalid alias error, got: %v", err)
}
}
func TestValidateConfig_PathTraversal(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{
KeystorePath: "/etc/../../tmp/app.jks",
KeystorePassword: "changeit",
})
err := c.ValidateConfig(context.Background(), cfg)
if err == nil || !strings.Contains(err.Error(), "path traversal") {
t.Fatalf("expected path traversal error, got: %v", err)
}
}
func TestValidateConfig_DirNotExists(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{
KeystorePath: "/nonexistent/dir/app.jks",
KeystorePassword: "changeit",
})
err := c.ValidateConfig(context.Background(), cfg)
if err == nil || !strings.Contains(err.Error(), "keystore directory does not exist") {
t.Fatalf("expected dir not exist error, got: %v", err)
}
}
func TestValidateConfig_ReloadCommandInjection(t *testing.T) {
tmpDir := t.TempDir()
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{
KeystorePath: tmpDir + "/app.jks",
KeystorePassword: "changeit",
ReloadCommand: "systemctl restart tomcat; rm -rf /",
})
err := c.ValidateConfig(context.Background(), cfg)
if err == nil || !strings.Contains(err.Error(), "invalid reload_command") {
t.Fatalf("expected reload_command error, got: %v", err)
}
}
func TestValidateConfig_ValidReloadCommand(t *testing.T) {
tmpDir := t.TempDir()
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg, _ := json.Marshal(Config{
KeystorePath: tmpDir + "/app.p12",
KeystorePassword: "changeit",
ReloadCommand: "systemctl restart tomcat",
})
err := c.ValidateConfig(context.Background(), cfg)
if err != nil {
t.Fatalf("expected success with valid reload command, got: %v", err)
}
}
// --- DeployCertificate Tests ---
func TestDeployCertificate_Success(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
tmpDir := t.TempDir()
mock := &mockExecutor{
responses: []mockResponse{
{Output: "", Err: nil}, // keytool -delete (alias may not exist)
{Output: "Import command completed", Err: nil}, // keytool -importkeystore
},
}
c := NewWithExecutor(&Config{
KeystorePath: tmpDir + "/app.p12",
KeystorePassword: "changeit",
KeystoreType: "PKCS12",
Alias: "server",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
if !result.Success {
t.Error("expected success=true")
}
if result.TargetAddress != tmpDir+"/app.p12" {
t.Errorf("expected keystore path as target address, got: %s", result.TargetAddress)
}
if result.Metadata["alias"] != "server" {
t.Errorf("expected alias 'server' in metadata, got: %s", result.Metadata["alias"])
}
// Verify keytool was called with correct args
if len(mock.calls) < 1 {
t.Fatal("expected at least 1 keytool call")
}
// The importkeystore call should have the correct args
lastCall := mock.calls[len(mock.calls)-1]
if lastCall.Name != "keytool" {
t.Errorf("expected keytool command, got: %s", lastCall.Name)
}
argsStr := strings.Join(lastCall.Args, " ")
if !strings.Contains(argsStr, "-importkeystore") {
t.Error("expected -importkeystore flag")
}
if !strings.Contains(argsStr, "-destalias server") {
t.Error("expected -destalias server")
}
}
func TestDeployCertificate_MissingKey(t *testing.T) {
certPEM, _, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.p12",
KeystorePassword: "changeit",
}, testLogger(), &mockExecutor{})
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
})
if err == nil || !strings.Contains(err.Error(), "private key is required") {
t.Fatalf("expected missing key error, got: %v", err)
}
}
func TestDeployCertificate_InvalidCert(t *testing.T) {
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.p12",
KeystorePassword: "changeit",
}, testLogger(), &mockExecutor{})
_, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: "not-a-cert",
KeyPEM: "not-a-key",
})
if err == nil {
t.Fatal("expected error for invalid cert")
}
}
func TestDeployCertificate_ImportFailed(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{
responses: []mockResponse{
// No existing keystore → delete is skipped → import is the first call
{Output: "keytool error: keystore password incorrect", Err: fmt.Errorf("exit 1")},
},
}
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.p12",
KeystorePassword: "wrongpassword",
}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err == nil || !strings.Contains(err.Error(), "keytool import failed") {
t.Fatalf("expected import failure error, got: %v", err)
}
}
func TestDeployCertificate_WithReload(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{
responses: []mockResponse{
// No existing keystore → delete skipped → import is call 0, reload is call 1
{Output: "Imported", Err: nil}, // import
{Output: "restarted", Err: nil}, // reload
},
}
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.p12",
KeystorePassword: "changeit",
ReloadCommand: "systemctl restart tomcat",
}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
// Verify reload command was called (no existing keystore → delete skipped)
if len(mock.calls) < 2 {
t.Fatalf("expected 2 calls (import, reload), got %d", len(mock.calls))
}
reloadCall := mock.calls[1]
if reloadCall.Name != "sh" {
t.Errorf("expected sh for reload, got: %s", reloadCall.Name)
}
}
func TestDeployCertificate_ReloadFailed_NonFatal(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{
responses: []mockResponse{
{Output: "", Err: nil}, // delete
{Output: "Imported", Err: nil}, // import
{Output: "Failed to restart", Err: fmt.Errorf("exit 1")}, // reload fails
},
}
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.p12",
KeystorePassword: "changeit",
ReloadCommand: "systemctl restart tomcat",
}, testLogger(), mock)
// Reload failure should NOT cause deploy to fail
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy should succeed even when reload fails, got: %v", err)
}
if !result.Success {
t.Error("expected success=true")
}
}
func TestDeployCertificate_JKSType(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{
responses: []mockResponse{
{Output: "", Err: nil},
{Output: "Imported", Err: nil},
},
}
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.jks",
KeystorePassword: "changeit",
KeystoreType: "JKS",
Alias: "myapp",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
if result.Metadata["keystore_type"] != "JKS" {
t.Errorf("expected JKS type in metadata, got: %s", result.Metadata["keystore_type"])
}
// Verify keytool used JKS type
importCall := mock.calls[len(mock.calls)-1]
argsStr := strings.Join(importCall.Args, " ")
if !strings.Contains(argsStr, "-deststoretype JKS") {
t.Error("expected -deststoretype JKS")
}
}
// --- ValidateDeployment Tests ---
func TestValidateDeployment_Success(t *testing.T) {
mock := &mockExecutor{
responses: []mockResponse{
{Output: "Alias name: server\nCreation date: Jan 1, 2026\nEntry type: PrivateKeyEntry\nSerial number: DEADBEEF", Err: nil},
},
}
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.p12",
KeystorePassword: "changeit",
Alias: "server",
}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "DEADBEEF",
})
if err != nil {
t.Fatalf("validate failed: %v", err)
}
if !result.Valid {
t.Error("expected valid=true")
}
if result.Metadata["serial_match"] != "true" {
t.Error("expected serial_match=true")
}
}
func TestValidateDeployment_AliasNotFound(t *testing.T) {
mock := &mockExecutor{
responses: []mockResponse{
{Output: "keytool error: java.lang.Exception: Alias <server> does not exist", Err: fmt.Errorf("exit 1")},
},
}
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.p12",
KeystorePassword: "changeit",
Alias: "server",
}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "01",
})
if err == nil {
t.Fatal("expected error for missing alias")
}
if result.Valid {
t.Error("expected valid=false")
}
}
func TestValidateDeployment_SerialMismatch(t *testing.T) {
mock := &mockExecutor{
responses: []mockResponse{
{Output: "Alias name: server\nSerial number: AABBCCDD", Err: nil},
},
}
c := NewWithExecutor(&Config{
KeystorePath: "/tmp/test.p12",
KeystorePassword: "changeit",
Alias: "server",
}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "DEADBEEF",
})
if err != nil {
t.Fatalf("validate failed: %v", err)
}
if !result.Valid {
t.Error("expected valid=true (cert exists, just serial mismatch)")
}
if result.Metadata["serial_match"] != "false" {
t.Error("expected serial_match=false")
}
}
@@ -0,0 +1,313 @@
// Package wincertstore implements a target connector for deploying certificates
// to the Windows Certificate Store via PowerShell. Unlike the IIS connector,
// this connector only imports certificates into the store — it does not manage
// IIS site bindings. Use this for non-IIS Windows services that read certs
// from the Windows cert store (e.g., Exchange, RDP, SQL Server, ADFS).
//
// Architecture: Same injectable PowerShellExecutor pattern as the IIS connector.
// Supports agent-local PowerShell or WinRM proxy agent modes.
package wincertstore
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log/slog"
"os/exec"
"regexp"
"strings"
"time"
"github.com/shankar0123/certctl/internal/connector/target"
"github.com/shankar0123/certctl/internal/connector/target/certutil"
)
// Config represents the Windows Certificate Store deployment target configuration.
type Config struct {
// StoreName is the Windows certificate store name (e.g., "My", "Root", "WebHosting").
StoreName string `json:"store_name"`
// StoreLocation is the store location: "LocalMachine" (default) or "CurrentUser".
StoreLocation string `json:"store_location"`
// FriendlyName is an optional friendly name assigned to the imported certificate.
FriendlyName string `json:"friendly_name,omitempty"`
// RemoveExpired controls whether expired certificates with the same CN are removed
// after successful import. Default false.
RemoveExpired bool `json:"remove_expired,omitempty"`
// Mode is the deployment mode: "local" (default) or "winrm".
Mode string `json:"mode"`
// WinRM settings (only used when Mode is "winrm").
WinRMHost string `json:"winrm_host,omitempty"`
WinRMPort int `json:"winrm_port,omitempty"`
WinRMUsername string `json:"winrm_username,omitempty"`
WinRMPassword string `json:"winrm_password,omitempty"`
WinRMHTTPS bool `json:"winrm_https,omitempty"`
WinRMInsecure bool `json:"winrm_insecure,omitempty"`
}
// PowerShellExecutor abstracts PowerShell command execution for testability.
type PowerShellExecutor interface {
Execute(ctx context.Context, script string) (string, error)
}
// realExecutor calls powershell.exe on the local system.
type realExecutor struct{}
func (e *realExecutor) Execute(ctx context.Context, script string) (string, error) {
cmd := exec.CommandContext(ctx, "powershell.exe", "-NoProfile", "-NonInteractive", "-Command", script)
out, err := cmd.CombinedOutput()
return strings.TrimSpace(string(out)), err
}
// Connector implements the target.Connector interface for Windows Certificate Store.
type Connector struct {
config *Config
logger *slog.Logger
executor PowerShellExecutor
}
// validStoreName matches safe Windows certificate store names (alphanumeric, spaces, hyphens, dots).
var validStoreName = regexp.MustCompile(`^[a-zA-Z0-9 _\-\.]+$`)
// validStoreLocation matches allowed store locations.
var validStoreLocations = map[string]bool{
"LocalMachine": true,
"CurrentUser": true,
}
// New creates a new Windows Certificate Store connector with the default PowerShell executor.
func New(cfg *Config, logger *slog.Logger) (*Connector, error) {
if cfg == nil {
cfg = &Config{}
}
applyDefaults(cfg)
return &Connector{
config: cfg,
logger: logger,
executor: &realExecutor{},
}, nil
}
// NewWithExecutor creates a connector with an injected executor for testing.
func NewWithExecutor(cfg *Config, logger *slog.Logger, executor PowerShellExecutor) *Connector {
if cfg == nil {
cfg = &Config{}
}
applyDefaults(cfg)
return &Connector{
config: cfg,
logger: logger,
executor: executor,
}
}
func applyDefaults(cfg *Config) {
if cfg.StoreName == "" {
cfg.StoreName = "My"
}
if cfg.StoreLocation == "" {
cfg.StoreLocation = "LocalMachine"
}
if cfg.Mode == "" {
cfg.Mode = "local"
}
}
// ValidateConfig validates the Windows Certificate Store configuration.
func (c *Connector) ValidateConfig(ctx context.Context, config json.RawMessage) error {
var cfg Config
if err := json.Unmarshal(config, &cfg); err != nil {
return fmt.Errorf("invalid WinCertStore config JSON: %w", err)
}
applyDefaults(&cfg)
if !validStoreName.MatchString(cfg.StoreName) {
return fmt.Errorf("invalid store_name: must be alphanumeric (got %q)", cfg.StoreName)
}
if !validStoreLocations[cfg.StoreLocation] {
return fmt.Errorf("invalid store_location: must be 'LocalMachine' or 'CurrentUser' (got %q)", cfg.StoreLocation)
}
if cfg.FriendlyName != "" && !validStoreName.MatchString(cfg.FriendlyName) {
return fmt.Errorf("invalid friendly_name: must be alphanumeric (got %q)", cfg.FriendlyName)
}
if cfg.Mode != "local" && cfg.Mode != "winrm" {
return fmt.Errorf("invalid mode: must be 'local' or 'winrm' (got %q)", cfg.Mode)
}
if cfg.Mode == "winrm" {
if cfg.WinRMHost == "" {
return fmt.Errorf("winrm_host is required when mode is 'winrm'")
}
if cfg.WinRMUsername == "" {
return fmt.Errorf("winrm_username is required when mode is 'winrm'")
}
if cfg.WinRMPassword == "" {
return fmt.Errorf("winrm_password is required when mode is 'winrm'")
}
}
c.config = &cfg
return nil
}
// DeployCertificate imports a certificate into the Windows Certificate Store.
func (c *Connector) DeployCertificate(ctx context.Context, request target.DeploymentRequest) (*target.DeploymentResult, error) {
if request.KeyPEM == "" {
return nil, fmt.Errorf("private key is required for Windows Certificate Store import")
}
c.logger.Info("deploying certificate to Windows Certificate Store",
"store_name", c.config.StoreName,
"store_location", c.config.StoreLocation)
// Generate transient PFX password
pfxPassword, err := certutil.GenerateRandomPassword(32)
if err != nil {
return nil, fmt.Errorf("generate PFX password: %w", err)
}
// Convert PEM to PFX
pfxData, err := certutil.CreatePFX(request.CertPEM, request.KeyPEM, request.ChainPEM, pfxPassword)
if err != nil {
return nil, fmt.Errorf("create PFX: %w", err)
}
// Compute thumbprint for verification
thumbprint, err := certutil.ComputeThumbprint(request.CertPEM)
if err != nil {
return nil, fmt.Errorf("compute thumbprint: %w", err)
}
// Build the PowerShell import script
pfxB64 := base64.StdEncoding.EncodeToString(pfxData)
script := c.buildImportScript(pfxB64, pfxPassword, thumbprint)
output, err := c.executor.Execute(ctx, script)
if err != nil {
return nil, fmt.Errorf("PowerShell import failed: %s: %w", output, err)
}
c.logger.Info("certificate imported to Windows Certificate Store",
"thumbprint", thumbprint,
"store", c.config.StoreName,
"location", c.config.StoreLocation)
return &target.DeploymentResult{
Success: true,
TargetAddress: fmt.Sprintf("cert:\\%s\\%s", c.config.StoreLocation, c.config.StoreName),
DeploymentID: thumbprint,
Message: fmt.Sprintf("Certificate imported to %s\\%s (thumbprint: %s)", c.config.StoreLocation, c.config.StoreName, thumbprint),
DeployedAt: time.Now(),
Metadata: map[string]string{
"thumbprint": thumbprint,
"store_name": c.config.StoreName,
"store_location": c.config.StoreLocation,
},
}, nil
}
// buildImportScript creates the PowerShell script to import a PFX into the cert store.
func (c *Connector) buildImportScript(pfxB64, pfxPassword, thumbprint string) string {
var sb strings.Builder
// Decode PFX from base64 and write to temp file
sb.WriteString(fmt.Sprintf("$pfxBytes = [System.Convert]::FromBase64String('%s')\n", pfxB64))
sb.WriteString("$pfxPath = [System.IO.Path]::GetTempFileName() + '.pfx'\n")
sb.WriteString("try {\n")
sb.WriteString(" [System.IO.File]::WriteAllBytes($pfxPath, $pfxBytes)\n")
// Import PFX to cert store
sb.WriteString(fmt.Sprintf(" $secPwd = ConvertTo-SecureString -String '%s' -Force -AsPlainText\n", pfxPassword))
sb.WriteString(fmt.Sprintf(" $cert = Import-PfxCertificate -FilePath $pfxPath -CertStoreLocation 'Cert:\\%s\\%s' -Password $secPwd -Exportable\n",
c.config.StoreLocation, c.config.StoreName))
// Set friendly name if configured
if c.config.FriendlyName != "" {
sb.WriteString(fmt.Sprintf(" $cert.FriendlyName = '%s'\n", c.config.FriendlyName))
}
// Verify import
sb.WriteString(fmt.Sprintf(" $imported = Get-ChildItem 'Cert:\\%s\\%s\\%s' -ErrorAction SilentlyContinue\n",
c.config.StoreLocation, c.config.StoreName, thumbprint))
sb.WriteString(" if (-not $imported) { throw 'Certificate import verification failed' }\n")
// Remove expired certs with same subject (optional)
if c.config.RemoveExpired {
sb.WriteString(" $subject = $cert.Subject\n")
sb.WriteString(fmt.Sprintf(" Get-ChildItem 'Cert:\\%s\\%s' | Where-Object { $_.Subject -eq $subject -and $_.NotAfter -lt (Get-Date) -and $_.Thumbprint -ne '%s' } | Remove-Item -Force\n",
c.config.StoreLocation, c.config.StoreName, thumbprint))
}
sb.WriteString(fmt.Sprintf(" Write-Output 'SUCCESS:%s'\n", thumbprint))
sb.WriteString("} finally {\n")
sb.WriteString(" if (Test-Path $pfxPath) { Remove-Item $pfxPath -Force }\n")
sb.WriteString("}\n")
return sb.String()
}
// ValidateDeployment verifies that a certificate exists in the Windows Certificate Store.
func (c *Connector) ValidateDeployment(ctx context.Context, request target.ValidationRequest) (*target.ValidationResult, error) {
// Get thumbprint from metadata if available, otherwise query by serial
thumbprint := ""
if request.Metadata != nil {
thumbprint = request.Metadata["thumbprint"]
}
var script string
if thumbprint != "" {
script = fmt.Sprintf("$cert = Get-ChildItem 'Cert:\\%s\\%s\\%s' -ErrorAction SilentlyContinue; if ($cert) { Write-Output ('FOUND:' + $cert.Thumbprint + ':' + $cert.NotAfter.ToString('o')) } else { Write-Output 'NOT_FOUND' }",
c.config.StoreLocation, c.config.StoreName, thumbprint)
} else {
// Fallback: search by serial number
script = fmt.Sprintf("$cert = Get-ChildItem 'Cert:\\%s\\%s' | Where-Object { $_.SerialNumber -eq '%s' } | Select-Object -First 1; if ($cert) { Write-Output ('FOUND:' + $cert.Thumbprint + ':' + $cert.NotAfter.ToString('o')) } else { Write-Output 'NOT_FOUND' }",
c.config.StoreLocation, c.config.StoreName, request.Serial)
}
output, err := c.executor.Execute(ctx, script)
if err != nil {
return &target.ValidationResult{
Valid: false,
Serial: request.Serial,
Message: fmt.Sprintf("PowerShell query failed: %s", output),
ValidatedAt: time.Now(),
}, fmt.Errorf("validation query failed: %w", err)
}
if strings.HasPrefix(output, "FOUND:") {
parts := strings.SplitN(output, ":", 3)
foundThumb := ""
if len(parts) >= 2 {
foundThumb = parts[1]
}
return &target.ValidationResult{
Valid: true,
Serial: request.Serial,
TargetAddress: fmt.Sprintf("cert:\\%s\\%s", c.config.StoreLocation, c.config.StoreName),
Message: fmt.Sprintf("Certificate found in store (thumbprint: %s)", foundThumb),
ValidatedAt: time.Now(),
Metadata: map[string]string{
"thumbprint": foundThumb,
},
}, nil
}
return &target.ValidationResult{
Valid: false,
Serial: request.Serial,
Message: "Certificate not found in Windows Certificate Store",
ValidatedAt: time.Now(),
}, fmt.Errorf("certificate not found in %s\\%s", c.config.StoreLocation, c.config.StoreName)
}
// Ensure Connector implements target.Connector.
var _ target.Connector = (*Connector)(nil)
@@ -0,0 +1,412 @@
package wincertstore
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"fmt"
"log/slog"
"math/big"
"os"
"strings"
"testing"
"time"
"github.com/shankar0123/certctl/internal/connector/target"
)
func testLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))
}
// mockExecutor records PowerShell scripts and returns configurable responses.
type mockExecutor struct {
scripts []string
responses []string
errors []error
callIndex int
}
func (m *mockExecutor) Execute(ctx context.Context, script string) (string, error) {
m.scripts = append(m.scripts, script)
idx := m.callIndex
m.callIndex++
if idx < len(m.errors) && m.errors[idx] != nil {
resp := ""
if idx < len(m.responses) {
resp = m.responses[idx]
}
return resp, m.errors[idx]
}
if idx < len(m.responses) {
return m.responses[idx], nil
}
return "", nil
}
// generateTestCertAndKey creates a self-signed certificate and key for testing.
func generateTestCertAndKey() (string, string, error) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return "", "", err
}
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{CommonName: "test.example.com"},
NotBefore: time.Now().Add(-1 * time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
}
certDER, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
if err != nil {
return "", "", err
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
keyDER, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return "", "", err
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: keyDER})
return string(certPEM), string(keyPEM), nil
}
// --- ValidateConfig Tests ---
func TestValidateConfig_Success(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"store_name":"My","store_location":"LocalMachine"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err != nil {
t.Fatalf("expected success, got: %v", err)
}
}
func TestValidateConfig_Defaults(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err != nil {
t.Fatalf("expected success with defaults, got: %v", err)
}
if c.config.StoreName != "My" {
t.Errorf("expected default store_name 'My', got: %s", c.config.StoreName)
}
if c.config.StoreLocation != "LocalMachine" {
t.Errorf("expected default store_location 'LocalMachine', got: %s", c.config.StoreLocation)
}
if c.config.Mode != "local" {
t.Errorf("expected default mode 'local', got: %s", c.config.Mode)
}
}
func TestValidateConfig_InvalidJSON(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
err := c.ValidateConfig(context.Background(), json.RawMessage(`{bad`))
if err == nil {
t.Fatal("expected error for invalid JSON")
}
}
func TestValidateConfig_InvalidStoreName(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"store_name":"My; Drop-Database"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "invalid store_name") {
t.Fatalf("expected invalid store_name error, got: %v", err)
}
}
func TestValidateConfig_InvalidStoreLocation(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"store_location":"InvalidLocation"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "invalid store_location") {
t.Fatalf("expected invalid store_location error, got: %v", err)
}
}
func TestValidateConfig_CurrentUser(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"store_location":"CurrentUser"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err != nil {
t.Fatalf("expected success with CurrentUser, got: %v", err)
}
}
func TestValidateConfig_InvalidMode(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"mode":"ssh"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "invalid mode") {
t.Fatalf("expected invalid mode error, got: %v", err)
}
}
func TestValidateConfig_WinRM_MissingHost(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"mode":"winrm","winrm_username":"admin","winrm_password":"pass"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "winrm_host") {
t.Fatalf("expected winrm_host error, got: %v", err)
}
}
func TestValidateConfig_WinRM_MissingUsername(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"mode":"winrm","winrm_host":"host","winrm_password":"pass"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "winrm_username") {
t.Fatalf("expected winrm_username error, got: %v", err)
}
}
func TestValidateConfig_InvalidFriendlyName(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"friendly_name":"cert; rm -rf /"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err == nil || !strings.Contains(err.Error(), "invalid friendly_name") {
t.Fatalf("expected invalid friendly_name error, got: %v", err)
}
}
func TestValidateConfig_WithFriendlyName(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
cfg := `{"friendly_name":"My Production Cert"}`
err := c.ValidateConfig(context.Background(), json.RawMessage(cfg))
if err != nil {
t.Fatalf("expected success with friendly name, got: %v", err)
}
}
// --- DeployCertificate Tests ---
func TestDeployCertificate_Success(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{
responses: []string{"SUCCESS:AABBCCDD"},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
result, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
if !result.Success {
t.Error("expected success=true")
}
if result.TargetAddress != "cert:\\LocalMachine\\My" {
t.Errorf("expected target address cert:\\LocalMachine\\My, got: %s", result.TargetAddress)
}
if result.Metadata["store_name"] != "My" {
t.Errorf("expected store_name metadata 'My', got: %s", result.Metadata["store_name"])
}
// Verify the PowerShell script was called
if len(mock.scripts) != 1 {
t.Fatalf("expected 1 script call, got %d", len(mock.scripts))
}
script := mock.scripts[0]
if !strings.Contains(script, "Import-PfxCertificate") {
t.Error("expected Import-PfxCertificate in script")
}
if !strings.Contains(script, "Cert:\\LocalMachine\\My") {
t.Error("expected correct cert store path in script")
}
}
func TestDeployCertificate_MissingKey(t *testing.T) {
certPEM, _, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
})
if err == nil || !strings.Contains(err.Error(), "private key is required") {
t.Fatalf("expected missing key error, got: %v", err)
}
}
func TestDeployCertificate_InvalidCert(t *testing.T) {
c := NewWithExecutor(&Config{}, testLogger(), &mockExecutor{})
_, err := c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: "not-a-cert",
KeyPEM: "not-a-key",
})
if err == nil {
t.Fatal("expected error for invalid cert")
}
}
func TestDeployCertificate_ImportFailed(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{
responses: []string{"Access denied"},
errors: []error{fmt.Errorf("exit code 1")},
}
c := NewWithExecutor(&Config{}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err == nil || !strings.Contains(err.Error(), "PowerShell import failed") {
t.Fatalf("expected import failure error, got: %v", err)
}
}
func TestDeployCertificate_WithFriendlyName(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{responses: []string{"SUCCESS:AABB"}}
c := NewWithExecutor(&Config{
StoreName: "My",
FriendlyName: "Production API Cert",
}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
if !strings.Contains(mock.scripts[0], "FriendlyName") {
t.Error("expected FriendlyName in PowerShell script")
}
}
func TestDeployCertificate_WithRemoveExpired(t *testing.T) {
certPEM, keyPEM, err := generateTestCertAndKey()
if err != nil {
t.Fatalf("generate cert: %v", err)
}
mock := &mockExecutor{responses: []string{"SUCCESS:AABB"}}
c := NewWithExecutor(&Config{
StoreName: "My",
RemoveExpired: true,
}, testLogger(), mock)
_, err = c.DeployCertificate(context.Background(), target.DeploymentRequest{
CertPEM: certPEM,
KeyPEM: keyPEM,
})
if err != nil {
t.Fatalf("deploy failed: %v", err)
}
if !strings.Contains(mock.scripts[0], "Remove-Item") {
t.Error("expected Remove-Item for expired cert cleanup in script")
}
}
// --- ValidateDeployment Tests ---
func TestValidateDeployment_Success(t *testing.T) {
mock := &mockExecutor{
responses: []string{"FOUND:AABBCCDD:2027-01-01T00:00:00"},
}
c := NewWithExecutor(&Config{
StoreName: "My",
StoreLocation: "LocalMachine",
}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "01",
Metadata: map[string]string{
"thumbprint": "AABBCCDD",
},
})
if err != nil {
t.Fatalf("validate failed: %v", err)
}
if !result.Valid {
t.Error("expected valid=true")
}
if result.Metadata["thumbprint"] != "AABBCCDD" {
t.Errorf("expected thumbprint AABBCCDD, got: %s", result.Metadata["thumbprint"])
}
}
func TestValidateDeployment_NotFound(t *testing.T) {
mock := &mockExecutor{
responses: []string{"NOT_FOUND"},
}
c := NewWithExecutor(&Config{}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "01",
})
if err == nil {
t.Fatal("expected error for not found cert")
}
if result.Valid {
t.Error("expected valid=false")
}
}
func TestValidateDeployment_QueryFailed(t *testing.T) {
mock := &mockExecutor{
responses: []string{"error"},
errors: []error{fmt.Errorf("powershell error")},
}
c := NewWithExecutor(&Config{}, testLogger(), mock)
result, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "01",
})
if err == nil {
t.Fatal("expected error for query failure")
}
if result.Valid {
t.Error("expected valid=false")
}
}
func TestValidateDeployment_BySerial(t *testing.T) {
mock := &mockExecutor{
responses: []string{"FOUND:AABB:2027-01-01T00:00:00"},
}
c := NewWithExecutor(&Config{}, testLogger(), mock)
// No thumbprint in metadata — should query by serial
_, err := c.ValidateDeployment(context.Background(), target.ValidationRequest{
Serial: "DEADBEEF",
})
if err != nil {
t.Fatalf("validate failed: %v", err)
}
if !strings.Contains(mock.scripts[0], "SerialNumber") {
t.Error("expected serial number query in script")
}
}
+3 -1
View File
@@ -97,5 +97,7 @@ const (
TargetTypeEnvoy TargetType = "Envoy" TargetTypeEnvoy TargetType = "Envoy"
TargetTypePostfix TargetType = "Postfix" TargetTypePostfix TargetType = "Postfix"
TargetTypeDovecot TargetType = "Dovecot" TargetTypeDovecot TargetType = "Dovecot"
TargetTypeSSH TargetType = "SSH" TargetTypeSSH TargetType = "SSH"
TargetTypeWinCertStore TargetType = "WinCertStore"
TargetTypeJavaKeystore TargetType = "JavaKeystore"
) )
+5
View File
@@ -536,6 +536,11 @@ func (s *IssuerService) CreateIssuer(iss domain.Issuer) (*domain.Issuer, error)
if iss.Source == "" { if iss.Source == "" {
iss.Source = "database" 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 // Encrypt config
if len(iss.Config) > 0 { if len(iss.Config) > 0 {
+8 -1
View File
@@ -24,7 +24,9 @@ var validTargetTypes = map[domain.TargetType]bool{
domain.TargetTypeEnvoy: true, domain.TargetTypeEnvoy: true,
domain.TargetTypePostfix: true, domain.TargetTypePostfix: true,
domain.TargetTypeDovecot: true, domain.TargetTypeDovecot: true,
domain.TargetTypeSSH: true, domain.TargetTypeSSH: true,
domain.TargetTypeWinCertStore: true,
domain.TargetTypeJavaKeystore: true,
} }
// isValidTargetType checks if a type string is a known target type. // isValidTargetType checks if a type string is a known target type.
@@ -282,6 +284,11 @@ func (s *TargetService) CreateTarget(target domain.DeploymentTarget) (*domain.De
if target.Source == "" { if target.Source == "" {
target.Source = "database" 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 // Encrypt config
if len(target.Config) > 0 { if len(target.Config) > 0 {
+6
View File
@@ -169,6 +169,9 @@ export const getNotifications = (params: Record<string, string> = {}) => {
return fetchJSON<PaginatedResponse<Notification>>(`${BASE}/notifications?${qs}`); return fetchJSON<PaginatedResponse<Notification>>(`${BASE}/notifications?${qs}`);
}; };
export const getNotification = (id: string) =>
fetchJSON<Notification>(`${BASE}/notifications/${id}`);
export const markNotificationRead = (id: string) => export const markNotificationRead = (id: string) =>
fetchJSON<{ message: string }>(`${BASE}/notifications/${id}/read`, { method: 'POST' }); fetchJSON<{ message: string }>(`${BASE}/notifications/${id}/read`, { method: 'POST' });
@@ -178,6 +181,9 @@ export const getAuditEvents = (params: Record<string, string> = {}) => {
return fetchJSON<PaginatedResponse<AuditEvent>>(`${BASE}/audit?${qs}`); return fetchJSON<PaginatedResponse<AuditEvent>>(`${BASE}/audit?${qs}`);
}; };
export const getAuditEvent = (id: string) =>
fetchJSON<AuditEvent>(`${BASE}/audit/${id}`);
// Policies // Policies
export const getPolicies = (params: Record<string, string> = {}) => { export const getPolicies = (params: Record<string, string> = {}) => {
const qs = new URLSearchParams({ page: '1', per_page: '50', ...params }).toString(); const qs = new URLSearchParams({ page: '1', per_page: '50', ...params }).toString();
+8 -10
View File
@@ -10,11 +10,11 @@ export interface Certificate {
team_id: string; team_id: string;
renewal_policy_id: string; renewal_policy_id: string;
certificate_profile_id: string; certificate_profile_id: string;
serial_number: string; serial_number?: string;
fingerprint: string; fingerprint_sha256?: string;
key_algorithm: string; key_algorithm?: string;
key_size: number; key_size?: number;
issued_at: string; issued_at?: string;
expires_at: string; expires_at: string;
revoked_at?: string; revoked_at?: string;
revocation_reason?: string; revocation_reason?: string;
@@ -40,11 +40,9 @@ export const REVOCATION_REASONS = [
export interface CertificateVersion { export interface CertificateVersion {
id: string; id: string;
certificate_id: string; certificate_id: string;
version: number;
serial_number: string; serial_number: string;
fingerprint: string; fingerprint_sha256: string;
cert_pem: string; pem_chain: string;
chain_pem: string;
csr_pem: string; csr_pem: string;
not_before: string; not_before: string;
not_after: string; not_after: string;
@@ -80,7 +78,7 @@ export interface Job {
status: string; status: string;
attempts: number; attempts: number;
max_attempts: number; max_attempts: number;
error_message: string; last_error?: string;
scheduled_at: string; scheduled_at: string;
started_at: string; started_at: string;
completed_at: string; completed_at: string;
+2 -2
View File
@@ -1,9 +1,9 @@
export function formatDate(iso: string): string { export function formatDate(iso: string | undefined | null): string {
if (!iso) return '—'; if (!iso) return '—';
return new Date(iso).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' }); 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 '—'; if (!iso) return '—';
return new Date(iso).toLocaleString('en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); return new Date(iso).toLocaleString('en-US', { year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' });
} }
+21 -13
View File
@@ -29,19 +29,23 @@ export interface IssuerTypeConfig {
} }
/** /**
* Canonical type label map. Keys match what the backend API returns. * Canonical type label map. Keys MUST match backend IssuerType constants
* DB stores: local, acme, stepca, openssl, VaultPKI, DigiCert * defined in internal/domain/connector.go (e.g., "ACME", "GenericCA", "StepCA").
*/ */
export const typeLabels: Record<string, string> = { 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) local_ca: 'Local CA', // backward compat (some frontend references)
acme: 'ACME', ACME: 'ACME',
stepca: 'step-ca', acme: 'ACME', // backward compat for old DB records
openssl: 'OpenSSL/Custom', 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', VaultPKI: 'Vault PKI',
DigiCert: 'DigiCert', DigiCert: 'DigiCert',
Sectigo: 'Sectigo SCM', Sectigo: 'Sectigo SCM',
manual: 'Manual', GoogleCAS: 'Google CAS',
}; };
/** /**
@@ -50,7 +54,7 @@ export const typeLabels: Record<string, string> = {
*/ */
export const issuerTypes: IssuerTypeConfig[] = [ export const issuerTypes: IssuerTypeConfig[] = [
{ {
id: 'acme', id: 'ACME',
name: 'ACME', name: 'ACME',
description: "Let's Encrypt, ZeroSSL, or any ACME-compatible CA", description: "Let's Encrypt, ZeroSSL, or any ACME-compatible CA",
icon: '\uD83D\uDD12', icon: '\uD83D\uDD12',
@@ -64,7 +68,7 @@ export const issuerTypes: IssuerTypeConfig[] = [
], ],
}, },
{ {
id: 'local', id: 'GenericCA',
name: 'Local CA', name: 'Local CA',
description: 'Self-signed or subordinate CA for internal certificates', description: 'Self-signed or subordinate CA for internal certificates',
icon: '\uD83C\uDFE0', icon: '\uD83C\uDFE0',
@@ -74,14 +78,15 @@ export const issuerTypes: IssuerTypeConfig[] = [
], ],
}, },
{ {
id: 'stepca', id: 'StepCA',
name: 'step-ca', name: 'step-ca',
description: 'Smallstep private CA with JWK provisioner auth', description: 'Smallstep private CA with JWK provisioner auth',
icon: '\uD83D\uDC63', icon: '\uD83D\uDC63',
configFields: [ configFields: [
{ key: 'ca_url', label: 'CA URL', placeholder: 'https://ca.example.com', required: true }, { 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_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', name: 'OpenSSL/Custom',
description: 'Script-based signing with your own CA', description: 'Script-based signing with your own CA',
icon: '\uD83D\uDD27', icon: '\uD83D\uDD27',
@@ -188,7 +193,10 @@ export function getIssuerCatalogStatus(
} }
// Match both the canonical id and common aliases // Match both the canonical id and common aliases
const aliases: Record<string, string[]> = { 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 matchIds = aliases[t.id] || [t.id];
const matching = configuredIssuers.filter(i => matchIds.includes(i.type)); const matching = configuredIssuers.filter(i => matchIds.includes(i.type));
+12 -6
View File
@@ -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({ const { data: jobsData } = useQuery({
queryKey: ['jobs', { certificate_id: certId }], queryKey: ['jobs', { certificate_id: certId }],
queryFn: () => getJobs({ 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 days = daysUntil(cert.expires_at);
const isRevoked = cert.status === 'Revoked'; const isRevoked = cert.status === 'Revoked';
const isArchived = cert.status === 'Archived'; const isArchived = cert.status === 'Archived';
@@ -492,7 +498,7 @@ export default function CertificateDetailPage() {
)} )}
{/* Deployment Status Timeline */} {/* 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"> <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
{/* Certificate Info */} {/* Certificate Info */}
@@ -518,9 +524,9 @@ export default function CertificateDetailPage() {
})} })}
</span> </span>
) : '—'} /> ) : '—'} />
<InfoRow label="Serial Number" value={cert.serial_number || '—'} /> <InfoRow label="Serial Number" value={serialNumber || '—'} />
<InfoRow label="Fingerprint" value={ <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 Algorithm" value={cert.key_algorithm || '—'} />
<InfoRow label="Key Size" value={cert.key_size ? `${cert.key_size} bits` : '—'} /> <InfoRow label="Key Size" value={cert.key_size ? `${cert.key_size} bits` : '—'} />
@@ -556,7 +562,7 @@ export default function CertificateDetailPage() {
{/* Lifecycle */} {/* Lifecycle */}
<div className="bg-surface border border-surface-border rounded p-5 shadow-sm"> <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> <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={ <InfoRow label="Expires" value={
<span className={isRevoked ? 'text-red-600 line-through' : expiryColor(days)}> <span className={isRevoked ? 'text-red-600 line-through' : expiryColor(days)}>
{formatDate(cert.expires_at)} ({days <= 0 ? 'expired' : `${days} 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 key={v.id} className="flex items-center justify-between py-2 border-b border-surface-border/50 last:border-0">
<div> <div>
<div className="flex items-center gap-2"> <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>} {idx === 0 && <span className="text-xs bg-brand-100 text-brand-700 px-1.5 py-0.5 rounded">Current</span>}
</div> </div>
<div className="text-xs text-ink-faint font-mono">{v.serial_number}</div> <div className="text-xs text-ink-faint font-mono">{v.serial_number}</div>
+2 -2
View File
@@ -114,9 +114,9 @@ export default function JobDetailPage() {
} /> } />
)} )}
<InfoRow label="Attempts" value={`${job.attempts} / ${job.max_attempts}`} /> <InfoRow label="Attempts" value={`${job.attempts} / ${job.max_attempts}`} />
{job.error_message && ( {job.last_error && (
<InfoRow label="Error" value={ <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> </div>
+3 -3
View File
@@ -139,9 +139,9 @@ export default function JobsPage() {
{ {
key: 'error', key: 'error',
label: 'Error', label: 'Error',
render: (j) => j.status === 'Failed' && 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.error_message}> <span className="text-xs text-red-600 truncate max-w-[200px] inline-block" title={j.last_error}>
{j.error_message.length > 80 ? j.error_message.substring(0, 80) + '...' : j.error_message} {j.last_error.length > 80 ? j.last_error.substring(0, 80) + '...' : j.last_error}
</span> </span>
) : <span className="text-xs text-ink-faint"></span>, ) : <span className="text-xs text-ink-faint"></span>,
}, },
+3 -1
View File
@@ -22,6 +22,8 @@ const typeLabels: Record<string, string> = {
Postfix: 'Postfix', Postfix: 'Postfix',
Dovecot: 'Dovecot', Dovecot: 'Dovecot',
SSH: 'SSH', SSH: 'SSH',
WinCertStore: 'Windows Cert Store',
JavaKeystore: 'Java Keystore',
}; };
function InfoRow({ label, value }: { label: string; value: React.ReactNode }) { function InfoRow({ label, value }: { label: string; value: React.ReactNode }) {
@@ -229,7 +231,7 @@ export default function TargetDetailPage() {
{target.config && Object.keys(target.config).length > 0 ? ( {target.config && Object.keys(target.config).length > 0 ? (
<div className="space-y-0"> <div className="space-y-0">
{Object.entries(target.config).map(([key, val]) => { {Object.entries(target.config).map(([key, val]) => {
const sensitiveKeys = ['password', 'secret', 'token', 'key', 'winrm_password']; const sensitiveKeys = ['password', 'secret', 'token', 'key', 'passphrase', 'winrm_password', 'keystore_password'];
const isSensitive = sensitiveKeys.some(s => key.toLowerCase().includes(s)); const isSensitive = sensitiveKeys.some(s => key.toLowerCase().includes(s));
const displayVal = isSensitive && val ? '********' : String(val); const displayVal = isSensitive && val ? '********' : String(val);
return ( return (
+98 -11
View File
@@ -22,6 +22,8 @@ const typeLabels: Record<string, string> = {
F5: 'F5 BIG-IP', F5: 'F5 BIG-IP',
IIS: 'IIS', IIS: 'IIS',
SSH: 'SSH', SSH: 'SSH',
WinCertStore: 'Windows Cert Store',
JavaKeystore: 'Java Keystore',
}; };
const TARGET_TYPES = [ const TARGET_TYPES = [
@@ -36,6 +38,8 @@ const TARGET_TYPES = [
{ value: 'F5', label: 'F5 BIG-IP', description: 'iControl REST — cert upload, SSL profile update via proxy agent' }, { value: 'F5', label: 'F5 BIG-IP', description: 'iControl REST — cert upload, SSL profile update via proxy agent' },
{ value: 'IIS', label: 'IIS', description: 'Windows IIS via agent-local PowerShell or remote WinRM proxy agent' }, { value: 'IIS', label: 'IIS', description: 'Windows IIS via agent-local PowerShell or remote WinRM proxy agent' },
{ value: 'SSH', label: 'SSH', description: 'Agentless deployment via SSH/SFTP — deploy to any Linux/Unix server without installing an agent' }, { 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' },
]; ];
const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: string; required?: boolean }[]> = { const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: string; required?: boolean }[]> = {
@@ -43,18 +47,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: '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: '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: '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: [ Apache: [
{ key: 'cert_path', label: 'Certificate Path', placeholder: '/etc/apache2/ssl/cert.pem', required: true }, { 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: '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: '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: [ HAProxy: [
{ key: 'pem_path', label: 'Combined PEM Path', placeholder: '/etc/haproxy/certs/combined.pem', required: true }, { 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: 'reload_command', label: 'Reload Command', placeholder: 'systemctl reload haproxy' },
{ key: 'validate_cmd', label: 'Validate Command (optional)', placeholder: 'haproxy -c -f /etc/haproxy/haproxy.cfg' }, { key: 'validate_command', label: 'Validate Command (optional)', placeholder: 'haproxy -c -f /etc/haproxy/haproxy.cfg' },
], ],
Traefik: [ Traefik: [
{ key: 'cert_dir', label: 'Certificate Directory', placeholder: '/etc/traefik/certs', required: true }, { key: 'cert_dir', label: 'Certificate Directory', placeholder: '/etc/traefik/certs', required: true },
@@ -83,6 +89,7 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
{ key: 'validate_command', label: 'Validate Command', placeholder: 'postfix check' }, { key: 'validate_command', label: 'Validate Command', placeholder: 'postfix check' },
], ],
Dovecot: [ Dovecot: [
{ key: 'mode', label: 'Mode', placeholder: 'dovecot (auto-set)' },
{ key: 'cert_path', label: 'Certificate Path', placeholder: '/etc/dovecot/certs/cert.pem' }, { 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: 'key_path', label: 'Key Path', placeholder: '/etc/dovecot/certs/key.pem' },
{ key: 'chain_path', label: 'Chain Path (optional)', placeholder: '/etc/dovecot/certs/chain.pem' }, { key: 'chain_path', label: 'Chain Path (optional)', placeholder: '/etc/dovecot/certs/chain.pem' },
@@ -100,6 +107,7 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
{ key: 'timeout', label: 'Timeout (seconds)', placeholder: '30' }, { key: 'timeout', label: 'Timeout (seconds)', placeholder: '30' },
], ],
IIS: [ 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: 'site_name', label: 'IIS Site Name', placeholder: 'Default Web Site', required: true },
{ key: 'cert_store', label: 'Certificate Store', placeholder: 'My', required: true }, { key: 'cert_store', label: 'Certificate Store', placeholder: 'My', required: true },
{ key: 'port', label: 'HTTPS Port', placeholder: '443' }, { key: 'port', label: 'HTTPS Port', placeholder: '443' },
@@ -107,12 +115,13 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
{ key: 'binding_info', label: 'Host Header (SNI)', placeholder: 'www.example.com' }, { key: 'binding_info', label: 'Host Header (SNI)', placeholder: 'www.example.com' },
{ key: 'sni', label: 'Enable SNI', placeholder: 'true or false' }, { key: 'sni', label: 'Enable SNI', placeholder: 'true or false' },
{ key: 'mode', label: 'Deployment Mode', placeholder: 'local (default) or winrm' }, { 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_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_port', label: 'WinRM Port', placeholder: '5985 (HTTP) or 5986 (HTTPS)' },
{ key: 'winrm.winrm_username', label: 'WinRM Username', placeholder: 'Administrator' }, { key: 'winrm_username', label: 'WinRM Username', placeholder: 'Administrator' },
{ key: 'winrm.winrm_password', label: 'WinRM Password', placeholder: '(sensitive)' }, { key: 'winrm_password', label: 'WinRM Password', placeholder: '(sensitive)' },
{ key: 'winrm.winrm_https', label: 'WinRM Use HTTPS', placeholder: 'true or false' }, { key: 'winrm_https', label: 'WinRM Use HTTPS', placeholder: 'true or false' },
{ key: 'winrm.winrm_insecure', label: 'WinRM Skip TLS Verify', placeholder: 'false' }, { key: 'winrm_insecure', label: 'WinRM Skip TLS Verify', placeholder: 'false' },
{ key: 'winrm_timeout', label: 'WinRM Timeout (seconds)', placeholder: '60' },
], ],
SSH: [ SSH: [
{ key: 'host', label: 'SSH Host', placeholder: '192.168.1.100 or server.example.com', required: true }, { key: 'host', label: 'SSH Host', placeholder: '192.168.1.100 or server.example.com', required: true },
@@ -120,13 +129,39 @@ const CONFIG_FIELDS: Record<string, { key: string; label: string; placeholder: s
{ key: 'user', label: 'SSH Username', placeholder: 'root or certctl', required: true }, { key: 'user', label: 'SSH Username', placeholder: 'root or certctl', required: true },
{ key: 'auth_method', label: 'Auth Method', placeholder: 'key (default) or password' }, { 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_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: '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: '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: '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: '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: 'reload_command', label: 'Reload Command (optional)', placeholder: 'systemctl reload nginx' },
{ key: 'timeout', label: 'Connection Timeout (seconds)', placeholder: '30 (default)' }, { key: 'timeout', label: 'Connection Timeout (seconds)', placeholder: '30 (default)' },
], ],
WinCertStore: [
{ key: 'store_name', label: 'Certificate Store', placeholder: 'My (default)', required: true },
{ key: 'store_location', label: 'Store Location', placeholder: 'LocalMachine (default) or CurrentUser' },
{ key: 'friendly_name', label: 'Friendly Name (optional)', placeholder: 'My Production Cert' },
{ key: 'remove_expired', label: 'Remove Expired Certs', placeholder: 'false (default)' },
{ key: 'mode', label: 'Deployment Mode', placeholder: 'local (default) or winrm' },
{ key: 'winrm_host', label: 'WinRM Host (remote mode)', placeholder: 'win-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' },
],
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)' },
],
}; };
function CreateTargetWizard({ onClose, onSuccess }: { onClose: () => void; onSuccess: () => void }) { function CreateTargetWizard({ onClose, onSuccess }: { onClose: () => void; onSuccess: () => void }) {
@@ -137,12 +172,64 @@ function CreateTargetWizard({ onClose, onSuccess }: { onClose: () => void; onSuc
const [config, setConfig] = useState<Record<string, string>>({}); const [config, setConfig] = useState<Record<string, string>>({});
const [error, setError] = useState(''); 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({ const mutation = useMutation({
mutationFn: () => createTarget({ mutationFn: () => createTarget({
name, name,
type: targetType, type: targetType,
agent_id: agentId, agent_id: agentId,
config: Object.fromEntries(Object.entries(config).filter(([, v]) => v)), config: buildConfigPayload(),
}), }),
onSuccess: () => onSuccess(), onSuccess: () => onSuccess(),
onError: (err: Error) => setError(err.message), onError: (err: Error) => setError(err.message),