auth.go 30 KB

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