api.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "github.com/go-playground/locales/en"
  8. ut "github.com/go-playground/universal-translator"
  9. vr "github.com/go-playground/validator/v10"
  10. "github.com/porter-dev/porter/internal/auth/sessionstore"
  11. "github.com/porter-dev/porter/internal/auth/token"
  12. "github.com/porter-dev/porter/internal/kubernetes/local"
  13. "github.com/porter-dev/porter/internal/notifier/sendgrid"
  14. "github.com/porter-dev/porter/internal/oauth"
  15. "golang.org/x/oauth2"
  16. "gorm.io/gorm"
  17. "github.com/gorilla/sessions"
  18. "github.com/porter-dev/porter/internal/helm"
  19. "github.com/porter-dev/porter/internal/helm/loader"
  20. "github.com/porter-dev/porter/internal/kubernetes"
  21. lr "github.com/porter-dev/porter/internal/logger"
  22. notif "github.com/porter-dev/porter/internal/notifier"
  23. "github.com/porter-dev/porter/internal/repository"
  24. "github.com/porter-dev/porter/internal/validator"
  25. "helm.sh/helm/v3/pkg/storage"
  26. "github.com/porter-dev/porter/internal/analytics"
  27. "github.com/porter-dev/porter/internal/config"
  28. )
  29. // TestAgents are the k8s agents used for testing
  30. type TestAgents struct {
  31. HelmAgent *helm.Agent
  32. HelmTestStorageDriver *storage.Storage
  33. K8sAgent *kubernetes.Agent
  34. }
  35. // AppConfig is the configuration required for creating a new App
  36. type AppConfig struct {
  37. DB *gorm.DB
  38. Logger *lr.Logger
  39. Repository repository.Repository
  40. ServerConf config.ServerConf
  41. RedisConf *config.RedisConf
  42. DBConf config.DBConf
  43. CapConf config.CapConf
  44. // TestAgents if API is in testing mode
  45. TestAgents *TestAgents
  46. }
  47. // App represents an API instance with handler methods attached, a DB connection
  48. // and a logger instance
  49. type App struct {
  50. // Server configuration
  51. ServerConf config.ServerConf
  52. // Logger for logging
  53. Logger *lr.Logger
  54. // Repo implements a query repository
  55. Repo repository.Repository
  56. // session store for cookie-based sessions
  57. Store sessions.Store
  58. // agents exposed for testing
  59. TestAgents *TestAgents
  60. // An in-cluster agent if service is running in cluster
  61. ProvisionerAgent *kubernetes.Agent
  62. IngressAgent *kubernetes.Agent
  63. // redis client for redis connection
  64. RedisConf *config.RedisConf
  65. // config for db
  66. DBConf config.DBConf
  67. // config for capabilities
  68. Capabilities *AppCapabilities
  69. // ChartLookupURLs contains an in-memory store of Porter chart names matched with
  70. // a repo URL, so that finding a chart does not involve multiple lookups to our
  71. // chart repo's index.yaml file
  72. ChartLookupURLs map[string]string
  73. // oauth-specific clients
  74. GithubUserConf *oauth2.Config
  75. GithubProjectConf *oauth2.Config
  76. GithubAppConf *oauth.GithubAppConf
  77. DOConf *oauth2.Config
  78. GoogleUserConf *oauth2.Config
  79. SlackConf *oauth2.Config
  80. db *gorm.DB
  81. validator *vr.Validate
  82. translator *ut.Translator
  83. tokenConf *token.TokenGeneratorConf
  84. analyticsClient analytics.AnalyticsSegmentClient
  85. notifier notif.UserNotifier
  86. }
  87. type AppCapabilities struct {
  88. Provisioning bool `json:"provisioner"`
  89. Github bool `json:"github"`
  90. BasicLogin bool `json:"basic_login"`
  91. GithubLogin bool `json:"github_login"`
  92. GoogleLogin bool `json:"google_login"`
  93. SlackNotifications bool `json:"slack_notifs"`
  94. Email bool `json:"email"`
  95. Analytics bool `json:"analytics"`
  96. }
  97. // New returns a new App instance
  98. func New(conf *AppConfig) (*App, error) {
  99. // create a new validator and translator
  100. validator := validator.New()
  101. en := en.New()
  102. uni := ut.New(en, en)
  103. translator, found := uni.GetTranslator("en")
  104. if !found {
  105. return nil, fmt.Errorf("could not find \"en\" translator")
  106. }
  107. app := &App{
  108. Logger: conf.Logger,
  109. Repo: conf.Repository,
  110. ServerConf: conf.ServerConf,
  111. RedisConf: conf.RedisConf,
  112. DBConf: conf.DBConf,
  113. TestAgents: conf.TestAgents,
  114. Capabilities: &AppCapabilities{},
  115. db: conf.DB,
  116. validator: validator,
  117. translator: &translator,
  118. }
  119. // if repository not specified, default to in-memory
  120. // if app.Repo == nil {
  121. // app.Repo = test.NewRepository(true)
  122. // }
  123. // create the session store
  124. store, err := sessionstore.NewStore(
  125. &sessionstore.NewStoreOpts{
  126. SessionRepository: app.Repo.Session(),
  127. CookieSecrets: app.ServerConf.CookieSecrets,
  128. },
  129. )
  130. if err != nil {
  131. return nil, err
  132. }
  133. app.Store = store
  134. sc := conf.ServerConf
  135. // get the InClusterAgent from either a file-based kubeconfig or the in-cluster agent
  136. app.assignProvisionerAgent(&sc)
  137. app.assignIngressAgent(&sc)
  138. // if server config contains OAuth client info, create clients
  139. if sc.GithubClientID != "" && sc.GithubClientSecret != "" {
  140. app.Capabilities.Github = true
  141. app.GithubUserConf = oauth.NewGithubClient(&oauth.Config{
  142. ClientID: sc.GithubClientID,
  143. ClientSecret: sc.GithubClientSecret,
  144. Scopes: []string{"read:user", "user:email"},
  145. BaseURL: sc.ServerURL,
  146. })
  147. app.GithubProjectConf = oauth.NewGithubClient(&oauth.Config{
  148. ClientID: sc.GithubClientID,
  149. ClientSecret: sc.GithubClientSecret,
  150. Scopes: []string{"repo", "read:user", "workflow"},
  151. BaseURL: sc.ServerURL,
  152. })
  153. app.Capabilities.GithubLogin = sc.GithubLoginEnabled
  154. }
  155. if sc.GithubAppClientID != "" &&
  156. sc.GithubAppClientSecret != "" &&
  157. sc.GithubAppName != "" &&
  158. sc.GithubAppWebhookSecret != "" &&
  159. sc.GithubAppSecretPath != "" &&
  160. sc.GithubAppID != "" {
  161. if AppID, err := strconv.ParseInt(sc.GithubAppID, 10, 64); err == nil {
  162. app.GithubAppConf = oauth.NewGithubAppClient(&oauth.Config{
  163. ClientID: sc.GithubAppClientID,
  164. ClientSecret: sc.GithubAppClientSecret,
  165. Scopes: []string{"read:user"},
  166. BaseURL: sc.ServerURL,
  167. }, sc.GithubAppName, sc.GithubAppWebhookSecret, sc.GithubAppSecretPath, AppID)
  168. }
  169. }
  170. if sc.GoogleClientID != "" && sc.GoogleClientSecret != "" {
  171. app.Capabilities.GoogleLogin = true
  172. app.GoogleUserConf = oauth.NewGoogleClient(&oauth.Config{
  173. ClientID: sc.GoogleClientID,
  174. ClientSecret: sc.GoogleClientSecret,
  175. Scopes: []string{
  176. "openid",
  177. "profile",
  178. "email",
  179. },
  180. BaseURL: sc.ServerURL,
  181. })
  182. }
  183. if sc.SlackClientID != "" && sc.SlackClientSecret != "" {
  184. app.Capabilities.SlackNotifications = true
  185. app.SlackConf = oauth.NewSlackClient(&oauth.Config{
  186. ClientID: sc.SlackClientID,
  187. ClientSecret: sc.SlackClientSecret,
  188. Scopes: []string{
  189. "incoming-webhook",
  190. "team:read",
  191. },
  192. BaseURL: sc.ServerURL,
  193. })
  194. }
  195. if sc.DOClientID != "" && sc.DOClientSecret != "" {
  196. app.DOConf = oauth.NewDigitalOceanClient(&oauth.Config{
  197. ClientID: sc.DOClientID,
  198. ClientSecret: sc.DOClientSecret,
  199. Scopes: []string{"read", "write"},
  200. BaseURL: sc.ServerURL,
  201. })
  202. }
  203. if sc.SendgridAPIKey != "" {
  204. app.Capabilities.Email = true
  205. sgClient := &sendgrid.Client{
  206. APIKey: sc.SendgridAPIKey,
  207. PWResetTemplateID: sc.SendgridPWResetTemplateID,
  208. PWGHTemplateID: sc.SendgridPWGHTemplateID,
  209. VerifyEmailTemplateID: sc.SendgridVerifyEmailTemplateID,
  210. ProjectInviteTemplateID: sc.SendgridProjectInviteTemplateID,
  211. SenderEmail: sc.SendgridSenderEmail,
  212. }
  213. app.notifier = sendgrid.NewUserNotifier(sgClient)
  214. }
  215. app.Capabilities.Analytics = sc.SegmentClientKey != ""
  216. app.Capabilities.BasicLogin = sc.BasicLoginEnabled
  217. app.tokenConf = &token.TokenGeneratorConf{
  218. TokenSecret: conf.ServerConf.TokenGeneratorSecret,
  219. }
  220. newSegmentClient := analytics.InitializeAnalyticsSegmentClient(sc.SegmentClientKey, app.Logger)
  221. app.analyticsClient = newSegmentClient
  222. app.updateChartRepoURLs()
  223. return app, nil
  224. }
  225. func (app *App) assignProvisionerAgent(sc *config.ServerConf) error {
  226. if sc.ProvisionerCluster == "kubeconfig" && sc.SelfKubeconfig != "" {
  227. app.Capabilities.Provisioning = true
  228. agent, err := local.GetSelfAgentFromFileConfig(sc.SelfKubeconfig)
  229. if err != nil {
  230. return fmt.Errorf("could not get in-cluster agent: %v", err)
  231. }
  232. app.ProvisionerAgent = agent
  233. return nil
  234. } else if sc.ProvisionerCluster == "kubeconfig" {
  235. return fmt.Errorf(`"kubeconfig" cluster option requires path to kubeconfig`)
  236. }
  237. app.Capabilities.Provisioning = true
  238. agent, err := kubernetes.GetAgentInClusterConfig()
  239. if err != nil {
  240. return fmt.Errorf("could not get in-cluster agent: %v", err)
  241. }
  242. app.ProvisionerAgent = agent
  243. return nil
  244. }
  245. func (app *App) assignIngressAgent(sc *config.ServerConf) error {
  246. if sc.IngressCluster == "kubeconfig" && sc.SelfKubeconfig != "" {
  247. agent, err := local.GetSelfAgentFromFileConfig(sc.SelfKubeconfig)
  248. if err != nil {
  249. return fmt.Errorf("could not get in-cluster agent: %v", err)
  250. }
  251. app.IngressAgent = agent
  252. return nil
  253. } else if sc.IngressCluster == "kubeconfig" {
  254. return fmt.Errorf(`"kubeconfig" cluster option requires path to kubeconfig`)
  255. }
  256. agent, err := kubernetes.GetAgentInClusterConfig()
  257. if err != nil {
  258. return fmt.Errorf("could not get in-cluster agent: %v", err)
  259. }
  260. app.IngressAgent = agent
  261. return nil
  262. }
  263. func (app *App) getTokenFromRequest(r *http.Request) *token.Token {
  264. reqToken := r.Header.Get("Authorization")
  265. splitToken := strings.Split(reqToken, "Bearer")
  266. if len(splitToken) != 2 {
  267. return nil
  268. }
  269. reqToken = strings.TrimSpace(splitToken[1])
  270. tok, err := token.GetTokenFromEncoded(reqToken, app.tokenConf)
  271. if err != nil {
  272. return nil
  273. }
  274. return tok
  275. }
  276. func (app *App) updateChartRepoURLs() {
  277. newCharts := make(map[string]string)
  278. for _, chartRepo := range []string{
  279. app.ServerConf.DefaultApplicationHelmRepoURL,
  280. app.ServerConf.DefaultAddonHelmRepoURL,
  281. } {
  282. indexFile, err := loader.LoadRepoIndexPublic(chartRepo)
  283. if err != nil {
  284. continue
  285. }
  286. for chartName, _ := range indexFile.Entries {
  287. newCharts[chartName] = chartRepo
  288. }
  289. }
  290. app.ChartLookupURLs = newCharts
  291. }