main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "log"
  4. "github.com/porter-dev/porter/api/server/shared/config/envloader"
  5. "github.com/porter-dev/porter/cmd/migrate/keyrotate"
  6. "github.com/porter-dev/porter/cmd/migrate/stable_source_config_id_population"
  7. adapter "github.com/porter-dev/porter/internal/adapter"
  8. "github.com/porter-dev/porter/internal/repository/gorm"
  9. lr "github.com/porter-dev/porter/pkg/logger"
  10. "github.com/joeshaw/envdecode"
  11. )
  12. func main() {
  13. logger := lr.NewConsole(true)
  14. logger.Info().Msg("running migrations")
  15. envConf, err := envloader.FromEnv()
  16. if err != nil {
  17. logger.Fatal().Err(err).Msg("could not load env conf")
  18. return
  19. }
  20. db, err := adapter.New(envConf.DBConf)
  21. if err != nil {
  22. logger.Fatal().Err(err).Msg("could not connect to the database")
  23. return
  24. }
  25. err = gorm.AutoMigrate(db, envConf.ServerConf.Debug)
  26. if err != nil {
  27. logger.Fatal().Err(err).Msg("gorm auto-migration failed")
  28. return
  29. }
  30. if err := db.Raw("ALTER TABLE clusters DROP CONSTRAINT IF EXISTS fk_cluster_token_caches").Error; err != nil {
  31. logger.Fatal().Err(err).Msg("failed to drop cluster token cache constraint")
  32. return
  33. }
  34. if err := db.Raw("ALTER TABLE cluster_token_caches DROP CONSTRAINT IF EXISTS fk_clusters_token_cache").Error; err != nil {
  35. logger.Fatal().Err(err).Msg("failed to drop clusters token cache constraint")
  36. return
  37. }
  38. if shouldRotate, oldKeyStr, newKeyStr := shouldKeyRotate(); shouldRotate {
  39. oldKey := [32]byte{}
  40. newKey := [32]byte{}
  41. copy(oldKey[:], []byte(oldKeyStr))
  42. copy(newKey[:], []byte(newKeyStr))
  43. err := keyrotate.Rotate(db, &oldKey, &newKey)
  44. if err != nil {
  45. logger.Fatal().Err(err).Msg("key rotation failed")
  46. }
  47. }
  48. if shouldPopulateStableSourceConfigId() {
  49. stable_source_config_id_population.PopulateStableSourceConfigId(db, logger)
  50. }
  51. if err := InstanceMigrate(db, envConf.DBConf); err != nil {
  52. logger.Fatal().Err(err).Msg("vault migration failed")
  53. }
  54. }
  55. type RotateConf struct {
  56. // we add a dummy field to avoid empty struct issue with envdecode
  57. DummyField string `env:"ASDF,default=asdf"`
  58. OldEncryptionKey string `env:"OLD_ENCRYPTION_KEY"`
  59. NewEncryptionKey string `env:"NEW_ENCRYPTION_KEY"`
  60. }
  61. func shouldKeyRotate() (bool, string, string) {
  62. var c RotateConf
  63. if err := envdecode.StrictDecode(&c); err != nil {
  64. log.Fatalf("Failed to decode migration conf: %s", err)
  65. return false, "", ""
  66. }
  67. return c.OldEncryptionKey != "" && c.NewEncryptionKey != "", c.OldEncryptionKey, c.NewEncryptionKey
  68. }
  69. type StableSourcePopulateConf struct {
  70. // we add a dummy field to avoid empty struct issue with envdecode
  71. DummyField string `env:"ASDF,default=asdf"`
  72. // Simple env variable that will let us know if we should populate the stable_source_config_id column
  73. POPULATE_SOURCE_CONFIG_ID bool `env:"POPULATE_SOURCE_CONFIG_ID"`
  74. }
  75. func shouldPopulateStableSourceConfigId() bool {
  76. var c StableSourcePopulateConf
  77. if err := envdecode.StrictDecode(&c); err != nil {
  78. log.Fatalf("Failed to decode migration conf: %s", err)
  79. return false
  80. }
  81. return c.POPULATE_SOURCE_CONFIG_ID
  82. }