| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- package forms_test
- import (
- "encoding/base64"
- "testing"
- "github.com/porter-dev/porter/internal/forms"
- "github.com/porter-dev/porter/internal/kubernetes"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/repository/test"
- )
- func TestPopulateServiceAccountBasic(t *testing.T) {
- // create the in-memory repository
- repo := test.NewRepository(true)
- // create a new project
- repo.Project.CreateProject(&models.Project{
- Name: "test-project",
- })
- // create a ServiceAccountCandidate from a kubeconfig
- saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterCAWithData))
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- for _, saCandidate := range saCandidates {
- repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
- }
- // create a new form
- form := forms.ServiceAccountActionResolver{
- ServiceAccountCandidateID: 1,
- }
- err = form.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- sa, err := repo.ServiceAccount.CreateServiceAccount(form.SA)
- decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
- if len(sa.Clusters) != 1 {
- t.Fatalf("cluster not written\n")
- }
- if sa.Clusters[0].ServiceAccountID != 1 {
- t.Errorf("service account ID of joined cluster is not 1")
- }
- if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
- t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
- string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
- }
- if sa.AuthMechanism != "x509" {
- t.Errorf("service account auth mechanism is not x509")
- }
- if string(sa.ClientCertificateData) != string(decodedStr) {
- t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
- string(sa.ClientCertificateData), string(decodedStr))
- }
- if string(sa.ClientKeyData) != string(decodedStr) {
- t.Errorf("service account key data and input do not match: expected %s, got %s\n",
- string(sa.ClientKeyData), string(decodedStr))
- }
- }
- func TestPopulateServiceAccountClusterDataAction(t *testing.T) {
- // create the in-memory repository
- repo := test.NewRepository(true)
- // create a new project
- repo.Project.CreateProject(&models.Project{
- Name: "test-project",
- })
- // create a ServiceAccountCandidate from a kubeconfig
- saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClusterCAWithoutData))
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- for _, saCandidate := range saCandidates {
- repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
- }
- // create a new form
- form := forms.ClusterCADataAction{
- ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
- ServiceAccountCandidateID: 1,
- },
- ClusterCAData: "LS0tLS1CRUdJTiBDRVJ=",
- }
- err = form.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
- decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
- if len(sa.Clusters) != 1 {
- t.Fatalf("cluster not written\n")
- }
- if sa.Clusters[0].ServiceAccountID != 1 {
- t.Errorf("service account ID of joined cluster is not 1")
- }
- if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
- t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
- string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
- }
- if sa.AuthMechanism != "x509" {
- t.Errorf("service account auth mechanism is not x509")
- }
- if string(sa.ClientCertificateData) != string(decodedStr) {
- t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
- string(sa.ClientCertificateData), string(decodedStr))
- }
- if string(sa.ClientKeyData) != string(decodedStr) {
- t.Errorf("service account key data and input do not match: expected %s, got %s\n",
- string(sa.ClientKeyData), string(decodedStr))
- }
- }
- func TestPopulateServiceAccountClientCertAction(t *testing.T) {
- // create the in-memory repository
- repo := test.NewRepository(true)
- // create a new project
- repo.Project.CreateProject(&models.Project{
- Name: "test-project",
- })
- // create a ServiceAccountCandidate from a kubeconfig
- saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClientWithoutCertData))
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- for _, saCandidate := range saCandidates {
- repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
- }
- // create a new form
- form := forms.ClientCertDataAction{
- ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
- ServiceAccountCandidateID: 1,
- },
- ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
- }
- err = form.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
- decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
- if len(sa.Clusters) != 1 {
- t.Fatalf("cluster not written\n")
- }
- if sa.Clusters[0].ServiceAccountID != 1 {
- t.Errorf("service account ID of joined cluster is not 1")
- }
- if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
- t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
- string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
- }
- if sa.AuthMechanism != "x509" {
- t.Errorf("service account auth mechanism is not x509")
- }
- if string(sa.ClientCertificateData) != string(decodedStr) {
- t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
- string(sa.ClientCertificateData), string(decodedStr))
- }
- if string(sa.ClientKeyData) != string(decodedStr) {
- t.Errorf("service account key data and input do not match: expected %s, got %s\n",
- string(sa.ClientKeyData), string(decodedStr))
- }
- }
- func TestPopulateServiceAccountClientCertAndKeyActions(t *testing.T) {
- // create the in-memory repository
- repo := test.NewRepository(true)
- // create a new project
- repo.Project.CreateProject(&models.Project{
- Name: "test-project",
- })
- // create a ServiceAccountCandidate from a kubeconfig
- saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(ClientWithoutCertAndKeyData))
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- for _, saCandidate := range saCandidates {
- repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
- }
- // create a new form
- form := forms.ClientCertDataAction{
- ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
- ServiceAccountCandidateID: 1,
- },
- ClientCertData: "LS0tLS1CRUdJTiBDRVJ=",
- }
- err = form.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- keyForm := forms.ClientKeyDataAction{
- ServiceAccountActionResolver: form.ServiceAccountActionResolver,
- ClientKeyData: "LS0tLS1CRUdJTiBDRVJ=",
- }
- err = keyForm.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- sa, err := repo.ServiceAccount.CreateServiceAccount(keyForm.ServiceAccountActionResolver.SA)
- decodedStr, _ := base64.StdEncoding.DecodeString("LS0tLS1CRUdJTiBDRVJ=")
- if len(sa.Clusters) != 1 {
- t.Fatalf("cluster not written\n")
- }
- if sa.Clusters[0].ServiceAccountID != 1 {
- t.Errorf("service account ID of joined cluster is not 1")
- }
- if string(sa.Clusters[0].CertificateAuthorityData) != string(decodedStr) {
- t.Errorf("cluster ca data and input do not match: expected %s, got %s\n",
- string(sa.Clusters[0].CertificateAuthorityData), string(decodedStr))
- }
- if sa.AuthMechanism != "x509" {
- t.Errorf("service account auth mechanism is not x509")
- }
- if string(sa.ClientCertificateData) != string(decodedStr) {
- t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
- string(sa.ClientCertificateData), string(decodedStr))
- }
- if string(sa.ClientKeyData) != string(decodedStr) {
- t.Errorf("service account cert data and input do not match: expected %s, got %s\n",
- string(sa.ClientKeyData), string(decodedStr))
- }
- }
- func TestPopulateServiceAccountTokenDataAction(t *testing.T) {
- // create the in-memory repository
- repo := test.NewRepository(true)
- tokenData := "abcdefghijklmnop"
- // create a new project
- repo.Project.CreateProject(&models.Project{
- Name: "test-project",
- })
- // create a ServiceAccountCandidate from a kubeconfig
- saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(BearerTokenWithoutData))
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- for _, saCandidate := range saCandidates {
- repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
- }
- // create a new form
- form := forms.TokenDataAction{
- ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
- ServiceAccountCandidateID: 1,
- },
- TokenData: tokenData,
- }
- err = form.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
- if len(sa.Clusters) != 1 {
- t.Fatalf("cluster not written\n")
- }
- if sa.Clusters[0].ServiceAccountID != 1 {
- t.Errorf("service account ID of joined cluster is not 1")
- }
- if sa.AuthMechanism != models.Bearer {
- t.Errorf("service account auth mechanism is not %s\n", models.Bearer)
- }
- if sa.Token != tokenData {
- t.Errorf("service account token data is wrong: expected %s, got %s\n",
- tokenData, sa.Token)
- }
- }
- func TestPopulateServiceAccountGCPKeyDataAction(t *testing.T) {
- // create the in-memory repository
- repo := test.NewRepository(true)
- gcpKeyData := []byte(`{"key": "data"}`)
- // create a new project
- repo.Project.CreateProject(&models.Project{
- Name: "test-project",
- })
- // create a ServiceAccountCandidate from a kubeconfig
- saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(GCPPlugin))
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- for _, saCandidate := range saCandidates {
- repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
- }
- // create a new form
- form := forms.GCPKeyDataAction{
- ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
- ServiceAccountCandidateID: 1,
- },
- GCPKeyData: string(gcpKeyData),
- }
- err = form.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
- if len(sa.Clusters) != 1 {
- t.Fatalf("cluster not written\n")
- }
- if sa.Clusters[0].ServiceAccountID != 1 {
- t.Errorf("service account ID of joined cluster is not 1")
- }
- if sa.AuthMechanism != models.GCP {
- t.Errorf("service account auth mechanism is not %s\n", models.GCP)
- }
- if string(sa.GCPKeyData) != string(gcpKeyData) {
- t.Errorf("service account token data is wrong: expected %s, got %s\n",
- string(sa.GCPKeyData), string(gcpKeyData))
- }
- }
- func TestPopulateServiceAccountAWSKeyDataAction(t *testing.T) {
- // create the in-memory repository
- repo := test.NewRepository(true)
- // create a new project
- repo.Project.CreateProject(&models.Project{
- Name: "test-project",
- })
- // create a ServiceAccountCandidate from a kubeconfig
- saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(AWSEKSGetTokenExec))
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- for _, saCandidate := range saCandidates {
- repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
- }
- // create a new form
- form := forms.AWSDataAction{
- ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
- ServiceAccountCandidateID: 1,
- },
- AWSAccessKeyID: "ALSDKJFADSF",
- AWSSecretAccessKey: "ASDLFKJALSDKFJ",
- AWSClusterID: "cluster-test",
- }
- err = form.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
- if len(sa.Clusters) != 1 {
- t.Fatalf("cluster not written\n")
- }
- if sa.Clusters[0].ServiceAccountID != 1 {
- t.Errorf("service account ID of joined cluster is not 1")
- }
- if sa.AuthMechanism != models.AWS {
- t.Errorf("service account auth mechanism is not %s\n", models.AWS)
- }
- if sa.AWSAccessKeyID != "ALSDKJFADSF" {
- t.Errorf("service account aws access key id is wrong: expected %s, got %s\n",
- "ALSDKJFADSF", sa.AWSAccessKeyID)
- }
- if sa.AWSSecretAccessKey != "ASDLFKJALSDKFJ" {
- t.Errorf("service account aws access secret key is wrong: expected %s, got %s\n",
- "ASDLFKJALSDKFJ", sa.AWSSecretAccessKey)
- }
- if sa.AWSClusterID != "cluster-test" {
- t.Errorf("service account aws cluster id is wrong: expected %s, got %s\n",
- "cluster-test", sa.AWSClusterID)
- }
- }
- func TestPopulateServiceAccountOIDCAction(t *testing.T) {
- // create the in-memory repository
- repo := test.NewRepository(true)
- // create a new project
- repo.Project.CreateProject(&models.Project{
- Name: "test-project",
- })
- // create a ServiceAccountCandidate from a kubeconfig
- saCandidates, err := kubernetes.GetServiceAccountCandidates([]byte(OIDCAuthWithoutData))
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- for _, saCandidate := range saCandidates {
- repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
- }
- // create a new form
- form := forms.OIDCIssuerDataAction{
- ServiceAccountActionResolver: &forms.ServiceAccountActionResolver{
- ServiceAccountCandidateID: 1,
- },
- OIDCIssuerCAData: "LS0tLS1CRUdJTiBDRVJ=",
- }
- err = form.PopulateServiceAccount(repo.ServiceAccount)
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- sa, err := repo.ServiceAccount.CreateServiceAccount(form.ServiceAccountActionResolver.SA)
- if len(sa.Clusters) != 1 {
- t.Fatalf("cluster not written\n")
- }
- if sa.Clusters[0].ServiceAccountID != 1 {
- t.Errorf("service account ID of joined cluster is not 1")
- }
- if sa.AuthMechanism != models.OIDC {
- t.Errorf("service account auth mechanism is not %s\n", models.OIDC)
- }
- if string(sa.OIDCCertificateAuthorityData) != "LS0tLS1CRUdJTiBDRVJ=" {
- t.Errorf("service account key data and input do not match: expected %s, got %s\n",
- string(sa.OIDCCertificateAuthorityData), "LS0tLS1CRUdJTiBDRVJ=")
- }
- }
- const ClusterCAWithData string = `
- apiVersion: v1
- kind: Config
- clusters:
- - name: cluster-test
- cluster:
- server: https://localhost
- certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- users:
- - name: test-admin
- user:
- client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
- client-key-data: LS0tLS1CRUdJTiBDRVJ=
- current-context: context-test
- `
- const ClusterCAWithoutData string = `
- apiVersion: v1
- kind: Config
- clusters:
- - name: cluster-test
- cluster:
- server: https://localhost
- certificate-authority: /fake/path/to/ca.pem
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- users:
- - name: test-admin
- user:
- client-certificate-data: LS0tLS1CRUdJTiBDRVJ=
- client-key-data: LS0tLS1CRUdJTiBDRVJ=
- current-context: context-test
- `
- const ClientWithoutCertData string = `
- apiVersion: v1
- kind: Config
- clusters:
- - name: cluster-test
- cluster:
- server: https://localhost
- certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- users:
- - name: test-admin
- user:
- client-certificate: /fake/path/to/ca.pem
- client-key-data: LS0tLS1CRUdJTiBDRVJ=
- current-context: context-test
- `
- const ClientWithoutCertAndKeyData string = `
- apiVersion: v1
- kind: Config
- clusters:
- - name: cluster-test
- cluster:
- server: https://localhost
- certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- users:
- - name: test-admin
- user:
- client-certificate: /fake/path/to/ca.pem
- client-key: /fake/path/to/ca.pem
- current-context: context-test
- `
- const BearerTokenWithoutData string = `
- apiVersion: v1
- kind: Config
- preferences: {}
- current-context: context-test
- clusters:
- - cluster:
- server: https://localhost
- name: cluster-test
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- users:
- - name: test-admin
- user:
- tokenFile: /path/to/token/file.txt
- `
- const GCPPlugin string = `
- apiVersion: v1
- kind: Config
- clusters:
- - name: cluster-test
- cluster:
- server: https://localhost
- certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
- users:
- - name: test-admin
- user:
- auth-provider:
- name: gcp
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- current-context: context-test
- `
- const AWSEKSGetTokenExec string = `
- apiVersion: v1
- clusters:
- - cluster:
- server: https://localhost
- certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
- name: cluster-test
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- current-context: context-test
- kind: Config
- preferences: {}
- users:
- - name: test-admin
- user:
- exec:
- apiVersion: client.authentication.k8s.io/v1alpha1
- command: aws
- args:
- - "eks"
- - "get-token"
- - "--cluster-name"
- - "cluster-test"
- `
- const OIDCAuthWithoutData string = `
- apiVersion: v1
- clusters:
- - cluster:
- server: https://localhost
- certificate-authority-data: LS0tLS1CRUdJTiBDRVJ=
- name: cluster-test
- contexts:
- - context:
- cluster: cluster-test
- user: test-admin
- name: context-test
- current-context: context-test
- kind: Config
- preferences: {}
- users:
- - name: test-admin
- user:
- auth-provider:
- config:
- client-id: porter-api
- id-token: token
- idp-issuer-url: https://localhost
- idp-certificate-authority: /fake/path/to/ca.pem
- name: oidc
- `
|