diff --git a/handlers.go b/handlers.go index 605b7e993406267f02dc84cc21bea4cda509cb17..8dc8c0b8396104c367bf2610100ce6d8b59fcd35 100644 --- a/handlers.go +++ b/handlers.go @@ -87,7 +87,7 @@ func (r *KeycloakProxy) securityHandler() gin.HandlerFunc { } // -// entrypointHandler checks to see if the request requires authentication +// entryPointHandler checks to see if the request requires authentication // func (r *KeycloakProxy) entryPointHandler() gin.HandlerFunc { return func(cx *gin.Context) { diff --git a/oauth.yml b/oauth.yml new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/oauth_test.go b/oauth_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/server.go b/server.go index f0c96d265efae710f874f8b8f587593452e5fda4..f9af57b04395f334c26f9d41fcd5f9ec89182bac 100644 --- a/server.go +++ b/server.go @@ -16,18 +16,19 @@ limitations under the License. package main import ( + "crypto/tls" + "crypto/x509" "fmt" + "io/ioutil" "net/http" "net/url" "os" "sync" - "crypto/tls" - "crypto/x509" - log "github.com/Sirupsen/logrus" "github.com/gambol99/go-oidc/oidc" + + log "github.com/Sirupsen/logrus" "github.com/gin-gonic/gin" - "io/ioutil" ) // KeycloakProxy is the server component @@ -96,7 +97,6 @@ func newKeycloakProxy(cfg *Config) (*KeycloakProxy, error) { // step: initialize the gin router router := gin.New() service.router = router - // step: load the templates service.initializeTemplates() for _, resource := range cfg.Resources { @@ -106,25 +106,29 @@ func newKeycloakProxy(cfg *Config) (*KeycloakProxy, error) { log.Infof("the token must container the claim: %s, required: %s", name, value) } - router.Use(gin.Recovery()) + service.initializeRouter() + + return service, nil +} + +// initializeRouter sets up the gin routing +func (r KeycloakProxy) initializeRouter() { + r.router.Use(gin.Recovery()) // step: are we logging the traffic? - if cfg.LogRequests { - router.Use(service.loggingHandler()) + if r.config.LogRequests { + r.router.Use(r.loggingHandler()) } - // step: if gin release production if os.Getenv("GIN_MODE") == "release" { log.Infof("enabling the security handler for release mode") - router.Use(service.securityHandler()) + r.router.Use(r.securityHandler()) } // step: add the routing - router.GET(authorizationURL, service.oauthAuthorizationHandler) - router.GET(callbackURL, service.oauthCallbackHandler) - router.GET(healthURL, service.healthHandler) - router.Use(service.entryPointHandler(), service.authenticationHandler(), service.admissionHandler()) - - return service, nil + r.router.GET(authorizationURL, r.oauthAuthorizationHandler) + r.router.GET(callbackURL, r.oauthCallbackHandler) + r.router.GET(healthURL, r.healthHandler) + r.router.Use(r.entryPointHandler(), r.authenticationHandler(), r.admissionHandler()) } // initializeTemplates loads the custom template @@ -151,6 +155,8 @@ func (r *KeycloakProxy) Run() error { // step: are we doing mutual tls? if r.config.TLSCaCertificate != "" { + log.Infof("enabling mutual tls, reading in the ca: %s", r.config.TLSCaCertificate) + caCert, err := ioutil.ReadFile(r.config.TLSCaCertificate) if err != nil { return err diff --git a/server_test.go b/server_test.go index e921dadde4083ec0a5538666d91bb12d224b6cb0..6ce2a46a39f5ce0075c387ffba59b16fd4dbbb83 100644 --- a/server_test.go +++ b/server_test.go @@ -50,6 +50,7 @@ func newFakeKeycloakProxyWithResources(t *testing.T, resources []*Resource) *Key func newFakeKeycloakProxy(t *testing.T) *KeycloakProxy { log.SetOutput(ioutil.Discard) + kc := &KeycloakProxy{ config: &Config{ DiscoveryURL: "127.0.0.1:", @@ -96,6 +97,10 @@ func newFakeKeycloakProxy(t *testing.T) *KeycloakProxy { }, proxy: new(fakeReverseProxy), } + kc.router = gin.New() + gin.SetMode(gin.ReleaseMode) + // step: add the gin routing + kc.initializeRouter() return kc } diff --git a/util_test.go b/util_test.go index 30cbb3e8c4ed0ad2b2418ce56e1ec41855b19d31..41f3c1d9666b5d692cc851304e5ec92b46c9ff46 100644 --- a/util_test.go +++ b/util_test.go @@ -17,8 +17,11 @@ package main import ( "bytes" + "fmt" + "io/ioutil" "net/http" "net/url" + "os" "reflect" "testing" @@ -205,6 +208,21 @@ func TestValidateResources(t *testing.T) { } } +func TestFileExists(t *testing.T) { + if fileExists("no_such_file_exsit_32323232") { + t.Errorf("we should have received false") + } + tmpfile, err := ioutil.TempFile("/tmp", fmt.Sprintf("test_file_%d", os.Getpid())) + if err != nil { + t.Fatalf("failed to create the temporary file, %s", err) + } + defer os.Remove(tmpfile.Name()) + + if !fileExists(tmpfile.Name()) { + t.Errorf("we should have received a true") + } +} + func TestDecodeResource(t *testing.T) { testCases := []struct { Option string