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"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
@@ -15,10 +16,15 @@ import (
|
||||
|
||||
type recordService struct {
|
||||
pb.UnimplementedRecordServiceServer
|
||||
db *db.DB
|
||||
db *db.DB
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (s *recordService) ListRecords(_ context.Context, req *pb.ListRecordsRequest) (*pb.ListRecordsResponse, error) {
|
||||
if req.Zone == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "zone is required")
|
||||
}
|
||||
|
||||
records, err := s.db.ListRecords(req.Zone, req.Name, req.Type)
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "zone not found")
|
||||
@@ -29,12 +35,16 @@ func (s *recordService) ListRecords(_ context.Context, req *pb.ListRecordsReques
|
||||
|
||||
resp := &pb.ListRecordsResponse{}
|
||||
for _, r := range records {
|
||||
resp.Records = append(resp.Records, recordToProto(r))
|
||||
resp.Records = append(resp.Records, s.recordToProto(r))
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *recordService) GetRecord(_ context.Context, req *pb.GetRecordRequest) (*pb.Record, error) {
|
||||
if req.Id <= 0 {
|
||||
return nil, status.Error(codes.InvalidArgument, "id must be positive")
|
||||
}
|
||||
|
||||
record, err := s.db.GetRecord(req.Id)
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "record not found")
|
||||
@@ -42,10 +52,23 @@ func (s *recordService) GetRecord(_ context.Context, req *pb.GetRecordRequest) (
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.Internal, "failed to get record")
|
||||
}
|
||||
return recordToProto(*record), nil
|
||||
return s.recordToProto(*record), nil
|
||||
}
|
||||
|
||||
func (s *recordService) CreateRecord(_ context.Context, req *pb.CreateRecordRequest) (*pb.Record, error) {
|
||||
if req.Zone == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "zone is required")
|
||||
}
|
||||
if req.Name == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "name is required")
|
||||
}
|
||||
if req.Type == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "type is required")
|
||||
}
|
||||
if req.Value == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "value is required")
|
||||
}
|
||||
|
||||
record, err := s.db.CreateRecord(req.Zone, req.Name, req.Type, req.Value, int(req.Ttl))
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "zone not found")
|
||||
@@ -56,10 +79,23 @@ func (s *recordService) CreateRecord(_ context.Context, req *pb.CreateRecordRequ
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
return recordToProto(*record), nil
|
||||
return s.recordToProto(*record), nil
|
||||
}
|
||||
|
||||
func (s *recordService) UpdateRecord(_ context.Context, req *pb.UpdateRecordRequest) (*pb.Record, error) {
|
||||
if req.Id <= 0 {
|
||||
return nil, status.Error(codes.InvalidArgument, "id must be positive")
|
||||
}
|
||||
if req.Name == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "name is required")
|
||||
}
|
||||
if req.Type == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "type is required")
|
||||
}
|
||||
if req.Value == "" {
|
||||
return nil, status.Error(codes.InvalidArgument, "value is required")
|
||||
}
|
||||
|
||||
record, err := s.db.UpdateRecord(req.Id, req.Name, req.Type, req.Value, int(req.Ttl))
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "record not found")
|
||||
@@ -70,10 +106,14 @@ func (s *recordService) UpdateRecord(_ context.Context, req *pb.UpdateRecordRequ
|
||||
if err != nil {
|
||||
return nil, status.Error(codes.InvalidArgument, err.Error())
|
||||
}
|
||||
return recordToProto(*record), nil
|
||||
return s.recordToProto(*record), nil
|
||||
}
|
||||
|
||||
func (s *recordService) DeleteRecord(_ context.Context, req *pb.DeleteRecordRequest) (*pb.DeleteRecordResponse, error) {
|
||||
if req.Id <= 0 {
|
||||
return nil, status.Error(codes.InvalidArgument, "id must be positive")
|
||||
}
|
||||
|
||||
err := s.db.DeleteRecord(req.Id)
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return nil, status.Error(codes.NotFound, "record not found")
|
||||
@@ -84,7 +124,7 @@ func (s *recordService) DeleteRecord(_ context.Context, req *pb.DeleteRecordRequ
|
||||
return &pb.DeleteRecordResponse{}, nil
|
||||
}
|
||||
|
||||
func recordToProto(r db.Record) *pb.Record {
|
||||
func (s *recordService) recordToProto(r db.Record) *pb.Record {
|
||||
return &pb.Record{
|
||||
Id: r.ID,
|
||||
Zone: r.ZoneName,
|
||||
@@ -92,14 +132,15 @@ func recordToProto(r db.Record) *pb.Record {
|
||||
Type: r.Type,
|
||||
Value: r.Value,
|
||||
Ttl: int32(r.TTL),
|
||||
CreatedAt: parseRecordTimestamp(r.CreatedAt),
|
||||
UpdatedAt: parseRecordTimestamp(r.UpdatedAt),
|
||||
CreatedAt: s.parseRecordTimestamp(r.CreatedAt),
|
||||
UpdatedAt: s.parseRecordTimestamp(r.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func parseRecordTimestamp(s string) *timestamppb.Timestamp {
|
||||
t, err := parseTime(s)
|
||||
func (s *recordService) parseRecordTimestamp(v string) *timestamppb.Timestamp {
|
||||
t, err := parseTime(v)
|
||||
if err != nil {
|
||||
s.logger.Warn("failed to parse record timestamp", "value", v, "error", err)
|
||||
return nil
|
||||
}
|
||||
return timestamppb.New(t)
|
||||
|
||||
Reference in New Issue
Block a user