From 67b868d1198c4c1fa762a42abc69f47009ffc15a Mon Sep 17 00:00:00 2001 From: Alan Moran Date: Mon, 16 Dec 2024 15:05:12 -0300 Subject: [PATCH] Reads CF_INSTANCE_CERT and KEY from filepath - Make `generate-fakes` target `.PHONY` in Makefile - Remove dependency of `generate-fakes` from `testsuite` target - Simplify `configureEventGenerator` function by directly setting `CertFile` and `KeyFile` from environment variables - Update tests to reflect changes in `configureEventGenerator` and remove unnecessary file creation for `CF_INSTANCE_CERT` and `CF_INSTANCE_KEY` --- src/autoscaler/Makefile | 3 +- src/autoscaler/api/config/config.go | 24 +++------------ src/autoscaler/api/config/config_test.go | 37 ++++++++++++++++-------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/autoscaler/Makefile b/src/autoscaler/Makefile index 34d73e2d47..9e6c750e6b 100644 --- a/src/autoscaler/Makefile +++ b/src/autoscaler/Makefile @@ -121,11 +121,12 @@ build_test-%: generate-fakes check: fmt lint build test +.PHONY: generate-fakes test: generate-fakes @echo "Running tests" APP_AUTOSCALER_TEST_RUN='true' go run 'github.com/onsi/ginkgo/v2/ginkgo@${GINKGO_VERSION}' -p ${GINKGO_OPTS} ${TEST} --skip-package='integration' -testsuite: generate-fakes +testsuite: @echo " - using DBURL=${DBURL} TEST=${TEST}" APP_AUTOSCALER_TEST_RUN='true' go run 'github.com/onsi/ginkgo/v2/ginkgo@${GINKGO_VERSION}' -p ${GINKGO_OPTS} ${TEST} diff --git a/src/autoscaler/api/config/config.go b/src/autoscaler/api/config/config.go index c728ff53b5..62d0d764eb 100644 --- a/src/autoscaler/api/config/config.go +++ b/src/autoscaler/api/config/config.go @@ -210,31 +210,15 @@ func loadVcapConfig(conf *Config, vcapReader configutil.VCAPConfigurationReader) return err } - if err := configureEventGenerator(conf, vcapReader); err != nil { - return err - } + configureEventGenerator(conf) return nil } -func configureEventGenerator(conf *Config, vcapReader configutil.VCAPConfigurationReader) error { - cfInstanceKey := os.Getenv("CF_INSTANCE_KEY") - cfInstanceCert := os.Getenv("CF_INSTANCE_CERT") - - if keyFile, err := configutil.MaterializeContentInFile("eventgenerator", "eventgenerator.key", cfInstanceKey); err != nil { - return err - } else { - conf.EventGenerator.TLSClientCerts.KeyFile = keyFile - } +func configureEventGenerator(conf *Config) { + conf.EventGenerator.TLSClientCerts.CertFile = os.Getenv("CF_INSTANCE_CERT") + conf.EventGenerator.TLSClientCerts.KeyFile = os.Getenv("CF_INSTANCE_KEY") - if certFile, err := configutil.MaterializeContentInFile("eventgenerator", "eventgenerator.crt", cfInstanceCert); err != nil { - return err - } else { - conf.EventGenerator.TLSClientCerts.CertFile = certFile - conf.EventGenerator.TLSClientCerts.CACertFile = certFile - } - - return nil } func configurePolicyDb(conf *Config, vcapReader configutil.VCAPConfigurationReader) error { diff --git a/src/autoscaler/api/config/config_test.go b/src/autoscaler/api/config/config_test.go index 8848750828..883500680a 100644 --- a/src/autoscaler/api/config/config_test.go +++ b/src/autoscaler/api/config/config_test.go @@ -7,6 +7,7 @@ import ( "os" "time" + "code.cloudfoundry.org/app-autoscaler/src/autoscaler/configutil" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/fakes" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/testhelpers" @@ -45,24 +46,40 @@ var _ = Describe("Config", func() { }) When("vcap CF_INSTANCE_CERT is set", func() { - var cfInstanceCert []byte - var cfInstanceKey []byte + var ( + cfInstanceCertFile string + cfInstanceKeyFile string + + cfInstanceCertContent []byte + cfInstanceKeyContent []byte + ) BeforeEach(func() { rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) Expect(err).NotTo(HaveOccurred()) - cfInstanceCert, err = testhelpers.GenerateClientCertWithPrivateKey("org-guid", "space-guid", rsaPrivateKey) + cfInstanceCertContent, err = testhelpers.GenerateClientCertWithPrivateKey("org-guid", "space-guid", rsaPrivateKey) Expect(err).NotTo(HaveOccurred()) - cfInstanceKey = testhelpers.GenerateClientKeyWithPrivateKey(rsaPrivateKey) - os.Setenv("CF_INSTANCE_KEY", string(cfInstanceKey)) - os.Setenv("CF_INSTANCE_CERT", string(cfInstanceCert)) + cfInstanceKeyContent = testhelpers.GenerateClientKeyWithPrivateKey(rsaPrivateKey) + + tmpdir := os.TempDir() + cfInstanceCertFile, err = configutil.MaterializeContentInFile(tmpdir, "eventgenerator.crt", string(cfInstanceCertContent)) + Expect(err).NotTo(HaveOccurred()) + + cfInstanceKeyFile, err = configutil.MaterializeContentInFile(tmpdir, "eventgenerator.key", string(cfInstanceKeyContent)) + Expect(err).NotTo(HaveOccurred()) + + os.Setenv("CF_INSTANCE_KEY", cfInstanceKeyFile) + os.Setenv("CF_INSTANCE_CERT", cfInstanceCertFile) }) AfterEach(func() { os.Unsetenv("CF_INSTANCE_KEY") os.Unsetenv("CF_INSTANCE_CERT") + + os.Remove(cfInstanceCertFile) + os.Remove(cfInstanceKeyFile) }) It("sets EventGenerator TlSClientCert", func() { @@ -72,12 +89,8 @@ var _ = Describe("Config", func() { actualCertContent, err := os.ReadFile(conf.EventGenerator.TLSClientCerts.CertFile) Expect(err).NotTo(HaveOccurred()) - actualCACertContent, err := os.ReadFile(conf.EventGenerator.TLSClientCerts.CACertFile) - Expect(err).NotTo(HaveOccurred()) - - Expect(actualKeyContent).To(Equal(cfInstanceKey)) - Expect(actualCertContent).To(Equal(cfInstanceCert)) - Expect(actualCACertContent).To(Equal(cfInstanceCert)) + Expect(actualKeyContent).To(Equal(cfInstanceKeyContent)) + Expect(actualCertContent).To(Equal(cfInstanceCertContent)) }) })