kubeconfig_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. package kubernetes_test
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. "github.com/porter-dev/porter/internal/kubernetes"
  7. "github.com/porter-dev/porter/internal/models"
  8. "k8s.io/client-go/tools/clientcmd"
  9. )
  10. type kubeConfigTest struct {
  11. msg string
  12. raw []byte
  13. allowedContexts []string
  14. expected []models.Context
  15. }
  16. type kubeConfigTestValidateError struct {
  17. msg string
  18. raw []byte
  19. allowedContexts []string
  20. contextName string
  21. errorContains string // a string that the error message should contain
  22. }
  23. var ValidateErrorTests = []kubeConfigTestValidateError{
  24. kubeConfigTestValidateError{
  25. msg: "No configuration",
  26. raw: []byte(""),
  27. allowedContexts: []string{},
  28. contextName: "",
  29. errorContains: "invalid configuration: no configuration has been provided",
  30. },
  31. kubeConfigTestValidateError{
  32. msg: "Context name does not exist",
  33. raw: []byte(noContexts),
  34. allowedContexts: []string{"porter-test-1"},
  35. contextName: "context-test",
  36. errorContains: "invalid configuration: context was not found for specified context: context-test",
  37. },
  38. kubeConfigTestValidateError{
  39. msg: "Cluster to join does not exist",
  40. raw: []byte(noClusters),
  41. allowedContexts: []string{"porter-test-1"},
  42. contextName: "context-test",
  43. errorContains: "invalid configuration: context was not found for specified context: context-test",
  44. },
  45. kubeConfigTestValidateError{
  46. msg: "User to join does not exist",
  47. raw: []byte(noUsers),
  48. allowedContexts: []string{"porter-test-1"},
  49. contextName: "context-test",
  50. errorContains: "invalid configuration: context was not found for specified context: context-test",
  51. },
  52. }
  53. func TestValidateErrors(t *testing.T) {
  54. for _, c := range ValidateErrorTests {
  55. _, err := kubernetes.GetRestrictedClientConfigFromBytes(c.raw, c.contextName, c.allowedContexts)
  56. if err == nil {
  57. t.Fatalf("Testing %s did not return an error\n", c.msg)
  58. }
  59. if !strings.Contains(err.Error(), c.errorContains) {
  60. t.Errorf("Testing %s -- Error was:\n \"%s\" \n It did not contain string \"%s\"\n", c.msg, err.Error(), c.errorContains)
  61. }
  62. }
  63. }
  64. var BasicContextAllowedTests = []kubeConfigTest{
  65. kubeConfigTest{
  66. msg: "basic test",
  67. raw: []byte(basic),
  68. allowedContexts: []string{"context-test"},
  69. expected: []models.Context{
  70. models.Context{
  71. Name: "context-test",
  72. Server: "https://localhost",
  73. Cluster: "cluster-test",
  74. User: "test-admin",
  75. Selected: true,
  76. },
  77. },
  78. },
  79. }
  80. func TestBasicAllowed(t *testing.T) {
  81. for _, c := range BasicContextAllowedTests {
  82. res, err := kubernetes.GetContextsFromBytes(c.raw, c.allowedContexts)
  83. if err != nil {
  84. t.Fatalf("Testing %s returned an error: %v\n", c.msg, err.Error())
  85. }
  86. isEqual := reflect.DeepEqual(c.expected, res)
  87. if !isEqual {
  88. t.Errorf("Testing: %s, Expected: %v, Got: %v\n", c.msg, c.expected, res)
  89. }
  90. }
  91. }
  92. var BasicContextAllTests = []kubeConfigTest{
  93. kubeConfigTest{
  94. msg: "basic test",
  95. raw: []byte(basic),
  96. allowedContexts: []string{},
  97. expected: []models.Context{
  98. models.Context{
  99. Name: "context-test",
  100. Server: "https://localhost",
  101. Cluster: "cluster-test",
  102. User: "test-admin",
  103. Selected: false,
  104. },
  105. },
  106. },
  107. }
  108. func TestBasicAll(t *testing.T) {
  109. for _, c := range BasicContextAllTests {
  110. res, err := kubernetes.GetContextsFromBytes(c.raw, c.allowedContexts)
  111. if err != nil {
  112. t.Fatalf("Testing %s returned an error: %v\n", c.msg, err.Error())
  113. }
  114. isEqual := reflect.DeepEqual(c.expected, res)
  115. if !isEqual {
  116. t.Errorf("Testing: %s, Expected: %v, Got: %v\n", c.msg, c.expected, res)
  117. }
  118. }
  119. }
  120. func TestGetRestrictedClientConfig(t *testing.T) {
  121. contexts := []string{"context-test"}
  122. contextName := "context-test"
  123. clientConf, err := kubernetes.GetRestrictedClientConfigFromBytes([]byte(basic), contextName, contexts)
  124. if err != nil {
  125. t.Fatalf("Fatal error: %s\n", err.Error())
  126. }
  127. rawConf, err := clientConf.RawConfig()
  128. if err != nil {
  129. t.Fatalf("Fatal error: %s\n", err.Error())
  130. }
  131. if cluster, clusterFound := rawConf.Clusters["cluster-test"]; !clusterFound || cluster.Server != "https://localhost" {
  132. t.Errorf("invalid cluster returned")
  133. }
  134. if _, contextFound := rawConf.Contexts["context-test"]; !contextFound {
  135. t.Errorf("invalid context returned")
  136. }
  137. if _, authInfoFound := rawConf.AuthInfos["test-admin"]; !authInfoFound {
  138. t.Errorf("invalid auth info returned")
  139. }
  140. }
  141. type saCandidatesTest struct {
  142. name string
  143. raw []byte
  144. expected []*models.ServiceAccountCandidate
  145. }
  146. var SACandidatesTests = []saCandidatesTest{
  147. saCandidatesTest{
  148. name: "test without cluster ca data",
  149. raw: []byte(ClusterCAWithoutData),
  150. expected: []*models.ServiceAccountCandidate{
  151. &models.ServiceAccountCandidate{
  152. Actions: []models.ServiceAccountAction{
  153. models.ServiceAccountAction{
  154. Name: "upload-cluster-ca-data",
  155. Resolved: false,
  156. Filename: "/fake/path/to/ca.pem",
  157. },
  158. },
  159. Kind: "connector",
  160. ClusterName: "cluster-test",
  161. ClusterEndpoint: "https://localhost",
  162. AuthMechanism: models.X509,
  163. Kubeconfig: []byte(ClusterCAWithoutData),
  164. },
  165. },
  166. },
  167. saCandidatesTest{
  168. name: "x509 test with cert and key data",
  169. raw: []byte(x509WithData),
  170. expected: []*models.ServiceAccountCandidate{
  171. &models.ServiceAccountCandidate{
  172. Actions: []models.ServiceAccountAction{},
  173. Kind: "connector",
  174. ClusterName: "cluster-test",
  175. ClusterEndpoint: "https://localhost",
  176. AuthMechanism: models.X509,
  177. Kubeconfig: []byte(x509WithData),
  178. },
  179. },
  180. },
  181. saCandidatesTest{
  182. name: "x509 test without cert data",
  183. raw: []byte(x509WithoutCertData),
  184. expected: []*models.ServiceAccountCandidate{
  185. &models.ServiceAccountCandidate{
  186. Actions: []models.ServiceAccountAction{
  187. models.ServiceAccountAction{
  188. Name: "upload-client-cert-data",
  189. Resolved: false,
  190. Filename: "/fake/path/to/cert.pem",
  191. },
  192. },
  193. Kind: "connector",
  194. ClusterName: "cluster-test",
  195. ClusterEndpoint: "https://localhost",
  196. AuthMechanism: models.X509,
  197. Kubeconfig: []byte(x509WithoutCertData),
  198. },
  199. },
  200. },
  201. saCandidatesTest{
  202. name: "x509 test without key data",
  203. raw: []byte(x509WithoutKeyData),
  204. expected: []*models.ServiceAccountCandidate{
  205. &models.ServiceAccountCandidate{
  206. Actions: []models.ServiceAccountAction{
  207. models.ServiceAccountAction{
  208. Name: "upload-client-key-data",
  209. Resolved: false,
  210. Filename: "/fake/path/to/key.pem",
  211. },
  212. },
  213. Kind: "connector",
  214. ClusterName: "cluster-test",
  215. ClusterEndpoint: "https://localhost",
  216. AuthMechanism: models.X509,
  217. Kubeconfig: []byte(x509WithoutKeyData),
  218. },
  219. },
  220. },
  221. saCandidatesTest{
  222. name: "x509 test without cert and key data",
  223. raw: []byte(x509WithoutCertAndKeyData),
  224. expected: []*models.ServiceAccountCandidate{
  225. &models.ServiceAccountCandidate{
  226. Actions: []models.ServiceAccountAction{
  227. models.ServiceAccountAction{
  228. Name: "upload-client-cert-data",
  229. Resolved: false,
  230. Filename: "/fake/path/to/cert.pem",
  231. },
  232. models.ServiceAccountAction{
  233. Name: "upload-client-key-data",
  234. Resolved: false,
  235. Filename: "/fake/path/to/key.pem",
  236. },
  237. },
  238. Kind: "connector",
  239. ClusterName: "cluster-test",
  240. ClusterEndpoint: "https://localhost",
  241. AuthMechanism: models.X509,
  242. Kubeconfig: []byte(x509WithoutCertAndKeyData),
  243. },
  244. },
  245. },
  246. saCandidatesTest{
  247. name: "bearer token test with data",
  248. raw: []byte(BearerTokenWithData),
  249. expected: []*models.ServiceAccountCandidate{
  250. &models.ServiceAccountCandidate{
  251. Actions: []models.ServiceAccountAction{},
  252. Kind: "connector",
  253. ClusterName: "cluster-test",
  254. ClusterEndpoint: "https://localhost",
  255. AuthMechanism: models.Bearer,
  256. Kubeconfig: []byte(BearerTokenWithData),
  257. },
  258. },
  259. },
  260. saCandidatesTest{
  261. name: "bearer token test without data",
  262. raw: []byte(BearerTokenWithoutData),
  263. expected: []*models.ServiceAccountCandidate{
  264. &models.ServiceAccountCandidate{
  265. Actions: []models.ServiceAccountAction{
  266. models.ServiceAccountAction{
  267. Name: "upload-token-data",
  268. Resolved: false,
  269. Filename: "/path/to/token/file.txt",
  270. },
  271. },
  272. Kind: "connector",
  273. ClusterName: "cluster-test",
  274. ClusterEndpoint: "https://localhost",
  275. AuthMechanism: models.Bearer,
  276. Kubeconfig: []byte(BearerTokenWithoutData),
  277. },
  278. },
  279. },
  280. saCandidatesTest{
  281. name: "gcp test",
  282. raw: []byte(GCPPlugin),
  283. expected: []*models.ServiceAccountCandidate{
  284. &models.ServiceAccountCandidate{
  285. Actions: []models.ServiceAccountAction{
  286. models.ServiceAccountAction{
  287. Name: "upload-gcp-key-data",
  288. Resolved: false,
  289. },
  290. },
  291. Kind: "connector",
  292. ClusterName: "cluster-test",
  293. ClusterEndpoint: "https://localhost",
  294. AuthMechanism: models.GCP,
  295. Kubeconfig: []byte(GCPPlugin),
  296. },
  297. },
  298. },
  299. saCandidatesTest{
  300. name: "aws iam authenticator test",
  301. raw: []byte(AWSIamAuthenticatorExec),
  302. expected: []*models.ServiceAccountCandidate{
  303. &models.ServiceAccountCandidate{
  304. Actions: []models.ServiceAccountAction{
  305. models.ServiceAccountAction{
  306. Name: "upload-aws-data",
  307. Resolved: false,
  308. },
  309. },
  310. Kind: "connector",
  311. ClusterName: "cluster-test",
  312. ClusterEndpoint: "https://localhost",
  313. AuthMechanism: models.AWS,
  314. Kubeconfig: []byte(AWSIamAuthenticatorExec),
  315. },
  316. },
  317. },
  318. saCandidatesTest{
  319. name: "aws eks get-token test",
  320. raw: []byte(AWSEKSGetTokenExec),
  321. expected: []*models.ServiceAccountCandidate{
  322. &models.ServiceAccountCandidate{
  323. Actions: []models.ServiceAccountAction{
  324. models.ServiceAccountAction{
  325. Name: "upload-aws-data",
  326. Resolved: false,
  327. },
  328. },
  329. Kind: "connector",
  330. ClusterName: "cluster-test",
  331. ClusterEndpoint: "https://localhost",
  332. AuthMechanism: models.AWS,
  333. Kubeconfig: []byte(AWSEKSGetTokenExec),
  334. },
  335. },
  336. },
  337. saCandidatesTest{
  338. name: "oidc without ca data",
  339. raw: []byte(OIDCAuthWithoutData),
  340. expected: []*models.ServiceAccountCandidate{
  341. &models.ServiceAccountCandidate{
  342. Actions: []models.ServiceAccountAction{
  343. models.ServiceAccountAction{
  344. Name: "upload-oidc-idp-issuer-ca-data",
  345. Resolved: false,
  346. Filename: "/fake/path/to/ca.pem",
  347. },
  348. },
  349. Kind: "connector",
  350. ClusterName: "cluster-test",
  351. ClusterEndpoint: "https://localhost",
  352. AuthMechanism: models.OIDC,
  353. Kubeconfig: []byte(OIDCAuthWithoutData),
  354. },
  355. },
  356. },
  357. saCandidatesTest{
  358. name: "oidc with ca data",
  359. raw: []byte(OIDCAuthWithData),
  360. expected: []*models.ServiceAccountCandidate{
  361. &models.ServiceAccountCandidate{
  362. Actions: []models.ServiceAccountAction{},
  363. Kind: "connector",
  364. ClusterName: "cluster-test",
  365. ClusterEndpoint: "https://localhost",
  366. AuthMechanism: models.OIDC,
  367. Kubeconfig: []byte(OIDCAuthWithData),
  368. },
  369. },
  370. },
  371. saCandidatesTest{
  372. name: "basic auth test",
  373. raw: []byte(BasicAuth),
  374. expected: []*models.ServiceAccountCandidate{
  375. &models.ServiceAccountCandidate{
  376. Actions: []models.ServiceAccountAction{},
  377. Kind: "connector",
  378. ClusterName: "cluster-test",
  379. ClusterEndpoint: "https://localhost",
  380. AuthMechanism: models.Basic,
  381. Kubeconfig: []byte(BasicAuth),
  382. },
  383. },
  384. },
  385. }
  386. func TestGetServiceAccountCandidates(t *testing.T) {
  387. for _, c := range SACandidatesTests {
  388. result, err := kubernetes.GetServiceAccountCandidates(c.raw)
  389. if err != nil {
  390. t.Fatalf("error occurred %v\n", err)
  391. }
  392. // make result into a map so it's easier to compare
  393. resMap := make(map[string]*models.ServiceAccountCandidate)
  394. for _, res := range result {
  395. resMap[res.Kind+"-"+res.ClusterEndpoint+"-"+res.AuthMechanism] = res
  396. }
  397. for _, exp := range c.expected {
  398. res, ok := resMap[exp.Kind+"-"+exp.ClusterEndpoint+"-"+exp.AuthMechanism]
  399. if !ok {
  400. t.Fatalf("%s failed: no matching result for %s\n", c.name,
  401. exp.Kind+"-"+exp.ClusterEndpoint+"-"+exp.AuthMechanism)
  402. }
  403. // compare basic string fields
  404. if exp.AuthMechanism != res.AuthMechanism {
  405. t.Errorf("%s failed on auth mechanism: expected %s, got %s\n",
  406. c.name, exp.AuthMechanism, res.AuthMechanism)
  407. }
  408. if exp.ClusterName != res.ClusterName {
  409. t.Errorf("%s failed on cluster name: expected %s, got %s\n",
  410. c.name, exp.ClusterName, res.ClusterName)
  411. }
  412. if exp.ClusterEndpoint != res.ClusterEndpoint {
  413. t.Errorf("%s failed on cluster endpoint: expected %s, got %s\n",
  414. c.name, exp.ClusterEndpoint, res.ClusterEndpoint)
  415. }
  416. if len(res.Actions) != len(exp.Actions) {
  417. t.Errorf("%s failed on action names: expected length %d, got length %d\n",
  418. c.name, len(res.Actions), len(exp.Actions))
  419. } else {
  420. for i, action := range exp.Actions {
  421. if res.Actions[i].Name != action.Name {
  422. t.Errorf("%s failed on action names: expected res to contain %s, got %s\n",
  423. c.name, action.Name, res.Actions[i].Name)
  424. }
  425. if res.Actions[i].Filename != action.Filename {
  426. t.Errorf("%s failed on action file names: expected res to contain %s, got %s\n",
  427. c.name, action.Filename, res.Actions[i].Filename)
  428. }
  429. }
  430. }
  431. // compare kubeconfig by transforming into a client config
  432. resConfig, _ := clientcmd.NewClientConfigFromBytes(res.Kubeconfig)
  433. expConfig, err := clientcmd.NewClientConfigFromBytes(exp.Kubeconfig)
  434. if err != nil {
  435. t.Fatalf("config from bytes, error occurred %v\n", err)
  436. }
  437. resRawConf, _ := resConfig.RawConfig()
  438. expRawConf, err := expConfig.RawConfig()
  439. if err != nil {
  440. t.Fatalf("raw config conversion, error occurred %v\n", err)
  441. }
  442. if !reflect.DeepEqual(resRawConf, expRawConf) {
  443. t.Errorf("%s failed: expected %v, got %v\n", c.name, expRawConf, resRawConf)
  444. }
  445. }
  446. }
  447. }
  448. func TestAWSClusterIDGuess(t *testing.T) {
  449. result, err := kubernetes.GetServiceAccountCandidates([]byte(AWSIamAuthenticatorExec))
  450. if err != nil {
  451. t.Fatalf("error occurred %v\n", err)
  452. }
  453. if len(result) != 1 {
  454. t.Fatalf("result length was not 1\n")
  455. }
  456. if result[0].AWSClusterIDGuess != "cluster-test-aws-id-guess" {
  457. t.Errorf("Guess AWS cluster id failed: expected %s, got %s\n", "cluster-test-aws-id-guess", result[0].AWSClusterIDGuess)
  458. }
  459. result, err = kubernetes.GetServiceAccountCandidates([]byte(AWSEKSGetTokenExec))
  460. if err != nil {
  461. t.Fatalf("error occurred %v\n", err)
  462. }
  463. if len(result) != 1 {
  464. t.Fatalf("result length was not 1\n")
  465. }
  466. if result[0].AWSClusterIDGuess != "cluster-test-aws-id-guess" {
  467. t.Errorf("Guess AWS cluster id failed: expected %s, got %s\n", "cluster-test-aws-id-guess", result[0].AWSClusterIDGuess)
  468. }
  469. }
  470. const noContexts string = `
  471. apiVersion: v1
  472. kind: Config
  473. preferences: {}
  474. clusters:
  475. - cluster:
  476. server: https://localhost
  477. name: porter-test-1
  478. current-context: context-test
  479. users:
  480. - name: test-admin
  481. user:
  482. `
  483. const noClusters string = `
  484. apiVersion: v1
  485. kind: Config
  486. preferences: {}
  487. current-context: context-test
  488. contexts:
  489. - context:
  490. cluster: porter-test-1
  491. user: test-admin
  492. name: context-test
  493. users:
  494. - name: test-admin
  495. user:
  496. `
  497. const noUsers string = `
  498. apiVersion: v1
  499. kind: Config
  500. preferences: {}
  501. current-context: default
  502. clusters:
  503. - cluster:
  504. server: https://localhost
  505. name: porter-test-1
  506. contexts:
  507. - context:
  508. cluster: porter-test-1
  509. user: test-admin
  510. name: context-test
  511. `
  512. const noContextClusters string = `
  513. apiVersion: v1
  514. kind: Config
  515. preferences: {}
  516. current-context: default
  517. clusters:
  518. - cluster:
  519. server: https://localhost
  520. name: porter-test-1
  521. contexts:
  522. - context:
  523. # cluster: porter-test-1
  524. user: test-admin
  525. name: context-test
  526. users:
  527. - name: test-admin
  528. user:
  529. `
  530. const noContextUsers string = `
  531. apiVersion: v1
  532. kind: Config
  533. preferences: {}
  534. current-context: default
  535. clusters:
  536. - cluster:
  537. server: https://localhost
  538. name: porter-test-1
  539. contexts:
  540. - context:
  541. cluster: porter-test-1
  542. # user: test-admin
  543. name: context-test
  544. users:
  545. - name: test-admin
  546. user:
  547. `
  548. const basic string = `
  549. apiVersion: v1
  550. kind: Config
  551. preferences: {}
  552. current-context: context-test
  553. clusters:
  554. - cluster:
  555. server: https://localhost
  556. name: cluster-test
  557. contexts:
  558. - context:
  559. cluster: cluster-test
  560. user: test-admin
  561. name: context-test
  562. users:
  563. - name: test-admin
  564. `
  565. const ClusterCAWithoutData string = `
  566. apiVersion: v1
  567. kind: Config
  568. clusters:
  569. - name: cluster-test
  570. cluster:
  571. server: https://localhost
  572. certificate-authority: /fake/path/to/ca.pem
  573. contexts:
  574. - context:
  575. cluster: cluster-test
  576. user: test-admin
  577. name: context-test
  578. users:
  579. - name: test-admin
  580. user:
  581. client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
  582. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  583. current-context: context-test
  584. `
  585. const x509WithData string = `
  586. apiVersion: v1
  587. kind: Config
  588. preferences: {}
  589. current-context: context-test
  590. clusters:
  591. - cluster:
  592. server: https://localhost
  593. name: cluster-test
  594. contexts:
  595. - context:
  596. cluster: cluster-test
  597. user: test-admin
  598. name: context-test
  599. users:
  600. - name: test-admin
  601. user:
  602. client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
  603. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  604. `
  605. const x509WithoutCertData string = `
  606. apiVersion: v1
  607. kind: Config
  608. preferences: {}
  609. current-context: context-test
  610. clusters:
  611. - cluster:
  612. server: https://localhost
  613. name: cluster-test
  614. contexts:
  615. - context:
  616. cluster: cluster-test
  617. user: test-admin
  618. name: context-test
  619. users:
  620. - name: test-admin
  621. user:
  622. client-certificate: /fake/path/to/cert.pem
  623. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  624. `
  625. const x509WithoutKeyData string = `
  626. apiVersion: v1
  627. kind: Config
  628. preferences: {}
  629. current-context: context-test
  630. clusters:
  631. - cluster:
  632. server: https://localhost
  633. name: cluster-test
  634. contexts:
  635. - context:
  636. cluster: cluster-test
  637. user: test-admin
  638. name: context-test
  639. users:
  640. - name: test-admin
  641. user:
  642. client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
  643. client-key: /fake/path/to/key.pem
  644. `
  645. const x509WithoutCertAndKeyData string = `
  646. apiVersion: v1
  647. kind: Config
  648. preferences: {}
  649. current-context: context-test
  650. clusters:
  651. - cluster:
  652. server: https://localhost
  653. name: cluster-test
  654. contexts:
  655. - context:
  656. cluster: cluster-test
  657. user: test-admin
  658. name: context-test
  659. users:
  660. - name: test-admin
  661. user:
  662. client-certificate: /fake/path/to/cert.pem
  663. client-key: /fake/path/to/key.pem
  664. `
  665. const BearerTokenWithData string = `
  666. apiVersion: v1
  667. kind: Config
  668. preferences: {}
  669. current-context: context-test
  670. clusters:
  671. - cluster:
  672. server: https://localhost
  673. name: cluster-test
  674. contexts:
  675. - context:
  676. cluster: cluster-test
  677. user: test-admin
  678. name: context-test
  679. users:
  680. - name: test-admin
  681. user:
  682. token: LS0tLS1CRUdJTiBDRVJ=
  683. `
  684. const BearerTokenWithoutData string = `
  685. apiVersion: v1
  686. kind: Config
  687. preferences: {}
  688. current-context: context-test
  689. clusters:
  690. - cluster:
  691. server: https://localhost
  692. name: cluster-test
  693. contexts:
  694. - context:
  695. cluster: cluster-test
  696. user: test-admin
  697. name: context-test
  698. users:
  699. - name: test-admin
  700. user:
  701. tokenFile: /path/to/token/file.txt
  702. `
  703. const GCPPlugin string = `
  704. apiVersion: v1
  705. kind: Config
  706. clusters:
  707. - name: cluster-test
  708. cluster:
  709. server: https://localhost
  710. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  711. users:
  712. - name: test-admin
  713. user:
  714. auth-provider:
  715. name: gcp
  716. contexts:
  717. - context:
  718. cluster: cluster-test
  719. user: test-admin
  720. name: context-test
  721. current-context: context-test
  722. `
  723. const AWSIamAuthenticatorExec = `
  724. apiVersion: v1
  725. clusters:
  726. - cluster:
  727. server: https://localhost
  728. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  729. name: cluster-test
  730. contexts:
  731. - context:
  732. cluster: cluster-test
  733. user: test-admin
  734. name: context-test
  735. current-context: context-test
  736. kind: Config
  737. preferences: {}
  738. users:
  739. - name: test-admin
  740. user:
  741. exec:
  742. apiVersion: client.authentication.k8s.io/v1alpha1
  743. command: aws-iam-authenticator
  744. args:
  745. - "token"
  746. - "-i"
  747. - "cluster-test-aws-id-guess"
  748. `
  749. const AWSEKSGetTokenExec = `
  750. apiVersion: v1
  751. clusters:
  752. - cluster:
  753. server: https://localhost
  754. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  755. name: cluster-test
  756. contexts:
  757. - context:
  758. cluster: cluster-test
  759. user: test-admin
  760. name: context-test
  761. current-context: context-test
  762. kind: Config
  763. preferences: {}
  764. users:
  765. - name: test-admin
  766. user:
  767. exec:
  768. apiVersion: client.authentication.k8s.io/v1alpha1
  769. command: aws
  770. args:
  771. - "eks"
  772. - "get-token"
  773. - "--cluster-name"
  774. - "cluster-test-aws-id-guess"
  775. `
  776. const OIDCAuthWithoutData = `
  777. apiVersion: v1
  778. clusters:
  779. - cluster:
  780. server: https://localhost
  781. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  782. name: cluster-test
  783. contexts:
  784. - context:
  785. cluster: cluster-test
  786. user: test-admin
  787. name: context-test
  788. current-context: context-test
  789. kind: Config
  790. preferences: {}
  791. users:
  792. - name: test-admin
  793. user:
  794. auth-provider:
  795. config:
  796. client-id: porter-api
  797. id-token: token
  798. idp-issuer-url: https://localhost
  799. idp-certificate-authority: /fake/path/to/ca.pem
  800. name: oidc
  801. `
  802. const OIDCAuthWithData = `
  803. apiVersion: v1
  804. clusters:
  805. - cluster:
  806. server: https://localhost
  807. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  808. name: cluster-test
  809. contexts:
  810. - context:
  811. cluster: cluster-test
  812. user: test-admin
  813. name: context-test
  814. current-context: context-test
  815. kind: Config
  816. preferences: {}
  817. users:
  818. - name: test-admin
  819. user:
  820. auth-provider:
  821. config:
  822. client-id: porter-api
  823. id-token: token
  824. idp-issuer-url: https://localhost
  825. idp-certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  826. name: oidc
  827. `
  828. const BasicAuth = `
  829. apiVersion: v1
  830. clusters:
  831. - cluster:
  832. server: https://localhost
  833. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  834. name: cluster-test
  835. contexts:
  836. - context:
  837. cluster: cluster-test
  838. user: test-admin
  839. name: context-test
  840. current-context: context-test
  841. kind: Config
  842. preferences: {}
  843. users:
  844. - name: test-admin
  845. user:
  846. username: admin
  847. password: changeme
  848. `