validate.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package porter_app
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "connectrpc.com/connect"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/api-contracts/generated/go/helpers"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. "github.com/porter-dev/porter/api/server/shared"
  11. "github.com/porter-dev/porter/api/server/shared/apierrors"
  12. "github.com/porter-dev/porter/api/server/shared/config"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/models"
  15. )
  16. // ValidatePorterAppHandler is handles requests to the /apps/validate endpoint
  17. type ValidatePorterAppHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. // NewValidatePorterAppHandler returns a new ValidatePorterAppHandler
  21. func NewValidatePorterAppHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *ValidatePorterAppHandler {
  26. return &ValidatePorterAppHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. }
  29. }
  30. // ValidatePorterAppRequest is the request object for the /apps/validate endpoint
  31. type ValidatePorterAppRequest struct {
  32. Base64AppProto string `json:"b64_app_proto"`
  33. DeploymentTargetId string `json:"deployment_target_id"`
  34. CommitSHA string `json:"commit_sha"`
  35. }
  36. // ValidatePorterAppResponse is the response object for the /apps/validate endpoint
  37. type ValidatePorterAppResponse struct {
  38. ValidatedBase64AppProto string `json:"validate_b64_app_proto"`
  39. }
  40. // ServeHTTP translates requests into protobuf objects and forwards them to the cluster control plane, returning the result
  41. func (c *ValidatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  42. ctx, span := telemetry.NewSpan(r.Context(), "serve-validate-porter-app")
  43. defer span.End()
  44. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  45. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  46. telemetry.WithAttributes(span,
  47. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  48. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  49. )
  50. if !project.ValidateApplyV2 {
  51. err := telemetry.Error(ctx, span, nil, "project does not have validate apply v2 enabled")
  52. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  53. return
  54. }
  55. request := &ValidatePorterAppRequest{}
  56. if ok := c.DecodeAndValidate(w, r, request); !ok {
  57. err := telemetry.Error(ctx, span, nil, "error decoding request")
  58. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  59. return
  60. }
  61. if request.Base64AppProto == "" {
  62. err := telemetry.Error(ctx, span, nil, "b64 yaml is empty")
  63. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  64. return
  65. }
  66. decoded, err := base64.StdEncoding.DecodeString(request.Base64AppProto)
  67. if err != nil {
  68. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  69. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  70. return
  71. }
  72. appProto := &porterv1.PorterApp{}
  73. err = helpers.UnmarshalContractObject(decoded, appProto)
  74. if err != nil {
  75. err := telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  76. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  77. return
  78. }
  79. if appProto.Name == "" {
  80. err := telemetry.Error(ctx, span, err, "app proto name is empty")
  81. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  82. return
  83. }
  84. telemetry.WithAttributes(span,
  85. telemetry.AttributeKV{Key: "app-name", Value: appProto.Name},
  86. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetId},
  87. telemetry.AttributeKV{Key: "commit-sha", Value: request.CommitSHA},
  88. )
  89. validateReq := connect.NewRequest(&porterv1.ValidatePorterAppRequest{
  90. ProjectId: int64(project.ID),
  91. DeploymentTargetId: request.DeploymentTargetId,
  92. CommitSha: request.CommitSHA,
  93. App: appProto,
  94. })
  95. ccpResp, err := c.Config().ClusterControlPlaneClient.ValidatePorterApp(ctx, validateReq)
  96. if err != nil {
  97. err := telemetry.Error(ctx, span, err, "error calling ccp validate porter app")
  98. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  99. return
  100. }
  101. if ccpResp == nil {
  102. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  103. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  104. return
  105. }
  106. if ccpResp.Msg == nil {
  107. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  108. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  109. return
  110. }
  111. if ccpResp.Msg.App == nil {
  112. err := telemetry.Error(ctx, span, err, "ccp resp app is nil")
  113. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  114. return
  115. }
  116. encoded, err := helpers.MarshalContractObject(ctx, ccpResp.Msg.App)
  117. if err != nil {
  118. err := telemetry.Error(ctx, span, err, "error marshalling app proto back to json")
  119. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  120. return
  121. }
  122. b64 := base64.StdEncoding.EncodeToString(encoded)
  123. response := &ValidatePorterAppResponse{
  124. ValidatedBase64AppProto: b64,
  125. }
  126. c.WriteResult(w, r, response)
  127. }