auth.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  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. // DeleteBasicIntegration deletes an existing basic auth mechanism
  231. func (repo *BasicIntegrationRepository) DeleteBasicIntegration(
  232. am *ints.BasicIntegration,
  233. ) (*ints.BasicIntegration, error) {
  234. project := &models.Project{}
  235. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  236. return nil, err
  237. }
  238. assoc := repo.db.Model(&project).Association("BasicIntegrations")
  239. if assoc.Error != nil {
  240. return nil, assoc.Error
  241. }
  242. if err := assoc.Delete(am); err != nil {
  243. return nil, err
  244. }
  245. if err := repo.db.Delete(am).Error; err != nil {
  246. return nil, err
  247. }
  248. return am, nil
  249. }
  250. // EncryptBasicIntegrationData will encrypt the basic integration data before
  251. // writing to the DB
  252. func (repo *BasicIntegrationRepository) EncryptBasicIntegrationData(
  253. basic *ints.BasicIntegration,
  254. key *[32]byte,
  255. ) error {
  256. if len(basic.Username) > 0 {
  257. cipherData, err := encryption.Encrypt(basic.Username, key)
  258. if err != nil {
  259. return err
  260. }
  261. basic.Username = cipherData
  262. }
  263. if len(basic.Password) > 0 {
  264. cipherData, err := encryption.Encrypt(basic.Password, key)
  265. if err != nil {
  266. return err
  267. }
  268. basic.Password = cipherData
  269. }
  270. return nil
  271. }
  272. // DecryptBasicIntegrationData will decrypt the basic integration data before
  273. // returning it from the DB
  274. func (repo *BasicIntegrationRepository) DecryptBasicIntegrationData(
  275. basic *ints.BasicIntegration,
  276. key *[32]byte,
  277. ) error {
  278. if len(basic.Username) > 0 {
  279. plaintext, err := encryption.Decrypt(basic.Username, key)
  280. if err != nil {
  281. return err
  282. }
  283. basic.Username = plaintext
  284. }
  285. if len(basic.Password) > 0 {
  286. plaintext, err := encryption.Decrypt(basic.Password, key)
  287. if err != nil {
  288. return err
  289. }
  290. basic.Password = plaintext
  291. }
  292. return nil
  293. }
  294. // OIDCIntegrationRepository uses gorm.DB for querying the database
  295. type OIDCIntegrationRepository struct {
  296. db *gorm.DB
  297. key *[32]byte
  298. }
  299. // NewOIDCIntegrationRepository returns a OIDCIntegrationRepository which uses
  300. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  301. // sensitive data
  302. func NewOIDCIntegrationRepository(
  303. db *gorm.DB,
  304. key *[32]byte,
  305. ) repository.OIDCIntegrationRepository {
  306. return &OIDCIntegrationRepository{db, key}
  307. }
  308. // CreateOIDCIntegration creates a new oidc auth mechanism
  309. func (repo *OIDCIntegrationRepository) CreateOIDCIntegration(
  310. am *ints.OIDCIntegration,
  311. ) (*ints.OIDCIntegration, error) {
  312. err := repo.EncryptOIDCIntegrationData(am, repo.key)
  313. if err != nil {
  314. return nil, err
  315. }
  316. project := &models.Project{}
  317. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  318. return nil, err
  319. }
  320. assoc := repo.db.Model(&project).Association("OIDCIntegrations")
  321. if assoc.Error != nil {
  322. return nil, assoc.Error
  323. }
  324. if err := assoc.Append(am); err != nil {
  325. return nil, err
  326. }
  327. return am, nil
  328. }
  329. // ReadOIDCIntegration finds a oidc auth mechanism by id
  330. func (repo *OIDCIntegrationRepository) ReadOIDCIntegration(
  331. projectID, id uint,
  332. ) (*ints.OIDCIntegration, error) {
  333. oidc := &ints.OIDCIntegration{}
  334. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&oidc).Error; err != nil {
  335. return nil, err
  336. }
  337. err := repo.DecryptOIDCIntegrationData(oidc, repo.key)
  338. if err != nil {
  339. return nil, err
  340. }
  341. return oidc, nil
  342. }
  343. // ListOIDCIntegrationsByProjectID finds all oidc auth mechanisms
  344. // for a given project id
  345. func (repo *OIDCIntegrationRepository) ListOIDCIntegrationsByProjectID(
  346. projectID uint,
  347. ) ([]*ints.OIDCIntegration, error) {
  348. oidcs := []*ints.OIDCIntegration{}
  349. if err := repo.db.Where("project_id = ?", projectID).Find(&oidcs).Error; err != nil {
  350. return nil, err
  351. }
  352. return oidcs, nil
  353. }
  354. // EncryptOIDCIntegrationData will encrypt the oidc integration data before
  355. // writing to the DB
  356. func (repo *OIDCIntegrationRepository) EncryptOIDCIntegrationData(
  357. oidc *ints.OIDCIntegration,
  358. key *[32]byte,
  359. ) error {
  360. if len(oidc.IssuerURL) > 0 {
  361. cipherData, err := encryption.Encrypt(oidc.IssuerURL, key)
  362. if err != nil {
  363. return err
  364. }
  365. oidc.IssuerURL = cipherData
  366. }
  367. if len(oidc.ClientID) > 0 {
  368. cipherData, err := encryption.Encrypt(oidc.ClientID, key)
  369. if err != nil {
  370. return err
  371. }
  372. oidc.ClientID = cipherData
  373. }
  374. if len(oidc.ClientSecret) > 0 {
  375. cipherData, err := encryption.Encrypt(oidc.ClientSecret, key)
  376. if err != nil {
  377. return err
  378. }
  379. oidc.ClientSecret = cipherData
  380. }
  381. if len(oidc.CertificateAuthorityData) > 0 {
  382. cipherData, err := encryption.Encrypt(oidc.CertificateAuthorityData, key)
  383. if err != nil {
  384. return err
  385. }
  386. oidc.CertificateAuthorityData = cipherData
  387. }
  388. if len(oidc.IDToken) > 0 {
  389. cipherData, err := encryption.Encrypt(oidc.IDToken, key)
  390. if err != nil {
  391. return err
  392. }
  393. oidc.IDToken = cipherData
  394. }
  395. if len(oidc.RefreshToken) > 0 {
  396. cipherData, err := encryption.Encrypt(oidc.RefreshToken, key)
  397. if err != nil {
  398. return err
  399. }
  400. oidc.RefreshToken = cipherData
  401. }
  402. return nil
  403. }
  404. // DecryptOIDCIntegrationData will decrypt the kube integration data before
  405. // returning it from the DB
  406. func (repo *OIDCIntegrationRepository) DecryptOIDCIntegrationData(
  407. oidc *ints.OIDCIntegration,
  408. key *[32]byte,
  409. ) error {
  410. if len(oidc.IssuerURL) > 0 {
  411. plaintext, err := encryption.Decrypt(oidc.IssuerURL, key)
  412. if err != nil {
  413. return err
  414. }
  415. oidc.IssuerURL = plaintext
  416. }
  417. if len(oidc.ClientID) > 0 {
  418. plaintext, err := encryption.Decrypt(oidc.ClientID, key)
  419. if err != nil {
  420. return err
  421. }
  422. oidc.ClientID = plaintext
  423. }
  424. if len(oidc.ClientSecret) > 0 {
  425. plaintext, err := encryption.Decrypt(oidc.ClientSecret, key)
  426. if err != nil {
  427. return err
  428. }
  429. oidc.ClientSecret = plaintext
  430. }
  431. if len(oidc.CertificateAuthorityData) > 0 {
  432. plaintext, err := encryption.Decrypt(oidc.CertificateAuthorityData, key)
  433. if err != nil {
  434. return err
  435. }
  436. oidc.CertificateAuthorityData = plaintext
  437. }
  438. if len(oidc.IDToken) > 0 {
  439. plaintext, err := encryption.Decrypt(oidc.IDToken, key)
  440. if err != nil {
  441. return err
  442. }
  443. oidc.IDToken = plaintext
  444. }
  445. if len(oidc.RefreshToken) > 0 {
  446. plaintext, err := encryption.Decrypt(oidc.RefreshToken, key)
  447. if err != nil {
  448. return err
  449. }
  450. oidc.RefreshToken = plaintext
  451. }
  452. return nil
  453. }
  454. // OAuthIntegrationRepository uses gorm.DB for querying the database
  455. type OAuthIntegrationRepository struct {
  456. db *gorm.DB
  457. key *[32]byte
  458. storageBackend credentials.CredentialStorage
  459. }
  460. // NewOAuthIntegrationRepository returns a OAuthIntegrationRepository which uses
  461. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  462. // sensitive data
  463. func NewOAuthIntegrationRepository(
  464. db *gorm.DB,
  465. key *[32]byte,
  466. storageBackend credentials.CredentialStorage,
  467. ) repository.OAuthIntegrationRepository {
  468. return &OAuthIntegrationRepository{db, key, storageBackend}
  469. }
  470. // CreateOAuthIntegration creates a new oauth auth mechanism
  471. func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
  472. am *ints.OAuthIntegration,
  473. ) (*ints.OAuthIntegration, error) {
  474. err := repo.EncryptOAuthIntegrationData(am, repo.key)
  475. if err != nil {
  476. return nil, err
  477. }
  478. // if storage backend is not nil, strip out credential data, which will be stored in credential
  479. // storage backend after write to DB
  480. credentialData := &credentials.OAuthCredential{}
  481. if repo.storageBackend != nil {
  482. credentialData.AccessToken = am.AccessToken
  483. credentialData.RefreshToken = am.RefreshToken
  484. credentialData.ClientID = am.ClientID
  485. am.AccessToken = []byte{}
  486. am.RefreshToken = []byte{}
  487. am.ClientID = []byte{}
  488. }
  489. project := &models.Project{}
  490. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  491. return nil, err
  492. }
  493. assoc := repo.db.Model(&project).Association("OAuthIntegrations")
  494. if assoc.Error != nil {
  495. return nil, assoc.Error
  496. }
  497. if err := assoc.Append(am); err != nil {
  498. return nil, err
  499. }
  500. if repo.storageBackend != nil {
  501. err = repo.storageBackend.WriteOAuthCredential(am, credentialData)
  502. if err != nil {
  503. return nil, err
  504. }
  505. }
  506. return am, nil
  507. }
  508. // ReadOAuthIntegration finds a oauth auth mechanism by id
  509. func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
  510. projectID, id uint,
  511. ) (*ints.OAuthIntegration, error) {
  512. oauth := &ints.OAuthIntegration{}
  513. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&oauth).Error; err != nil {
  514. return nil, err
  515. }
  516. if repo.storageBackend != nil {
  517. credentialData, err := repo.storageBackend.GetOAuthCredential(oauth)
  518. if err != nil {
  519. return nil, err
  520. }
  521. oauth.AccessToken = credentialData.AccessToken
  522. oauth.RefreshToken = credentialData.RefreshToken
  523. oauth.ClientID = credentialData.ClientID
  524. }
  525. err := repo.DecryptOAuthIntegrationData(oauth, repo.key)
  526. if err != nil {
  527. return nil, err
  528. }
  529. return oauth, nil
  530. }
  531. // ListOAuthIntegrationsByProjectID finds all oauth auth mechanisms
  532. // for a given project id
  533. func (repo *OAuthIntegrationRepository) ListOAuthIntegrationsByProjectID(
  534. projectID uint,
  535. ) ([]*ints.OAuthIntegration, error) {
  536. oauths := []*ints.OAuthIntegration{}
  537. if err := repo.db.Where("project_id = ?", projectID).Find(&oauths).Error; err != nil {
  538. return nil, err
  539. }
  540. return oauths, nil
  541. }
  542. // UpdateOAuthIntegration modifies an existing oauth integration in the database
  543. func (repo *OAuthIntegrationRepository) UpdateOAuthIntegration(
  544. am *ints.OAuthIntegration,
  545. ) (*ints.OAuthIntegration, error) {
  546. err := repo.EncryptOAuthIntegrationData(am, repo.key)
  547. if err != nil {
  548. return nil, err
  549. }
  550. // if storage backend is not nil, strip out credential data, which will be stored in credential
  551. // storage backend after write to DB
  552. credentialData := &credentials.OAuthCredential{}
  553. if repo.storageBackend != nil {
  554. credentialData.AccessToken = am.AccessToken
  555. credentialData.RefreshToken = am.RefreshToken
  556. credentialData.ClientID = am.ClientID
  557. am.AccessToken = []byte{}
  558. am.RefreshToken = []byte{}
  559. am.ClientID = []byte{}
  560. }
  561. if err := repo.db.Save(am).Error; err != nil {
  562. return nil, err
  563. }
  564. err = repo.DecryptOAuthIntegrationData(am, repo.key)
  565. if err != nil {
  566. return nil, err
  567. }
  568. if repo.storageBackend != nil {
  569. err = repo.storageBackend.WriteOAuthCredential(am, credentialData)
  570. if err != nil {
  571. return nil, err
  572. }
  573. }
  574. return am, nil
  575. }
  576. // EncryptOAuthIntegrationData will encrypt the oauth integration data before
  577. // writing to the DB
  578. func (repo *OAuthIntegrationRepository) EncryptOAuthIntegrationData(
  579. oauth *ints.OAuthIntegration,
  580. key *[32]byte,
  581. ) error {
  582. if len(oauth.ClientID) > 0 {
  583. cipherData, err := encryption.Encrypt(oauth.ClientID, key)
  584. if err != nil {
  585. return err
  586. }
  587. oauth.ClientID = cipherData
  588. }
  589. if len(oauth.AccessToken) > 0 {
  590. cipherData, err := encryption.Encrypt(oauth.AccessToken, key)
  591. if err != nil {
  592. return err
  593. }
  594. oauth.AccessToken = cipherData
  595. }
  596. if len(oauth.RefreshToken) > 0 {
  597. cipherData, err := encryption.Encrypt(oauth.RefreshToken, key)
  598. if err != nil {
  599. return err
  600. }
  601. oauth.RefreshToken = cipherData
  602. }
  603. return nil
  604. }
  605. // DecryptOAuthIntegrationData will decrypt the oauth integration data before
  606. // returning it from the DB
  607. func (repo *OAuthIntegrationRepository) DecryptOAuthIntegrationData(
  608. oauth *ints.OAuthIntegration,
  609. key *[32]byte,
  610. ) error {
  611. if len(oauth.ClientID) > 0 {
  612. plaintext, err := encryption.Decrypt(oauth.ClientID, key)
  613. if err != nil {
  614. return err
  615. }
  616. oauth.ClientID = plaintext
  617. }
  618. if len(oauth.AccessToken) > 0 {
  619. plaintext, err := encryption.Decrypt(oauth.AccessToken, key)
  620. if err != nil {
  621. return err
  622. }
  623. oauth.AccessToken = plaintext
  624. }
  625. if len(oauth.RefreshToken) > 0 {
  626. plaintext, err := encryption.Decrypt(oauth.RefreshToken, key)
  627. if err != nil {
  628. return err
  629. }
  630. oauth.RefreshToken = plaintext
  631. }
  632. return nil
  633. }
  634. // GCPIntegrationRepository uses gorm.DB for querying the database
  635. type GCPIntegrationRepository struct {
  636. db *gorm.DB
  637. key *[32]byte
  638. storageBackend credentials.CredentialStorage
  639. }
  640. // NewGCPIntegrationRepository returns a GCPIntegrationRepository which uses
  641. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  642. // sensitive data
  643. func NewGCPIntegrationRepository(
  644. db *gorm.DB,
  645. key *[32]byte,
  646. storageBackend credentials.CredentialStorage,
  647. ) repository.GCPIntegrationRepository {
  648. return &GCPIntegrationRepository{db, key, storageBackend}
  649. }
  650. // CreateGCPIntegration creates a new gcp auth mechanism
  651. func (repo *GCPIntegrationRepository) CreateGCPIntegration(
  652. am *ints.GCPIntegration,
  653. ) (*ints.GCPIntegration, error) {
  654. err := repo.EncryptGCPIntegrationData(am, repo.key)
  655. if err != nil {
  656. return nil, err
  657. }
  658. // if storage backend is not nil, strip out credential data, which will be stored in credential
  659. // storage backend after write to DB
  660. credentialData := &credentials.GCPCredential{}
  661. if repo.storageBackend != nil {
  662. credentialData.GCPKeyData = am.GCPKeyData
  663. am.GCPKeyData = []byte{}
  664. }
  665. project := &models.Project{}
  666. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  667. return nil, err
  668. }
  669. assoc := repo.db.Model(&project).Association("GCPIntegrations")
  670. if assoc.Error != nil {
  671. return nil, assoc.Error
  672. }
  673. if err := assoc.Append(am); err != nil {
  674. return nil, err
  675. }
  676. if repo.storageBackend != nil {
  677. err = repo.storageBackend.WriteGCPCredential(am, credentialData)
  678. if err != nil {
  679. return nil, err
  680. }
  681. }
  682. return am, nil
  683. }
  684. // ReadGCPIntegration finds a gcp auth mechanism by id
  685. func (repo *GCPIntegrationRepository) ReadGCPIntegration(
  686. projectID, id uint,
  687. ) (*ints.GCPIntegration, error) {
  688. gcp := &ints.GCPIntegration{}
  689. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&gcp).Error; err != nil {
  690. return nil, err
  691. }
  692. if repo.storageBackend != nil {
  693. credentialData, err := repo.storageBackend.GetGCPCredential(gcp)
  694. if err != nil {
  695. return nil, err
  696. }
  697. gcp.GCPKeyData = credentialData.GCPKeyData
  698. }
  699. err := repo.DecryptGCPIntegrationData(gcp, repo.key)
  700. if err != nil {
  701. return nil, err
  702. }
  703. return gcp, nil
  704. }
  705. // ListGCPIntegrationsByProjectID finds all gcp auth mechanisms
  706. // for a given project id
  707. func (repo *GCPIntegrationRepository) ListGCPIntegrationsByProjectID(
  708. projectID uint,
  709. ) ([]*ints.GCPIntegration, error) {
  710. gcps := []*ints.GCPIntegration{}
  711. if err := repo.db.Where("project_id = ?", projectID).Find(&gcps).Error; err != nil {
  712. return nil, err
  713. }
  714. return gcps, nil
  715. }
  716. // EncryptGCPIntegrationData will encrypt the gcp integration data before
  717. // writing to the DB
  718. func (repo *GCPIntegrationRepository) EncryptGCPIntegrationData(
  719. gcp *ints.GCPIntegration,
  720. key *[32]byte,
  721. ) error {
  722. if len(gcp.GCPKeyData) > 0 {
  723. cipherData, err := encryption.Encrypt(gcp.GCPKeyData, key)
  724. if err != nil {
  725. return err
  726. }
  727. gcp.GCPKeyData = cipherData
  728. }
  729. return nil
  730. }
  731. // DecryptGCPIntegrationData will decrypt the gcp integration data before
  732. // returning it from the DB
  733. func (repo *GCPIntegrationRepository) DecryptGCPIntegrationData(
  734. gcp *ints.GCPIntegration,
  735. key *[32]byte,
  736. ) error {
  737. if len(gcp.GCPKeyData) > 0 {
  738. plaintext, err := encryption.Decrypt(gcp.GCPKeyData, key)
  739. if err != nil {
  740. return err
  741. }
  742. gcp.GCPKeyData = plaintext
  743. }
  744. return nil
  745. }
  746. // AWSIntegrationRepository uses gorm.DB for querying the database
  747. type AWSIntegrationRepository struct {
  748. db *gorm.DB
  749. key *[32]byte
  750. storageBackend credentials.CredentialStorage
  751. }
  752. // NewAWSIntegrationRepository returns a AWSIntegrationRepository which uses
  753. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  754. // sensitive data
  755. func NewAWSIntegrationRepository(
  756. db *gorm.DB,
  757. key *[32]byte,
  758. storageBackend credentials.CredentialStorage,
  759. ) repository.AWSIntegrationRepository {
  760. return &AWSIntegrationRepository{db, key, storageBackend}
  761. }
  762. // CreateAWSIntegration creates a new aws auth mechanism
  763. func (repo *AWSIntegrationRepository) CreateAWSIntegration(
  764. am *ints.AWSIntegration,
  765. ) (*ints.AWSIntegration, error) {
  766. err := repo.EncryptAWSIntegrationData(am, repo.key)
  767. if err != nil {
  768. return nil, err
  769. }
  770. // if storage backend is not nil, strip out credential data, which will be stored in credential
  771. // storage backend after write to DB
  772. credentialData := &credentials.AWSCredential{}
  773. if repo.storageBackend != nil {
  774. credentialData.AWSAccessKeyID = am.AWSAccessKeyID
  775. credentialData.AWSClusterID = am.AWSClusterID
  776. credentialData.AWSSecretAccessKey = am.AWSSecretAccessKey
  777. credentialData.AWSSessionToken = am.AWSSessionToken
  778. am.AWSAccessKeyID = []byte{}
  779. am.AWSClusterID = []byte{}
  780. am.AWSSecretAccessKey = []byte{}
  781. am.AWSSessionToken = []byte{}
  782. }
  783. project := &models.Project{}
  784. if err := repo.db.Where("id = ?", am.ProjectID).First(&project).Error; err != nil {
  785. return nil, err
  786. }
  787. assoc := repo.db.Model(&project).Association("AWSIntegrations")
  788. if assoc.Error != nil {
  789. return nil, assoc.Error
  790. }
  791. if err := assoc.Append(am); err != nil {
  792. return nil, err
  793. }
  794. if repo.storageBackend != nil {
  795. err = repo.storageBackend.WriteAWSCredential(am, credentialData)
  796. if err != nil {
  797. return nil, err
  798. }
  799. }
  800. return am, nil
  801. }
  802. // UpdateCluster modifies an existing Cluster in the database
  803. func (repo *AWSIntegrationRepository) OverwriteAWSIntegration(
  804. am *ints.AWSIntegration,
  805. ) (*ints.AWSIntegration, error) {
  806. err := repo.EncryptAWSIntegrationData(am, repo.key)
  807. if err != nil {
  808. return nil, err
  809. }
  810. // if storage backend is not nil, strip out credential data, which will be stored in credential
  811. // storage backend after write to DB
  812. credentialData := &credentials.AWSCredential{}
  813. if repo.storageBackend != nil {
  814. credentialData.AWSAccessKeyID = am.AWSAccessKeyID
  815. credentialData.AWSClusterID = am.AWSClusterID
  816. credentialData.AWSSecretAccessKey = am.AWSSecretAccessKey
  817. credentialData.AWSSessionToken = am.AWSSessionToken
  818. am.AWSAccessKeyID = []byte{}
  819. am.AWSClusterID = []byte{}
  820. am.AWSSecretAccessKey = []byte{}
  821. am.AWSSessionToken = []byte{}
  822. }
  823. if err := repo.db.Save(am).Error; err != nil {
  824. return nil, err
  825. }
  826. if repo.storageBackend != nil {
  827. err = repo.storageBackend.WriteAWSCredential(am, credentialData)
  828. if err != nil {
  829. return nil, err
  830. }
  831. }
  832. return am, nil
  833. }
  834. // ReadAWSIntegration finds a aws auth mechanism by id
  835. func (repo *AWSIntegrationRepository) ReadAWSIntegration(
  836. projectID, id uint,
  837. ) (*ints.AWSIntegration, error) {
  838. aws := &ints.AWSIntegration{}
  839. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&aws).Error; err != nil {
  840. return nil, err
  841. }
  842. if repo.storageBackend != nil {
  843. credentialData, err := repo.storageBackend.GetAWSCredential(aws)
  844. if err != nil {
  845. return nil, err
  846. }
  847. aws.AWSAccessKeyID = credentialData.AWSAccessKeyID
  848. aws.AWSClusterID = credentialData.AWSClusterID
  849. aws.AWSSecretAccessKey = credentialData.AWSSecretAccessKey
  850. aws.AWSSessionToken = credentialData.AWSSessionToken
  851. }
  852. err := repo.DecryptAWSIntegrationData(aws, repo.key)
  853. if err != nil {
  854. return nil, err
  855. }
  856. return aws, nil
  857. }
  858. // ListAWSIntegrationsByProjectID finds all aws auth mechanisms
  859. // for a given project id
  860. func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
  861. projectID uint,
  862. ) ([]*ints.AWSIntegration, error) {
  863. awss := []*ints.AWSIntegration{}
  864. if err := repo.db.Where("project_id = ?", projectID).Find(&awss).Error; err != nil {
  865. return nil, err
  866. }
  867. return awss, nil
  868. }
  869. // EncryptAWSIntegrationData will encrypt the aws integration data before
  870. // writing to the DB
  871. func (repo *AWSIntegrationRepository) EncryptAWSIntegrationData(
  872. aws *ints.AWSIntegration,
  873. key *[32]byte,
  874. ) error {
  875. if len(aws.AWSClusterID) > 0 {
  876. cipherData, err := encryption.Encrypt(aws.AWSClusterID, key)
  877. if err != nil {
  878. return err
  879. }
  880. aws.AWSClusterID = cipherData
  881. }
  882. if len(aws.AWSAccessKeyID) > 0 {
  883. cipherData, err := encryption.Encrypt(aws.AWSAccessKeyID, key)
  884. if err != nil {
  885. return err
  886. }
  887. aws.AWSAccessKeyID = cipherData
  888. }
  889. if len(aws.AWSSecretAccessKey) > 0 {
  890. cipherData, err := encryption.Encrypt(aws.AWSSecretAccessKey, key)
  891. if err != nil {
  892. return err
  893. }
  894. aws.AWSSecretAccessKey = cipherData
  895. }
  896. if len(aws.AWSSessionToken) > 0 {
  897. cipherData, err := encryption.Encrypt(aws.AWSSessionToken, key)
  898. if err != nil {
  899. return err
  900. }
  901. aws.AWSSessionToken = cipherData
  902. }
  903. return nil
  904. }
  905. // DecryptAWSIntegrationData will decrypt the aws integration data before
  906. // returning it from the DB
  907. func (repo *AWSIntegrationRepository) DecryptAWSIntegrationData(
  908. aws *ints.AWSIntegration,
  909. key *[32]byte,
  910. ) error {
  911. if len(aws.AWSClusterID) > 0 {
  912. plaintext, err := encryption.Decrypt(aws.AWSClusterID, key)
  913. if err != nil {
  914. return err
  915. }
  916. aws.AWSClusterID = plaintext
  917. }
  918. if len(aws.AWSAccessKeyID) > 0 {
  919. plaintext, err := encryption.Decrypt(aws.AWSAccessKeyID, key)
  920. if err != nil {
  921. return err
  922. }
  923. aws.AWSAccessKeyID = plaintext
  924. }
  925. if len(aws.AWSSecretAccessKey) > 0 {
  926. plaintext, err := encryption.Decrypt(aws.AWSSecretAccessKey, key)
  927. if err != nil {
  928. return err
  929. }
  930. aws.AWSSecretAccessKey = plaintext
  931. }
  932. if len(aws.AWSSessionToken) > 0 {
  933. plaintext, err := encryption.Decrypt(aws.AWSSessionToken, key)
  934. if err != nil {
  935. return err
  936. }
  937. aws.AWSSessionToken = plaintext
  938. }
  939. return nil
  940. }
  941. // GithubAppInstallationRepository implements repository.GithubAppInstallationRepository
  942. type GithubAppInstallationRepository struct {
  943. db *gorm.DB
  944. }
  945. // NewGithubAppInstallationRepository creates a new GithubAppInstallationRepository
  946. func NewGithubAppInstallationRepository(db *gorm.DB) repository.GithubAppInstallationRepository {
  947. return &GithubAppInstallationRepository{db}
  948. }
  949. // CreateGithubAppInstallation creates a new GithubAppInstallation instance
  950. func (repo *GithubAppInstallationRepository) CreateGithubAppInstallation(am *ints.GithubAppInstallation) (*ints.GithubAppInstallation, error) {
  951. if err := repo.db.Create(am).Error; err != nil {
  952. return nil, err
  953. }
  954. return am, nil
  955. }
  956. // ReadGithubAppInstallationByInstallationID finds a GithubAppInstallation by id
  957. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByInstallationID(gaID uint) (*ints.GithubAppInstallation, error) {
  958. ret := &ints.GithubAppInstallation{}
  959. if err := repo.db.Where("installation_id = ?", gaID).First(&ret).Error; err != nil {
  960. return nil, err
  961. }
  962. return ret, nil
  963. }
  964. // ReadGithubAppInstallationByAccountID finds a GithubAppInstallation by an account ID
  965. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
  966. ret := &ints.GithubAppInstallation{}
  967. if err := repo.db.Where("account_id = ?", accountID).First(&ret).Error; err != nil {
  968. return nil, err
  969. }
  970. return ret, nil
  971. }
  972. // ReadGithubAppInstallationByAccountIDs finds all instances of GithubInstallations given a list of account IDs
  973. // note that if there is not Installation for a given ID, no error will be generated
  974. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountIDs(accountIDs []int64) ([]*ints.GithubAppInstallation, error) {
  975. ret := make([]*ints.GithubAppInstallation, 0)
  976. if err := repo.db.Where("account_id IN ?", accountIDs).Find(&ret).Error; err != nil {
  977. return nil, err
  978. }
  979. return ret, nil
  980. }
  981. // DeleteGithubAppInstallationByAccountID deletes a GithubAppInstallation given an account ID
  982. // note that this deletion is done with db.Unscoped(), so the record is actually deleted
  983. func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccountID(accountID int64) error {
  984. if err := repo.db.Unscoped().Where("account_id = ?", accountID).Delete(&ints.GithubAppInstallation{}).Error; err != nil {
  985. return err
  986. }
  987. return nil
  988. }
  989. // GithubAppOAuthIntegrationRepository implements repository.GithubAppOAuthIntegrationRepository
  990. type GithubAppOAuthIntegrationRepository struct {
  991. db *gorm.DB
  992. }
  993. // NewGithubAppOAuthIntegrationRepository creates a GithubAppOAuthIntegrationRepository
  994. func NewGithubAppOAuthIntegrationRepository(db *gorm.DB) repository.GithubAppOAuthIntegrationRepository {
  995. return &GithubAppOAuthIntegrationRepository{db}
  996. }
  997. // CreateGithubAppOAuthIntegration creates a new GithubAppOAuthIntegration
  998. func (repo *GithubAppOAuthIntegrationRepository) CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  999. if err := repo.db.Create(am).Error; err != nil {
  1000. return nil, err
  1001. }
  1002. return am, nil
  1003. }
  1004. // ReadGithubAppOauthIntegration finds a GithubAppOauthIntegration by id
  1005. func (repo *GithubAppOAuthIntegrationRepository) ReadGithubAppOauthIntegration(id uint) (*ints.GithubAppOAuthIntegration, error) {
  1006. ret := &ints.GithubAppOAuthIntegration{}
  1007. if err := repo.db.Where("id = ?", id).First(&ret).Error; err != nil {
  1008. return nil, err
  1009. }
  1010. return ret, nil
  1011. }
  1012. // UpdateGithubAppOauthIntegration updates a GithubAppOauthIntegration
  1013. func (repo *GithubAppOAuthIntegrationRepository) UpdateGithubAppOauthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  1014. err := repo.db.Save(am).Error
  1015. if err != nil {
  1016. return nil, err
  1017. }
  1018. return am, nil
  1019. }
  1020. // AzureIntegrationRepository uses gorm.DB for querying the database
  1021. type AzureIntegrationRepository struct {
  1022. db *gorm.DB
  1023. key *[32]byte
  1024. storageBackend credentials.CredentialStorage
  1025. }
  1026. // NewAzureIntegrationRepository returns a AzureIntegrationRepository which uses
  1027. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  1028. // sensitive data
  1029. func NewAzureIntegrationRepository(
  1030. db *gorm.DB,
  1031. key *[32]byte,
  1032. storageBackend credentials.CredentialStorage,
  1033. ) repository.AzureIntegrationRepository {
  1034. return &AzureIntegrationRepository{db, key, storageBackend}
  1035. }
  1036. // CreateAzureIntegration creates a new Azure auth mechanism
  1037. func (repo *AzureIntegrationRepository) CreateAzureIntegration(
  1038. az *ints.AzureIntegration,
  1039. ) (*ints.AzureIntegration, error) {
  1040. err := repo.EncryptAzureIntegrationData(az, repo.key)
  1041. if err != nil {
  1042. return nil, err
  1043. }
  1044. // if storage backend is not nil, strip out credential data, which will be stored in credential
  1045. // storage backend after write to DB
  1046. credentialData := &credentials.AzureCredential{}
  1047. if repo.storageBackend != nil {
  1048. credentialData.ServicePrincipalSecret = az.ServicePrincipalSecret
  1049. credentialData.ACRPassword1 = az.ACRPassword1
  1050. credentialData.ACRPassword2 = az.ACRPassword2
  1051. credentialData.AKSPassword = az.AKSPassword
  1052. az.ServicePrincipalSecret = []byte{}
  1053. az.ACRPassword1 = []byte{}
  1054. az.ACRPassword2 = []byte{}
  1055. az.AKSPassword = []byte{}
  1056. }
  1057. project := &models.Project{}
  1058. if err := repo.db.Where("id = ?", az.ProjectID).First(&project).Error; err != nil {
  1059. return nil, err
  1060. }
  1061. assoc := repo.db.Model(&project).Association("AzureIntegrations")
  1062. if assoc.Error != nil {
  1063. return nil, assoc.Error
  1064. }
  1065. if err := assoc.Append(az); err != nil {
  1066. return nil, err
  1067. }
  1068. if repo.storageBackend != nil {
  1069. err = repo.storageBackend.WriteAzureCredential(az, credentialData)
  1070. if err != nil {
  1071. return nil, err
  1072. }
  1073. }
  1074. return az, nil
  1075. }
  1076. // OverwriteAzureIntegration overwrites the Azure credential in the DB
  1077. func (repo *AzureIntegrationRepository) OverwriteAzureIntegration(
  1078. az *ints.AzureIntegration,
  1079. ) (*ints.AzureIntegration, error) {
  1080. err := repo.EncryptAzureIntegrationData(az, repo.key)
  1081. if err != nil {
  1082. return nil, err
  1083. }
  1084. // if storage backend is not nil, strip out credential data, which will be stored in credential
  1085. // storage backend after write to DB
  1086. credentialData := &credentials.AzureCredential{}
  1087. if repo.storageBackend != nil {
  1088. credentialData.ServicePrincipalSecret = az.ServicePrincipalSecret
  1089. credentialData.ACRPassword1 = az.ACRPassword1
  1090. credentialData.ACRPassword2 = az.ACRPassword2
  1091. credentialData.AKSPassword = az.AKSPassword
  1092. az.ServicePrincipalSecret = []byte{}
  1093. az.ACRPassword1 = []byte{}
  1094. az.ACRPassword2 = []byte{}
  1095. az.AKSPassword = []byte{}
  1096. }
  1097. if err := repo.db.Save(az).Error; err != nil {
  1098. return nil, err
  1099. }
  1100. if repo.storageBackend != nil {
  1101. err = repo.storageBackend.WriteAzureCredential(az, credentialData)
  1102. if err != nil {
  1103. return nil, err
  1104. }
  1105. }
  1106. // perform another read
  1107. return repo.ReadAzureIntegration(az.ProjectID, az.ID)
  1108. }
  1109. // ReadAzureIntegration finds a Azure auth mechanism by id
  1110. func (repo *AzureIntegrationRepository) ReadAzureIntegration(
  1111. projectID, id uint,
  1112. ) (*ints.AzureIntegration, error) {
  1113. az := &ints.AzureIntegration{}
  1114. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&az).Error; err != nil {
  1115. return nil, err
  1116. }
  1117. if repo.storageBackend != nil {
  1118. credentialData, err := repo.storageBackend.GetAzureCredential(az)
  1119. if err != nil {
  1120. return nil, err
  1121. }
  1122. az.ServicePrincipalSecret = credentialData.ServicePrincipalSecret
  1123. az.ACRPassword1 = credentialData.ACRPassword1
  1124. az.ACRPassword2 = credentialData.ACRPassword2
  1125. az.AKSPassword = credentialData.AKSPassword
  1126. }
  1127. err := repo.DecryptAzureIntegrationData(az, repo.key)
  1128. if err != nil {
  1129. return nil, err
  1130. }
  1131. return az, nil
  1132. }
  1133. // ListAzureIntegrationsByProjectID finds all Azure auth mechanisms
  1134. // for a given project id
  1135. func (repo *AzureIntegrationRepository) ListAzureIntegrationsByProjectID(
  1136. projectID uint,
  1137. ) ([]*ints.AzureIntegration, error) {
  1138. azs := []*ints.AzureIntegration{}
  1139. if err := repo.db.Where("project_id = ?", projectID).Find(&azs).Error; err != nil {
  1140. return nil, err
  1141. }
  1142. return azs, nil
  1143. }
  1144. // EncryptAWSIntegrationData will encrypt the aws integration data before
  1145. // writing to the DB
  1146. func (repo *AzureIntegrationRepository) EncryptAzureIntegrationData(
  1147. az *ints.AzureIntegration,
  1148. key *[32]byte,
  1149. ) error {
  1150. if len(az.ServicePrincipalSecret) > 0 {
  1151. cipherData, err := encryption.Encrypt(az.ServicePrincipalSecret, key)
  1152. if err != nil {
  1153. return err
  1154. }
  1155. az.ServicePrincipalSecret = cipherData
  1156. }
  1157. if len(az.ACRPassword1) > 0 {
  1158. cipherData, err := encryption.Encrypt(az.ACRPassword1, key)
  1159. if err != nil {
  1160. return err
  1161. }
  1162. az.ACRPassword1 = cipherData
  1163. }
  1164. if len(az.ACRPassword2) > 0 {
  1165. cipherData, err := encryption.Encrypt(az.ACRPassword2, key)
  1166. if err != nil {
  1167. return err
  1168. }
  1169. az.ACRPassword2 = cipherData
  1170. }
  1171. if len(az.AKSPassword) > 0 {
  1172. cipherData, err := encryption.Encrypt(az.AKSPassword, key)
  1173. if err != nil {
  1174. return err
  1175. }
  1176. az.AKSPassword = cipherData
  1177. }
  1178. return nil
  1179. }
  1180. // DecryptAzureIntegrationData will decrypt the Azure integration data before
  1181. // returning it from the DB
  1182. func (repo *AzureIntegrationRepository) DecryptAzureIntegrationData(
  1183. az *ints.AzureIntegration,
  1184. key *[32]byte,
  1185. ) error {
  1186. if len(az.ServicePrincipalSecret) > 0 {
  1187. plaintext, err := encryption.Decrypt(az.ServicePrincipalSecret, key)
  1188. if err != nil {
  1189. return err
  1190. }
  1191. az.ServicePrincipalSecret = plaintext
  1192. }
  1193. if len(az.ACRPassword1) > 0 {
  1194. plaintext, err := encryption.Decrypt(az.ACRPassword1, key)
  1195. if err != nil {
  1196. return err
  1197. }
  1198. az.ACRPassword1 = plaintext
  1199. }
  1200. if len(az.ACRPassword2) > 0 {
  1201. plaintext, err := encryption.Decrypt(az.ACRPassword2, key)
  1202. if err != nil {
  1203. return err
  1204. }
  1205. az.ACRPassword2 = plaintext
  1206. }
  1207. if len(az.AKSPassword) > 0 {
  1208. plaintext, err := encryption.Decrypt(az.AKSPassword, key)
  1209. if err != nil {
  1210. return err
  1211. }
  1212. az.AKSPassword = plaintext
  1213. }
  1214. return nil
  1215. }
  1216. // GitlabIntegrationRepository uses gorm.DB for querying the database
  1217. type GitlabIntegrationRepository struct {
  1218. db *gorm.DB
  1219. key *[32]byte
  1220. storageBackend credentials.CredentialStorage
  1221. }
  1222. // NewGitlabIntegrationRepository returns a GitlabIntegrationRepository which uses
  1223. // gorm.DB for querying the database
  1224. func NewGitlabIntegrationRepository(
  1225. db *gorm.DB,
  1226. key *[32]byte,
  1227. storageBackend credentials.CredentialStorage,
  1228. ) repository.GitlabIntegrationRepository {
  1229. return &GitlabIntegrationRepository{db, key, storageBackend}
  1230. }
  1231. // CreateIntegration adds a new GitlabIntegration row to the gitlab_integration table in the database
  1232. func (repo *GitlabIntegrationRepository) CreateGitlabIntegration(gi *ints.GitlabIntegration) (*ints.GitlabIntegration, error) {
  1233. err := repo.EncryptGitlabIntegrationData(gi, repo.key)
  1234. if err != nil {
  1235. return nil, err
  1236. }
  1237. // if storage backend is not nil, strip out credential data, which will be stored in credential
  1238. // storage backend after write to DB
  1239. credentialData := &credentials.GitlabCredential{}
  1240. if repo.storageBackend != nil {
  1241. credentialData.AppClientID = gi.AppClientID
  1242. credentialData.AppClientSecret = gi.AppClientSecret
  1243. gi.AppClientID = []byte{}
  1244. gi.AppClientSecret = []byte{}
  1245. }
  1246. project := &models.Project{}
  1247. if err := repo.db.Where("id = ?", gi.ProjectID).First(&project).Error; err != nil {
  1248. return nil, err
  1249. }
  1250. assoc := repo.db.Model(&project).Association("GitlabIntegrations")
  1251. if assoc.Error != nil {
  1252. return nil, assoc.Error
  1253. }
  1254. if err := assoc.Append(gi); err != nil {
  1255. return nil, err
  1256. }
  1257. if repo.storageBackend != nil {
  1258. err = repo.storageBackend.WriteGitlabCredential(gi, credentialData)
  1259. if err != nil {
  1260. return nil, err
  1261. }
  1262. }
  1263. return gi, nil
  1264. }
  1265. func (repo *GitlabIntegrationRepository) ReadGitlabIntegration(projectID, id uint) (*ints.GitlabIntegration, error) {
  1266. gi := &ints.GitlabIntegration{}
  1267. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).First(&gi).Error; err != nil {
  1268. return nil, err
  1269. }
  1270. if repo.storageBackend != nil {
  1271. credentialData, err := repo.storageBackend.GetGitlabCredential(gi)
  1272. if err != nil {
  1273. return nil, err
  1274. }
  1275. gi.AppClientID = credentialData.AppClientID
  1276. gi.AppClientSecret = credentialData.AppClientSecret
  1277. }
  1278. err := repo.DecryptGitlabIntegrationData(gi, repo.key)
  1279. if err != nil {
  1280. return nil, err
  1281. }
  1282. return gi, nil
  1283. }
  1284. func (repo *GitlabIntegrationRepository) ListGitlabIntegrationsByProjectID(projectID uint) ([]*ints.GitlabIntegration, error) {
  1285. gi := []*ints.GitlabIntegration{}
  1286. if err := repo.db.Where("project_id = ? AND deleted_at IS NULL", projectID).Find(&gi).Error; err != nil {
  1287. return nil, err
  1288. }
  1289. return gi, nil
  1290. }
  1291. func (repo *GitlabIntegrationRepository) DeleteGitlabIntegrationByID(projectID, id uint) error {
  1292. if err := repo.db.Where("project_id = ? AND id = ?", projectID, id).Delete(&ints.GitlabIntegration{}).Error; err != nil {
  1293. return err
  1294. }
  1295. return nil
  1296. }
  1297. // EncryptGitlabIntegrationData will encrypt the gitlab integration data before
  1298. // writing to the DB
  1299. func (repo *GitlabIntegrationRepository) EncryptGitlabIntegrationData(
  1300. gi *ints.GitlabIntegration,
  1301. key *[32]byte,
  1302. ) error {
  1303. if len(gi.AppClientID) > 0 {
  1304. cipherData, err := encryption.Encrypt(gi.AppClientID, key)
  1305. if err != nil {
  1306. return err
  1307. }
  1308. gi.AppClientID = cipherData
  1309. }
  1310. if len(gi.AppClientSecret) > 0 {
  1311. cipherData, err := encryption.Encrypt(gi.AppClientSecret, key)
  1312. if err != nil {
  1313. return err
  1314. }
  1315. gi.AppClientSecret = cipherData
  1316. }
  1317. return nil
  1318. }
  1319. // DecryptGitlabIntegrationData will decrypt the gitlab integration data before
  1320. // returning it from the DB
  1321. func (repo *GitlabIntegrationRepository) DecryptGitlabIntegrationData(
  1322. gi *ints.GitlabIntegration,
  1323. key *[32]byte,
  1324. ) error {
  1325. if len(gi.AppClientID) > 0 {
  1326. plaintext, err := encryption.Decrypt(gi.AppClientID, key)
  1327. if err != nil {
  1328. return err
  1329. }
  1330. gi.AppClientID = plaintext
  1331. }
  1332. if len(gi.AppClientSecret) > 0 {
  1333. plaintext, err := encryption.Decrypt(gi.AppClientSecret, key)
  1334. if err != nil {
  1335. return err
  1336. }
  1337. gi.AppClientSecret = plaintext
  1338. }
  1339. return nil
  1340. }
  1341. // GitlabAppOAuthIntegrationRepository uses gorm.DB for querying the database
  1342. type GitlabAppOAuthIntegrationRepository struct {
  1343. db *gorm.DB
  1344. key *[32]byte
  1345. storageBackend credentials.CredentialStorage
  1346. }
  1347. // NewGitlabAppOAuthIntegrationRepository returns a GitlabAppOAuthIntegrationRepository which uses
  1348. // gorm.DB for querying the database
  1349. func NewGitlabAppOAuthIntegrationRepository(
  1350. db *gorm.DB,
  1351. key *[32]byte,
  1352. storageBackend credentials.CredentialStorage,
  1353. ) repository.GitlabAppOAuthIntegrationRepository {
  1354. return &GitlabAppOAuthIntegrationRepository{db, key, storageBackend}
  1355. }
  1356. func (repo *GitlabAppOAuthIntegrationRepository) CreateGitlabAppOAuthIntegration(
  1357. gi *ints.GitlabAppOAuthIntegration,
  1358. ) (*ints.GitlabAppOAuthIntegration, error) {
  1359. if err := repo.db.Create(gi).Error; err != nil {
  1360. return nil, err
  1361. }
  1362. return gi, nil
  1363. }
  1364. func (repo *GitlabAppOAuthIntegrationRepository) ReadGitlabAppOAuthIntegration(
  1365. userID, projectID, integrationID uint,
  1366. ) (*ints.GitlabAppOAuthIntegration, error) {
  1367. gi := &ints.GitlabAppOAuthIntegration{}
  1368. if err := repo.db.
  1369. Order("gitlab_app_o_auth_integrations.id desc").
  1370. Joins("INNER JOIN gitlab_integrations ON gitlab_integrations.id = gitlab_app_o_auth_integrations.gitlab_integration_id").
  1371. Joins("INNER JOIN o_auth_integrations ON o_auth_integrations.id = gitlab_app_o_auth_integrations.o_auth_integration_id").
  1372. Where("o_auth_integrations.user_id = ? AND o_auth_integrations.project_id = ? AND"+
  1373. " gitlab_integrations.id = ? AND gitlab_integrations.deleted_at IS NULL AND"+
  1374. " gitlab_app_o_auth_integrations.deleted_at IS NULL AND o_auth_integrations.deleted_at IS NULL",
  1375. userID, projectID, integrationID).First(&gi).Error; err != nil {
  1376. return nil, err
  1377. }
  1378. return gi, nil
  1379. }