create.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. package release
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "github.com/porter-dev/porter/api/server/authz"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. "github.com/porter-dev/porter/api/server/shared"
  11. "github.com/porter-dev/porter/api/server/shared/apierrors"
  12. "github.com/porter-dev/porter/api/server/shared/config"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/analytics"
  15. "github.com/porter-dev/porter/internal/auth/token"
  16. "github.com/porter-dev/porter/internal/encryption"
  17. "github.com/porter-dev/porter/internal/helm"
  18. "github.com/porter-dev/porter/internal/helm/loader"
  19. "github.com/porter-dev/porter/internal/integrations/ci/actions"
  20. "github.com/porter-dev/porter/internal/integrations/ci/gitlab"
  21. "github.com/porter-dev/porter/internal/models"
  22. "github.com/porter-dev/porter/internal/oauth"
  23. "github.com/porter-dev/porter/internal/registry"
  24. "golang.org/x/crypto/bcrypt"
  25. "gopkg.in/yaml.v2"
  26. "helm.sh/helm/v3/pkg/release"
  27. v1 "k8s.io/api/core/v1"
  28. )
  29. type CreateReleaseHandler struct {
  30. handlers.PorterHandlerReadWriter
  31. authz.KubernetesAgentGetter
  32. }
  33. func NewCreateReleaseHandler(
  34. config *config.Config,
  35. decoderValidator shared.RequestDecoderValidator,
  36. writer shared.ResultWriter,
  37. ) *CreateReleaseHandler {
  38. return &CreateReleaseHandler{
  39. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  40. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  41. }
  42. }
  43. func (c *CreateReleaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  44. user, _ := r.Context().Value(types.UserScope).(*models.User)
  45. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  46. namespace := r.Context().Value(types.NamespaceScope).(string)
  47. operationID := oauth.CreateRandomState()
  48. c.Config().AnalyticsClient.Track(analytics.ApplicationLaunchStartTrack(
  49. &analytics.ApplicationLaunchStartTrackOpts{
  50. ClusterScopedTrackOpts: analytics.GetClusterScopedTrackOpts(user.ID, cluster.ProjectID, cluster.ID),
  51. FlowID: operationID,
  52. },
  53. ))
  54. helmAgent, err := c.GetHelmAgent(r, cluster, "")
  55. if err != nil {
  56. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  57. return
  58. }
  59. request := &types.CreateReleaseRequest{}
  60. if ok := c.DecodeAndValidate(w, r, request); !ok {
  61. return
  62. }
  63. if request.RepoURL == "" {
  64. request.RepoURL = c.Config().ServerConf.DefaultApplicationHelmRepoURL
  65. }
  66. if request.TemplateVersion == "latest" {
  67. request.TemplateVersion = ""
  68. }
  69. chart, err := loader.LoadChartPublic(request.RepoURL, request.TemplateName, request.TemplateVersion)
  70. if err != nil {
  71. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  72. return
  73. }
  74. registries, err := c.Repo().Registry().ListRegistriesByProjectID(cluster.ProjectID)
  75. if err != nil {
  76. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  77. return
  78. }
  79. conf := &helm.InstallChartConfig{
  80. Chart: chart,
  81. Name: request.Name,
  82. Namespace: namespace,
  83. Values: request.Values,
  84. Cluster: cluster,
  85. Repo: c.Repo(),
  86. Registries: registries,
  87. }
  88. helmRelease, err := helmAgent.InstallChart(conf, c.Config().DOConf)
  89. if err != nil {
  90. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  91. fmt.Errorf("error installing a new chart: %s", err.Error()),
  92. http.StatusBadRequest,
  93. ))
  94. return
  95. }
  96. k8sAgent, err := c.GetAgent(r, cluster, "")
  97. if err != nil {
  98. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  99. return
  100. }
  101. configMaps := make([]*v1.ConfigMap, 0)
  102. if request.SyncedEnvGroups != nil && len(request.SyncedEnvGroups) > 0 {
  103. for _, envGroupName := range request.SyncedEnvGroups {
  104. // read the attached configmap
  105. cm, _, err := k8sAgent.GetLatestVersionedConfigMap(envGroupName, namespace)
  106. if err != nil {
  107. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("Couldn't find the env group"), http.StatusNotFound))
  108. return
  109. }
  110. configMaps = append(configMaps, cm)
  111. }
  112. }
  113. release, err := CreateAppReleaseFromHelmRelease(c.Config(), cluster.ProjectID, cluster.ID, 0, helmRelease)
  114. if err != nil {
  115. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  116. return
  117. }
  118. if len(configMaps) > 0 {
  119. for _, cm := range configMaps {
  120. _, err = k8sAgent.AddApplicationToVersionedConfigMap(cm, release.Name)
  121. if err != nil {
  122. c.HandleAPIErrorNoWrite(w, r, apierrors.NewErrInternal(fmt.Errorf("Couldn't add %s to the config map %s", release.Name, cm.Name)))
  123. }
  124. }
  125. }
  126. if request.Tags != nil {
  127. tags, err := c.Repo().Tag().LinkTagsToRelease(request.Tags, release)
  128. if err == nil {
  129. release.Tags = append(release.Tags, tags...)
  130. }
  131. }
  132. if request.GitActionConfig != nil {
  133. _, _, err := createGitAction(
  134. c.Config(),
  135. user.ID,
  136. cluster.ProjectID,
  137. cluster.ID,
  138. request.GitActionConfig,
  139. request.Name,
  140. namespace,
  141. release,
  142. )
  143. if err != nil {
  144. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  145. return
  146. }
  147. }
  148. if request.BuildConfig != nil {
  149. _, err = createBuildConfig(c.Config(), release, request.BuildConfig)
  150. }
  151. if err != nil {
  152. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  153. return
  154. }
  155. c.Config().AnalyticsClient.Track(analytics.ApplicationLaunchSuccessTrack(
  156. &analytics.ApplicationLaunchSuccessTrackOpts{
  157. ApplicationScopedTrackOpts: analytics.GetApplicationScopedTrackOpts(
  158. user.ID,
  159. cluster.ProjectID,
  160. cluster.ID,
  161. release.Name,
  162. release.Namespace,
  163. chart.Metadata.Name,
  164. ),
  165. FlowID: operationID,
  166. },
  167. ))
  168. }
  169. func CreateAppReleaseFromHelmRelease(
  170. config *config.Config,
  171. projectID, clusterID, stackResourceID uint,
  172. helmRelease *release.Release,
  173. ) (*models.Release, error) {
  174. token, err := encryption.GenerateRandomBytes(16)
  175. if err != nil {
  176. return nil, err
  177. }
  178. // create release with webhook token in db
  179. image, ok := helmRelease.Config["image"].(map[string]interface{})
  180. if !ok {
  181. return nil, fmt.Errorf("Could not find field image in config")
  182. }
  183. repository := image["repository"]
  184. repoStr, ok := repository.(string)
  185. if !ok {
  186. return nil, fmt.Errorf("Could not find field repository in config")
  187. }
  188. release := &models.Release{
  189. ClusterID: clusterID,
  190. ProjectID: projectID,
  191. Namespace: helmRelease.Namespace,
  192. Name: helmRelease.Name,
  193. WebhookToken: token,
  194. ImageRepoURI: repoStr,
  195. StackResourceID: stackResourceID,
  196. }
  197. return config.Repo.Release().CreateRelease(release)
  198. }
  199. func CreateAddonReleaseFromHelmRelease(
  200. config *config.Config,
  201. projectID, clusterID, stackResourceID uint,
  202. helmRelease *release.Release,
  203. ) (*models.Release, error) {
  204. release := &models.Release{
  205. ClusterID: clusterID,
  206. ProjectID: projectID,
  207. Namespace: helmRelease.Namespace,
  208. Name: helmRelease.Name,
  209. StackResourceID: stackResourceID,
  210. }
  211. return config.Repo.Release().CreateRelease(release)
  212. }
  213. func createGitAction(
  214. config *config.Config,
  215. userID, projectID, clusterID uint,
  216. request *types.CreateGitActionConfigRequest,
  217. name, namespace string,
  218. release *models.Release,
  219. ) (*types.GitActionConfig, []byte, error) {
  220. // if the registry was provisioned through Porter, create a repository if necessary
  221. if release != nil && request.RegistryID != 0 {
  222. // read the registry
  223. reg, err := config.Repo.Registry().ReadRegistry(projectID, request.RegistryID)
  224. if err != nil {
  225. return nil, nil, err
  226. }
  227. _reg := registry.Registry(*reg)
  228. regAPI := &_reg
  229. // parse the name from the registry
  230. nameSpl := strings.Split(request.ImageRepoURI, "/")
  231. repoName := nameSpl[len(nameSpl)-1]
  232. err = regAPI.CreateRepository(config.Repo, repoName)
  233. if err != nil {
  234. return nil, nil, err
  235. }
  236. }
  237. isDryRun := release == nil
  238. repoSplit := strings.Split(request.GitRepo, "/")
  239. if len(repoSplit) != 2 {
  240. return nil, nil, fmt.Errorf("invalid formatting of repo name")
  241. }
  242. encoded := ""
  243. var err error
  244. // if this isn't a dry run, generate the token
  245. if !isDryRun {
  246. encoded, err = getToken(config, userID, projectID, clusterID, request)
  247. if err != nil {
  248. return nil, nil, err
  249. }
  250. }
  251. var workflowYAML []byte
  252. var gitErr error
  253. if request.GitlabIntegrationID != 0 {
  254. giRunner := &gitlab.GitlabCI{
  255. ServerURL: config.ServerConf.ServerURL,
  256. GitRepoOwner: repoSplit[0],
  257. GitRepoName: repoSplit[1],
  258. GitBranch: request.GitBranch,
  259. Repo: config.Repo,
  260. ProjectID: projectID,
  261. ClusterID: clusterID,
  262. UserID: userID,
  263. IntegrationID: request.GitlabIntegrationID,
  264. PorterConf: config,
  265. ReleaseName: name,
  266. ReleaseNamespace: namespace,
  267. FolderPath: request.FolderPath,
  268. PorterToken: encoded,
  269. }
  270. gitErr = giRunner.Setup()
  271. } else {
  272. // create the commit in the git repo
  273. gaRunner := &actions.GithubActions{
  274. InstanceName: config.ServerConf.InstanceName,
  275. ServerURL: config.ServerConf.ServerURL,
  276. GithubOAuthIntegration: nil,
  277. GithubAppID: config.GithubAppConf.AppID,
  278. GithubAppSecretPath: config.GithubAppConf.SecretPath,
  279. GithubInstallationID: request.GitRepoID,
  280. GitRepoName: repoSplit[1],
  281. GitRepoOwner: repoSplit[0],
  282. Repo: config.Repo,
  283. ProjectID: projectID,
  284. ClusterID: clusterID,
  285. ReleaseName: name,
  286. ReleaseNamespace: namespace,
  287. GitBranch: request.GitBranch,
  288. DockerFilePath: request.DockerfilePath,
  289. FolderPath: request.FolderPath,
  290. ImageRepoURL: request.ImageRepoURI,
  291. PorterToken: encoded,
  292. Version: "v0.1.0",
  293. ShouldCreateWorkflow: request.ShouldCreateWorkflow,
  294. DryRun: release == nil,
  295. }
  296. // Save the github err for after creating the git action config. However, we
  297. // need to call Setup() in order to get the workflow file before writing the
  298. // action config, in the case of a dry run, since the dry run does not create
  299. // a git action config.
  300. workflowYAML, githubErr := gaRunner.Setup()
  301. if gaRunner.DryRun {
  302. if githubErr != nil {
  303. return nil, nil, githubErr
  304. }
  305. return nil, workflowYAML, nil
  306. }
  307. }
  308. // handle write to the database
  309. ga, err := config.Repo.GitActionConfig().CreateGitActionConfig(&models.GitActionConfig{
  310. ReleaseID: release.ID,
  311. GitRepo: request.GitRepo,
  312. GitBranch: request.GitBranch,
  313. ImageRepoURI: request.ImageRepoURI,
  314. GitRepoID: request.GitRepoID,
  315. GitlabIntegrationID: request.GitlabIntegrationID,
  316. DockerfilePath: request.DockerfilePath,
  317. FolderPath: request.FolderPath,
  318. IsInstallation: true,
  319. Version: "v0.1.0",
  320. })
  321. if err != nil {
  322. return nil, nil, err
  323. }
  324. // update the release in the db with the image repo uri
  325. release.ImageRepoURI = ga.ImageRepoURI
  326. _, err = config.Repo.Release().UpdateRelease(release)
  327. if err != nil {
  328. return nil, nil, err
  329. }
  330. if gitErr != nil {
  331. return nil, nil, gitErr
  332. }
  333. return ga.ToGitActionConfigType(), workflowYAML, nil
  334. }
  335. func getToken(
  336. config *config.Config,
  337. userID, projectID, clusterID uint,
  338. request *types.CreateGitActionConfigRequest,
  339. ) (string, error) {
  340. // create a policy for the token
  341. policy := []*types.PolicyDocument{
  342. {
  343. Scope: types.ProjectScope,
  344. Verbs: types.ReadWriteVerbGroup(),
  345. Children: map[types.PermissionScope]*types.PolicyDocument{
  346. types.ClusterScope: {
  347. Scope: types.ClusterScope,
  348. Verbs: types.ReadWriteVerbGroup(),
  349. },
  350. types.RegistryScope: {
  351. Scope: types.RegistryScope,
  352. Verbs: types.ReadVerbGroup(),
  353. },
  354. types.HelmRepoScope: {
  355. Scope: types.HelmRepoScope,
  356. Verbs: types.ReadVerbGroup(),
  357. },
  358. },
  359. },
  360. }
  361. uid, err := encryption.GenerateRandomBytes(16)
  362. if err != nil {
  363. return "", err
  364. }
  365. policyBytes, err := json.Marshal(policy)
  366. if err != nil {
  367. return "", err
  368. }
  369. policyModel := &models.Policy{
  370. ProjectID: projectID,
  371. UniqueID: uid,
  372. CreatedByUserID: userID,
  373. Name: strings.ToLower(fmt.Sprintf("repo-%s-token-policy", request.GitRepo)),
  374. PolicyBytes: policyBytes,
  375. }
  376. policyModel, err = config.Repo.Policy().CreatePolicy(policyModel)
  377. if err != nil {
  378. return "", err
  379. }
  380. // create the token in the database
  381. tokenUID, err := encryption.GenerateRandomBytes(16)
  382. if err != nil {
  383. return "", err
  384. }
  385. secretKey, err := encryption.GenerateRandomBytes(16)
  386. if err != nil {
  387. return "", err
  388. }
  389. // hash the secret key for storage in the db
  390. hashedToken, err := bcrypt.GenerateFromPassword([]byte(secretKey), 8)
  391. if err != nil {
  392. return "", err
  393. }
  394. expiresAt := time.Now().Add(time.Hour * 24 * 365)
  395. apiToken := &models.APIToken{
  396. UniqueID: tokenUID,
  397. ProjectID: projectID,
  398. CreatedByUserID: userID,
  399. Expiry: &expiresAt,
  400. Revoked: false,
  401. PolicyUID: policyModel.UniqueID,
  402. PolicyName: policyModel.Name,
  403. Name: strings.ToLower(fmt.Sprintf("repo-%s-token", request.GitRepo)),
  404. SecretKey: hashedToken,
  405. }
  406. apiToken, err = config.Repo.APIToken().CreateAPIToken(apiToken)
  407. if err != nil {
  408. return "", err
  409. }
  410. // generate porter jwt token
  411. jwt, err := token.GetStoredTokenForAPI(userID, projectID, apiToken.UniqueID, secretKey)
  412. if err != nil {
  413. return "", err
  414. }
  415. return jwt.EncodeToken(config.TokenConf)
  416. }
  417. func createBuildConfig(
  418. config *config.Config,
  419. release *models.Release,
  420. bcRequest *types.CreateBuildConfigRequest,
  421. ) (*types.BuildConfig, error) {
  422. data, err := json.Marshal(bcRequest.Config)
  423. if err != nil {
  424. return nil, err
  425. }
  426. // handle write to the database
  427. bc, err := config.Repo.BuildConfig().CreateBuildConfig(&models.BuildConfig{
  428. Builder: bcRequest.Builder,
  429. Buildpacks: strings.Join(bcRequest.Buildpacks, ","),
  430. Config: data,
  431. })
  432. if err != nil {
  433. return nil, err
  434. }
  435. release.BuildConfig = bc.ID
  436. _, err = config.Repo.Release().UpdateRelease(release)
  437. if err != nil {
  438. return nil, err
  439. }
  440. return bc.ToBuildConfigType(), nil
  441. }
  442. type containerEnvConfig struct {
  443. Container struct {
  444. Env struct {
  445. Normal map[string]string `yaml:"normal"`
  446. } `yaml:"env"`
  447. } `yaml:"container"`
  448. }
  449. func getGARunner(
  450. config *config.Config,
  451. userID, projectID, clusterID uint,
  452. ga *models.GitActionConfig,
  453. name, namespace string,
  454. release *models.Release,
  455. helmRelease *release.Release,
  456. ) (*actions.GithubActions, error) {
  457. cEnv := &containerEnvConfig{}
  458. rawValues, err := yaml.Marshal(helmRelease.Config)
  459. if err == nil {
  460. err = yaml.Unmarshal(rawValues, cEnv)
  461. // if unmarshal error, just set to empty map
  462. if err != nil {
  463. cEnv.Container.Env.Normal = make(map[string]string)
  464. }
  465. }
  466. repoSplit := strings.Split(ga.GitRepo, "/")
  467. if len(repoSplit) != 2 {
  468. return nil, fmt.Errorf("invalid formatting of repo name")
  469. }
  470. // create the commit in the git repo
  471. return &actions.GithubActions{
  472. ServerURL: config.ServerConf.ServerURL,
  473. GithubOAuthIntegration: nil,
  474. BuildEnv: cEnv.Container.Env.Normal,
  475. GithubAppID: config.GithubAppConf.AppID,
  476. GithubAppSecretPath: config.GithubAppConf.SecretPath,
  477. GithubInstallationID: ga.GitRepoID,
  478. GitRepoName: repoSplit[1],
  479. GitRepoOwner: repoSplit[0],
  480. Repo: config.Repo,
  481. ProjectID: projectID,
  482. ClusterID: clusterID,
  483. ReleaseName: name,
  484. GitBranch: ga.GitBranch,
  485. DockerFilePath: ga.DockerfilePath,
  486. FolderPath: ga.FolderPath,
  487. ImageRepoURL: ga.ImageRepoURI,
  488. Version: "v0.1.0",
  489. }, nil
  490. }