auth.go 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. // UpdateCluster modifies an existing Cluster in the database
  708. func (repo *AWSIntegrationRepository) OverwriteAWSIntegration(
  709. am *ints.AWSIntegration,
  710. ) (*ints.AWSIntegration, error) {
  711. err := repo.EncryptAWSIntegrationData(am, repo.key)
  712. if err != nil {
  713. return nil, err
  714. }
  715. if err := repo.db.Save(am).Error; err != nil {
  716. return nil, err
  717. }
  718. return am, nil
  719. }
  720. // ReadAWSIntegration finds a aws auth mechanism by id
  721. func (repo *AWSIntegrationRepository) ReadAWSIntegration(
  722. id uint,
  723. ) (*ints.AWSIntegration, error) {
  724. aws := &ints.AWSIntegration{}
  725. if err := repo.db.Where("id = ?", id).First(&aws).Error; err != nil {
  726. return nil, err
  727. }
  728. err := repo.DecryptAWSIntegrationData(aws, repo.key)
  729. if err != nil {
  730. return nil, err
  731. }
  732. return aws, nil
  733. }
  734. // ListAWSIntegrationsByProjectID finds all aws auth mechanisms
  735. // for a given project id
  736. func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
  737. projectID uint,
  738. ) ([]*ints.AWSIntegration, error) {
  739. awss := []*ints.AWSIntegration{}
  740. if err := repo.db.Where("project_id = ?", projectID).Find(&awss).Error; err != nil {
  741. return nil, err
  742. }
  743. for _, aws := range awss {
  744. repo.DecryptAWSIntegrationData(aws, repo.key)
  745. }
  746. return awss, nil
  747. }
  748. // EncryptAWSIntegrationData will encrypt the aws integration data before
  749. // writing to the DB
  750. func (repo *AWSIntegrationRepository) EncryptAWSIntegrationData(
  751. aws *ints.AWSIntegration,
  752. key *[32]byte,
  753. ) error {
  754. if len(aws.AWSClusterID) > 0 {
  755. cipherData, err := repository.Encrypt(aws.AWSClusterID, key)
  756. if err != nil {
  757. return err
  758. }
  759. aws.AWSClusterID = cipherData
  760. }
  761. if len(aws.AWSAccessKeyID) > 0 {
  762. cipherData, err := repository.Encrypt(aws.AWSAccessKeyID, key)
  763. if err != nil {
  764. return err
  765. }
  766. aws.AWSAccessKeyID = cipherData
  767. }
  768. if len(aws.AWSSecretAccessKey) > 0 {
  769. cipherData, err := repository.Encrypt(aws.AWSSecretAccessKey, key)
  770. if err != nil {
  771. return err
  772. }
  773. aws.AWSSecretAccessKey = cipherData
  774. }
  775. if len(aws.AWSSessionToken) > 0 {
  776. cipherData, err := repository.Encrypt(aws.AWSSessionToken, key)
  777. if err != nil {
  778. return err
  779. }
  780. aws.AWSSessionToken = cipherData
  781. }
  782. return nil
  783. }
  784. // DecryptAWSIntegrationData will decrypt the aws integration data before
  785. // returning it from the DB
  786. func (repo *AWSIntegrationRepository) DecryptAWSIntegrationData(
  787. aws *ints.AWSIntegration,
  788. key *[32]byte,
  789. ) error {
  790. if len(aws.AWSClusterID) > 0 {
  791. plaintext, err := repository.Decrypt(aws.AWSClusterID, key)
  792. if err != nil {
  793. return err
  794. }
  795. aws.AWSClusterID = plaintext
  796. }
  797. if len(aws.AWSAccessKeyID) > 0 {
  798. plaintext, err := repository.Decrypt(aws.AWSAccessKeyID, key)
  799. if err != nil {
  800. return err
  801. }
  802. aws.AWSAccessKeyID = plaintext
  803. }
  804. if len(aws.AWSSecretAccessKey) > 0 {
  805. plaintext, err := repository.Decrypt(aws.AWSSecretAccessKey, key)
  806. if err != nil {
  807. return err
  808. }
  809. aws.AWSSecretAccessKey = plaintext
  810. }
  811. if len(aws.AWSSessionToken) > 0 {
  812. plaintext, err := repository.Decrypt(aws.AWSSessionToken, key)
  813. if err != nil {
  814. return err
  815. }
  816. aws.AWSSessionToken = plaintext
  817. }
  818. return nil
  819. }
  820. // GithubAppInstallationRepository implements repository.GithubAppInstallationRepository
  821. type GithubAppInstallationRepository struct {
  822. db *gorm.DB
  823. }
  824. // NewGithubAppInstallationRepository creates a new GithubAppInstallationRepository
  825. func NewGithubAppInstallationRepository(db *gorm.DB) repository.GithubAppInstallationRepository {
  826. return &GithubAppInstallationRepository{db}
  827. }
  828. // CreateGithubAppInstallation creates a new GithubAppInstallation instance
  829. func (repo *GithubAppInstallationRepository) CreateGithubAppInstallation(am *ints.GithubAppInstallation) (*ints.GithubAppInstallation, error) {
  830. if err := repo.db.Create(am).Error; err != nil {
  831. return nil, err
  832. }
  833. return am, nil
  834. }
  835. // ReadGithubAppInstallation finds a GithubAppInstallation by id
  836. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallation(id uint) (*ints.GithubAppInstallation, error) {
  837. ret := &ints.GithubAppInstallation{}
  838. if err := repo.db.Where("id = ?", id).First(&ret).Error; err != nil {
  839. return nil, err
  840. }
  841. return ret, nil
  842. }
  843. // ReadGithubAppInstallationByAccountID finds a GithubAppInstallation by an account ID
  844. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
  845. ret := &ints.GithubAppInstallation{}
  846. if err := repo.db.Where("account_id = ?", accountID).First(&ret).Error; err != nil {
  847. return nil, err
  848. }
  849. return ret, nil
  850. }
  851. // ReadGithubAppInstallationByAccountIDs finds all instances of GithubInstallations given a list of account IDs
  852. // note that if there is not Installation for a given ID, no error will be generated
  853. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountIDs(accountIDs []int64) ([]*ints.GithubAppInstallation, error) {
  854. ret := make([]*ints.GithubAppInstallation, 0)
  855. if err := repo.db.Where("account_id IN ?", accountIDs).Find(&ret).Error; err != nil {
  856. return nil, err
  857. }
  858. return ret, nil
  859. }
  860. // DeleteGithubAppInstallationByAccountID deletes a GithubAppInstallation given an account ID
  861. // note that this deletion is done with db.Unscoped(), so the record is actually deleted
  862. func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccountID(accountID int64) error {
  863. if err := repo.db.Unscoped().Where("account_id = ?", accountID).Delete(&ints.GithubAppInstallation{}).Error; err != nil {
  864. return err
  865. }
  866. return nil
  867. }
  868. // GithubAppOAuthIntegrationRepository implements repository.GithubAppOAuthIntegrationRepository
  869. type GithubAppOAuthIntegrationRepository struct {
  870. db *gorm.DB
  871. }
  872. // NewGithubAppOAuthIntegrationRepository creates a GithubAppOAuthIntegrationRepository
  873. func NewGithubAppOAuthIntegrationRepository(db *gorm.DB) repository.GithubAppOAuthIntegrationRepository {
  874. return &GithubAppOAuthIntegrationRepository{db}
  875. }
  876. // CreateGithubAppOAuthIntegration creates a new GithubAppOAuthIntegration
  877. func (repo *GithubAppOAuthIntegrationRepository) CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  878. if err := repo.db.Create(am).Error; err != nil {
  879. return nil, err
  880. }
  881. return am, nil
  882. }
  883. // ReadGithubAppOauthIntegration finds a GithubAppOauthIntegration by id
  884. func (repo *GithubAppOAuthIntegrationRepository) ReadGithubAppOauthIntegration(id uint) (*ints.GithubAppOAuthIntegration, error) {
  885. ret := &ints.GithubAppOAuthIntegration{}
  886. if err := repo.db.Where("id = ?", id).First(&ret).Error; err != nil {
  887. return nil, err
  888. }
  889. return ret, nil
  890. }
  891. // UpdateGithubAppOauthIntegration updates a GithubAppOauthIntegration
  892. func (repo *GithubAppOAuthIntegrationRepository) UpdateGithubAppOauthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  893. err := repo.db.Save(am).Error
  894. if err != nil {
  895. return nil, err
  896. }
  897. return am, nil
  898. }