auth.go 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. package gorm
  2. import (
  3. "github.com/porter-dev/porter/internal/models"
  4. "github.com/porter-dev/porter/internal/repository"
  5. "gorm.io/gorm"
  6. ints "github.com/porter-dev/porter/internal/models/integrations"
  7. )
  8. // KubeIntegrationRepository uses gorm.DB for querying the database
  9. type KubeIntegrationRepository struct {
  10. db *gorm.DB
  11. key *[32]byte
  12. }
  13. // NewKubeIntegrationRepository returns a KubeIntegrationRepository which uses
  14. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  15. // sensitive data
  16. func NewKubeIntegrationRepository(
  17. db *gorm.DB,
  18. key *[32]byte,
  19. ) repository.KubeIntegrationRepository {
  20. return &KubeIntegrationRepository{db, key}
  21. }
  22. // CreateKubeIntegration creates a new kube auth mechanism
  23. func (repo *KubeIntegrationRepository) CreateKubeIntegration(
  24. am *ints.KubeIntegration,
  25. ) (*ints.KubeIntegration, error) {
  26. err := repo.EncryptKubeIntegrationData(am, repo.key)
  27. if err != nil {
  28. return nil, err
  29. }
  30. project := &models.Project{}
  31. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  32. return nil, err
  33. }
  34. assoc := repo.db.Model(&project).Association("KubeIntegrations")
  35. if assoc.Error != nil {
  36. return nil, assoc.Error
  37. }
  38. if err := assoc.Append(am); err != nil {
  39. return nil, err
  40. }
  41. return am, nil
  42. }
  43. // ReadKubeIntegration finds a kube auth mechanism by id
  44. func (repo *KubeIntegrationRepository) ReadKubeIntegration(
  45. id uint,
  46. ) (*ints.KubeIntegration, error) {
  47. ki := &ints.KubeIntegration{}
  48. if err := repo.db.Where("id = ?", id).First(&ki).Error; err != nil {
  49. return nil, err
  50. }
  51. err := repo.DecryptKubeIntegrationData(ki, repo.key)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return ki, nil
  56. }
  57. // ListKubeIntegrationsByProjectID finds all kube auth mechanisms
  58. // for a given project id
  59. func (repo *KubeIntegrationRepository) ListKubeIntegrationsByProjectID(
  60. projectID uint,
  61. ) ([]*ints.KubeIntegration, error) {
  62. kis := []*ints.KubeIntegration{}
  63. if err := repo.db.Where("project_id = ?", projectID).Find(&kis).Error; err != nil {
  64. return nil, err
  65. }
  66. for _, ki := range kis {
  67. repo.DecryptKubeIntegrationData(ki, repo.key)
  68. }
  69. return kis, nil
  70. }
  71. // EncryptKubeIntegrationData will encrypt the kube integration data before
  72. // writing to the DB
  73. func (repo *KubeIntegrationRepository) EncryptKubeIntegrationData(
  74. ki *ints.KubeIntegration,
  75. key *[32]byte,
  76. ) error {
  77. if len(ki.ClientCertificateData) > 0 {
  78. cipherData, err := repository.Encrypt(ki.ClientCertificateData, key)
  79. if err != nil {
  80. return err
  81. }
  82. ki.ClientCertificateData = cipherData
  83. }
  84. if len(ki.ClientKeyData) > 0 {
  85. cipherData, err := repository.Encrypt(ki.ClientKeyData, key)
  86. if err != nil {
  87. return err
  88. }
  89. ki.ClientKeyData = cipherData
  90. }
  91. if len(ki.Token) > 0 {
  92. cipherData, err := repository.Encrypt(ki.Token, key)
  93. if err != nil {
  94. return err
  95. }
  96. ki.Token = cipherData
  97. }
  98. if len(ki.Username) > 0 {
  99. cipherData, err := repository.Encrypt(ki.Username, key)
  100. if err != nil {
  101. return err
  102. }
  103. ki.Username = cipherData
  104. }
  105. if len(ki.Password) > 0 {
  106. cipherData, err := repository.Encrypt(ki.Password, key)
  107. if err != nil {
  108. return err
  109. }
  110. ki.Password = cipherData
  111. }
  112. if len(ki.Kubeconfig) > 0 {
  113. cipherData, err := repository.Encrypt(ki.Kubeconfig, key)
  114. if err != nil {
  115. return err
  116. }
  117. ki.Kubeconfig = cipherData
  118. }
  119. return nil
  120. }
  121. // DecryptKubeIntegrationData will decrypt the kube integration data before
  122. // returning it from the DB
  123. func (repo *KubeIntegrationRepository) DecryptKubeIntegrationData(
  124. ki *ints.KubeIntegration,
  125. key *[32]byte,
  126. ) error {
  127. if len(ki.ClientCertificateData) > 0 {
  128. plaintext, err := repository.Decrypt(ki.ClientCertificateData, key)
  129. if err != nil {
  130. return err
  131. }
  132. ki.ClientCertificateData = plaintext
  133. }
  134. if len(ki.ClientKeyData) > 0 {
  135. plaintext, err := repository.Decrypt(ki.ClientKeyData, key)
  136. if err != nil {
  137. return err
  138. }
  139. ki.ClientKeyData = plaintext
  140. }
  141. if len(ki.Token) > 0 {
  142. plaintext, err := repository.Decrypt(ki.Token, key)
  143. if err != nil {
  144. return err
  145. }
  146. ki.Token = plaintext
  147. }
  148. if len(ki.Username) > 0 {
  149. plaintext, err := repository.Decrypt(ki.Username, key)
  150. if err != nil {
  151. return err
  152. }
  153. ki.Username = plaintext
  154. }
  155. if len(ki.Password) > 0 {
  156. plaintext, err := repository.Decrypt(ki.Password, key)
  157. if err != nil {
  158. return err
  159. }
  160. ki.Password = plaintext
  161. }
  162. if len(ki.Kubeconfig) > 0 {
  163. plaintext, err := repository.Decrypt(ki.Kubeconfig, key)
  164. if err != nil {
  165. return err
  166. }
  167. ki.Kubeconfig = plaintext
  168. }
  169. return nil
  170. }
  171. // BasicIntegrationRepository uses gorm.DB for querying the database
  172. type BasicIntegrationRepository struct {
  173. db *gorm.DB
  174. key *[32]byte
  175. }
  176. // NewBasicIntegrationRepository returns a BasicIntegrationRepository which uses
  177. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  178. // sensitive data
  179. func NewBasicIntegrationRepository(
  180. db *gorm.DB,
  181. key *[32]byte,
  182. ) repository.BasicIntegrationRepository {
  183. return &BasicIntegrationRepository{db, key}
  184. }
  185. // CreateBasicIntegration creates a new basic auth mechanism
  186. func (repo *BasicIntegrationRepository) CreateBasicIntegration(
  187. am *ints.BasicIntegration,
  188. ) (*ints.BasicIntegration, error) {
  189. err := repo.EncryptBasicIntegrationData(am, repo.key)
  190. if err != nil {
  191. return nil, err
  192. }
  193. project := &models.Project{}
  194. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  195. return nil, err
  196. }
  197. assoc := repo.db.Model(&project).Association("BasicIntegrations")
  198. if assoc.Error != nil {
  199. return nil, assoc.Error
  200. }
  201. if err := assoc.Append(am); err != nil {
  202. return nil, err
  203. }
  204. return am, nil
  205. }
  206. // ReadBasicIntegration finds a basic auth mechanism by id
  207. func (repo *BasicIntegrationRepository) ReadBasicIntegration(
  208. id uint,
  209. ) (*ints.BasicIntegration, error) {
  210. basic := &ints.BasicIntegration{}
  211. if err := repo.db.Where("id = ?", id).First(&basic).Error; err != nil {
  212. return nil, err
  213. }
  214. err := repo.DecryptBasicIntegrationData(basic, repo.key)
  215. if err != nil {
  216. return nil, err
  217. }
  218. return basic, nil
  219. }
  220. // ListBasicIntegrationsByProjectID finds all basic auth mechanisms
  221. // for a given project id
  222. func (repo *BasicIntegrationRepository) ListBasicIntegrationsByProjectID(
  223. projectID uint,
  224. ) ([]*ints.BasicIntegration, error) {
  225. basics := []*ints.BasicIntegration{}
  226. if err := repo.db.Where("project_id = ?", projectID).Find(&basics).Error; err != nil {
  227. return nil, err
  228. }
  229. for _, basic := range basics {
  230. repo.DecryptBasicIntegrationData(basic, repo.key)
  231. }
  232. return basics, nil
  233. }
  234. // EncryptBasicIntegrationData will encrypt the basic integration data before
  235. // writing to the DB
  236. func (repo *BasicIntegrationRepository) EncryptBasicIntegrationData(
  237. basic *ints.BasicIntegration,
  238. key *[32]byte,
  239. ) error {
  240. if len(basic.Username) > 0 {
  241. cipherData, err := repository.Encrypt(basic.Username, key)
  242. if err != nil {
  243. return err
  244. }
  245. basic.Username = cipherData
  246. }
  247. if len(basic.Password) > 0 {
  248. cipherData, err := repository.Encrypt(basic.Password, key)
  249. if err != nil {
  250. return err
  251. }
  252. basic.Password = cipherData
  253. }
  254. return nil
  255. }
  256. // DecryptBasicIntegrationData will decrypt the basic integration data before
  257. // returning it from the DB
  258. func (repo *BasicIntegrationRepository) DecryptBasicIntegrationData(
  259. basic *ints.BasicIntegration,
  260. key *[32]byte,
  261. ) error {
  262. if len(basic.Username) > 0 {
  263. plaintext, err := repository.Decrypt(basic.Username, key)
  264. if err != nil {
  265. return err
  266. }
  267. basic.Username = plaintext
  268. }
  269. if len(basic.Password) > 0 {
  270. plaintext, err := repository.Decrypt(basic.Password, key)
  271. if err != nil {
  272. return err
  273. }
  274. basic.Password = plaintext
  275. }
  276. return nil
  277. }
  278. // OIDCIntegrationRepository uses gorm.DB for querying the database
  279. type OIDCIntegrationRepository struct {
  280. db *gorm.DB
  281. key *[32]byte
  282. }
  283. // NewOIDCIntegrationRepository returns a OIDCIntegrationRepository which uses
  284. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  285. // sensitive data
  286. func NewOIDCIntegrationRepository(
  287. db *gorm.DB,
  288. key *[32]byte,
  289. ) repository.OIDCIntegrationRepository {
  290. return &OIDCIntegrationRepository{db, key}
  291. }
  292. // CreateOIDCIntegration creates a new oidc auth mechanism
  293. func (repo *OIDCIntegrationRepository) CreateOIDCIntegration(
  294. am *ints.OIDCIntegration,
  295. ) (*ints.OIDCIntegration, error) {
  296. err := repo.EncryptOIDCIntegrationData(am, repo.key)
  297. if err != nil {
  298. return nil, err
  299. }
  300. project := &models.Project{}
  301. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  302. return nil, err
  303. }
  304. assoc := repo.db.Model(&project).Association("OIDCIntegrations")
  305. if assoc.Error != nil {
  306. return nil, assoc.Error
  307. }
  308. if err := assoc.Append(am); err != nil {
  309. return nil, err
  310. }
  311. return am, nil
  312. }
  313. // ReadOIDCIntegration finds a oidc auth mechanism by id
  314. func (repo *OIDCIntegrationRepository) ReadOIDCIntegration(
  315. id uint,
  316. ) (*ints.OIDCIntegration, error) {
  317. oidc := &ints.OIDCIntegration{}
  318. if err := repo.db.Where("id = ?", id).First(&oidc).Error; err != nil {
  319. return nil, err
  320. }
  321. err := repo.DecryptOIDCIntegrationData(oidc, repo.key)
  322. if err != nil {
  323. return nil, err
  324. }
  325. return oidc, nil
  326. }
  327. // ListOIDCIntegrationsByProjectID finds all oidc auth mechanisms
  328. // for a given project id
  329. func (repo *OIDCIntegrationRepository) ListOIDCIntegrationsByProjectID(
  330. projectID uint,
  331. ) ([]*ints.OIDCIntegration, error) {
  332. oidcs := []*ints.OIDCIntegration{}
  333. if err := repo.db.Where("project_id = ?", projectID).Find(&oidcs).Error; err != nil {
  334. return nil, err
  335. }
  336. for _, oidc := range oidcs {
  337. repo.DecryptOIDCIntegrationData(oidc, repo.key)
  338. }
  339. return oidcs, nil
  340. }
  341. // EncryptOIDCIntegrationData will encrypt the oidc integration data before
  342. // writing to the DB
  343. func (repo *OIDCIntegrationRepository) EncryptOIDCIntegrationData(
  344. oidc *ints.OIDCIntegration,
  345. key *[32]byte,
  346. ) error {
  347. if len(oidc.IssuerURL) > 0 {
  348. cipherData, err := repository.Encrypt(oidc.IssuerURL, key)
  349. if err != nil {
  350. return err
  351. }
  352. oidc.IssuerURL = cipherData
  353. }
  354. if len(oidc.ClientID) > 0 {
  355. cipherData, err := repository.Encrypt(oidc.ClientID, key)
  356. if err != nil {
  357. return err
  358. }
  359. oidc.ClientID = cipherData
  360. }
  361. if len(oidc.ClientSecret) > 0 {
  362. cipherData, err := repository.Encrypt(oidc.ClientSecret, key)
  363. if err != nil {
  364. return err
  365. }
  366. oidc.ClientSecret = cipherData
  367. }
  368. if len(oidc.CertificateAuthorityData) > 0 {
  369. cipherData, err := repository.Encrypt(oidc.CertificateAuthorityData, key)
  370. if err != nil {
  371. return err
  372. }
  373. oidc.CertificateAuthorityData = cipherData
  374. }
  375. if len(oidc.IDToken) > 0 {
  376. cipherData, err := repository.Encrypt(oidc.IDToken, key)
  377. if err != nil {
  378. return err
  379. }
  380. oidc.IDToken = cipherData
  381. }
  382. if len(oidc.RefreshToken) > 0 {
  383. cipherData, err := repository.Encrypt(oidc.RefreshToken, key)
  384. if err != nil {
  385. return err
  386. }
  387. oidc.RefreshToken = cipherData
  388. }
  389. return nil
  390. }
  391. // DecryptOIDCIntegrationData will decrypt the kube integration data before
  392. // returning it from the DB
  393. func (repo *OIDCIntegrationRepository) DecryptOIDCIntegrationData(
  394. oidc *ints.OIDCIntegration,
  395. key *[32]byte,
  396. ) error {
  397. if len(oidc.IssuerURL) > 0 {
  398. plaintext, err := repository.Decrypt(oidc.IssuerURL, key)
  399. if err != nil {
  400. return err
  401. }
  402. oidc.IssuerURL = plaintext
  403. }
  404. if len(oidc.ClientID) > 0 {
  405. plaintext, err := repository.Decrypt(oidc.ClientID, key)
  406. if err != nil {
  407. return err
  408. }
  409. oidc.ClientID = plaintext
  410. }
  411. if len(oidc.ClientSecret) > 0 {
  412. plaintext, err := repository.Decrypt(oidc.ClientSecret, key)
  413. if err != nil {
  414. return err
  415. }
  416. oidc.ClientSecret = plaintext
  417. }
  418. if len(oidc.CertificateAuthorityData) > 0 {
  419. plaintext, err := repository.Decrypt(oidc.CertificateAuthorityData, key)
  420. if err != nil {
  421. return err
  422. }
  423. oidc.CertificateAuthorityData = plaintext
  424. }
  425. if len(oidc.IDToken) > 0 {
  426. plaintext, err := repository.Decrypt(oidc.IDToken, key)
  427. if err != nil {
  428. return err
  429. }
  430. oidc.IDToken = plaintext
  431. }
  432. if len(oidc.RefreshToken) > 0 {
  433. plaintext, err := repository.Decrypt(oidc.RefreshToken, key)
  434. if err != nil {
  435. return err
  436. }
  437. oidc.RefreshToken = plaintext
  438. }
  439. return nil
  440. }
  441. // OAuthIntegrationRepository uses gorm.DB for querying the database
  442. type OAuthIntegrationRepository struct {
  443. db *gorm.DB
  444. key *[32]byte
  445. }
  446. // NewOAuthIntegrationRepository returns a OAuthIntegrationRepository which uses
  447. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  448. // sensitive data
  449. func NewOAuthIntegrationRepository(
  450. db *gorm.DB,
  451. key *[32]byte,
  452. ) repository.OAuthIntegrationRepository {
  453. return &OAuthIntegrationRepository{db, key}
  454. }
  455. // CreateOAuthIntegration creates a new oauth auth mechanism
  456. func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
  457. am *ints.OAuthIntegration,
  458. ) (*ints.OAuthIntegration, error) {
  459. err := repo.EncryptOAuthIntegrationData(am, repo.key)
  460. if err != nil {
  461. return nil, err
  462. }
  463. project := &models.Project{}
  464. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  465. return nil, err
  466. }
  467. assoc := repo.db.Model(&project).Association("OAuthIntegrations")
  468. if assoc.Error != nil {
  469. return nil, assoc.Error
  470. }
  471. if err := assoc.Append(am); err != nil {
  472. return nil, err
  473. }
  474. return am, nil
  475. }
  476. // ReadOAuthIntegration finds a oauth auth mechanism by id
  477. func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
  478. id uint,
  479. ) (*ints.OAuthIntegration, error) {
  480. oauth := &ints.OAuthIntegration{}
  481. if err := repo.db.Where("id = ?", id).First(&oauth).Error; err != nil {
  482. return nil, err
  483. }
  484. err := repo.DecryptOAuthIntegrationData(oauth, repo.key)
  485. if err != nil {
  486. return nil, err
  487. }
  488. return oauth, nil
  489. }
  490. // ListOAuthIntegrationsByProjectID finds all oauth auth mechanisms
  491. // for a given project id
  492. func (repo *OAuthIntegrationRepository) ListOAuthIntegrationsByProjectID(
  493. projectID uint,
  494. ) ([]*ints.OAuthIntegration, error) {
  495. oauths := []*ints.OAuthIntegration{}
  496. if err := repo.db.Where("project_id = ?", projectID).Find(&oauths).Error; err != nil {
  497. return nil, err
  498. }
  499. for _, oauth := range oauths {
  500. repo.DecryptOAuthIntegrationData(oauth, repo.key)
  501. }
  502. return oauths, nil
  503. }
  504. // UpdateOAuthIntegration modifies an existing oauth integration in the database
  505. func (repo *OAuthIntegrationRepository) UpdateOAuthIntegration(
  506. am *ints.OAuthIntegration,
  507. ) (*ints.OAuthIntegration, error) {
  508. err := repo.EncryptOAuthIntegrationData(am, repo.key)
  509. if err != nil {
  510. return nil, err
  511. }
  512. if err := repo.db.Save(am).Error; err != nil {
  513. return nil, err
  514. }
  515. err = repo.DecryptOAuthIntegrationData(am, repo.key)
  516. if err != nil {
  517. return nil, err
  518. }
  519. return am, nil
  520. }
  521. // EncryptOAuthIntegrationData will encrypt the oauth integration data before
  522. // writing to the DB
  523. func (repo *OAuthIntegrationRepository) EncryptOAuthIntegrationData(
  524. oauth *ints.OAuthIntegration,
  525. key *[32]byte,
  526. ) error {
  527. if len(oauth.ClientID) > 0 {
  528. cipherData, err := repository.Encrypt(oauth.ClientID, key)
  529. if err != nil {
  530. return err
  531. }
  532. oauth.ClientID = cipherData
  533. }
  534. if len(oauth.AccessToken) > 0 {
  535. cipherData, err := repository.Encrypt(oauth.AccessToken, key)
  536. if err != nil {
  537. return err
  538. }
  539. oauth.AccessToken = cipherData
  540. }
  541. if len(oauth.RefreshToken) > 0 {
  542. cipherData, err := repository.Encrypt(oauth.RefreshToken, key)
  543. if err != nil {
  544. return err
  545. }
  546. oauth.RefreshToken = cipherData
  547. }
  548. return nil
  549. }
  550. // DecryptOAuthIntegrationData will decrypt the oauth integration data before
  551. // returning it from the DB
  552. func (repo *OAuthIntegrationRepository) DecryptOAuthIntegrationData(
  553. oauth *ints.OAuthIntegration,
  554. key *[32]byte,
  555. ) error {
  556. if len(oauth.ClientID) > 0 {
  557. plaintext, err := repository.Decrypt(oauth.ClientID, key)
  558. if err != nil {
  559. return err
  560. }
  561. oauth.ClientID = plaintext
  562. }
  563. if len(oauth.AccessToken) > 0 {
  564. plaintext, err := repository.Decrypt(oauth.AccessToken, key)
  565. if err != nil {
  566. return err
  567. }
  568. oauth.AccessToken = plaintext
  569. }
  570. if len(oauth.RefreshToken) > 0 {
  571. plaintext, err := repository.Decrypt(oauth.RefreshToken, key)
  572. if err != nil {
  573. return err
  574. }
  575. oauth.RefreshToken = plaintext
  576. }
  577. return nil
  578. }
  579. // GCPIntegrationRepository uses gorm.DB for querying the database
  580. type GCPIntegrationRepository struct {
  581. db *gorm.DB
  582. key *[32]byte
  583. }
  584. // NewGCPIntegrationRepository returns a GCPIntegrationRepository which uses
  585. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  586. // sensitive data
  587. func NewGCPIntegrationRepository(
  588. db *gorm.DB,
  589. key *[32]byte,
  590. ) repository.GCPIntegrationRepository {
  591. return &GCPIntegrationRepository{db, key}
  592. }
  593. // CreateGCPIntegration creates a new gcp auth mechanism
  594. func (repo *GCPIntegrationRepository) CreateGCPIntegration(
  595. am *ints.GCPIntegration,
  596. ) (*ints.GCPIntegration, error) {
  597. err := repo.EncryptGCPIntegrationData(am, repo.key)
  598. if err != nil {
  599. return nil, err
  600. }
  601. project := &models.Project{}
  602. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  603. return nil, err
  604. }
  605. assoc := repo.db.Model(&project).Association("GCPIntegrations")
  606. if assoc.Error != nil {
  607. return nil, assoc.Error
  608. }
  609. if err := assoc.Append(am); err != nil {
  610. return nil, err
  611. }
  612. return am, nil
  613. }
  614. // ReadGCPIntegration finds a gcp auth mechanism by id
  615. func (repo *GCPIntegrationRepository) ReadGCPIntegration(
  616. id uint,
  617. ) (*ints.GCPIntegration, error) {
  618. gcp := &ints.GCPIntegration{}
  619. if err := repo.db.Where("id = ?", id).First(&gcp).Error; err != nil {
  620. return nil, err
  621. }
  622. err := repo.DecryptGCPIntegrationData(gcp, repo.key)
  623. if err != nil {
  624. return nil, err
  625. }
  626. return gcp, nil
  627. }
  628. // ListGCPIntegrationsByProjectID finds all gcp auth mechanisms
  629. // for a given project id
  630. func (repo *GCPIntegrationRepository) ListGCPIntegrationsByProjectID(
  631. projectID uint,
  632. ) ([]*ints.GCPIntegration, error) {
  633. gcps := []*ints.GCPIntegration{}
  634. if err := repo.db.Where("project_id = ?", projectID).Find(&gcps).Error; err != nil {
  635. return nil, err
  636. }
  637. for _, gcp := range gcps {
  638. repo.DecryptGCPIntegrationData(gcp, repo.key)
  639. }
  640. return gcps, nil
  641. }
  642. // EncryptGCPIntegrationData will encrypt the gcp integration data before
  643. // writing to the DB
  644. func (repo *GCPIntegrationRepository) EncryptGCPIntegrationData(
  645. gcp *ints.GCPIntegration,
  646. key *[32]byte,
  647. ) error {
  648. if len(gcp.GCPKeyData) > 0 {
  649. cipherData, err := repository.Encrypt(gcp.GCPKeyData, key)
  650. if err != nil {
  651. return err
  652. }
  653. gcp.GCPKeyData = cipherData
  654. }
  655. return nil
  656. }
  657. // DecryptGCPIntegrationData will decrypt the gcp integration data before
  658. // returning it from the DB
  659. func (repo *GCPIntegrationRepository) DecryptGCPIntegrationData(
  660. gcp *ints.GCPIntegration,
  661. key *[32]byte,
  662. ) error {
  663. if len(gcp.GCPKeyData) > 0 {
  664. plaintext, err := repository.Decrypt(gcp.GCPKeyData, key)
  665. if err != nil {
  666. return err
  667. }
  668. gcp.GCPKeyData = plaintext
  669. }
  670. return nil
  671. }
  672. // AWSIntegrationRepository uses gorm.DB for querying the database
  673. type AWSIntegrationRepository struct {
  674. db *gorm.DB
  675. key *[32]byte
  676. }
  677. // NewAWSIntegrationRepository returns a AWSIntegrationRepository which uses
  678. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  679. // sensitive data
  680. func NewAWSIntegrationRepository(
  681. db *gorm.DB,
  682. key *[32]byte,
  683. ) repository.AWSIntegrationRepository {
  684. return &AWSIntegrationRepository{db, key}
  685. }
  686. // CreateAWSIntegration creates a new aws auth mechanism
  687. func (repo *AWSIntegrationRepository) CreateAWSIntegration(
  688. am *ints.AWSIntegration,
  689. ) (*ints.AWSIntegration, error) {
  690. err := repo.EncryptAWSIntegrationData(am, repo.key)
  691. if err != nil {
  692. return nil, err
  693. }
  694. project := &models.Project{}
  695. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  696. return nil, err
  697. }
  698. assoc := repo.db.Model(&project).Association("AWSIntegrations")
  699. if assoc.Error != nil {
  700. return nil, assoc.Error
  701. }
  702. if err := assoc.Append(am); err != nil {
  703. return nil, err
  704. }
  705. return am, nil
  706. }
  707. // ReadAWSIntegration finds a aws auth mechanism by id
  708. func (repo *AWSIntegrationRepository) ReadAWSIntegration(
  709. id uint,
  710. ) (*ints.AWSIntegration, error) {
  711. aws := &ints.AWSIntegration{}
  712. if err := repo.db.Where("id = ?", id).First(&aws).Error; err != nil {
  713. return nil, err
  714. }
  715. err := repo.DecryptAWSIntegrationData(aws, repo.key)
  716. if err != nil {
  717. return nil, err
  718. }
  719. return aws, nil
  720. }
  721. // ListAWSIntegrationsByProjectID finds all aws auth mechanisms
  722. // for a given project id
  723. func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
  724. projectID uint,
  725. ) ([]*ints.AWSIntegration, error) {
  726. awss := []*ints.AWSIntegration{}
  727. if err := repo.db.Where("project_id = ?", projectID).Find(&awss).Error; err != nil {
  728. return nil, err
  729. }
  730. for _, aws := range awss {
  731. repo.DecryptAWSIntegrationData(aws, repo.key)
  732. }
  733. return awss, nil
  734. }
  735. // EncryptAWSIntegrationData will encrypt the aws integration data before
  736. // writing to the DB
  737. func (repo *AWSIntegrationRepository) EncryptAWSIntegrationData(
  738. aws *ints.AWSIntegration,
  739. key *[32]byte,
  740. ) error {
  741. if len(aws.AWSClusterID) > 0 {
  742. cipherData, err := repository.Encrypt(aws.AWSClusterID, key)
  743. if err != nil {
  744. return err
  745. }
  746. aws.AWSClusterID = cipherData
  747. }
  748. if len(aws.AWSAccessKeyID) > 0 {
  749. cipherData, err := repository.Encrypt(aws.AWSAccessKeyID, key)
  750. if err != nil {
  751. return err
  752. }
  753. aws.AWSAccessKeyID = cipherData
  754. }
  755. if len(aws.AWSSecretAccessKey) > 0 {
  756. cipherData, err := repository.Encrypt(aws.AWSSecretAccessKey, key)
  757. if err != nil {
  758. return err
  759. }
  760. aws.AWSSecretAccessKey = cipherData
  761. }
  762. if len(aws.AWSSessionToken) > 0 {
  763. cipherData, err := repository.Encrypt(aws.AWSSessionToken, key)
  764. if err != nil {
  765. return err
  766. }
  767. aws.AWSSessionToken = cipherData
  768. }
  769. return nil
  770. }
  771. // DecryptAWSIntegrationData will decrypt the aws integration data before
  772. // returning it from the DB
  773. func (repo *AWSIntegrationRepository) DecryptAWSIntegrationData(
  774. aws *ints.AWSIntegration,
  775. key *[32]byte,
  776. ) error {
  777. if len(aws.AWSClusterID) > 0 {
  778. plaintext, err := repository.Decrypt(aws.AWSClusterID, key)
  779. if err != nil {
  780. return err
  781. }
  782. aws.AWSClusterID = plaintext
  783. }
  784. if len(aws.AWSAccessKeyID) > 0 {
  785. plaintext, err := repository.Decrypt(aws.AWSAccessKeyID, key)
  786. if err != nil {
  787. return err
  788. }
  789. aws.AWSAccessKeyID = plaintext
  790. }
  791. if len(aws.AWSSecretAccessKey) > 0 {
  792. plaintext, err := repository.Decrypt(aws.AWSSecretAccessKey, key)
  793. if err != nil {
  794. return err
  795. }
  796. aws.AWSSecretAccessKey = plaintext
  797. }
  798. if len(aws.AWSSessionToken) > 0 {
  799. plaintext, err := repository.Decrypt(aws.AWSSessionToken, key)
  800. if err != nil {
  801. return err
  802. }
  803. aws.AWSSessionToken = plaintext
  804. }
  805. return nil
  806. }