auth.go 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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. // EncryptGitlabIntegrationData will encrypt the gitlab integration data before
  1292. // writing to the DB
  1293. func (repo *GitlabIntegrationRepository) EncryptGitlabIntegrationData(
  1294. gi *ints.GitlabIntegration,
  1295. key *[32]byte,
  1296. ) error {
  1297. if len(gi.AppClientID) > 0 {
  1298. cipherData, err := encryption.Encrypt(gi.AppClientID, key)
  1299. if err != nil {
  1300. return err
  1301. }
  1302. gi.AppClientID = cipherData
  1303. }
  1304. if len(gi.AppClientSecret) > 0 {
  1305. cipherData, err := encryption.Encrypt(gi.AppClientSecret, key)
  1306. if err != nil {
  1307. return err
  1308. }
  1309. gi.AppClientSecret = cipherData
  1310. }
  1311. return nil
  1312. }
  1313. // DecryptGitlabIntegrationData will decrypt the gitlab integration data before
  1314. // returning it from the DB
  1315. func (repo *GitlabIntegrationRepository) DecryptGitlabIntegrationData(
  1316. gi *ints.GitlabIntegration,
  1317. key *[32]byte,
  1318. ) error {
  1319. if len(gi.AppClientID) > 0 {
  1320. plaintext, err := encryption.Decrypt(gi.AppClientID, key)
  1321. if err != nil {
  1322. return err
  1323. }
  1324. gi.AppClientID = plaintext
  1325. }
  1326. if len(gi.AppClientSecret) > 0 {
  1327. plaintext, err := encryption.Decrypt(gi.AppClientSecret, key)
  1328. if err != nil {
  1329. return err
  1330. }
  1331. gi.AppClientSecret = plaintext
  1332. }
  1333. return nil
  1334. }
  1335. // GitlabAppOAuthIntegrationRepository uses gorm.DB for querying the database
  1336. type GitlabAppOAuthIntegrationRepository struct {
  1337. db *gorm.DB
  1338. key *[32]byte
  1339. storageBackend credentials.CredentialStorage
  1340. }
  1341. // NewGitlabAppOAuthIntegrationRepository returns a GitlabAppOAuthIntegrationRepository which uses
  1342. // gorm.DB for querying the database
  1343. func NewGitlabAppOAuthIntegrationRepository(
  1344. db *gorm.DB,
  1345. key *[32]byte,
  1346. storageBackend credentials.CredentialStorage,
  1347. ) repository.GitlabAppOAuthIntegrationRepository {
  1348. return &GitlabAppOAuthIntegrationRepository{db, key, storageBackend}
  1349. }
  1350. func (repo *GitlabAppOAuthIntegrationRepository) CreateGitlabAppOAuthIntegration(
  1351. gi *ints.GitlabAppOAuthIntegration,
  1352. ) (*ints.GitlabAppOAuthIntegration, error) {
  1353. if err := repo.db.Create(gi).Error; err != nil {
  1354. return nil, err
  1355. }
  1356. return gi, nil
  1357. }
  1358. func (repo *GitlabAppOAuthIntegrationRepository) ReadGitlabAppOAuthIntegration(
  1359. userID, projectID, integrationID uint,
  1360. ) (*ints.GitlabAppOAuthIntegration, error) {
  1361. gi := &ints.GitlabAppOAuthIntegration{}
  1362. if err := repo.db.
  1363. Order("gitlab_app_o_auth_integrations.id desc").
  1364. Joins("INNER JOIN gitlab_integrations ON gitlab_integrations.id = gitlab_app_o_auth_integrations.gitlab_integration_id").
  1365. Joins("INNER JOIN o_auth_integrations ON o_auth_integrations.id = gitlab_app_o_auth_integrations.o_auth_integration_id").
  1366. Where("o_auth_integrations.user_id = ? AND o_auth_integrations.project_id = ? AND"+
  1367. " gitlab_integrations.id = ? AND gitlab_integrations.deleted_at IS NULL AND"+
  1368. " gitlab_app_o_auth_integrations.deleted_at IS NULL AND o_auth_integrations.deleted_at IS NULL",
  1369. userID, projectID, integrationID).First(&gi).Error; err != nil {
  1370. return nil, err
  1371. }
  1372. return gi, nil
  1373. }