validate.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package porter_app
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/handlers/porter_app/validate"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/telemetry"
  15. )
  16. type ValidatePorterAppRequest struct {
  17. PorterYAMLBase64 string `json:"porter_yaml"`
  18. LatestCommit string `json:"latest_commit"`
  19. }
  20. type ValidatePorterAppHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. authz.KubernetesAgentGetter
  23. }
  24. func NewValidatePorterAppHandler(
  25. config *config.Config,
  26. decoderValidator shared.RequestDecoderValidator,
  27. writer shared.ResultWriter,
  28. ) *ValidatePorterAppHandler {
  29. return &ValidatePorterAppHandler{
  30. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  31. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  32. }
  33. }
  34. func (c *ValidatePorterAppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  35. ctx, span := telemetry.NewSpan(r.Context(), "serve-validate-porter-app")
  36. defer span.End()
  37. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  38. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  39. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "project-id", Value: project.ID})
  40. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID})
  41. // read the request body
  42. request := &ValidatePorterAppRequest{}
  43. if ok := c.DecodeAndValidate(w, r, request); !ok {
  44. err := telemetry.Error(ctx, span, nil, "error decoding request")
  45. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  46. return
  47. }
  48. fmt.Println(request)
  49. porterYamlBase64 := request.PorterYAMLBase64
  50. porterYaml, err := base64.StdEncoding.DecodeString(porterYamlBase64)
  51. if err != nil {
  52. err = telemetry.Error(ctx, span, err, "error decoding porter yaml")
  53. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  54. return
  55. }
  56. // validate the porter yaml
  57. apps, err := validate.ValidatePorterYAML(porterYaml, c.Repo().Revision())
  58. if err != nil {
  59. err = telemetry.Error(ctx, span, err, "error validating porter yaml")
  60. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  61. return
  62. }
  63. c.WriteResult(w, r, apps)
  64. }