mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 17:41:29 +00:00
8b75e0311b
Mechanical sed across the main go.mod's module declaration, the f5-mock-icontrol
sub-module's go.mod, every Go file's import path (361 files), and a rebuild of
the checked-in f5-mock-icontrol binary so its embedded build-info reflects the
new module path. No behavior change.
Choice B from cowork/transfer-certctl-to-org.md, executed 2026-05-04. Choice A
(keep module path declared as github.com/shankar0123/certctl regardless of
repo URL) shipped on the day of the org transfer (2026-05-03) since we had no
external Go consumers; this commit closes that deferral.
Backward-compat: GitHub HTTP redirects continue to forward
github.com/shankar0123/certctl → github.com/certctl-io/certctl at the URL
level, but Go's module proxy uses the path declared in go.mod as the
canonical name. Pre-fix, anyone trying `go get github.com/certctl-io/certctl/...`
hit a "module path mismatch" error because go.mod said
github.com/shankar0123/certctl and the URL they fetched it from said
certctl-io/certctl. Post-fix, the canonical name and the URL agree, so
go get / go install / external Go consumers / Go-tooling integrations
work cleanly via either the new path (preferred) or the old path (which
redirects and Go follows the redirect for source fetch).
Anyone still importing the old path inside their own code keeps working
provided they update their go.mod's `require` line to match — the module
path declared in their consumer's go.sum / go.mod is the authoritative
import name, so a mass sed across their import statements is the migration
on the consumer side. No external consumers exist today.
Diff shape:
361 *.go files — import path replacement only
2 go.mod — module declaration replacement only
1 binary — deploy/test/f5-mock-icontrol/f5-mock-icontrol rebuilt
so embedded build-info reflects the new path (8618965 vs
8618933 bytes; 32-byte diff is the build-info change)
Total: 364 files, 730 insertions / 730 deletions, net-zero size, pure
mechanical substitution.
Verification:
gofmt: 17 files needed re-alignment after sed (the new path is one char
shorter than the old, so column-aligned import groups drifted). Applied
`gofmt -w` to fix.
go mod tidy: clean exit on both modules.
go vet ./...: clean exit.
go build ./...: clean exit.
go test -short -count=1 on representative packages: all green
(internal/domain, internal/validation, internal/crypto, internal/crypto/signer,
cmd/agent). Test output now reads `ok github.com/certctl-io/certctl/...`
confirming the module path resolves correctly.
binary: f5-mock-icontrol rebuilt; `strings | grep shankar0123` returns
nothing; `strings | grep certctl-io/certctl` shows the new module path
embedded in build-info.
Files intentionally NOT touched in this commit:
README.md / CHANGELOG.md / docs/ / etc. — already swept to certctl-io
URLs in commit 0729ee4 (the post-transfer URL refresh). This commit is
purely the Go-tooling layer.
Scarf pixels (`shankar0123.docker.scarf.sh/...`) — Scarf-account
namespace, not a Go import or GitHub repo URL. Stays.
This is a non-blocking, non-customer-impacting change. Operators pulling
container images, running `make verify`, hitting the API, or installing the
agent see no functional difference. Only Go-tooling consumers (none today)
are affected, and they're enabled — not broken — by this commit.
258 lines
7.8 KiB
Go
258 lines
7.8 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/certctl-io/certctl/internal/repository"
|
|
"time"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// ProfileRepository implements repository.CertificateProfileRepository
|
|
type ProfileRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
// NewProfileRepository creates a new ProfileRepository
|
|
func NewProfileRepository(db *sql.DB) *ProfileRepository {
|
|
return &ProfileRepository{db: db}
|
|
}
|
|
|
|
// List returns all certificate profiles
|
|
func (r *ProfileRepository) List(ctx context.Context) ([]*domain.CertificateProfile, error) {
|
|
rows, err := r.db.QueryContext(ctx, `
|
|
SELECT id, name, description, allowed_key_algorithms, max_ttl_seconds,
|
|
allowed_ekus, required_san_patterns, spiffe_uri_pattern,
|
|
allow_short_lived, must_staple, required_csr_attributes,
|
|
COALESCE(acme_auth_mode, 'trust_authenticated'),
|
|
enabled, created_at, updated_at
|
|
FROM certificate_profiles
|
|
ORDER BY created_at DESC
|
|
`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to query profiles: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var profiles []*domain.CertificateProfile
|
|
for rows.Next() {
|
|
p, err := scanProfile(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
profiles = append(profiles, p)
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("error iterating profile rows: %w", err)
|
|
}
|
|
|
|
return profiles, nil
|
|
}
|
|
|
|
// Get retrieves a certificate profile by ID
|
|
func (r *ProfileRepository) Get(ctx context.Context, id string) (*domain.CertificateProfile, error) {
|
|
row := r.db.QueryRowContext(ctx, `
|
|
SELECT id, name, description, allowed_key_algorithms, max_ttl_seconds,
|
|
allowed_ekus, required_san_patterns, spiffe_uri_pattern,
|
|
allow_short_lived, must_staple, required_csr_attributes,
|
|
COALESCE(acme_auth_mode, 'trust_authenticated'),
|
|
enabled, created_at, updated_at
|
|
FROM certificate_profiles
|
|
WHERE id = $1
|
|
`, id)
|
|
|
|
p, err := scanProfile(row)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, fmt.Errorf("profile not found: %w", repository.ErrNotFound)
|
|
}
|
|
return nil, fmt.Errorf("failed to query profile: %w", err)
|
|
}
|
|
|
|
return p, nil
|
|
}
|
|
|
|
// Create stores a new certificate profile
|
|
func (r *ProfileRepository) Create(ctx context.Context, profile *domain.CertificateProfile) error {
|
|
if profile.ID == "" {
|
|
profile.ID = uuid.New().String()
|
|
}
|
|
if profile.CreatedAt.IsZero() {
|
|
profile.CreatedAt = time.Now()
|
|
}
|
|
if profile.UpdatedAt.IsZero() {
|
|
profile.UpdatedAt = time.Now()
|
|
}
|
|
|
|
algJSON, err := json.Marshal(profile.AllowedKeyAlgorithms)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal allowed_key_algorithms: %w", err)
|
|
}
|
|
ekuJSON, err := json.Marshal(profile.AllowedEKUs)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal allowed_ekus: %w", err)
|
|
}
|
|
sanJSON, err := json.Marshal(profile.RequiredSANPatterns)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal required_san_patterns: %w", err)
|
|
}
|
|
// Phase 6.1: required_csr_attributes is the per-profile EST csrattrs hint
|
|
// list. Marshal as JSONB; nil → "[]" via the json.Marshal stdlib contract.
|
|
csrAttrsJSON, err := json.Marshal(profile.RequiredCSRAttributes)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal required_csr_attributes: %w", err)
|
|
}
|
|
|
|
err = r.db.QueryRowContext(ctx, `
|
|
INSERT INTO certificate_profiles (
|
|
id, name, description, allowed_key_algorithms, max_ttl_seconds,
|
|
allowed_ekus, required_san_patterns, spiffe_uri_pattern,
|
|
allow_short_lived, must_staple, required_csr_attributes,
|
|
enabled, created_at, updated_at
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
|
RETURNING id
|
|
`, profile.ID, profile.Name, profile.Description, algJSON, profile.MaxTTLSeconds,
|
|
ekuJSON, sanJSON, profile.SPIFFEURIPattern,
|
|
profile.AllowShortLived, profile.MustStaple, csrAttrsJSON,
|
|
profile.Enabled, profile.CreatedAt, profile.UpdatedAt).Scan(&profile.ID)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create profile: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Update modifies an existing certificate profile
|
|
func (r *ProfileRepository) Update(ctx context.Context, profile *domain.CertificateProfile) error {
|
|
profile.UpdatedAt = time.Now()
|
|
|
|
algJSON, err := json.Marshal(profile.AllowedKeyAlgorithms)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal allowed_key_algorithms: %w", err)
|
|
}
|
|
ekuJSON, err := json.Marshal(profile.AllowedEKUs)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal allowed_ekus: %w", err)
|
|
}
|
|
sanJSON, err := json.Marshal(profile.RequiredSANPatterns)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal required_san_patterns: %w", err)
|
|
}
|
|
csrAttrsJSON, err := json.Marshal(profile.RequiredCSRAttributes)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal required_csr_attributes: %w", err)
|
|
}
|
|
|
|
result, err := r.db.ExecContext(ctx, `
|
|
UPDATE certificate_profiles SET
|
|
name = $1,
|
|
description = $2,
|
|
allowed_key_algorithms = $3,
|
|
max_ttl_seconds = $4,
|
|
allowed_ekus = $5,
|
|
required_san_patterns = $6,
|
|
spiffe_uri_pattern = $7,
|
|
allow_short_lived = $8,
|
|
must_staple = $9,
|
|
required_csr_attributes = $10,
|
|
enabled = $11,
|
|
updated_at = $12
|
|
WHERE id = $13
|
|
`, profile.Name, profile.Description, algJSON, profile.MaxTTLSeconds,
|
|
ekuJSON, sanJSON, profile.SPIFFEURIPattern,
|
|
profile.AllowShortLived, profile.MustStaple, csrAttrsJSON,
|
|
profile.Enabled, profile.UpdatedAt, profile.ID)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update profile: %w", err)
|
|
}
|
|
|
|
rows, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
|
}
|
|
|
|
if rows == 0 {
|
|
return fmt.Errorf("profile not found: %w", repository.ErrNotFound)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Delete removes a certificate profile
|
|
func (r *ProfileRepository) Delete(ctx context.Context, id string) error {
|
|
result, err := r.db.ExecContext(ctx, "DELETE FROM certificate_profiles WHERE id = $1", id)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete profile: %w", err)
|
|
}
|
|
|
|
rows, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
|
}
|
|
|
|
if rows == 0 {
|
|
return fmt.Errorf("profile not found: %w", repository.ErrNotFound)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// scanProfile scans a certificate profile from a row or rows
|
|
func scanProfile(scanner interface {
|
|
Scan(...interface{}) error
|
|
}) (*domain.CertificateProfile, error) {
|
|
var p domain.CertificateProfile
|
|
var algJSON, ekuJSON, sanJSON, csrAttrsJSON []byte
|
|
|
|
err := scanner.Scan(
|
|
&p.ID, &p.Name, &p.Description, &algJSON, &p.MaxTTLSeconds,
|
|
&ekuJSON, &sanJSON, &p.SPIFFEURIPattern,
|
|
&p.AllowShortLived, &p.MustStaple, &csrAttrsJSON,
|
|
&p.ACMEAuthMode,
|
|
&p.Enabled, &p.CreatedAt, &p.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to scan profile: %w", err)
|
|
}
|
|
|
|
if len(algJSON) > 0 {
|
|
if err := json.Unmarshal(algJSON, &p.AllowedKeyAlgorithms); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal allowed_key_algorithms: %w", err)
|
|
}
|
|
} else {
|
|
p.AllowedKeyAlgorithms = domain.DefaultKeyAlgorithms()
|
|
}
|
|
|
|
if len(ekuJSON) > 0 {
|
|
if err := json.Unmarshal(ekuJSON, &p.AllowedEKUs); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal allowed_ekus: %w", err)
|
|
}
|
|
} else {
|
|
p.AllowedEKUs = domain.DefaultEKUs()
|
|
}
|
|
|
|
if len(sanJSON) > 0 {
|
|
if err := json.Unmarshal(sanJSON, &p.RequiredSANPatterns); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal required_san_patterns: %w", err)
|
|
}
|
|
}
|
|
// Phase 6.1: required_csr_attributes column ships with default '[]', so
|
|
// every existing row scans into an empty slice (back-compat 204 stub
|
|
// behavior). Older rows from before the migration could land here as
|
|
// empty bytes — guard against that with the same len() check pattern.
|
|
if len(csrAttrsJSON) > 0 {
|
|
if err := json.Unmarshal(csrAttrsJSON, &p.RequiredCSRAttributes); err != nil {
|
|
return nil, fmt.Errorf("failed to unmarshal required_csr_attributes: %w", err)
|
|
}
|
|
}
|
|
|
|
return &p, nil
|
|
}
|