M-2 PR-C: Collapse Policy/Profile/Owner/Team services to ctx-first signatures

- Add ctx first param to 21 service-layer handler-interface methods
  across policy.go (6), profile.go (5), owner.go (5), team.go (5)
- Replace 24 context.Background() call sites with received ctx; use
  context.WithoutCancel(ctx) for subsidiary audit-recording ops to
  preserve fire-and-forget audit semantics without inheriting caller
  cancellation
- Add ctx first param to 21 handler-interface method signatures across
  policies.go (6), profiles.go (5), owners.go (5), teams.go (5)
- Thread r.Context() through 21 HTTP handler sites (ListPolicies,
  GetPolicy, CreatePolicy, UpdatePolicy, DeletePolicy, ListViolations,
  ListProfiles, GetProfile, CreateProfile, UpdateProfile, DeleteProfile,
  ListOwners, GetOwner, CreateOwner, UpdateOwner, DeleteOwner,
  ListTeams, GetTeam, CreateTeam, UpdateTeam, DeleteTeam)
- Update MockPolicyService/MockProfileService/MockOwnerService/
  MockTeamService mock method impls with _ context.Context first param
  (Fn fields unchanged — closures do not need ctx); update mock impls
  in integration/lifecycle_test.go for all four services
- Update 12 service-layer test callsites (policy_test.go ×2,
  owner_test.go ×5, team_test.go ×5, profile_test.go ×13) to pass
  context.Background() at the call site

Audit complete. Commit: 1f6cf0eafa. Sections: 12. Findings: 2/7/10/4/6.
This commit is contained in:
shankar0123
2026-04-18 01:10:06 +00:00
parent 25dd6c07f3
commit 2497be496d
17 changed files with 156 additions and 148 deletions
+6 -5
View File
@@ -2,6 +2,7 @@ package handler
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -20,35 +21,35 @@ type MockOwnerService struct {
DeleteOwnerFn func(id string) error
}
func (m *MockOwnerService) ListOwners(page, perPage int) ([]domain.Owner, int64, error) {
func (m *MockOwnerService) ListOwners(_ context.Context, page, perPage int) ([]domain.Owner, int64, error) {
if m.ListOwnersFn != nil {
return m.ListOwnersFn(page, perPage)
}
return nil, 0, nil
}
func (m *MockOwnerService) GetOwner(id string) (*domain.Owner, error) {
func (m *MockOwnerService) GetOwner(_ context.Context, id string) (*domain.Owner, error) {
if m.GetOwnerFn != nil {
return m.GetOwnerFn(id)
}
return nil, nil
}
func (m *MockOwnerService) CreateOwner(owner domain.Owner) (*domain.Owner, error) {
func (m *MockOwnerService) CreateOwner(_ context.Context, owner domain.Owner) (*domain.Owner, error) {
if m.CreateOwnerFn != nil {
return m.CreateOwnerFn(owner)
}
return nil, nil
}
func (m *MockOwnerService) UpdateOwner(id string, owner domain.Owner) (*domain.Owner, error) {
func (m *MockOwnerService) UpdateOwner(_ context.Context, id string, owner domain.Owner) (*domain.Owner, error) {
if m.UpdateOwnerFn != nil {
return m.UpdateOwnerFn(id, owner)
}
return nil, nil
}
func (m *MockOwnerService) DeleteOwner(id string) error {
func (m *MockOwnerService) DeleteOwner(_ context.Context, id string) error {
if m.DeleteOwnerFn != nil {
return m.DeleteOwnerFn(id)
}
+11 -10
View File
@@ -1,6 +1,7 @@
package handler
import (
"context"
"encoding/json"
"net/http"
"strconv"
@@ -12,11 +13,11 @@ import (
// OwnerService defines the service interface for owner operations.
type OwnerService interface {
ListOwners(page, perPage int) ([]domain.Owner, int64, error)
GetOwner(id string) (*domain.Owner, error)
CreateOwner(owner domain.Owner) (*domain.Owner, error)
UpdateOwner(id string, owner domain.Owner) (*domain.Owner, error)
DeleteOwner(id string) error
ListOwners(ctx context.Context, page, perPage int) ([]domain.Owner, int64, error)
GetOwner(ctx context.Context, id string) (*domain.Owner, error)
CreateOwner(ctx context.Context, owner domain.Owner) (*domain.Owner, error)
UpdateOwner(ctx context.Context, id string, owner domain.Owner) (*domain.Owner, error)
DeleteOwner(ctx context.Context, id string) error
}
// OwnerHandler handles HTTP requests for owner operations.
@@ -53,7 +54,7 @@ func (h OwnerHandler) ListOwners(w http.ResponseWriter, r *http.Request) {
}
}
owners, total, err := h.svc.ListOwners(page, perPage)
owners, total, err := h.svc.ListOwners(r.Context(), page, perPage)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list owners", requestID)
return
@@ -87,7 +88,7 @@ func (h OwnerHandler) GetOwner(w http.ResponseWriter, r *http.Request) {
}
id = parts[0]
owner, err := h.svc.GetOwner(id)
owner, err := h.svc.GetOwner(r.Context(), id)
if err != nil {
ErrorWithRequestID(w, http.StatusNotFound, "Owner not found", requestID)
return
@@ -122,7 +123,7 @@ func (h OwnerHandler) CreateOwner(w http.ResponseWriter, r *http.Request) {
return
}
created, err := h.svc.CreateOwner(owner)
created, err := h.svc.CreateOwner(r.Context(), owner)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to create owner", requestID)
return
@@ -155,7 +156,7 @@ func (h OwnerHandler) UpdateOwner(w http.ResponseWriter, r *http.Request) {
return
}
updated, err := h.svc.UpdateOwner(id, owner)
updated, err := h.svc.UpdateOwner(r.Context(), id, owner)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to update owner", requestID)
return
@@ -182,7 +183,7 @@ func (h OwnerHandler) DeleteOwner(w http.ResponseWriter, r *http.Request) {
}
id = parts[0]
if err := h.svc.DeleteOwner(id); err != nil {
if err := h.svc.DeleteOwner(r.Context(), id); err != nil {
if strings.Contains(err.Error(), "violates foreign key") || strings.Contains(err.Error(), "RESTRICT") {
ErrorWithRequestID(w, http.StatusConflict, "Cannot delete owner: certificates are still assigned to this owner", requestID)
} else if strings.Contains(err.Error(), "not found") {
+13 -12
View File
@@ -1,6 +1,7 @@
package handler
import (
"context"
"encoding/json"
"net/http"
"strconv"
@@ -12,12 +13,12 @@ import (
// PolicyService defines the service interface for policy rule operations.
type PolicyService interface {
ListPolicies(page, perPage int) ([]domain.PolicyRule, int64, error)
GetPolicy(id string) (*domain.PolicyRule, error)
CreatePolicy(policy domain.PolicyRule) (*domain.PolicyRule, error)
UpdatePolicy(id string, policy domain.PolicyRule) (*domain.PolicyRule, error)
DeletePolicy(id string) error
ListViolations(policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error)
ListPolicies(ctx context.Context, page, perPage int) ([]domain.PolicyRule, int64, error)
GetPolicy(ctx context.Context, id string) (*domain.PolicyRule, error)
CreatePolicy(ctx context.Context, policy domain.PolicyRule) (*domain.PolicyRule, error)
UpdatePolicy(ctx context.Context, id string, policy domain.PolicyRule) (*domain.PolicyRule, error)
DeletePolicy(ctx context.Context, id string) error
ListViolations(ctx context.Context, policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error)
}
// PolicyHandler handles HTTP requests for policy rule operations.
@@ -54,7 +55,7 @@ func (h PolicyHandler) ListPolicies(w http.ResponseWriter, r *http.Request) {
}
}
policies, total, err := h.svc.ListPolicies(page, perPage)
policies, total, err := h.svc.ListPolicies(r.Context(), page, perPage)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list policies", requestID)
return
@@ -88,7 +89,7 @@ func (h PolicyHandler) GetPolicy(w http.ResponseWriter, r *http.Request) {
}
id = parts[0]
policy, err := h.svc.GetPolicy(id)
policy, err := h.svc.GetPolicy(r.Context(), id)
if err != nil {
ErrorWithRequestID(w, http.StatusNotFound, "Policy not found", requestID)
return
@@ -127,7 +128,7 @@ func (h PolicyHandler) CreatePolicy(w http.ResponseWriter, r *http.Request) {
return
}
created, err := h.svc.CreatePolicy(policy)
created, err := h.svc.CreatePolicy(r.Context(), policy)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to create policy", requestID)
return
@@ -174,7 +175,7 @@ func (h PolicyHandler) UpdatePolicy(w http.ResponseWriter, r *http.Request) {
}
}
updated, err := h.svc.UpdatePolicy(id, policy)
updated, err := h.svc.UpdatePolicy(r.Context(), id, policy)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to update policy", requestID)
return
@@ -201,7 +202,7 @@ func (h PolicyHandler) DeletePolicy(w http.ResponseWriter, r *http.Request) {
}
id = parts[0]
if err := h.svc.DeletePolicy(id); err != nil {
if err := h.svc.DeletePolicy(r.Context(), id); err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to delete policy", requestID)
return
}
@@ -242,7 +243,7 @@ func (h PolicyHandler) ListViolations(w http.ResponseWriter, r *http.Request) {
}
}
violations, total, err := h.svc.ListViolations(policyID, page, perPage)
violations, total, err := h.svc.ListViolations(r.Context(), policyID, page, perPage)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list violations", requestID)
return
+7 -6
View File
@@ -2,6 +2,7 @@ package handler
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -21,42 +22,42 @@ type MockPolicyService struct {
ListViolationsFn func(policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error)
}
func (m *MockPolicyService) ListPolicies(page, perPage int) ([]domain.PolicyRule, int64, error) {
func (m *MockPolicyService) ListPolicies(_ context.Context, page, perPage int) ([]domain.PolicyRule, int64, error) {
if m.ListPoliciesFn != nil {
return m.ListPoliciesFn(page, perPage)
}
return nil, 0, nil
}
func (m *MockPolicyService) GetPolicy(id string) (*domain.PolicyRule, error) {
func (m *MockPolicyService) GetPolicy(_ context.Context, id string) (*domain.PolicyRule, error) {
if m.GetPolicyFn != nil {
return m.GetPolicyFn(id)
}
return nil, nil
}
func (m *MockPolicyService) CreatePolicy(policy domain.PolicyRule) (*domain.PolicyRule, error) {
func (m *MockPolicyService) CreatePolicy(_ context.Context, policy domain.PolicyRule) (*domain.PolicyRule, error) {
if m.CreatePolicyFn != nil {
return m.CreatePolicyFn(policy)
}
return nil, nil
}
func (m *MockPolicyService) UpdatePolicy(id string, policy domain.PolicyRule) (*domain.PolicyRule, error) {
func (m *MockPolicyService) UpdatePolicy(_ context.Context, id string, policy domain.PolicyRule) (*domain.PolicyRule, error) {
if m.UpdatePolicyFn != nil {
return m.UpdatePolicyFn(id, policy)
}
return nil, nil
}
func (m *MockPolicyService) DeletePolicy(id string) error {
func (m *MockPolicyService) DeletePolicy(_ context.Context, id string) error {
if m.DeletePolicyFn != nil {
return m.DeletePolicyFn(id)
}
return nil
}
func (m *MockPolicyService) ListViolations(policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error) {
func (m *MockPolicyService) ListViolations(_ context.Context, policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error) {
if m.ListViolationsFn != nil {
return m.ListViolationsFn(policyID, page, perPage)
}
+6 -5
View File
@@ -2,6 +2,7 @@ package handler
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -20,35 +21,35 @@ type MockProfileService struct {
DeleteProfileFn func(id string) error
}
func (m *MockProfileService) ListProfiles(page, perPage int) ([]domain.CertificateProfile, int64, error) {
func (m *MockProfileService) ListProfiles(_ context.Context, page, perPage int) ([]domain.CertificateProfile, int64, error) {
if m.ListProfilesFn != nil {
return m.ListProfilesFn(page, perPage)
}
return nil, 0, nil
}
func (m *MockProfileService) GetProfile(id string) (*domain.CertificateProfile, error) {
func (m *MockProfileService) GetProfile(_ context.Context, id string) (*domain.CertificateProfile, error) {
if m.GetProfileFn != nil {
return m.GetProfileFn(id)
}
return nil, nil
}
func (m *MockProfileService) CreateProfile(profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
func (m *MockProfileService) CreateProfile(_ context.Context, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
if m.CreateProfileFn != nil {
return m.CreateProfileFn(profile)
}
return nil, nil
}
func (m *MockProfileService) UpdateProfile(id string, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
func (m *MockProfileService) UpdateProfile(_ context.Context, id string, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
if m.UpdateProfileFn != nil {
return m.UpdateProfileFn(id, profile)
}
return nil, nil
}
func (m *MockProfileService) DeleteProfile(id string) error {
func (m *MockProfileService) DeleteProfile(_ context.Context, id string) error {
if m.DeleteProfileFn != nil {
return m.DeleteProfileFn(id)
}
+11 -10
View File
@@ -1,6 +1,7 @@
package handler
import (
"context"
"encoding/json"
"net/http"
"strconv"
@@ -12,11 +13,11 @@ import (
// ProfileService defines the service interface for certificate profile operations.
type ProfileService interface {
ListProfiles(page, perPage int) ([]domain.CertificateProfile, int64, error)
GetProfile(id string) (*domain.CertificateProfile, error)
CreateProfile(profile domain.CertificateProfile) (*domain.CertificateProfile, error)
UpdateProfile(id string, profile domain.CertificateProfile) (*domain.CertificateProfile, error)
DeleteProfile(id string) error
ListProfiles(ctx context.Context, page, perPage int) ([]domain.CertificateProfile, int64, error)
GetProfile(ctx context.Context, id string) (*domain.CertificateProfile, error)
CreateProfile(ctx context.Context, profile domain.CertificateProfile) (*domain.CertificateProfile, error)
UpdateProfile(ctx context.Context, id string, profile domain.CertificateProfile) (*domain.CertificateProfile, error)
DeleteProfile(ctx context.Context, id string) error
}
// ProfileHandler handles HTTP requests for certificate profile operations.
@@ -53,7 +54,7 @@ func (h ProfileHandler) ListProfiles(w http.ResponseWriter, r *http.Request) {
}
}
profiles, total, err := h.svc.ListProfiles(page, perPage)
profiles, total, err := h.svc.ListProfiles(r.Context(), page, perPage)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list profiles", requestID)
return
@@ -85,7 +86,7 @@ func (h ProfileHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
return
}
profile, err := h.svc.GetProfile(id)
profile, err := h.svc.GetProfile(r.Context(), id)
if err != nil {
ErrorWithRequestID(w, http.StatusNotFound, "Profile not found", requestID)
return
@@ -120,7 +121,7 @@ func (h ProfileHandler) CreateProfile(w http.ResponseWriter, r *http.Request) {
return
}
created, err := h.svc.CreateProfile(profile)
created, err := h.svc.CreateProfile(r.Context(), profile)
if err != nil {
// Check if it's a validation error from the service
if strings.Contains(err.Error(), "invalid") || strings.Contains(err.Error(), "required") ||
@@ -159,7 +160,7 @@ func (h ProfileHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
return
}
updated, err := h.svc.UpdateProfile(id, profile)
updated, err := h.svc.UpdateProfile(r.Context(), id, profile)
if err != nil {
if strings.Contains(err.Error(), "not found") {
ErrorWithRequestID(w, http.StatusNotFound, "Profile not found", requestID)
@@ -193,7 +194,7 @@ func (h ProfileHandler) DeleteProfile(w http.ResponseWriter, r *http.Request) {
return
}
if err := h.svc.DeleteProfile(id); err != nil {
if err := h.svc.DeleteProfile(r.Context(), id); err != nil {
if strings.Contains(err.Error(), "not found") {
ErrorWithRequestID(w, http.StatusNotFound, "Profile not found", requestID)
return
+6 -5
View File
@@ -2,6 +2,7 @@ package handler
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
@@ -20,35 +21,35 @@ type MockTeamService struct {
DeleteTeamFn func(id string) error
}
func (m *MockTeamService) ListTeams(page, perPage int) ([]domain.Team, int64, error) {
func (m *MockTeamService) ListTeams(_ context.Context, page, perPage int) ([]domain.Team, int64, error) {
if m.ListTeamsFn != nil {
return m.ListTeamsFn(page, perPage)
}
return nil, 0, nil
}
func (m *MockTeamService) GetTeam(id string) (*domain.Team, error) {
func (m *MockTeamService) GetTeam(_ context.Context, id string) (*domain.Team, error) {
if m.GetTeamFn != nil {
return m.GetTeamFn(id)
}
return nil, nil
}
func (m *MockTeamService) CreateTeam(team domain.Team) (*domain.Team, error) {
func (m *MockTeamService) CreateTeam(_ context.Context, team domain.Team) (*domain.Team, error) {
if m.CreateTeamFn != nil {
return m.CreateTeamFn(team)
}
return nil, nil
}
func (m *MockTeamService) UpdateTeam(id string, team domain.Team) (*domain.Team, error) {
func (m *MockTeamService) UpdateTeam(_ context.Context, id string, team domain.Team) (*domain.Team, error) {
if m.UpdateTeamFn != nil {
return m.UpdateTeamFn(id, team)
}
return nil, nil
}
func (m *MockTeamService) DeleteTeam(id string) error {
func (m *MockTeamService) DeleteTeam(_ context.Context, id string) error {
if m.DeleteTeamFn != nil {
return m.DeleteTeamFn(id)
}
+11 -10
View File
@@ -1,6 +1,7 @@
package handler
import (
"context"
"encoding/json"
"net/http"
"strconv"
@@ -12,11 +13,11 @@ import (
// TeamService defines the service interface for team operations.
type TeamService interface {
ListTeams(page, perPage int) ([]domain.Team, int64, error)
GetTeam(id string) (*domain.Team, error)
CreateTeam(team domain.Team) (*domain.Team, error)
UpdateTeam(id string, team domain.Team) (*domain.Team, error)
DeleteTeam(id string) error
ListTeams(ctx context.Context, page, perPage int) ([]domain.Team, int64, error)
GetTeam(ctx context.Context, id string) (*domain.Team, error)
CreateTeam(ctx context.Context, team domain.Team) (*domain.Team, error)
UpdateTeam(ctx context.Context, id string, team domain.Team) (*domain.Team, error)
DeleteTeam(ctx context.Context, id string) error
}
// TeamHandler handles HTTP requests for team operations.
@@ -53,7 +54,7 @@ func (h TeamHandler) ListTeams(w http.ResponseWriter, r *http.Request) {
}
}
teams, total, err := h.svc.ListTeams(page, perPage)
teams, total, err := h.svc.ListTeams(r.Context(), page, perPage)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to list teams", requestID)
return
@@ -87,7 +88,7 @@ func (h TeamHandler) GetTeam(w http.ResponseWriter, r *http.Request) {
}
id = parts[0]
team, err := h.svc.GetTeam(id)
team, err := h.svc.GetTeam(r.Context(), id)
if err != nil {
ErrorWithRequestID(w, http.StatusNotFound, "Team not found", requestID)
return
@@ -122,7 +123,7 @@ func (h TeamHandler) CreateTeam(w http.ResponseWriter, r *http.Request) {
return
}
created, err := h.svc.CreateTeam(team)
created, err := h.svc.CreateTeam(r.Context(), team)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to create team", requestID)
return
@@ -155,7 +156,7 @@ func (h TeamHandler) UpdateTeam(w http.ResponseWriter, r *http.Request) {
return
}
updated, err := h.svc.UpdateTeam(id, team)
updated, err := h.svc.UpdateTeam(r.Context(), id, team)
if err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to update team", requestID)
return
@@ -182,7 +183,7 @@ func (h TeamHandler) DeleteTeam(w http.ResponseWriter, r *http.Request) {
}
id = parts[0]
if err := h.svc.DeleteTeam(id); err != nil {
if err := h.svc.DeleteTeam(r.Context(), id); err != nil {
ErrorWithRequestID(w, http.StatusInternalServerError, "Failed to delete team", requestID)
return
}
+15 -15
View File
@@ -1077,70 +1077,70 @@ func (m *mockTargetService) TestConnection(ctx context.Context, id string) error
type mockTeamService struct{}
func (m *mockTeamService) ListTeams(page, perPage int) ([]domain.Team, int64, error) {
func (m *mockTeamService) ListTeams(_ context.Context, page, perPage int) ([]domain.Team, int64, error) {
return []domain.Team{}, 0, nil
}
func (m *mockTeamService) GetTeam(id string) (*domain.Team, error) {
func (m *mockTeamService) GetTeam(_ context.Context, id string) (*domain.Team, error) {
return nil, fmt.Errorf("team not found")
}
func (m *mockTeamService) CreateTeam(team domain.Team) (*domain.Team, error) {
func (m *mockTeamService) CreateTeam(_ context.Context, team domain.Team) (*domain.Team, error) {
return &team, nil
}
func (m *mockTeamService) UpdateTeam(id string, team domain.Team) (*domain.Team, error) {
func (m *mockTeamService) UpdateTeam(_ context.Context, id string, team domain.Team) (*domain.Team, error) {
team.ID = id
return &team, nil
}
func (m *mockTeamService) DeleteTeam(id string) error {
func (m *mockTeamService) DeleteTeam(_ context.Context, id string) error {
return nil
}
type mockOwnerService struct{}
func (m *mockOwnerService) ListOwners(page, perPage int) ([]domain.Owner, int64, error) {
func (m *mockOwnerService) ListOwners(_ context.Context, page, perPage int) ([]domain.Owner, int64, error) {
return []domain.Owner{}, 0, nil
}
func (m *mockOwnerService) GetOwner(id string) (*domain.Owner, error) {
func (m *mockOwnerService) GetOwner(_ context.Context, id string) (*domain.Owner, error) {
return nil, fmt.Errorf("owner not found")
}
func (m *mockOwnerService) CreateOwner(owner domain.Owner) (*domain.Owner, error) {
func (m *mockOwnerService) CreateOwner(_ context.Context, owner domain.Owner) (*domain.Owner, error) {
return &owner, nil
}
func (m *mockOwnerService) UpdateOwner(id string, owner domain.Owner) (*domain.Owner, error) {
func (m *mockOwnerService) UpdateOwner(_ context.Context, id string, owner domain.Owner) (*domain.Owner, error) {
owner.ID = id
return &owner, nil
}
func (m *mockOwnerService) DeleteOwner(id string) error {
func (m *mockOwnerService) DeleteOwner(_ context.Context, id string) error {
return nil
}
type mockProfileService struct{}
func (m *mockProfileService) ListProfiles(page, perPage int) ([]domain.CertificateProfile, int64, error) {
func (m *mockProfileService) ListProfiles(_ context.Context, page, perPage int) ([]domain.CertificateProfile, int64, error) {
return []domain.CertificateProfile{}, 0, nil
}
func (m *mockProfileService) GetProfile(id string) (*domain.CertificateProfile, error) {
func (m *mockProfileService) GetProfile(_ context.Context, id string) (*domain.CertificateProfile, error) {
return nil, fmt.Errorf("profile not found")
}
func (m *mockProfileService) CreateProfile(profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
func (m *mockProfileService) CreateProfile(_ context.Context, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
return &profile, nil
}
func (m *mockProfileService) UpdateProfile(id string, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
func (m *mockProfileService) UpdateProfile(_ context.Context, id string, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
profile.ID = id
return &profile, nil
}
func (m *mockProfileService) DeleteProfile(id string) error {
func (m *mockProfileService) DeleteProfile(_ context.Context, id string) error {
return nil
}
+10 -10
View File
@@ -126,7 +126,7 @@ func (s *OwnerService) Delete(ctx context.Context, id string, actor string) erro
}
// ListOwners returns paginated owners (handler interface method).
func (s *OwnerService) ListOwners(page, perPage int) ([]domain.Owner, int64, error) {
func (s *OwnerService) ListOwners(ctx context.Context, page, perPage int) ([]domain.Owner, int64, error) {
if page < 1 {
page = 1
}
@@ -134,7 +134,7 @@ func (s *OwnerService) ListOwners(page, perPage int) ([]domain.Owner, int64, err
perPage = 50
}
owners, err := s.ownerRepo.List(context.Background())
owners, err := s.ownerRepo.List(ctx)
if err != nil {
return nil, 0, fmt.Errorf("failed to list owners: %w", err)
}
@@ -151,12 +151,12 @@ func (s *OwnerService) ListOwners(page, perPage int) ([]domain.Owner, int64, err
}
// GetOwner returns a single owner (handler interface method).
func (s *OwnerService) GetOwner(id string) (*domain.Owner, error) {
return s.ownerRepo.Get(context.Background(), id)
func (s *OwnerService) GetOwner(ctx context.Context, id string) (*domain.Owner, error) {
return s.ownerRepo.Get(ctx, id)
}
// CreateOwner creates a new owner (handler interface method).
func (s *OwnerService) CreateOwner(owner domain.Owner) (*domain.Owner, error) {
func (s *OwnerService) CreateOwner(ctx context.Context, owner domain.Owner) (*domain.Owner, error) {
if owner.ID == "" {
owner.ID = generateID("owner")
}
@@ -167,22 +167,22 @@ func (s *OwnerService) CreateOwner(owner domain.Owner) (*domain.Owner, error) {
if owner.UpdatedAt.IsZero() {
owner.UpdatedAt = now
}
if err := s.ownerRepo.Create(context.Background(), &owner); err != nil {
if err := s.ownerRepo.Create(ctx, &owner); err != nil {
return nil, fmt.Errorf("failed to create owner: %w", err)
}
return &owner, nil
}
// UpdateOwner modifies an owner (handler interface method).
func (s *OwnerService) UpdateOwner(id string, owner domain.Owner) (*domain.Owner, error) {
func (s *OwnerService) UpdateOwner(ctx context.Context, id string, owner domain.Owner) (*domain.Owner, error) {
owner.ID = id
if err := s.ownerRepo.Update(context.Background(), &owner); err != nil {
if err := s.ownerRepo.Update(ctx, &owner); err != nil {
return nil, fmt.Errorf("failed to update owner: %w", err)
}
return &owner, nil
}
// DeleteOwner removes an owner (handler interface method).
func (s *OwnerService) DeleteOwner(id string) error {
return s.ownerRepo.Delete(context.Background(), id)
func (s *OwnerService) DeleteOwner(ctx context.Context, id string) error {
return s.ownerRepo.Delete(ctx, id)
}
+5 -5
View File
@@ -638,7 +638,7 @@ func TestOwnerService_ListOwners_HandlerInterface(t *testing.T) {
ownerService := NewOwnerService(ownerRepo, auditService)
owners, total, err := ownerService.ListOwners(1, 50)
owners, total, err := ownerService.ListOwners(context.Background(), 1, 50)
if err != nil {
t.Fatalf("ListOwners failed: %v", err)
}
@@ -678,7 +678,7 @@ func TestOwnerService_GetOwner_HandlerInterface(t *testing.T) {
ownerService := NewOwnerService(ownerRepo, auditService)
retrieved, err := ownerService.GetOwner("owner-001")
retrieved, err := ownerService.GetOwner(context.Background(), "owner-001")
if err != nil {
t.Fatalf("GetOwner failed: %v", err)
}
@@ -702,7 +702,7 @@ func TestOwnerService_CreateOwner_HandlerInterface(t *testing.T) {
TeamID: "team-001",
}
created, err := ownerService.CreateOwner(owner)
created, err := ownerService.CreateOwner(context.Background(), owner)
if err != nil {
t.Fatalf("CreateOwner failed: %v", err)
}
@@ -752,7 +752,7 @@ func TestOwnerService_UpdateOwner_HandlerInterface(t *testing.T) {
TeamID: "team-002",
}
updated, err := ownerService.UpdateOwner("owner-001", updatedOwner)
updated, err := ownerService.UpdateOwner(context.Background(), "owner-001", updatedOwner)
if err != nil {
t.Fatalf("UpdateOwner failed: %v", err)
}
@@ -798,7 +798,7 @@ func TestOwnerService_DeleteOwner_HandlerInterface(t *testing.T) {
ownerService := NewOwnerService(ownerRepo, auditService)
err := ownerService.DeleteOwner("owner-001")
err := ownerService.DeleteOwner(context.Background(), "owner-001")
if err != nil {
t.Fatalf("DeleteOwner failed: %v", err)
}
+12 -12
View File
@@ -230,7 +230,7 @@ func (s *PolicyService) ListViolationsWithContext(ctx context.Context, filter *r
}
// ListPolicies returns paginated policies (handler interface method).
func (s *PolicyService) ListPolicies(page, perPage int) ([]domain.PolicyRule, int64, error) {
func (s *PolicyService) ListPolicies(ctx context.Context, page, perPage int) ([]domain.PolicyRule, int64, error) {
if page < 1 {
page = 1
}
@@ -238,7 +238,7 @@ func (s *PolicyService) ListPolicies(page, perPage int) ([]domain.PolicyRule, in
perPage = 50
}
rules, err := s.policyRepo.ListRules(context.Background())
rules, err := s.policyRepo.ListRules(ctx)
if err != nil {
return nil, 0, fmt.Errorf("failed to list policies: %w", err)
}
@@ -264,12 +264,12 @@ func (s *PolicyService) ListPolicies(page, perPage int) ([]domain.PolicyRule, in
}
// GetPolicy returns a single policy (handler interface method).
func (s *PolicyService) GetPolicy(id string) (*domain.PolicyRule, error) {
return s.policyRepo.GetRule(context.Background(), id)
func (s *PolicyService) GetPolicy(ctx context.Context, id string) (*domain.PolicyRule, error) {
return s.policyRepo.GetRule(ctx, id)
}
// CreatePolicy creates a new policy (handler interface method).
func (s *PolicyService) CreatePolicy(policy domain.PolicyRule) (*domain.PolicyRule, error) {
func (s *PolicyService) CreatePolicy(ctx context.Context, policy domain.PolicyRule) (*domain.PolicyRule, error) {
if policy.ID == "" {
policy.ID = generateID("rule")
}
@@ -277,30 +277,30 @@ func (s *PolicyService) CreatePolicy(policy domain.PolicyRule) (*domain.PolicyRu
policy.CreatedAt = time.Now()
}
if err := s.policyRepo.CreateRule(context.Background(), &policy); err != nil {
if err := s.policyRepo.CreateRule(ctx, &policy); err != nil {
return nil, fmt.Errorf("failed to create policy: %w", err)
}
return &policy, nil
}
// UpdatePolicy modifies a policy (handler interface method).
func (s *PolicyService) UpdatePolicy(id string, policy domain.PolicyRule) (*domain.PolicyRule, error) {
func (s *PolicyService) UpdatePolicy(ctx context.Context, id string, policy domain.PolicyRule) (*domain.PolicyRule, error) {
policy.ID = id
policy.UpdatedAt = time.Now()
if err := s.policyRepo.UpdateRule(context.Background(), &policy); err != nil {
if err := s.policyRepo.UpdateRule(ctx, &policy); err != nil {
return nil, fmt.Errorf("failed to update policy: %w", err)
}
return &policy, nil
}
// DeletePolicy removes a policy (handler interface method).
func (s *PolicyService) DeletePolicy(id string) error {
return s.policyRepo.DeleteRule(context.Background(), id)
func (s *PolicyService) DeletePolicy(ctx context.Context, id string) error {
return s.policyRepo.DeleteRule(ctx, id)
}
// ListViolations returns policy violations with pagination (handler interface method).
func (s *PolicyService) ListViolations(policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error) {
func (s *PolicyService) ListViolations(ctx context.Context, policyID string, page, perPage int) ([]domain.PolicyViolation, int64, error) {
if page < 1 {
page = 1
}
@@ -313,7 +313,7 @@ func (s *PolicyService) ListViolations(policyID string, page, perPage int) ([]do
PerPage: 1000, // Get all violations for the policy
}
violations, err := s.policyRepo.ListViolations(context.Background(), filter)
violations, err := s.policyRepo.ListViolations(ctx, filter)
if err != nil {
return nil, 0, fmt.Errorf("failed to list violations: %w", err)
}
+2 -2
View File
@@ -376,7 +376,7 @@ func TestListPolicies(t *testing.T) {
policyService := NewPolicyService(policyRepo, auditService)
policies, total, err := policyService.ListPolicies(1, 50)
policies, total, err := policyService.ListPolicies(context.Background(), 1, 50)
if err != nil {
t.Fatalf("ListPolicies failed: %v", err)
}
@@ -407,7 +407,7 @@ func TestCreatePolicy(t *testing.T) {
CreatedAt: now,
}
created, err := policyService.CreatePolicy(policy)
created, err := policyService.CreatePolicy(context.Background(), policy)
if err != nil {
t.Fatalf("CreatePolicy failed: %v", err)
}
+13 -13
View File
@@ -28,7 +28,7 @@ func NewProfileService(
}
// ListProfiles returns all profiles (handler interface method).
func (s *ProfileService) ListProfiles(page, perPage int) ([]domain.CertificateProfile, int64, error) {
func (s *ProfileService) ListProfiles(ctx context.Context, page, perPage int) ([]domain.CertificateProfile, int64, error) {
if page < 1 {
page = 1
}
@@ -36,7 +36,7 @@ func (s *ProfileService) ListProfiles(page, perPage int) ([]domain.CertificatePr
perPage = 50
}
profiles, err := s.profileRepo.List(context.Background())
profiles, err := s.profileRepo.List(ctx)
if err != nil {
return nil, 0, fmt.Errorf("failed to list profiles: %w", err)
}
@@ -53,12 +53,12 @@ func (s *ProfileService) ListProfiles(page, perPage int) ([]domain.CertificatePr
}
// GetProfile returns a single profile (handler interface method).
func (s *ProfileService) GetProfile(id string) (*domain.CertificateProfile, error) {
return s.profileRepo.Get(context.Background(), id)
func (s *ProfileService) GetProfile(ctx context.Context, id string) (*domain.CertificateProfile, error) {
return s.profileRepo.Get(ctx, id)
}
// CreateProfile creates a new profile with validation (handler interface method).
func (s *ProfileService) CreateProfile(profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
func (s *ProfileService) CreateProfile(ctx context.Context, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
if err := validateProfile(&profile); err != nil {
return nil, err
}
@@ -82,12 +82,12 @@ func (s *ProfileService) CreateProfile(profile domain.CertificateProfile) (*doma
profile.AllowedEKUs = domain.DefaultEKUs()
}
if err := s.profileRepo.Create(context.Background(), &profile); err != nil {
if err := s.profileRepo.Create(ctx, &profile); err != nil {
return nil, fmt.Errorf("failed to create profile: %w", err)
}
if s.auditService != nil {
if auditErr := s.auditService.RecordEvent(context.Background(), "api", domain.ActorTypeUser,
if auditErr := s.auditService.RecordEvent(context.WithoutCancel(ctx), "api", domain.ActorTypeUser,
"create_profile", "certificate_profile", profile.ID, nil); auditErr != nil {
slog.Error("failed to record audit event", "error", auditErr)
}
@@ -97,18 +97,18 @@ func (s *ProfileService) CreateProfile(profile domain.CertificateProfile) (*doma
}
// UpdateProfile modifies an existing profile (handler interface method).
func (s *ProfileService) UpdateProfile(id string, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
func (s *ProfileService) UpdateProfile(ctx context.Context, id string, profile domain.CertificateProfile) (*domain.CertificateProfile, error) {
if err := validateProfile(&profile); err != nil {
return nil, err
}
profile.ID = id
if err := s.profileRepo.Update(context.Background(), &profile); err != nil {
if err := s.profileRepo.Update(ctx, &profile); err != nil {
return nil, fmt.Errorf("failed to update profile: %w", err)
}
if s.auditService != nil {
if auditErr := s.auditService.RecordEvent(context.Background(), "api", domain.ActorTypeUser,
if auditErr := s.auditService.RecordEvent(context.WithoutCancel(ctx), "api", domain.ActorTypeUser,
"update_profile", "certificate_profile", id, nil); auditErr != nil {
slog.Error("failed to record audit event", "error", auditErr)
}
@@ -118,13 +118,13 @@ func (s *ProfileService) UpdateProfile(id string, profile domain.CertificateProf
}
// DeleteProfile removes a profile (handler interface method).
func (s *ProfileService) DeleteProfile(id string) error {
if err := s.profileRepo.Delete(context.Background(), id); err != nil {
func (s *ProfileService) DeleteProfile(ctx context.Context, id string) error {
if err := s.profileRepo.Delete(ctx, id); err != nil {
return fmt.Errorf("failed to delete profile: %w", err)
}
if s.auditService != nil {
if auditErr := s.auditService.RecordEvent(context.Background(), "api", domain.ActorTypeUser,
if auditErr := s.auditService.RecordEvent(context.WithoutCancel(ctx), "api", domain.ActorTypeUser,
"delete_profile", "certificate_profile", id, nil); auditErr != nil {
slog.Error("failed to record audit event", "error", auditErr)
}
+13 -13
View File
@@ -82,7 +82,7 @@ func TestProfileService_ListProfiles(t *testing.T) {
repo.AddProfile(&domain.CertificateProfile{ID: "prof-2", Name: "Internal mTLS", Enabled: true})
svc := NewProfileService(repo, nil)
profiles, total, err := svc.ListProfiles(1, 50)
profiles, total, err := svc.ListProfiles(context.Background(), 1, 50)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -98,7 +98,7 @@ func TestProfileService_ListProfiles_Empty(t *testing.T) {
repo := newMockProfileRepository()
svc := NewProfileService(repo, nil)
profiles, total, err := svc.ListProfiles(1, 50)
profiles, total, err := svc.ListProfiles(context.Background(), 1, 50)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -115,7 +115,7 @@ func TestProfileService_ListProfiles_RepoError(t *testing.T) {
repo.ListErr = errors.New("db error")
svc := NewProfileService(repo, nil)
_, _, err := svc.ListProfiles(1, 50)
_, _, err := svc.ListProfiles(context.Background(), 1, 50)
if err == nil {
t.Fatal("expected error, got nil")
}
@@ -126,7 +126,7 @@ func TestProfileService_GetProfile(t *testing.T) {
repo.AddProfile(&domain.CertificateProfile{ID: "prof-1", Name: "Standard TLS"})
svc := NewProfileService(repo, nil)
profile, err := svc.GetProfile("prof-1")
profile, err := svc.GetProfile(context.Background(), "prof-1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -139,7 +139,7 @@ func TestProfileService_GetProfile_NotFound(t *testing.T) {
repo := newMockProfileRepository()
svc := NewProfileService(repo, nil)
_, err := svc.GetProfile("nonexistent")
_, err := svc.GetProfile(context.Background(), "nonexistent")
if err == nil {
t.Fatal("expected error, got nil")
}
@@ -156,7 +156,7 @@ func TestProfileService_CreateProfile_Defaults(t *testing.T) {
MaxTTLSeconds: 86400,
}
created, err := svc.CreateProfile(profile)
created, err := svc.CreateProfile(context.Background(), profile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -258,7 +258,7 @@ func TestProfileService_CreateProfile_ValidationErrors(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := svc.CreateProfile(tt.profile)
_, err := svc.CreateProfile(context.Background(), tt.profile)
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.errMsg)
}
@@ -274,7 +274,7 @@ func TestProfileService_CreateProfile_RepoError(t *testing.T) {
repo.CreateErr = errors.New("db create failed")
svc := NewProfileService(repo, nil)
_, err := svc.CreateProfile(domain.CertificateProfile{Name: "Valid"})
_, err := svc.CreateProfile(context.Background(), domain.CertificateProfile{Name: "Valid"})
if err == nil {
t.Fatal("expected error, got nil")
}
@@ -287,7 +287,7 @@ func TestProfileService_UpdateProfile(t *testing.T) {
auditSvc := NewAuditService(auditRepo)
svc := NewProfileService(repo, auditSvc)
updated, err := svc.UpdateProfile("prof-1", domain.CertificateProfile{
updated, err := svc.UpdateProfile(context.Background(), "prof-1", domain.CertificateProfile{
Name: "Updated",
MaxTTLSeconds: 43200,
})
@@ -306,7 +306,7 @@ func TestProfileService_UpdateProfile_ValidationError(t *testing.T) {
repo := newMockProfileRepository()
svc := NewProfileService(repo, nil)
_, err := svc.UpdateProfile("prof-1", domain.CertificateProfile{Name: ""})
_, err := svc.UpdateProfile(context.Background(), "prof-1", domain.CertificateProfile{Name: ""})
if err == nil {
t.Fatal("expected validation error, got nil")
}
@@ -319,7 +319,7 @@ func TestProfileService_DeleteProfile(t *testing.T) {
auditSvc := NewAuditService(auditRepo)
svc := NewProfileService(repo, auditSvc)
err := svc.DeleteProfile("prof-1")
err := svc.DeleteProfile(context.Background(), "prof-1")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -333,7 +333,7 @@ func TestProfileService_DeleteProfile_RepoError(t *testing.T) {
repo.DeleteErr = errors.New("db delete failed")
svc := NewProfileService(repo, nil)
err := svc.DeleteProfile("prof-1")
err := svc.DeleteProfile(context.Background(), "prof-1")
if err == nil {
t.Fatal("expected error, got nil")
}
@@ -344,7 +344,7 @@ func TestProfileService_CreateProfile_ValidShortLived(t *testing.T) {
svc := NewProfileService(repo, nil)
// Short-lived with TTL under 1 hour should succeed
created, err := svc.CreateProfile(domain.CertificateProfile{
created, err := svc.CreateProfile(context.Background(), domain.CertificateProfile{
Name: "CI Ephemeral",
AllowShortLived: true,
MaxTTLSeconds: 300, // 5 minutes
+10 -10
View File
@@ -126,7 +126,7 @@ func (s *TeamService) Delete(ctx context.Context, id string, actor string) error
}
// ListTeams returns paginated teams (handler interface method).
func (s *TeamService) ListTeams(page, perPage int) ([]domain.Team, int64, error) {
func (s *TeamService) ListTeams(ctx context.Context, page, perPage int) ([]domain.Team, int64, error) {
if page < 1 {
page = 1
}
@@ -134,7 +134,7 @@ func (s *TeamService) ListTeams(page, perPage int) ([]domain.Team, int64, error)
perPage = 50
}
teams, err := s.teamRepo.List(context.Background())
teams, err := s.teamRepo.List(ctx)
if err != nil {
return nil, 0, fmt.Errorf("failed to list teams: %w", err)
}
@@ -151,12 +151,12 @@ func (s *TeamService) ListTeams(page, perPage int) ([]domain.Team, int64, error)
}
// GetTeam returns a single team (handler interface method).
func (s *TeamService) GetTeam(id string) (*domain.Team, error) {
return s.teamRepo.Get(context.Background(), id)
func (s *TeamService) GetTeam(ctx context.Context, id string) (*domain.Team, error) {
return s.teamRepo.Get(ctx, id)
}
// CreateTeam creates a new team (handler interface method).
func (s *TeamService) CreateTeam(team domain.Team) (*domain.Team, error) {
func (s *TeamService) CreateTeam(ctx context.Context, team domain.Team) (*domain.Team, error) {
if team.ID == "" {
team.ID = generateID("team")
}
@@ -167,22 +167,22 @@ func (s *TeamService) CreateTeam(team domain.Team) (*domain.Team, error) {
if team.UpdatedAt.IsZero() {
team.UpdatedAt = now
}
if err := s.teamRepo.Create(context.Background(), &team); err != nil {
if err := s.teamRepo.Create(ctx, &team); err != nil {
return nil, fmt.Errorf("failed to create team: %w", err)
}
return &team, nil
}
// UpdateTeam modifies a team (handler interface method).
func (s *TeamService) UpdateTeam(id string, team domain.Team) (*domain.Team, error) {
func (s *TeamService) UpdateTeam(ctx context.Context, id string, team domain.Team) (*domain.Team, error) {
team.ID = id
if err := s.teamRepo.Update(context.Background(), &team); err != nil {
if err := s.teamRepo.Update(ctx, &team); err != nil {
return nil, fmt.Errorf("failed to update team: %w", err)
}
return &team, nil
}
// DeleteTeam removes a team (handler interface method).
func (s *TeamService) DeleteTeam(id string) error {
return s.teamRepo.Delete(context.Background(), id)
func (s *TeamService) DeleteTeam(ctx context.Context, id string) error {
return s.teamRepo.Delete(ctx, id)
}
+5 -5
View File
@@ -544,7 +544,7 @@ func TestTeamService_ListTeams_HandlerInterface(t *testing.T) {
})
}
teams, total, err := teamService.ListTeams(1, 2)
teams, total, err := teamService.ListTeams(context.Background(), 1, 2)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -571,7 +571,7 @@ func TestTeamService_GetTeam_HandlerInterface(t *testing.T) {
}
mockTeamRepo.AddTeam(testTeam)
team, err := teamService.GetTeam("handler-team")
team, err := teamService.GetTeam(context.Background(), "handler-team")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -593,7 +593,7 @@ func TestTeamService_CreateTeam_HandlerInterface(t *testing.T) {
Description: "Created via handler",
}
result, err := teamService.CreateTeam(team)
result, err := teamService.CreateTeam(context.Background(), team)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -629,7 +629,7 @@ func TestTeamService_UpdateTeam_HandlerInterface(t *testing.T) {
Description: "Handler update",
}
result, err := teamService.UpdateTeam("handler-update-team", updateTeam)
result, err := teamService.UpdateTeam(context.Background(), "handler-update-team", updateTeam)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
@@ -656,7 +656,7 @@ func TestTeamService_DeleteTeam_HandlerInterface(t *testing.T) {
Name: "To Delete",
})
err := teamService.DeleteTeam("handler-delete-team")
err := teamService.DeleteTeam(context.Background(), "handler-delete-team")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}