overwrite_aws.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package project_integration
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. type OverwriteAWSHandler struct {
  12. handlers.PorterHandlerReadWriter
  13. }
  14. func NewOverwriteAWSHandler(
  15. config *config.Config,
  16. decoderValidator shared.RequestDecoderValidator,
  17. writer shared.ResultWriter,
  18. ) *OverwriteAWSHandler {
  19. return &OverwriteAWSHandler{
  20. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  21. }
  22. }
  23. func (p *OverwriteAWSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  24. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  25. request := &types.OverwriteAWSRequest{}
  26. if ok := p.DecodeAndValidate(w, r, request); !ok {
  27. return
  28. }
  29. // read the aws integration by ID and overwrite the access id/secret
  30. awsIntegration, err := p.Repo().AWSIntegration().ReadAWSIntegration(project.ID, request.AWSIntegrationID)
  31. if err != nil {
  32. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  33. return
  34. }
  35. awsIntegration.AWSAccessKeyID = []byte(request.AWSAccessKeyID)
  36. awsIntegration.AWSSecretAccessKey = []byte(request.AWSSecretAccessKey)
  37. // handle write to the database
  38. awsIntegration, err = p.Repo().AWSIntegration().OverwriteAWSIntegration(awsIntegration)
  39. if err != nil {
  40. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  41. return
  42. }
  43. if err != nil {
  44. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  45. return
  46. }
  47. if request.ClusterID > 0 {
  48. cluster, err := p.Repo().Cluster().ReadCluster(project.ID, request.ClusterID)
  49. // clear the token
  50. cluster.TokenCache.Token = []byte("")
  51. cluster, err = p.Repo().Cluster().UpdateClusterTokenCache(&cluster.TokenCache)
  52. if err != nil {
  53. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  54. return
  55. }
  56. }
  57. // app.Logger.Info().Msgf("AWS integration overwritten: %d", awsIntegration.ID)
  58. res := types.OverwriteAWSResponse{
  59. AWSIntegration: awsIntegration.ToAWSIntegrationType(),
  60. }
  61. p.WriteResult(w, r, res)
  62. }