project_handler.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/porter-dev/porter/internal/forms"
  8. "github.com/porter-dev/porter/internal/models"
  9. )
  10. // Enumeration of user API error codes, represented as int64
  11. const (
  12. ErrProjectDecode ErrorCode = iota + 600
  13. ErrProjectValidateFields
  14. ErrProjectDataRead
  15. )
  16. // HandleCreateProject validates a project form entry, converts the project to a gorm
  17. // model, and saves the user to the database
  18. func (app *App) HandleCreateProject(w http.ResponseWriter, r *http.Request) {
  19. session, err := app.store.Get(r, app.cookieName)
  20. if err != nil {
  21. http.Error(w, err.Error(), http.StatusInternalServerError)
  22. return
  23. }
  24. userID, _ := session.Values["user_id"].(uint)
  25. form := &forms.CreateProjectForm{}
  26. // decode from JSON to form value
  27. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  28. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  29. return
  30. }
  31. // validate the form
  32. if err := app.validator.Struct(form); err != nil {
  33. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  34. return
  35. }
  36. // convert the form to a project model
  37. projModel, err := form.ToProject(app.repo.Project)
  38. if err != nil {
  39. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  40. return
  41. }
  42. // handle write to the database
  43. projModel, err = app.repo.Project.CreateProject(projModel)
  44. if err != nil {
  45. app.handleErrorDataWrite(err, w)
  46. return
  47. }
  48. // create a new Role with the user as the admin
  49. _, err = app.repo.Project.CreateProjectRole(projModel, &models.Role{
  50. UserID: userID,
  51. ProjectID: projModel.ID,
  52. Kind: models.RoleAdmin,
  53. })
  54. if err != nil {
  55. app.handleErrorDataWrite(err, w)
  56. return
  57. }
  58. app.logger.Info().Msgf("New project created: %d", projModel.ID)
  59. w.WriteHeader(http.StatusCreated)
  60. projExt := projModel.Externalize()
  61. if err := json.NewEncoder(w).Encode(projExt); err != nil {
  62. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  63. return
  64. }
  65. }
  66. // HandleReadProject returns an externalized Project (models.ProjectExternal)
  67. // based on an ID
  68. func (app *App) HandleReadProject(w http.ResponseWriter, r *http.Request) {
  69. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  70. if err != nil || id == 0 {
  71. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  72. return
  73. }
  74. proj, err := app.repo.Project.ReadProject(uint(id))
  75. if err != nil {
  76. app.handleErrorRead(err, ErrProjectDataRead, w)
  77. return
  78. }
  79. projExt := proj.Externalize()
  80. w.WriteHeader(http.StatusOK)
  81. if err := json.NewEncoder(w).Encode(projExt); err != nil {
  82. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  83. return
  84. }
  85. }
  86. // HandleReadProjectServiceAccount reads a service account by id
  87. func (app *App) HandleReadProjectServiceAccount(w http.ResponseWriter, r *http.Request) {
  88. id, err := strconv.ParseUint(chi.URLParam(r, "service_account_id"), 0, 64)
  89. if err != nil || id == 0 {
  90. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  91. return
  92. }
  93. sa, err := app.repo.ServiceAccount.ReadServiceAccount(uint(id))
  94. if err != nil {
  95. app.handleErrorRead(err, ErrProjectDataRead, w)
  96. return
  97. }
  98. saExt := sa.Externalize()
  99. w.WriteHeader(http.StatusOK)
  100. if err := json.NewEncoder(w).Encode(saExt); err != nil {
  101. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  102. return
  103. }
  104. }
  105. // HandleListProjectClusters returns a list of clusters that have linked ServiceAccounts.
  106. // If multiple service accounts exist for a cluster, the service account created later
  107. // will take precedence. This may be changed in a future release to return multiple
  108. // service accounts.
  109. func (app *App) HandleListProjectClusters(w http.ResponseWriter, r *http.Request) {
  110. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  111. if err != nil || id == 0 {
  112. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  113. return
  114. }
  115. sas, err := app.repo.ServiceAccount.ListServiceAccountsByProjectID(uint(id))
  116. if err != nil {
  117. app.handleErrorRead(err, ErrProjectDataRead, w)
  118. return
  119. }
  120. clusters := make([]*models.ClusterExternal, 0)
  121. // clusterMapIndex used for checking if cluster has already been added
  122. // maps from the cluster's endpoint to the index in the cluster array
  123. clusterMapIndex := make(map[string]int)
  124. for _, sa := range sas {
  125. for _, cluster := range sa.Clusters {
  126. if currIndex, ok := clusterMapIndex[cluster.Server]; ok {
  127. if clusters[currIndex].ServiceAccountID <= cluster.ServiceAccountID {
  128. clusters[currIndex] = cluster.Externalize()
  129. continue
  130. }
  131. }
  132. clusterMapIndex[cluster.Server] = len(clusters)
  133. clusters = append(clusters, cluster.Externalize())
  134. }
  135. }
  136. w.WriteHeader(http.StatusOK)
  137. if err := json.NewEncoder(w).Encode(clusters); err != nil {
  138. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  139. return
  140. }
  141. }
  142. // HandleCreateProjectSACandidates handles the creation of ServiceAccountCandidates
  143. // using a kubeconfig and a project id
  144. func (app *App) HandleCreateProjectSACandidates(w http.ResponseWriter, r *http.Request) {
  145. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  146. if err != nil || projID == 0 {
  147. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  148. return
  149. }
  150. form := &forms.CreateServiceAccountCandidatesForm{
  151. ProjectID: uint(projID),
  152. }
  153. // decode from JSON to form value
  154. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  155. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  156. return
  157. }
  158. // validate the form
  159. if err := app.validator.Struct(form); err != nil {
  160. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  161. return
  162. }
  163. // convert the form to a ServiceAccountCandidate
  164. saCandidates, err := form.ToServiceAccountCandidates()
  165. if err != nil {
  166. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  167. return
  168. }
  169. extSACandidates := make([]*models.ServiceAccountCandidateExternal, 0)
  170. for _, saCandidate := range saCandidates {
  171. // handle write to the database
  172. saCandidate, err = app.repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  173. if err != nil {
  174. app.handleErrorDataWrite(err, w)
  175. return
  176. }
  177. app.logger.Info().Msgf("New service account candidate created: %d", saCandidate.ID)
  178. // if the SA candidate does not have any actions to perform, create the ServiceAccount
  179. // automatically
  180. if len(saCandidate.Actions) == 0 {
  181. // we query the repo again to get the decrypted version of the SA candidate
  182. saCandidate, err = app.repo.ServiceAccount.ReadServiceAccountCandidate(saCandidate.ID)
  183. if err != nil {
  184. app.handleErrorDataRead(err, w)
  185. return
  186. }
  187. saForm := &forms.ServiceAccountActionResolver{
  188. ServiceAccountCandidateID: saCandidate.ID,
  189. SACandidate: saCandidate,
  190. }
  191. err := saForm.PopulateServiceAccount(app.repo.ServiceAccount)
  192. if err != nil {
  193. app.handleErrorDataWrite(err, w)
  194. return
  195. }
  196. sa, err := app.repo.ServiceAccount.CreateServiceAccount(saForm.SA)
  197. if err != nil {
  198. app.handleErrorDataWrite(err, w)
  199. return
  200. }
  201. saCandidate, err = app.repo.ServiceAccount.UpdateServiceAccountCandidateCreatedSAID(saCandidate.ID, sa.ID)
  202. if err != nil {
  203. app.handleErrorDataWrite(err, w)
  204. return
  205. }
  206. app.logger.Info().Msgf("New service account created: %d", sa.ID)
  207. }
  208. extSACandidates = append(extSACandidates, saCandidate.Externalize())
  209. }
  210. w.WriteHeader(http.StatusCreated)
  211. if err := json.NewEncoder(w).Encode(extSACandidates); err != nil {
  212. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  213. return
  214. }
  215. }
  216. // HandleListProjectSACandidates returns a list of externalized ServiceAccountCandidate
  217. // ([]models.ServiceAccountCandidateExternal) based on a project ID
  218. func (app *App) HandleListProjectSACandidates(w http.ResponseWriter, r *http.Request) {
  219. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  220. if err != nil || projID == 0 {
  221. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  222. return
  223. }
  224. saCandidates, err := app.repo.ServiceAccount.ListServiceAccountCandidatesByProjectID(uint(projID))
  225. if err != nil {
  226. app.handleErrorRead(err, ErrProjectDataRead, w)
  227. return
  228. }
  229. extSACandidates := make([]*models.ServiceAccountCandidateExternal, 0)
  230. for _, saCandidate := range saCandidates {
  231. extSACandidates = append(extSACandidates, saCandidate.Externalize())
  232. }
  233. w.WriteHeader(http.StatusOK)
  234. if err := json.NewEncoder(w).Encode(extSACandidates); err != nil {
  235. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  236. return
  237. }
  238. }
  239. // HandleResolveSACandidateActions accepts a list of action configurations for a
  240. // given ServiceAccountCandidate, which "resolves" that ServiceAccountCandidate
  241. // and creates a ServiceAccount for a specific project
  242. func (app *App) HandleResolveSACandidateActions(w http.ResponseWriter, r *http.Request) {
  243. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  244. if err != nil || projID == 0 {
  245. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  246. return
  247. }
  248. candID, err := strconv.ParseUint(chi.URLParam(r, "candidate_id"), 0, 64)
  249. if err != nil || projID == 0 {
  250. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  251. return
  252. }
  253. // decode actions from request
  254. actions := make([]*models.ServiceAccountAllActions, 0)
  255. if err := json.NewDecoder(r.Body).Decode(&actions); err != nil {
  256. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  257. return
  258. }
  259. var saResolverBase *forms.ServiceAccountActionResolver = &forms.ServiceAccountActionResolver{
  260. ServiceAccountCandidateID: uint(candID),
  261. SA: nil,
  262. SACandidate: nil,
  263. }
  264. // for each action, create the relevant form and populate the service account
  265. // we'll chain the .PopulateServiceAccount functions
  266. for _, action := range actions {
  267. var err error
  268. switch action.Name {
  269. case models.ClusterCADataAction:
  270. form := &forms.ClusterCADataAction{
  271. ServiceAccountActionResolver: saResolverBase,
  272. ClusterCAData: action.ClusterCAData,
  273. }
  274. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  275. case models.ClientCertDataAction:
  276. form := &forms.ClientCertDataAction{
  277. ServiceAccountActionResolver: saResolverBase,
  278. ClientCertData: action.ClientCertData,
  279. }
  280. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  281. case models.ClientKeyDataAction:
  282. form := &forms.ClientKeyDataAction{
  283. ServiceAccountActionResolver: saResolverBase,
  284. ClientKeyData: action.ClientKeyData,
  285. }
  286. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  287. case models.OIDCIssuerDataAction:
  288. form := &forms.OIDCIssuerDataAction{
  289. ServiceAccountActionResolver: saResolverBase,
  290. OIDCIssuerCAData: action.OIDCIssuerCAData,
  291. }
  292. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  293. case models.TokenDataAction:
  294. form := &forms.TokenDataAction{
  295. ServiceAccountActionResolver: saResolverBase,
  296. TokenData: action.TokenData,
  297. }
  298. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  299. case models.GCPKeyDataAction:
  300. form := &forms.GCPKeyDataAction{
  301. ServiceAccountActionResolver: saResolverBase,
  302. GCPKeyData: action.GCPKeyData,
  303. }
  304. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  305. case models.AWSDataAction:
  306. form := &forms.AWSDataAction{
  307. ServiceAccountActionResolver: saResolverBase,
  308. AWSAccessKeyID: action.AWSAccessKeyID,
  309. AWSSecretAccessKey: action.AWSSecretAccessKey,
  310. AWSClusterID: action.AWSClusterID,
  311. }
  312. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  313. }
  314. if err != nil {
  315. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  316. return
  317. }
  318. }
  319. sa, err := app.repo.ServiceAccount.CreateServiceAccount(saResolverBase.SA)
  320. if err != nil {
  321. app.handleErrorDataWrite(err, w)
  322. return
  323. }
  324. if sa != nil {
  325. app.logger.Info().Msgf("New service account created: %d", sa.ID)
  326. _, err := app.repo.ServiceAccount.UpdateServiceAccountCandidateCreatedSAID(uint(candID), sa.ID)
  327. if err != nil {
  328. app.handleErrorDataWrite(err, w)
  329. return
  330. }
  331. saExternal := sa.Externalize()
  332. w.WriteHeader(http.StatusCreated)
  333. if err := json.NewEncoder(w).Encode(saExternal); err != nil {
  334. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  335. return
  336. }
  337. } else {
  338. w.WriteHeader(http.StatusNotModified)
  339. }
  340. }
  341. // HandleDeleteProject deletes a project from the db, reading from the project_id
  342. // in the URL param
  343. func (app *App) HandleDeleteProject(w http.ResponseWriter, r *http.Request) {
  344. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  345. if err != nil || id == 0 {
  346. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  347. return
  348. }
  349. proj, err := app.repo.Project.ReadProject(uint(id))
  350. if err != nil {
  351. app.handleErrorRead(err, ErrProjectDataRead, w)
  352. return
  353. }
  354. proj, err = app.repo.Project.DeleteProject(proj)
  355. if err != nil {
  356. app.handleErrorRead(err, ErrProjectDataRead, w)
  357. return
  358. }
  359. projExternal := proj.Externalize()
  360. w.WriteHeader(http.StatusOK)
  361. if err := json.NewEncoder(w).Encode(projExternal); err != nil {
  362. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  363. return
  364. }
  365. }