validate.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package porter_app
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "github.com/bufbuild/connect-go"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/porter/api/server/authz"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/handlers/porter_app/conversion"
  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. "github.com/porter-dev/porter/internal/telemetry"
  16. )
  17. type ValidatePorterAppRequest struct {
  18. DeploymentTargetID string `json:"deployment_target_id"`
  19. AppName string `json:"app_name"`
  20. PorterYAMLBase64 string `json:"porter_yaml"`
  21. LatestCommit string `json:"latest_commit"`
  22. }
  23. type ValidatePorterAppHandler struct {
  24. handlers.PorterHandlerReadWriter
  25. authz.KubernetesAgentGetter
  26. }
  27. func NewValidatePorterAppHandler(
  28. config *config.Config,
  29. decoderValidator shared.RequestDecoderValidator,
  30. writer shared.ResultWriter,
  31. ) *ValidatePorterAppHandler {
  32. return &ValidatePorterAppHandler{
  33. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  34. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  35. }
  36. }
  37. func (c *ValidatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  38. ctx, span := telemetry.NewSpan(r.Context(), "serve-validate-porter-app")
  39. defer span.End()
  40. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  41. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  42. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "project-id", Value: project.ID})
  43. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID})
  44. // read the request body
  45. request := &ValidatePorterAppRequest{}
  46. if ok := c.DecodeAndValidate(w, r, request); !ok {
  47. err := telemetry.Error(ctx, span, nil, "error decoding request")
  48. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  49. return
  50. }
  51. porterYamlBase64 := request.PorterYAMLBase64
  52. porterYaml, err := base64.StdEncoding.DecodeString(porterYamlBase64)
  53. if err != nil {
  54. err = telemetry.Error(ctx, span, err, "error decoding porter yaml")
  55. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  56. return
  57. }
  58. // validate the porter yaml
  59. apps, err := conversion.AppProtoFromYaml(porterYaml)
  60. if err != nil {
  61. err = telemetry.Error(ctx, span, err, "error extracting apps from porter yaml")
  62. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  63. return
  64. }
  65. target, ok := apps[request.AppName]
  66. if !ok {
  67. err = telemetry.Error(ctx, span, err, "app name not found in porter yaml")
  68. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  69. return
  70. }
  71. // validate the app
  72. validateRequest := connect.NewRequest(&porterv1.ValidatePorterAppRequest{
  73. ProjectId: int64(project.ID),
  74. DeploymentTargetId: request.DeploymentTargetID,
  75. Application: target,
  76. })
  77. validation, err := c.Config().ClusterControlPlaneClient.ValidatePorterApp(ctx, validateRequest)
  78. if err != nil {
  79. e := telemetry.Error(ctx, span, err, "error sending contract for update")
  80. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusInternalServerError))
  81. return
  82. }
  83. w.WriteHeader(http.StatusOK)
  84. c.WriteResult(w, r, validation.Msg)
  85. }