Files
certctl/internal/service/verification.go
T
shankar0123 21aeed4f4e legal: addlicense headers + normalize legacy variants (Phase 0 RED-4)
Phase 0 closure (Path B2, post-rewrite):

addlicense sweep — adds the canonical certctl LLC copyright + BUSL-1.1
SPDX header to every production Go file. Template:

  // Copyright 2026 certctl LLC. All rights reserved.
  // SPDX-License-Identifier: BUSL-1.1

Coverage: 338 / 338 production Go files (cmd/ + internal/, excluding
*_test.go and **/testdata/**). Pre-sweep coverage was 22 / 338 (6.5%);
post-sweep is 338 / 338 (100%).

Normalized 22 pre-existing legacy headers (`// Copyright (c) certctl`
+ `// SPDX-License-Identifier: BSL-1.1`) and 1 file using a
`Certctl Contributors` attribution. The legacy SPDX ID `BSL-1.1`
is non-standard; the official SPDX identifier for Business Source
License 1.1 is `BUSL-1.1` (capital U). All 338 files now share the
canonical form.

Generated via:
  addlicense -c "certctl LLC" -y 2026 \
    -f cowork/legal/copyright-header.tpl \
    -ignore '**/testdata/**' -ignore '**/*_test.go' \
    cmd/ internal/

Verification:
  find cmd internal -name '*.go' -not -name '*_test.go' \
    -not -path '*/testdata/*' \
    -exec grep -L '^// Copyright 2026 certctl LLC' {} \; | wc -l

  Returns: 0

gofmt clean. Header additions are comments only, no compile impact.

Closes: cowork/certctl-architecture-diligence-audit.html#fix-RED-4
2026-05-13 21:23:35 +00:00

142 lines
3.8 KiB
Go

// Copyright 2026 certctl LLC. All rights reserved.
// SPDX-License-Identifier: BUSL-1.1
package service
import (
"context"
"fmt"
"log/slog"
"github.com/certctl-io/certctl/internal/domain"
"github.com/certctl-io/certctl/internal/repository"
)
// VerificationService handles recording and querying certificate deployment verification results.
type VerificationService struct {
jobRepo repository.JobRepository
auditService *AuditService
logger *slog.Logger
}
// NewVerificationService creates a new verification service.
func NewVerificationService(
jobRepo repository.JobRepository,
auditService *AuditService,
logger *slog.Logger,
) *VerificationService {
return &VerificationService{
jobRepo: jobRepo,
auditService: auditService,
logger: logger,
}
}
// RecordVerificationResult updates a job with the results of TLS endpoint verification.
// This records both success and failure results, along with timestamp and fingerprint comparison.
// An audit event is recorded for every verification result.
func (s *VerificationService) RecordVerificationResult(ctx context.Context, result *domain.VerificationResult) error {
if result == nil {
return fmt.Errorf("verification result is required")
}
if result.JobID == "" {
return fmt.Errorf("job ID is required")
}
// Get the current job to update it
job, err := s.jobRepo.Get(ctx, result.JobID)
if err != nil {
return fmt.Errorf("failed to fetch job for verification: %w", err)
}
// Determine verification status
var status domain.VerificationStatus
if result.Error != "" {
status = domain.VerificationFailed
} else if result.Verified {
status = domain.VerificationSuccess
} else {
status = domain.VerificationFailed
}
// Update job with verification results
job.VerificationStatus = status
job.VerifiedAt = &result.VerifiedAt
job.VerificationFp = &result.ActualFingerprint
if result.Error != "" {
job.VerificationError = &result.Error
}
if err := s.jobRepo.Update(ctx, job); err != nil {
if s.logger != nil {
s.logger.Error("failed to record verification result",
"job_id", result.JobID,
"error", err)
}
return fmt.Errorf("failed to update job with verification result: %w", err)
}
// Record audit event
auditEvent := "job_verification_success"
auditDetails := map[string]interface{}{
"job_id": result.JobID,
"target_id": result.TargetID,
"expected_fingerprint": result.ExpectedFingerprint,
"actual_fingerprint": result.ActualFingerprint,
"verified": result.Verified,
}
if result.Error != "" {
auditEvent = "job_verification_failed"
auditDetails["error"] = result.Error
}
s.auditService.RecordEvent(ctx, "agent", domain.ActorTypeAgent,
auditEvent, "job", result.JobID,
auditDetails)
if s.logger != nil {
s.logger.Info("recorded verification result",
"job_id", result.JobID,
"status", status,
"verified", result.Verified)
}
return nil
}
// GetVerificationResult retrieves the verification status and details for a job.
func (s *VerificationService) GetVerificationResult(ctx context.Context, jobID string) (*domain.VerificationResult, error) {
if jobID == "" {
return nil, fmt.Errorf("job ID is required")
}
job, err := s.jobRepo.Get(ctx, jobID)
if err != nil {
return nil, fmt.Errorf("failed to fetch job: %w", err)
}
result := &domain.VerificationResult{
JobID: job.ID,
Verified: job.VerificationStatus == domain.VerificationSuccess,
}
// If target ID is set, populate it
if job.TargetID != nil {
result.TargetID = *job.TargetID
}
// Populate fingerprints if available
if job.VerificationFp != nil {
result.ActualFingerprint = *job.VerificationFp
}
if job.VerificationError != nil {
result.Error = *job.VerificationError
}
if job.VerifiedAt != nil {
result.VerifiedAt = *job.VerifiedAt
}
return result, nil
}