From ee509b7edcc3813f4c34481ed5825a8a808a888b Mon Sep 17 00:00:00 2001 From: Alan Moran Date: Fri, 27 Dec 2024 16:22:58 -0300 Subject: [PATCH] Updated VSCode settings and added HTTP configuration for scheduler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Changed VSCode Java build configuration setting from automatic to interactive • Added new CFHTTPConfiguration class to configure additional Tomcat connector for HTTP on port 8090 • Set server port to 8083 and added HTTP port configuration in application.yml --- jobs/scalingengine/spec | 4 +- src/autoscaler/api/cmd/api/api_test.go | 102 +++++++++++------- src/scheduler/.vscode/settings.json | 2 +- .../scheduler/conf/CFHTTPConfiguration.java | 21 ++++ .../src/main/resources/application.yml | 3 + 5 files changed, 90 insertions(+), 42 deletions(-) create mode 100644 src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/CFHTTPConfiguration.java diff --git a/jobs/scalingengine/spec b/jobs/scalingengine/spec index f92c0bd40f..08eddd5219 100644 --- a/jobs/scalingengine/spec +++ b/jobs/scalingengine/spec @@ -174,11 +174,11 @@ properties: default: 8080 autoscaler.cf_server.xfcc.valid_org_guid: - description: approve org guid for xfcc endpoint + description: allowed org guid for xfcc endpoint default: '' autoscaler.cf_server.xfcc.valid_space_guid: - description: approve space guid for xfcc endpoint + description: allowed space guid for xfcc endpoint default: '' autoscaler.scalingengine.health.port: diff --git a/src/autoscaler/api/cmd/api/api_test.go b/src/autoscaler/api/cmd/api/api_test.go index 67e13684db..c8c349a740 100644 --- a/src/autoscaler/api/cmd/api/api_test.go +++ b/src/autoscaler/api/cmd/api/api_test.go @@ -1,6 +1,8 @@ package main_test import ( + "crypto/rand" + "crypto/rsa" "fmt" "io" "net/http" @@ -9,9 +11,9 @@ import ( "strings" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/api/config" + "code.cloudfoundry.org/app-autoscaler/src/autoscaler/configutil" "code.cloudfoundry.org/app-autoscaler/src/autoscaler/db" - - . "code.cloudfoundry.org/app-autoscaler/src/autoscaler/testhelpers" + "code.cloudfoundry.org/app-autoscaler/src/autoscaler/testhelpers" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -44,9 +46,9 @@ var _ = Describe("Api", func() { vcapPort = 8080 + GinkgoParallelProcess() - brokerHttpClient = NewServiceBrokerClient() + brokerHttpClient = testhelpers.NewServiceBrokerClient() healthHttpClient = &http.Client{} - apiHttpClient = NewPublicApiClient() + apiHttpClient = testhelpers.NewPublicApiClient() cfServerHttpClient = &http.Client{} serverURL, err = url.Parse(fmt.Sprintf("https://127.0.0.1:%d", cfg.Server.Port)) @@ -166,7 +168,7 @@ var _ = Describe("Api", func() { bodyBytes, err := io.ReadAll(rsp.Body) - FailOnError("Read failed", err) + testhelpers.FailOnError("Read failed", err) if len(bodyBytes) == 0 { Fail("body empty") } @@ -297,50 +299,72 @@ var _ = Describe("Api", func() { }) When("running CF server", func() { - XWhen("running in outside cf", func() {}) - When("running in CF", func() { + var ( + cfInstanceKeyFile string + cfInstanceCertFile string + ) - BeforeEach(func() { - os.Setenv("VCAP_APPLICATION", "{}") - os.Setenv("VCAP_SERVICES", getVcapServices()) - os.Setenv("PORT", fmt.Sprintf("%d", vcapPort)) - runner.Start() - }) - AfterEach(func() { - runner.Interrupt() - Eventually(runner.Session, 5).Should(Exit(0)) - os.Unsetenv("VCAP_APPLICATION") - os.Unsetenv("VCAP_SERVICES") - os.Unsetenv("PORT") - }) + BeforeEach(func() { + rsaPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) + Expect(err).NotTo(HaveOccurred()) - It("should start a cf server", func() { - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/info", cfServerURL), nil) - Expect(err).NotTo(HaveOccurred()) + cfInstanceCert, err := testhelpers.GenerateClientCertWithPrivateKey("org-guid", "space-guid", rsaPrivateKey) + Expect(err).NotTo(HaveOccurred()) - rsp, err = cfServerHttpClient.Do(req) - Expect(err).ToNot(HaveOccurred()) + certTmpDir := os.TempDir() - bodyBytes, err := io.ReadAll(rsp.Body) - Expect(err).ToNot(HaveOccurred()) - Expect(bodyBytes).To(ContainSubstring("Automatically increase or decrease the number of application instances based on a policy you define.")) + cfInstanceCertFile, err := configutil.MaterializeContentInFile(certTmpDir, "eventgenerator.crt", string(cfInstanceCert)) + Expect(err).NotTo(HaveOccurred()) + os.Setenv("CF_INSTANCE_CERT", string(cfInstanceCertFile)) - req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/catalog", cfServerURL), nil) - Expect(err).NotTo(HaveOccurred()) - req.SetBasicAuth(username, password) + cfInstanceKey := testhelpers.GenerateClientKeyWithPrivateKey(rsaPrivateKey) + cfInstanceKeyFile, err = configutil.MaterializeContentInFile(certTmpDir, "eventgenerator.key", string(cfInstanceKey)) + Expect(err).NotTo(HaveOccurred()) + os.Setenv("CF_INSTANCE_KEY", string(cfInstanceKeyFile)) - rsp, err = cfServerHttpClient.Do(req) - Expect(err).ToNot(HaveOccurred()) - Expect(rsp.StatusCode).To(Equal(http.StatusOK)) + os.Setenv("VCAP_APPLICATION", "{}") + os.Setenv("VCAP_SERVICES", getVcapServices()) + os.Setenv("PORT", fmt.Sprintf("%d", vcapPort)) + runner.Start() + }) + AfterEach(func() { + runner.Interrupt() + Eventually(runner.Session, 5).Should(Exit(0)) - bodyBytes, err = io.ReadAll(rsp.Body) - Expect(err).ToNot(HaveOccurred()) - Expect(bodyBytes).To(ContainSubstring("autoscaler-free-plan-id")) - }) + os.Remove(cfInstanceKeyFile) + os.Remove(cfInstanceCertFile) + os.Unsetenv("CF_INSTANCE_KEY") + os.Unsetenv("CF_INSTANCE_CERT") + os.Unsetenv("VCAP_APPLICATION") + os.Unsetenv("VCAP_SERVICES") + os.Unsetenv("PORT") }) - }) + It("should start a cf server", func() { + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/info", cfServerURL), nil) + Expect(err).NotTo(HaveOccurred()) + + rsp, err = cfServerHttpClient.Do(req) + Expect(err).ToNot(HaveOccurred()) + + bodyBytes, err := io.ReadAll(rsp.Body) + Expect(err).ToNot(HaveOccurred()) + Expect(bodyBytes).To(ContainSubstring("Automatically increase or decrease the number of application instances based on a policy you define.")) + + req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v2/catalog", cfServerURL), nil) + Expect(err).NotTo(HaveOccurred()) + req.SetBasicAuth(username, password) + + rsp, err = cfServerHttpClient.Do(req) + Expect(err).ToNot(HaveOccurred()) + Expect(rsp.StatusCode).To(Equal(http.StatusOK)) + + bodyBytes, err = io.ReadAll(rsp.Body) + Expect(err).ToNot(HaveOccurred()) + Expect(bodyBytes).To(ContainSubstring("autoscaler-free-plan-id")) + }) + }) }) func getVcapServices() (result string) { diff --git a/src/scheduler/.vscode/settings.json b/src/scheduler/.vscode/settings.json index 04cd618865..050505ce79 100644 --- a/src/scheduler/.vscode/settings.json +++ b/src/scheduler/.vscode/settings.json @@ -1,3 +1,3 @@ { - "java.configuration.updateBuildConfiguration": "automatic" + "java.configuration.updateBuildConfiguration": "interactive" } diff --git a/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/CFHTTPConfiguration.java b/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/CFHTTPConfiguration.java new file mode 100644 index 0000000000..db6bff3142 --- /dev/null +++ b/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/conf/CFHTTPConfiguration.java @@ -0,0 +1,21 @@ +package org.cloudfoundry.autoscaler.scheduler.conf; + +import org.apache.catalina.connector.Connector; +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; +import org.springframework.boot.web.server.WebServerFactoryCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class CFHTTPConfiguration { + + @Bean + public WebServerFactoryCustomizer httpConnectorCustomizer() { + return factory -> { + Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL); + connector.setPort(8090); + connector.setSecure(false); // Set to false for HTTP + factory.addAdditionalTomcatConnectors(connector); + }; + } +} diff --git a/src/scheduler/src/main/resources/application.yml b/src/scheduler/src/main/resources/application.yml index 49e8b1f2a9..7bac8e67e5 100644 --- a/src/scheduler/src/main/resources/application.yml +++ b/src/scheduler/src/main/resources/application.yml @@ -124,6 +124,9 @@ scheduler: ############################################################ server: + port: 8083 + http: + port: 8090 ssl: ciphers: TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 enabled-protocols: TLSv1.3