github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/selfcontained/kubernetes_auth_test.go (about)

     1  package selfcontained_test
     2  
     3  import (
     4  	"net/http"
     5  	"path/filepath"
     6  
     7  	. "github.com/onsi/ginkgo"
     8  	. "github.com/onsi/gomega"
     9  	"github.com/onsi/gomega/gexec"
    10  	apiv1 "k8s.io/client-go/tools/clientcmd/api/v1"
    11  
    12  	"code.cloudfoundry.org/cli/integration/helpers"
    13  	"code.cloudfoundry.org/cli/integration/v7/selfcontained/fake"
    14  	"code.cloudfoundry.org/cli/resources"
    15  	"code.cloudfoundry.org/cli/util/configv3"
    16  )
    17  
    18  var _ = Describe("auth-provider", func() {
    19  	var (
    20  		apiConfig  fake.CFAPIConfig
    21  		kubeConfig apiv1.Config
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		apiConfig = fake.CFAPIConfig{
    26  			Routes: map[string]fake.Response{
    27  				"GET /v3/apps": {
    28  					Code: http.StatusOK, Body: map[string]interface{}{
    29  						"pagination": map[string]interface{}{},
    30  						"resources":  []resources.Application{},
    31  					},
    32  				},
    33  				"GET /whoami": {
    34  					Code: http.StatusOK, Body: map[string]interface{}{
    35  						"name": "my-user",
    36  						"kind": "User",
    37  					},
    38  				},
    39  			},
    40  		}
    41  		apiServer.SetConfiguration(apiConfig)
    42  		helpers.SetConfig(func(config *configv3.Config) {
    43  			config.ConfigFile.Target = apiServer.URL()
    44  			config.ConfigFile.CFOnK8s.Enabled = true
    45  			config.ConfigFile.CFOnK8s.AuthInfo = "my-user"
    46  			config.ConfigFile.TargetedOrganization = configv3.Organization{
    47  				GUID: "my-org",
    48  				Name: "My Org",
    49  			}
    50  
    51  			config.ConfigFile.TargetedSpace = configv3.Space{
    52  				GUID: "my-space",
    53  				Name: "My Space",
    54  			}
    55  		})
    56  
    57  		kubeConfig = apiv1.Config{
    58  			Kind:       "Config",
    59  			APIVersion: "v1",
    60  			AuthInfos: []apiv1.NamedAuthInfo{
    61  				{
    62  					Name: "my-user",
    63  					AuthInfo: apiv1.AuthInfo{
    64  						AuthProvider: &apiv1.AuthProviderConfig{
    65  							Name: "oidc",
    66  							Config: map[string]string{
    67  								"id-token":       string(token),
    68  								"idp-issuer-url": "-",
    69  								"client-id":      "-",
    70  							},
    71  						},
    72  					},
    73  				},
    74  			},
    75  			Clusters: []apiv1.NamedCluster{
    76  				{
    77  					Name: "my-cluster",
    78  					Cluster: apiv1.Cluster{
    79  						Server: "https://example.org",
    80  					},
    81  				},
    82  			},
    83  			Contexts: []apiv1.NamedContext{
    84  				{
    85  					Name: "my-context",
    86  					Context: apiv1.Context{
    87  						Cluster:   "my-cluster",
    88  						AuthInfo:  "my-auth-info",
    89  						Namespace: "my-namespace",
    90  					},
    91  				},
    92  			},
    93  			CurrentContext: "my-context",
    94  		}
    95  		kubeConfigPath := filepath.Join(homeDir, ".kube", "config")
    96  		storeKubeConfig(kubeConfig, kubeConfigPath)
    97  
    98  		env = helpers.CFEnv{
    99  			EnvVars: map[string]string{
   100  				"KUBECONFIG": kubeConfigPath,
   101  			},
   102  		}
   103  	})
   104  
   105  	JustBeforeEach(func() {
   106  		Eventually(helpers.CustomCF(env, "apps")).Should(gexec.Exit(0))
   107  	})
   108  
   109  	It("sends the Bearer token in the Authorization header", func() {
   110  		reqs := apiServer.ReceivedRequests()["GET /v3/apps"]
   111  		Expect(reqs).To(HaveLen(1))
   112  		Expect(reqs[0].Header).To(HaveKeyWithValue("Authorization", ConsistOf("Bearer "+string(token))))
   113  	})
   114  })