main.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/porter-dev/porter/cmd/migrate/keyrotate"
  6. adapter "github.com/porter-dev/porter/internal/adapter"
  7. "github.com/porter-dev/porter/internal/config"
  8. lr "github.com/porter-dev/porter/internal/logger"
  9. "github.com/porter-dev/porter/internal/models"
  10. ints "github.com/porter-dev/porter/internal/models/integrations"
  11. "github.com/joeshaw/envdecode"
  12. )
  13. func main() {
  14. fmt.Println("running migrations...")
  15. appConf := config.FromEnv()
  16. logger := lr.NewConsole(true)
  17. db, err := adapter.New(&appConf.Db)
  18. if err != nil {
  19. logger.Fatal().Err(err).Msg("")
  20. return
  21. }
  22. err = db.AutoMigrate(
  23. &models.Project{},
  24. &models.Role{},
  25. &models.User{},
  26. &models.Release{},
  27. &models.Session{},
  28. &models.GitRepo{},
  29. &models.Registry{},
  30. &models.HelmRepo{},
  31. &models.Cluster{},
  32. &models.ClusterCandidate{},
  33. &models.ClusterResolver{},
  34. &models.Infra{},
  35. &models.GitActionConfig{},
  36. &models.Invite{},
  37. &models.AuthCode{},
  38. &models.DNSRecord{},
  39. &models.PWResetToken{},
  40. &ints.KubeIntegration{},
  41. &ints.BasicIntegration{},
  42. &ints.OIDCIntegration{},
  43. &ints.OAuthIntegration{},
  44. &ints.GCPIntegration{},
  45. &ints.AWSIntegration{},
  46. &ints.TokenCache{},
  47. &ints.ClusterTokenCache{},
  48. &ints.RegTokenCache{},
  49. &ints.HelmRepoTokenCache{},
  50. &ints.GithubAppInstallation{},
  51. &ints.GithubAppOAuthIntegration{},
  52. )
  53. if err != nil {
  54. panic(err)
  55. }
  56. if shouldRotate, oldKeyStr, newKeyStr := shouldKeyRotate(); shouldRotate {
  57. oldKey := [32]byte{}
  58. newKey := [32]byte{}
  59. copy(oldKey[:], []byte(oldKeyStr))
  60. copy(newKey[:], []byte(newKeyStr))
  61. err := keyrotate.Rotate(db, &oldKey, &newKey)
  62. if err != nil {
  63. panic(err)
  64. }
  65. }
  66. }
  67. type RotateConf struct {
  68. // we add a dummy field to avoid empty struct issue with envdecode
  69. DummyField string `env:"ASDF,default=asdf"`
  70. OldEncryptionKey string `env:"OLD_ENCRYPTION_KEY"`
  71. NewEncryptionKey string `env:"NEW_ENCRYPTION_KEY"`
  72. }
  73. func shouldKeyRotate() (bool, string, string) {
  74. var c RotateConf
  75. if err := envdecode.StrictDecode(&c); err != nil {
  76. log.Fatalf("Failed to decode migration conf: %s", err)
  77. return false, "", ""
  78. }
  79. return c.OldEncryptionKey != "" && c.NewEncryptionKey != "", c.OldEncryptionKey, c.NewEncryptionKey
  80. }