File
Blob: config/rpc.go
| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | |
| 8 | "go.miragespace.co/specter/spec/protocol" |
| 9 | "go.miragespace.co/specter/util" |
| 10 | |
| 11 | "github.com/go-chi/chi/v5/middleware" |
| 12 | "github.com/twitchtv/twirp" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | // MaxGetRequestBytes bounds keyed reads before the generated handler decodes them. |
| 17 | MaxGetRequestBytes = 4 << 10 |
| 18 | // Three length-delimited fields add their tags and length prefixes. |
| 19 | maxGetResponseBytes = MaxNameBytes + versionBytes + MaxValueBytes + 9 |
| 20 | ) |
| 21 | |
| 22 | // ReadHandler exposes only keyed reads on the local RPC listener. Peer exchange |
| 23 | // remains available only through the authenticated configuration transport. |
| 24 | func ReadHandler(service protocol.ConfigService) http.Handler { |
| 25 | server := protocol.NewConfigServiceServer(service) |
| 26 | read := middleware.SetHeader("Cache-Control", "no-store")(util.LimitBody(MaxGetRequestBytes)(server)) |
| 27 | mux := http.NewServeMux() |
| 28 | mux.Handle(server.PathPrefix()+"Get", read) |
| 29 | return mux |
| 30 | } |
| 31 | |
| 32 | // Get reads one document from the local durable replica without interpreting its schema. |
| 33 | func (s *Syncer) Get(_ context.Context, request *protocol.ConfigGetRequest) (*protocol.ConfigRecord, error) { |
| 34 | name := request.GetName() |
| 35 | if !validName(name) { |
| 36 | return nil, twirp.InvalidArgument.Error("invalid configuration document name") |
| 37 | } |
| 38 | record, found, err := s.store.Get(name) |
| 39 | if err != nil { |
| 40 | return nil, twirp.Unavailable.Error(err.Error()) |
| 41 | } |
| 42 | if !found { |
| 43 | return nil, twirp.NotFound.Error("configuration document not found") |
| 44 | } |
| 45 | return &protocol.ConfigRecord{Name: record.Name, Version: record.Version.bytes(), Value: record.Value}, nil |
| 46 | } |
| 47 | |
| 48 | // Client reads complete, validated documents through the generated protobuf RPC. |
| 49 | // Callers own request cancellation and deadlines. |
| 50 | type Client struct { |
| 51 | service protocol.ConfigService |
| 52 | } |
| 53 | |
| 54 | func NewClient(baseURL string, httpClient *http.Client) *Client { |
| 55 | return &Client{service: protocol.NewConfigServiceProtobufClient(baseURL, boundedClient(httpClient, maxGetResponseBytes))} |
| 56 | } |
| 57 | |
| 58 | func (c *Client) Get(ctx context.Context, name string) (Record, error) { |
| 59 | response, err := c.service.Get(ctx, &protocol.ConfigGetRequest{Name: name}) |
| 60 | if err != nil { |
| 61 | return Record{}, err |
| 62 | } |
| 63 | version, err := decodeVersion(response.GetVersion()) |
| 64 | if err != nil { |
| 65 | return Record{}, err |
| 66 | } |
| 67 | record := Record{Name: response.GetName(), Version: version, Value: response.GetValue()} |
| 68 | if err := record.Validate(); err != nil { |
| 69 | return Record{}, err |
| 70 | } |
| 71 | if record.Name != name { |
| 72 | return Record{}, fmt.Errorf("%w: requested document %q, received %q", ErrInvalidRecord, name, record.Name) |
| 73 | } |
| 74 | return record, nil |
| 75 | } |
| 76 | |
| 77 | // Bound successful and error responses before generated Twirp decoding reads |
| 78 | // them. The request bound is installed on the receiving HTTP handler. |
| 79 | type boundedRoundTripper struct { |
| 80 | http.RoundTripper |
| 81 | limit int64 |
| 82 | } |
| 83 | |
| 84 | func boundedClient(client *http.Client, limit int64) *http.Client { |
| 85 | copy := *client |
| 86 | transport := copy.Transport |
| 87 | if transport == nil { |
| 88 | transport = http.DefaultTransport |
| 89 | } |
| 90 | // Keep the concrete client visible so Twirp owns its redirect policy. |
| 91 | copy.Transport = boundedRoundTripper{RoundTripper: transport, limit: limit} |
| 92 | return © |
| 93 | } |
| 94 | |
| 95 | func (t boundedRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) { |
| 96 | response, err := t.RoundTripper.RoundTrip(request) |
| 97 | if err == nil && response != nil && response.Body != nil { |
| 98 | response.Body = http.MaxBytesReader(nil, response.Body, t.limit) |
| 99 | } |
| 100 | return response, err |
| 101 | } |