mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 17:31:30 +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.
149 lines
5.2 KiB
Go
149 lines
5.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
|
|
"github.com/certctl-io/certctl/internal/domain"
|
|
"github.com/certctl-io/certctl/internal/repository"
|
|
)
|
|
|
|
// ErrBulkReassignOwnerNotFound is the typed sentinel for a non-existent
|
|
// target OwnerID. The handler maps it to 400 (bad input — the operator
|
|
// picked an owner that doesn't exist) rather than 500 (server error).
|
|
// Sentinel-error rather than substring-error matches the project's
|
|
// post-M-1 error-mapping convention.
|
|
var ErrBulkReassignOwnerNotFound = errors.New("owner not found")
|
|
|
|
// BulkReassignmentService coordinates bulk owner-reassignment of
|
|
// certificates.
|
|
//
|
|
// L-2 closure (cat-l-8a1fb258a38a): the GUI used to loop
|
|
// `await updateCertificate(id, { owner_id })` over the selection at
|
|
// `web/src/pages/CertificatesPage.tsx::handleReassign`. Post-L-2 the
|
|
// GUI POSTs once. Narrower than BulkRenewal: explicit IDs only, no
|
|
// criteria-mode (criteria-mode reassignment doesn't have a strong use
|
|
// case — operators query first then reassign by ID).
|
|
//
|
|
// Validation order: empty IDs → 400, missing OwnerID → 400, OwnerID
|
|
// not in owners table → 400 (ErrBulkReassignOwnerNotFound). Resolving
|
|
// the owner upfront means we fail-fast without mutating any cert if
|
|
// the operator typo'd the owner ID.
|
|
type BulkReassignmentService struct {
|
|
certRepo repository.CertificateRepository
|
|
ownerRepo repository.OwnerRepository
|
|
auditService *AuditService
|
|
logger *slog.Logger
|
|
}
|
|
|
|
// NewBulkReassignmentService creates a new BulkReassignmentService.
|
|
func NewBulkReassignmentService(
|
|
certRepo repository.CertificateRepository,
|
|
ownerRepo repository.OwnerRepository,
|
|
auditService *AuditService,
|
|
logger *slog.Logger,
|
|
) *BulkReassignmentService {
|
|
return &BulkReassignmentService{
|
|
certRepo: certRepo,
|
|
ownerRepo: ownerRepo,
|
|
auditService: auditService,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
// BulkReassign updates owner_id (and optionally team_id) on every cert
|
|
// in request.CertificateIDs. Skips certs whose owner_id already equals
|
|
// the target (silent no-op — surfaced as TotalSkipped++, not as a fake
|
|
// "succeeded" count, so operators see "5 of your 10 selections were
|
|
// no-ops because Alice already owned them" without triaging fake
|
|
// errors).
|
|
//
|
|
// Partial failures don't abort the batch — the failing cert lands in
|
|
// Errors[]; the loop continues. Mirrors BulkRevocationService and
|
|
// BulkRenewalService partial-failure semantics.
|
|
//
|
|
// Audit: a single audit event is emitted at the end with the criteria
|
|
// + counts. NOT N events.
|
|
func (s *BulkReassignmentService) BulkReassign(ctx context.Context, request domain.BulkReassignmentRequest, actor string) (*domain.BulkReassignmentResult, error) {
|
|
if request.IsEmpty() {
|
|
return nil, fmt.Errorf("at least one certificate_id is required")
|
|
}
|
|
if request.OwnerID == "" {
|
|
return nil, fmt.Errorf("owner_id is required")
|
|
}
|
|
|
|
// Validate the target owner exists BEFORE touching any cert. This
|
|
// fail-fast pattern means an operator who typo'd 'o-alic' (missing
|
|
// 'e') doesn't half-reassign 50 certs before the 51st surfaces the
|
|
// FK violation.
|
|
if _, err := s.ownerRepo.Get(ctx, request.OwnerID); err != nil {
|
|
return nil, fmt.Errorf("%w: %s", ErrBulkReassignOwnerNotFound, request.OwnerID)
|
|
}
|
|
|
|
result := &domain.BulkReassignmentResult{}
|
|
|
|
for _, id := range request.CertificateIDs {
|
|
cert, err := s.certRepo.Get(ctx, id)
|
|
if err != nil {
|
|
result.TotalFailed++
|
|
result.Errors = append(result.Errors, domain.BulkOperationError{
|
|
CertificateID: id,
|
|
Error: fmt.Sprintf("failed to fetch certificate: %v", err),
|
|
})
|
|
continue
|
|
}
|
|
result.TotalMatched++
|
|
|
|
// No-op skip: cert already owned by the target. team_id may
|
|
// still differ — we still skip if owner matches AND
|
|
// team_id-update is a no-op (team unchanged or team_id field
|
|
// not set on the request). This prevents fake "reassigned"
|
|
// counts when nothing actually changed.
|
|
ownerUnchanged := cert.OwnerID == request.OwnerID
|
|
teamUnchanged := request.TeamID == "" || cert.TeamID == request.TeamID
|
|
if ownerUnchanged && teamUnchanged {
|
|
result.TotalSkipped++
|
|
continue
|
|
}
|
|
|
|
cert.OwnerID = request.OwnerID
|
|
if request.TeamID != "" {
|
|
cert.TeamID = request.TeamID
|
|
}
|
|
if err := s.certRepo.Update(ctx, cert); err != nil {
|
|
result.TotalFailed++
|
|
result.Errors = append(result.Errors, domain.BulkOperationError{
|
|
CertificateID: id,
|
|
Error: fmt.Sprintf("failed to update certificate: %v", err),
|
|
})
|
|
s.logger.Warn("bulk reassignment: update failed",
|
|
"certificate_id", id, "error", err)
|
|
continue
|
|
}
|
|
result.TotalReassigned++
|
|
}
|
|
|
|
// Single bulk audit event at the end.
|
|
auditDetails := map[string]interface{}{
|
|
"owner_id": request.OwnerID,
|
|
"certificate_ids": strings.Join(request.CertificateIDs, ","),
|
|
"total_matched": result.TotalMatched,
|
|
"total_reassigned": result.TotalReassigned,
|
|
"total_skipped": result.TotalSkipped,
|
|
"total_failed": result.TotalFailed,
|
|
}
|
|
if request.TeamID != "" {
|
|
auditDetails["team_id"] = request.TeamID
|
|
}
|
|
if err := s.auditService.RecordEvent(ctx, actor, domain.ActorTypeUser,
|
|
"bulk_reassignment_initiated", "certificate", "bulk",
|
|
auditDetails); err != nil {
|
|
s.logger.Error("failed to record bulk reassignment audit event", "error", err)
|
|
}
|
|
|
|
return result, nil
|
|
}
|