project_handler.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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, ErrUserDecode, 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. // HandleCreateProjectSACandidates handles the creation of ServiceAccountCandidates
  87. // using a kubeconfig and a project id
  88. func (app *App) HandleCreateProjectSACandidates(w http.ResponseWriter, r *http.Request) {
  89. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  90. if err != nil || projID == 0 {
  91. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  92. return
  93. }
  94. form := &forms.CreateServiceAccountCandidatesForm{
  95. ProjectID: uint(projID),
  96. }
  97. // decode from JSON to form value
  98. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  99. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  100. return
  101. }
  102. // validate the form
  103. if err := app.validator.Struct(form); err != nil {
  104. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  105. return
  106. }
  107. // convert the form to a ServiceAccountCandidate
  108. saCandidates, err := form.ToServiceAccountCandidates()
  109. if err != nil {
  110. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  111. return
  112. }
  113. extSACandidates := make([]*models.ServiceAccountCandidateExternal, 0)
  114. for _, saCandidate := range saCandidates {
  115. // handle write to the database
  116. saCandidate, err = app.repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  117. if err != nil {
  118. app.handleErrorDataWrite(err, w)
  119. return
  120. }
  121. app.logger.Info().Msgf("New service account candidate created: %d", saCandidate.ID)
  122. // if the SA candidate does not have any actions to perform, create the ServiceAccount
  123. // automatically
  124. if len(saCandidate.Actions) == 0 {
  125. saForm := &forms.ServiceAccountActionResolver{
  126. ServiceAccountCandidateID: saCandidate.ID,
  127. SACandidate: saCandidate,
  128. }
  129. err := saForm.PopulateServiceAccount(app.repo.ServiceAccount)
  130. if err != nil {
  131. app.handleErrorDataWrite(err, w)
  132. return
  133. }
  134. sa, err := app.repo.ServiceAccount.CreateServiceAccount(saForm.SA)
  135. if err != nil {
  136. app.handleErrorDataWrite(err, w)
  137. return
  138. }
  139. app.logger.Info().Msgf("New service account created: %d", sa.ID)
  140. }
  141. extSACandidates = append(extSACandidates, saCandidate.Externalize())
  142. }
  143. w.WriteHeader(http.StatusCreated)
  144. if err := json.NewEncoder(w).Encode(extSACandidates); err != nil {
  145. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  146. return
  147. }
  148. }
  149. // HandleListProjectSACandidates returns a list of externalized ServiceAccountCandidate
  150. // ([]models.ServiceAccountCandidateExternal) based on a project ID
  151. func (app *App) HandleListProjectSACandidates(w http.ResponseWriter, r *http.Request) {
  152. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  153. if err != nil || projID == 0 {
  154. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  155. return
  156. }
  157. saCandidates, err := app.repo.ServiceAccount.ListServiceAccountCandidatesByProjectID(uint(projID))
  158. if err != nil {
  159. app.handleErrorRead(err, ErrProjectDataRead, w)
  160. return
  161. }
  162. extSACandidates := make([]*models.ServiceAccountCandidateExternal, 0)
  163. for _, saCandidate := range saCandidates {
  164. extSACandidates = append(extSACandidates, saCandidate.Externalize())
  165. }
  166. w.WriteHeader(http.StatusOK)
  167. if err := json.NewEncoder(w).Encode(extSACandidates); err != nil {
  168. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  169. return
  170. }
  171. }
  172. // HandleResolveSACandidateActions accepts a list of action configurations for a
  173. // given ServiceAccountCandidate, which "resolves" that ServiceAccountCandidate
  174. // and creates a ServiceAccount for a specific project
  175. func (app *App) HandleResolveSACandidateActions(w http.ResponseWriter, r *http.Request) {
  176. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  177. if err != nil || projID == 0 {
  178. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  179. return
  180. }
  181. candID, err := strconv.ParseUint(chi.URLParam(r, "candidate_id"), 0, 64)
  182. if err != nil || projID == 0 {
  183. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  184. return
  185. }
  186. // decode actions from request
  187. actions := make([]*models.ServiceAccountAllActions, 0)
  188. if err := json.NewDecoder(r.Body).Decode(&actions); err != nil {
  189. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  190. return
  191. }
  192. var saResolverBase *forms.ServiceAccountActionResolver = &forms.ServiceAccountActionResolver{
  193. ServiceAccountCandidateID: uint(candID),
  194. SA: nil,
  195. SACandidate: nil,
  196. }
  197. // for each action, create the relevant form and populate the service account
  198. // we'll chain the .PopulateServiceAccount functions
  199. for _, action := range actions {
  200. var err error
  201. switch action.Name {
  202. case models.ClusterCADataAction:
  203. form := &forms.ClusterCADataAction{
  204. ServiceAccountActionResolver: saResolverBase,
  205. ClusterCAData: action.ClusterCAData,
  206. }
  207. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  208. case models.ClientCertDataAction:
  209. form := &forms.ClientCertDataAction{
  210. ServiceAccountActionResolver: saResolverBase,
  211. ClientCertData: action.ClientCertData,
  212. }
  213. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  214. case models.ClientKeyDataAction:
  215. form := &forms.ClientKeyDataAction{
  216. ServiceAccountActionResolver: saResolverBase,
  217. ClientKeyData: action.ClientKeyData,
  218. }
  219. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  220. case models.OIDCIssuerDataAction:
  221. form := &forms.OIDCIssuerDataAction{
  222. ServiceAccountActionResolver: saResolverBase,
  223. OIDCIssuerCAData: action.OIDCIssuerCAData,
  224. }
  225. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  226. case models.TokenDataAction:
  227. form := &forms.TokenDataAction{
  228. ServiceAccountActionResolver: saResolverBase,
  229. TokenData: action.TokenData,
  230. }
  231. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  232. case models.GCPKeyDataAction:
  233. form := &forms.GCPKeyDataAction{
  234. ServiceAccountActionResolver: saResolverBase,
  235. GCPKeyData: action.GCPKeyData,
  236. }
  237. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  238. case models.AWSKeyDataAction:
  239. form := &forms.AWSKeyDataAction{
  240. ServiceAccountActionResolver: saResolverBase,
  241. AWSKeyData: action.AWSKeyData,
  242. }
  243. err = form.PopulateServiceAccount(app.repo.ServiceAccount)
  244. }
  245. if err != nil {
  246. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  247. return
  248. }
  249. }
  250. sa, err := app.repo.ServiceAccount.CreateServiceAccount(saResolverBase.SA)
  251. if err != nil {
  252. app.handleErrorDataWrite(err, w)
  253. return
  254. }
  255. if sa != nil {
  256. app.logger.Info().Msgf("New service account created: %d", sa.ID)
  257. saExternal := sa.Externalize()
  258. w.WriteHeader(http.StatusCreated)
  259. if err := json.NewEncoder(w).Encode(saExternal); err != nil {
  260. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  261. return
  262. }
  263. } else {
  264. w.WriteHeader(http.StatusNotModified)
  265. }
  266. }