validate.go 6.0 KB

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