validate.go 7.4 KB

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