|
| 1 | +package cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "connectrpc.com/connect" |
| 7 | + porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1" |
| 8 | + "github.com/porter-dev/porter/api/server/handlers" |
| 9 | + "github.com/porter-dev/porter/api/server/shared" |
| 10 | + "github.com/porter-dev/porter/api/server/shared/apierrors" |
| 11 | + "github.com/porter-dev/porter/api/server/shared/config" |
| 12 | + "github.com/porter-dev/porter/api/types" |
| 13 | + "github.com/porter-dev/porter/internal/compliance" |
| 14 | + "github.com/porter-dev/porter/internal/models" |
| 15 | + "github.com/porter-dev/porter/internal/telemetry" |
| 16 | +) |
| 17 | + |
| 18 | +// ListComplianceChecksHandler is the handler for /compliance/checks |
| 19 | +type ListComplianceChecksHandler struct { |
| 20 | + handlers.PorterHandlerReadWriter |
| 21 | +} |
| 22 | + |
| 23 | +// NewListComplianceChecksHandler returns a new ListComplianceChecksHandler |
| 24 | +func NewListComplianceChecksHandler( |
| 25 | + config *config.Config, |
| 26 | + decoderValidator shared.RequestDecoderValidator, |
| 27 | + writer shared.ResultWriter, |
| 28 | +) *ListComplianceChecksHandler { |
| 29 | + return &ListComplianceChecksHandler{ |
| 30 | + PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// ListComplianceChecksRequest is the expected format for a request to /compliance/checks |
| 35 | +type ListComplianceChecksRequest struct { |
| 36 | + Vendor compliance.Vendor `schema:"vendor"` |
| 37 | +} |
| 38 | + |
| 39 | +// ListComplianceChecksResponse is the expected format for a response from /compliance/checks |
| 40 | +type ListComplianceChecksResponse struct { |
| 41 | + CheckGroups []compliance.CheckGroup `json:"check_groups,omitempty"` |
| 42 | + VendorChecks []compliance.VendorComplianceCheck `json:"vendor_checks,omitempty"` |
| 43 | +} |
| 44 | + |
| 45 | +// ServeHTTP retrieves the evaluated compliance checks for a cluster |
| 46 | +func (c *ListComplianceChecksHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 47 | + ctx, span := telemetry.NewSpan(r.Context(), "serve-compliance-checks") |
| 48 | + defer span.End() |
| 49 | + |
| 50 | + project, _ := ctx.Value(types.ProjectScope).(*models.Project) |
| 51 | + cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster) |
| 52 | + |
| 53 | + request := &ListComplianceChecksRequest{} |
| 54 | + if ok := c.DecodeAndValidate(w, r, request); !ok { |
| 55 | + err := telemetry.Error(ctx, span, nil, "error decoding request") |
| 56 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + var vendor porterv1.EnumComplianceVendor |
| 61 | + if request.Vendor != "" { |
| 62 | + switch request.Vendor { |
| 63 | + case compliance.Vendor_Vanta: |
| 64 | + vendor = porterv1.EnumComplianceVendor_ENUM_COMPLIANCE_VENDOR_VANTA |
| 65 | + default: |
| 66 | + err := telemetry.Error(ctx, span, nil, "invalid vendor") |
| 67 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) |
| 68 | + return |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + req := connect.NewRequest(&porterv1.ContractComplianceChecksRequest{ |
| 73 | + ProjectId: int64(project.ID), |
| 74 | + ClusterId: int64(cluster.ID), |
| 75 | + Vendor: vendor, |
| 76 | + }) |
| 77 | + |
| 78 | + ccpResp, err := c.Config().ClusterControlPlaneClient.ContractComplianceChecks(ctx, req) |
| 79 | + if err != nil { |
| 80 | + err := telemetry.Error(ctx, span, err, "error calling ccp for contract compliance checks") |
| 81 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 82 | + return |
| 83 | + } |
| 84 | + if ccpResp == nil { |
| 85 | + err := telemetry.Error(ctx, span, err, "ccp resp is nil") |
| 86 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 87 | + return |
| 88 | + } |
| 89 | + if ccpResp.Msg == nil { |
| 90 | + err := telemetry.Error(ctx, span, err, "ccp resp msg is nil") |
| 91 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + cgs, err := compliance.CheckGroupsFromProto(ctx, ccpResp.Msg.CheckGroups) |
| 96 | + if err != nil { |
| 97 | + err := telemetry.Error(ctx, span, err, "error converting compliance check groups from proto") |
| 98 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 99 | + return |
| 100 | + } |
| 101 | + telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "num-check-groups", Value: len(cgs)}) |
| 102 | + |
| 103 | + vendorChecks, err := compliance.VendorCheckGroupsFromProto(ctx, ccpResp.Msg.VendorChecks) |
| 104 | + if err != nil { |
| 105 | + err := telemetry.Error(ctx, span, err, "error converting vendor compliance check groups from proto") |
| 106 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) |
| 107 | + return |
| 108 | + } |
| 109 | + telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "num-vendor-checks", Value: len(vendorChecks)}) |
| 110 | + |
| 111 | + resp := &ListComplianceChecksResponse{ |
| 112 | + CheckGroups: cgs, |
| 113 | + VendorChecks: vendorChecks, |
| 114 | + } |
| 115 | + |
| 116 | + c.WriteResult(w, r, resp) |
| 117 | + w.WriteHeader(http.StatusOK) |
| 118 | +} |
0 commit comments