Replace the CoreDNS precursor with a purpose-built authoritative DNS server. Zones and records (A, AAAA, CNAME) are stored in SQLite and managed via synchronized gRPC + REST APIs authenticated through MCIAS. Non-authoritative queries are forwarded to upstream resolvers with in-memory caching. Key components: - DNS server (miekg/dns) with authoritative zone handling and forwarding - gRPC + REST management APIs with MCIAS auth (mcdsl integration) - SQLite storage with CNAME exclusivity enforcement and auto SOA serials - 30 tests covering database CRUD, DNS resolution, and caching Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
package grpcserver
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
pb "git.wntrmute.dev/kyle/mcns/gen/mcns/v1"
|
|
"git.wntrmute.dev/kyle/mcns/internal/db"
|
|
)
|
|
|
|
type recordService struct {
|
|
pb.UnimplementedRecordServiceServer
|
|
db *db.DB
|
|
}
|
|
|
|
func (s *recordService) ListRecords(_ context.Context, req *pb.ListRecordsRequest) (*pb.ListRecordsResponse, error) {
|
|
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")
|
|
}
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, "failed to list records")
|
|
}
|
|
|
|
resp := &pb.ListRecordsResponse{}
|
|
for _, r := range records {
|
|
resp.Records = append(resp.Records, recordToProto(r))
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *recordService) GetRecord(_ context.Context, req *pb.GetRecordRequest) (*pb.Record, error) {
|
|
record, err := s.db.GetRecord(req.Id)
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
return nil, status.Error(codes.NotFound, "record not found")
|
|
}
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, "failed to get record")
|
|
}
|
|
return recordToProto(*record), nil
|
|
}
|
|
|
|
func (s *recordService) CreateRecord(_ context.Context, req *pb.CreateRecordRequest) (*pb.Record, error) {
|
|
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")
|
|
}
|
|
if errors.Is(err, db.ErrConflict) {
|
|
return nil, status.Error(codes.AlreadyExists, err.Error())
|
|
}
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
return recordToProto(*record), nil
|
|
}
|
|
|
|
func (s *recordService) UpdateRecord(_ context.Context, req *pb.UpdateRecordRequest) (*pb.Record, error) {
|
|
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")
|
|
}
|
|
if errors.Is(err, db.ErrConflict) {
|
|
return nil, status.Error(codes.AlreadyExists, err.Error())
|
|
}
|
|
if err != nil {
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
}
|
|
return recordToProto(*record), nil
|
|
}
|
|
|
|
func (s *recordService) DeleteRecord(_ context.Context, req *pb.DeleteRecordRequest) (*pb.DeleteRecordResponse, error) {
|
|
err := s.db.DeleteRecord(req.Id)
|
|
if errors.Is(err, db.ErrNotFound) {
|
|
return nil, status.Error(codes.NotFound, "record not found")
|
|
}
|
|
if err != nil {
|
|
return nil, status.Error(codes.Internal, "failed to delete record")
|
|
}
|
|
return &pb.DeleteRecordResponse{}, nil
|
|
}
|
|
|
|
func recordToProto(r db.Record) *pb.Record {
|
|
return &pb.Record{
|
|
Id: r.ID,
|
|
Zone: r.ZoneName,
|
|
Name: r.Name,
|
|
Type: r.Type,
|
|
Value: r.Value,
|
|
Ttl: int32(r.TTL),
|
|
CreatedAt: parseRecordTimestamp(r.CreatedAt),
|
|
UpdatedAt: parseRecordTimestamp(r.UpdatedAt),
|
|
}
|
|
}
|
|
|
|
func parseRecordTimestamp(s string) *timestamppb.Timestamp {
|
|
t, err := parseTime(s)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return timestamppb.New(t)
|
|
}
|
|
|
|
func parseTime(s string) (time.Time, error) {
|
|
return time.Parse("2006-01-02T15:04:05Z", s)
|
|
}
|