cluster_test.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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/models"
  7. "gorm.io/gorm"
  8. ints "github.com/porter-dev/porter/internal/models/integrations"
  9. )
  10. func TestClusterLocal(t *testing.T) {
  11. tester := &tester{
  12. dbFileName: "./cluster_local.db",
  13. }
  14. setupTestEnv(tester, t)
  15. initUser(tester, t)
  16. initProject(tester, t)
  17. defer cleanup(tester, t)
  18. // create cluster candidate
  19. ccForm := &forms.CreateClusterCandidatesForm{
  20. ProjectID: tester.initProjects[0].ID,
  21. Kubeconfig: ClusterCAWithData,
  22. IsLocal: true,
  23. }
  24. ccs, err := ccForm.ToClusterCandidates(true)
  25. if err != nil {
  26. t.Fatalf("%v\n", err)
  27. }
  28. var cc *models.ClusterCandidate
  29. for _, _cc := range ccs {
  30. cc, err = tester.repo.Cluster.CreateClusterCandidate(_cc)
  31. if err != nil {
  32. t.Fatalf("%v\n", err)
  33. }
  34. cc, err = tester.repo.Cluster.ReadClusterCandidate(cc.ID)
  35. if err != nil {
  36. t.Fatalf("%v\n", err)
  37. }
  38. }
  39. form := &forms.ResolveClusterForm{
  40. Resolver: &models.ClusterResolverAll{},
  41. ClusterCandidateID: cc.ID,
  42. ProjectID: tester.initProjects[0].ID,
  43. UserID: tester.initUsers[0].ID,
  44. }
  45. // resolve integration (should be kube with local)
  46. err = form.ResolveIntegration(*tester.repo)
  47. if err != nil {
  48. t.Fatalf("%v\n", err)
  49. }
  50. expIntegration := &ints.KubeIntegration{
  51. Mechanism: ints.KubeLocal,
  52. UserID: tester.initUsers[0].ID,
  53. ProjectID: tester.initProjects[0].ID,
  54. Kubeconfig: cc.Kubeconfig,
  55. }
  56. // make sure integration is equal, read integration from DB
  57. gotIntegration, err := tester.repo.KubeIntegration.ReadKubeIntegration(form.IntegrationID)
  58. if err != nil {
  59. t.Fatalf("%v\n", err)
  60. }
  61. // reset got integration model
  62. gotIntegration.Model = gorm.Model{}
  63. if diff := deep.Equal(expIntegration, gotIntegration); diff != nil {
  64. t.Errorf("incorrect integration")
  65. t.Error(diff)
  66. }
  67. // resolve cluster
  68. gotCluster, err := form.ResolveCluster(*tester.repo)
  69. if err != nil {
  70. t.Fatalf("%v\n", err)
  71. }
  72. expCluster := &models.Cluster{
  73. AuthMechanism: models.Local,
  74. ProjectID: 1,
  75. Name: "cluster-test",
  76. Server: "https://localhost",
  77. KubeIntegrationID: 1,
  78. CertificateAuthorityData: []byte("-----BEGIN CER"),
  79. }
  80. gotCluster.Model = gorm.Model{}
  81. if diff := deep.Equal(expCluster, gotCluster); diff != nil {
  82. t.Errorf("incorrect cluster")
  83. t.Error(diff)
  84. }
  85. }
  86. // func TestPopulateServiceAccountBasic(t *testing.T) {
  87. // // create the in-memory repository
  88. // repo := test.NewRepository(true)
  89. // // create a new project
  90. // repo.Project.CreateProject(&models.Project{
  91. // Name: "test-project",
  92. // })
  93. // // create a ServiceAccountCandidate from a kubeconfig
  94. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterCAWithData), false)
  95. // if err != nil {
  96. // t.Fatalf("%v\n", err)
  97. // }
  98. // for _, saCandidate := range saCandidates {
  99. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  100. // }
  101. // // create a new form
  102. // form := forms.ServiceAccountActionResolver{
  103. // ServiceAccountCandidateID: 1,
  104. // }
  105. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  106. // if err != nil {
  107. // t.Fatalf("%v\n", err)
  108. // }
  109. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.SA)
  110. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  111. // if len(sa.Clusters) != 1 {
  112. // t.Fatalf("cluster not written\n")
  113. // }
  114. // if sa.Clusters[0].ServiceAccountID != 1 {
  115. // t.Errorf("service account ID of joined cluster is not 1")
  116. // }
  117. // if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  118. // t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  119. // string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  120. // }
  121. // if sa.Integration != "x509" {
  122. // t.Errorf("service account auth mechanism is not x509")
  123. // }
  124. // if string(sa.ClientCertificateData) != string(decodedStr) {
  125. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  126. // string(sa.ClientCertificateData), string(decodedStr))
  127. // }
  128. // if string(sa.ClientKeyData) != string(decodedStr) {
  129. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  130. // string(sa.ClientKeyData), string(decodedStr))
  131. // }
  132. // }
  133. // func TestPopulateServiceAccountClusterDataAction(t *testing.T) {
  134. // // create the in-memory repository
  135. // repo := test.NewRepository(true)
  136. // // create a new project
  137. // repo.Project.CreateProject(&models.Project{
  138. // Name: "test-project",
  139. // })
  140. // // create a ServiceAccountCandidate from a kubeconfig
  141. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterCAWithoutData), false)
  142. // if err != nil {
  143. // t.Fatalf("%v\n", err)
  144. // }
  145. // for _, saCandidate := range saCandidates {
  146. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  147. // }
  148. // // create a new form
  149. // form := forms.ClusterCADataAction{
  150. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  151. // ServiceAccountCandidateID: 1,
  152. // },
  153. // ClusterCAData: "LS0tLS1CRUdJTiBDRVJ=",
  154. // }
  155. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  156. // if err != nil {
  157. // t.Fatalf("%v\n", err)
  158. // }
  159. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  160. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  161. // if len(sa.Clusters) != 1 {
  162. // t.Fatalf("cluster not written\n")
  163. // }
  164. // if sa.Clusters[0].ServiceAccountID != 1 {
  165. // t.Errorf("service account ID of joined cluster is not 1")
  166. // }
  167. // if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  168. // t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  169. // string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  170. // }
  171. // if sa.Integration != "x509" {
  172. // t.Errorf("service account auth mechanism is not x509")
  173. // }
  174. // if string(sa.ClientCertificateData) != string(decodedStr) {
  175. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  176. // string(sa.ClientCertificateData), string(decodedStr))
  177. // }
  178. // if string(sa.ClientKeyData) != string(decodedStr) {
  179. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  180. // string(sa.ClientKeyData), string(decodedStr))
  181. // }
  182. // }
  183. // func TestPopulateServiceAccountClusterLocalhostAction(t *testing.T) {
  184. // // create the in-memory repository
  185. // repo := test.NewRepository(true)
  186. // // create a new project
  187. // repo.Project.CreateProject(&models.Project{
  188. // Name: "test-project",
  189. // })
  190. // // create a ServiceAccountCandidate from a kubeconfig
  191. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterLocalhost), false)
  192. // if err != nil {
  193. // t.Fatalf("%v\n", err)
  194. // }
  195. // for _, saCandidate := range saCandidates {
  196. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  197. // }
  198. // // create a new form
  199. // form := forms.ClusterLocalhostAction{
  200. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  201. // ServiceAccountCandidateID: 1,
  202. // },
  203. // ClusterHostname: "host.docker.internal",
  204. // }
  205. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  206. // if err != nil {
  207. // t.Fatalf("%v\n", err)
  208. // }
  209. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  210. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  211. // if len(sa.Clusters) != 1 {
  212. // t.Fatalf("cluster not written\n")
  213. // }
  214. // if sa.Clusters[0].ServiceAccountID != 1 {
  215. // t.Errorf("service account ID of joined cluster is not 1")
  216. // }
  217. // if sa.Clusters[0].Server != "https://host.docker.internal:30000" {
  218. // t.Errorf("service account cluster server is incorrect: expected %s, got %s\n",
  219. // "https://host.docker.internal:30000", sa.Clusters[0].Server)
  220. // }
  221. // if sa.Integration != "x509" {
  222. // t.Errorf("service account auth mechanism is not x509")
  223. // }
  224. // if string(sa.ClientCertificateData) != string(decodedStr) {
  225. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  226. // string(sa.ClientCertificateData), string(decodedStr))
  227. // }
  228. // if string(sa.ClientKeyData) != string(decodedStr) {
  229. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  230. // string(sa.ClientKeyData), string(decodedStr))
  231. // }
  232. // }
  233. // func TestPopulateServiceAccountClientCertAction(t *testing.T) {
  234. // // create the in-memory repository
  235. // repo := test.NewRepository(true)
  236. // // create a new project
  237. // repo.Project.CreateProject(&models.Project{
  238. // Name: "test-project",
  239. // })
  240. // // create a ServiceAccountCandidate from a kubeconfig
  241. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClientWithoutCertData), false)
  242. // if err != nil {
  243. // t.Fatalf("%v\n", err)
  244. // }
  245. // for _, saCandidate := range saCandidates {
  246. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  247. // }
  248. // // create a new form
  249. // form := forms.ClientCertDataAction{
  250. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  251. // ServiceAccountCandidateID: 1,
  252. // },
  253. // ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
  254. // }
  255. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  256. // if err != nil {
  257. // t.Fatalf("%v\n", err)
  258. // }
  259. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  260. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  261. // if len(sa.Clusters) != 1 {
  262. // t.Fatalf("cluster not written\n")
  263. // }
  264. // if sa.Clusters[0].ServiceAccountID != 1 {
  265. // t.Errorf("service account ID of joined cluster is not 1")
  266. // }
  267. // if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  268. // t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  269. // string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  270. // }
  271. // if sa.Integration != "x509" {
  272. // t.Errorf("service account auth mechanism is not x509")
  273. // }
  274. // if string(sa.ClientCertificateData) != string(decodedStr) {
  275. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  276. // string(sa.ClientCertificateData), string(decodedStr))
  277. // }
  278. // if string(sa.ClientKeyData) != string(decodedStr) {
  279. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  280. // string(sa.ClientKeyData), string(decodedStr))
  281. // }
  282. // }
  283. // func TestPopulateServiceAccountClientCertAndKeyActions(t *testing.T) {
  284. // // create the in-memory repository
  285. // repo := test.NewRepository(true)
  286. // // create a new project
  287. // repo.Project.CreateProject(&models.Project{
  288. // Name: "test-project",
  289. // })
  290. // // create a ServiceAccountCandidate from a kubeconfig
  291. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClientWithoutCertAndKeyData), false)
  292. // if err != nil {
  293. // t.Fatalf("%v\n", err)
  294. // }
  295. // for _, saCandidate := range saCandidates {
  296. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  297. // }
  298. // // create a new form
  299. // form := forms.ClientCertDataAction{
  300. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  301. // ServiceAccountCandidateID: 1,
  302. // },
  303. // ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
  304. // }
  305. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  306. // if err != nil {
  307. // t.Fatalf("%v\n", err)
  308. // }
  309. // keyForm := forms.ClientKeyDataAction{
  310. // ServiceAccountActionResolver: form.ServiceAccountActionResolver,
  311. // ClientKeyData: "LS0tLS1CRUdJTiBDRVJ=",
  312. // }
  313. // err = keyForm.PopulateServiceAccount(repo.ServiceAccount)
  314. // if err != nil {
  315. // t.Fatalf("%v\n", err)
  316. // }
  317. // sa, err := repo.ServiceAccount.CreateServiceAccount(keyForm.ServiceAccountActionResolver.SA)
  318. // decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  319. // if len(sa.Clusters) != 1 {
  320. // t.Fatalf("cluster not written\n")
  321. // }
  322. // if sa.Clusters[0].ServiceAccountID != 1 {
  323. // t.Errorf("service account ID of joined cluster is not 1")
  324. // }
  325. // if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  326. // t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  327. // string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  328. // }
  329. // if sa.Integration != "x509" {
  330. // t.Errorf("service account auth mechanism is not x509")
  331. // }
  332. // if string(sa.ClientCertificateData) != string(decodedStr) {
  333. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  334. // string(sa.ClientCertificateData), string(decodedStr))
  335. // }
  336. // if string(sa.ClientKeyData) != string(decodedStr) {
  337. // t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  338. // string(sa.ClientKeyData), string(decodedStr))
  339. // }
  340. // }
  341. // func TestPopulateServiceAccountTokenDataAction(t *testing.T) {
  342. // // create the in-memory repository
  343. // repo := test.NewRepository(true)
  344. // tokenData := "abcdefghijklmnop"
  345. // // create a new project
  346. // repo.Project.CreateProject(&models.Project{
  347. // Name: "test-project",
  348. // })
  349. // // create a ServiceAccountCandidate from a kubeconfig
  350. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(BearerTokenWithoutData), false)
  351. // if err != nil {
  352. // t.Fatalf("%v\n", err)
  353. // }
  354. // for _, saCandidate := range saCandidates {
  355. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  356. // }
  357. // // create a new form
  358. // form := forms.TokenDataAction{
  359. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  360. // ServiceAccountCandidateID: 1,
  361. // },
  362. // TokenData: tokenData,
  363. // }
  364. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  365. // if err != nil {
  366. // t.Fatalf("%v\n", err)
  367. // }
  368. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  369. // if len(sa.Clusters) != 1 {
  370. // t.Fatalf("cluster not written\n")
  371. // }
  372. // if sa.Clusters[0].ServiceAccountID != 1 {
  373. // t.Errorf("service account ID of joined cluster is not 1")
  374. // }
  375. // if sa.Integration != models.Bearer {
  376. // t.Errorf("service account auth mechanism is not %s\n", models.Bearer)
  377. // }
  378. // if string(sa.Token) != tokenData {
  379. // t.Errorf("service account token data is wrong: expected %s, got %s\n",
  380. // tokenData, sa.Token)
  381. // }
  382. // }
  383. // func TestPopulateServiceAccountGCPKeyDataAction(t *testing.T) {
  384. // // create the in-memory repository
  385. // repo := test.NewRepository(true)
  386. // gcpKeyData := []byte(`{"key": "data"}`)
  387. // // create a new project
  388. // repo.Project.CreateProject(&models.Project{
  389. // Name: "test-project",
  390. // })
  391. // // create a ServiceAccountCandidate from a kubeconfig
  392. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(GCPPlugin), false)
  393. // if err != nil {
  394. // t.Fatalf("%v\n", err)
  395. // }
  396. // for _, saCandidate := range saCandidates {
  397. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  398. // }
  399. // // create a new form
  400. // form := forms.GCPKeyDataAction{
  401. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  402. // ServiceAccountCandidateID: 1,
  403. // },
  404. // GCPKeyData: string(gcpKeyData),
  405. // }
  406. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  407. // if err != nil {
  408. // t.Fatalf("%v\n", err)
  409. // }
  410. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  411. // if len(sa.Clusters) != 1 {
  412. // t.Fatalf("cluster not written\n")
  413. // }
  414. // if sa.Clusters[0].ServiceAccountID != 1 {
  415. // t.Errorf("service account ID of joined cluster is not 1")
  416. // }
  417. // if sa.Integration != models.GCP {
  418. // t.Errorf("service account auth mechanism is not %s\n", models.GCP)
  419. // }
  420. // if string(sa.GCPKeyData) != string(gcpKeyData) {
  421. // t.Errorf("service account token data is wrong: expected %s, got %s\n",
  422. // string(sa.GCPKeyData), string(gcpKeyData))
  423. // }
  424. // }
  425. // func TestPopulateServiceAccountAWSKeyDataAction(t *testing.T) {
  426. // // create the in-memory repository
  427. // repo := test.NewRepository(true)
  428. // // create a new project
  429. // repo.Project.CreateProject(&models.Project{
  430. // Name: "test-project",
  431. // })
  432. // // create a ServiceAccountCandidate from a kubeconfig
  433. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(AWSEKSGetTokenExec), false)
  434. // if err != nil {
  435. // t.Fatalf("%v\n", err)
  436. // }
  437. // for _, saCandidate := range saCandidates {
  438. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  439. // }
  440. // // create a new form
  441. // form := forms.AWSDataAction{
  442. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  443. // ServiceAccountCandidateID: 1,
  444. // },
  445. // AWSAccessKeyID: "ALSDKJFADSF",
  446. // AWSSecretAccessKey: "ASDLFKJALSDKFJ",
  447. // AWSClusterID: "cluster-test",
  448. // }
  449. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  450. // if err != nil {
  451. // t.Fatalf("%v\n", err)
  452. // }
  453. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  454. // if len(sa.Clusters) != 1 {
  455. // t.Fatalf("cluster not written\n")
  456. // }
  457. // if sa.Clusters[0].ServiceAccountID != 1 {
  458. // t.Errorf("service account ID of joined cluster is not 1")
  459. // }
  460. // if sa.Integration != models.AWS {
  461. // t.Errorf("service account auth mechanism is not %s\n", models.AWS)
  462. // }
  463. // if string(sa.AWSAccessKeyID) != "ALSDKJFADSF" {
  464. // t.Errorf("service account aws access key id is wrong: expected %s, got %s\n",
  465. // "ALSDKJFADSF", sa.AWSAccessKeyID)
  466. // }
  467. // if string(sa.AWSSecretAccessKey) != "ASDLFKJALSDKFJ" {
  468. // t.Errorf("service account aws access secret key is wrong: expected %s, got %s\n",
  469. // "ASDLFKJALSDKFJ", sa.AWSSecretAccessKey)
  470. // }
  471. // if string(sa.AWSClusterID) != "cluster-test" {
  472. // t.Errorf("service account aws cluster id is wrong: expected %s, got %s\n",
  473. // "cluster-test", sa.AWSClusterID)
  474. // }
  475. // }
  476. // func TestPopulateServiceAccountOIDCAction(t *testing.T) {
  477. // // create the in-memory repository
  478. // repo := test.NewRepository(true)
  479. // // create a new project
  480. // repo.Project.CreateProject(&models.Project{
  481. // Name: "test-project",
  482. // })
  483. // // create a ServiceAccountCandidate from a kubeconfig
  484. // saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(OIDCAuthWithoutData), false)
  485. // if err != nil {
  486. // t.Fatalf("%v\n", err)
  487. // }
  488. // for _, saCandidate := range saCandidates {
  489. // repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  490. // }
  491. // // create a new form
  492. // form := forms.OIDCIssuerDataAction{
  493. // ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  494. // ServiceAccountCandidateID: 1,
  495. // },
  496. // OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  497. // }
  498. // err = form.PopulateServiceAccount(repo.ServiceAccount)
  499. // if err != nil {
  500. // t.Fatalf("%v\n", err)
  501. // }
  502. // sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  503. // if len(sa.Clusters) != 1 {
  504. // t.Fatalf("cluster not written\n")
  505. // }
  506. // if sa.Clusters[0].ServiceAccountID != 1 {
  507. // t.Errorf("service account ID of joined cluster is not 1")
  508. // }
  509. // if sa.Integration != models.OIDC {
  510. // t.Errorf("service account auth mechanism is not %s\n", models.OIDC)
  511. // }
  512. // if string(sa.OIDCCertificateAuthorityData) != "LS0tLS1CRUdJTiBDRVJ=" {
  513. // t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  514. // string(sa.OIDCCertificateAuthorityData), "LS0tLS1CRUdJTiBDRVJ=")
  515. // }
  516. // }
  517. const ClusterCAWithData string = `
  518. apiVersion: v1
  519. kind: Config
  520. clusters:
  521. - name: cluster-test
  522. cluster:
  523. server: https://localhost
  524. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  525. contexts:
  526. - context:
  527. cluster: cluster-test
  528. user: test-admin
  529. name: context-test
  530. users:
  531. - name: test-admin
  532. user:
  533. client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
  534. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  535. current-context: context-test
  536. `
  537. const ClusterCAWithoutData string = `
  538. apiVersion: v1
  539. kind: Config
  540. clusters:
  541. - name: cluster-test
  542. cluster:
  543. server: https://localhost
  544. certificate-authority: /fake/path/to/ca.pem
  545. contexts:
  546. - context:
  547. cluster: cluster-test
  548. user: test-admin
  549. name: context-test
  550. users:
  551. - name: test-admin
  552. user:
  553. client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
  554. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  555. current-context: context-test
  556. `
  557. const ClusterLocalhost string = `
  558. apiVersion: v1
  559. kind: Config
  560. clusters:
  561. - name: cluster-test
  562. cluster:
  563. server: https://localhost:30000
  564. contexts:
  565. - context:
  566. cluster: cluster-test
  567. user: test-admin
  568. name: context-test
  569. users:
  570. - name: test-admin
  571. user:
  572. client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
  573. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  574. current-context: context-test
  575. `
  576. const ClientWithoutCertData string = `
  577. apiVersion: v1
  578. kind: Config
  579. clusters:
  580. - name: cluster-test
  581. cluster:
  582. server: https://localhost
  583. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  584. contexts:
  585. - context:
  586. cluster: cluster-test
  587. user: test-admin
  588. name: context-test
  589. users:
  590. - name: test-admin
  591. user:
  592. client-certificate: /fake/path/to/ca.pem
  593. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  594. current-context: context-test
  595. `
  596. const ClientWithoutCertAndKeyData string = `
  597. apiVersion: v1
  598. kind: Config
  599. clusters:
  600. - name: cluster-test
  601. cluster:
  602. server: https://localhost
  603. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  604. contexts:
  605. - context:
  606. cluster: cluster-test
  607. user: test-admin
  608. name: context-test
  609. users:
  610. - name: test-admin
  611. user:
  612. client-certificate: /fake/path/to/ca.pem
  613. client-key: /fake/path/to/ca.pem
  614. current-context: context-test
  615. `
  616. const BearerTokenWithoutData string = `
  617. apiVersion: v1
  618. kind: Config
  619. preferences: {}
  620. current-context: context-test
  621. clusters:
  622. - cluster:
  623. server: https://localhost
  624. name: cluster-test
  625. contexts:
  626. - context:
  627. cluster: cluster-test
  628. user: test-admin
  629. name: context-test
  630. users:
  631. - name: test-admin
  632. user:
  633. tokenFile: /path/to/token/file.txt
  634. `
  635. const GCPPlugin string = `
  636. apiVersion: v1
  637. kind: Config
  638. clusters:
  639. - name: cluster-test
  640. cluster:
  641. server: https://localhost
  642. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  643. users:
  644. - name: test-admin
  645. user:
  646. auth-provider:
  647. name: gcp
  648. contexts:
  649. - context:
  650. cluster: cluster-test
  651. user: test-admin
  652. name: context-test
  653. current-context: context-test
  654. `
  655. const AWSEKSGetTokenExec string = `
  656. apiVersion: v1
  657. clusters:
  658. - cluster:
  659. server: https://localhost
  660. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  661. name: cluster-test
  662. contexts:
  663. - context:
  664. cluster: cluster-test
  665. user: test-admin
  666. name: context-test
  667. current-context: context-test
  668. kind: Config
  669. preferences: {}
  670. users:
  671. - name: test-admin
  672. user:
  673. exec:
  674. apiVersion: client.authentication.k8s.io/v1alpha1
  675. command: aws
  676. args:
  677. - "eks"
  678. - "get-token"
  679. - "--cluster-name"
  680. - "cluster-test"
  681. `
  682. const OIDCAuthWithoutData string = `
  683. apiVersion: v1
  684. clusters:
  685. - cluster:
  686. server: https://localhost
  687. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  688. name: cluster-test
  689. contexts:
  690. - context:
  691. cluster: cluster-test
  692. user: test-admin
  693. name: context-test
  694. current-context: context-test
  695. kind: Config
  696. preferences: {}
  697. users:
  698. - name: test-admin
  699. user:
  700. auth-provider:
  701. config:
  702. client-id: porter-api
  703. id-token: token
  704. idp-issuer-url: https://localhost
  705. idp-certificate-authority: /fake/path/to/ca.pem
  706. name: oidc
  707. `