Skip to content

Commit

Permalink
WIP: Uses basic auth instead of mtls for components
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed Jul 3, 2024
1 parent 3305ba9 commit 9417582
Show file tree
Hide file tree
Showing 31 changed files with 225 additions and 154 deletions.
6 changes: 6 additions & 0 deletions jobs/eventgenerator/spec
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ properties:
autoscaler.eventgenerator.server.port:
description: "the listening port of server"
default: 6105
autoscaler.eventgenerator.server.username:
description: "the basic auth username for server endpoint"
default: ''
autoscaler.eventgenerator.server.password:
description: "the basic auth password for server endpoint"
default: ''
autoscaler.eventgenerator.http_client_timeout:
description: "Http client imeout for eventgenerator to communicate with other autoscaler components"
default: 60s
Expand Down
8 changes: 6 additions & 2 deletions jobs/eventgenerator/templates/eventgenerator.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ end


server:
basic_auth:
username: <%= p("autoscaler.eventgenerator.server.username") %>
password: <%= p("autoscaler.eventgenerator.server.password") %>
port: <%= p("autoscaler.eventgenerator.server.port") %>
tls:
key_file: /var/vcap/jobs/eventgenerator/config/certs/eventgenerator/server.key
Expand All @@ -69,8 +72,9 @@ logging:
level: <%= p("autoscaler.eventgenerator.logging.level") %>
http_client_timeout: <%= p("autoscaler.eventgenerator.http_client_timeout") %>
health:
username: <%= p("autoscaler.eventgenerator.health.username") %>
password: <%= p("autoscaler.eventgenerator.health.password") %>
basic_auth:
username: <%= p("autoscaler.eventgenerator.health.username") %>
password: <%= p("autoscaler.eventgenerator.health.password") %>

db:
policy_db:
Expand Down
5 changes: 3 additions & 2 deletions jobs/metricsforwarder/templates/metricsforwarder.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ cache_ttl: <%= p("autoscaler.metricsforwarder.cache_ttl") %>
cache_cleanup_interval: <%= p("autoscaler.metricsforwarder.cache_cleanup_interval") %>
policy_poller_interval: <%= p("autoscaler.metricsforwarder.policy_poller_interval") %>
health:
username: <%= p("autoscaler.metricsforwarder.health.username") %>
password: <%= p("autoscaler.metricsforwarder.health.password") %>
basic_auth:
username: <%= p("autoscaler.metricsforwarder.health.username") %>
password: <%= p("autoscaler.metricsforwarder.health.password") %>

rate_limit:
valid_duration: <%= p("autoscaler.metricsforwarder.rate_limit.valid_duration") %>
Expand Down
5 changes: 3 additions & 2 deletions jobs/operator/templates/operator.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ server:
logging:
level: <%= p("autoscaler.operator.logging.level") %>
health:
username: <%= p("autoscaler.operator.health.username") %>
password: <%= p("autoscaler.operator.health.password") %>
basic_auth:
username: <%= p("autoscaler.operator.health.username") %>
password: <%= p("autoscaler.operator.health.password") %>

http_client_timeout: <%= p("autoscaler.operator.http_client_timeout") %>

Expand Down
5 changes: 3 additions & 2 deletions jobs/scalingengine/templates/scalingengine.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ logging:
level: <%= p("autoscaler.scalingengine.logging.level") %>
http_client_timeout: <%= p("autoscaler.scalingengine.http_client_timeout") %>
health:
username: <%= p("autoscaler.scalingengine.health.username") %>
password: <%= p("autoscaler.scalingengine.health.password") %>
basic_auth:
username: <%= p("autoscaler.scalingengine.health.username") %>
password: <%= p("autoscaler.scalingengine.health.password") %>

db:
policy_db:
Expand Down
9 changes: 5 additions & 4 deletions src/autoscaler/api/cmd/api/api_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ var (
catalogBytes string
schedulerServer *ghttp.Server
brokerPort int
publicApiPort int
infoBytes string
ccServer *mocks.Server
)
Expand Down Expand Up @@ -113,7 +112,7 @@ var _ = SynchronizedBeforeSuite(func() []byte {
catalogBytes = info.CatalogBytes
infoBytes = info.InfoBytes
brokerPort = 8000 + GinkgoParallelProcess()
publicApiPort = 9000 + GinkgoParallelProcess()
publicApiPort := 9000 + GinkgoParallelProcess()

cfg.BrokerServer = helpers.ServerConfig{
Port: brokerPort,
Expand Down Expand Up @@ -196,8 +195,10 @@ var _ = SynchronizedBeforeSuite(func() []byte {
cfg.CF.Secret = "client-secret"
cfg.CF.SkipSSLValidation = true
cfg.Health = helpers.HealthConfig{
HealthCheckUsername: "healthcheckuser",
HealthCheckPassword: "healthcheckpassword",
BasicAuth: models.BasicAuth{
Username: "healthcheckuser",
Password: "healthcheckpassword",
},
}
cfg.RateLimit.MaxAmount = 10
cfg.RateLimit.ValidDuration = 1 * time.Second
Expand Down
15 changes: 8 additions & 7 deletions src/autoscaler/api/cmd/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var _ = Describe("Api", func() {
BeforeEach(func() {
brokerHttpClient = NewServiceBrokerClient()
runner = NewApiRunner()
serverURL = fmt.Sprintf("https://127.0.0.1:%d", cfg.PublicApiServer.Port)
serverURL = fmt.Sprintf("http://127.0.0.1:%d", cfg.PublicApiServer.Port)
})

Describe("Api configuration check", func() {
Expand Down Expand Up @@ -120,8 +120,9 @@ var _ = Describe("Api", func() {
BeforeEach(func() {
runner.Start()
})

It("succeeds with a 200", func() {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://127.0.0.1:%d/v2/catalog", brokerPort), nil)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/v2/catalog", brokerPort), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth(username, password)
Expand Down Expand Up @@ -155,7 +156,7 @@ var _ = Describe("Api", func() {
runner.Start()
})
It("succeeds with a 200", func() {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://127.0.0.1:%d/v1/info", publicApiPort), nil)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/info", serverURL), nil)
Expect(err).NotTo(HaveOccurred())

rsp, err = apiHttpClient.Do(req)
Expand All @@ -171,8 +172,8 @@ var _ = Describe("Api", func() {
Describe("when Health server is ready to serve RESTful API", func() {
BeforeEach(func() {
basicAuthConfig := cfg
basicAuthConfig.Health.HealthCheckUsername = ""
basicAuthConfig.Health.HealthCheckPassword = ""
basicAuthConfig.Health.BasicAuth.Username = ""
basicAuthConfig.Health.BasicAuth.Password = ""
runner.configPath = writeConfig(&basicAuthConfig).Name()
runner.Start()
})
Expand Down Expand Up @@ -230,7 +231,7 @@ var _ = Describe("Api", func() {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/health", serverURL), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth(cfg.Health.HealthCheckUsername, cfg.Health.HealthCheckPassword)
req.SetBasicAuth(cfg.Health.BasicAuth.Username, cfg.Health.BasicAuth.Password)

rsp, err := apiHttpClient.Do(req)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -252,7 +253,7 @@ var _ = Describe("Api", func() {
})
Context("when a request to query health comes", func() {
It("returns with a 200", func() {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://127.0.0.1:%d/v1/info", publicApiPort), nil)
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/info", serverURL), nil)
Expect(err).NotTo(HaveOccurred())

rsp, err = apiHttpClient.Do(req)
Expand Down
4 changes: 2 additions & 2 deletions src/autoscaler/db/sqldb/scalingengine_sqldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ var _ = Describe("ScalingEngineSqldb", func() {
})

Context("when there is no previous app cooldown record", func() {
It("creates the record", func() {
XIt("creates the record", func() {
Expect(err).NotTo(HaveOccurred())
Expect(hasScalingCooldownRecord(appId, 222222)).To(BeTrue())
})
Expand All @@ -628,7 +628,7 @@ var _ = Describe("ScalingEngineSqldb", func() {
Expect(err).NotTo(HaveOccurred())
})

It("removes the previous record and inserts a new record", func() {
XIt("removes the previous record and inserts a new record", func() {
Expect(err).NotTo(HaveOccurred())
Expect(hasScalingCooldownRecord(appId, 111111)).To(BeFalse())
Expect(hasScalingCooldownRecord(appId, 222222)).To(BeTrue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,10 @@ func initConfig() {
DefaultStatWindowSecs: 300,
HttpClientTimeout: 10 * time.Second,
Health: helpers.HealthConfig{
HealthCheckUsername: "healthcheckuser",
HealthCheckPassword: "healthcheckpassword",
BasicAuth: models.BasicAuth{
Username: "healthcheckuser",
Password: "healthcheckpassword",
},
},
}
configFile = writeConfig(&conf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var _ = Describe("Eventgenerator", func() {
BeforeEach(func() {
runner = NewEventGeneratorRunner()
httpsClient = testhelpers.NewEventGeneratorClient()
serverURL = fmt.Sprintf("https://127.0.0.1:%d", conf.Server.Port)
serverURL = fmt.Sprintf("http://127.0.0.1:%d", conf.Server.Port)
})

AfterEach(func() {
Expand Down Expand Up @@ -145,8 +145,8 @@ var _ = Describe("Eventgenerator", func() {
Describe("when Health server is ready to serve RESTful API", func() {
BeforeEach(func() {
basicAuthConfig := conf
basicAuthConfig.Health.HealthCheckUsername = ""
basicAuthConfig.Health.HealthCheckPassword = ""
basicAuthConfig.Health.BasicAuth.Username = ""
basicAuthConfig.Health.BasicAuth.Password = ""
runner.configPath = writeConfig(&basicAuthConfig).Name()

runner.Start()
Expand Down Expand Up @@ -195,7 +195,7 @@ var _ = Describe("Eventgenerator", func() {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/health", serverURL), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth(conf.Health.HealthCheckUsername, conf.Health.HealthCheckPassword)
req.SetBasicAuth(conf.Health.BasicAuth.Username, conf.Health.BasicAuth.Password)

rsp, err := httpsClient.Do(req)
Expect(err).ToNot(HaveOccurred())
Expand Down Expand Up @@ -227,7 +227,7 @@ var _ = Describe("Eventgenerator", func() {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/health", serverURL), nil)
Expect(err).NotTo(HaveOccurred())

req.SetBasicAuth(conf.Health.HealthCheckUsername, conf.Health.HealthCheckPassword)
req.SetBasicAuth(conf.Health.BasicAuth.Username, conf.Health.BasicAuth.Password)

rsp, err := httpsClient.Do(req)
Expect(err).ToNot(HaveOccurred())
Expand Down
19 changes: 13 additions & 6 deletions src/autoscaler/eventgenerator/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,35 @@ func (vh VarsFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
vh(w, r, vars)
}

func NewServer(logger lager.Logger, conf *config.Config, appMetricDB db.AppMetricDB, policyDb db.PolicyDB, queryAppMetric aggregator.QueryAppMetricsFunc, httpStatusCollector healthendpoint.HTTPStatusCollector) (ifrit.Runner, error) {
eh := NewEventGenHandler(logger, queryAppMetric)
func createEventGeneratorRouter(logger lager.Logger, queryAppMetric aggregator.QueryAppMetricsFunc, httpStatusCollector healthendpoint.HTTPStatusCollector, serverConfig config.ServerConfig) (*mux.Router, error) {
ba, _ := helpers.CreateBasicAuthMiddleware(logger, serverConfig.BasicAuth)
httpStatusCollectMiddleware := healthendpoint.NewHTTPStatusCollectMiddleware(httpStatusCollector)
eh := NewEventGenHandler(logger, queryAppMetric)
r := routes.EventGeneratorRoutes()
r.Use(otelmux.Middleware("eventgenerator"))
r.Use(ba.BasicAuthenticationMiddleware)
r.Use(httpStatusCollectMiddleware.Collect)
r.Get(routes.GetAggregatedMetricHistoriesRouteName).Handler(VarsFunc(eh.GetAggregatedMetricHistories))
return r, nil
}

func NewServer(logger lager.Logger, conf *config.Config, appMetricDB db.AppMetricDB, policyDb db.PolicyDB, queryAppMetric aggregator.QueryAppMetricsFunc, httpStatusCollector healthendpoint.HTTPStatusCollector) (ifrit.Runner, error) {
eventGeneratorRouter, _ := createEventGeneratorRouter(logger, queryAppMetric, httpStatusCollector, conf.Server)

healthRouter, err := createHealthRouter(appMetricDB, policyDb, logger, conf, httpStatusCollector)
if err != nil {
return nil, fmt.Errorf("failed to create health router: %w", err)
}

mainRouter := setupMainRouter(r, healthRouter)
mainRouter := setupMainRouter(eventGeneratorRouter, healthRouter)
return helpers.NewHTTPServer(logger, serverConfigFrom(conf), mainRouter)
}

func serverConfigFrom(conf *config.Config) helpers.ServerConfig {
return helpers.ServerConfig{
Port: conf.Server.Port,
TLS: conf.Server.TLS,
BasicAuth: conf.Server.BasicAuth,
Port: conf.Server.Port,
TLS: conf.Server.TLS,
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/autoscaler/eventgenerator/server/server_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
policyDB *fakes.FakePolicyDB

appMetricDB *fakes.FakeAppMetricDB
conf *config.Config
)

func TestServer(t *testing.T) {
Expand All @@ -34,10 +35,14 @@ func TestServer(t *testing.T) {

var _ = BeforeSuite(func() {
port := 1111 + GinkgoParallelProcess()
conf := &config.Config{
conf = &config.Config{
Server: config.ServerConfig{
ServerConfig: helpers.ServerConfig{
Port: port,
BasicAuth: models.BasicAuth{
Username: "eventgenerator",
Password: "some-password",
},
},
},
}
Expand Down
80 changes: 56 additions & 24 deletions src/autoscaler/eventgenerator/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,81 @@ package server_test

import (
"net/http"
"net/url"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

const TestPathAggregatedMetricHistories = "/v1/apps/an-app-id/aggregated_metric_histories/a-metric-type"

var _ = Describe("Server", func() {
var (
rsp *http.Response
err error
)

Context("when retrieving aggregared metrics history", func() {
Describe("request on /v1/apps/an-app-id/aggregated_metric_histories/a-metric-type", func() {
BeforeEach(func() {
serverUrl.Path = TestPathAggregatedMetricHistories
})
serverUrl.Path = "/v1/apps/an-app-id/aggregated_metric_histories/a-metric-type"

JustBeforeEach(func() {
rsp, err = http.Get(serverUrl.String())
})

It("should return 200", func() {
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
rsp.Body.Close()
})
})
Context("when retrieving aggregared metrics history", func() {
var (
username string
password string
)

Context("when using wrong method to retrieve aggregared metrics history", func() {
BeforeEach(func() {
serverUrl.Path = TestPathAggregatedMetricHistories
})
JustBeforeEach(func() {
serverUrl.User = url.UserPassword(username, password)
rsp, err = http.Get(serverUrl.String())
})

When("basic auth is enabled", func() {

JustBeforeEach(func() {
rsp, err = http.Post(serverUrl.String(), "garbage", nil)
BeforeEach(func() {
username = conf.Server.BasicAuth.Username
password = conf.Server.BasicAuth.Password
})

It("should return 200", func() {
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
rsp.Body.Close()
})

When("basic auth request is not set", func() {
BeforeEach(func() {
username = ""
password = ""
})

It("should return 401 when basic auth is not provided", func() {
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusUnauthorized))
rsp.Body.Close()
})
})
})

When("basic auth is disabled", func() {
XIt("should return 200", func() {
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusOK))
rsp.Body.Close()
})
})
})

It("should return 405", func() {
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusMethodNotAllowed))
rsp.Body.Close()
Context("when using wrong method to retrieve aggregared metrics history", func() {
JustBeforeEach(func() {
rsp, err = http.Post(serverUrl.String(), "garbage", nil)
})

It("should return 405", func() {
Expect(err).ToNot(HaveOccurred())
Expect(rsp.StatusCode).To(Equal(http.StatusMethodNotAllowed))
rsp.Body.Close()
})
})
})

})
Loading

0 comments on commit 9417582

Please sign in to comment.