|
| 1 | +// Copyright 2026 Edgeless Systems GmbH |
| 2 | +// SPDX-License-Identifier: BUSL-1.1 |
| 3 | + |
| 4 | +package httpapi |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "errors" |
| 10 | + "io" |
| 11 | + "mime" |
| 12 | + "net/http" |
| 13 | + |
| 14 | + "github.com/edgelesssys/contrast/apitypes" |
| 15 | + "github.com/edgelesssys/contrast/internal/constants" |
| 16 | + "github.com/edgelesssys/contrast/internal/userapi" |
| 17 | + "google.golang.org/grpc/codes" |
| 18 | + "google.golang.org/grpc/status" |
| 19 | +) |
| 20 | + |
| 21 | +// maxSetManifestBodySize limits the accepted request body size. A manifest and its policies |
| 22 | +// are much smaller than this, but they're the largest input the Coordinator accepts. |
| 23 | +const maxSetManifestBodySize = 16 << 20 // 16 MiB |
| 24 | + |
| 25 | +// ManifestSetter sets a manifest. It is a *userapi.Server at runtime, but can be stubbed in tests. |
| 26 | +type ManifestSetter interface { |
| 27 | + SetManifest(context.Context, *userapi.SetManifestRequest) (*userapi.SetManifestResponse, error) |
| 28 | +} |
| 29 | + |
| 30 | +// SetManifestHandler handles POST requests to /v1/manifest. |
| 31 | +// |
| 32 | +// It's a thin translation layer in front of the gRPC UserAPI's SetManifest: the request is |
| 33 | +// converted to its protobuf equivalent, handled by the same server logic, and the response is |
| 34 | +// converted back to JSON. |
| 35 | +type SetManifestHandler struct { |
| 36 | + UserAPI ManifestSetter |
| 37 | +} |
| 38 | + |
| 39 | +// ServeHTTP implements [http.Handler]. |
| 40 | +func (h *SetManifestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 41 | + if r.Method != http.MethodPost { |
| 42 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + contentType := r.Header.Get("Content-Type") |
| 47 | + mediaType, _, err := mime.ParseMediaType(contentType) |
| 48 | + if err != nil { |
| 49 | + writeJSONError(w, http.StatusBadRequest, err) |
| 50 | + return |
| 51 | + } |
| 52 | + if mediaType != "application/json" { |
| 53 | + writeJSONError(w, http.StatusUnsupportedMediaType, errContentType) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + bodyReader := http.MaxBytesReader(w, r.Body, maxSetManifestBodySize) |
| 58 | + defer bodyReader.Close() |
| 59 | + body, err := io.ReadAll(bodyReader) |
| 60 | + if err != nil { |
| 61 | + var maxBytesErr *http.MaxBytesError |
| 62 | + if errors.As(err, &maxBytesErr) { |
| 63 | + writeJSONError(w, http.StatusRequestEntityTooLarge, maxBytesErr) |
| 64 | + return |
| 65 | + } |
| 66 | + writeJSONError(w, http.StatusBadRequest, err) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + var req apitypes.SetManifestRequest |
| 71 | + if err := json.Unmarshal(body, &req); err != nil { |
| 72 | + writeJSONError(w, http.StatusBadRequest, err) |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + resp, err := h.UserAPI.SetManifest(r.Context(), &userapi.SetManifestRequest{ |
| 77 | + Manifest: req.Manifest, |
| 78 | + Policies: req.Policies, |
| 79 | + PreviousTransitionHash: req.PreviousTransitionHash, |
| 80 | + Signature: req.Signature, |
| 81 | + }) |
| 82 | + if err != nil { |
| 83 | + writeJSONError(w, httpStatusFromGRPC(err), err) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + w.Header().Set("Content-Type", "application/json") |
| 88 | + enc := json.NewEncoder(w) |
| 89 | + enc.SetEscapeHTML(false) |
| 90 | + if err := enc.Encode(setManifestResponse(resp)); err != nil { |
| 91 | + writeJSONError(w, http.StatusInternalServerError, err) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// setManifestResponse converts the gRPC response to its wire-format equivalent. |
| 96 | +func setManifestResponse(resp *userapi.SetManifestResponse) *apitypes.SetManifestResponse { |
| 97 | + out := &apitypes.SetManifestResponse{ |
| 98 | + Version: constants.Version, |
| 99 | + RootCA: resp.GetRootCA(), |
| 100 | + MeshCA: resp.GetMeshCA(), |
| 101 | + } |
| 102 | + doc := resp.GetSeedSharesDoc() |
| 103 | + if doc == nil { |
| 104 | + return out |
| 105 | + } |
| 106 | + |
| 107 | + shares := make([]apitypes.SeedShare, 0, len(doc.GetSeedShares())) |
| 108 | + for _, share := range doc.GetSeedShares() { |
| 109 | + shares = append(shares, apitypes.SeedShare{ |
| 110 | + PublicKey: share.GetPublicKey(), |
| 111 | + EncryptedSeed: share.GetEncryptedSeed(), |
| 112 | + }) |
| 113 | + } |
| 114 | + out.SeedSharesDoc = &apitypes.SeedShareDocument{ |
| 115 | + SeedShares: shares, |
| 116 | + Salt: doc.GetSalt(), |
| 117 | + } |
| 118 | + return out |
| 119 | +} |
| 120 | + |
| 121 | +// httpStatusFromGRPC maps the gRPC status codes returned by the UserAPI to HTTP status codes. |
| 122 | +func httpStatusFromGRPC(err error) int { |
| 123 | + switch status.Code(err) { |
| 124 | + case codes.InvalidArgument: |
| 125 | + return http.StatusBadRequest |
| 126 | + case codes.PermissionDenied: |
| 127 | + return http.StatusForbidden |
| 128 | + case codes.FailedPrecondition: |
| 129 | + return http.StatusPreconditionFailed |
| 130 | + default: |
| 131 | + return http.StatusInternalServerError |
| 132 | + } |
| 133 | +} |
0 commit comments