2
0

action_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. package forms_test
  2. import (
  3. "encoding/base64"
  4. "testing"
  5. "github.com/porter-dev/porter/internal/forms"
  6. "github.com/porter-dev/porter/internal/kubernetes"
  7. "github.com/porter-dev/porter/internal/models"
  8. "github.com/porter-dev/porter/internal/repository/test"
  9. )
  10. func TestPopulateServiceAccountBasic(t *testing.T) {
  11. // create the in-memory repository
  12. repo := test.NewRepository(true)
  13. // create a new project
  14. repo.Project.CreateProject(&models.Project{
  15. Name: "test-project",
  16. })
  17. // create a ServiceAccountCandidate from a kubeconfig
  18. saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterCAWithData))
  19. if err != nil {
  20. t.Fatalf("%v\n", err)
  21. }
  22. for _, saCandidate := range saCandidates {
  23. repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  24. }
  25. // create a new form
  26. form := forms.ServiceAccountActionResolver{
  27. ServiceAccountCandidateID: 1,
  28. }
  29. err = form.PopulateServiceAccount(repo.ServiceAccount)
  30. if err != nil {
  31. t.Fatalf("%v\n", err)
  32. }
  33. sa, err := repo.ServiceAccount.CreateServiceAccount(form.SA)
  34. decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  35. if len(sa.Clusters) != 1 {
  36. t.Fatalf("cluster not written\n")
  37. }
  38. if sa.Clusters[0].ServiceAccountID != 1 {
  39. t.Errorf("service account ID of joined cluster is not 1")
  40. }
  41. if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  42. t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  43. string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  44. }
  45. if sa.AuthMechanism != "x509" {
  46. t.Errorf("service account auth mechanism is not x509")
  47. }
  48. if string(sa.ClientCertificateData) != string(decodedStr) {
  49. t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  50. string(sa.ClientCertificateData), string(decodedStr))
  51. }
  52. if string(sa.ClientKeyData) != string(decodedStr) {
  53. t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  54. string(sa.ClientKeyData), string(decodedStr))
  55. }
  56. }
  57. func TestPopulateServiceAccountClusterDataAction(t *testing.T) {
  58. // create the in-memory repository
  59. repo := test.NewRepository(true)
  60. // create a new project
  61. repo.Project.CreateProject(&models.Project{
  62. Name: "test-project",
  63. })
  64. // create a ServiceAccountCandidate from a kubeconfig
  65. saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterCAWithoutData))
  66. if err != nil {
  67. t.Fatalf("%v\n", err)
  68. }
  69. for _, saCandidate := range saCandidates {
  70. repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  71. }
  72. // create a new form
  73. form := forms.ClusterCADataAction{
  74. ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  75. ServiceAccountCandidateID: 1,
  76. },
  77. ClusterCAData: "LS0tLS1CRUdJTiBDRVJ=",
  78. }
  79. err = form.PopulateServiceAccount(repo.ServiceAccount)
  80. if err != nil {
  81. t.Fatalf("%v\n", err)
  82. }
  83. sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  84. decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  85. if len(sa.Clusters) != 1 {
  86. t.Fatalf("cluster not written\n")
  87. }
  88. if sa.Clusters[0].ServiceAccountID != 1 {
  89. t.Errorf("service account ID of joined cluster is not 1")
  90. }
  91. if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  92. t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  93. string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  94. }
  95. if sa.AuthMechanism != "x509" {
  96. t.Errorf("service account auth mechanism is not x509")
  97. }
  98. if string(sa.ClientCertificateData) != string(decodedStr) {
  99. t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  100. string(sa.ClientCertificateData), string(decodedStr))
  101. }
  102. if string(sa.ClientKeyData) != string(decodedStr) {
  103. t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  104. string(sa.ClientKeyData), string(decodedStr))
  105. }
  106. }
  107. func TestPopulateServiceAccountClientCertAction(t *testing.T) {
  108. // create the in-memory repository
  109. repo := test.NewRepository(true)
  110. // create a new project
  111. repo.Project.CreateProject(&models.Project{
  112. Name: "test-project",
  113. })
  114. // create a ServiceAccountCandidate from a kubeconfig
  115. saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClientWithoutCertData))
  116. if err != nil {
  117. t.Fatalf("%v\n", err)
  118. }
  119. for _, saCandidate := range saCandidates {
  120. repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  121. }
  122. // create a new form
  123. form := forms.ClientCertDataAction{
  124. ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  125. ServiceAccountCandidateID: 1,
  126. },
  127. ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
  128. }
  129. err = form.PopulateServiceAccount(repo.ServiceAccount)
  130. if err != nil {
  131. t.Fatalf("%v\n", err)
  132. }
  133. sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  134. decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  135. if len(sa.Clusters) != 1 {
  136. t.Fatalf("cluster not written\n")
  137. }
  138. if sa.Clusters[0].ServiceAccountID != 1 {
  139. t.Errorf("service account ID of joined cluster is not 1")
  140. }
  141. if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  142. t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  143. string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  144. }
  145. if sa.AuthMechanism != "x509" {
  146. t.Errorf("service account auth mechanism is not x509")
  147. }
  148. if string(sa.ClientCertificateData) != string(decodedStr) {
  149. t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  150. string(sa.ClientCertificateData), string(decodedStr))
  151. }
  152. if string(sa.ClientKeyData) != string(decodedStr) {
  153. t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  154. string(sa.ClientKeyData), string(decodedStr))
  155. }
  156. }
  157. func TestPopulateServiceAccountClientCertAndKeyActions(t *testing.T) {
  158. // create the in-memory repository
  159. repo := test.NewRepository(true)
  160. // create a new project
  161. repo.Project.CreateProject(&models.Project{
  162. Name: "test-project",
  163. })
  164. // create a ServiceAccountCandidate from a kubeconfig
  165. saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClientWithoutCertAndKeyData))
  166. if err != nil {
  167. t.Fatalf("%v\n", err)
  168. }
  169. for _, saCandidate := range saCandidates {
  170. repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  171. }
  172. // create a new form
  173. form := forms.ClientCertDataAction{
  174. ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  175. ServiceAccountCandidateID: 1,
  176. },
  177. ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
  178. }
  179. err = form.PopulateServiceAccount(repo.ServiceAccount)
  180. if err != nil {
  181. t.Fatalf("%v\n", err)
  182. }
  183. keyForm := forms.ClientKeyDataAction{
  184. ServiceAccountActionResolver: form.ServiceAccountActionResolver,
  185. ClientKeyData: "LS0tLS1CRUdJTiBDRVJ=",
  186. }
  187. err = keyForm.PopulateServiceAccount(repo.ServiceAccount)
  188. if err != nil {
  189. t.Fatalf("%v\n", err)
  190. }
  191. sa, err := repo.ServiceAccount.CreateServiceAccount(keyForm.ServiceAccountActionResolver.SA)
  192. decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
  193. if len(sa.Clusters) != 1 {
  194. t.Fatalf("cluster not written\n")
  195. }
  196. if sa.Clusters[0].ServiceAccountID != 1 {
  197. t.Errorf("service account ID of joined cluster is not 1")
  198. }
  199. if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
  200. t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
  201. string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
  202. }
  203. if sa.AuthMechanism != "x509" {
  204. t.Errorf("service account auth mechanism is not x509")
  205. }
  206. if string(sa.ClientCertificateData) != string(decodedStr) {
  207. t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  208. string(sa.ClientCertificateData), string(decodedStr))
  209. }
  210. if string(sa.ClientKeyData) != string(decodedStr) {
  211. t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
  212. string(sa.ClientKeyData), string(decodedStr))
  213. }
  214. }
  215. func TestPopulateServiceAccountTokenDataAction(t *testing.T) {
  216. // create the in-memory repository
  217. repo := test.NewRepository(true)
  218. tokenData := "abcdefghijklmnop"
  219. // create a new project
  220. repo.Project.CreateProject(&models.Project{
  221. Name: "test-project",
  222. })
  223. // create a ServiceAccountCandidate from a kubeconfig
  224. saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(BearerTokenWithoutData))
  225. if err != nil {
  226. t.Fatalf("%v\n", err)
  227. }
  228. for _, saCandidate := range saCandidates {
  229. repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  230. }
  231. // create a new form
  232. form := forms.TokenDataAction{
  233. ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  234. ServiceAccountCandidateID: 1,
  235. },
  236. TokenData: tokenData,
  237. }
  238. err = form.PopulateServiceAccount(repo.ServiceAccount)
  239. if err != nil {
  240. t.Fatalf("%v\n", err)
  241. }
  242. sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  243. if len(sa.Clusters) != 1 {
  244. t.Fatalf("cluster not written\n")
  245. }
  246. if sa.Clusters[0].ServiceAccountID != 1 {
  247. t.Errorf("service account ID of joined cluster is not 1")
  248. }
  249. if sa.AuthMechanism != models.Bearer {
  250. t.Errorf("service account auth mechanism is not %s\n", models.Bearer)
  251. }
  252. if sa.Token != tokenData {
  253. t.Errorf("service account token data is wrong: expected %s, got %s\n",
  254. tokenData, sa.Token)
  255. }
  256. }
  257. func TestPopulateServiceAccountGCPKeyDataAction(t *testing.T) {
  258. // create the in-memory repository
  259. repo := test.NewRepository(true)
  260. gcpKeyData := []byte(`{"key": "data"}`)
  261. // create a new project
  262. repo.Project.CreateProject(&models.Project{
  263. Name: "test-project",
  264. })
  265. // create a ServiceAccountCandidate from a kubeconfig
  266. saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(GCPPlugin))
  267. if err != nil {
  268. t.Fatalf("%v\n", err)
  269. }
  270. for _, saCandidate := range saCandidates {
  271. repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  272. }
  273. // create a new form
  274. form := forms.GCPKeyDataAction{
  275. ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  276. ServiceAccountCandidateID: 1,
  277. },
  278. GCPKeyData: string(gcpKeyData),
  279. }
  280. err = form.PopulateServiceAccount(repo.ServiceAccount)
  281. if err != nil {
  282. t.Fatalf("%v\n", err)
  283. }
  284. sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  285. if len(sa.Clusters) != 1 {
  286. t.Fatalf("cluster not written\n")
  287. }
  288. if sa.Clusters[0].ServiceAccountID != 1 {
  289. t.Errorf("service account ID of joined cluster is not 1")
  290. }
  291. if sa.AuthMechanism != models.GCP {
  292. t.Errorf("service account auth mechanism is not %s\n", models.GCP)
  293. }
  294. if string(sa.KeyData) != string(gcpKeyData) {
  295. t.Errorf("service account token data is wrong: expected %s, got %s\n",
  296. string(sa.KeyData), string(gcpKeyData))
  297. }
  298. }
  299. func TestPopulateServiceAccountAWSKeyDataAction(t *testing.T) {
  300. // create the in-memory repository
  301. repo := test.NewRepository(true)
  302. awsKeyData := []byte(`{"key": "data"}`)
  303. // create a new project
  304. repo.Project.CreateProject(&models.Project{
  305. Name: "test-project",
  306. })
  307. // create a ServiceAccountCandidate from a kubeconfig
  308. saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(AWSEKSGetTokenExec))
  309. if err != nil {
  310. t.Fatalf("%v\n", err)
  311. }
  312. for _, saCandidate := range saCandidates {
  313. repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  314. }
  315. // create a new form
  316. form := forms.AWSKeyDataAction{
  317. ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  318. ServiceAccountCandidateID: 1,
  319. },
  320. AWSKeyData: string(awsKeyData),
  321. }
  322. err = form.PopulateServiceAccount(repo.ServiceAccount)
  323. if err != nil {
  324. t.Fatalf("%v\n", err)
  325. }
  326. sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  327. if len(sa.Clusters) != 1 {
  328. t.Fatalf("cluster not written\n")
  329. }
  330. if sa.Clusters[0].ServiceAccountID != 1 {
  331. t.Errorf("service account ID of joined cluster is not 1")
  332. }
  333. if sa.AuthMechanism != models.AWS {
  334. t.Errorf("service account auth mechanism is not %s\n", models.AWS)
  335. }
  336. if string(sa.KeyData) != string(awsKeyData) {
  337. t.Errorf("service account token data is wrong: expected %s, got %s\n",
  338. string(sa.KeyData), string(awsKeyData))
  339. }
  340. }
  341. func TestPopulateServiceAccountOIDCAction(t *testing.T) {
  342. // create the in-memory repository
  343. repo := test.NewRepository(true)
  344. // create a new project
  345. repo.Project.CreateProject(&models.Project{
  346. Name: "test-project",
  347. })
  348. // create a ServiceAccountCandidate from a kubeconfig
  349. saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(OIDCAuthWithoutData))
  350. if err != nil {
  351. t.Fatalf("%v\n", err)
  352. }
  353. for _, saCandidate := range saCandidates {
  354. repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  355. }
  356. // create a new form
  357. form := forms.OIDCIssuerDataAction{
  358. ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
  359. ServiceAccountCandidateID: 1,
  360. },
  361. OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
  362. }
  363. err = form.PopulateServiceAccount(repo.ServiceAccount)
  364. if err != nil {
  365. t.Fatalf("%v\n", err)
  366. }
  367. sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
  368. if len(sa.Clusters) != 1 {
  369. t.Fatalf("cluster not written\n")
  370. }
  371. if sa.Clusters[0].ServiceAccountID != 1 {
  372. t.Errorf("service account ID of joined cluster is not 1")
  373. }
  374. if sa.AuthMechanism != models.OIDC {
  375. t.Errorf("service account auth mechanism is not %s\n", models.OIDC)
  376. }
  377. if string(sa.OIDCCertificateAuthorityData) != "LS0tLS1CRUdJTiBDRVJ=" {
  378. t.Errorf("service account key data and input do not match: expected %s, got %s\n",
  379. string(sa.OIDCCertificateAuthorityData), "LS0tLS1CRUdJTiBDRVJ=")
  380. }
  381. }
  382. const ClusterCAWithData string = `
  383. apiVersion: v1
  384. kind: Config
  385. clusters:
  386. - name: cluster-test
  387. cluster:
  388. server: https://localhost
  389. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  390. contexts:
  391. - context:
  392. cluster: cluster-test
  393. user: test-admin
  394. name: context-test
  395. users:
  396. - name: test-admin
  397. user:
  398. client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
  399. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  400. current-context: context-test
  401. `
  402. const ClusterCAWithoutData string = `
  403. apiVersion: v1
  404. kind: Config
  405. clusters:
  406. - name: cluster-test
  407. cluster:
  408. server: https://localhost
  409. certificate-authority: /fake/path/to/ca.pem
  410. contexts:
  411. - context:
  412. cluster: cluster-test
  413. user: test-admin
  414. name: context-test
  415. users:
  416. - name: test-admin
  417. user:
  418. client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
  419. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  420. current-context: context-test
  421. `
  422. const ClientWithoutCertData string = `
  423. apiVersion: v1
  424. kind: Config
  425. clusters:
  426. - name: cluster-test
  427. cluster:
  428. server: https://localhost
  429. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  430. contexts:
  431. - context:
  432. cluster: cluster-test
  433. user: test-admin
  434. name: context-test
  435. users:
  436. - name: test-admin
  437. user:
  438. client-certificate: /fake/path/to/ca.pem
  439. client-key-data: LS0tLS1CRUdJTiBDRVJ=
  440. current-context: context-test
  441. `
  442. const ClientWithoutCertAndKeyData string = `
  443. apiVersion: v1
  444. kind: Config
  445. clusters:
  446. - name: cluster-test
  447. cluster:
  448. server: https://localhost
  449. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  450. contexts:
  451. - context:
  452. cluster: cluster-test
  453. user: test-admin
  454. name: context-test
  455. users:
  456. - name: test-admin
  457. user:
  458. client-certificate: /fake/path/to/ca.pem
  459. client-key: /fake/path/to/ca.pem
  460. current-context: context-test
  461. `
  462. const BearerTokenWithoutData string = `
  463. apiVersion: v1
  464. kind: Config
  465. preferences: {}
  466. current-context: context-test
  467. clusters:
  468. - cluster:
  469. server: https://localhost
  470. name: cluster-test
  471. contexts:
  472. - context:
  473. cluster: cluster-test
  474. user: test-admin
  475. name: context-test
  476. users:
  477. - name: test-admin
  478. user:
  479. tokenFile: /path/to/token/file.txt
  480. `
  481. const GCPPlugin string = `
  482. apiVersion: v1
  483. kind: Config
  484. clusters:
  485. - name: cluster-test
  486. cluster:
  487. server: https://localhost
  488. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  489. users:
  490. - name: test-admin
  491. user:
  492. auth-provider:
  493. name: gcp
  494. contexts:
  495. - context:
  496. cluster: cluster-test
  497. user: test-admin
  498. name: context-test
  499. current-context: context-test
  500. `
  501. const AWSEKSGetTokenExec string = `
  502. apiVersion: v1
  503. clusters:
  504. - cluster:
  505. server: https://localhost
  506. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  507. name: cluster-test
  508. contexts:
  509. - context:
  510. cluster: cluster-test
  511. user: test-admin
  512. name: context-test
  513. current-context: context-test
  514. kind: Config
  515. preferences: {}
  516. users:
  517. - name: test-admin
  518. user:
  519. exec:
  520. apiVersion: client.authentication.k8s.io/v1alpha1
  521. command: aws
  522. args:
  523. - "eks"
  524. - "get-token"
  525. - "--cluster-name"
  526. - "cluster-test"
  527. `
  528. const OIDCAuthWithoutData string = `
  529. apiVersion: v1
  530. clusters:
  531. - cluster:
  532. server: https://localhost
  533. certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
  534. name: cluster-test
  535. contexts:
  536. - context:
  537. cluster: cluster-test
  538. user: test-admin
  539. name: context-test
  540. current-context: context-test
  541. kind: Config
  542. preferences: {}
  543. users:
  544. - name: test-admin
  545. user:
  546. auth-provider:
  547. config:
  548. client-id: porter-api
  549. id-token: token
  550. idp-issuer-url: https://localhost
  551. idp-certificate-authority: /fake/path/to/ca.pem
  552. name: oidc
  553. `