api.go 4.2 KB

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