|
| 1 | +// Copyright 2025 The Sigstore Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package server |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "errors" |
| 20 | + "os" |
| 21 | + "strconv" |
| 22 | + |
| 23 | + pb "github.com/sigstore/rekor-tiles/pkg/generated/protobuf" |
| 24 | + "github.com/sigstore/rekor-tiles/pkg/tessera" |
| 25 | + "github.com/sigstore/sigstore/pkg/signature" |
| 26 | + "github.com/transparency-dev/trillian-tessera/api/layout" |
| 27 | + "google.golang.org/genproto/googleapis/api/httpbody" |
| 28 | + "google.golang.org/grpc" |
| 29 | + "google.golang.org/grpc/codes" |
| 30 | + "google.golang.org/grpc/metadata" |
| 31 | + "google.golang.org/grpc/status" |
| 32 | + "google.golang.org/protobuf/types/known/emptypb" |
| 33 | +) |
| 34 | + |
| 35 | +// ReadServer implements the read APIs along with the write APIs |
| 36 | +type ReadServer struct { |
| 37 | + Server |
| 38 | +} |
| 39 | + |
| 40 | +func NewReadServer(storage tessera.Storage, readOnly bool, algorithmRegistry *signature.AlgorithmRegistryConfig) *ReadServer { |
| 41 | + if readOnly { |
| 42 | + return &ReadServer{ |
| 43 | + Server: Server{ |
| 44 | + readOnly: readOnly, |
| 45 | + storage: storage, |
| 46 | + }, |
| 47 | + } |
| 48 | + } |
| 49 | + return &ReadServer{ |
| 50 | + Server: Server{ |
| 51 | + storage: storage, |
| 52 | + algorithmRegistry: algorithmRegistry, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func (s *ReadServer) GetTile(ctx context.Context, req *pb.TileRequest) (*httpbody.HttpBody, error) { |
| 58 | + // Verifies and parses level, index, and optional width for partial tile |
| 59 | + l, i, w, err := layout.ParseTileLevelIndexPartial(strconv.FormatUint(uint64(req.L), 10), req.N) |
| 60 | + if err != nil { |
| 61 | + return nil, status.Error(codes.InvalidArgument, "invalid level, index and optional width") |
| 62 | + } |
| 63 | + tile, err := s.storage.ReadTile(ctx, l, i, w) |
| 64 | + if err != nil { |
| 65 | + return nil, status.Error(codes.Unknown, "failed to read tile") |
| 66 | + } |
| 67 | + _ = grpc.SetHeader(ctx, metadata.Pairs(httpCacheControlHeader, "max-age=31536000, immutable")) |
| 68 | + return &httpbody.HttpBody{ |
| 69 | + ContentType: "application/octet-stream", |
| 70 | + Data: tile, |
| 71 | + }, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (s *ReadServer) GetEntryBundle(ctx context.Context, req *pb.EntryBundleRequest) (*httpbody.HttpBody, error) { |
| 75 | + // Parses index and optional width for partial tile |
| 76 | + i, w, err := layout.ParseTileIndexPartial(req.N) |
| 77 | + if err != nil { |
| 78 | + return nil, status.Error(codes.InvalidArgument, "invalid index and optional width") |
| 79 | + } |
| 80 | + entryBundle, err := s.storage.ReadEntryBundle(ctx, i, w) |
| 81 | + if err != nil { |
| 82 | + return nil, status.Error(codes.Unknown, "failed to read entry bundle") |
| 83 | + } |
| 84 | + _ = grpc.SetHeader(ctx, metadata.Pairs(httpCacheControlHeader, "max-age=31536000, immutable")) |
| 85 | + return &httpbody.HttpBody{ |
| 86 | + ContentType: "application/octet-stream", |
| 87 | + Data: entryBundle, |
| 88 | + }, nil |
| 89 | +} |
| 90 | + |
| 91 | +func (s *ReadServer) GetCheckpoint(ctx context.Context, _ *emptypb.Empty) (*httpbody.HttpBody, error) { |
| 92 | + checkpoint, err := s.storage.ReadCheckpoint(ctx) |
| 93 | + if err != nil { |
| 94 | + if errors.Is(err, os.ErrNotExist) { |
| 95 | + return nil, status.Error(codes.NotFound, "checkpoint does not exist") |
| 96 | + } |
| 97 | + return nil, status.Error(codes.Unknown, "failed to read checkpoint") |
| 98 | + } |
| 99 | + _ = grpc.SetHeader(ctx, metadata.Pairs(httpCacheControlHeader, "no-cache")) |
| 100 | + return &httpbody.HttpBody{ |
| 101 | + ContentType: "text/plain; charset=utf-8", |
| 102 | + Data: checkpoint, |
| 103 | + }, nil |
| 104 | +} |
0 commit comments