Skip to content

Commit

Permalink
wip fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
bonzofenix committed Dec 3, 2024
1 parent 59e25f4 commit 4693ad4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/golangapiserver/spec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ files:
- autoscaler/db/sqldb/* # gosub
- autoscaler/healthendpoint/* # gosub
- autoscaler/helpers/* # gosub
- autoscaler/helpers/auth/* # gosub
- autoscaler/helpers/handlers/* # gosub
- autoscaler/metricsforwarder/server/common/* # gosub
- autoscaler/models/* # gosub
Expand Down
15 changes: 7 additions & 8 deletions src/autoscaler/helpers/auth/xfcc_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ func (m *xfccAuthMiddleware) checkAuth(r *http.Request) error {
return ErrXFCCHeaderNotFound
}

data, err := base64.StdEncoding.DecodeString(removeQuotes(xfccHeader))
attrs := make(map[string]string)
for _, v := range strings.Split(xfccHeader, ";") {
attr := strings.Split(v, "=")
attrs[attr[0]] = attr[1]
}

data, err := base64.StdEncoding.DecodeString(attrs["Cert"])
if err != nil {
return fmt.Errorf("base64 parsing failed: %w", err)
}
Expand Down Expand Up @@ -136,10 +142,3 @@ func getOrgGuid(cert *x509.Certificate) string {
}
return certOrgGuid
}

func removeQuotes(xfccHeader string) string {
if xfccHeader[0] == '"' {
xfccHeader = xfccHeader[1 : len(xfccHeader)-1]
}
return xfccHeader
}
21 changes: 8 additions & 13 deletions src/autoscaler/helpers/auth/xfcc_auth_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package auth_test

import (
"encoding/base64"
"encoding/pem"
"net/http"
"net/http/httptest"

Expand Down Expand Up @@ -36,31 +34,28 @@ var _ = Describe("XfccAuthMiddleware", func() {

AfterEach(func() {
server.Close()

})

JustBeforeEach(func() {
logger := lagertest.NewTestLogger("xfcc-auth-test")
buffer = logger.Buffer()
xfccAuth := models.XFCCAuth{
xm := auth.NewXfccAuthMiddleware(logger, models.XFCCAuth{
ValidOrgGuid: orgGuid,
ValidSpaceGuid: spaceGuid,
}
xm := auth.NewXfccAuthMiddleware(logger, xfccAuth)
})

server = httptest.NewServer(xm.XFCCAuthenticationMiddleware(handler))
server = httptest.NewUnstartedServer(xm.XFCCAuthenticationMiddleware(handler))

req, err := http.NewRequest("GET", server.URL+"/some-protected-endpoint", nil)
Expect(err).NotTo(HaveOccurred())

if len(xfccClientCert) > 0 {
block, _ := pem.Decode(xfccClientCert)
Expect(err).NotTo(HaveOccurred())
Expect(block).ShouldNot(BeNil())

req.Header.Add("X-Forwarded-Client-Cert", base64.StdEncoding.EncodeToString(block.Bytes))
cert := auth.NewCert(string(xfccClientCert))
req.Header.Add("X-Forwarded-Client-Cert", cert.GetXFCCHeader())
}
Expect(err).NotTo(HaveOccurred())

resp, err = http.DefaultClient.Do(req)
resp, err = server.Client().Do(req)
Expect(err).NotTo(HaveOccurred())
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
"github.com/onsi/gomega/gbytes"

"bytes"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -280,11 +278,3 @@ var _ = Describe("Main", func() {
})
})
})

func setXFCCCertHeader(req *http.Request, orgGuid, spaceGuid string) {
xfccClientCert, err := GenerateClientCert(orgGuid, spaceGuid)
block, _ := pem.Decode(xfccClientCert)
Expect(err).NotTo(HaveOccurred())
Expect(block).ShouldNot(BeNil())
req.Header.Add("X-Forwarded-Client-Cert", base64.StdEncoding.EncodeToString(block.Bytes))
}

0 comments on commit 4693ad4

Please sign in to comment.