api.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/go-playground/locales/en"
  5. ut "github.com/go-playground/universal-translator"
  6. vr "github.com/go-playground/validator/v10"
  7. "github.com/porter-dev/porter/internal/auth/sessionstore"
  8. "github.com/porter-dev/porter/internal/oauth"
  9. "golang.org/x/oauth2"
  10. "gorm.io/gorm"
  11. "github.com/gorilla/sessions"
  12. "github.com/porter-dev/porter/internal/helm"
  13. "github.com/porter-dev/porter/internal/kubernetes"
  14. lr "github.com/porter-dev/porter/internal/logger"
  15. "github.com/porter-dev/porter/internal/repository"
  16. memory "github.com/porter-dev/porter/internal/repository/memory"
  17. "github.com/porter-dev/porter/internal/validator"
  18. "helm.sh/helm/v3/pkg/storage"
  19. "github.com/porter-dev/porter/internal/config"
  20. )
  21. // TestAgents are the k8s agents used for testing
  22. type TestAgents struct {
  23. HelmAgent *helm.Agent
  24. HelmTestStorageDriver *storage.Storage
  25. K8sAgent *kubernetes.Agent
  26. }
  27. // AppConfig is the configuration required for creating a new App
  28. type AppConfig struct {
  29. DB *gorm.DB
  30. Logger *lr.Logger
  31. Repository *repository.Repository
  32. ServerConf config.ServerConf
  33. RedisConf *config.RedisConf
  34. DBConf config.DBConf
  35. // TestAgents if API is in testing mode
  36. TestAgents *TestAgents
  37. }
  38. // App represents an API instance with handler methods attached, a DB connection
  39. // and a logger instance
  40. type App struct {
  41. // Server configuration
  42. ServerConf config.ServerConf
  43. // Logger for logging
  44. Logger *lr.Logger
  45. // Repo implements a query repository
  46. Repo *repository.Repository
  47. // session store for cookie-based sessions
  48. Store sessions.Store
  49. // agents exposed for testing
  50. TestAgents *TestAgents
  51. // redis client for redis connection
  52. RedisConf *config.RedisConf
  53. // config for db
  54. DBConf config.DBConf
  55. // oauth-specific clients
  56. GithubUserConf *oauth2.Config
  57. GithubProjectConf *oauth2.Config
  58. DOConf *oauth2.Config
  59. db *gorm.DB
  60. validator *vr.Validate
  61. translator *ut.Translator
  62. }
  63. // New returns a new App instance
  64. func New(conf *AppConfig) (*App, error) {
  65. // create a new validator and translator
  66. validator := validator.New()
  67. en := en.New()
  68. uni := ut.New(en, en)
  69. translator, found := uni.GetTranslator("en")
  70. if !found {
  71. return nil, fmt.Errorf("could not find \"en\" translator")
  72. }
  73. app := &App{
  74. Logger: conf.Logger,
  75. Repo: conf.Repository,
  76. ServerConf: conf.ServerConf,
  77. RedisConf: conf.RedisConf,
  78. DBConf: conf.DBConf,
  79. TestAgents: conf.TestAgents,
  80. db: conf.DB,
  81. validator: validator,
  82. translator: &translator,
  83. }
  84. // if repository not specified, default to in-memory
  85. if app.Repo == nil {
  86. app.Repo = memory.NewRepository(true)
  87. }
  88. // create the session store
  89. store, err := sessionstore.NewStore(app.Repo, app.ServerConf)
  90. if err != nil {
  91. return nil, err
  92. }
  93. app.Store = store
  94. // if server config contains OAuth client info, create clients
  95. if sc := conf.ServerConf; sc.GithubClientID != "" && sc.GithubClientSecret != "" {
  96. app.GithubUserConf = oauth.NewGithubClient(&oauth.Config{
  97. ClientID: sc.GithubClientID,
  98. ClientSecret: sc.GithubClientSecret,
  99. Scopes: []string{"read:user", "user:email"},
  100. BaseURL: sc.ServerURL,
  101. })
  102. app.GithubProjectConf = oauth.NewGithubClient(&oauth.Config{
  103. ClientID: sc.GithubClientID,
  104. ClientSecret: sc.GithubClientSecret,
  105. Scopes: []string{"repo", "read:user", "workflow"},
  106. BaseURL: sc.ServerURL,
  107. })
  108. }
  109. if sc := conf.ServerConf; sc.DOClientID != "" && sc.DOClientSecret != "" {
  110. app.DOConf = oauth.NewDigitalOceanClient(&oauth.Config{
  111. ClientID: sc.DOClientID,
  112. ClientSecret: sc.DOClientSecret,
  113. Scopes: []string{"read", "write"},
  114. BaseURL: sc.ServerURL,
  115. })
  116. }
  117. return app, nil
  118. }