mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 16:31:33 +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.
227 lines
6.5 KiB
Go
227 lines
6.5 KiB
Go
//go:build integration
|
|
|
|
// Package test contains the deploy-hardening I Phase 11 cross-
|
|
// cutting end-to-end integration tests. These exercise the
|
|
// internal/deploy package's load-bearing invariants end-to-end:
|
|
//
|
|
// - atomicity: kill mid-deploy → file is fully old or fully new;
|
|
// never torn.
|
|
// - post-verify: deploy a wrong-fingerprint cert + the connector's
|
|
// verify hook → the rollback wire restores the previous bytes.
|
|
// - idempotency: deploy the same bytes twice → the second attempt
|
|
// is a no-op (no PreCommit/PostCommit calls).
|
|
// - concurrency: N simultaneous deploys to the same destination
|
|
// serialize via the deploy package's file-level mutex.
|
|
//
|
|
// Run via `INTEGRATION=1 go test -tags integration -race ./deploy/test/... -run Deploy`.
|
|
package integration
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/certctl-io/certctl/internal/deploy"
|
|
)
|
|
|
|
// TestDeploy_Atomicity_FileIsAlwaysOldOrNew pins the load-bearing
|
|
// POSIX-rename atomicity invariant. A reader hammering the
|
|
// destination during 30 alternating writes either sees the OLD
|
|
// bytes or the NEW bytes — never an intermediate state. Closes
|
|
// the operator-facing question "is my cert deploy interruption-
|
|
// safe?".
|
|
func TestDeploy_Atomicity_FileIsAlwaysOldOrNew(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "cert.pem")
|
|
old := []byte(strings.Repeat("OLD-CERT-PEM-", 200))
|
|
newer := []byte(strings.Repeat("NEW-CERT-PEM-", 200))
|
|
if err := os.WriteFile(path, old, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
stop := make(chan struct{})
|
|
var torn atomic.Bool
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for {
|
|
select {
|
|
case <-stop:
|
|
return
|
|
default:
|
|
}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
s := string(b)
|
|
if s != string(old) && s != string(newer) {
|
|
torn.Store(true)
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
for i := 0; i < 30; i++ {
|
|
writeBytes := old
|
|
if i%2 == 0 {
|
|
writeBytes = newer
|
|
}
|
|
if _, err := deploy.AtomicWriteFile(context.Background(), path, writeBytes, deploy.WriteOptions{
|
|
SkipIdempotent: true,
|
|
}); err != nil {
|
|
t.Fatalf("write %d: %v", i, err)
|
|
}
|
|
}
|
|
close(stop)
|
|
wg.Wait()
|
|
if torn.Load() {
|
|
t.Error("torn read observed (rename atomicity broken)")
|
|
}
|
|
}
|
|
|
|
// TestDeploy_PostVerify_WrongCertTriggersRollback simulates a
|
|
// mis-deployed cert: the deploy.Apply succeeds at the file-write
|
|
// + reload level, but the connector's post-deploy verify (run
|
|
// AFTER Apply returns) detects the SHA-256 mismatch and rolls
|
|
// back manually using the BackupPaths that Apply returned. The
|
|
// final on-disk state matches the OLD bytes; the rollback wire
|
|
// works end-to-end.
|
|
func TestDeploy_PostVerify_WrongCertTriggersRollback(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cert := filepath.Join(dir, "cert.pem")
|
|
if err := os.WriteFile(cert, []byte("OLD-CERT"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
plan := deploy.Plan{
|
|
Files: []deploy.File{{Path: cert, Bytes: []byte("WRONG-CERT")}},
|
|
PostCommit: func(_ context.Context) error {
|
|
// Reload would normally verify the cert via the post-deploy
|
|
// TLS handshake. Here we simulate the verify failure by
|
|
// returning an error from PostCommit (which triggers the
|
|
// deploy package's automatic rollback).
|
|
//
|
|
// On the first call (the real deploy), return an error so
|
|
// the rollback fires; on the second call (the rollback's
|
|
// re-PostCommit against the restored bytes), succeed so
|
|
// rollback completes cleanly.
|
|
return errors.New("post-deploy verify: SHA-256 mismatch")
|
|
},
|
|
}
|
|
|
|
// First call to PostCommit fails; the rollback's second call
|
|
// would also fail with the same handler — so we use a stateful
|
|
// counter.
|
|
var postCalls int32
|
|
plan.PostCommit = func(_ context.Context) error {
|
|
if atomic.AddInt32(&postCalls, 1) == 1 {
|
|
return errors.New("post-deploy verify: SHA-256 mismatch")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
_, err := deploy.Apply(context.Background(), plan)
|
|
if !errors.Is(err, deploy.ErrReloadFailed) {
|
|
t.Fatalf("got %v, want ErrReloadFailed", err)
|
|
}
|
|
got, _ := os.ReadFile(cert)
|
|
if string(got) != "OLD-CERT" {
|
|
t.Errorf("cert after rollback = %q, want OLD-CERT", got)
|
|
}
|
|
if atomic.LoadInt32(&postCalls) != 2 {
|
|
t.Errorf("PostCommit calls = %d, want 2 (1 deploy + 1 rollback re-call)", postCalls)
|
|
}
|
|
}
|
|
|
|
// TestDeploy_Idempotency_SecondDeployIsNoOp pins the SHA-256
|
|
// short-circuit. Defends against agent-restart retry storms that
|
|
// otherwise hammer targets with no-op reloads.
|
|
func TestDeploy_Idempotency_SecondDeployIsNoOp(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cert := filepath.Join(dir, "cert.pem")
|
|
bytes := []byte("STABLE-CERT-PEM")
|
|
if err := os.WriteFile(cert, bytes, 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var preCalls, postCalls int32
|
|
plan := deploy.Plan{
|
|
Files: []deploy.File{{Path: cert, Bytes: bytes}},
|
|
PreCommit: func(_ context.Context, _ map[string]string) error {
|
|
atomic.AddInt32(&preCalls, 1)
|
|
return nil
|
|
},
|
|
PostCommit: func(_ context.Context) error {
|
|
atomic.AddInt32(&postCalls, 1)
|
|
return nil
|
|
},
|
|
}
|
|
res, err := deploy.Apply(context.Background(), plan)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !res.SkippedAsIdempotent {
|
|
t.Error("expected SkippedAsIdempotent=true")
|
|
}
|
|
if preCalls != 0 || postCalls != 0 {
|
|
t.Errorf("expected 0 calls, got %d/%d", preCalls, postCalls)
|
|
}
|
|
}
|
|
|
|
// TestDeploy_Concurrent_SamePathsSerialize fires N simultaneous
|
|
// deploys to the same destination. The deploy package's file-
|
|
// level mutex must serialize them: max-in-flight = 1.
|
|
func TestDeploy_Concurrent_SamePathsSerialize(t *testing.T) {
|
|
dir := t.TempDir()
|
|
cert := filepath.Join(dir, "cert.pem")
|
|
|
|
const N = 8
|
|
var inFlight, maxInFlight int32
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < N; i++ {
|
|
wg.Add(1)
|
|
go func(idx int) {
|
|
defer wg.Done()
|
|
plan := deploy.Plan{
|
|
Files: []deploy.File{{
|
|
Path: cert,
|
|
Bytes: []byte(fmt.Sprintf("WRITER-%d", idx)),
|
|
}},
|
|
SkipIdempotent: true,
|
|
PostCommit: func(_ context.Context) error {
|
|
n := atomic.AddInt32(&inFlight, 1)
|
|
for {
|
|
m := atomic.LoadInt32(&maxInFlight)
|
|
if n <= m || atomic.CompareAndSwapInt32(&maxInFlight, m, n) {
|
|
break
|
|
}
|
|
}
|
|
time.Sleep(2 * time.Millisecond)
|
|
atomic.AddInt32(&inFlight, -1)
|
|
return nil
|
|
},
|
|
}
|
|
if _, err := deploy.Apply(context.Background(), plan); err != nil {
|
|
t.Errorf("Apply %d: %v", idx, err)
|
|
}
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
if maxInFlight > 1 {
|
|
t.Errorf("max in-flight = %d, want 1 (mutex broken)", maxInFlight)
|
|
}
|
|
got, _ := os.ReadFile(cert)
|
|
if !strings.HasPrefix(string(got), "WRITER-") {
|
|
t.Errorf("file content not from any writer: %q", got)
|
|
}
|
|
}
|