auth.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. package test
  2. import (
  3. "errors"
  4. "github.com/porter-dev/porter/internal/repository"
  5. "gorm.io/gorm"
  6. ints "github.com/porter-dev/porter/internal/models/integrations"
  7. )
  8. // KubeIntegrationRepository implements repository.KubeIntegrationRepository
  9. type KubeIntegrationRepository struct {
  10. canQuery bool
  11. kubeIntegrations []*ints.KubeIntegration
  12. }
  13. // NewKubeIntegrationRepository will return errors if canQuery is false
  14. func NewKubeIntegrationRepository(canQuery bool) repository.KubeIntegrationRepository {
  15. return &KubeIntegrationRepository{
  16. canQuery,
  17. []*ints.KubeIntegration{},
  18. }
  19. }
  20. // CreateKubeIntegration creates a new kube auth mechanism
  21. func (repo *KubeIntegrationRepository) CreateKubeIntegration(
  22. am *ints.KubeIntegration,
  23. ) (*ints.KubeIntegration, error) {
  24. if !repo.canQuery {
  25. return nil, errors.New("Cannot write database")
  26. }
  27. repo.kubeIntegrations = append(repo.kubeIntegrations, am)
  28. am.ID = uint(len(repo.kubeIntegrations))
  29. return am, nil
  30. }
  31. // ReadKubeIntegration finds a kube auth mechanism by id
  32. func (repo *KubeIntegrationRepository) ReadKubeIntegration(
  33. projectID, id uint,
  34. ) (*ints.KubeIntegration, error) {
  35. if !repo.canQuery {
  36. return nil, errors.New("Cannot read from database")
  37. }
  38. if int(id-1) >= len(repo.kubeIntegrations) || repo.kubeIntegrations[id-1] == nil {
  39. return nil, gorm.ErrRecordNotFound
  40. }
  41. index := int(id - 1)
  42. return repo.kubeIntegrations[index], nil
  43. }
  44. // ListKubeIntegrationsByProjectID finds all kube auth mechanisms
  45. // for a given project id
  46. func (repo *KubeIntegrationRepository) ListKubeIntegrationsByProjectID(
  47. projectID uint,
  48. ) ([]*ints.KubeIntegration, error) {
  49. if !repo.canQuery {
  50. return nil, errors.New("Cannot read from database")
  51. }
  52. res := make([]*ints.KubeIntegration, 0)
  53. for _, kubeAM := range repo.kubeIntegrations {
  54. if kubeAM.ProjectID == projectID {
  55. res = append(res, kubeAM)
  56. }
  57. }
  58. return res, nil
  59. }
  60. // BasicIntegrationRepository implements repository.BasicIntegrationRepository
  61. type BasicIntegrationRepository struct {
  62. canQuery bool
  63. basicIntegrations []*ints.BasicIntegration
  64. }
  65. // NewBasicIntegrationRepository will return errors if canQuery is false
  66. func NewBasicIntegrationRepository(canQuery bool) repository.BasicIntegrationRepository {
  67. return &BasicIntegrationRepository{
  68. canQuery,
  69. []*ints.BasicIntegration{},
  70. }
  71. }
  72. // CreateBasicIntegration creates a new basic auth mechanism
  73. func (repo *BasicIntegrationRepository) CreateBasicIntegration(
  74. am *ints.BasicIntegration,
  75. ) (*ints.BasicIntegration, error) {
  76. if !repo.canQuery {
  77. return nil, errors.New("Cannot write database")
  78. }
  79. repo.basicIntegrations = append(repo.basicIntegrations, am)
  80. am.ID = uint(len(repo.basicIntegrations))
  81. return am, nil
  82. }
  83. // ReadBasicIntegration finds a basic auth mechanism by id
  84. func (repo *BasicIntegrationRepository) ReadBasicIntegration(
  85. projectID, id uint,
  86. ) (*ints.BasicIntegration, error) {
  87. if !repo.canQuery {
  88. return nil, errors.New("Cannot read from database")
  89. }
  90. if int(id-1) >= len(repo.basicIntegrations) || repo.basicIntegrations[id-1] == nil {
  91. return nil, gorm.ErrRecordNotFound
  92. }
  93. index := int(id - 1)
  94. return repo.basicIntegrations[index], nil
  95. }
  96. // ListBasicIntegrationsByProjectID finds all basic auth mechanisms
  97. // for a given project id
  98. func (repo *BasicIntegrationRepository) ListBasicIntegrationsByProjectID(
  99. projectID uint,
  100. ) ([]*ints.BasicIntegration, error) {
  101. if !repo.canQuery {
  102. return nil, errors.New("Cannot read from database")
  103. }
  104. res := make([]*ints.BasicIntegration, 0)
  105. for _, basicAM := range repo.basicIntegrations {
  106. if basicAM.ProjectID == projectID {
  107. res = append(res, basicAM)
  108. }
  109. }
  110. return res, nil
  111. }
  112. // OIDCIntegrationRepository implements repository.OIDCIntegrationRepository
  113. type OIDCIntegrationRepository struct {
  114. canQuery bool
  115. oidcIntegrations []*ints.OIDCIntegration
  116. }
  117. // NewOIDCIntegrationRepository will return errors if canQuery is false
  118. func NewOIDCIntegrationRepository(canQuery bool) repository.OIDCIntegrationRepository {
  119. return &OIDCIntegrationRepository{
  120. canQuery,
  121. []*ints.OIDCIntegration{},
  122. }
  123. }
  124. // CreateOIDCIntegration creates a new oidc auth mechanism
  125. func (repo *OIDCIntegrationRepository) CreateOIDCIntegration(
  126. am *ints.OIDCIntegration,
  127. ) (*ints.OIDCIntegration, error) {
  128. if !repo.canQuery {
  129. return nil, errors.New("Cannot write database")
  130. }
  131. repo.oidcIntegrations = append(repo.oidcIntegrations, am)
  132. am.ID = uint(len(repo.oidcIntegrations))
  133. return am, nil
  134. }
  135. // ReadOIDCIntegration finds a oidc auth mechanism by id
  136. func (repo *OIDCIntegrationRepository) ReadOIDCIntegration(
  137. projectID, id uint,
  138. ) (*ints.OIDCIntegration, error) {
  139. if !repo.canQuery {
  140. return nil, errors.New("Cannot read from database")
  141. }
  142. if int(id-1) >= len(repo.oidcIntegrations) || repo.oidcIntegrations[id-1] == nil {
  143. return nil, gorm.ErrRecordNotFound
  144. }
  145. index := int(id - 1)
  146. return repo.oidcIntegrations[index], nil
  147. }
  148. // ListOIDCIntegrationsByProjectID finds all oidc auth mechanisms
  149. // for a given project id
  150. func (repo *OIDCIntegrationRepository) ListOIDCIntegrationsByProjectID(
  151. projectID uint,
  152. ) ([]*ints.OIDCIntegration, error) {
  153. if !repo.canQuery {
  154. return nil, errors.New("Cannot read from database")
  155. }
  156. res := make([]*ints.OIDCIntegration, 0)
  157. for _, oidcAM := range repo.oidcIntegrations {
  158. if oidcAM.ProjectID == projectID {
  159. res = append(res, oidcAM)
  160. }
  161. }
  162. return res, nil
  163. }
  164. // OAuthIntegrationRepository implements repository.OAuthIntegrationRepository
  165. type OAuthIntegrationRepository struct {
  166. canQuery bool
  167. oIntegrations []*ints.OAuthIntegration
  168. }
  169. // NewOAuthIntegrationRepository will return errors if canQuery is false
  170. func NewOAuthIntegrationRepository(canQuery bool) repository.OAuthIntegrationRepository {
  171. return &OAuthIntegrationRepository{
  172. canQuery,
  173. []*ints.OAuthIntegration{},
  174. }
  175. }
  176. // CreateOAuthIntegration creates a new o auth mechanism
  177. func (repo *OAuthIntegrationRepository) CreateOAuthIntegration(
  178. am *ints.OAuthIntegration,
  179. ) (*ints.OAuthIntegration, error) {
  180. if !repo.canQuery {
  181. return nil, errors.New("cannot write database")
  182. }
  183. repo.oIntegrations = append(repo.oIntegrations, am)
  184. am.ID = uint(len(repo.oIntegrations))
  185. return am, nil
  186. }
  187. // ReadOAuthIntegration finds a o auth mechanism by id
  188. func (repo *OAuthIntegrationRepository) ReadOAuthIntegration(
  189. projectID, id uint,
  190. ) (*ints.OAuthIntegration, error) {
  191. if !repo.canQuery {
  192. return nil, errors.New("Cannot read from database")
  193. }
  194. if int(id-1) >= len(repo.oIntegrations) || repo.oIntegrations[id-1] == nil {
  195. return nil, gorm.ErrRecordNotFound
  196. }
  197. index := int(id - 1)
  198. return repo.oIntegrations[index], nil
  199. }
  200. // ListOAuthIntegrationsByProjectID finds all o auth mechanisms
  201. // for a given project id
  202. func (repo *OAuthIntegrationRepository) ListOAuthIntegrationsByProjectID(
  203. projectID uint,
  204. ) ([]*ints.OAuthIntegration, error) {
  205. if !repo.canQuery {
  206. return nil, errors.New("Cannot read from database")
  207. }
  208. res := make([]*ints.OAuthIntegration, 0)
  209. for _, oAM := range repo.oIntegrations {
  210. if oAM.ProjectID == projectID {
  211. res = append(res, oAM)
  212. }
  213. }
  214. return res, nil
  215. }
  216. // UpdateOAuthIntegration updates an oauth integration in the DB
  217. func (repo *OAuthIntegrationRepository) UpdateOAuthIntegration(
  218. am *ints.OAuthIntegration,
  219. ) (*ints.OAuthIntegration, error) {
  220. if !repo.canQuery {
  221. return nil, errors.New("Cannot write database")
  222. }
  223. if int(am.ID-1) >= len(repo.oIntegrations) || repo.oIntegrations[am.ID-1] == nil {
  224. return nil, gorm.ErrRecordNotFound
  225. }
  226. index := int(am.ID - 1)
  227. repo.oIntegrations[index] = am
  228. return am, nil
  229. }
  230. // AWSIntegrationRepository implements repository.AWSIntegrationRepository
  231. type AWSIntegrationRepository struct {
  232. canQuery bool
  233. awsIntegrations []*ints.AWSIntegration
  234. }
  235. // NewAWSIntegrationRepository will return errors if canQuery is false
  236. func NewAWSIntegrationRepository(canQuery bool) repository.AWSIntegrationRepository {
  237. return &AWSIntegrationRepository{
  238. canQuery,
  239. []*ints.AWSIntegration{},
  240. }
  241. }
  242. // CreateAWSIntegration creates a new aws auth mechanism
  243. func (repo *AWSIntegrationRepository) CreateAWSIntegration(
  244. am *ints.AWSIntegration,
  245. ) (*ints.AWSIntegration, error) {
  246. if !repo.canQuery {
  247. return nil, errors.New("Cannot write database")
  248. }
  249. repo.awsIntegrations = append(repo.awsIntegrations, am)
  250. am.ID = uint(len(repo.awsIntegrations))
  251. return am, nil
  252. }
  253. func (repo *AWSIntegrationRepository) OverwriteAWSIntegration(
  254. am *ints.AWSIntegration,
  255. ) (*ints.AWSIntegration, error) {
  256. if !repo.canQuery {
  257. return nil, errors.New("Cannot write database")
  258. }
  259. if int(am.ID-1) >= len(repo.awsIntegrations) || repo.awsIntegrations[am.ID-1] == nil {
  260. return nil, gorm.ErrRecordNotFound
  261. }
  262. index := int(am.ID - 1)
  263. repo.awsIntegrations[index] = am
  264. return am, nil
  265. }
  266. // ReadAWSIntegration finds a aws auth mechanism by id
  267. func (repo *AWSIntegrationRepository) ReadAWSIntegration(
  268. projectID, id uint,
  269. ) (*ints.AWSIntegration, error) {
  270. if !repo.canQuery {
  271. return nil, errors.New("Cannot read from database")
  272. }
  273. if int(id-1) >= len(repo.awsIntegrations) || repo.awsIntegrations[id-1] == nil {
  274. return nil, gorm.ErrRecordNotFound
  275. }
  276. index := int(id - 1)
  277. return repo.awsIntegrations[index], nil
  278. }
  279. // ListAWSIntegrationsByProjectID finds all aws auth mechanisms
  280. // for a given project id
  281. func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
  282. projectID uint,
  283. ) ([]*ints.AWSIntegration, error) {
  284. if !repo.canQuery {
  285. return nil, errors.New("Cannot read from database")
  286. }
  287. res := make([]*ints.AWSIntegration, 0)
  288. for _, awsAM := range repo.awsIntegrations {
  289. if awsAM.ProjectID == projectID {
  290. res = append(res, awsAM)
  291. }
  292. }
  293. return res, nil
  294. }
  295. // GCPIntegrationRepository implements repository.GCPIntegrationRepository
  296. type GCPIntegrationRepository struct {
  297. canQuery bool
  298. gcpIntegrations []*ints.GCPIntegration
  299. }
  300. // NewGCPIntegrationRepository will return errors if canQuery is false
  301. func NewGCPIntegrationRepository(canQuery bool) repository.GCPIntegrationRepository {
  302. return &GCPIntegrationRepository{
  303. canQuery,
  304. []*ints.GCPIntegration{},
  305. }
  306. }
  307. // CreateGCPIntegration creates a new gcp auth mechanism
  308. func (repo *GCPIntegrationRepository) CreateGCPIntegration(
  309. am *ints.GCPIntegration,
  310. ) (*ints.GCPIntegration, error) {
  311. if !repo.canQuery {
  312. return nil, errors.New("Cannot write database")
  313. }
  314. repo.gcpIntegrations = append(repo.gcpIntegrations, am)
  315. am.ID = uint(len(repo.gcpIntegrations))
  316. return am, nil
  317. }
  318. // ReadGCPIntegration finds a gcp auth mechanism by id
  319. func (repo *GCPIntegrationRepository) ReadGCPIntegration(
  320. projectID, id uint,
  321. ) (*ints.GCPIntegration, error) {
  322. if !repo.canQuery {
  323. return nil, errors.New("Cannot read from database")
  324. }
  325. if int(id-1) >= len(repo.gcpIntegrations) || repo.gcpIntegrations[id-1] == nil {
  326. return nil, gorm.ErrRecordNotFound
  327. }
  328. index := int(id - 1)
  329. return repo.gcpIntegrations[index], nil
  330. }
  331. // ListGCPIntegrationsByProjectID finds all gcp auth mechanisms
  332. // for a given project id
  333. func (repo *GCPIntegrationRepository) ListGCPIntegrationsByProjectID(
  334. projectID uint,
  335. ) ([]*ints.GCPIntegration, error) {
  336. if !repo.canQuery {
  337. return nil, errors.New("Cannot read from database")
  338. }
  339. res := make([]*ints.GCPIntegration, 0)
  340. for _, gcpAM := range repo.gcpIntegrations {
  341. if gcpAM.ProjectID == projectID {
  342. res = append(res, gcpAM)
  343. }
  344. }
  345. return res, nil
  346. }
  347. // GithubAppInstallationRepository implements repository.GithubAppInstallationRepository
  348. type GithubAppInstallationRepository struct {
  349. canQuery bool
  350. githubAppInstallations []*ints.GithubAppInstallation
  351. }
  352. func NewGithubAppInstallationRepository(canQuery bool) repository.GithubAppInstallationRepository {
  353. return &GithubAppInstallationRepository{
  354. canQuery,
  355. []*ints.GithubAppInstallation{},
  356. }
  357. }
  358. func (repo *GithubAppInstallationRepository) CreateGithubAppInstallation(am *ints.GithubAppInstallation) (*ints.GithubAppInstallation, error) {
  359. if !repo.canQuery {
  360. return nil, errors.New("cannot write database")
  361. }
  362. repo.githubAppInstallations = append(repo.githubAppInstallations, am)
  363. am.ID = uint(len(repo.githubAppInstallations))
  364. return am, nil
  365. }
  366. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallation(projectID, id uint) (*ints.GithubAppInstallation, error) {
  367. if !repo.canQuery {
  368. return nil, errors.New("cannot write database")
  369. }
  370. if int(id-1) >= len(repo.githubAppInstallations) || repo.githubAppInstallations[id-1] == nil {
  371. return nil, gorm.ErrRecordNotFound
  372. }
  373. return repo.githubAppInstallations[int(id-1)], nil
  374. }
  375. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByInstallationID(gaID uint) (*ints.GithubAppInstallation, error) {
  376. panic("unimplemented")
  377. }
  378. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountID(accountID int64) (*ints.GithubAppInstallation, error) {
  379. if !repo.canQuery {
  380. return nil, errors.New("cannot write database")
  381. }
  382. for _, installation := range repo.githubAppInstallations {
  383. if installation != nil && installation.AccountID == accountID {
  384. return installation, nil
  385. }
  386. }
  387. return nil, gorm.ErrRecordNotFound
  388. }
  389. func (repo *GithubAppInstallationRepository) ReadGithubAppInstallationByAccountIDs(accountIDs []int64) ([]*ints.GithubAppInstallation, error) {
  390. if !repo.canQuery {
  391. return nil, errors.New("cannot write database")
  392. }
  393. ret := make([]*ints.GithubAppInstallation, 0)
  394. for _, installation := range repo.githubAppInstallations {
  395. // O(n^2) can be made into O(n) if this is too slow
  396. for _, id := range accountIDs {
  397. if installation.AccountID == id {
  398. ret = append(ret, installation)
  399. }
  400. }
  401. }
  402. return ret, nil
  403. }
  404. func (repo *GithubAppInstallationRepository) DeleteGithubAppInstallationByAccountID(accountID int64) error {
  405. if !repo.canQuery {
  406. return errors.New("cannot write database")
  407. }
  408. for i, installation := range repo.githubAppInstallations {
  409. if installation != nil && installation.AccountID == accountID {
  410. repo.githubAppInstallations[i] = nil
  411. }
  412. }
  413. return nil
  414. }
  415. type GithubAppOAuthIntegrationRepository struct {
  416. canQuery bool
  417. githubAppOauthIntegrations []*ints.GithubAppOAuthIntegration
  418. }
  419. func NewGithubAppOAuthIntegrationRepository(canQuery bool) repository.GithubAppOAuthIntegrationRepository {
  420. return &GithubAppOAuthIntegrationRepository{
  421. canQuery,
  422. []*ints.GithubAppOAuthIntegration{},
  423. }
  424. }
  425. func (repo *GithubAppOAuthIntegrationRepository) CreateGithubAppOAuthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  426. if !repo.canQuery {
  427. return nil, errors.New("cannot write database")
  428. }
  429. repo.githubAppOauthIntegrations = append(repo.githubAppOauthIntegrations, am)
  430. am.ID = uint(len(repo.githubAppOauthIntegrations))
  431. return am, nil
  432. }
  433. func (repo *GithubAppOAuthIntegrationRepository) ReadGithubAppOauthIntegration(id uint) (*ints.GithubAppOAuthIntegration, error) {
  434. if !repo.canQuery {
  435. return nil, errors.New("cannot write database")
  436. }
  437. if int(id-1) >= len(repo.githubAppOauthIntegrations) || repo.githubAppOauthIntegrations[id-1] == nil {
  438. return nil, gorm.ErrRecordNotFound
  439. }
  440. return repo.githubAppOauthIntegrations[int(id-1)], nil
  441. }
  442. func (repo *GithubAppOAuthIntegrationRepository) UpdateGithubAppOauthIntegration(am *ints.GithubAppOAuthIntegration) (*ints.GithubAppOAuthIntegration, error) {
  443. if !repo.canQuery {
  444. return nil, errors.New("Cannot write database")
  445. }
  446. if int(am.ID-1) >= len(repo.githubAppOauthIntegrations) || repo.githubAppOauthIntegrations[am.ID-1] == nil {
  447. return nil, gorm.ErrRecordNotFound
  448. }
  449. index := int(am.ID - 1)
  450. repo.githubAppOauthIntegrations[index] = am
  451. return am, nil
  452. }
  453. // AzureIntegrationRepository (unimplemented)
  454. type AzureIntegrationRepository struct {
  455. }
  456. // NewAzureIntegrationRepository returns a AzureIntegrationRepository which uses
  457. // gorm.DB for querying the database. It accepts an encryption key to encrypt
  458. // sensitive data
  459. func NewAzureIntegrationRepository() repository.AzureIntegrationRepository {
  460. return &AzureIntegrationRepository{}
  461. }
  462. // CreateAzureIntegration creates a new Azure auth mechanism
  463. func (repo *AzureIntegrationRepository) CreateAzureIntegration(
  464. az *ints.AzureIntegration,
  465. ) (*ints.AzureIntegration, error) {
  466. panic("unimplemented")
  467. }
  468. // OverwriteAzureIntegration overwrites the Azure credential in the DB
  469. func (repo *AzureIntegrationRepository) OverwriteAzureIntegration(
  470. az *ints.AzureIntegration,
  471. ) (*ints.AzureIntegration, error) {
  472. panic("unimplemented")
  473. }
  474. // ReadAzureIntegration finds a Azure auth mechanism by id
  475. func (repo *AzureIntegrationRepository) ReadAzureIntegration(
  476. projectID, id uint,
  477. ) (*ints.AzureIntegration, error) {
  478. panic("unimplemented")
  479. }
  480. // ListAzureIntegrationsByProjectID finds all Azure auth mechanisms
  481. // for a given project id
  482. func (repo *AzureIntegrationRepository) ListAzureIntegrationsByProjectID(
  483. projectID uint,
  484. ) ([]*ints.AzureIntegration, error) {
  485. panic("unimplemented")
  486. }