Apply review fixes: validation, idempotency, SOA dedup, startup cleanup
- Migration v2: INSERT → INSERT OR IGNORE for idempotency - Config: validate server.tls_cert and server.tls_key are non-empty - gRPC: add input validation matching REST handlers - gRPC: add logger to zone/record services, log timestamp parse errors - REST+gRPC: extract SOA defaults into shared db.ApplySOADefaults() - DNS: simplify SOA query condition (remove dead code from precedence bug) - Startup: consolidate shutdown into shutdownAll(), clean up gRPC listener on error path, shut down sibling servers when one fails Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package grpcserver
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
@@ -14,7 +15,8 @@ import (
|
||||
|
||||
type zoneService struct {
|
||||
pb.UnimplementedZoneServiceServer
|
||||
db *db.DB
|
||||
db *db.DB
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (s *zoneService) ListZones(_ context.Context, _ *pb.ListZonesRequest) (*pb.ListZonesResponse, error) {
|
||||
@@ -25,12 +27,16 @@ func (s *zoneService) ListZones(_ context.Context, _ *pb.ListZonesRequest) (*pb.
|
||||
|
||||
resp := &pb.ListZonesResponse{}
|
||||
for _, z := range zones {
|
||||
resp.Zones = append(resp.Zones, zoneToProto(z))
|
||||
resp.Zones = append(resp.Zones, s.zoneToProto(z))
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *zoneService) GetZone(_ context.Context, req *pb.GetZoneRequest) (*pb.Zone, error) {
|
||||
if req.Name == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "name is required")
|
||||
}
|
||||
|
||||
zone, err := s.db.GetZone(req.Name)
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "zone not found")
|
||||
@@ -38,27 +44,22 @@ func (s *zoneService) GetZone(_ context.Context, req *pb.GetZoneRequest) (*pb.Zo
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "failed to get zone")
|
||||
}
|
||||
return zoneToProto(*zone), nil
|
||||
return s.zoneToProto(*zone), nil
|
||||
}
|
||||
|
||||
func (s *zoneService) CreateZone(_ context.Context, req *pb.CreateZoneRequest) (*pb.Zone, error) {
|
||||
refresh := int(req.Refresh)
|
||||
if refresh == 0 {
|
||||
refresh = 3600
|
||||
if req.Name == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "name is required")
|
||||
}
|
||||
retry := int(req.Retry)
|
||||
if retry == 0 {
|
||||
retry = 600
|
||||
if req.PrimaryNs == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "primary_ns is required")
|
||||
}
|
||||
expire := int(req.Expire)
|
||||
if expire == 0 {
|
||||
expire = 86400
|
||||
}
|
||||
minTTL := int(req.MinimumTtl)
|
||||
if minTTL == 0 {
|
||||
minTTL = 300
|
||||
if req.AdminEmail == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "admin_email is required")
|
||||
}
|
||||
|
||||
refresh, retry, expire, minTTL := db.ApplySOADefaults(int(req.Refresh), int(req.Retry), int(req.Expire), int(req.MinimumTtl))
|
||||
|
||||
zone, err := s.db.CreateZone(req.Name, req.PrimaryNs, req.AdminEmail, refresh, retry, expire, minTTL)
|
||||
if errors.Is(err, db.ErrConflict) {
|
||||
return nil, status.Error(codes.AlreadyExists, err.Error())
|
||||
@@ -66,27 +67,22 @@ func (s *zoneService) CreateZone(_ context.Context, req *pb.CreateZoneRequest) (
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "failed to create zone")
|
||||
}
|
||||
return zoneToProto(*zone), nil
|
||||
return s.zoneToProto(*zone), nil
|
||||
}
|
||||
|
||||
func (s *zoneService) UpdateZone(_ context.Context, req *pb.UpdateZoneRequest) (*pb.Zone, error) {
|
||||
refresh := int(req.Refresh)
|
||||
if refresh == 0 {
|
||||
refresh = 3600
|
||||
if req.Name == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "name is required")
|
||||
}
|
||||
retry := int(req.Retry)
|
||||
if retry == 0 {
|
||||
retry = 600
|
||||
if req.PrimaryNs == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "primary_ns is required")
|
||||
}
|
||||
expire := int(req.Expire)
|
||||
if expire == 0 {
|
||||
expire = 86400
|
||||
}
|
||||
minTTL := int(req.MinimumTtl)
|
||||
if minTTL == 0 {
|
||||
minTTL = 300
|
||||
if req.AdminEmail == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "admin_email is required")
|
||||
}
|
||||
|
||||
refresh, retry, expire, minTTL := db.ApplySOADefaults(int(req.Refresh), int(req.Retry), int(req.Expire), int(req.MinimumTtl))
|
||||
|
||||
zone, err := s.db.UpdateZone(req.Name, req.PrimaryNs, req.AdminEmail, refresh, retry, expire, minTTL)
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "zone not found")
|
||||
@@ -94,10 +90,14 @@ func (s *zoneService) UpdateZone(_ context.Context, req *pb.UpdateZoneRequest) (
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "failed to update zone")
|
||||
}
|
||||
return zoneToProto(*zone), nil
|
||||
return s.zoneToProto(*zone), nil
|
||||
}
|
||||
|
||||
func (s *zoneService) DeleteZone(_ context.Context, req *pb.DeleteZoneRequest) (*pb.DeleteZoneResponse, error) {
|
||||
if req.Name == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "name is required")
|
||||
}
|
||||
|
||||
err := s.db.DeleteZone(req.Name)
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "zone not found")
|
||||
@@ -108,7 +108,7 @@ func (s *zoneService) DeleteZone(_ context.Context, req *pb.DeleteZoneRequest) (
|
||||
return &pb.DeleteZoneResponse{}, nil
|
||||
}
|
||||
|
||||
func zoneToProto(z db.Zone) *pb.Zone {
|
||||
func (s *zoneService) zoneToProto(z db.Zone) *pb.Zone {
|
||||
return &pb.Zone{
|
||||
Id: z.ID,
|
||||
Name: z.Name,
|
||||
@@ -119,15 +119,15 @@ func zoneToProto(z db.Zone) *pb.Zone {
|
||||
Expire: int32(z.Expire),
|
||||
MinimumTtl: int32(z.MinimumTTL),
|
||||
Serial: z.Serial,
|
||||
CreatedAt: parseTimestamp(z.CreatedAt),
|
||||
UpdatedAt: parseTimestamp(z.UpdatedAt),
|
||||
CreatedAt: s.parseTimestamp(z.CreatedAt),
|
||||
UpdatedAt: s.parseTimestamp(z.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func parseTimestamp(s string) *timestamppb.Timestamp {
|
||||
// SQLite stores as "2006-01-02T15:04:05Z".
|
||||
t, err := parseTime(s)
|
||||
func (s *zoneService) parseTimestamp(v string) *timestamppb.Timestamp {
|
||||
t, err := parseTime(v)
|
||||
if err != nil {
|
||||
s.logger.Warn("failed to parse zone timestamp", "value", v, "error", err)
|
||||
return nil
|
||||
}
|
||||
return timestamppb.New(t)
|
||||
|
||||
Reference in New Issue
Block a user