create.go 16 KB

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