Skip to content

Commit

Permalink
Reads CF_INSTANCE_CERT and KEY from filepath
Browse files Browse the repository at this point in the history
 - 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`
  • Loading branch information
bonzofenix committed Dec 16, 2024
1 parent 2bc780c commit 67b868d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
3 changes: 2 additions & 1 deletion src/autoscaler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
24 changes: 4 additions & 20 deletions src/autoscaler/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 25 additions & 12 deletions src/autoscaler/api/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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() {
Expand All @@ -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))
})
})

Expand Down

0 comments on commit 67b868d

Please sign in to comment.