validate.go 5.9 KB

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