auth.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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. credentialData.AKSPassword = az.AKSPassword
  1032. az.ServicePrincipalSecret = []byte{}
  1033. az.ACRPassword1 = []byte{}
  1034. az.ACRPassword2 = []byte{}
  1035. az.AKSPassword = []byte{}
  1036. }
  1037. project := &models.Project{}
  1038. if err := repo.db.Where("id = ?", az.ProjectID).First(&project).Error; err != nil {
  1039. return nil, err
  1040. }
  1041. assoc := repo.db.Model(&project).Association("AzureIntegrations")
  1042. if assoc.Error != nil {
  1043. return nil, assoc.Error
  1044. }
  1045. if err := assoc.Append(az); err != nil {
  1046. return nil, err
  1047. }
  1048. if repo.storageBackend != nil {
  1049. err = repo.storageBackend.WriteAzureCredential(az, credentialData)
  1050. if err != nil {
  1051. return nil, err
  1052. }
  1053. }
  1054. return az, nil
  1055. }
  1056. // OverwriteAzureIntegration overwrites the Azure credential in the DB
  1057. func (repo *AzureIntegrationRepository) OverwriteAzureIntegration(
  1058. az *ints.AzureIntegration,
  1059. ) (*ints.AzureIntegration, error) {
  1060. err := repo.EncryptAzureIntegrationData(az, repo.key)
  1061. if err != nil {
  1062. return nil, err
  1063. }
  1064. // if storage backend is not nil, strip out credential data, which will be stored in credential
  1065. // storage backend after write to DB
  1066. var credentialData = &credentials.AzureCredential{}
  1067. if repo.storageBackend != nil {
  1068. credentialData.ServicePrincipalSecret = az.ServicePrincipalSecret
  1069. credentialData.ACRPassword1 = az.ACRPassword1
  1070. credentialData.ACRPassword2 = az.ACRPassword2
  1071. credentialData.AKSPassword = az.AKSPassword
  1072. az.ServicePrincipalSecret = []byte{}
  1073. az.ACRPassword1 = []byte{}
  1074. az.ACRPassword2 = []byte{}
  1075. az.AKSPassword = []byte{}
  1076. }
  1077. if err := repo.db.Save(az).Error; err != nil {
  1078. return nil, err
  1079. }
  1080. if repo.storageBackend != nil {
  1081. err = repo.storageBackend.WriteAzureCredential(az, credentialData)
  1082. if err != nil {
  1083. return nil, err
  1084. }
  1085. }
  1086. // perform another read
  1087. return repo.ReadAzureIntegration(az.ProjectID, az.ID)
  1088. }
  1089. // ReadAzureIntegration finds a Azure auth mechanism by id
  1090. func (repo *AzureIntegrationRepository) ReadAzureIntegration(
  1091. projectID, id uint,
  1092. ) (*ints.AzureIntegration, error) {
  1093. az := &ints.AzureIntegration{}
  1094. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&az).Error; err != nil {
  1095. return nil, err
  1096. }
  1097. if repo.storageBackend != nil {
  1098. credentialData, err := repo.storageBackend.GetAzureCredential(az)
  1099. if err != nil {
  1100. return nil, err
  1101. }
  1102. az.ServicePrincipalSecret = credentialData.ServicePrincipalSecret
  1103. az.ACRPassword1 = credentialData.ACRPassword1
  1104. az.ACRPassword2 = credentialData.ACRPassword2
  1105. az.AKSPassword = credentialData.AKSPassword
  1106. }
  1107. err := repo.DecryptAzureIntegrationData(az, repo.key)
  1108. if err != nil {
  1109. return nil, err
  1110. }
  1111. return az, nil
  1112. }
  1113. // ListAzureIntegrationsByProjectID finds all Azure auth mechanisms
  1114. // for a given project id
  1115. func (repo *AzureIntegrationRepository) ListAzureIntegrationsByProjectID(
  1116. projectID uint,
  1117. ) ([]*ints.AzureIntegration, error) {
  1118. azs := []*ints.AzureIntegration{}
  1119. if err := repo.db.Where("project_id = ?", projectID).Find(&azs).Error; err != nil {
  1120. return nil, err
  1121. }
  1122. return azs, nil
  1123. }
  1124. // EncryptAWSIntegrationData will encrypt the aws integration data before
  1125. // writing to the DB
  1126. func (repo *AzureIntegrationRepository) EncryptAzureIntegrationData(
  1127. az *ints.AzureIntegration,
  1128. key *[32]byte,
  1129. ) error {
  1130. if len(az.ServicePrincipalSecret) > 0 {
  1131. cipherData, err := encryption.Encrypt(az.ServicePrincipalSecret, key)
  1132. if err != nil {
  1133. return err
  1134. }
  1135. az.ServicePrincipalSecret = cipherData
  1136. }
  1137. if len(az.ACRPassword1) > 0 {
  1138. cipherData, err := encryption.Encrypt(az.ACRPassword1, key)
  1139. if err != nil {
  1140. return err
  1141. }
  1142. az.ACRPassword1 = cipherData
  1143. }
  1144. if len(az.ACRPassword2) > 0 {
  1145. cipherData, err := encryption.Encrypt(az.ACRPassword2, key)
  1146. if err != nil {
  1147. return err
  1148. }
  1149. az.ACRPassword2 = cipherData
  1150. }
  1151. if len(az.AKSPassword) > 0 {
  1152. cipherData, err := encryption.Encrypt(az.AKSPassword, key)
  1153. if err != nil {
  1154. return err
  1155. }
  1156. az.AKSPassword = cipherData
  1157. }
  1158. return nil
  1159. }
  1160. // DecryptAzureIntegrationData will decrypt the Azure integration data before
  1161. // returning it from the DB
  1162. func (repo *AzureIntegrationRepository) DecryptAzureIntegrationData(
  1163. az *ints.AzureIntegration,
  1164. key *[32]byte,
  1165. ) error {
  1166. if len(az.ServicePrincipalSecret) > 0 {
  1167. plaintext, err := encryption.Decrypt(az.ServicePrincipalSecret, key)
  1168. if err != nil {
  1169. return err
  1170. }
  1171. az.ServicePrincipalSecret = plaintext
  1172. }
  1173. if len(az.ACRPassword1) > 0 {
  1174. plaintext, err := encryption.Decrypt(az.ACRPassword1, key)
  1175. if err != nil {
  1176. return err
  1177. }
  1178. az.ACRPassword1 = plaintext
  1179. }
  1180. if len(az.ACRPassword2) > 0 {
  1181. plaintext, err := encryption.Decrypt(az.ACRPassword2, key)
  1182. if err != nil {
  1183. return err
  1184. }
  1185. az.ACRPassword2 = plaintext
  1186. }
  1187. if len(az.AKSPassword) > 0 {
  1188. plaintext, err := encryption.Decrypt(az.AKSPassword, key)
  1189. if err != nil {
  1190. return err
  1191. }
  1192. az.AKSPassword = plaintext
  1193. }
  1194. return nil
  1195. }
  1196. // GitlabIntegrationRepository uses gorm.DB for querying the database
  1197. type GitlabIntegrationRepository struct {
  1198. db *gorm.DB
  1199. key *[32]byte
  1200. storageBackend credentials.CredentialStorage
  1201. }
  1202. // NewGitlabIntegrationRepository returns a GitlabIntegrationRepository which uses
  1203. // gorm.DB for querying the database
  1204. func NewGitlabIntegrationRepository(
  1205. db *gorm.DB,
  1206. key *[32]byte,
  1207. storageBackend credentials.CredentialStorage,
  1208. ) repository.GitlabIntegrationRepository {
  1209. return &GitlabIntegrationRepository{db, key, storageBackend}
  1210. }
  1211. // CreateIntegration adds a new GitlabIntegration row to the gitlab_integration table in the database
  1212. func (repo *GitlabIntegrationRepository) CreateGitlabIntegration(gi *ints.GitlabIntegration) (*ints.GitlabIntegration, error) {
  1213. err := repo.EncryptGitlabIntegrationData(gi, repo.key)
  1214. if err != nil {
  1215. return nil, err
  1216. }
  1217. // if storage backend is not nil, strip out credential data, which will be stored in credential
  1218. // storage backend after write to DB
  1219. var credentialData = &credentials.GitlabCredential{}
  1220. if repo.storageBackend != nil {
  1221. credentialData.AppClientID = gi.AppClientID
  1222. credentialData.AppClientSecret = gi.AppClientSecret
  1223. gi.AppClientID = []byte{}
  1224. gi.AppClientSecret = []byte{}
  1225. }
  1226. project := &models.Project{}
  1227. if err := repo.db.Where("id = ?", gi.ProjectID).First(&project).Error; err != nil {
  1228. return nil, err
  1229. }
  1230. assoc := repo.db.Model(&project).Association("GitlabIntegrations")
  1231. if assoc.Error != nil {
  1232. return nil, assoc.Error
  1233. }
  1234. if err := assoc.Append(gi); err != nil {
  1235. return nil, err
  1236. }
  1237. if repo.storageBackend != nil {
  1238. err = repo.storageBackend.WriteGitlabCredential(gi, credentialData)
  1239. if err != nil {
  1240. return nil, err
  1241. }
  1242. }
  1243. return gi, nil
  1244. }
  1245. func (repo *GitlabIntegrationRepository) ReadGitlabIntegration(projectID, id uint) (*ints.GitlabIntegration, error) {
  1246. gi := &ints.GitlabIntegration{}
  1247. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&gi).Error; err != nil {
  1248. return nil, err
  1249. }
  1250. if repo.storageBackend != nil {
  1251. credentialData, err := repo.storageBackend.GetGitlabCredential(gi)
  1252. if err != nil {
  1253. return nil, err
  1254. }
  1255. gi.AppClientID = credentialData.AppClientID
  1256. gi.AppClientSecret = credentialData.AppClientSecret
  1257. }
  1258. err := repo.DecryptGitlabIntegrationData(gi, repo.key)
  1259. if err != nil {
  1260. return nil, err
  1261. }
  1262. return gi, nil
  1263. }
  1264. func (repo *GitlabIntegrationRepository) ListGitlabIntegrationsByProjectID(projectID uint) ([]*ints.GitlabIntegration, error) {
  1265. gi := []*ints.GitlabIntegration{}
  1266. if err := repo.db.Where("project_id = ? AND deleted_at IS NULL", projectID).Find(&gi).Error; err != nil {
  1267. return nil, err
  1268. }
  1269. return gi, nil
  1270. }
  1271. // EncryptGitlabIntegrationData will encrypt the gitlab integration data before
  1272. // writing to the DB
  1273. func (repo *GitlabIntegrationRepository) EncryptGitlabIntegrationData(
  1274. gi *ints.GitlabIntegration,
  1275. key *[32]byte,
  1276. ) error {
  1277. if len(gi.AppClientID) > 0 {
  1278. cipherData, err := encryption.Encrypt(gi.AppClientID, key)
  1279. if err != nil {
  1280. return err
  1281. }
  1282. gi.AppClientID = cipherData
  1283. }
  1284. if len(gi.AppClientSecret) > 0 {
  1285. cipherData, err := encryption.Encrypt(gi.AppClientSecret, key)
  1286. if err != nil {
  1287. return err
  1288. }
  1289. gi.AppClientSecret = cipherData
  1290. }
  1291. return nil
  1292. }
  1293. // DecryptGitlabIntegrationData will decrypt the gitlab integration data before
  1294. // returning it from the DB
  1295. func (repo *GitlabIntegrationRepository) DecryptGitlabIntegrationData(
  1296. gi *ints.GitlabIntegration,
  1297. key *[32]byte,
  1298. ) error {
  1299. if len(gi.AppClientID) > 0 {
  1300. plaintext, err := encryption.Decrypt(gi.AppClientID, key)
  1301. if err != nil {
  1302. return err
  1303. }
  1304. gi.AppClientID = plaintext
  1305. }
  1306. if len(gi.AppClientSecret) > 0 {
  1307. plaintext, err := encryption.Decrypt(gi.AppClientSecret, key)
  1308. if err != nil {
  1309. return err
  1310. }
  1311. gi.AppClientSecret = plaintext
  1312. }
  1313. return nil
  1314. }
  1315. // GitlabAppOAuthIntegrationRepository uses gorm.DB for querying the database
  1316. type GitlabAppOAuthIntegrationRepository struct {
  1317. db *gorm.DB
  1318. key *[32]byte
  1319. storageBackend credentials.CredentialStorage
  1320. }
  1321. // NewGitlabAppOAuthIntegrationRepository returns a GitlabAppOAuthIntegrationRepository which uses
  1322. // gorm.DB for querying the database
  1323. func NewGitlabAppOAuthIntegrationRepository(
  1324. db *gorm.DB,
  1325. key *[32]byte,
  1326. storageBackend credentials.CredentialStorage,
  1327. ) repository.GitlabAppOAuthIntegrationRepository {
  1328. return &GitlabAppOAuthIntegrationRepository{db, key, storageBackend}
  1329. }
  1330. func (repo *GitlabAppOAuthIntegrationRepository) CreateGitlabAppOAuthIntegration(
  1331. gi *ints.GitlabAppOAuthIntegration,
  1332. ) (*ints.GitlabAppOAuthIntegration, error) {
  1333. if err := repo.db.Create(gi).Error; err != nil {
  1334. return nil, err
  1335. }
  1336. return gi, nil
  1337. }
  1338. func (repo *GitlabAppOAuthIntegrationRepository) ReadGitlabAppOAuthIntegration(
  1339. userID, projectID, integrationID uint,
  1340. ) (*ints.GitlabAppOAuthIntegration, error) {
  1341. gi := &ints.GitlabAppOAuthIntegration{}
  1342. if err := repo.db.
  1343. Order("gitlab_app_o_auth_integrations.id desc").
  1344. Joins("INNER JOIN gitlab_integrations ON gitlab_integrations.id = gitlab_app_o_auth_integrations.gitlab_integration_id").
  1345. Joins("INNER JOIN o_auth_integrations ON o_auth_integrations.id = gitlab_app_o_auth_integrations.o_auth_integration_id").
  1346. Where("o_auth_integrations.user_id = ? AND o_auth_integrations.project_id = ? AND"+
  1347. " gitlab_integrations.id = ? AND gitlab_integrations.deleted_at IS NULL AND"+
  1348. " gitlab_app_o_auth_integrations.deleted_at IS NULL AND o_auth_integrations.deleted_at IS NULL",
  1349. userID, projectID, integrationID).First(&gi).Error; err != nil {
  1350. return nil, err
  1351. }
  1352. return gi, nil
  1353. }