github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/integration/v7/selfcontained/kubernetes_auth_log_test.go (about)

     1  package selfcontained_test
     2  
     3  import (
     4  	"net/http"
     5  	"path/filepath"
     6  
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/integration/helpers"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/integration/v7/selfcontained/fake"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/gexec"
    13  	apiv1 "k8s.io/client-go/tools/clientcmd/api/v1"
    14  
    15  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    16  )
    17  
    18  var _ = Describe("logclient 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 /api/v1/read/test-guid": {
    28  					Code: http.StatusOK, Body: map[string]interface{}{
    29  						"envelopes": map[string]interface{}{
    30  							"batch": []string{},
    31  						},
    32  					},
    33  				},
    34  				"GET /api/v1/info": {
    35  					Code: http.StatusOK, Body: map[string]interface{}{
    36  						"version":   "42.1.2",
    37  						"vm_uptime": "0",
    38  					},
    39  				},
    40  				"GET /v3/apps": {
    41  					Code: http.StatusOK, Body: map[string]interface{}{
    42  						"pagination": map[string]interface{}{},
    43  						"resources": []map[string]string{
    44  							{
    45  								"guid": "test-guid",
    46  							},
    47  						},
    48  					},
    49  				},
    50  				"GET /whoami": {
    51  					Code: http.StatusOK, Body: map[string]interface{}{
    52  						"name": "my-user",
    53  						"kind": "User",
    54  					},
    55  				},
    56  			},
    57  		}
    58  		apiServer.SetConfiguration(apiConfig)
    59  		helpers.SetConfig(func(config *configv3.Config) {
    60  			config.ConfigFile.Target = apiServer.URL()
    61  			config.ConfigFile.LogCacheEndpoint = apiServer.URL()
    62  			config.ConfigFile.CFOnK8s.Enabled = true
    63  			config.ConfigFile.CFOnK8s.AuthInfo = "my-user"
    64  			config.ConfigFile.TargetedOrganization = configv3.Organization{
    65  				GUID: "my-org",
    66  				Name: "My Org",
    67  			}
    68  
    69  			config.ConfigFile.TargetedSpace = configv3.Space{
    70  				GUID: "my-space",
    71  				Name: "My Space",
    72  			}
    73  		})
    74  
    75  		kubeConfig = apiv1.Config{
    76  			Kind:       "Config",
    77  			APIVersion: "v1",
    78  			AuthInfos: []apiv1.NamedAuthInfo{
    79  				{
    80  					Name: "my-user",
    81  					AuthInfo: apiv1.AuthInfo{
    82  						AuthProvider: &apiv1.AuthProviderConfig{
    83  							Name: "oidc",
    84  							Config: map[string]string{
    85  								"id-token":       string(token),
    86  								"idp-issuer-url": "-",
    87  								"client-id":      "-",
    88  							},
    89  						},
    90  					},
    91  				},
    92  			},
    93  			Clusters: []apiv1.NamedCluster{
    94  				{
    95  					Name: "my-cluster",
    96  					Cluster: apiv1.Cluster{
    97  						Server: "https://example.org",
    98  					},
    99  				},
   100  			},
   101  			Contexts: []apiv1.NamedContext{
   102  				{
   103  					Name: "my-context",
   104  					Context: apiv1.Context{
   105  						Cluster:   "my-cluster",
   106  						AuthInfo:  "my-auth-info",
   107  						Namespace: "my-namespace",
   108  					},
   109  				},
   110  			},
   111  			CurrentContext: "my-context",
   112  		}
   113  		kubeConfigPath := filepath.Join(homeDir, ".kube", "config")
   114  		storeKubeConfig(kubeConfig, kubeConfigPath)
   115  
   116  		env = helpers.CFEnv{
   117  			EnvVars: map[string]string{
   118  				"KUBECONFIG": kubeConfigPath,
   119  			},
   120  		}
   121  	})
   122  
   123  	JustBeforeEach(func() {
   124  		Eventually(helpers.CustomCF(env, "logs", "--recent", "my-test-app")).Should(gexec.Exit(0))
   125  	})
   126  
   127  	It("sends the Bearer token in the Authorization header", func() {
   128  		reqs := apiServer.ReceivedRequests()["GET /api/v1/read/test-guid"]
   129  		Expect(reqs).To(HaveLen(1))
   130  		Expect(reqs[0].Header).To(HaveKeyWithValue("Authorization", ConsistOf("Bearer "+string(token))))
   131  	})
   132  
   133  	It("sends the User-Agent header", func() {
   134  		reqs := apiServer.ReceivedRequests()["GET /api/v1/read/test-guid"]
   135  		Expect(reqs).To(HaveLen(1))
   136  		Expect(reqs[0].Header).To(HaveKeyWithValue("User-Agent", ConsistOf(ContainSubstring("cf"))))
   137  	})
   138  })