Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed Nov 25, 2024
1 parent 05c7cc4 commit 602f78c
Show file tree
Hide file tree
Showing 19 changed files with 912 additions and 728 deletions.
2 changes: 1 addition & 1 deletion src/autoscaler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ testsuite: generate-fakes
APP_AUTOSCALER_TEST_RUN='true' go run 'github.com/onsi/ginkgo/v2/ginkgo@${GINKGO_VERSION}' -p ${GINKGO_OPTS} ${TEST}

.PHONY: integration
integration: generate-fakes
integration: #generate-fakes
@echo "# Running integration tests"
APP_AUTOSCALER_TEST_RUN='true' go run 'github.com/onsi/ginkgo/v2/ginkgo@${GINKGO_VERSION}' ${GINKGO_OPTS} integration

Expand Down
1 change: 1 addition & 0 deletions src/autoscaler/api/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type Config struct {
PlanCheck *PlanCheckConfig `yaml:"plan_check"`
CatalogPath string `yaml:"catalog_path"`
CatalogSchemaPath string `yaml:"catalog_schema_path"`
CfInstanceCert string `yaml:"cf_instance_cert"`
DashboardRedirectURI string `yaml:"dashboard_redirect_uri"`
PolicySchemaPath string `yaml:"policy_schema_path"`
Scheduler SchedulerConfig `yaml:"scheduler"`
Expand Down
11 changes: 11 additions & 0 deletions src/autoscaler/api/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ var _ = Describe("Config", func() {
conf, err = LoadConfig("", mockVCAPConfigurationReader)
})

When("vcap CF_INSTANCE_CERT is set", func() {
BeforeEach(func() {
mockVCAPConfigurationReader
})
It("sets env variable over config file", func() {
Expect(err).NotTo(HaveOccurred())
Expect(conf.CFInstanceCert).To(Equal("cert"))
}

})

When("vcap PORT is set to a number", func() {
BeforeEach(func() {
mockVCAPConfigurationReader.GetPortReturns(3333)
Expand Down
2 changes: 1 addition & 1 deletion src/autoscaler/api/publicapiserver/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (mw *Middleware) Oauth(next http.Handler) http.Handler {
if err != nil {
mw.logger.Error("failed to check if user is admin", err, nil)
handlers.WriteJSONResponse(w, http.StatusInternalServerError, models.ErrorResponse{
Code: "Internal-Server-Error",
Code: http.StatusText(http.StatusInternalServerError),
Message: "Failed to check if user is admin"})
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/autoscaler/api/publicapiserver/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var _ = Describe("Middleware", func() {
})
It("should fail with 500", func() {
CheckResponse(resp, http.StatusInternalServerError, models.ErrorResponse{
Code: "Internal-Server-Error",
Code: http.StatusText(http.StatusInternalServerError),
Message: "Failed to check if user is admin",
})
})
Expand Down
58 changes: 39 additions & 19 deletions src/autoscaler/api/publicapiserver/public_api_handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package publicapiserver

import (
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -225,9 +226,42 @@ func (h *PublicApiHandler) DetachScalingPolicy(w http.ResponseWriter, r *http.Re
}
}

func proxyRequest(pathFn func() string, call func(url string) (*http.Response, error), w http.ResponseWriter, reqUrl *url.URL, parameters *url.Values, requestDescription string, logger lager.Logger) {
aUrl := pathFn()
resp, err := call(aUrl)
func (h *PublicApiHandler) proxyRequest(logger lager.Logger, appId string, metricType string, w http.ResponseWriter, req *http.Request, parameters *url.Values, requestDescription string) {
reqUrl := req.URL
r := routes.NewRouter()
router := r.CreateEventGeneratorRoutes()
if router == nil {
panic("Failed to create event generator routes")
}

route := router.Get(routes.GetAggregatedMetricHistoriesRouteName)
path, err := route.URLPath("appid", appId, "metrictype", metricType)
if err != nil {
logger.Error("Failed to create path", err)
panic(err)
}

aUrl := h.conf.EventGenerator.EventGeneratorUrl + path.RequestURI() + "?" + parameters.Encode()

req, err = http.NewRequest("GET", aUrl, nil)

if h.conf.CfInstanceCert != "" {
certPEM := []byte(h.conf.CfInstanceCert)

// Calculate SHA-256 hash of the certificate
hash := sha256.Sum256(certPEM)

// URL encode the PEM certificate
encodedCert := url.QueryEscape(string(certPEM))

// Construct the XFCC header value
xfccHeader := fmt.Sprintf("Hash=%x;Cert=\"%s\"", hash, encodedCert)

req.Header.Set("X-Forwarded-Client-Cert", xfccHeader)
}

resp, err := h.eventGeneratorClient.Do(req)

if err != nil {
logger.Error("Failed to retrieve "+requestDescription, err, lager.Data{"url": aUrl})
writeErrorResponse(w, http.StatusInternalServerError, "Error retrieving "+requestDescription)
Expand Down Expand Up @@ -274,22 +308,8 @@ func (h *PublicApiHandler) GetAggregatedMetricsHistories(w http.ResponseWriter,
return
}

pathFn := func() string {
r := routes.NewRouter()
router := r.CreateEventGeneratorRoutes()
if router == nil {
panic("Failed to create event generator routes")
}

route := router.Get(routes.GetAggregatedMetricHistoriesRouteName)
path, err := route.URLPath("appid", appId, "metrictype", metricType)
if err != nil {
logger.Error("Failed to create path", err)
panic(err)
}
return h.conf.EventGenerator.EventGeneratorUrl + path.RequestURI() + "?" + parameters.Encode()
}
proxyRequest(pathFn, h.eventGeneratorClient.Get, w, req.URL, parameters, "metrics history from eventgenerator", logger)
h.proxyRequest(logger, appId, metricType, w, req, parameters, "metrics history from eventgenerator")
//proxyRequest(pathFn, h.eventGeneratorClient, w, req.URL, parameters, "metrics history from eventgenerator", logger)
}

func (h *PublicApiHandler) GetApiInfo(w http.ResponseWriter, _ *http.Request, _ map[string]string) {
Expand Down
Loading

0 comments on commit 602f78c

Please sign in to comment.