api.go 9.0 KB

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