cluster_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. package forms_test
  2. import (
  3. "testing"
  4. "github.com/go-test/deep"
  5. "github.com/porter-dev/porter/internal/forms"
  6. "github.com/porter-dev/porter/internal/kubernetes/fixtures"
  7. "github.com/porter-dev/porter/internal/models"
  8. "gorm.io/gorm"
  9. "k8s.io/client-go/tools/clientcmd"
  10. ints "github.com/porter-dev/porter/internal/models/integrations"
  11. )
  12. type clusterTest struct {
  13. name string
  14. raw string
  15. isLocal bool
  16. resolver *models.ClusterResolverAll
  17. expIntegration interface{}
  18. expCluster *models.Cluster
  19. }
  20. var ClusterTests = []clusterTest{
  21. clusterTest{
  22. name: "local test should preserve kubeconfig",
  23. raw: fixtures.ClusterCAWithData,
  24. isLocal: true,
  25. resolver: &models.ClusterResolverAll{},
  26. expIntegration: &ints.KubeIntegration{
  27. Mechanism: ints.KubeLocal,
  28. UserID: 1,
  29. ProjectID: 1,
  30. Kubeconfig: []byte(fixtures.ClusterCAWithData),
  31. },
  32. expCluster: &models.Cluster{
  33. AuthMechanism: models.Local,
  34. ProjectID: 1,
  35. Name: "cluster-test",
  36. Server: "https://10.10.10.10",
  37. KubeIntegrationID: 1,
  38. CertificateAuthorityData: []byte("-----BEGIN CER"),
  39. },
  40. },
  41. clusterTest{
  42. name: "cluster with data",
  43. raw: fixtures.ClusterCAWithData,
  44. isLocal: false,
  45. resolver: &models.ClusterResolverAll{},
  46. expIntegration: &ints.KubeIntegration{
  47. Mechanism: ints.KubeX509,
  48. UserID: 1,
  49. ProjectID: 1,
  50. ClientCertificateData: []byte("-----BEGIN CER"),
  51. ClientKeyData: []byte("-----BEGIN CER"),
  52. },
  53. expCluster: &models.Cluster{
  54. AuthMechanism: models.X509,
  55. ProjectID: 1,
  56. Name: "cluster-test",
  57. Server: "https://10.10.10.10",
  58. KubeIntegrationID: 2,
  59. CertificateAuthorityData: []byte("-----BEGIN CER"),
  60. },
  61. },
  62. clusterTest{
  63. name: "cluster without data",
  64. raw: fixtures.ClusterCAWithoutData,
  65. isLocal: false,
  66. resolver: &models.ClusterResolverAll{
  67. ClusterCAData: "LS0tLS1CRUdJTiBDRVJ=",
  68. },
  69. expIntegration: &ints.KubeIntegration{
  70. Mechanism: ints.KubeX509,
  71. UserID: 1,
  72. ProjectID: 1,
  73. ClientCertificateData: []byte("-----BEGIN CER"),
  74. ClientKeyData: []byte("-----BEGIN CER"),
  75. },
  76. expCluster: &models.Cluster{
  77. AuthMechanism: models.X509,
  78. ProjectID: 1,
  79. Name: "cluster-test",
  80. Server: "https://10.10.10.10",
  81. KubeIntegrationID: 3,
  82. CertificateAuthorityData: []byte("-----BEGIN CER"),
  83. },
  84. },
  85. clusterTest{
  86. name: "cluster localhost",
  87. raw: fixtures.ClusterLocalhost,
  88. isLocal: false,
  89. resolver: &models.ClusterResolverAll{
  90. ClusterHostname: "example.com",
  91. },
  92. expIntegration: &ints.KubeIntegration{
  93. Mechanism: ints.KubeX509,
  94. UserID: 1,
  95. ProjectID: 1,
  96. ClientCertificateData: []byte("-----BEGIN CER"),
  97. ClientKeyData: []byte("-----BEGIN CER"),
  98. },
  99. expCluster: &models.Cluster{
  100. AuthMechanism: models.X509,
  101. ProjectID: 1,
  102. Name: "cluster-test",
  103. Server: "https://example.com:30000",
  104. KubeIntegrationID: 4,
  105. },
  106. },
  107. clusterTest{
  108. name: "x509 cert and key data",
  109. raw: fixtures.X509WithData,
  110. isLocal: false,
  111. resolver: &models.ClusterResolverAll{},
  112. expIntegration: &ints.KubeIntegration{
  113. Mechanism: ints.KubeX509,
  114. UserID: 1,
  115. ProjectID: 1,
  116. ClientCertificateData: []byte("-----BEGIN CER"),
  117. ClientKeyData: []byte("-----BEGIN CER"),
  118. },
  119. expCluster: &models.Cluster{
  120. AuthMechanism: models.X509,
  121. ProjectID: 1,
  122. Name: "cluster-test",
  123. Server: "https://10.10.10.10",
  124. KubeIntegrationID: 5,
  125. },
  126. },
  127. clusterTest{
  128. name: "x509 no cert data",
  129. raw: fixtures.X509WithoutCertData,
  130. isLocal: false,
  131. resolver: &models.ClusterResolverAll{
  132. ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
  133. },
  134. expIntegration: &ints.KubeIntegration{
  135. Mechanism: ints.KubeX509,
  136. UserID: 1,
  137. ProjectID: 1,
  138. ClientCertificateData: []byte("-----BEGIN CER"),
  139. ClientKeyData: []byte("-----BEGIN CER"),
  140. },
  141. expCluster: &models.Cluster{
  142. AuthMechanism: models.X509,
  143. ProjectID: 1,
  144. Name: "cluster-test",
  145. Server: "https://10.10.10.10",
  146. KubeIntegrationID: 6,
  147. },
  148. },
  149. clusterTest{
  150. name: "x509 no key data",
  151. raw: fixtures.X509WithoutKeyData,
  152. isLocal: false,
  153. resolver: &models.ClusterResolverAll{
  154. ClientKeyData: "LS0tLS1CRUdJTiBDRVJ=",
  155. },
  156. expIntegration: &ints.KubeIntegration{
  157. Mechanism: ints.KubeX509,
  158. UserID: 1,
  159. ProjectID: 1,
  160. ClientCertificateData: []byte("-----BEGIN CER"),
  161. ClientKeyData: []byte("-----BEGIN CER"),
  162. },
  163. expCluster: &models.Cluster{
  164. AuthMechanism: models.X509,
  165. ProjectID: 1,
  166. Name: "cluster-test",
  167. Server: "https://10.10.10.10",
  168. KubeIntegrationID: 7,
  169. },
  170. },
  171. clusterTest{
  172. name: "x509 no cert and key data",
  173. raw: fixtures.X509WithoutCertAndKeyData,
  174. isLocal: false,
  175. resolver: &models.ClusterResolverAll{
  176. ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
  177. ClientKeyData: "LS0tLS1CRUdJTiBDRVJ=",
  178. },
  179. expIntegration: &ints.KubeIntegration{
  180. Mechanism: ints.KubeX509,
  181. UserID: 1,
  182. ProjectID: 1,
  183. ClientCertificateData: []byte("-----BEGIN CER"),
  184. ClientKeyData: []byte("-----BEGIN CER"),
  185. },
  186. expCluster: &models.Cluster{
  187. AuthMechanism: models.X509,
  188. ProjectID: 1,
  189. Name: "cluster-test",
  190. Server: "https://10.10.10.10",
  191. KubeIntegrationID: 8,
  192. },
  193. },
  194. clusterTest{
  195. name: "bearer token with data",
  196. raw: fixtures.BearerTokenWithData,
  197. isLocal: false,
  198. resolver: &models.ClusterResolverAll{},
  199. expIntegration: &ints.KubeIntegration{
  200. Mechanism: ints.KubeBearer,
  201. UserID: 1,
  202. ProjectID: 1,
  203. Token: []byte("LS0tLS1CRUdJTiBDRVJ="),
  204. },
  205. expCluster: &models.Cluster{
  206. AuthMechanism: models.Bearer,
  207. ProjectID: 1,
  208. Name: "cluster-test",
  209. Server: "https://10.10.10.10",
  210. KubeIntegrationID: 9,
  211. },
  212. },
  213. clusterTest{
  214. name: "bearer token without data",
  215. raw: fixtures.BearerTokenWithoutData,
  216. isLocal: false,
  217. resolver: &models.ClusterResolverAll{
  218. TokenData: "tokentoken",
  219. },
  220. expIntegration: &ints.KubeIntegration{
  221. Mechanism: ints.KubeBearer,
  222. UserID: 1,
  223. ProjectID: 1,
  224. Token: []byte("tokentoken"),
  225. },
  226. expCluster: &models.Cluster{
  227. AuthMechanism: models.Bearer,
  228. ProjectID: 1,
  229. Name: "cluster-test",
  230. Server: "https://10.10.10.10",
  231. KubeIntegrationID: 10,
  232. },
  233. },
  234. clusterTest{
  235. name: "basic auth",
  236. raw: fixtures.BasicAuth,
  237. isLocal: false,
  238. resolver: &models.ClusterResolverAll{},
  239. expIntegration: &ints.KubeIntegration{
  240. Mechanism: ints.KubeBasic,
  241. UserID: 1,
  242. ProjectID: 1,
  243. Username: []byte("admin"),
  244. Password: []byte("changeme"),
  245. },
  246. expCluster: &models.Cluster{
  247. AuthMechanism: models.Basic,
  248. ProjectID: 1,
  249. Name: "cluster-test",
  250. Server: "https://10.10.10.10",
  251. KubeIntegrationID: 11,
  252. CertificateAuthorityData: []byte("-----BEGIN CER"),
  253. },
  254. },
  255. clusterTest{
  256. name: "gcp plugin",
  257. raw: fixtures.GCPPlugin,
  258. isLocal: false,
  259. resolver: &models.ClusterResolverAll{
  260. GCPKeyData: `{"key":"data"}`,
  261. },
  262. expIntegration: &ints.GCPIntegration{
  263. UserID: 1,
  264. ProjectID: 1,
  265. GCPKeyData: []byte(`{"key":"data"}`),
  266. },
  267. expCluster: &models.Cluster{
  268. AuthMechanism: models.GCP,
  269. ProjectID: 1,
  270. Name: "cluster-test",
  271. Server: "https://10.10.10.10",
  272. GCPIntegrationID: 1,
  273. CertificateAuthorityData: []byte("-----BEGIN CER"),
  274. },
  275. },
  276. clusterTest{
  277. name: "aws iam authenticator",
  278. raw: fixtures.AWSIamAuthenticatorExec,
  279. isLocal: false,
  280. resolver: &models.ClusterResolverAll{
  281. AWSAccessKeyID: "accesskey",
  282. AWSClusterID: "cluster-test-aws-id-guess",
  283. AWSSecretAccessKey: "secret",
  284. },
  285. expIntegration: &ints.AWSIntegration{
  286. UserID: 1,
  287. ProjectID: 1,
  288. AWSAccessKeyID: []byte("accesskey"),
  289. AWSClusterID: []byte("cluster-test-aws-id-guess"),
  290. AWSSecretAccessKey: []byte("secret"),
  291. },
  292. expCluster: &models.Cluster{
  293. AuthMechanism: models.AWS,
  294. ProjectID: 1,
  295. Name: "cluster-test",
  296. Server: "https://10.10.10.10",
  297. AWSIntegrationID: 1,
  298. CertificateAuthorityData: []byte("-----BEGIN CER"),
  299. },
  300. },
  301. clusterTest{
  302. name: "aws eks get token",
  303. raw: fixtures.AWSEKSGetTokenExec,
  304. isLocal: false,
  305. resolver: &models.ClusterResolverAll{
  306. AWSAccessKeyID: "accesskey",
  307. AWSClusterID: "cluster-test-aws-id-guess",
  308. AWSSecretAccessKey: "secret",
  309. },
  310. expIntegration: &ints.AWSIntegration{
  311. UserID: 1,
  312. ProjectID: 1,
  313. AWSAccessKeyID: []byte("accesskey"),
  314. AWSClusterID: []byte("cluster-test-aws-id-guess"),
  315. AWSSecretAccessKey: []byte("secret"),
  316. },
  317. expCluster: &models.Cluster{
  318. AuthMechanism: models.AWS,
  319. ProjectID: 1,
  320. Name: "cluster-test",
  321. Server: "https://10.10.10.10",
  322. AWSIntegrationID: 2,
  323. CertificateAuthorityData: []byte("-----BEGIN CER"),
  324. },
  325. },
  326. clusterTest{
  327. name: "oidc without idp issuer data",
  328. raw: fixtures.OIDCAuthWithoutData,
  329. isLocal: false,
  330. resolver: &models.ClusterResolverAll{
  331. OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  332. },
  333. expIntegration: &ints.OIDCIntegration{
  334. Client: ints.OIDCKube,
  335. UserID: 1,
  336. ProjectID: 1,
  337. IssuerURL: []byte("https://10.10.10.10"),
  338. ClientID: []byte("porter-api"),
  339. CertificateAuthorityData: []byte("LS0tLS1CRUdJTiBDRVJ="),
  340. IDToken: []byte("token"),
  341. },
  342. expCluster: &models.Cluster{
  343. AuthMechanism: models.OIDC,
  344. ProjectID: 1,
  345. Name: "cluster-test",
  346. Server: "https://10.10.10.10",
  347. OIDCIntegrationID: 1,
  348. CertificateAuthorityData: []byte("-----BEGIN CER"),
  349. },
  350. },
  351. clusterTest{
  352. name: "oidc with idp issuer data",
  353. raw: fixtures.OIDCAuthWithData,
  354. isLocal: false,
  355. resolver: &models.ClusterResolverAll{},
  356. expIntegration: &ints.OIDCIntegration{
  357. Client: ints.OIDCKube,
  358. UserID: 1,
  359. ProjectID: 1,
  360. IssuerURL: []byte("https://10.10.10.10"),
  361. ClientID: []byte("porter-api"),
  362. CertificateAuthorityData: []byte("LS0tLS1CRUdJTiBDRVJ="),
  363. IDToken: []byte("token"),
  364. },
  365. expCluster: &models.Cluster{
  366. AuthMechanism: models.OIDC,
  367. ProjectID: 1,
  368. Name: "cluster-test",
  369. Server: "https://10.10.10.10",
  370. OIDCIntegrationID: 2,
  371. CertificateAuthorityData: []byte("-----BEGIN CER"),
  372. },
  373. },
  374. }
  375. func TestClusters(t *testing.T) {
  376. tester := &tester{
  377. dbFileName: "./cluster_test.db",
  378. }
  379. setupTestEnv(tester, t)
  380. initUser(tester, t)
  381. initProject(tester, t)
  382. defer cleanup(tester, t)
  383. for _, c := range ClusterTests {
  384. // create cluster candidate
  385. ccForm := &forms.CreateClusterCandidatesForm{
  386. ProjectID: tester.initProjects[0].ID,
  387. Kubeconfig: c.raw,
  388. IsLocal: c.isLocal,
  389. }
  390. ccs, err := ccForm.ToClusterCandidates(c.isLocal)
  391. if err != nil {
  392. t.Fatalf("%v\n", err)
  393. }
  394. var cc *models.ClusterCandidate
  395. for _, _cc := range ccs {
  396. cc, err = tester.repo.Cluster.CreateClusterCandidate(_cc)
  397. if err != nil {
  398. t.Fatalf("%v\n", err)
  399. }
  400. cc, err = tester.repo.Cluster.ReadClusterCandidate(cc.ID)
  401. if err != nil {
  402. t.Fatalf("%v\n", err)
  403. }
  404. }
  405. form := &forms.ResolveClusterForm{
  406. Resolver: c.resolver,
  407. ClusterCandidateID: cc.ID,
  408. ProjectID: tester.initProjects[0].ID,
  409. UserID: tester.initUsers[0].ID,
  410. }
  411. // resolve integration (should be kube with local)
  412. err = form.ResolveIntegration(*tester.repo)
  413. if err != nil {
  414. t.Fatalf("%v\n", err)
  415. }
  416. switch c.expIntegration.(type) {
  417. case *ints.KubeIntegration:
  418. // make sure integration is equal, read integration from DB
  419. gotIntegration, err := tester.repo.KubeIntegration.ReadKubeIntegration(form.IntegrationID)
  420. if err != nil {
  421. t.Fatalf("%v\n", err)
  422. }
  423. // reset got integration model
  424. gotIntegration.Model = gorm.Model{}
  425. ki, _ := c.expIntegration.(*ints.KubeIntegration)
  426. // if kubeconfig, compare
  427. if len(ki.Kubeconfig) > 0 {
  428. compareKubeconfig(t, gotIntegration.Kubeconfig, ki.Kubeconfig)
  429. // reset kubeconfig fields for deep.Equal
  430. gotIntegration.Kubeconfig = []byte{}
  431. ki.Kubeconfig = []byte{}
  432. }
  433. if diff := deep.Equal(ki, gotIntegration); diff != nil {
  434. t.Errorf("incorrect kube integration")
  435. t.Error(diff)
  436. }
  437. case *ints.OIDCIntegration:
  438. // make sure integration is equal, read integration from DB
  439. gotIntegration, err := tester.repo.OIDCIntegration.ReadOIDCIntegration(form.IntegrationID)
  440. if err != nil {
  441. t.Fatalf("%v\n", err)
  442. }
  443. // reset got integration model
  444. gotIntegration.Model = gorm.Model{}
  445. oidc, _ := c.expIntegration.(*ints.OIDCIntegration)
  446. if diff := deep.Equal(oidc, gotIntegration); diff != nil {
  447. t.Errorf("incorrect oidc integration")
  448. t.Error(diff)
  449. }
  450. case *ints.GCPIntegration:
  451. // make sure integration is equal, read integration from DB
  452. gotIntegration, err := tester.repo.GCPIntegration.ReadGCPIntegration(form.IntegrationID)
  453. if err != nil {
  454. t.Fatalf("%v\n", err)
  455. }
  456. // reset got integration model
  457. gotIntegration.Model = gorm.Model{}
  458. gcp, _ := c.expIntegration.(*ints.GCPIntegration)
  459. if diff := deep.Equal(gcp, gotIntegration); diff != nil {
  460. t.Errorf("incorrect gcp integration")
  461. t.Error(diff)
  462. }
  463. case *ints.AWSIntegration:
  464. // make sure integration is equal, read integration from DB
  465. gotIntegration, err := tester.repo.AWSIntegration.ReadAWSIntegration(form.IntegrationID)
  466. if err != nil {
  467. t.Fatalf("%v\n", err)
  468. }
  469. // reset got integration model
  470. gotIntegration.Model = gorm.Model{}
  471. aws, _ := c.expIntegration.(*ints.AWSIntegration)
  472. if diff := deep.Equal(aws, gotIntegration); diff != nil {
  473. t.Errorf("incorrect aws integration")
  474. t.Error(diff)
  475. }
  476. }
  477. // resolve cluster
  478. gotCluster, err := form.ResolveCluster(*tester.repo)
  479. if err != nil {
  480. t.Fatalf("%v\n", err)
  481. }
  482. gotCluster.Model = gorm.Model{}
  483. if diff := deep.Equal(c.expCluster, gotCluster); diff != nil {
  484. t.Errorf("incorrect cluster")
  485. t.Error(diff)
  486. }
  487. }
  488. }
  489. func compareKubeconfig(t *testing.T, resKube []byte, expKube []byte) {
  490. // compare kubeconfig by transforming into a client config
  491. resConfig, _ := clientcmd.NewClientConfigFromBytes(resKube)
  492. expConfig, err := clientcmd.NewClientConfigFromBytes(expKube)
  493. if err != nil {
  494. t.Fatalf("config from bytes, error occurred %v\n", err)
  495. }
  496. resRawConf, _ := resConfig.RawConfig()
  497. expRawConf, err := expConfig.RawConfig()
  498. if err != nil {
  499. t.Fatalf("raw config conversion, error occurred %v\n", err)
  500. }
  501. if diff := deep.Equal(expRawConf, resRawConf); diff != nil {
  502. t.Errorf("incorrect kubeconfigs")
  503. t.Error(diff)
  504. }
  505. }
  506. // func TestPopulateServiceAccountClusterDataAction(t *testing.T) {
  507. // // create the in-memory repository
  508. // repo := test.NewRepository(true)
  509. // // create a new project
  510. // repo.Project.CreateProject(&models.Project{
  511. // Name: "test-project",
  512. // })
  513. // // create a ServiceAccountCandidate from a kubeconfig
  514. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterCAWithoutData), false)
  515. // if err != nil {
  516. // t.Fatalf("%v\n", err)
  517. // }
  518. // for _, saCandidate := range saCandidates {
  519. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  520. // }
  521. // // create a new form
  522. // form := forms.ClusterCADataAction{
  523. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  524. // ServiceAccountCandidateID: 1,
  525. // },
  526. // ClusterCAData: "LS0tLS1CRUdJTiBDRVJ=",
  527. // }
  528. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  529. // if err != nil {
  530. // t.Fatalf("%v\n", err)
  531. // }
  532. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  533. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  534. // if len(sa.Clusters) != 1 {
  535. // t.Fatalf("cluster not written\n")
  536. // }
  537. // if sa.Clusters[0].ServiceAccountID != 1 {
  538. // t.Errorf("service account ID of joined cluster is not 1")
  539. // }
  540. // if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  541. // t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  542. // string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  543. // }
  544. // if sa.Integration != "x509" {
  545. // t.Errorf("service account auth mechanism is not x509")
  546. // }
  547. // if string(sa.ClientCertificateData) != string(decodedStr) {
  548. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  549. // string(sa.ClientCertificateData), string(decodedStr))
  550. // }
  551. // if string(sa.ClientKeyData) != string(decodedStr) {
  552. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  553. // string(sa.ClientKeyData), string(decodedStr))
  554. // }
  555. // }
  556. // func TestPopulateServiceAccountClusterLocalhostAction(t *testing.T) {
  557. // // create the in-memory repository
  558. // repo := test.NewRepository(true)
  559. // // create a new project
  560. // repo.Project.CreateProject(&models.Project{
  561. // Name: "test-project",
  562. // })
  563. // // create a ServiceAccountCandidate from a kubeconfig
  564. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterLocalhost), false)
  565. // if err != nil {
  566. // t.Fatalf("%v\n", err)
  567. // }
  568. // for _, saCandidate := range saCandidates {
  569. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  570. // }
  571. // // create a new form
  572. // form := forms.ClusterLocalhostAction{
  573. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  574. // ServiceAccountCandidateID: 1,
  575. // },
  576. // ClusterHostname: "host.docker.internal",
  577. // }
  578. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  579. // if err != nil {
  580. // t.Fatalf("%v\n", err)
  581. // }
  582. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  583. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  584. // if len(sa.Clusters) != 1 {
  585. // t.Fatalf("cluster not written\n")
  586. // }
  587. // if sa.Clusters[0].ServiceAccountID != 1 {
  588. // t.Errorf("service account ID of joined cluster is not 1")
  589. // }
  590. // if sa.Clusters[0].Server != "https://host.docker.internal:30000" {
  591. // t.Errorf("service account cluster server is incorrect: expected %s, got %s\n",
  592. // "https://host.docker.internal:30000", sa.Clusters[0].Server)
  593. // }
  594. // if sa.Integration != "x509" {
  595. // t.Errorf("service account auth mechanism is not x509")
  596. // }
  597. // if string(sa.ClientCertificateData) != string(decodedStr) {
  598. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  599. // string(sa.ClientCertificateData), string(decodedStr))
  600. // }
  601. // if string(sa.ClientKeyData) != string(decodedStr) {
  602. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  603. // string(sa.ClientKeyData), string(decodedStr))
  604. // }
  605. // }
  606. // func TestPopulateServiceAccountClientCertAction(t *testing.T) {
  607. // // create the in-memory repository
  608. // repo := test.NewRepository(true)
  609. // // create a new project
  610. // repo.Project.CreateProject(&models.Project{
  611. // Name: "test-project",
  612. // })
  613. // // create a ServiceAccountCandidate from a kubeconfig
  614. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClientWithoutCertData), false)
  615. // if err != nil {
  616. // t.Fatalf("%v\n", err)
  617. // }
  618. // for _, saCandidate := range saCandidates {
  619. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  620. // }
  621. // // create a new form
  622. // form := forms.ClientCertDataAction{
  623. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  624. // ServiceAccountCandidateID: 1,
  625. // },
  626. // ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
  627. // }
  628. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  629. // if err != nil {
  630. // t.Fatalf("%v\n", err)
  631. // }
  632. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  633. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  634. // if len(sa.Clusters) != 1 {
  635. // t.Fatalf("cluster not written\n")
  636. // }
  637. // if sa.Clusters[0].ServiceAccountID != 1 {
  638. // t.Errorf("service account ID of joined cluster is not 1")
  639. // }
  640. // if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  641. // t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  642. // string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  643. // }
  644. // if sa.Integration != "x509" {
  645. // t.Errorf("service account auth mechanism is not x509")
  646. // }
  647. // if string(sa.ClientCertificateData) != string(decodedStr) {
  648. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  649. // string(sa.ClientCertificateData), string(decodedStr))
  650. // }
  651. // if string(sa.ClientKeyData) != string(decodedStr) {
  652. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  653. // string(sa.ClientKeyData), string(decodedStr))
  654. // }
  655. // }
  656. // func TestPopulateServiceAccountClientCertAndKeyActions(t *testing.T) {
  657. // // create the in-memory repository
  658. // repo := test.NewRepository(true)
  659. // // create a new project
  660. // repo.Project.CreateProject(&models.Project{
  661. // Name: "test-project",
  662. // })
  663. // // create a ServiceAccountCandidate from a kubeconfig
  664. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClientWithoutCertAndKeyData), false)
  665. // if err != nil {
  666. // t.Fatalf("%v\n", err)
  667. // }
  668. // for _, saCandidate := range saCandidates {
  669. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  670. // }
  671. // // create a new form
  672. // form := forms.ClientCertDataAction{
  673. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  674. // ServiceAccountCandidateID: 1,
  675. // },
  676. // ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
  677. // }
  678. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  679. // if err != nil {
  680. // t.Fatalf("%v\n", err)
  681. // }
  682. // keyForm := forms.ClientKeyDataAction{
  683. // ServiceAccountActionResolver: form.ServiceAccountActionResolver,
  684. // ClientKeyData: "LS0tLS1CRUdJTiBDRVJ=",
  685. // }
  686. // err = keyForm.PopulateServiceAccount(repo.ServiceAccount)
  687. // if err != nil {
  688. // t.Fatalf("%v\n", err)
  689. // }
  690. // sa, err := repo.ServiceAccount.CreateServiceAccount(keyForm.ServiceAccountActionResolver.SA)
  691. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  692. // if len(sa.Clusters) != 1 {
  693. // t.Fatalf("cluster not written\n")
  694. // }
  695. // if sa.Clusters[0].ServiceAccountID != 1 {
  696. // t.Errorf("service account ID of joined cluster is not 1")
  697. // }
  698. // if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  699. // t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  700. // string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  701. // }
  702. // if sa.Integration != "x509" {
  703. // t.Errorf("service account auth mechanism is not x509")
  704. // }
  705. // if string(sa.ClientCertificateData) != string(decodedStr) {
  706. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  707. // string(sa.ClientCertificateData), string(decodedStr))
  708. // }
  709. // if string(sa.ClientKeyData) != string(decodedStr) {
  710. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  711. // string(sa.ClientKeyData), string(decodedStr))
  712. // }
  713. // }
  714. // func TestPopulateServiceAccountTokenDataAction(t *testing.T) {
  715. // // create the in-memory repository
  716. // repo := test.NewRepository(true)
  717. // tokenData := "abcdefghijklmnop"
  718. // // create a new project
  719. // repo.Project.CreateProject(&models.Project{
  720. // Name: "test-project",
  721. // })
  722. // // create a ServiceAccountCandidate from a kubeconfig
  723. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(BearerTokenWithoutData), false)
  724. // if err != nil {
  725. // t.Fatalf("%v\n", err)
  726. // }
  727. // for _, saCandidate := range saCandidates {
  728. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  729. // }
  730. // // create a new form
  731. // form := forms.TokenDataAction{
  732. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  733. // ServiceAccountCandidateID: 1,
  734. // },
  735. // TokenData: tokenData,
  736. // }
  737. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  738. // if err != nil {
  739. // t.Fatalf("%v\n", err)
  740. // }
  741. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  742. // if len(sa.Clusters) != 1 {
  743. // t.Fatalf("cluster not written\n")
  744. // }
  745. // if sa.Clusters[0].ServiceAccountID != 1 {
  746. // t.Errorf("service account ID of joined cluster is not 1")
  747. // }
  748. // if sa.Integration != models.Bearer {
  749. // t.Errorf("service account auth mechanism is not %s\n", models.Bearer)
  750. // }
  751. // if string(sa.Token) != tokenData {
  752. // t.Errorf("service account token data is wrong: expected %s, got %s\n",
  753. // tokenData, sa.Token)
  754. // }
  755. // }
  756. // func TestPopulateServiceAccountGCPKeyDataAction(t *testing.T) {
  757. // // create the in-memory repository
  758. // repo := test.NewRepository(true)
  759. // gcpKeyData := []byte(`{"key": "data"}`)
  760. // // create a new project
  761. // repo.Project.CreateProject(&models.Project{
  762. // Name: "test-project",
  763. // })
  764. // // create a ServiceAccountCandidate from a kubeconfig
  765. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(GCPPlugin), false)
  766. // if err != nil {
  767. // t.Fatalf("%v\n", err)
  768. // }
  769. // for _, saCandidate := range saCandidates {
  770. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  771. // }
  772. // // create a new form
  773. // form := forms.GCPKeyDataAction{
  774. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  775. // ServiceAccountCandidateID: 1,
  776. // },
  777. // GCPKeyData: string(gcpKeyData),
  778. // }
  779. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  780. // if err != nil {
  781. // t.Fatalf("%v\n", err)
  782. // }
  783. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  784. // if len(sa.Clusters) != 1 {
  785. // t.Fatalf("cluster not written\n")
  786. // }
  787. // if sa.Clusters[0].ServiceAccountID != 1 {
  788. // t.Errorf("service account ID of joined cluster is not 1")
  789. // }
  790. // if sa.Integration != models.GCP {
  791. // t.Errorf("service account auth mechanism is not %s\n", models.GCP)
  792. // }
  793. // if string(sa.GCPKeyData) != string(gcpKeyData) {
  794. // t.Errorf("service account token data is wrong: expected %s, got %s\n",
  795. // string(sa.GCPKeyData), string(gcpKeyData))
  796. // }
  797. // }
  798. // func TestPopulateServiceAccountAWSKeyDataAction(t *testing.T) {
  799. // // create the in-memory repository
  800. // repo := test.NewRepository(true)
  801. // // create a new project
  802. // repo.Project.CreateProject(&models.Project{
  803. // Name: "test-project",
  804. // })
  805. // // create a ServiceAccountCandidate from a kubeconfig
  806. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(AWSEKSGetTokenExec), false)
  807. // if err != nil {
  808. // t.Fatalf("%v\n", err)
  809. // }
  810. // for _, saCandidate := range saCandidates {
  811. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  812. // }
  813. // // create a new form
  814. // form := forms.AWSDataAction{
  815. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  816. // ServiceAccountCandidateID: 1,
  817. // },
  818. // AWSAccessKeyID: "ALSDKJFADSF",
  819. // AWSSecretAccessKey: "ASDLFKJALSDKFJ",
  820. // AWSClusterID: "cluster-test",
  821. // }
  822. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  823. // if err != nil {
  824. // t.Fatalf("%v\n", err)
  825. // }
  826. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  827. // if len(sa.Clusters) != 1 {
  828. // t.Fatalf("cluster not written\n")
  829. // }
  830. // if sa.Clusters[0].ServiceAccountID != 1 {
  831. // t.Errorf("service account ID of joined cluster is not 1")
  832. // }
  833. // if sa.Integration != models.AWS {
  834. // t.Errorf("service account auth mechanism is not %s\n", models.AWS)
  835. // }
  836. // if string(sa.AWSAccessKeyID) != "ALSDKJFADSF" {
  837. // t.Errorf("service account aws access key id is wrong: expected %s, got %s\n",
  838. // "ALSDKJFADSF", sa.AWSAccessKeyID)
  839. // }
  840. // if string(sa.AWSSecretAccessKey) != "ASDLFKJALSDKFJ" {
  841. // t.Errorf("service account aws access secret key is wrong: expected %s, got %s\n",
  842. // "ASDLFKJALSDKFJ", sa.AWSSecretAccessKey)
  843. // }
  844. // if string(sa.AWSClusterID) != "cluster-test" {
  845. // t.Errorf("service account aws cluster id is wrong: expected %s, got %s\n",
  846. // "cluster-test", sa.AWSClusterID)
  847. // }
  848. // }
  849. // func TestPopulateServiceAccountOIDCAction(t *testing.T) {
  850. // // create the in-memory repository
  851. // repo := test.NewRepository(true)
  852. // // create a new project
  853. // repo.Project.CreateProject(&models.Project{
  854. // Name: "test-project",
  855. // })
  856. // // create a ServiceAccountCandidate from a kubeconfig
  857. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(OIDCAuthWithoutData), false)
  858. // if err != nil {
  859. // t.Fatalf("%v\n", err)
  860. // }
  861. // for _, saCandidate := range saCandidates {
  862. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  863. // }
  864. // // create a new form
  865. // form := forms.OIDCIssuerDataAction{
  866. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  867. // ServiceAccountCandidateID: 1,
  868. // },
  869. // OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  870. // }
  871. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  872. // if err != nil {
  873. // t.Fatalf("%v\n", err)
  874. // }
  875. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  876. // if len(sa.Clusters) != 1 {
  877. // t.Fatalf("cluster not written\n")
  878. // }
  879. // if sa.Clusters[0].ServiceAccountID != 1 {
  880. // t.Errorf("service account ID of joined cluster is not 1")
  881. // }
  882. // if sa.Integration != models.OIDC {
  883. // t.Errorf("service account auth mechanism is not %s\n", models.OIDC)
  884. // }
  885. // if string(sa.OIDCCertificateAuthorityData) != "LS0tLS1CRUdJTiBDRVJ=" {
  886. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  887. // string(sa.OIDCCertificateAuthorityData), "LS0tLS1CRUdJTiBDRVJ=")
  888. // }
  889. // }