mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-04 14:11:22 +09:00
Phase 0 of the DNS roadmap. #2 Shared Cloudflare client (app/services/dns/): DnsCredential + DnsRecordSpec + CloudflareClient. Both DNSProviderService and DNSZoneService delegate to it, so auth (scoped token / global key), the CAA structured-data wire format, MX/SRV priority parsing, and idempotent upsert live in one place. Fixes dup-on-recreate, silent update/delete no-op, and missing proxied/priority on the provider path. #1 Single credential source: new FK dns_zones.dns_provider_config_id (migration 025) + DNSZoneService._resolve_credential (linked -> auto-discover-by-domain -> legacy inline-token). create_zone accepts a connection id; link_legacy_zones() converges inline tokens onto encrypted connections at boot. The /dns create-zone modal now picks a connection instead of pasting a token. DDNS rides it for free. Tests: test_dns_cloudflare_client.py, test_dns_credential_resolution.py. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
84 lines
3.2 KiB
Python
84 lines
3.2 KiB
Python
from datetime import datetime
|
|
from app import db
|
|
import json
|
|
|
|
|
|
class DNSZone(db.Model):
|
|
"""DNS zone for a domain with provider integration."""
|
|
__tablename__ = 'dns_zones'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
domain = db.Column(db.String(256), nullable=False, unique=True)
|
|
provider = db.Column(db.String(64)) # cloudflare, route53, digitalocean, manual
|
|
provider_zone_id = db.Column(db.String(128))
|
|
provider_config_json = db.Column(db.Text) # legacy inline credentials (pre-unification)
|
|
|
|
# Canonical credential link: the DNSProviderConfig (Settings -> Connections)
|
|
# whose API token manages this zone. Supersedes the inline token in
|
|
# provider_config_json; nullable so manual zones and not-yet-migrated rows work.
|
|
dns_provider_config_id = db.Column(
|
|
db.Integer, db.ForeignKey('dns_provider_configs.id'), nullable=True)
|
|
|
|
status = db.Column(db.String(32), default='active')
|
|
last_sync_at = db.Column(db.DateTime)
|
|
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
records = db.relationship('DNSRecord', backref='zone', lazy='dynamic', cascade='all, delete-orphan')
|
|
|
|
@property
|
|
def provider_config(self):
|
|
return json.loads(self.provider_config_json) if self.provider_config_json else {}
|
|
|
|
@provider_config.setter
|
|
def provider_config(self, v):
|
|
self.provider_config_json = json.dumps(v)
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'id': self.id,
|
|
'domain': self.domain,
|
|
'provider': self.provider,
|
|
'provider_zone_id': self.provider_zone_id,
|
|
'dns_provider_config_id': self.dns_provider_config_id,
|
|
'status': self.status,
|
|
'record_count': self.records.count(),
|
|
'last_sync_at': self.last_sync_at.isoformat() if self.last_sync_at else None,
|
|
'created_at': self.created_at.isoformat() if self.created_at else None,
|
|
}
|
|
|
|
|
|
class DNSRecord(db.Model):
|
|
"""Individual DNS record within a zone."""
|
|
__tablename__ = 'dns_records'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
zone_id = db.Column(db.Integer, db.ForeignKey('dns_zones.id'), nullable=False)
|
|
|
|
record_type = db.Column(db.String(10), nullable=False) # A, AAAA, CNAME, MX, TXT, SRV, CAA
|
|
name = db.Column(db.String(256), nullable=False)
|
|
content = db.Column(db.Text, nullable=False)
|
|
ttl = db.Column(db.Integer, default=3600)
|
|
priority = db.Column(db.Integer) # MX, SRV
|
|
proxied = db.Column(db.Boolean, default=False) # Cloudflare proxy
|
|
|
|
provider_record_id = db.Column(db.String(128))
|
|
|
|
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'id': self.id,
|
|
'zone_id': self.zone_id,
|
|
'record_type': self.record_type,
|
|
'name': self.name,
|
|
'content': self.content,
|
|
'ttl': self.ttl,
|
|
'priority': self.priority,
|
|
'proxied': self.proxied,
|
|
'provider_record_id': self.provider_record_id,
|
|
'created_at': self.created_at.isoformat() if self.created_at else None,
|
|
}
|