validate.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // Deletions are the names of services and env variables to delete
  31. type Deletions struct {
  32. ServiceNames []string `json:"service_names"`
  33. EnvVariableNames []string `json:"env_variable_names"`
  34. }
  35. // ValidatePorterAppRequest is the request object for the /apps/validate endpoint
  36. type ValidatePorterAppRequest struct {
  37. Base64AppProto string `json:"b64_app_proto"`
  38. DeploymentTargetId string `json:"deployment_target_id"`
  39. CommitSHA string `json:"commit_sha"`
  40. Deletions Deletions `json:"deletions"`
  41. }
  42. // ValidatePorterAppResponse is the response object for the /apps/validate endpoint
  43. type ValidatePorterAppResponse struct {
  44. ValidatedBase64AppProto string `json:"validate_b64_app_proto"`
  45. }
  46. // ServeHTTP translates requests into protobuf objects and forwards them to the cluster control plane, returning the result
  47. func (c *ValidatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  48. ctx, span := telemetry.NewSpan(r.Context(), "serve-validate-porter-app")
  49. defer span.End()
  50. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  51. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  52. telemetry.WithAttributes(span,
  53. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  54. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  55. )
  56. if !project.ValidateApplyV2 {
  57. err := telemetry.Error(ctx, span, nil, "project does not have validate apply v2 enabled")
  58. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  59. return
  60. }
  61. request := &ValidatePorterAppRequest{}
  62. if ok := c.DecodeAndValidate(w, r, request); !ok {
  63. err := telemetry.Error(ctx, span, nil, "error decoding request")
  64. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  65. return
  66. }
  67. if request.Base64AppProto == "" {
  68. err := telemetry.Error(ctx, span, nil, "b64 yaml is empty")
  69. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  70. return
  71. }
  72. decoded, err := base64.StdEncoding.DecodeString(request.Base64AppProto)
  73. if err != nil {
  74. err := telemetry.Error(ctx, span, err, "error decoding base yaml")
  75. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  76. return
  77. }
  78. appProto := &porterv1.PorterApp{}
  79. err = helpers.UnmarshalContractObject(decoded, appProto)
  80. if err != nil {
  81. err := telemetry.Error(ctx, span, err, "error unmarshalling app proto")
  82. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  83. return
  84. }
  85. if appProto.Name == "" {
  86. err := telemetry.Error(ctx, span, err, "app proto name is empty")
  87. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  88. return
  89. }
  90. telemetry.WithAttributes(span,
  91. telemetry.AttributeKV{Key: "app-name", Value: appProto.Name},
  92. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetId},
  93. telemetry.AttributeKV{Key: "commit-sha", Value: request.CommitSHA},
  94. )
  95. validateReq := connect.NewRequest(&porterv1.ValidatePorterAppRequest{
  96. ProjectId: int64(project.ID),
  97. DeploymentTargetId: request.DeploymentTargetId,
  98. CommitSha: request.CommitSHA,
  99. App: appProto,
  100. Deletions: &porterv1.Deletions{
  101. ServiceNames: request.Deletions.ServiceNames,
  102. EnvVariableNames: request.Deletions.EnvVariableNames,
  103. },
  104. })
  105. ccpResp, err := c.Config().ClusterControlPlaneClient.ValidatePorterApp(ctx, validateReq)
  106. if err != nil {
  107. err := telemetry.Error(ctx, span, err, "error calling ccp validate porter app")
  108. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  109. return
  110. }
  111. if ccpResp == nil {
  112. err := telemetry.Error(ctx, span, err, "ccp resp is nil")
  113. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  114. return
  115. }
  116. if ccpResp.Msg == nil {
  117. err := telemetry.Error(ctx, span, err, "ccp resp msg is nil")
  118. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  119. return
  120. }
  121. if ccpResp.Msg.App == nil {
  122. err := telemetry.Error(ctx, span, err, "ccp resp app is nil")
  123. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  124. return
  125. }
  126. encoded, err := helpers.MarshalContractObject(ctx, ccpResp.Msg.App)
  127. if err != nil {
  128. err := telemetry.Error(ctx, span, err, "error marshalling app proto back to json")
  129. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  130. return
  131. }
  132. b64 := base64.StdEncoding.EncodeToString(encoded)
  133. response := &ValidatePorterAppResponse{
  134. ValidatedBase64AppProto: b64,
  135. }
  136. c.WriteResult(w, r, response)
  137. }