apply.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package provision
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/internal/analytics"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/random"
  13. "github.com/porter-dev/porter/provisioner/integrations/provisioner"
  14. "github.com/porter-dev/porter/provisioner/integrations/redis_stream"
  15. "github.com/porter-dev/porter/provisioner/server/config"
  16. "golang.org/x/crypto/bcrypt"
  17. ptypes "github.com/porter-dev/porter/provisioner/types"
  18. )
  19. type ProvisionApplyHandler struct {
  20. Config *config.Config
  21. decoderValidator shared.RequestDecoderValidator
  22. resultWriter shared.ResultWriter
  23. }
  24. func NewProvisionApplyHandler(
  25. config *config.Config,
  26. ) *ProvisionApplyHandler {
  27. return &ProvisionApplyHandler{
  28. Config: config,
  29. decoderValidator: shared.NewDefaultRequestDecoderValidator(config.Logger, config.Alerter),
  30. resultWriter: shared.NewDefaultResultWriter(config.Logger, config.Alerter),
  31. }
  32. }
  33. func (c *ProvisionApplyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  34. // read the project and infra from the attached scope
  35. infra, _ := r.Context().Value(types.InfraScope).(*models.Infra)
  36. req := &ptypes.ApplyBaseRequest{}
  37. if ok := c.decoderValidator.DecodeAndValidate(w, r, req); !ok {
  38. return
  39. }
  40. // create a new operation and write it to the database
  41. operationUID, err := models.GetOperationID()
  42. if err != nil {
  43. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  44. return
  45. }
  46. // parse values to JSON to store in the operation
  47. valuesJSON, err := json.Marshal(req.Values)
  48. if err != nil {
  49. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  50. return
  51. }
  52. operation := &models.Operation{
  53. UID: operationUID,
  54. InfraID: infra.ID,
  55. Type: req.OperationKind,
  56. Status: "starting",
  57. LastApplied: valuesJSON,
  58. TemplateVersion: "v0.1.0",
  59. }
  60. operation, err = c.Config.Repo.Infra().AddOperation(infra, operation)
  61. if err != nil {
  62. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  63. return
  64. }
  65. ceToken, rawToken, err := createCredentialsExchangeToken(c.Config, infra)
  66. if err != nil {
  67. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  68. return
  69. }
  70. // push a first message to the operation stream
  71. err = redis_stream.PushToOperationStream(c.Config.RedisClient, infra, operation, &ptypes.TFResourceState{
  72. Status: "OPERATION_STARTED",
  73. })
  74. if err != nil {
  75. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  76. return
  77. }
  78. // spawn a new provisioning process
  79. err = c.Config.Provisioner.Provision(&provisioner.ProvisionOpts{
  80. Infra: infra,
  81. Operation: operation,
  82. OperationKind: provisioner.Apply,
  83. Kind: req.Kind,
  84. Values: req.Values,
  85. CredentialExchange: &provisioner.ProvisionCredentialExchange{
  86. CredExchangeEndpoint: fmt.Sprintf(
  87. "%s/api/v1/%s/credentials",
  88. c.Config.ProvisionerConf.ProvisionerCredExchangeURL,
  89. models.GetWorkspaceID(infra, operation),
  90. ),
  91. CredExchangeToken: rawToken,
  92. CredExchangeID: ceToken.ID,
  93. },
  94. })
  95. if err != nil {
  96. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  97. return
  98. }
  99. // update the infrastructure as either "updating" or "creating"
  100. if req.OperationKind == "create" || req.OperationKind == "retry_create" {
  101. infra.Status = types.InfraStatus("creating")
  102. } else if req.OperationKind == "update" {
  103. infra.Status = types.InfraStatus("updating")
  104. }
  105. infra, err = c.Config.Repo.Infra().UpdateInfra(infra)
  106. if err != nil {
  107. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  108. return
  109. }
  110. op, err := operation.ToOperationType()
  111. if err != nil {
  112. apierrors.HandleAPIError(c.Config.Logger, c.Config.Alerter, w, r, apierrors.NewErrInternal(err), true)
  113. return
  114. }
  115. // return the operation response type to the server
  116. c.resultWriter.WriteResult(w, r, op)
  117. // if this is a cluster or registry infra type, send to analytics client
  118. switch infra.Kind {
  119. case types.InfraDOKS, types.InfraEKS, types.InfraGKE, types.InfraAKS:
  120. c.Config.AnalyticsClient.Track(analytics.ClusterProvisioningStartTrack(
  121. &analytics.ClusterProvisioningStartTrackOpts{
  122. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(0, infra.ProjectID),
  123. ClusterType: infra.Kind,
  124. InfraID: infra.ID,
  125. },
  126. ))
  127. case types.InfraDOCR, types.InfraECR, types.InfraGCR, types.InfraGAR, types.InfraACR:
  128. c.Config.AnalyticsClient.Track(analytics.RegistryProvisioningStartTrack(
  129. &analytics.RegistryProvisioningStartTrackOpts{
  130. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(0, infra.ProjectID),
  131. RegistryType: infra.Kind,
  132. InfraID: infra.ID,
  133. },
  134. ))
  135. }
  136. }
  137. func createCredentialsExchangeToken(conf *config.Config, infra *models.Infra) (*models.CredentialsExchangeToken, string, error) {
  138. // convert the form to a project model
  139. expiry := time.Now().Add(6 * time.Hour)
  140. rawToken, err := random.StringWithCharset(32, "")
  141. if err != nil {
  142. return nil, "", err
  143. }
  144. hashedToken, err := bcrypt.GenerateFromPassword([]byte(rawToken), 8)
  145. if err != nil {
  146. return nil, "", err
  147. }
  148. ceToken := &models.CredentialsExchangeToken{
  149. ProjectID: infra.ProjectID,
  150. Expiry: &expiry,
  151. Token: hashedToken,
  152. DOCredentialID: infra.DOIntegrationID,
  153. AWSCredentialID: infra.AWSIntegrationID,
  154. GCPCredentialID: infra.GCPIntegrationID,
  155. AzureCredentialID: infra.AzureIntegrationID,
  156. }
  157. // handle write to the database
  158. ceToken, err = conf.Repo.CredentialsExchangeToken().CreateCredentialsExchangeToken(ceToken)
  159. if err != nil {
  160. return nil, "", err
  161. }
  162. return ceToken, rawToken, nil
  163. }