Files
certctl/internal/connector/discovery/gcpsm/gcpsm.go
T
shankar0123 02438ad9e1 ci: floor raise + doc drift (Phase 3 closure — TEST-H1/H2/M1/M2/M3/M4/L1, ARCH-H3/L1/L2/L3/L4)
Twelve findings from the architecture diligence audit's Phase 3 bundle
closed in one PR. All touch the CI workflows + small doc-drift fixes
across the production Go tree + migration headers.

CI workflow changes
====================

TEST-H1 — Race detection on ./... -short
  .github/workflows/ci.yml:106 was a 9-package explicit list. Audit
  finding TEST-H1 flagged that 25+ packages (internal/auth/*,
  internal/repository/*, internal/mcp, internal/scep, internal/pkcs7,
  internal/api/router, internal/api/acme, internal/cli, internal/cms,
  internal/config, internal/deploy, internal/integration,
  internal/ratelimit, internal/secret, internal/trustanchor, all of
  cmd/) silently dropped off race coverage.
  Post-fix: 'go test -race -short ./... -count=1 -timeout 600s'.
  76 testing.Short() guards already cover testcontainers + live-DB
  integration suites, so -short keeps the long-running tests out.

TEST-H2 — Cross-platform build matrix
  New 'cross-platform-build' job in ci.yml. Matrix:
  ubuntu-latest + windows-latest + macos-latest, fail-fast: false.
  Builds cmd/server + cmd/agent + cmd/cli + cmd/mcp-server on each.
  Catches Windows-specific regressions (path separators, file
  permissions, exec.Command semantics) the pre-Phase-3 Ubuntu-only
  CI missed.

TEST-L1 — actions/setup-go cache: true (explicit)
  setup-go v5 defaults cache: true; making it explicit so a future
  setup-go upgrade can't silently flip it. Re-runs hit the Go module
  + build cache instead of recompiling cold.

TEST-M1 — Mutation-testing floor at 55%
  security-deep-scan.yml::go-mutesting step rewritten. Removed
  continue-on-error + per-package '|| true'. New post-loop check
  extracts every 'The mutation score is X.YZ' line and fails the
  step if any package drops below 0.55. Floor rationale: starter
  ratio catches major regressions without rejecting the audit's
  'this is OK' steady state; raise quarterly.

TEST-M2 — 3 advisory deep-scan gates promoted to blocking
  Removed continue-on-error: true from:
    - gosec (filtered to G201/G202/G304/G108 high-signal rules:
      SQL-injection + path-traversal + pprof-exposed)
    - osv-scanner (multi-ecosystem CVE; complements govulncheck
      which is already blocking in ci.yml)
    - trivy image scan (--severity HIGH,CRITICAL --exit-code 1)
  continue-on-error count: 15 → 11.
  ZAP / schemathesis / nuclei / testssl stay advisory because their
  false-positive rates on https://localhost:8443-targeted DAST runs
  are high.

TEST-M3 — Playwright harness stub
  web/package.json adds '@playwright/test' devDep + 'e2e' / 'e2e:install'
  npm scripts. web/playwright.config.ts ships single chromium project
  with webServer block pointing at 'npm run dev'. web/src/__tests__/
  e2e/smoke.spec.ts proves the harness wires through. The full 15-flow
  suite ships in frontend-design-audit Phase 8 (TEST-H1 in THAT audit);
  this is the wiring + a single smoke test as the regression floor.
  New Makefile target: 'make e2e-test'.

Doc/code drift fixes
====================

TEST-M4 + ARCH-L2 — Skip inventory artifact + CI guard
  scripts/skip-inventory.sh walks every t.Skip site under cmd/ +
  internal/ + deploy/test/ and emits docs/testing/skip-inventory.md
  grouped by package with file:line:expression triples. Current
  inventory: 142 t.Skip sites, 76 testing.Short() guards.
  scripts/ci-guards/skip-inventory-drift.sh regenerates and fails on
  diff (excluding the 'Last reviewed' timestamp line which drifts
  daily). The Markdown is the canonical acquisition-diligence artifact
  for 'what tests are being skipped and why.'

ARCH-H3 — MCP catalogue floor reconciliation
  Audit framing was '121 vs floor 150 — doc/code drift.' Live count
  via the test's actual regex over all 5 tool files (tools.go +
  tools_audit_fix.go + tools_auth.go + tools_auth_bundle2.go +
  tools_est.go): 155 unique 'Name: "certctl_*"' declarations.
  Pre-Phase-3 audit measured tools.go in isolation (121) and missed
  the other 4 files (+34 unique names). The test at
  internal/ciparity/surface_parity_test.go::TestSurfaceParity_MCP
  passes today (155 ≥ 150). Added a clarifying comment near
  mcpBaselineFloor explaining the measurement scope so future
  reviewers don't repeat the audit's framing error.
  STATUS: stale — no code drift, just a measurement scoping error in
  the audit.

ARCH-L1 — panic() rationale comments
  5 panic sites in production Go (excluding _test.go):
    - internal/repository/postgres/tx.go:84
    - internal/service/issuer.go:861 (mustJSON)
    - internal/service/est.go:728 (mustParseTime)
    - internal/service/acme.go:1288 (rand source failure — already documented)
    - internal/pkcs7/certrep.go:270 (OID marshal — already documented)
  Added ARCH-L1 rationale comments to the 3 sites that didn't have
  them. All 5 are defensible impossible-path / rethrow / hardcoded-
  constant guards.

ARCH-L3 — Migration IF-NOT-EXISTS carve-outs
  4 migrations skip the literal 'IF NOT EXISTS' token but ARE
  idempotent via different Postgres patterns:
    - 000014_policy_violation_severity_check.up.sql: ALTER TABLE
      ADD CONSTRAINT CHECK doesn't accept IF NOT EXISTS; idempotency
      via DROP CONSTRAINT IF EXISTS preamble.
    - 000018_audit_events_worm.up.sql: CREATE OR REPLACE FUNCTION
      + DROP TRIGGER IF EXISTS + CREATE TRIGGER + DO $$ pg_roles
      existence check. CREATE TRIGGER doesn't take IF NOT EXISTS.
    - 000030_rbac_admin_perms.up.sql: INSERT ... ON CONFLICT DO NOTHING.
    - 000039_audit_crit1_perms.up.sql: same INSERT + ON CONFLICT pattern.
  Added ARCH-L3 header comments to each explaining the carve-out so
  reviewers don't flag the missing literal token.
  STATUS: largely stale — migrations are already idempotent.

ARCH-L4 — TODO/FIXME → see #<descriptor>
  5 TODOs rewritten to the allowed 'see #<descriptor>' pattern:
    - internal/repository/postgres/auth.go:220 → see #bundle-2-scope-fk
    - internal/connector/discovery/gcpsm/gcpsm.go:547 → see #gcpsm-pagination
    - internal/service/audit.go:244 → see #audit-pagination-count
    - internal/service/job.go:295, 299 → see #validation-job-impl
  New CI guard scripts/ci-guards/no-todo-in-prod.sh grep-fails any
  new TODO/FIXME in cmd/ + internal/ (excluding _test.go); allows
  'see #N' / 'see #<descriptor>' patterns.

Sandbox limitation
==================
The 6.1 GB certctl working tree fills the sandbox volume; go1.25.10
toolchain download fails with 'no space left on device' (sandbox has
1.25.9; go.mod requires 1.25.10). Local 'go test' / 'go build' NOT
run in this commit. Operator must run 'make verify' on their
workstation before push per CLAUDE.md operating rules.

The smoke.spec.ts NOT executed in the sandbox (no chromium installed).
Operator runs 'cd web && npm install && npx playwright install
--with-deps chromium && npm run e2e' on first wire-up.

All CI guards (no-todo-in-prod, skip-inventory-drift, G-3
env-docs-drift, doc-rot-detector, and every existing guard) verified
clean by running each individually.

Closes: cowork/certctl-architecture-diligence-audit.html#fix-TEST-H1,
        cowork/certctl-architecture-diligence-audit.html#fix-TEST-H2,
        cowork/certctl-architecture-diligence-audit.html#fix-TEST-M1,
        cowork/certctl-architecture-diligence-audit.html#fix-TEST-M2,
        cowork/certctl-architecture-diligence-audit.html#fix-TEST-M3,
        cowork/certctl-architecture-diligence-audit.html#fix-TEST-M4,
        cowork/certctl-architecture-diligence-audit.html#fix-TEST-L1,
        cowork/certctl-architecture-diligence-audit.html#fix-ARCH-H3,
        cowork/certctl-architecture-diligence-audit.html#fix-ARCH-L1,
        cowork/certctl-architecture-diligence-audit.html#fix-ARCH-L2,
        cowork/certctl-architecture-diligence-audit.html#fix-ARCH-L3,
        cowork/certctl-architecture-diligence-audit.html#fix-ARCH-L4
2026-05-13 20:10:08 +00:00

615 lines
18 KiB
Go

// Package gcpsm implements the domain.DiscoverySource interface for GCP Secret Manager.
//
// GCP Secret Manager is a Google Cloud service for securely storing and managing secrets,
// including certificates. This discovery source scans Secret Manager for certificates stored
// as secrets, filters by configured tags, and reports discovered certificate metadata
// back to the control plane for triage and management.
//
// Discovery approach:
// 1. Authenticate using service account JSON credentials (JWT → OAuth2 token exchange)
// 2. List all secrets in the configured GCP project
// 3. Filter by label "type=certificate"
// 4. For each secret, retrieve the latest version's data
// 5. Base64-decode the secret value, then attempt PEM or DER parsing
// 6. Extract certificate metadata (CN, SANs, serial, validity, key algorithm, etc.)
// 7. Report findings with sentinel agent ID "cloud-gcp-sm" and source path "gcp-sm://{project}/{secret-name}"
//
// Authentication: OAuth2 service account via JWT assertion. The service account
// credentials must be provided in a JSON file. The connector loads the private key,
// builds a JWT, exchanges it for an access token, then uses Bearer token auth for
// all subsequent Secret Manager API calls.
//
// GCP Secret Manager API operations used:
//
// GET /v1/projects/{project}/secrets - List secrets with filtering
// GET /v1/projects/{project}/secrets/{name}/versions/latest:access - Access secret data
package gcpsm
import (
"bytes"
"context"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/certctl-io/certctl/internal/config"
"github.com/certctl-io/certctl/internal/domain"
)
// serviceAccountKey represents the relevant fields from a Google service account JSON file.
type serviceAccountKey struct {
Type string `json:"type"`
ProjectID string `json:"project_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
TokenURI string `json:"token_uri"`
}
// cachedToken holds an OAuth2 access token and its expiry.
type cachedToken struct {
token string
expiresAt time.Time
}
// SMClient defines the interface for interacting with GCP Secret Manager.
// This allows for dependency injection and testing with mock clients.
type SMClient interface {
// ListSecrets lists secrets in the project, filtered by the "type=certificate" label.
ListSecrets(ctx context.Context, project string) ([]SecretEntry, error)
// AccessSecretVersion retrieves the latest version data for a secret.
AccessSecretVersion(ctx context.Context, project, secretName string) ([]byte, error)
}
// SecretEntry represents metadata about a secret from ListSecrets.
type SecretEntry struct {
Name string // Full resource name: projects/{project}/secrets/{name}
Labels map[string]string
}
// Source represents a GCP Secret Manager discovery source.
type Source struct {
cfg *config.GCPSecretMgrDiscoveryConfig
// For real HTTP client
httpClient *http.Client
// For test injection
client SMClient
logger *slog.Logger
// OAuth2 token caching
mu sync.Mutex
tokenCache *cachedToken
saKey *serviceAccountKey
rsaKey *rsa.PrivateKey
}
// New creates a new GCP Secret Manager discovery source with the given configuration.
// It uses the real HTTP client for authenticating with GCP.
func New(cfg *config.GCPSecretMgrDiscoveryConfig, logger *slog.Logger) *Source {
if logger == nil {
logger = slog.Default()
}
if cfg == nil {
cfg = &config.GCPSecretMgrDiscoveryConfig{}
}
return &Source{
cfg: cfg,
logger: logger,
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
}
}
// NewWithClient creates a new GCP Secret Manager discovery source with an injected client.
// This is primarily for testing.
func NewWithClient(cfg *config.GCPSecretMgrDiscoveryConfig, client SMClient, logger *slog.Logger) *Source {
if logger == nil {
logger = slog.Default()
}
if cfg == nil {
cfg = &config.GCPSecretMgrDiscoveryConfig{}
}
return &Source{
cfg: cfg,
client: client,
logger: logger,
}
}
// Name returns a human-readable name for this discovery source.
func (s *Source) Name() string {
return "GCP Secret Manager"
}
// Type returns the short type identifier for this discovery source.
func (s *Source) Type() string {
return "gcp-sm"
}
// ValidateConfig checks that the source is properly configured.
func (s *Source) ValidateConfig() error {
if s.cfg == nil {
return fmt.Errorf("gcp secret manager discovery config is nil")
}
if s.cfg.Project == "" {
return fmt.Errorf("gcp secret manager project is required")
}
if s.cfg.Credentials == "" {
return fmt.Errorf("gcp secret manager credentials path is required")
}
// Verify credentials file exists and is valid
_, _, err := loadServiceAccountKey(s.cfg.Credentials)
if err != nil {
return fmt.Errorf("gcp secret manager credentials invalid: %w", err)
}
return nil
}
// Discover scans GCP Secret Manager for certificates and returns a DiscoveryReport.
func (s *Source) Discover(ctx context.Context) (*domain.DiscoveryReport, error) {
if err := s.ValidateConfig(); err != nil {
return nil, fmt.Errorf("invalid gcp secret manager config: %w", err)
}
startTime := time.Now()
report := &domain.DiscoveryReport{
AgentID: "cloud-gcp-sm",
Directories: []string{fmt.Sprintf("gcp-sm://%s/", s.cfg.Project)},
Certificates: []domain.DiscoveredCertEntry{},
Errors: []string{},
}
// Get or create client (use injected mock for testing, real client otherwise)
var client SMClient
if s.client != nil {
client = s.client
} else {
client = &httpSMClient{
source: s,
logger: s.logger,
}
}
// List secrets in GCP Secret Manager
s.logger.Debug("listing secrets in gcp secret manager", "project", s.cfg.Project)
secrets, err := client.ListSecrets(ctx, s.cfg.Project)
if err != nil {
errMsg := fmt.Sprintf("failed to list secrets: %v", err)
report.Errors = append(report.Errors, errMsg)
s.logger.Error(errMsg)
return report, err
}
s.logger.Debug("found secrets", "count", len(secrets))
// Process each secret
for _, secret := range secrets {
// Extract secret name from full resource name: projects/{project}/secrets/{name}
parts := strings.Split(secret.Name, "/")
if len(parts) < 2 {
report.Errors = append(report.Errors, fmt.Sprintf("invalid secret name format: %s", secret.Name))
continue
}
secretName := parts[len(parts)-1]
// Access the latest version of the secret
data, err := client.AccessSecretVersion(ctx, s.cfg.Project, secretName)
if err != nil {
report.Errors = append(report.Errors, fmt.Sprintf("failed to access secret %s: %v", secretName, err))
s.logger.Warn("failed to access secret", "secret", secretName, "error", err)
continue
}
// Try to parse the data as a certificate (PEM or DER)
cert, err := parseCertificate(data)
if err != nil {
report.Errors = append(report.Errors, fmt.Sprintf("failed to parse certificate in secret %s: %v", secretName, err))
s.logger.Warn("failed to parse certificate", "secret", secretName, "error", err)
continue
}
// Extract certificate metadata
entry := s.extractCertificateMetadata(cert, secretName)
report.Certificates = append(report.Certificates, entry)
}
report.ScanDurationMs = int(time.Since(startTime).Milliseconds())
s.logger.Info("gcp secret manager discovery completed",
"project", s.cfg.Project,
"certificates_found", len(report.Certificates),
"errors", len(report.Errors),
"duration_ms", report.ScanDurationMs)
return report, nil
}
// extractCertificateMetadata extracts certificate metadata from an x509.Certificate.
func (s *Source) extractCertificateMetadata(cert *x509.Certificate, secretName string) domain.DiscoveredCertEntry {
// Compute SHA-256 fingerprint
certDER := cert.Raw
hash := sha256.Sum256(certDER)
fingerprint := strings.ToUpper(fmt.Sprintf("%x", hash[:]))
// Extract SANs
var sans []string
sans = append(sans, cert.DNSNames...)
sans = append(sans, cert.EmailAddresses...)
for _, ip := range cert.IPAddresses {
sans = append(sans, ip.String())
}
// Determine key algorithm and size
keyAlgo := "unknown"
keySize := 0
switch pk := cert.PublicKey.(type) {
case *rsa.PublicKey:
keyAlgo = "RSA"
keySize = pk.N.BitLen()
case *ecdsa.PublicKey:
keyAlgo = "ECDSA"
switch pk.Curve.Params().Name {
case "P-256":
keySize = 256
case "P-384":
keySize = 384
case "P-521":
keySize = 521
default:
keySize = pk.X.BitLen()
}
case ed25519.PublicKey:
keyAlgo = "Ed25519"
keySize = 253
}
// Format timestamps
notBeforeStr := cert.NotBefore.UTC().Format(time.RFC3339)
notAfterStr := cert.NotAfter.UTC().Format(time.RFC3339)
// Build PEM representation
pemData := encodeCertificatePEM(cert)
// Source path: gcp-sm://{project}/{secret-name}
sourcePath := fmt.Sprintf("gcp-sm://%s/%s", s.cfg.Project, secretName)
return domain.DiscoveredCertEntry{
FingerprintSHA256: fingerprint,
CommonName: cert.Subject.CommonName,
SANs: sans,
SerialNumber: fmt.Sprintf("%x", cert.SerialNumber),
IssuerDN: cert.Issuer.String(),
SubjectDN: cert.Subject.String(),
NotBefore: notBeforeStr,
NotAfter: notAfterStr,
KeyAlgorithm: keyAlgo,
KeySize: keySize,
IsCA: cert.IsCA,
PEMData: pemData,
SourcePath: sourcePath,
SourceFormat: "PEM",
}
}
// parseCertificate parses a certificate from data that may be PEM or base64-encoded DER.
func parseCertificate(data []byte) (*x509.Certificate, error) {
// First try PEM
block, _ := pem.Decode(data)
if block != nil && block.Type == "CERTIFICATE" {
return x509.ParseCertificate(block.Bytes)
}
// Try base64-decode and then DER
decoded, err := base64.StdEncoding.DecodeString(string(bytes.TrimSpace(data)))
if err == nil {
if cert, err := x509.ParseCertificate(decoded); err == nil {
return cert, nil
}
}
// Try raw DER
if cert, err := x509.ParseCertificate(data); err == nil {
return cert, nil
}
return nil, fmt.Errorf("failed to parse certificate from any format (PEM, base64 DER, or DER)")
}
// encodeCertificatePEM encodes an x509.Certificate as PEM.
func encodeCertificatePEM(cert *x509.Certificate) string {
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
}
return string(pem.EncodeToMemory(block))
}
// loadServiceAccountKey reads and parses a service account JSON file.
func loadServiceAccountKey(path string) (*serviceAccountKey, *rsa.PrivateKey, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, nil, fmt.Errorf("cannot read credentials file: %w", err)
}
var saKey serviceAccountKey
if err := json.Unmarshal(data, &saKey); err != nil {
return nil, nil, fmt.Errorf("cannot parse credentials JSON: %w", err)
}
if saKey.PrivateKey == "" {
return &saKey, nil, nil
}
// Parse the RSA private key
block, _ := pem.Decode([]byte(saKey.PrivateKey))
if block == nil {
return nil, nil, fmt.Errorf("cannot decode private key PEM")
}
// Try PKCS#8 first, then PKCS#1
var rsaKey *rsa.PrivateKey
if key, err := x509.ParsePKCS8PrivateKey(block.Bytes); err == nil {
var ok bool
rsaKey, ok = key.(*rsa.PrivateKey)
if !ok {
return nil, nil, fmt.Errorf("private key is not RSA")
}
} else if key, err := x509.ParsePKCS1PrivateKey(block.Bytes); err == nil {
rsaKey = key
} else {
return nil, nil, fmt.Errorf("cannot parse private key: not PKCS#8 or PKCS#1")
}
return &saKey, rsaKey, nil
}
// getAccessToken returns a valid OAuth2 access token, refreshing if needed.
func (s *Source) getAccessToken(ctx context.Context) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
// Return cached token if still valid (5 min buffer)
if s.tokenCache != nil && time.Now().Add(5*time.Minute).Before(s.tokenCache.expiresAt) {
return s.tokenCache.token, nil
}
// Load credentials if not cached
if s.saKey == nil || s.rsaKey == nil {
saKey, rsaKey, err := loadServiceAccountKey(s.cfg.Credentials)
if err != nil {
return "", fmt.Errorf("failed to load credentials: %w", err)
}
s.saKey = saKey
s.rsaKey = rsaKey
}
// Build JWT
now := time.Now()
header := base64URLEncode([]byte(`{"alg":"RS256","typ":"JWT"}`))
claims, err := json.Marshal(map[string]interface{}{
"iss": s.saKey.ClientEmail,
"scope": "https://www.googleapis.com/auth/cloud-platform",
"aud": s.saKey.TokenURI,
"iat": now.Unix(),
"exp": now.Add(time.Hour).Unix(),
})
if err != nil {
return "", fmt.Errorf("failed to marshal JWT claims: %w", err)
}
payload := base64URLEncode(claims)
// Sign
signingInput := header + "." + payload
hash := sha256.Sum256([]byte(signingInput))
sig, err := rsa.SignPKCS1v15(rand.Reader, s.rsaKey, crypto.SHA256, hash[:])
if err != nil {
return "", fmt.Errorf("failed to sign JWT: %w", err)
}
jwt := signingInput + "." + base64URLEncode(sig)
// Exchange JWT for access token
form := url.Values{
"grant_type": {"urn:ietf:params:oauth:grant-type:jwt-bearer"},
"assertion": {jwt},
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.saKey.TokenURI,
strings.NewReader(form.Encode()))
if err != nil {
return "", fmt.Errorf("failed to create token request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.httpClient.Do(req)
if err != nil {
return "", fmt.Errorf("token exchange failed: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read token response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("token exchange returned status %d: %s", resp.StatusCode, string(body))
}
var tokenResp struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
}
if err := json.Unmarshal(body, &tokenResp); err != nil {
return "", fmt.Errorf("failed to parse token response: %w", err)
}
if tokenResp.AccessToken == "" {
return "", fmt.Errorf("empty access token in response")
}
// Cache token
s.tokenCache = &cachedToken{
token: tokenResp.AccessToken,
expiresAt: now.Add(time.Duration(tokenResp.ExpiresIn) * time.Second),
}
return tokenResp.AccessToken, nil
}
// httpSMClient implements SMClient using the real GCP Secret Manager HTTP API.
type httpSMClient struct {
source *Source
logger *slog.Logger
}
// ListSecrets lists all secrets in the project, filtered by "type=certificate" label.
func (c *httpSMClient) ListSecrets(ctx context.Context, project string) ([]SecretEntry, error) {
token, err := c.source.getAccessToken(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get access token: %w", err)
}
// Build the list request URL with filter
// Filter for secrets with label "type=certificate"
filter := `labels.type=certificate`
listURL := fmt.Sprintf("https://secretmanager.googleapis.com/v1/projects/%s/secrets?filter=%s",
url.QueryEscape(project), url.QueryEscape(filter))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, listURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create list request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token)
resp, err := c.source.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("list secrets request failed: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read list response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("list secrets returned status %d: %s", resp.StatusCode, string(body))
}
// Parse response
var listResp struct {
Secrets []struct {
Name string `json:"name"`
Labels map[string]string `json:"labels"`
} `json:"secrets"`
NextPageToken string `json:"nextPageToken"`
}
if err := json.Unmarshal(body, &listResp); err != nil {
return nil, fmt.Errorf("failed to parse list response: %w", err)
}
var secrets []SecretEntry
for _, s := range listResp.Secrets {
secrets = append(secrets, SecretEntry{
Name: s.Name,
Labels: s.Labels,
})
}
// see #gcpsm-pagination — handle nextPageToken if needed for large
// secret managers. For now, just return the first page results;
// typical GCP Secret Manager pages contain up to 500 secrets, which
// covers every operator we've heard from so far. Add pagination when
// the first 500-secret-tenant surfaces.
return secrets, nil
}
// AccessSecretVersion retrieves the latest version of a secret's data.
func (c *httpSMClient) AccessSecretVersion(ctx context.Context, project, secretName string) ([]byte, error) {
token, err := c.source.getAccessToken(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get access token: %w", err)
}
// Build the access request URL
accessURL := fmt.Sprintf("https://secretmanager.googleapis.com/v1/projects/%s/secrets/%s/versions/latest:access",
url.QueryEscape(project), url.QueryEscape(secretName))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, accessURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create access request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token)
resp, err := c.source.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("access secret request failed: %w", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read access response: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("access secret returned status %d: %s", resp.StatusCode, string(body))
}
// Parse response to extract the payload data field
var accessResp struct {
Payload struct {
Data string `json:"data"` // base64-encoded secret data
} `json:"payload"`
}
if err := json.Unmarshal(body, &accessResp); err != nil {
return nil, fmt.Errorf("failed to parse access response: %w", err)
}
// Decode the base64-encoded data
data, err := base64.StdEncoding.DecodeString(accessResp.Payload.Data)
if err != nil {
return nil, fmt.Errorf("failed to base64-decode secret data: %w", err)
}
return data, nil
}
// base64URLEncode encodes data using base64url without padding.
func base64URLEncode(data []byte) string {
return base64.RawURLEncoding.EncodeToString(data)
}
// Ensure Source implements the domain.DiscoverySource interface.
var _ domain.DiscoverySource = (*Source)(nil)