feat: add safe multi-account routing and prepare public release
This commit is contained in:
71 files changed
+2906
-120
No files matched your search
+157
-55
@@ -91,14 +91,15 @@ type DNSBatchResult struct {
|
||||
type plannedDNSBatch struct {
|
||||
plan DNSBatchPlan
|
||||
zone domain.Zone
|
||||
cloud provider.CloudProvider
|
||||
cloud provider.Provider
|
||||
batch provider.DNSBatchService
|
||||
auth provider.Auth
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
repo store.Repository
|
||||
secrets store.SecretStore
|
||||
providers map[string]provider.CloudProvider
|
||||
providers map[string]provider.Provider
|
||||
now func() time.Time
|
||||
events chan Event
|
||||
healthCheck func(context.Context) error
|
||||
@@ -107,17 +108,22 @@ type Service struct {
|
||||
recoveryBlocked atomic.Bool
|
||||
}
|
||||
|
||||
var (
|
||||
_ provider.DNS01Solver = (*Service)(nil)
|
||||
_ provider.DNS01Reconciler = (*Service)(nil)
|
||||
)
|
||||
|
||||
func New(repo store.Repository, secrets store.SecretStore) *Service {
|
||||
return &Service{
|
||||
repo: repo,
|
||||
secrets: secrets,
|
||||
providers: make(map[string]provider.CloudProvider),
|
||||
providers: make(map[string]provider.Provider),
|
||||
now: time.Now,
|
||||
events: make(chan Event, 128),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) RegisterProvider(name string, value provider.CloudProvider) {
|
||||
func (s *Service) RegisterProvider(name string, value provider.Provider) {
|
||||
s.providers[name] = value
|
||||
}
|
||||
|
||||
@@ -177,6 +183,50 @@ func (s *Service) ResolveCredential(ctx context.Context, credentialID string) (p
|
||||
return s.auth(ctx, credential)
|
||||
}
|
||||
|
||||
// PresentDNS01, CleanupDNS01, and ReconcileDNS01 make Service the provider
|
||||
// router used by the ACME engine and crash recovery. Routing is based only on
|
||||
// the provider identity bound into Auth; remote zone IDs are not globally
|
||||
// unique and must never select a provider implicitly.
|
||||
func (s *Service) PresentDNS01(ctx context.Context, auth provider.Auth, zoneID, fqdn, value, jobID string) (string, error) {
|
||||
p, err := s.dns01Provider(auth)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return p.PresentDNS01(ctx, auth, zoneID, fqdn, value, jobID)
|
||||
}
|
||||
|
||||
func (s *Service) CleanupDNS01(ctx context.Context, auth provider.Auth, zoneID, recordID string) error {
|
||||
p, err := s.dns01Provider(auth)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return p.CleanupDNS01(ctx, auth, zoneID, recordID)
|
||||
}
|
||||
|
||||
func (s *Service) ReconcileDNS01(ctx context.Context, auth provider.Auth, zoneID, fqdn, valueHash, jobID string) (string, bool, error) {
|
||||
p, err := s.dns01Provider(auth)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
reconciler, ok := p.(provider.DNS01Reconciler)
|
||||
if !ok {
|
||||
return "", false, provider.Unsupported(auth.Provider, "DNS-01 reconciliation")
|
||||
}
|
||||
return reconciler.ReconcileDNS01(ctx, auth, zoneID, fqdn, valueHash, jobID)
|
||||
}
|
||||
|
||||
func (s *Service) dns01Provider(auth provider.Auth) (provider.Provider, error) {
|
||||
providerName := strings.TrimSpace(auth.Provider)
|
||||
if providerName == "" {
|
||||
return nil, errors.New("DNS-01 credential has no provider identity")
|
||||
}
|
||||
p, ok := s.providers[providerName]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("DNS-01 provider %q is not registered", providerName)
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (s *Service) IssueCertificates(ctx context.Context, request IssueZonesRequest) (IssueZonesResult, error) {
|
||||
if err := s.ensureRecoveryReady(ctx); err != nil {
|
||||
return IssueZonesResult{}, err
|
||||
@@ -341,7 +391,7 @@ func (s *Service) AddCloudflareCredential(ctx context.Context, input AddCredenti
|
||||
}
|
||||
|
||||
id := identifier.New("cred")
|
||||
auth := provider.Auth{CredentialID: id, Token: strings.TrimSpace(input.Token), Kind: input.Kind, AccountID: strings.TrimSpace(input.AccountID)}
|
||||
auth := provider.Auth{Provider: domain.ProviderCloudflare, CredentialID: id, Token: strings.TrimSpace(input.Token), Kind: input.Kind, AccountID: strings.TrimSpace(input.AccountID)}
|
||||
verification, err := p.VerifyCredential(ctx, auth)
|
||||
if err != nil {
|
||||
return domain.Credential{}, fmt.Errorf("verify Cloudflare credential: %w", err)
|
||||
@@ -393,7 +443,7 @@ func (s *Service) AddCloudflareCredential(ctx context.Context, input AddCredenti
|
||||
if len(verification.Accounts) > 0 {
|
||||
links := make([]domain.AccountCredential, 0, len(verification.Accounts))
|
||||
for i := range verification.Accounts {
|
||||
normalizeAccount(&verification.Accounts[i])
|
||||
normalizeAccount(&verification.Accounts[i], domain.ProviderCloudflare)
|
||||
_, known := knownAccounts[verification.Accounts[i].ID]
|
||||
links = append(links, domain.AccountCredential{AccountID: verification.Accounts[i].ID, CredentialID: id, Preferred: !known})
|
||||
}
|
||||
@@ -567,7 +617,7 @@ func compensateSecretDelete(ctx context.Context, secrets store.SecretStore, refe
|
||||
}
|
||||
|
||||
func (s *Service) SyncCredential(ctx context.Context, credentialID string) (domain.Job, error) {
|
||||
job := s.startJob(ctx, "cloudflare.sync", credentialID, "Connecting to Cloudflare")
|
||||
job := s.startJob(ctx, "provider.sync", credentialID, "Connecting to provider")
|
||||
fail := func(err error) (domain.Job, error) {
|
||||
job.State = domain.JobFailed
|
||||
job.Error = err.Error()
|
||||
@@ -586,6 +636,10 @@ func (s *Service) SyncCredential(ctx context.Context, credentialID string) (doma
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
job.Kind = credential.Provider + ".sync"
|
||||
job.Message = "Connecting to " + credential.Provider
|
||||
job.UpdatedAt = s.now().UTC()
|
||||
_ = s.repo.SaveJob(ctx, job)
|
||||
p, ok := s.providers[credential.Provider]
|
||||
if !ok {
|
||||
return fail(fmt.Errorf("provider %q is not registered", credential.Provider))
|
||||
@@ -653,13 +707,14 @@ func (s *Service) SyncCredential(ctx context.Context, credentialID string) (doma
|
||||
|
||||
accountByID := make(map[string]domain.RemoteAccount)
|
||||
for _, account := range verification.Accounts {
|
||||
normalizeAccount(&account)
|
||||
normalizeAccount(&account, credential.Provider)
|
||||
accountByID[account.ID] = account
|
||||
}
|
||||
returnedZoneIDs := make(map[string]struct{}, len(zones))
|
||||
newZoneIDs := make(map[string]struct{}, len(zones))
|
||||
for i := range zones {
|
||||
normalizeZone(&zones[i], "")
|
||||
accountProviderID := zones[i].AccountID
|
||||
normalizeZone(&zones[i], credential.Provider, "")
|
||||
key := zones[i].Provider + "\x00" + zones[i].ProviderID
|
||||
returnedZoneIDs[key] = struct{}{}
|
||||
if existing, known := existingZoneByProviderID[key]; known {
|
||||
@@ -676,10 +731,13 @@ func (s *Service) SyncCredential(ctx context.Context, credentialID string) (doma
|
||||
newZoneIDs[zones[i].ID] = struct{}{}
|
||||
}
|
||||
if _, exists := accountByID[zones[i].AccountID]; !exists {
|
||||
providerID := strings.TrimPrefix(zones[i].AccountID, "cfacct_")
|
||||
providerID := accountProviderID
|
||||
if credential.Provider == domain.ProviderCloudflare {
|
||||
providerID = strings.TrimPrefix(zones[i].AccountID, "cfacct_")
|
||||
}
|
||||
accountByID[zones[i].AccountID] = domain.RemoteAccount{
|
||||
ID: zones[i].AccountID, Provider: domain.ProviderCloudflare,
|
||||
ProviderID: providerID, Name: "Cloudflare account " + shortID(providerID), CreatedAt: s.now().UTC(),
|
||||
ID: zones[i].AccountID, Provider: credential.Provider,
|
||||
ProviderID: providerID, Name: credential.Provider + " account " + shortID(providerID), CreatedAt: s.now().UTC(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -714,7 +772,7 @@ func (s *Service) SyncCredential(ctx context.Context, credentialID string) (doma
|
||||
if visibilityErr != nil || !visible {
|
||||
continue
|
||||
}
|
||||
normalizeZone(&visibleZone, "")
|
||||
normalizeZone(&visibleZone, candidate.Provider, "")
|
||||
visibleZone.PreferredCredentialID = ""
|
||||
visibleZone.LastSyncedAt = &staleAt
|
||||
existing = visibleZone
|
||||
@@ -774,7 +832,7 @@ func (s *Service) SyncCredential(ctx context.Context, credentialID string) (doma
|
||||
if listErr == nil {
|
||||
now := s.now().UTC()
|
||||
for i := range records {
|
||||
normalizeRecord(&records[i], zone.ID)
|
||||
normalizeRecord(&records[i], zone.Provider, zone.ID)
|
||||
records[i].LastSyncedAt = &now
|
||||
}
|
||||
listErr = s.repo.ReplaceDNSRecords(groupCtx, zone.ID, records, now)
|
||||
@@ -842,7 +900,7 @@ func (s *Service) CreateDNSRecord(ctx context.Context, zoneID string, record dom
|
||||
)
|
||||
}
|
||||
s.observeZoneCapability(context.WithoutCancel(ctx), auth.CredentialID, zone.ID, "dns:write")
|
||||
normalizeRecord(&created, zone.ID)
|
||||
normalizeRecord(&created, zone.Provider, zone.ID)
|
||||
s.audit(context.WithoutCancel(ctx), "dns.create", created.ID, nil, created)
|
||||
if _, reconcileErr := s.reconcileDNSZone(ctx, zone, p, auth); reconcileErr != nil {
|
||||
return created, dnsMutationAppliedButUnreconciledError("create", reconcileErr)
|
||||
@@ -898,7 +956,7 @@ func (s *Service) PatchDNSRecord(ctx context.Context, zoneID, recordID string, d
|
||||
reconcileStructuredRecordData(&desired, remote)
|
||||
desired.Raw = append(json.RawMessage(nil), remote.Raw...)
|
||||
desired.Proxiable = remote.Proxiable
|
||||
normalizeRecord(&remote, zone.ID)
|
||||
normalizeRecord(&remote, zone.Provider, zone.ID)
|
||||
updated, mutationErr := p.PatchDNSRecord(ctx, auth, zone.ProviderID, cached.ProviderID, desired)
|
||||
if mutationErr != nil {
|
||||
if mutationFailureIsDefinitive(mutationErr) {
|
||||
@@ -909,7 +967,7 @@ func (s *Service) PatchDNSRecord(ctx context.Context, zoneID, recordID string, d
|
||||
)
|
||||
}
|
||||
s.observeZoneCapability(context.WithoutCancel(ctx), auth.CredentialID, zone.ID, "dns:write")
|
||||
normalizeRecord(&updated, zone.ID)
|
||||
normalizeRecord(&updated, zone.Provider, zone.ID)
|
||||
s.audit(context.WithoutCancel(ctx), "dns.patch", updated.ID, remote, updated)
|
||||
if _, reconcileErr := s.reconcileDNSZone(ctx, zone, p, auth); reconcileErr != nil {
|
||||
return updated, dnsMutationAppliedButUnreconciledError("patch", reconcileErr)
|
||||
@@ -952,7 +1010,7 @@ func (s *Service) DeleteDNSRecord(ctx context.Context, zoneID, recordID string)
|
||||
if !sameDNSRecordVersion(cached, remote) {
|
||||
return fmt.Errorf("%w: %s", ErrDNSConflict, cached.Name)
|
||||
}
|
||||
normalizeRecord(&remote, zone.ID)
|
||||
normalizeRecord(&remote, zone.Provider, zone.ID)
|
||||
if mutationErr := p.DeleteDNSRecord(ctx, auth, zone.ProviderID, cached.ProviderID); mutationErr != nil {
|
||||
if mutationFailureIsDefinitive(mutationErr) {
|
||||
return mutationErr
|
||||
@@ -991,7 +1049,7 @@ func (s *Service) ApplyDNSBatch(ctx context.Context, zoneID string, mutations []
|
||||
return DNSBatchResult{}, err
|
||||
}
|
||||
result := DNSBatchResult{Plan: planned.plan}
|
||||
if _, applyErr := planned.cloud.ApplyDNSBatch(ctx, planned.auth, planned.zone.ProviderID, planned.plan.Mutations); applyErr != nil {
|
||||
if _, applyErr := planned.batch.ApplyDNSBatch(ctx, planned.auth, planned.zone.ProviderID, planned.plan.Mutations); applyErr != nil {
|
||||
if mutationFailureIsDefinitive(applyErr) {
|
||||
return result, applyErr
|
||||
}
|
||||
@@ -1004,7 +1062,7 @@ func (s *Service) ApplyDNSBatch(ctx context.Context, zoneID string, mutations []
|
||||
if refreshErr == nil {
|
||||
now := s.now().UTC()
|
||||
for index := range records {
|
||||
normalizeRecord(&records[index], planned.zone.ID)
|
||||
normalizeRecord(&records[index], planned.zone.Provider, planned.zone.ID)
|
||||
records[index].LastSyncedAt = &now
|
||||
}
|
||||
result.Records = records
|
||||
@@ -1015,7 +1073,7 @@ func (s *Service) ApplyDNSBatch(ctx context.Context, zoneID string, mutations []
|
||||
}
|
||||
}
|
||||
return result, errors.Join(
|
||||
fmt.Errorf("Cloudflare batch outcome is unknown; synchronize and review before retrying: %w", applyErr),
|
||||
fmt.Errorf("%s DNS batch outcome is unknown; synchronize and review before retrying: %w", planned.zone.Provider, applyErr),
|
||||
refreshErr,
|
||||
)
|
||||
}
|
||||
@@ -1030,7 +1088,7 @@ func (s *Service) ApplyDNSBatch(ctx context.Context, zoneID string, mutations []
|
||||
}
|
||||
now := s.now().UTC()
|
||||
for index := range records {
|
||||
normalizeRecord(&records[index], planned.zone.ID)
|
||||
normalizeRecord(&records[index], planned.zone.Provider, planned.zone.ID)
|
||||
records[index].LastSyncedAt = &now
|
||||
}
|
||||
result.Records = records
|
||||
@@ -1056,7 +1114,7 @@ func mutationFailureIsDefinitive(err error) bool {
|
||||
// snapshot. It deliberately outlives cancellation of the mutation request, but
|
||||
// remains bounded so an interrupted client cannot leave reconciliation running
|
||||
// indefinitely.
|
||||
func (s *Service) reconcileDNSZone(ctx context.Context, zone domain.Zone, cloud provider.CloudProvider, auth provider.Auth) ([]domain.DNSRecord, error) {
|
||||
func (s *Service) reconcileDNSZone(ctx context.Context, zone domain.Zone, cloud provider.Provider, auth provider.Auth) ([]domain.DNSRecord, error) {
|
||||
reconcileCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), dnsMutationReconcileTimeout)
|
||||
defer cancel()
|
||||
records, err := listAllRecords(reconcileCtx, cloud, auth, zone.ProviderID)
|
||||
@@ -1065,7 +1123,7 @@ func (s *Service) reconcileDNSZone(ctx context.Context, zone domain.Zone, cloud
|
||||
}
|
||||
now := s.now().UTC()
|
||||
for index := range records {
|
||||
normalizeRecord(&records[index], zone.ID)
|
||||
normalizeRecord(&records[index], zone.Provider, zone.ID)
|
||||
records[index].LastSyncedAt = &now
|
||||
}
|
||||
cacheErr := s.repo.ReplaceDNSRecords(reconcileCtx, zone.ID, records, now)
|
||||
@@ -1089,7 +1147,7 @@ func (s *Service) dnsMutationOutcomeUnknownError(
|
||||
before, after interface{},
|
||||
mutationErr error,
|
||||
zone domain.Zone,
|
||||
cloud provider.CloudProvider,
|
||||
cloud provider.Provider,
|
||||
auth provider.Auth,
|
||||
) error {
|
||||
s.audit(context.WithoutCancel(ctx), "dns."+action+".outcome_unknown", resourceID, before, after)
|
||||
@@ -1119,6 +1177,10 @@ func (s *Service) planDNSBatch(ctx context.Context, zoneID string, mutations []d
|
||||
if err != nil {
|
||||
return plannedDNSBatch{}, err
|
||||
}
|
||||
batch, ok := cloud.(provider.DNSBatchService)
|
||||
if !ok {
|
||||
return plannedDNSBatch{}, provider.Unsupported(zone.Provider, "DNS batch")
|
||||
}
|
||||
cachedRecords, err := s.repo.ListDNSRecords(ctx, zone.ID)
|
||||
if err != nil {
|
||||
return plannedDNSBatch{}, err
|
||||
@@ -1175,7 +1237,7 @@ func (s *Service) planDNSBatch(ctx context.Context, zoneID string, mutations []d
|
||||
return plannedDNSBatch{}, fmt.Errorf("DNS batch mutation %d: %w: %s", index, ErrDNSConflict, cached.Name)
|
||||
}
|
||||
remoteForPlan := cloneDNSRecord(remote)
|
||||
normalizeRecord(&remoteForPlan, zone.ID)
|
||||
normalizeRecord(&remoteForPlan, zone.Provider, zone.ID)
|
||||
plannedMutation.RecordID = cached.ProviderID
|
||||
plannedMutation.Before = &remoteForPlan
|
||||
if mutation.Kind == domain.MutationDelete {
|
||||
@@ -1212,7 +1274,7 @@ func (s *Service) planDNSBatch(ctx context.Context, zoneID string, mutations []d
|
||||
}
|
||||
plan.Mutations = append(plan.Mutations, plannedMutation)
|
||||
}
|
||||
return plannedDNSBatch{plan: plan, zone: zone, cloud: cloud, auth: auth}, nil
|
||||
return plannedDNSBatch{plan: plan, zone: zone, cloud: cloud, batch: batch, auth: auth}, nil
|
||||
}
|
||||
|
||||
func dnsMutationRecordID(mutation domain.DNSMutation) string {
|
||||
@@ -1293,7 +1355,11 @@ func (s *Service) TLSSettings(ctx context.Context, zoneID string, refresh bool)
|
||||
if err != nil {
|
||||
return domain.EdgeTLSSettings{}, err
|
||||
}
|
||||
settings, err := p.GetEdgeTLSSettings(ctx, auth, zone.ProviderID)
|
||||
tlsProvider, ok := p.(provider.EdgeTLSService)
|
||||
if !ok {
|
||||
return domain.EdgeTLSSettings{}, provider.Unsupported(zone.Provider, "edge TLS")
|
||||
}
|
||||
settings, err := tlsProvider.GetEdgeTLSSettings(ctx, auth, zone.ProviderID)
|
||||
if err != nil {
|
||||
return domain.EdgeTLSSettings{}, err
|
||||
}
|
||||
@@ -1313,10 +1379,14 @@ func (s *Service) EdgeCertificates(ctx context.Context, zoneID string) ([]domain
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tlsProvider, ok := p.(provider.EdgeTLSService)
|
||||
if !ok {
|
||||
return nil, provider.Unsupported(zone.Provider, "edge TLS")
|
||||
}
|
||||
const pageSize = 50
|
||||
values := make([]domain.EdgeCertificate, 0, pageSize)
|
||||
for page := 1; page <= 200; page++ {
|
||||
batch, listErr := p.ListEdgeCertificates(ctx, auth, zone.ProviderID, provider.PageRequest{Page: page, PerPage: pageSize})
|
||||
batch, listErr := tlsProvider.ListEdgeCertificates(ctx, auth, zone.ProviderID, provider.PageRequest{Page: page, PerPage: pageSize})
|
||||
if listErr != nil {
|
||||
return nil, listErr
|
||||
}
|
||||
@@ -1342,6 +1412,10 @@ func (s *Service) UpdateTLSSettings(ctx context.Context, zoneID string, desired
|
||||
if err != nil {
|
||||
return domain.EdgeTLSSettings{}, err
|
||||
}
|
||||
tlsProvider, ok := p.(provider.EdgeTLSService)
|
||||
if !ok {
|
||||
return domain.EdgeTLSSettings{}, provider.Unsupported(zone.Provider, "edge TLS")
|
||||
}
|
||||
baseline, err := s.repo.GetTLSSettings(ctx, zone.ID)
|
||||
if err != nil {
|
||||
return domain.EdgeTLSSettings{}, fmt.Errorf("load displayed TLS baseline: %w", err)
|
||||
@@ -1349,7 +1423,7 @@ func (s *Service) UpdateTLSSettings(ctx context.Context, zoneID string, desired
|
||||
if desired.LastSyncedAt != nil && baseline.LastSyncedAt != nil && !desired.LastSyncedAt.Equal(*baseline.LastSyncedAt) {
|
||||
return domain.EdgeTLSSettings{}, ErrTLSConflict
|
||||
}
|
||||
before, err := p.GetEdgeTLSSettings(ctx, auth, zone.ProviderID)
|
||||
before, err := tlsProvider.GetEdgeTLSSettings(ctx, auth, zone.ProviderID)
|
||||
if err != nil {
|
||||
return domain.EdgeTLSSettings{}, fmt.Errorf("re-fetch Cloudflare TLS settings: %w", err)
|
||||
}
|
||||
@@ -1369,7 +1443,7 @@ func (s *Service) UpdateTLSSettings(ctx context.Context, zoneID string, desired
|
||||
return before, nil
|
||||
}
|
||||
desired.ZoneID = zone.ProviderID
|
||||
updated, updateErr := p.UpdateEdgeTLSSettings(ctx, auth, zone.ProviderID, baseline, desired)
|
||||
updated, updateErr := tlsProvider.UpdateEdgeTLSSettings(ctx, auth, zone.ProviderID, baseline, desired)
|
||||
if updateErr != nil {
|
||||
reconcileCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second)
|
||||
defer cancel()
|
||||
@@ -1380,7 +1454,7 @@ func (s *Service) UpdateTLSSettings(ctx context.Context, zoneID string, desired
|
||||
saveErr := s.repo.SaveTLSSettings(reconcileCtx, before)
|
||||
return before, errors.Join(updateErr, wrapError("save unchanged TLS settings", saveErr))
|
||||
}
|
||||
effective, refreshErr := p.GetEdgeTLSSettings(reconcileCtx, auth, zone.ProviderID)
|
||||
effective, refreshErr := tlsProvider.GetEdgeTLSSettings(reconcileCtx, auth, zone.ProviderID)
|
||||
if refreshErr != nil {
|
||||
return domain.EdgeTLSSettings{}, errors.Join(
|
||||
ErrTLSMutationOutcomeUnknown,
|
||||
@@ -1490,6 +1564,10 @@ func (s *Service) ObserveCredentialZoneCapability(ctx context.Context, credentia
|
||||
(capability != "dns:read" && capability != "dns:write" && capability != "tls:read" && capability != "tls:write") {
|
||||
return errors.New("unsupported observed credential capability")
|
||||
}
|
||||
credential, err := s.repo.GetCredential(ctx, credentialID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
zone, err := s.repo.GetZone(ctx, zoneReference)
|
||||
if err != nil {
|
||||
zones, listErr := s.repo.ListZones(ctx)
|
||||
@@ -1498,7 +1576,7 @@ func (s *Service) ObserveCredentialZoneCapability(ctx context.Context, credentia
|
||||
}
|
||||
zone = domain.Zone{}
|
||||
for _, candidate := range zones {
|
||||
if candidate.ProviderID == zoneReference {
|
||||
if candidate.Provider == credential.Provider && candidate.ProviderID == zoneReference {
|
||||
zone = candidate
|
||||
break
|
||||
}
|
||||
@@ -1507,10 +1585,6 @@ func (s *Service) ObserveCredentialZoneCapability(ctx context.Context, credentia
|
||||
return fmt.Errorf("zone %q for observed capability was not found", zoneReference)
|
||||
}
|
||||
}
|
||||
credential, err := s.repo.GetCredential(ctx, credentialID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if credential.Provider != zone.Provider {
|
||||
return errors.New("observed capability credential and zone providers differ")
|
||||
}
|
||||
@@ -1537,10 +1611,10 @@ func (s *Service) auth(ctx context.Context, credential domain.Credential) (provi
|
||||
if err != nil {
|
||||
return provider.Auth{}, fmt.Errorf("unlock credential %q: %w", credential.Label, err)
|
||||
}
|
||||
return provider.Auth{CredentialID: credential.ID, Token: string(secret), Kind: credential.Kind, AccountID: credential.AccountHint}, nil
|
||||
return provider.Auth{Provider: credential.Provider, CredentialID: credential.ID, Token: string(secret), Kind: credential.Kind, AccountID: credential.AccountHint}, nil
|
||||
}
|
||||
|
||||
func (s *Service) zoneProvider(ctx context.Context, zoneID string) (domain.Zone, provider.CloudProvider, provider.Auth, error) {
|
||||
func (s *Service) zoneProvider(ctx context.Context, zoneID string) (domain.Zone, provider.Provider, provider.Auth, error) {
|
||||
zone, err := s.repo.GetZone(ctx, zoneID)
|
||||
if err != nil {
|
||||
return domain.Zone{}, nil, provider.Auth{}, err
|
||||
@@ -1563,7 +1637,7 @@ func (s *Service) zoneProvider(ctx context.Context, zoneID string) (domain.Zone,
|
||||
return zone, p, auth, err
|
||||
}
|
||||
|
||||
func (s *Service) dnsZoneProvider(ctx context.Context, zoneID string) (domain.Zone, provider.CloudProvider, provider.Auth, error) {
|
||||
func (s *Service) dnsZoneProvider(ctx context.Context, zoneID string) (domain.Zone, provider.Provider, provider.Auth, error) {
|
||||
zone, cloud, auth, err := s.zoneProvider(ctx, zoneID)
|
||||
if err != nil {
|
||||
return domain.Zone{}, nil, provider.Auth{}, err
|
||||
@@ -1695,25 +1769,46 @@ func advancePage(request *provider.PageRequest, nextCursor string, responsePage,
|
||||
return false
|
||||
}
|
||||
|
||||
func normalizeAccount(account *domain.RemoteAccount) {
|
||||
func normalizeAccount(account *domain.RemoteAccount, providerName string) {
|
||||
if providerName == "" {
|
||||
providerName = account.Provider
|
||||
}
|
||||
if account.ProviderID == "" {
|
||||
account.ProviderID = strings.TrimPrefix(account.ID, "cfacct_")
|
||||
account.ProviderID = account.ID
|
||||
if providerName == domain.ProviderCloudflare {
|
||||
account.ProviderID = strings.TrimPrefix(account.ProviderID, "cfacct_")
|
||||
}
|
||||
}
|
||||
account.Provider = providerName
|
||||
if providerName == domain.ProviderCloudflare {
|
||||
account.ID = "cfacct_" + account.ProviderID
|
||||
} else {
|
||||
account.ID = stableID("account", providerName, account.ProviderID)
|
||||
}
|
||||
account.Provider = domain.ProviderCloudflare
|
||||
account.ID = "cfacct_" + account.ProviderID
|
||||
if account.CreatedAt.IsZero() {
|
||||
account.CreatedAt = time.Now().UTC()
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeZone(zone *domain.Zone, credentialID string) {
|
||||
if zone.ProviderID == "" {
|
||||
zone.ProviderID = strings.TrimPrefix(zone.ID, "cfzone_")
|
||||
func normalizeZone(zone *domain.Zone, providerName, credentialID string) {
|
||||
if providerName == "" {
|
||||
providerName = zone.Provider
|
||||
}
|
||||
zone.Provider = domain.ProviderCloudflare
|
||||
zone.ID = "cfzone_" + zone.ProviderID
|
||||
if !strings.HasPrefix(zone.AccountID, "cfacct_") {
|
||||
zone.AccountID = "cfacct_" + zone.AccountID
|
||||
if zone.ProviderID == "" {
|
||||
zone.ProviderID = zone.ID
|
||||
if providerName == domain.ProviderCloudflare {
|
||||
zone.ProviderID = strings.TrimPrefix(zone.ProviderID, "cfzone_")
|
||||
}
|
||||
}
|
||||
zone.Provider = providerName
|
||||
if providerName == domain.ProviderCloudflare {
|
||||
zone.ID = "cfzone_" + zone.ProviderID
|
||||
if !strings.HasPrefix(zone.AccountID, "cfacct_") {
|
||||
zone.AccountID = "cfacct_" + zone.AccountID
|
||||
}
|
||||
} else {
|
||||
zone.ID = stableID("zone", providerName, zone.ProviderID)
|
||||
zone.AccountID = stableID("account", providerName, zone.AccountID)
|
||||
}
|
||||
zone.PreferredCredentialID = credentialID
|
||||
if zone.Status == "" {
|
||||
@@ -1721,11 +1816,18 @@ func normalizeZone(zone *domain.Zone, credentialID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeRecord(record *domain.DNSRecord, zoneID string) {
|
||||
func normalizeRecord(record *domain.DNSRecord, providerName, zoneID string) {
|
||||
if record.ProviderID == "" {
|
||||
record.ProviderID = strings.TrimPrefix(record.ID, "cfrecord_")
|
||||
record.ProviderID = record.ID
|
||||
if providerName == domain.ProviderCloudflare {
|
||||
record.ProviderID = strings.TrimPrefix(record.ProviderID, "cfrecord_")
|
||||
}
|
||||
}
|
||||
if providerName == domain.ProviderCloudflare {
|
||||
record.ID = "cfrecord_" + record.ProviderID
|
||||
} else {
|
||||
record.ID = stableID("record", providerName, zoneID, record.ProviderID)
|
||||
}
|
||||
record.ID = "cfrecord_" + record.ProviderID
|
||||
record.ZoneID = zoneID
|
||||
if unicodeName, err := idna.Lookup.ToUnicode(record.Name); err == nil {
|
||||
record.UnicodeName = unicodeName
|
||||
@@ -1797,7 +1899,7 @@ func validateRecordValue(record *domain.DNSRecord) error {
|
||||
// Empty TXT values are valid and intentionally preserved.
|
||||
}
|
||||
if record.Proxied && record.Type != domain.RecordA && record.Type != domain.RecordAAAA && record.Type != domain.RecordCNAME {
|
||||
return fmt.Errorf("record type %s cannot be Cloudflare proxied", record.Type)
|
||||
return fmt.Errorf("record type %s cannot be proxied by this provider", record.Type)
|
||||
}
|
||||
if record.Priority != nil && record.Type != domain.RecordMX && record.Type != domain.RecordSRV {
|
||||
return fmt.Errorf("record type %s does not use priority", record.Type)
|
||||
|
||||
@@ -132,7 +132,7 @@ func TestServiceMultiCredentialSyncAndDNSLifecycle(t *testing.T) {
|
||||
}
|
||||
|
||||
auth, providerZoneID, err := service.ResolveDNS01(ctx, "_acme-challenge.api.example.com.")
|
||||
if err != nil || auth.Token != "token-one" || providerZoneID != "zone-provider" {
|
||||
if err != nil || auth.Provider != domain.ProviderCloudflare || auth.CredentialID != first.ID || auth.Token != "token-one" || providerZoneID != "zone-provider" {
|
||||
t.Fatalf("DNS-01 resolution = %#v, %q, %v", auth, providerZoneID, err)
|
||||
}
|
||||
|
||||
@@ -1123,6 +1123,130 @@ func TestSetPreferredCredentialLiveProbePersistsZoneReadEvidence(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNS01RouterDispatchesExactProviderAndCredential(t *testing.T) {
|
||||
service := New(nil, nil)
|
||||
alpha := &routingProvider{name: "alpha"}
|
||||
beta := &routingProvider{name: "beta"}
|
||||
service.RegisterProvider("alpha", alpha)
|
||||
service.RegisterProvider("beta", beta)
|
||||
auth := provider.Auth{Provider: "beta", CredentialID: "credential-beta", Token: "beta-secret", Kind: domain.CredentialUserToken}
|
||||
|
||||
recordID, err := service.PresentDNS01(context.Background(), auth, "shared-zone-id", "_acme.example.", "value", "job-1")
|
||||
if err != nil || recordID != "beta-record" {
|
||||
t.Fatalf("PresentDNS01 = %q, %v", recordID, err)
|
||||
}
|
||||
if err := service.CleanupDNS01(context.Background(), auth, "shared-zone-id", recordID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reconciledID, found, err := service.ReconcileDNS01(context.Background(), auth, "shared-zone-id", "_acme.example.", "hash", "job-1")
|
||||
if err != nil || !found || reconciledID != "beta-reconciled" {
|
||||
t.Fatalf("ReconcileDNS01 = %q, %v, %v", reconciledID, found, err)
|
||||
}
|
||||
if len(alpha.calls) != 0 {
|
||||
t.Fatalf("unselected provider received calls: %#v", alpha.calls)
|
||||
}
|
||||
if len(beta.calls) != 3 {
|
||||
t.Fatalf("selected provider calls = %#v", beta.calls)
|
||||
}
|
||||
for _, call := range beta.calls {
|
||||
if call.auth != auth || call.zoneID != "shared-zone-id" {
|
||||
t.Fatalf("routed call changed auth/zone: %#v", call)
|
||||
}
|
||||
}
|
||||
if _, err := service.PresentDNS01(context.Background(), provider.Auth{CredentialID: "credential-beta"}, "shared-zone-id", "fqdn", "value", "job"); err == nil || !strings.Contains(err.Error(), "no provider identity") {
|
||||
t.Fatalf("provider-less auth was routed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOptionalProviderCapabilitiesFailClearly(t *testing.T) {
|
||||
fake := newFakeCloudProvider()
|
||||
service, _, zone := newBatchTestService(t, fake)
|
||||
service.RegisterProvider(domain.ProviderCloudflare, coreOnlyProvider{Provider: fake})
|
||||
|
||||
mutation := domain.DNSMutation{Kind: domain.MutationCreate, After: &domain.DNSRecord{Type: domain.RecordA, Name: "new", Content: "192.0.2.20", TTL: 300}}
|
||||
if _, err := service.PlanDNSBatch(context.Background(), zone.ID, []domain.DNSMutation{mutation}); !errors.Is(err, provider.ErrCapabilityUnsupported) || !strings.Contains(err.Error(), "DNS batch") {
|
||||
t.Fatalf("unsupported batch error = %v", err)
|
||||
}
|
||||
if _, err := service.TLSSettings(context.Background(), zone.ID, true); !errors.Is(err, provider.ErrCapabilityUnsupported) || !strings.Contains(err.Error(), "edge TLS") {
|
||||
t.Fatalf("unsupported TLS read error = %v", err)
|
||||
}
|
||||
if _, err := service.EdgeCertificates(context.Background(), zone.ID); !errors.Is(err, provider.ErrCapabilityUnsupported) {
|
||||
t.Fatalf("unsupported TLS inventory error = %v", err)
|
||||
}
|
||||
if _, err := service.UpdateTLSSettings(context.Background(), zone.ID, domain.EdgeTLSSettings{}); !errors.Is(err, provider.ErrCapabilityUnsupported) {
|
||||
t.Fatalf("unsupported TLS update error = %v", err)
|
||||
}
|
||||
auth, err := service.ResolveCredential(context.Background(), zone.PreferredCredentialID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := service.ReconcileDNS01(context.Background(), auth, zone.ProviderID, "_acme.example.", "hash", "job"); !errors.Is(err, provider.ErrCapabilityUnsupported) || !strings.Contains(err.Error(), "DNS-01 reconciliation") {
|
||||
t.Fatalf("unsupported DNS-01 reconciliation error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProviderNeutralNormalizationPreservesCloudflareAndIsolatesRemoteIDs(t *testing.T) {
|
||||
cloudflareAccount := domain.RemoteAccount{ID: "remote-account"}
|
||||
normalizeAccount(&cloudflareAccount, domain.ProviderCloudflare)
|
||||
if cloudflareAccount.ID != "cfacct_remote-account" || cloudflareAccount.Provider != domain.ProviderCloudflare {
|
||||
t.Fatalf("Cloudflare account normalization changed: %#v", cloudflareAccount)
|
||||
}
|
||||
cloudflareZone := domain.Zone{ID: "remote-zone", AccountID: "remote-account"}
|
||||
normalizeZone(&cloudflareZone, domain.ProviderCloudflare, "credential")
|
||||
if cloudflareZone.ID != "cfzone_remote-zone" || cloudflareZone.AccountID != "cfacct_remote-account" {
|
||||
t.Fatalf("Cloudflare zone normalization changed: %#v", cloudflareZone)
|
||||
}
|
||||
cloudflareRecord := domain.DNSRecord{ID: "remote-record"}
|
||||
normalizeRecord(&cloudflareRecord, domain.ProviderCloudflare, cloudflareZone.ID)
|
||||
if cloudflareRecord.ID != "cfrecord_remote-record" {
|
||||
t.Fatalf("Cloudflare record normalization changed: %#v", cloudflareRecord)
|
||||
}
|
||||
|
||||
firstZone := domain.Zone{ID: "shared-zone", AccountID: "shared-account"}
|
||||
secondZone := firstZone
|
||||
normalizeZone(&firstZone, "provider-a", "credential-a")
|
||||
normalizeZone(&secondZone, "provider-b", "credential-b")
|
||||
if firstZone.ID == secondZone.ID || firstZone.AccountID == secondZone.AccountID {
|
||||
t.Fatalf("provider-local zone/account IDs collided: %#v / %#v", firstZone, secondZone)
|
||||
}
|
||||
firstRecord := domain.DNSRecord{ID: "shared-record"}
|
||||
secondRecord := firstRecord
|
||||
normalizeRecord(&firstRecord, "provider-a", firstZone.ID)
|
||||
normalizeRecord(&secondRecord, "provider-b", secondZone.ID)
|
||||
if firstRecord.ID == secondRecord.ID || firstRecord.ProviderID != "shared-record" || secondRecord.ProviderID != "shared-record" {
|
||||
t.Fatalf("provider-local record IDs were not isolated: %#v / %#v", firstRecord, secondRecord)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservedCapabilityProviderIDFallbackCannotCrossProviders(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
service, repository, cloudflareZone := newBatchTestService(t, newFakeCloudProvider())
|
||||
credential, err := repository.GetCredential(ctx, cloudflareZone.PreferredCredentialID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreignAccount := domain.RemoteAccount{ID: stableID("account", "other", "account"), Provider: "other", ProviderID: "account", Name: "Other", CreatedAt: time.Now().UTC()}
|
||||
if err := repository.SaveAccounts(ctx, []domain.RemoteAccount{foreignAccount}, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreignZone := domain.Zone{ID: stableID("zone", "other", cloudflareZone.ProviderID), Provider: "other", ProviderID: cloudflareZone.ProviderID, AccountID: foreignAccount.ID, Name: "aaa-other.example", Status: domain.ZoneActive}
|
||||
if err := repository.SaveZones(ctx, []domain.Zone{foreignZone}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := service.ObserveCredentialZoneCapability(ctx, credential.ID, cloudflareZone.ProviderID, "tls:write"); err != nil {
|
||||
t.Fatalf("provider-scoped fallback: %v", err)
|
||||
}
|
||||
cloudflareObserved, err := repository.HasCredentialZoneCapability(ctx, credential.ID, cloudflareZone.ID, "tls:write")
|
||||
if err != nil || !cloudflareObserved {
|
||||
t.Fatalf("Cloudflare observation = %v, %v", cloudflareObserved, err)
|
||||
}
|
||||
foreignObserved, err := repository.HasCredentialZoneCapability(ctx, credential.ID, foreignZone.ID, "tls:write")
|
||||
if err != nil || foreignObserved {
|
||||
t.Fatalf("foreign observation = %v, %v", foreignObserved, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCredentialDeleteKeepsMetadataWhenVaultDeleteFails(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
fake := newFakeCloudProvider()
|
||||
@@ -1323,6 +1447,53 @@ type providerRequest struct {
|
||||
Cursor string
|
||||
}
|
||||
|
||||
type coreOnlyProvider struct{ provider.Provider }
|
||||
|
||||
type routingCall struct {
|
||||
action string
|
||||
auth provider.Auth
|
||||
zoneID string
|
||||
}
|
||||
|
||||
type routingProvider struct {
|
||||
name string
|
||||
calls []routingCall
|
||||
}
|
||||
|
||||
func (p *routingProvider) VerifyCredential(context.Context, provider.Auth) (provider.Verification, error) {
|
||||
return provider.Verification{Status: domain.CredentialValid}, nil
|
||||
}
|
||||
func (p *routingProvider) ListZones(context.Context, provider.Auth, provider.PageRequest) (provider.ZonePage, error) {
|
||||
return provider.ZonePage{}, nil
|
||||
}
|
||||
func (p *routingProvider) ListDNSRecords(context.Context, provider.Auth, string, provider.PageRequest) (provider.RecordPage, error) {
|
||||
return provider.RecordPage{}, nil
|
||||
}
|
||||
func (p *routingProvider) GetDNSRecord(context.Context, provider.Auth, string, string) (domain.DNSRecord, error) {
|
||||
return domain.DNSRecord{}, nil
|
||||
}
|
||||
func (p *routingProvider) CreateDNSRecord(context.Context, provider.Auth, string, domain.DNSRecord) (domain.DNSRecord, error) {
|
||||
return domain.DNSRecord{}, nil
|
||||
}
|
||||
func (p *routingProvider) PatchDNSRecord(context.Context, provider.Auth, string, string, domain.DNSRecord) (domain.DNSRecord, error) {
|
||||
return domain.DNSRecord{}, nil
|
||||
}
|
||||
func (p *routingProvider) DeleteDNSRecord(context.Context, provider.Auth, string, string) error {
|
||||
return nil
|
||||
}
|
||||
func (p *routingProvider) PresentDNS01(_ context.Context, auth provider.Auth, zoneID, _, _, _ string) (string, error) {
|
||||
p.calls = append(p.calls, routingCall{action: "present", auth: auth, zoneID: zoneID})
|
||||
return p.name + "-record", nil
|
||||
}
|
||||
func (p *routingProvider) CleanupDNS01(_ context.Context, auth provider.Auth, zoneID, _ string) error {
|
||||
p.calls = append(p.calls, routingCall{action: "cleanup", auth: auth, zoneID: zoneID})
|
||||
return nil
|
||||
}
|
||||
func (p *routingProvider) ReconcileDNS01(_ context.Context, auth provider.Auth, zoneID, _, _, _ string) (string, bool, error) {
|
||||
p.calls = append(p.calls, routingCall{action: "reconcile", auth: auth, zoneID: zoneID})
|
||||
return p.name + "-reconciled", true, nil
|
||||
}
|
||||
|
||||
type paginatedZoneProvider struct{ requests []providerRequest }
|
||||
|
||||
func (p *paginatedZoneProvider) ListZones(_ context.Context, _ provider.Auth, request provider.PageRequest) (provider.ZonePage, error) {
|
||||
|
||||
Reference in new issue
Block a user