auth.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. 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. 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. 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. 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. // ReadAWSIntegration finds a aws auth mechanism by id
  254. func (repo *AWSIntegrationRepository) ReadAWSIntegration(
  255. id uint,
  256. ) (*ints.AWSIntegration, error) {
  257. if !repo.canQuery {
  258. return nil, errors.New("Cannot read from database")
  259. }
  260. if int(id-1) >= len(repo.awsIntegrations) || repo.awsIntegrations[id-1] == nil {
  261. return nil, gorm.ErrRecordNotFound
  262. }
  263. index := int(id - 1)
  264. return repo.awsIntegrations[index], nil
  265. }
  266. // ListAWSIntegrationsByProjectID finds all aws auth mechanisms
  267. // for a given project id
  268. func (repo *AWSIntegrationRepository) ListAWSIntegrationsByProjectID(
  269. projectID uint,
  270. ) ([]*ints.AWSIntegration, error) {
  271. if !repo.canQuery {
  272. return nil, errors.New("Cannot read from database")
  273. }
  274. res := make([]*ints.AWSIntegration, 0)
  275. for _, awsAM := range repo.awsIntegrations {
  276. if awsAM.ProjectID == projectID {
  277. res = append(res, awsAM)
  278. }
  279. }
  280. return res, nil
  281. }
  282. // GCPIntegrationRepository implements repository.GCPIntegrationRepository
  283. type GCPIntegrationRepository struct {
  284. canQuery bool
  285. gcpIntegrations []*ints.GCPIntegration
  286. }
  287. // NewGCPIntegrationRepository will return errors if canQuery is false
  288. func NewGCPIntegrationRepository(canQuery bool) repository.GCPIntegrationRepository {
  289. return &GCPIntegrationRepository{
  290. canQuery,
  291. []*ints.GCPIntegration{},
  292. }
  293. }
  294. // CreateGCPIntegration creates a new gcp auth mechanism
  295. func (repo *GCPIntegrationRepository) CreateGCPIntegration(
  296. am *ints.GCPIntegration,
  297. ) (*ints.GCPIntegration, error) {
  298. if !repo.canQuery {
  299. return nil, errors.New("Cannot write database")
  300. }
  301. repo.gcpIntegrations = append(repo.gcpIntegrations, am)
  302. am.ID = uint(len(repo.gcpIntegrations))
  303. return am, nil
  304. }
  305. // ReadGCPIntegration finds a gcp auth mechanism by id
  306. func (repo *GCPIntegrationRepository) ReadGCPIntegration(
  307. id uint,
  308. ) (*ints.GCPIntegration, error) {
  309. if !repo.canQuery {
  310. return nil, errors.New("Cannot read from database")
  311. }
  312. if int(id-1) >= len(repo.gcpIntegrations) || repo.gcpIntegrations[id-1] == nil {
  313. return nil, gorm.ErrRecordNotFound
  314. }
  315. index := int(id - 1)
  316. return repo.gcpIntegrations[index], nil
  317. }
  318. // ListGCPIntegrationsByProjectID finds all gcp auth mechanisms
  319. // for a given project id
  320. func (repo *GCPIntegrationRepository) ListGCPIntegrationsByProjectID(
  321. projectID uint,
  322. ) ([]*ints.GCPIntegration, error) {
  323. if !repo.canQuery {
  324. return nil, errors.New("Cannot read from database")
  325. }
  326. res := make([]*ints.GCPIntegration, 0)
  327. for _, gcpAM := range repo.gcpIntegrations {
  328. if gcpAM.ProjectID == projectID {
  329. res = append(res, gcpAM)
  330. }
  331. }
  332. return res, nil
  333. }