auth.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. package gorm
  2. import (
  3. "github.com/porter-dev/porter/internal/encryption"
  4. "github.com/porter-dev/porter/internal/models"
  5. "github.com/porter-dev/porter/internal/repository"
  6. "github.com/porter-dev/porter/internal/repository/credentials"
  7. "gorm.io/gorm"
  8. ints "github.com/porter-dev/porter/internal/models/integrations"
  9. )
  10. // KubeIntegrationRepository uses gorm.DB for querying the database
  11. type KubeIntegrationRepository struct {
  12. db *gorm.DB
  13. key *[32]byte
  14. }
  15. // NewKubeIntegrationRepository returns a KubeIntegrationRepository which uses
  16. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  17. // sensitive data
  18. func NewKubeIntegrationRepository(
  19. db *gorm.DB,
  20. key *[32]byte,
  21. ) repository.KubeIntegrationRepository {
  22. return &KubeIntegrationRepository{db, key}
  23. }
  24. // CreateKubeIntegration creates a new kube auth mechanism
  25. func (repo *KubeIntegrationRepository) CreateKubeIntegration(
  26. am *ints.KubeIntegration,
  27. ) (*ints.KubeIntegration, error) {
  28. err := repo.EncryptKubeIntegrationData(am, repo.key)
  29. if err != nil {
  30. return nil, err
  31. }
  32. project := &models.Project{}
  33. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  34. return nil, err
  35. }
  36. assoc := repo.db.Model(&project).Association("KubeIntegrations")
  37. if assoc.Error != nil {
  38. return nil, assoc.Error
  39. }
  40. if err := assoc.Append(am); err != nil {
  41. return nil, err
  42. }
  43. return am, nil
  44. }
  45. // ReadKubeIntegration finds a kube auth mechanism by id
  46. func (repo *KubeIntegrationRepository) ReadKubeIntegration(
  47. projectID, id uint,
  48. ) (*ints.KubeIntegration, error) {
  49. ki := &ints.KubeIntegration{}
  50. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&ki).Error; err != nil {
  51. return nil, err
  52. }
  53. err := repo.DecryptKubeIntegrationData(ki, repo.key)
  54. if err != nil {
  55. return nil, err
  56. }
  57. return ki, nil
  58. }
  59. // ListKubeIntegrationsByProjectID finds all kube auth mechanisms
  60. // for a given project id
  61. func (repo *KubeIntegrationRepository) ListKubeIntegrationsByProjectID(
  62. projectID uint,
  63. ) ([]*ints.KubeIntegration, error) {
  64. kis := []*ints.KubeIntegration{}
  65. if err := repo.db.Where("project_id = ?", projectID).Find(&kis).Error; err != nil {
  66. return nil, err
  67. }
  68. return kis, nil
  69. }
  70. // EncryptKubeIntegrationData will encrypt the kube integration data before
  71. // writing to the DB
  72. func (repo *KubeIntegrationRepository) EncryptKubeIntegrationData(
  73. ki *ints.KubeIntegration,
  74. key *[32]byte,
  75. ) error {
  76. if len(ki.ClientCertificateData) > 0 {
  77. cipherData, err := encryption.Encrypt(ki.ClientCertificateData, key)
  78. if err != nil {
  79. return err
  80. }
  81. ki.ClientCertificateData = cipherData
  82. }
  83. if len(ki.ClientKeyData) > 0 {
  84. cipherData, err := encryption.Encrypt(ki.ClientKeyData, key)
  85. if err != nil {
  86. return err
  87. }
  88. ki.ClientKeyData = cipherData
  89. }
  90. if len(ki.Token) > 0 {
  91. cipherData, err := encryption.Encrypt(ki.Token, key)
  92. if err != nil {
  93. return err
  94. }
  95. ki.Token = cipherData
  96. }
  97. if len(ki.Username) > 0 {
  98. cipherData, err := encryption.Encrypt(ki.Username, key)
  99. if err != nil {
  100. return err
  101. }
  102. ki.Username = cipherData
  103. }
  104. if len(ki.Password) > 0 {
  105. cipherData, err := encryption.Encrypt(ki.Password, key)
  106. if err != nil {
  107. return err
  108. }
  109. ki.Password = cipherData
  110. }
  111. if len(ki.Kubeconfig) > 0 {
  112. cipherData, err := encryption.Encrypt(ki.Kubeconfig, key)
  113. if err != nil {
  114. return err
  115. }
  116. ki.Kubeconfig = cipherData
  117. }
  118. return nil
  119. }
  120. // DecryptKubeIntegrationData will decrypt the kube integration data before
  121. // returning it from the DB
  122. func (repo *KubeIntegrationRepository) DecryptKubeIntegrationData(
  123. ki *ints.KubeIntegration,
  124. key *[32]byte,
  125. ) error {
  126. if len(ki.ClientCertificateData) > 0 {
  127. plaintext, err := encryption.Decrypt(ki.ClientCertificateData, key)
  128. if err != nil {
  129. return err
  130. }
  131. ki.ClientCertificateData = plaintext
  132. }
  133. if len(ki.ClientKeyData) > 0 {
  134. plaintext, err := encryption.Decrypt(ki.ClientKeyData, key)
  135. if err != nil {
  136. return err
  137. }
  138. ki.ClientKeyData = plaintext
  139. }
  140. if len(ki.Token) > 0 {
  141. plaintext, err := encryption.Decrypt(ki.Token, key)
  142. if err != nil {
  143. return err
  144. }
  145. ki.Token = plaintext
  146. }
  147. if len(ki.Username) > 0 {
  148. plaintext, err := encryption.Decrypt(ki.Username, key)
  149. if err != nil {
  150. return err
  151. }
  152. ki.Username = plaintext
  153. }
  154. if len(ki.Password) > 0 {
  155. plaintext, err := encryption.Decrypt(ki.Password, key)
  156. if err != nil {
  157. return err
  158. }
  159. ki.Password = plaintext
  160. }
  161. if len(ki.Kubeconfig) > 0 {
  162. plaintext, err := encryption.Decrypt(ki.Kubeconfig, key)
  163. if err != nil {
  164. return err
  165. }
  166. ki.Kubeconfig = plaintext
  167. }
  168. return nil
  169. }
  170. // BasicIntegrationRepository uses gorm.DB for querying the database
  171. type BasicIntegrationRepository struct {
  172. db *gorm.DB
  173. key *[32]byte
  174. }
  175. // NewBasicIntegrationRepository returns a BasicIntegrationRepository which uses
  176. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  177. // sensitive data
  178. func NewBasicIntegrationRepository(
  179. db *gorm.DB,
  180. key *[32]byte,
  181. ) repository.BasicIntegrationRepository {
  182. return &BasicIntegrationRepository{db, key}
  183. }
  184. // CreateBasicIntegration creates a new basic auth mechanism
  185. func (repo *BasicIntegrationRepository) CreateBasicIntegration(
  186. am *ints.BasicIntegration,
  187. ) (*ints.BasicIntegration, error) {
  188. err := repo.EncryptBasicIntegrationData(am, repo.key)
  189. if err != nil {
  190. return nil, err
  191. }
  192. project := &models.Project{}
  193. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  194. return nil, err
  195. }
  196. assoc := repo.db.Model(&project).Association("BasicIntegrations")
  197. if assoc.Error != nil {
  198. return nil, assoc.Error
  199. }
  200. if err := assoc.Append(am); err != nil {
  201. return nil, err
  202. }
  203. return am, nil
  204. }
  205. // ReadBasicIntegration finds a basic auth mechanism by id
  206. func (repo *BasicIntegrationRepository) ReadBasicIntegration(
  207. projectID, id uint,
  208. ) (*ints.BasicIntegration, error) {
  209. basic := &ints.BasicIntegration{}
  210. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&basic).Error; err != nil {
  211. return nil, err
  212. }
  213. err := repo.DecryptBasicIntegrationData(basic, repo.key)
  214. if err != nil {
  215. return nil, err
  216. }
  217. return basic, nil
  218. }
  219. // ListBasicIntegrationsByProjectID finds all basic auth mechanisms
  220. // for a given project id
  221. func (repo *BasicIntegrationRepository) ListBasicIntegrationsByProjectID(
  222. projectID uint,
  223. ) ([]*ints.BasicIntegration, error) {
  224. basics := []*ints.BasicIntegration{}
  225. if err := repo.db.Where("project_id = ?", projectID).Find(&basics).Error; err != nil {
  226. return nil, err
  227. }
  228. return basics, nil
  229. }
  230. // EncryptBasicIntegrationData will encrypt the basic integration data before
  231. // writing to the DB
  232. func (repo *BasicIntegrationRepository) EncryptBasicIntegrationData(
  233. basic *ints.BasicIntegration,
  234. key *[32]byte,
  235. ) error {
  236. if len(basic.Username) > 0 {
  237. cipherData, err := encryption.Encrypt(basic.Username, key)
  238. if err != nil {
  239. return err
  240. }
  241. basic.Username = cipherData
  242. }
  243. if len(basic.Password) > 0 {
  244. cipherData, err := encryption.Encrypt(basic.Password, key)
  245. if err != nil {
  246. return err
  247. }
  248. basic.Password = cipherData
  249. }
  250. return nil
  251. }
  252. // DecryptBasicIntegrationData will decrypt the basic integration data before
  253. // returning it from the DB
  254. func (repo *BasicIntegrationRepository) DecryptBasicIntegrationData(
  255. basic *ints.BasicIntegration,
  256. key *[32]byte,
  257. ) error {
  258. if len(basic.Username) > 0 {
  259. plaintext, err := encryption.Decrypt(basic.Username, key)
  260. if err != nil {
  261. return err
  262. }
  263. basic.Username = plaintext
  264. }
  265. if len(basic.Password) > 0 {
  266. plaintext, err := encryption.Decrypt(basic.Password, key)
  267. if err != nil {
  268. return err
  269. }
  270. basic.Password = plaintext
  271. }
  272. return nil
  273. }
  274. // OIDCIntegrationRepository uses gorm.DB for querying the database
  275. type OIDCIntegrationRepository struct {
  276. db *gorm.DB
  277. key *[32]byte
  278. }
  279. // NewOIDCIntegrationRepository returns a OIDCIntegrationRepository which uses
  280. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  281. // sensitive data
  282. func NewOIDCIntegrationRepository(
  283. db *gorm.DB,
  284. key *[32]byte,
  285. ) repository.OIDCIntegrationRepository {
  286. return &OIDCIntegrationRepository{db, key}
  287. }
  288. // CreateOIDCIntegration creates a new oidc auth mechanism
  289. func (repo *OIDCIntegrationRepository) CreateOIDCIntegration(
  290. am *ints.OIDCIntegration,
  291. ) (*ints.OIDCIntegration, error) {
  292. err := repo.EncryptOIDCIntegrationData(am, repo.key)
  293. if err != nil {
  294. return nil, err
  295. }
  296. project := &models.Project{}
  297. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  298. return nil, err
  299. }
  300. assoc := repo.db.Model(&project).Association("OIDCIntegrations")
  301. if assoc.Error != nil {
  302. return nil, assoc.Error
  303. }
  304. if err := assoc.Append(am); err != nil {
  305. return nil, err
  306. }
  307. return am, nil
  308. }
  309. // ReadOIDCIntegration finds a oidc auth mechanism by id
  310. func (repo *OIDCIntegrationRepository) ReadOIDCIntegration(
  311. projectID, id uint,
  312. ) (*ints.OIDCIntegration, error) {
  313. oidc := &ints.OIDCIntegration{}
  314. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&oidc).Error; err != nil {
  315. return nil, err
  316. }
  317. err := repo.DecryptOIDCIntegrationData(oidc, repo.key)
  318. if err != nil {
  319. return nil, err
  320. }
  321. return oidc, nil
  322. }
  323. // ListOIDCIntegrationsByProjectID finds all oidc auth mechanisms
  324. // for a given project id
  325. func (repo *OIDCIntegrationRepository) ListOIDCIntegrationsByProjectID(
  326. projectID uint,
  327. ) ([]*ints.OIDCIntegration, error) {
  328. oidcs := []*ints.OIDCIntegration{}
  329. if err := repo.db.Where("project_id = ?", projectID).Find(&oidcs).Error; err != nil {
  330. return nil, err
  331. }
  332. return oidcs, nil
  333. }
  334. // EncryptOIDCIntegrationData will encrypt the oidc integration data before
  335. // writing to the DB
  336. func (repo *OIDCIntegrationRepository) EncryptOIDCIntegrationData(
  337. oidc *ints.OIDCIntegration,
  338. key *[32]byte,
  339. ) error {
  340. if len(oidc.IssuerURL) > 0 {
  341. cipherData, err := encryption.Encrypt(oidc.IssuerURL, key)
  342. if err != nil {
  343. return err
  344. }
  345. oidc.IssuerURL = cipherData
  346. }
  347. if len(oidc.ClientID) > 0 {
  348. cipherData, err := encryption.Encrypt(oidc.ClientID, key)
  349. if err != nil {
  350. return err
  351. }
  352. oidc.ClientID = cipherData
  353. }
  354. if len(oidc.ClientSecret) > 0 {
  355. cipherData, err := encryption.Encrypt(oidc.ClientSecret, key)
  356. if err != nil {
  357. return err
  358. }
  359. oidc.ClientSecret = cipherData
  360. }
  361. if len(oidc.CertificateAuthorityData) > 0 {
  362. cipherData, err := encryption.Encrypt(oidc.CertificateAuthorityData, key)
  363. if err != nil {
  364. return err
  365. }
  366. oidc.CertificateAuthorityData = cipherData
  367. }
  368. if len(oidc.IDToken) > 0 {
  369. cipherData, err := encryption.Encrypt(oidc.IDToken, key)
  370. if err != nil {
  371. return err
  372. }
  373. oidc.IDToken = cipherData
  374. }
  375. if len(oidc.RefreshToken) > 0 {
  376. cipherData, err := encryption.Encrypt(oidc.RefreshToken, key)
  377. if err != nil {
  378. return err
  379. }
  380. oidc.RefreshToken = cipherData
  381. }
  382. return nil
  383. }
  384. // DecryptOIDCIntegrationData will decrypt the kube integration data before
  385. // returning it from the DB
  386. func (repo *OIDCIntegrationRepository) DecryptOIDCIntegrationData(
  387. oidc *ints.OIDCIntegration,
  388. key *[32]byte,
  389. ) error {
  390. if len(oidc.IssuerURL) > 0 {
  391. plaintext, err := encryption.Decrypt(oidc.IssuerURL, key)
  392. if err != nil {
  393. return err
  394. }
  395. oidc.IssuerURL = plaintext
  396. }
  397. if len(oidc.ClientID) > 0 {
  398. plaintext, err := encryption.Decrypt(oidc.ClientID, key)
  399. if err != nil {
  400. return err
  401. }
  402. oidc.ClientID = plaintext
  403. }
  404. if len(oidc.ClientSecret) > 0 {
  405. plaintext, err := encryption.Decrypt(oidc.ClientSecret, key)
  406. if err != nil {
  407. return err
  408. }
  409. oidc.ClientSecret = plaintext
  410. }
  411. if len(oidc.CertificateAuthorityData) > 0 {
  412. plaintext, err := encryption.Decrypt(oidc.CertificateAuthorityData, key)
  413. if err != nil {
  414. return err
  415. }
  416. oidc.CertificateAuthorityData = plaintext
  417. }
  418. if len(oidc.IDToken) > 0 {
  419. plaintext, err := encryption.Decrypt(oidc.IDToken, key)
  420. if err != nil {
  421. return err
  422. }
  423. oidc.IDToken = plaintext
  424. }
  425. if len(oidc.RefreshToken) > 0 {
  426. plaintext, err := encryption.Decrypt(oidc.RefreshToken, key)
  427. if err != nil {
  428. return err
  429. }
  430. oidc.RefreshToken = plaintext
  431. }
  432. return nil
  433. }
  434. // OAuthIntegrationRepository uses gorm.DB for querying the database
  435. type OAuthIntegrationRepository struct {
  436. db *gorm.DB
  437. key *[32]byte
  438. storageBackend credentials.CredentialStorage
  439. }
  440. // NewOAuthIntegrationRepository returns a OAuthIntegrationRepository which uses
  441. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  442. // sensitive data
  443. func NewOAuthIntegrationRepository(
  444. db *gorm.DB,
  445. key *[32]byte,
  446. storageBackend credentials.CredentialStorage,
  447. ) repository.OAuthIntegrationRepository {
  448. return &OAuthIntegrationRepository{db, key, storageBackend}
  449. }
  450. // CreateOAuthIntegration creates a new oauth auth mechanism
  451. func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
  452. am *ints.OAuthIntegration,
  453. ) (*ints.OAuthIntegration, error) {
  454. err := repo.EncryptOAuthIntegrationData(am, repo.key)
  455. if err != nil {
  456. return nil, err
  457. }
  458. // if storage backend is not nil, strip out credential data, which will be stored in credential
  459. // storage backend after write to DB
  460. var credentialData = &credentials.OAuthCredential{}
  461. if repo.storageBackend != nil {
  462. credentialData.AccessToken = am.AccessToken
  463. credentialData.RefreshToken = am.RefreshToken
  464. credentialData.ClientID = am.ClientID
  465. am.AccessToken = []byte{}
  466. am.RefreshToken = []byte{}
  467. am.ClientID = []byte{}
  468. }
  469. project := &models.Project{}
  470. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  471. return nil, err
  472. }
  473. assoc := repo.db.Model(&project).Association("OAuthIntegrations")
  474. if assoc.Error != nil {
  475. return nil, assoc.Error
  476. }
  477. if err := assoc.Append(am); err != nil {
  478. return nil, err
  479. }
  480. if repo.storageBackend != nil {
  481. err = repo.storageBackend.WriteOAuthCredential(am, credentialData)
  482. if err != nil {
  483. return nil, err
  484. }
  485. }
  486. return am, nil
  487. }
  488. // ReadOAuthIntegration finds a oauth auth mechanism by id
  489. func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
  490. projectID, id uint,
  491. ) (*ints.OAuthIntegration, error) {
  492. oauth := &ints.OAuthIntegration{}
  493. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&oauth).Error; err != nil {
  494. return nil, err
  495. }
  496. if repo.storageBackend != nil {
  497. credentialData, err := repo.storageBackend.GetOAuthCredential(oauth)
  498. if err != nil {
  499. return nil, err
  500. }
  501. oauth.AccessToken = credentialData.AccessToken
  502. oauth.RefreshToken = credentialData.RefreshToken
  503. oauth.ClientID = credentialData.ClientID
  504. }
  505. err := repo.DecryptOAuthIntegrationData(oauth, repo.key)
  506. if err != nil {
  507. return nil, err
  508. }
  509. return oauth, nil
  510. }
  511. // ListOAuthIntegrationsByProjectID finds all oauth auth mechanisms
  512. // for a given project id
  513. func (repo *OAuthIntegrationRepository) ListOAuthIntegrationsByProjectID(
  514. projectID uint,
  515. ) ([]*ints.OAuthIntegration, error) {
  516. oauths := []*ints.OAuthIntegration{}
  517. if err := repo.db.Where("project_id = ?", projectID).Find(&oauths).Error; err != nil {
  518. return nil, err
  519. }
  520. return oauths, nil
  521. }
  522. // UpdateOAuthIntegration modifies an existing oauth integration in the database
  523. func (repo *OAuthIntegrationRepository) UpdateOAuthIntegration(
  524. am *ints.OAuthIntegration,
  525. ) (*ints.OAuthIntegration, error) {
  526. err := repo.EncryptOAuthIntegrationData(am, repo.key)
  527. if err != nil {
  528. return nil, err
  529. }
  530. // if storage backend is not nil, strip out credential data, which will be stored in credential
  531. // storage backend after write to DB
  532. var credentialData = &credentials.OAuthCredential{}
  533. if repo.storageBackend != nil {
  534. credentialData.AccessToken = am.AccessToken
  535. credentialData.RefreshToken = am.RefreshToken
  536. credentialData.ClientID = am.ClientID
  537. am.AccessToken = []byte{}
  538. am.RefreshToken = []byte{}
  539. am.ClientID = []byte{}
  540. }
  541. if err := repo.db.Save(am).Error; err != nil {
  542. return nil, err
  543. }
  544. err = repo.DecryptOAuthIntegrationData(am, repo.key)
  545. if err != nil {
  546. return nil, err
  547. }
  548. if repo.storageBackend != nil {
  549. err = repo.storageBackend.WriteOAuthCredential(am, credentialData)
  550. if err != nil {
  551. return nil, err
  552. }
  553. }
  554. return am, nil
  555. }
  556. // EncryptOAuthIntegrationData will encrypt the oauth integration data before
  557. // writing to the DB
  558. func (repo *OAuthIntegrationRepository) EncryptOAuthIntegrationData(
  559. oauth *ints.OAuthIntegration,
  560. key *[32]byte,
  561. ) error {
  562. if len(oauth.ClientID) > 0 {
  563. cipherData, err := encryption.Encrypt(oauth.ClientID, key)
  564. if err != nil {
  565. return err
  566. }
  567. oauth.ClientID = cipherData
  568. }
  569. if len(oauth.AccessToken) > 0 {
  570. cipherData, err := encryption.Encrypt(oauth.AccessToken, key)
  571. if err != nil {
  572. return err
  573. }
  574. oauth.AccessToken = cipherData
  575. }
  576. if len(oauth.RefreshToken) > 0 {
  577. cipherData, err := encryption.Encrypt(oauth.RefreshToken, key)
  578. if err != nil {
  579. return err
  580. }
  581. oauth.RefreshToken = cipherData
  582. }
  583. return nil
  584. }
  585. // DecryptOAuthIntegrationData will decrypt the oauth integration data before
  586. // returning it from the DB
  587. func (repo *OAuthIntegrationRepository) DecryptOAuthIntegrationData(
  588. oauth *ints.OAuthIntegration,
  589. key *[32]byte,
  590. ) error {
  591. if len(oauth.ClientID) > 0 {
  592. plaintext, err := encryption.Decrypt(oauth.ClientID, key)
  593. if err != nil {
  594. return err
  595. }
  596. oauth.ClientID = plaintext
  597. }
  598. if len(oauth.AccessToken) > 0 {
  599. plaintext, err := encryption.Decrypt(oauth.AccessToken, key)
  600. if err != nil {
  601. return err
  602. }
  603. oauth.AccessToken = plaintext
  604. }
  605. if len(oauth.RefreshToken) > 0 {
  606. plaintext, err := encryption.Decrypt(oauth.RefreshToken, key)
  607. if err != nil {
  608. return err
  609. }
  610. oauth.RefreshToken = plaintext
  611. }
  612. return nil
  613. }
  614. // GCPIntegrationRepository uses gorm.DB for querying the database
  615. type GCPIntegrationRepository struct {
  616. db *gorm.DB
  617. key *[32]byte
  618. storageBackend credentials.CredentialStorage
  619. }
  620. // NewGCPIntegrationRepository returns a GCPIntegrationRepository which uses
  621. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  622. // sensitive data
  623. func NewGCPIntegrationRepository(
  624. db *gorm.DB,
  625. key *[32]byte,
  626. storageBackend credentials.CredentialStorage,
  627. ) repository.GCPIntegrationRepository {
  628. return &GCPIntegrationRepository{db, key, storageBackend}
  629. }
  630. // CreateGCPIntegration creates a new gcp auth mechanism
  631. func (repo *GCPIntegrationRepository) CreateGCPIntegration(
  632. am *ints.GCPIntegration,
  633. ) (*ints.GCPIntegration, error) {
  634. err := repo.EncryptGCPIntegrationData(am, repo.key)
  635. if err != nil {
  636. return nil, err
  637. }
  638. // if storage backend is not nil, strip out credential data, which will be stored in credential
  639. // storage backend after write to DB
  640. var credentialData = &credentials.GCPCredential{}
  641. if repo.storageBackend != nil {
  642. credentialData.GCPKeyData = am.GCPKeyData
  643. am.GCPKeyData = []byte{}
  644. }
  645. project := &models.Project{}
  646. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  647. return nil, err
  648. }
  649. assoc := repo.db.Model(&project).Association("GCPIntegrations")
  650. if assoc.Error != nil {
  651. return nil, assoc.Error
  652. }
  653. if err := assoc.Append(am); err != nil {
  654. return nil, err
  655. }
  656. if repo.storageBackend != nil {
  657. err = repo.storageBackend.WriteGCPCredential(am, credentialData)
  658. if err != nil {
  659. return nil, err
  660. }
  661. }
  662. return am, nil
  663. }
  664. // ReadGCPIntegration finds a gcp auth mechanism by id
  665. func (repo *GCPIntegrationRepository) ReadGCPIntegration(
  666. projectID, id uint,
  667. ) (*ints.GCPIntegration, error) {
  668. gcp := &ints.GCPIntegration{}
  669. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&gcp).Error; err != nil {
  670. return nil, err
  671. }
  672. if repo.storageBackend != nil {
  673. credentialData, err := repo.storageBackend.GetGCPCredential(gcp)
  674. if err != nil {
  675. return nil, err
  676. }
  677. gcp.GCPKeyData = credentialData.GCPKeyData
  678. }
  679. err := repo.DecryptGCPIntegrationData(gcp, repo.key)
  680. if err != nil {
  681. return nil, err
  682. }
  683. return gcp, nil
  684. }
  685. // ListGCPIntegrationsByProjectID finds all gcp auth mechanisms
  686. // for a given project id
  687. func (repo *GCPIntegrationRepository) ListGCPIntegrationsByProjectID(
  688. projectID uint,
  689. ) ([]*ints.GCPIntegration, error) {
  690. gcps := []*ints.GCPIntegration{}
  691. if err := repo.db.Where("project_id = ?", projectID).Find(&gcps).Error; err != nil {
  692. return nil, err
  693. }
  694. return gcps, nil
  695. }
  696. // EncryptGCPIntegrationData will encrypt the gcp integration data before
  697. // writing to the DB
  698. func (repo *GCPIntegrationRepository) EncryptGCPIntegrationData(
  699. gcp *ints.GCPIntegration,
  700. key *[32]byte,
  701. ) error {
  702. if len(gcp.GCPKeyData) > 0 {
  703. cipherData, err := encryption.Encrypt(gcp.GCPKeyData, key)
  704. if err != nil {
  705. return err
  706. }
  707. gcp.GCPKeyData = cipherData
  708. }
  709. return nil
  710. }
  711. // DecryptGCPIntegrationData will decrypt the gcp integration data before
  712. // returning it from the DB
  713. func (repo *GCPIntegrationRepository) DecryptGCPIntegrationData(
  714. gcp *ints.GCPIntegration,
  715. key *[32]byte,
  716. ) error {
  717. if len(gcp.GCPKeyData) > 0 {
  718. plaintext, err := encryption.Decrypt(gcp.GCPKeyData, key)
  719. if err != nil {
  720. return err
  721. }
  722. gcp.GCPKeyData = plaintext
  723. }
  724. return nil
  725. }
  726. // AWSIntegrationRepository uses gorm.DB for querying the database
  727. type AWSIntegrationRepository struct {
  728. db *gorm.DB
  729. key *[32]byte
  730. storageBackend credentials.CredentialStorage
  731. }
  732. // NewAWSIntegrationRepository returns a AWSIntegrationRepository which uses
  733. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  734. // sensitive data
  735. func NewAWSIntegrationRepository(
  736. db *gorm.DB,
  737. key *[32]byte,
  738. storageBackend credentials.CredentialStorage,
  739. ) repository.AWSIntegrationRepository {
  740. return &AWSIntegrationRepository{db, key, storageBackend}
  741. }
  742. // CreateAWSIntegration creates a new aws auth mechanism
  743. func (repo *AWSIntegrationRepository) CreateAWSIntegration(
  744. am *ints.AWSIntegration,
  745. ) (*ints.AWSIntegration, error) {
  746. err := repo.EncryptAWSIntegrationData(am, repo.key)
  747. if err != nil {
  748. return nil, err
  749. }
  750. // if storage backend is not nil, strip out credential data, which will be stored in credential
  751. // storage backend after write to DB
  752. var credentialData = &credentials.AWSCredential{}
  753. if repo.storageBackend != nil {
  754. credentialData.AWSAccessKeyID = am.AWSAccessKeyID
  755. credentialData.AWSClusterID = am.AWSClusterID
  756. credentialData.AWSSecretAccessKey = am.AWSSecretAccessKey
  757. credentialData.AWSSessionToken = am.AWSSessionToken
  758. am.AWSAccessKeyID = []byte{}
  759. am.AWSClusterID = []byte{}
  760. am.AWSSecretAccessKey = []byte{}
  761. am.AWSSessionToken = []byte{}
  762. }
  763. project := &models.Project{}
  764. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  765. return nil, err
  766. }
  767. assoc := repo.db.Model(&project).Association("AWSIntegrations")
  768. if assoc.Error != nil {
  769. return nil, assoc.Error
  770. }
  771. if err := assoc.Append(am); err != nil {
  772. return nil, err
  773. }
  774. if repo.storageBackend != nil {
  775. err = repo.storageBackend.WriteAWSCredential(am, credentialData)
  776. if err != nil {
  777. return nil, err
  778. }
  779. }
  780. return am, nil
  781. }
  782. // UpdateCluster modifies an existing Cluster in the database
  783. func (repo *AWSIntegrationRepository) OverwriteAWSIntegration(
  784. am *ints.AWSIntegration,
  785. ) (*ints.AWSIntegration, error) {
  786. err := repo.EncryptAWSIntegrationData(am, repo.key)
  787. if err != nil {
  788. return nil, err
  789. }
  790. // if storage backend is not nil, strip out credential data, which will be stored in credential
  791. // storage backend after write to DB
  792. var credentialData = &credentials.AWSCredential{}
  793. if repo.storageBackend != nil {
  794. credentialData.AWSAccessKeyID = am.AWSAccessKeyID
  795. credentialData.AWSClusterID = am.AWSClusterID
  796. credentialData.AWSSecretAccessKey = am.AWSSecretAccessKey
  797. credentialData.AWSSessionToken = am.AWSSessionToken
  798. am.AWSAccessKeyID = []byte{}
  799. am.AWSClusterID = []byte{}
  800. am.AWSSecretAccessKey = []byte{}
  801. am.AWSSessionToken = []byte{}
  802. }
  803. if err := repo.db.Save(am).Error; err != nil {
  804. return nil, err
  805. }
  806. if repo.storageBackend != nil {
  807. err = repo.storageBackend.WriteAWSCredential(am, credentialData)
  808. if err != nil {
  809. return nil, err
  810. }
  811. }
  812. return am, nil
  813. }
  814. // ReadAWSIntegration finds a aws auth mechanism by id
  815. func (repo *AWSIntegrationRepository) ReadAWSIntegration(
  816. projectID, id uint,
  817. ) (*ints.AWSIntegration, error) {
  818. aws := &ints.AWSIntegration{}
  819. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&aws).Error; err != nil {
  820. return nil, err
  821. }
  822. if repo.storageBackend != nil {
  823. credentialData, err := repo.storageBackend.GetAWSCredential(aws)
  824. if err != nil {
  825. return nil, err
  826. }
  827. aws.AWSAccessKeyID = credentialData.AWSAccessKeyID
  828. aws.AWSClusterID = credentialData.AWSClusterID
  829. aws.AWSSecretAccessKey = credentialData.AWSSecretAccessKey
  830. aws.AWSSessionToken = credentialData.AWSSessionToken
  831. }
  832. err := repo.DecryptAWSIntegrationData(aws, repo.key)
  833. if err != nil {
  834. return nil, err
  835. }
  836. return aws, nil
  837. }
  838. // ListAWSIntegrationsByProjectID finds all aws auth mechanisms
  839. // for a given project id
  840. func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
  841. projectID uint,
  842. ) ([]*ints.AWSIntegration, error) {
  843. awss := []*ints.AWSIntegration{}
  844. if err := repo.db.Where("project_id = ?", projectID).Find(&awss).Error; err != nil {
  845. return nil, err
  846. }
  847. return awss, nil
  848. }
  849. // EncryptAWSIntegrationData will encrypt the aws integration data before
  850. // writing to the DB
  851. func (repo *AWSIntegrationRepository) EncryptAWSIntegrationData(
  852. aws *ints.AWSIntegration,
  853. key *[32]byte,
  854. ) error {
  855. if len(aws.AWSClusterID) > 0 {
  856. cipherData, err := encryption.Encrypt(aws.AWSClusterID, key)
  857. if err != nil {
  858. return err
  859. }
  860. aws.AWSClusterID = cipherData
  861. }
  862. if len(aws.AWSAccessKeyID) > 0 {
  863. cipherData, err := encryption.Encrypt(aws.AWSAccessKeyID, key)
  864. if err != nil {
  865. return err
  866. }
  867. aws.AWSAccessKeyID = cipherData
  868. }
  869. if len(aws.AWSSecretAccessKey) > 0 {
  870. cipherData, err := encryption.Encrypt(aws.AWSSecretAccessKey, key)
  871. if err != nil {
  872. return err
  873. }
  874. aws.AWSSecretAccessKey = cipherData
  875. }
  876. if len(aws.AWSSessionToken) > 0 {
  877. cipherData, err := encryption.Encrypt(aws.AWSSessionToken, key)
  878. if err != nil {
  879. return err
  880. }
  881. aws.AWSSessionToken = cipherData
  882. }
  883. return nil
  884. }
  885. // DecryptAWSIntegrationData will decrypt the aws integration data before
  886. // returning it from the DB
  887. func (repo *AWSIntegrationRepository) DecryptAWSIntegrationData(
  888. aws *ints.AWSIntegration,
  889. key *[32]byte,
  890. ) error {
  891. if len(aws.AWSClusterID) > 0 {
  892. plaintext, err := encryption.Decrypt(aws.AWSClusterID, key)
  893. if err != nil {
  894. return err
  895. }
  896. aws.AWSClusterID = plaintext
  897. }
  898. if len(aws.AWSAccessKeyID) > 0 {
  899. plaintext, err := encryption.Decrypt(aws.AWSAccessKeyID, key)
  900. if err != nil {
  901. return err
  902. }
  903. aws.AWSAccessKeyID = plaintext
  904. }
  905. if len(aws.AWSSecretAccessKey) > 0 {
  906. plaintext, err := encryption.Decrypt(aws.AWSSecretAccessKey, key)
  907. if err != nil {
  908. return err
  909. }
  910. aws.AWSSecretAccessKey = plaintext
  911. }
  912. if len(aws.AWSSessionToken) > 0 {
  913. plaintext, err := encryption.Decrypt(aws.AWSSessionToken, key)
  914. if err != nil {
  915. return err
  916. }
  917. aws.AWSSessionToken = plaintext
  918. }
  919. return nil
  920. }
  921. // GithubAppInstallationRepository implements repository.GithubAppInstallationRepository
  922. type GithubAppInstallationRepository struct {
  923. db *gorm.DB
  924. }
  925. // NewGithubAppInstallationRepository creates a new GithubAppInstallationRepository
  926. func NewGithubAppInstallationRepository(db *gorm.DB) repository.GithubAppInstallationRepository {
  927. return &GithubAppInstallationRepository{db}
  928. }
  929. // CreateGithubAppInstallation creates a new GithubAppInstallation instance
  930. func (repo *GithubAppInstallationRepository) CreateGithubAppInstallation(am *ints.GithubAppInstallation) (*ints.GithubAppInstallation, error) {
  931. if err := repo.db.Create(am).Error; err != nil {
  932. return nil, err
  933. }
  934. return am, nil
  935. }
  936. // ReadGithubAppInstallationByInstallationID finds a GithubAppInstallation by id
  937. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByInstallationID(gaID uint) (*ints.GithubAppInstallation, error) {
  938. ret := &ints.GithubAppInstallation{}
  939. if err := repo.db.Where("installation_id = ?", gaID).First(&ret).Error; err != nil {
  940. return nil, err
  941. }
  942. return ret, nil
  943. }
  944. // ReadGithubAppInstallationByAccountID finds a GithubAppInstallation by an account ID
  945. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
  946. ret := &ints.GithubAppInstallation{}
  947. if err := repo.db.Where("account_id = ?", accountID).First(&ret).Error; err != nil {
  948. return nil, err
  949. }
  950. return ret, nil
  951. }
  952. // ReadGithubAppInstallationByAccountIDs finds all instances of GithubInstallations given a list of account IDs
  953. // note that if there is not Installation for a given ID, no error will be generated
  954. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountIDs(accountIDs []int64) ([]*ints.GithubAppInstallation, error) {
  955. ret := make([]*ints.GithubAppInstallation, 0)
  956. if err := repo.db.Where("account_id IN ?", accountIDs).Find(&ret).Error; err != nil {
  957. return nil, err
  958. }
  959. return ret, nil
  960. }
  961. // DeleteGithubAppInstallationByAccountID deletes a GithubAppInstallation given an account ID
  962. // note that this deletion is done with db.Unscoped(), so the record is actually deleted
  963. func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccountID(accountID int64) error {
  964. if err := repo.db.Unscoped().Where("account_id = ?", accountID).Delete(&ints.GithubAppInstallation{}).Error; err != nil {
  965. return err
  966. }
  967. return nil
  968. }
  969. // GithubAppOAuthIntegrationRepository implements repository.GithubAppOAuthIntegrationRepository
  970. type GithubAppOAuthIntegrationRepository struct {
  971. db *gorm.DB
  972. }
  973. // NewGithubAppOAuthIntegrationRepository creates a GithubAppOAuthIntegrationRepository
  974. func NewGithubAppOAuthIntegrationRepository(db *gorm.DB) repository.GithubAppOAuthIntegrationRepository {
  975. return &GithubAppOAuthIntegrationRepository{db}
  976. }
  977. // CreateGithubAppOAuthIntegration creates a new GithubAppOAuthIntegration
  978. func (repo *GithubAppOAuthIntegrationRepository) CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  979. if err := repo.db.Create(am).Error; err != nil {
  980. return nil, err
  981. }
  982. return am, nil
  983. }
  984. // ReadGithubAppOauthIntegration finds a GithubAppOauthIntegration by id
  985. func (repo *GithubAppOAuthIntegrationRepository) ReadGithubAppOauthIntegration(id uint) (*ints.GithubAppOAuthIntegration, error) {
  986. ret := &ints.GithubAppOAuthIntegration{}
  987. if err := repo.db.Where("id = ?", id).First(&ret).Error; err != nil {
  988. return nil, err
  989. }
  990. return ret, nil
  991. }
  992. // UpdateGithubAppOauthIntegration updates a GithubAppOauthIntegration
  993. func (repo *GithubAppOAuthIntegrationRepository) UpdateGithubAppOauthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  994. err := repo.db.Save(am).Error
  995. if err != nil {
  996. return nil, err
  997. }
  998. return am, nil
  999. }
  1000. // AzureIntegrationRepository uses gorm.DB for querying the database
  1001. type AzureIntegrationRepository struct {
  1002. db *gorm.DB
  1003. key *[32]byte
  1004. storageBackend credentials.CredentialStorage
  1005. }
  1006. // NewAzureIntegrationRepository returns a AzureIntegrationRepository which uses
  1007. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  1008. // sensitive data
  1009. func NewAzureIntegrationRepository(
  1010. db *gorm.DB,
  1011. key *[32]byte,
  1012. storageBackend credentials.CredentialStorage,
  1013. ) repository.AzureIntegrationRepository {
  1014. return &AzureIntegrationRepository{db, key, storageBackend}
  1015. }
  1016. // CreateAzureIntegration creates a new Azure auth mechanism
  1017. func (repo *AzureIntegrationRepository) CreateAzureIntegration(
  1018. az *ints.AzureIntegration,
  1019. ) (*ints.AzureIntegration, error) {
  1020. err := repo.EncryptAzureIntegrationData(az, repo.key)
  1021. if err != nil {
  1022. return nil, err
  1023. }
  1024. // if storage backend is not nil, strip out credential data, which will be stored in credential
  1025. // storage backend after write to DB
  1026. var credentialData = &credentials.AzureCredential{}
  1027. if repo.storageBackend != nil {
  1028. credentialData.ServicePrincipalSecret = az.ServicePrincipalSecret
  1029. credentialData.ACRPassword1 = az.ACRPassword1
  1030. credentialData.ACRPassword2 = az.ACRPassword2
  1031. az.ServicePrincipalSecret = []byte{}
  1032. az.ACRPassword1 = []byte{}
  1033. az.ACRPassword2 = []byte{}
  1034. }
  1035. project := &models.Project{}
  1036. if err := repo.db.Where("id = ?", az.ProjectID).First(&project).Error; err != nil {
  1037. return nil, err
  1038. }
  1039. assoc := repo.db.Model(&project).Association("AzureIntegrations")
  1040. if assoc.Error != nil {
  1041. return nil, assoc.Error
  1042. }
  1043. if err := assoc.Append(az); err != nil {
  1044. return nil, err
  1045. }
  1046. if repo.storageBackend != nil {
  1047. err = repo.storageBackend.WriteAzureCredential(az, credentialData)
  1048. if err != nil {
  1049. return nil, err
  1050. }
  1051. }
  1052. return az, nil
  1053. }
  1054. // OverwriteAzureIntegration overwrites the Azure credential in the DB
  1055. func (repo *AzureIntegrationRepository) OverwriteAzureIntegration(
  1056. az *ints.AzureIntegration,
  1057. ) (*ints.AzureIntegration, error) {
  1058. err := repo.EncryptAzureIntegrationData(az, repo.key)
  1059. if err != nil {
  1060. return nil, err
  1061. }
  1062. // if storage backend is not nil, strip out credential data, which will be stored in credential
  1063. // storage backend after write to DB
  1064. var credentialData = &credentials.AzureCredential{}
  1065. if repo.storageBackend != nil {
  1066. credentialData.ServicePrincipalSecret = az.ServicePrincipalSecret
  1067. credentialData.ACRPassword1 = az.ACRPassword1
  1068. credentialData.ACRPassword2 = az.ACRPassword2
  1069. az.ServicePrincipalSecret = []byte{}
  1070. az.ACRPassword1 = []byte{}
  1071. az.ACRPassword2 = []byte{}
  1072. }
  1073. if err := repo.db.Save(az).Error; err != nil {
  1074. return nil, err
  1075. }
  1076. if repo.storageBackend != nil {
  1077. err = repo.storageBackend.WriteAzureCredential(az, credentialData)
  1078. if err != nil {
  1079. return nil, err
  1080. }
  1081. }
  1082. err = repo.DecryptAzureIntegrationData(az, repo.key)
  1083. if err != nil {
  1084. return nil, err
  1085. }
  1086. return az, nil
  1087. }
  1088. // ReadAzureIntegration finds a Azure auth mechanism by id
  1089. func (repo *AzureIntegrationRepository) ReadAzureIntegration(
  1090. projectID, id uint,
  1091. ) (*ints.AzureIntegration, error) {
  1092. az := &ints.AzureIntegration{}
  1093. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&az).Error; err != nil {
  1094. return nil, err
  1095. }
  1096. if repo.storageBackend != nil {
  1097. credentialData, err := repo.storageBackend.GetAzureCredential(az)
  1098. if err != nil {
  1099. return nil, err
  1100. }
  1101. az.ServicePrincipalSecret = credentialData.ServicePrincipalSecret
  1102. az.ACRPassword1 = credentialData.ACRPassword1
  1103. az.ACRPassword2 = credentialData.ACRPassword2
  1104. }
  1105. err := repo.DecryptAzureIntegrationData(az, repo.key)
  1106. if err != nil {
  1107. return nil, err
  1108. }
  1109. return az, nil
  1110. }
  1111. // ListAzureIntegrationsByProjectID finds all Azure auth mechanisms
  1112. // for a given project id
  1113. func (repo *AzureIntegrationRepository) ListAzureIntegrationsByProjectID(
  1114. projectID uint,
  1115. ) ([]*ints.AzureIntegration, error) {
  1116. azs := []*ints.AzureIntegration{}
  1117. if err := repo.db.Where("project_id = ?", projectID).Find(&azs).Error; err != nil {
  1118. return nil, err
  1119. }
  1120. return azs, nil
  1121. }
  1122. // EncryptAWSIntegrationData will encrypt the aws integration data before
  1123. // writing to the DB
  1124. func (repo *AzureIntegrationRepository) EncryptAzureIntegrationData(
  1125. az *ints.AzureIntegration,
  1126. key *[32]byte,
  1127. ) error {
  1128. if len(az.ServicePrincipalSecret) > 0 {
  1129. cipherData, err := encryption.Encrypt(az.ServicePrincipalSecret, key)
  1130. if err != nil {
  1131. return err
  1132. }
  1133. az.ServicePrincipalSecret = cipherData
  1134. }
  1135. if len(az.ACRPassword1) > 0 {
  1136. cipherData, err := encryption.Encrypt(az.ACRPassword1, key)
  1137. if err != nil {
  1138. return err
  1139. }
  1140. az.ACRPassword1 = cipherData
  1141. }
  1142. if len(az.ACRPassword2) > 0 {
  1143. cipherData, err := encryption.Encrypt(az.ACRPassword2, key)
  1144. if err != nil {
  1145. return err
  1146. }
  1147. az.ACRPassword2 = cipherData
  1148. }
  1149. return nil
  1150. }
  1151. // DecryptAzureIntegrationData will decrypt the Azure integration data before
  1152. // returning it from the DB
  1153. func (repo *AzureIntegrationRepository) DecryptAzureIntegrationData(
  1154. az *ints.AzureIntegration,
  1155. key *[32]byte,
  1156. ) error {
  1157. if len(az.ServicePrincipalSecret) > 0 {
  1158. plaintext, err := encryption.Decrypt(az.ServicePrincipalSecret, key)
  1159. if err != nil {
  1160. return err
  1161. }
  1162. az.ServicePrincipalSecret = plaintext
  1163. }
  1164. if len(az.ACRPassword1) > 0 {
  1165. plaintext, err := encryption.Decrypt(az.ACRPassword1, key)
  1166. if err != nil {
  1167. return err
  1168. }
  1169. az.ACRPassword1 = plaintext
  1170. }
  1171. if len(az.ACRPassword2) > 0 {
  1172. plaintext, err := encryption.Decrypt(az.ACRPassword2, key)
  1173. if err != nil {
  1174. return err
  1175. }
  1176. az.ACRPassword2 = plaintext
  1177. }
  1178. return nil
  1179. }