github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/integration/v7/selfcontained/selfcontained_suite_test.go (about) 1 package selfcontained_test 2 3 import ( 4 "bytes" 5 "crypto/rand" 6 "crypto/rsa" 7 "encoding/base64" 8 "encoding/json" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "testing" 13 "time" 14 15 "code.cloudfoundry.org/cli/integration/helpers" 16 "code.cloudfoundry.org/cli/integration/v7/selfcontained/fake" 17 "code.cloudfoundry.org/cli/util/configv3" 18 "github.com/SermoDigital/jose/crypto" 19 "github.com/SermoDigital/jose/jws" 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 "gopkg.in/yaml.v2" 23 apiv1 "k8s.io/client-go/tools/clientcmd/api/v1" 24 ) 25 26 var ( 27 homeDir string 28 apiServer *fake.CFAPI 29 env helpers.CFEnv 30 token []byte 31 ) 32 33 func TestSelfcontained(t *testing.T) { 34 RegisterFailHandler(Fail) 35 RunSpecs(t, "Selfcontained Suite") 36 } 37 38 var _ = BeforeEach(func() { 39 homeDir = helpers.SetHomeDir() 40 apiServer = fake.NewCFAPI() 41 helpers.SetConfig(func(config *configv3.Config) { 42 config.ConfigFile.Target = apiServer.URL() 43 }) 44 45 keyPair, err := rsa.GenerateKey(rand.Reader, 2048) 46 Expect(err).NotTo(HaveOccurred()) 47 48 jwt := jws.NewJWT(jws.Claims{ 49 "exp": time.Now().Add(time.Hour).Unix(), 50 }, crypto.SigningMethodRS256) 51 token, err = jwt.Serialize(keyPair) 52 Expect(err).NotTo(HaveOccurred()) 53 }) 54 55 var _ = AfterEach(func() { 56 apiServer.Close() 57 helpers.DestroyHomeDir(homeDir) 58 }) 59 60 func loadConfig() configv3.JSONConfig { 61 rawConfig, err := ioutil.ReadFile(filepath.Join(homeDir, ".cf", "config.json")) 62 Expect(err).NotTo(HaveOccurred()) 63 64 var configFile configv3.JSONConfig 65 Expect(json.Unmarshal(rawConfig, &configFile)).To(Succeed()) 66 67 return configFile 68 } 69 70 func storeKubeConfig(kubeconfig apiv1.Config, kubeConfigPath string) { 71 Expect(os.MkdirAll(filepath.Dir(kubeConfigPath), 0o755)).To(Succeed()) 72 kubeConfigFile, err := os.OpenFile(kubeConfigPath, os.O_CREATE|os.O_WRONLY, 0o755) 73 Expect(kubeConfigFile.Truncate(0)).To(Succeed()) 74 Expect(err).NotTo(HaveOccurred()) 75 76 // we need to serialise the config to JSON as the Config type only has json annotations (and no yaml ones) 77 // However, during json serialisation, byte arrays are base64 encoded which is not a desired side effect. 78 // In order to address this, we base64 decode them in advance 79 kubeconfig = base64DecodeClientCertByteArrays(kubeconfig) 80 var buf bytes.Buffer 81 err = json.NewEncoder(&buf).Encode(kubeconfig) 82 Expect(err).NotTo(HaveOccurred()) 83 84 var configmap map[string]interface{} 85 err = json.Unmarshal(buf.Bytes(), &configmap) 86 Expect(err).NotTo(HaveOccurred()) 87 88 // now we can save the config as yaml 89 err = yaml.NewEncoder(kubeConfigFile).Encode(configmap) 90 Expect(err).NotTo(HaveOccurred()) 91 Expect(kubeConfigFile.Close()).To(Succeed()) 92 } 93 94 func base64DecodeClientCertByteArrays(kubeconfig apiv1.Config) apiv1.Config { 95 decodedAuthInfos := []apiv1.NamedAuthInfo{} 96 for _, authInfo := range kubeconfig.AuthInfos { 97 if len(authInfo.AuthInfo.ClientCertificateData) > 0 { 98 decodedCertData, err := base64.StdEncoding.DecodeString(string(authInfo.AuthInfo.ClientCertificateData)) 99 Expect(err).NotTo(HaveOccurred()) 100 authInfo.AuthInfo.ClientCertificateData = decodedCertData 101 } 102 if len(authInfo.AuthInfo.ClientKeyData) > 0 { 103 decodedKeyData, err := base64.StdEncoding.DecodeString(string(authInfo.AuthInfo.ClientKeyData)) 104 Expect(err).NotTo(HaveOccurred()) 105 authInfo.AuthInfo.ClientKeyData = decodedKeyData 106 } 107 108 decodedAuthInfos = append(decodedAuthInfos, authInfo) 109 } 110 111 kubeconfig.AuthInfos = decodedAuthInfos 112 return kubeconfig 113 }