project_handler.go 9.1 KB

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