diff --git a/pkg/cfg/cfg.go b/pkg/cfg/cfg.go index 251ec7f0..b8177053 100644 --- a/pkg/cfg/cfg.go +++ b/pkg/cfg/cfg.go @@ -213,6 +213,16 @@ func Get(key string) string { return viper.GetString(key) } +// Get int value for key +func GetInt(key string) int { + return viper.GetInt(key) +} + +// Get bool value for key +func GetBool(key string) bool { + return viper.GetBool(key) +} + // BasicTest just a quick sanity check to see if the config is sound func BasicTest() error { for _, opt := range RequiredOptions { diff --git a/pkg/cookie/cookie.go b/pkg/cookie/cookie.go index f6a140b3..2a48a5ca 100644 --- a/pkg/cookie/cookie.go +++ b/pkg/cookie/cookie.go @@ -10,7 +10,8 @@ import ( "github.com/vouch/vouch-proxy/pkg/domains" ) -var defaultMaxAge = cfg.Cfg.JWT.MaxAge * 60 +var defaultMaxAge = cfg.GetInt("JWT.MaxAge") * 60 + // SetCookie http func SetCookie(w http.ResponseWriter, r *http.Request, val string) { @@ -24,25 +25,25 @@ func setCookie(w http.ResponseWriter, r *http.Request, val string, maxAge int) { } domain := domains.Matches(r.Host) // Allow overriding the cookie domain in the config file - if cfg.Cfg.Cookie.Domain != "" { - domain = cfg.Cfg.Cookie.Domain + if cfg.Get("Cookie.Domain") != "" { + domain = cfg.Get("Cookie.Domain") log.Debugf("setting the cookie domain to %v", domain) } // log.Debugf("cookie %s expires %d", cfg.Cfg.Cookie.Name, expires) http.SetCookie(w, &http.Cookie{ - Name: cfg.Cfg.Cookie.Name, + Name: cfg.Get("Cookie.Name"), Value: val, Path: "/", Domain: domain, MaxAge: maxAge, - Secure: cfg.Cfg.Cookie.Secure, - HttpOnly: cfg.Cfg.Cookie.HTTPOnly, + Secure: cfg.GetBool("Cookie.Secure"), + HttpOnly: cfg.GetBool("Cookie.HTTPOnly"), }) } // Cookie get the vouch jwt cookie func Cookie(r *http.Request) (string, error) { - cookie, err := r.Cookie(cfg.Cfg.Cookie.Name) + cookie, err := r.Cookie(cfg.Get("Cookie.Name")) if err != nil { return "", err } @@ -51,7 +52,7 @@ func Cookie(r *http.Request) (string, error) { } log.WithFields(log.Fields{ - "cookieName": cfg.Cfg.Cookie.Name, + "cookieName": cfg.Get("Cookie.Name"), "cookieValue": cookie.Value, }).Debug("cookie") return cookie.Value, err