Skip to content
Snippets Groups Projects
Commit aa7d6dd7 authored by Rohith's avatar Rohith
Browse files

- just a general cleanup of the code

parent 8cb64705
Branches
Tags
No related merge requests found
......@@ -8,14 +8,14 @@ stages:
tests:
stage: tests
image: golang:1.5.3
image: golang:1.6.0
script:
- make deps
- make test
build:
stage: build
image: golang:1.5.3
image: golang:1.6.0
script:
- make deps
- make static
......@@ -6,7 +6,7 @@
#
language: go
go:
- 1.5
- 1.5.3
- 1.6
install:
- go get github.com/tools/godep
......
FROM alpine:3.2
FROM alpine:3.3
MAINTAINER Rohith <gambol99@gmail.com>
RUN apk update && \
......
......@@ -40,17 +40,12 @@ func newDefaultConfig() *Config {
TagData: make(map[string]string, 0),
ClaimsMatch: make(map[string]string, 0),
Header: make(map[string]string, 0),
CORS: &CORS{
Origins: []string{},
Methods: []string{},
Headers: []string{},
},
CORS: &CORS{},
}
}
// isValid validates if the config is valid
func (r *Config) isValid() error {
// step: validate the configuration
if r.Upstream == "" {
return fmt.Errorf("you have not specified an upstream endpoint to proxy to")
}
......@@ -75,7 +70,6 @@ func (r *Config) isValid() error {
if r.TLSCaCertificate != "" && !fileExists(r.TLSCaCertificate) {
return fmt.Errorf("the tls ca certificate file %s does not exist", r.TLSCaCertificate)
}
// step: if the skip verification is off, we need the below
if !r.SkipTokenVerification {
if r.DiscoveryURL == "" {
......@@ -265,18 +259,15 @@ func readConfigFile(filename string, config *Config) error {
if err != nil {
return err
}
// step: attempt to un-marshal the data
if isJson := filepath.Ext(filename) == "json"; isJson {
switch ext := filepath.Ext(filename); ext {
case "json":
err = json.Unmarshal(content, config)
} else {
default:
err = yaml.Unmarshal(content, config)
}
if err != nil {
return err
}
return nil
return err
}
// getOptions returns the command line options
......@@ -320,7 +311,7 @@ func getOptions() []cli.Flag {
},
cli.StringFlag{
Name: "redirection-url",
Usage: "the redirection url, namely the site url, note: " + oauthURL + " will be added to it",
Usage: fmt.Sprintf("the redirection url, namely the site url, note: %s will be added to it", oauthURL),
},
cli.StringSliceFlag{
Name: "hostname",
......
......@@ -56,6 +56,7 @@ func (r *KeycloakProxy) loggingHandler() gin.HandlerFunc {
"client_ip": cx.ClientIP(),
"method": cx.Request.Method,
"status": cx.Writer.Status(),
"bytes": cx.Writer.Size(),
"path": cx.Request.URL.Path,
"latency": latency.String(),
}).Infof("[%d] |%s| |%10v| %-5s %s", cx.Writer.Status(), cx.ClientIP(), latency, cx.Request.Method, cx.Request.URL.Path)
......
......@@ -25,10 +25,7 @@ import (
)
func main() {
// step: the proxy configuration
config := newDefaultConfig()
// step: construct the application
kc := cli.NewApp()
kc.Name = prog
kc.Usage = description
......@@ -36,19 +33,17 @@ func main() {
kc.Author = author
kc.Email = email
kc.Flags = getOptions()
// the default actions
kc.Action = func(cx *cli.Context) {
// do we have a configuration file?
// step: do we have a configuration file?
if filename := cx.String("config"); filename != "" {
if err := readConfigFile(cx.String("config"), config); err != nil {
printUsage(err.Error())
}
}
// parse the command line options
// step: parse the command line options
if err := readOptions(cx, config); err != nil {
printUsage(err.Error())
}
// step: validate the configuration
if err := config.isValid(); err != nil {
printUsage(err.Error())
......@@ -68,7 +63,6 @@ func main() {
<-signalChannel
}
kc.Run(os.Args)
}
......
......@@ -34,7 +34,7 @@ func (r *Resource) isValid() error {
return fmt.Errorf("this is used by the oauth handlers")
}
// step: check we have a
// step: check we have a url
if r.URL == "" {
return fmt.Errorf("resource does not have url")
}
......@@ -59,23 +59,20 @@ func (r Resource) getRoles() string {
return strings.Join(r.Roles, ",")
}
// String returns a string representation of the resource
func (r Resource) String() string {
var roles string
var methods string
if r.WhiteListed {
return fmt.Sprintf("uri: %s, white-listed", r.URL)
}
if len(r.Roles) <= 0 {
roles = "authentication only"
} else {
methods = strings.Join(r.Roles, ",")
roles := "authentication only"
methods := "ANY"
if len(r.Roles) > 0 {
roles = strings.Join(r.Roles, ",")
}
if len(r.Methods) <= 0 {
methods = "ANY"
} else {
if len(r.Methods) > 0 {
methods = strings.Join(r.Methods, ",")
}
......
......@@ -213,7 +213,6 @@ func (r KeycloakProxy) accessForbidden(cx *gin.Context) {
}
cx.AbortWithStatus(http.StatusForbidden)
cx.Abort()
}
// redirectToAuthorization redirects the user to authorization handler
......@@ -266,7 +265,7 @@ func (r *KeycloakProxy) tryUpdateConnection(cx *gin.Context) error {
// step: we need to hijack the underlining client connection
clientConn, _, err := cx.Writer.(http.Hijacker).Hijack()
if err != nil {
return err
}
defer clientConn.Close()
......
......@@ -97,7 +97,7 @@ func initializeOpenID(discoveryURL, clientID, clientSecret, redirectURL string,
// step: attempt to retrieve the provider configuration
gotConfig := false
for i := 0; i < 3; i++ {
log.Infof("attempting to retreieve the openid configuration from the discovery url: %s", discoveryURL)
log.Infof("attempting to retrieve the openid configuration from the discovery url: %s", discoveryURL)
providerConfig, err = oidc.FetchProviderConfig(http.DefaultClient, discoveryURL)
if err == nil {
gotConfig = true
......@@ -163,28 +163,14 @@ func decodeKeyPairs(list []string) (map[string]string, error) {
// tryDialEndpoint dials the upstream endpoint via plain
func tryDialEndpoint(location *url.URL) (net.Conn, error) {
// get the dial address
dialAddr := dialAddress(location)
switch location.Scheme {
switch dialAddress := dialAddress(location); location.Scheme {
case "http":
conn, err := net.Dial("tcp", dialAddr)
if err != nil {
return nil, err
}
return conn, nil
return net.Dial("tcp", dialAddress)
default:
// step: construct and dial a tls endpoint
conn, err := tls.Dial("tcp", dialAddr, &tls.Config{
return tls.Dial("tcp", dialAddress, &tls.Config{
Rand: rand.Reader,
InsecureSkipVerify: true,
})
if err != nil {
return nil, err
}
return conn, nil
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment