3
0

upd(auth): update to use global config pkg

This commit is contained in:
Tit Petric
2018-09-11 16:37:38 +02:00
parent 7c968b1ae2
commit 5372966ed0
7 changed files with 45 additions and 146 deletions
+22 -20
View File
@@ -2,56 +2,58 @@ package auth
import (
"github.com/pkg/errors"
"github.com/crusttech/crust/config"
)
type (
configuration struct {
http *httpFlags
db *dbFlags
jwt *jwtFlags
appFlags struct {
http *config.HTTP
db *config.Database
jwt *config.JWT
}
)
var config *configuration
var flags *appFlags
func (c *configuration) validate() error {
func (c *appFlags) Validate() error {
if c == nil {
return errors.New("CRM config is not initialized, need to call Flags() or FullFlags()")
return errors.New("AUTH flags are not initialized, need to call Flags() or FullFlags()")
}
if err := c.http.validate(); err != nil {
if err := c.http.Validate(); err != nil {
return err
}
if err := c.db.validate(); err != nil {
if err := c.db.Validate(); err != nil {
return err
}
if err := c.jwt.validate(); err != nil {
if err := c.jwt.Validate(); err != nil {
return err
}
return nil
}
func Flags(prefix ...string) {
if config != nil {
if flags != nil {
return
}
if len(prefix) == 0 {
panic("crm.Flags() needs prefix on first call")
panic("auth.Flags() needs prefix on first call")
}
config = &configuration{
jwt: new(jwtFlags).flags(prefix...),
flags = &appFlags{
jwt: new(config.JWT).Init(prefix...),
}
}
func FullFlags(prefix ...string) {
if config != nil {
if flags != nil {
return
}
if len(prefix) == 0 {
panic("crm.Flags() needs prefix on first call")
panic("auth.Flags() needs prefix on first call")
}
config = &configuration{
new(httpFlags).flags(prefix...),
new(dbFlags).flags(prefix...),
new(jwtFlags).flags(prefix...),
flags = &appFlags{
new(config.HTTP).Init(prefix...),
new(config.Database).Init(prefix...),
new(config.JWT).Init(prefix...),
}
}
-34
View File
@@ -1,34 +0,0 @@
package auth
import (
"github.com/namsral/flag"
"github.com/pkg/errors"
)
type (
dbFlags struct {
dsn string
profiler string
}
)
func (c *dbFlags) validate() error {
if c == nil {
return nil
}
if c.dsn == "" {
return errors.New("No DB DSN is set, can't connect to database")
}
return nil
}
func (c *dbFlags) flags(prefix ...string) *dbFlags {
p := func(s string) string {
return prefix[0] + "-" + s
}
flag.StringVar(&c.dsn, p("db-dsn"), "crust:crust@tcp(db1:3306)/crust?collation=utf8mb4_general_ci", "DSN for database connection")
flag.StringVar(&c.profiler, p("db-profiler"), "", "Profiler for DB queries (none, stdout)")
return c
}
-40
View File
@@ -1,40 +0,0 @@
package auth
import (
"github.com/namsral/flag"
"github.com/pkg/errors"
)
type (
httpFlags struct {
addr string
logging bool
pretty bool
tracing bool
metrics bool
}
)
func (c *httpFlags) validate() error {
if c == nil {
return nil
}
if c.addr == "" {
return errors.New("No HTTP Addr is set, can't listen for HTTP")
}
return nil
}
func (c *httpFlags) flags(prefix ...string) *httpFlags {
p := func(s string) string {
return prefix[0] + "-" + s
}
flag.StringVar(&c.addr, p("http-addr"), ":3000", "Listen address for HTTP server")
flag.BoolVar(&c.logging, p("http-log"), true, "Enable/disable HTTP request log")
flag.BoolVar(&c.pretty, p("http-pretty-json"), false, "Prettify returned JSON output")
flag.BoolVar(&c.tracing, p("http-error-tracing"), false, "Return error stack frame")
flag.BoolVar(&c.metrics, p("http-metrics"), false, "Provide metrics export for prometheus")
return c
}
-31
View File
@@ -1,31 +0,0 @@
package auth
import (
"github.com/namsral/flag"
"github.com/pkg/errors"
)
type (
jwtFlags struct {
secret string
expiry int64
debugToken bool
}
)
func (c *jwtFlags) validate() error {
if c == nil {
return nil
}
if c.secret == "" {
return errors.New("JWT Secret not set for AUTH")
}
return nil
}
func (c *jwtFlags) flags(prefix ...string) *jwtFlags {
flag.StringVar(&c.secret, "auth-jwt-secret", "", "JWT Secret")
flag.Int64Var(&c.expiry, "auth-jwt-expiry", 3600, "JWT Expiration in minutes")
flag.BoolVar(&c.debugToken, "auth-jwt-debug", false, "Generate debug JWT key")
return c
}
+4 -4
View File
@@ -16,13 +16,13 @@ type jwt struct {
}
func JWT() (*jwt, error) {
if err := config.validate(); err != nil {
if err := flags.Validate(); err != nil {
return nil, err
}
jwt := &jwt{tokenAuth: jwtauth.New("HS256", []byte(config.jwt.secret), nil)}
jwt := &jwt{tokenAuth: jwtauth.New("HS256", []byte(flags.jwt.Secret), nil)}
if config.jwt.debugToken {
if flags.jwt.DebugToken {
log.Println("DEBUG JWT TOKEN:", jwt.Encode(NewIdentity(1)))
}
@@ -38,7 +38,7 @@ func (t *jwt) Encode(identity types.Identifiable) string {
// @todo Set expiry
claims := jwtauth.Claims{}
claims.Set("sub", strconv.FormatUint(identity.Identity(), 10))
claims.SetExpiryIn(time.Duration(config.jwt.expiry) * time.Minute)
claims.SetExpiryIn(time.Duration(flags.jwt.Expiry) * time.Minute)
_, jwt, _ := t.tokenAuth.Encode(claims)
return jwt
+8 -6
View File
@@ -7,13 +7,15 @@ import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/crusttech/crust/config"
)
func mountRoutes(r chi.Router, opts *configuration, mounts ...func(r chi.Router)) {
if opts.http.logging {
func mountRoutes(r chi.Router, opts *config.HTTP, mounts ...func(r chi.Router)) {
if opts.Logging {
r.Use(middleware.Logger)
}
if opts.http.metrics {
if opts.Metrics {
r.Use(metrics{}.Middleware("crm"))
}
@@ -22,14 +24,14 @@ func mountRoutes(r chi.Router, opts *configuration, mounts ...func(r chi.Router)
}
}
func mountSystemRoutes(r chi.Router, opts *configuration) {
if opts.http.metrics {
func mountSystemRoutes(r chi.Router, opts *config.HTTP) {
if opts.Metrics {
r.Handle("/metrics", metrics{}.Handler())
}
r.Mount("/debug", middleware.Profiler())
}
func printRoutes(r chi.Router, opts *configuration) {
func printRoutes(r chi.Router, opts *config.HTTP) {
var printRoutes func(chi.Routes, string, string)
printRoutes = func(r chi.Routes, indent string, prefix string) {
routes := r.Routes()
+11 -11
View File
@@ -20,18 +20,18 @@ import (
func Init() error {
// validate configuration
if err := config.validate(); err != nil {
if err := flags.Validate(); err != nil {
return err
}
// start/configure database connection
factory.Database.Add("default", config.db.dsn)
factory.Database.Add("default", flags.db.DSN)
db, err := factory.Database.Get()
if err != nil {
return err
}
// @todo: profiling as an external service?
switch config.db.profiler {
switch flags.db.Profiler {
case "stdout":
db.Profiler = &factory.Database.ProfilerStdout
default:
@@ -40,8 +40,8 @@ func Init() error {
// configure resputil options
resputil.SetConfig(resputil.Options{
Pretty: config.http.pretty,
Trace: config.http.tracing,
Pretty: flags.http.Pretty,
Trace: flags.http.Tracing,
Logger: func(err error) {
// @todo: error logging
},
@@ -53,10 +53,10 @@ func Init() error {
func Start() error {
var ctx = sigctx.New()
log.Println("Starting http server on address " + config.http.addr)
listener, err := net.Listen("tcp", config.http.addr)
log.Println("Starting http server on address " + flags.http.Addr)
listener, err := net.Listen("tcp", flags.http.Addr)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Can't listen on addr %s", config.http.addr))
return errors.Wrap(err, fmt.Sprintf("Can't listen on addr %s", flags.http.Addr))
}
// JWT Auth
@@ -71,11 +71,11 @@ func Start() error {
// Only protect application routes with JWT
r.Group(func(r chi.Router) {
r.Use(jwtAuth.Verifier(), jwtAuth.Authenticator())
mountRoutes(r, config, rest.MountRoutes(jwtAuth))
mountRoutes(r, flags.http, rest.MountRoutes(jwtAuth))
})
printRoutes(r, config)
mountSystemRoutes(r, config)
printRoutes(r, flags.http)
mountSystemRoutes(r, flags.http)
go http.Serve(listener, r)
<-ctx.Done()