diff --git a/main.go b/main.go index 89ba852..ad393db 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,12 @@ import ( "runtime" "time" + "amuz.es/go/mnemonics/middleware/logging" + "amuz.es/go/mnemonics/middleware/secure" + "amuz.es/go/mnemonics/middleware/session" + "amuz.es/go/mnemonics/middleware/static" + "amuz.es/go/mnemonics/middleware/template" + "amuz.es/go/mnemonics/route" "amuz.es/go/mnemonics/util" "github.com/gin-gonic/gin" @@ -34,16 +40,19 @@ func main() { flag.Parse() setMaxProcs() + store := session.NewCookieStore([]byte(util.GetRandomString(128))) //gin.SetMode(gin.ReleaseMode) r := gin.New() // middleware settings - r.Use(util.Ginrus(time.RFC3339, true)) + + r.Use(logging.Ginrus(time.RFC3339, true)) + r.Use(gin.Recovery()) // not found handler r.NoRoute(func(c *gin.Context) { code := http.StatusNotFound - c.HTML(code, "page_404.html", util.Context{ + c.HTML(code, "page_404.html", template.Context{ "statusCode": code, "statusMessage": http.StatusText(code), "path": c.Request.URL.Path, @@ -54,7 +63,24 @@ func main() { path := r.Group("/") // handlers - path.GET("/api", route.Api) + api := path.Group("/api") + + api.Use(secure.Secure(secure.Options{ + AllowedHosts: []string{}, + SSLRedirect: false, + SSLHost: "", + // SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"}, + SSLProxyHeaders: map[string]string{}, + STSSeconds: 315360000, + STSIncludeSubdomains: true, + FrameDeny: true, + ContentTypeNosniff: true, + BrowserXssFilter: true, + ContentSecurityPolicy: "default-src 'self'", + })) + api.Use(session.Sessions("mysession", store)) + api.GET("", route.Me) + path.GET("/self", route.Self) path.GET("/metric", route.Metric) path.GET("/health", func(c *gin.Context) { @@ -78,12 +104,12 @@ func main() { if gin.IsDebugging() { // static route - path.StaticFS("/static", util.StaticDebug("asset/static")) - r.HTMLRender = util.TemplateSourceDebug("asset/template") + path.StaticFS("/static", static.NewDebug("asset/static")) + r.HTMLRender = template.NewDebug("asset/template") } else { // template engine setting - path.StaticFS("/static", util.StaticRelease("")) - r.HTMLRender = util.TemplateSourceRelease("") + path.StaticFS("/static", static.NewRelease("")) + r.HTMLRender = template.NewRelease("") } util.Log().Error("bootstrapped application") diff --git a/middleware/logging/middleware.go b/middleware/logging/middleware.go new file mode 100644 index 0000000..2fc1d67 --- /dev/null +++ b/middleware/logging/middleware.go @@ -0,0 +1,51 @@ +// middleware +package logging + +import ( + "time" + + "amuz.es/go/mnemonics/util" + + "github.com/Sirupsen/logrus" + "github.com/gin-gonic/gin" +) + +// Ginrus returns a gin.HandlerFunc (middleware) that logs requests using logrus. +// +// Requests with errors are logged using logrus.Error(). +// Requests without errors are logged using logrus.Info(). +// +// It receives: +// 1. A time package format string (e.g. time.RFC3339). +// 2. A boolean stating whether to use UTC time zone or local. +func Ginrus(timeFormat string, utc bool) gin.HandlerFunc { + return func(c *gin.Context) { + start := time.Now() + // some evil middlewares modify this values + path := c.Request.URL.Path + c.Next() + + end := time.Now() + latency := end.Sub(start) + if utc { + end = end.UTC() + } + + entry := util.Log().WithFields(logrus.Fields{ + "status": c.Writer.Status(), + "method": c.Request.Method, + "path": path, + "ip": c.ClientIP(), + "latency": latency, + "user-agent": c.Request.UserAgent(), + "time": end.Format(timeFormat), + }) + + if len(c.Errors) > 0 { + // Append error field if this is an erroneous request. + entry.Error(c.Errors.String()) + } else { + entry.Info() + } + } +} diff --git a/middleware/secure/middleware.go b/middleware/secure/middleware.go new file mode 100644 index 0000000..ac85c21 --- /dev/null +++ b/middleware/secure/middleware.go @@ -0,0 +1,179 @@ +package secure + +import ( + "fmt" + "net/http" + "strings" + + "github.com/gin-gonic/gin" +) + +const ( + stsHeader = "Strict-Transport-Security" + stsSubdomainString = "; includeSubdomains" + frameOptionsHeader = "X-Frame-Options" + frameOptionsValue = "DENY" + contentTypeHeader = "X-Content-Type-Options" + contentTypeValue = "nosniff" + xssProtectionHeader = "X-XSS-Protection" + xssProtectionValue = "1; mode=block" + cspHeader = "Content-Security-Policy" +) + +func defaultBadHostHandler(w http.ResponseWriter, r *http.Request) { + http.Error(w, "Bad Host", http.StatusInternalServerError) +} + +// Options is a struct for specifying configuration options for the secure.Secure middleware. +type Options struct { + // AllowedHosts is a list of fully qualified domain names that are allowed. Default is empty list, which allows any and all host names. + AllowedHosts []string + // If SSLRedirect is set to true, then only allow https requests. Default is false. + SSLRedirect bool + // If SSLTemporaryRedirect is true, the a 302 will be used while redirecting. Default is false (301). + SSLTemporaryRedirect bool + // SSLHost is the host name that is used to redirect http requests to https. Default is "", which indicates to use the same host. + SSLHost string + // SSLProxyHeaders is set of header keys with associated values that would indicate a valid https request. Useful when using Nginx: `map[string]string{"X-Forwarded-Proto": "https"}`. Default is blank map. + SSLProxyHeaders map[string]string + // STSSeconds is the max-age of the Strict-Transport-Security header. Default is 0, which would NOT include the header. + STSSeconds int64 + // If STSIncludeSubdomains is set to true, the `includeSubdomains` will be appended to the Strict-Transport-Security header. Default is false. + STSIncludeSubdomains bool + // If FrameDeny is set to true, adds the X-Frame-Options header with the value of `DENY`. Default is false. + FrameDeny bool + // CustomFrameOptionsValue allows the X-Frame-Options header value to be set with a custom value. This overrides the FrameDeny option. + CustomFrameOptionsValue string + // If ContentTypeNosniff is true, adds the X-Content-Type-Options header with the value `nosniff`. Default is false. + ContentTypeNosniff bool + // If BrowserXssFilter is true, adds the X-XSS-Protection header with the value `1; mode=block`. Default is false. + BrowserXssFilter bool + // ContentSecurityPolicy allows the Content-Security-Policy header value to be set with a custom value. Default is "". + ContentSecurityPolicy string + // When developing, the AllowedHosts, SSL, and STS options can cause some unwanted effects. Usually testing happens on http, not https, and on localhost, not your production domain... so set this to true for dev environment. + // If you would like your development environment to mimic production with complete Host blocking, SSL redirects, and STS headers, leave this as false. Default if false. + IsDevelopment bool + + // Handlers for when an error occurs (ie bad host). + BadHostHandler http.Handler +} + +// Secure is a middleware that helps setup a few basic security features. A single secure.Options struct can be +// provided to configure which features should be enabled, and the ability to override a few of the default values. +type secure struct { + // Customize Secure with an Options struct. + opt Options +} + +// Constructs a new Secure instance with supplied options. +func New(options Options) *secure { + if options.BadHostHandler == nil { + options.BadHostHandler = http.HandlerFunc(defaultBadHostHandler) + } + + return &secure{ + opt: options, + } +} + +func (s *secure) process(w http.ResponseWriter, r *http.Request) error { + // Allowed hosts check. + if len(s.opt.AllowedHosts) > 0 && !s.opt.IsDevelopment { + isGoodHost := false + for _, allowedHost := range s.opt.AllowedHosts { + if strings.EqualFold(allowedHost, r.Host) { + isGoodHost = true + break + } + } + + if !isGoodHost { + s.opt.BadHostHandler.ServeHTTP(w, r) + return fmt.Errorf("Bad host name: %s", r.Host) + } + } + + // SSL check. + if s.opt.SSLRedirect && s.opt.IsDevelopment == false { + isSSL := false + if strings.EqualFold(r.URL.Scheme, "https") || r.TLS != nil { + isSSL = true + } else { + for k, v := range s.opt.SSLProxyHeaders { + if r.Header.Get(k) == v { + isSSL = true + break + } + } + } + + if isSSL == false { + url := r.URL + url.Scheme = "https" + url.Host = r.Host + + if len(s.opt.SSLHost) > 0 { + url.Host = s.opt.SSLHost + } + + status := http.StatusMovedPermanently + if s.opt.SSLTemporaryRedirect { + status = http.StatusTemporaryRedirect + } + + http.Redirect(w, r, url.String(), status) + return fmt.Errorf("Redirecting to HTTPS") + } + } + + // Strict Transport Security header. + if s.opt.STSSeconds != 0 && !s.opt.IsDevelopment { + stsSub := "" + if s.opt.STSIncludeSubdomains { + stsSub = stsSubdomainString + } + + w.Header().Add(stsHeader, fmt.Sprintf("max-age=%d%s", s.opt.STSSeconds, stsSub)) + } + + // Frame Options header. + if len(s.opt.CustomFrameOptionsValue) > 0 { + w.Header().Add(frameOptionsHeader, s.opt.CustomFrameOptionsValue) + } else if s.opt.FrameDeny { + w.Header().Add(frameOptionsHeader, frameOptionsValue) + } + + // Content Type Options header. + if s.opt.ContentTypeNosniff { + w.Header().Add(contentTypeHeader, contentTypeValue) + } + + // XSS Protection header. + if s.opt.BrowserXssFilter { + w.Header().Add(xssProtectionHeader, xssProtectionValue) + } + + // Content Security Policy header. + if len(s.opt.ContentSecurityPolicy) > 0 { + w.Header().Add(cspHeader, s.opt.ContentSecurityPolicy) + } + + return nil + +} + +func Secure(options Options) gin.HandlerFunc { + s := New(options) + + return func(c *gin.Context) { + err := s.process(c.Writer, c.Request) + if err != nil { + if c.Writer.Written() { + c.AbortWithStatus(c.Writer.Status()) + } else { + c.AbortWithError(http.StatusInternalServerError, err) + } + } + } + +} diff --git a/middleware/session/cookie.go b/middleware/session/cookie.go new file mode 100644 index 0000000..205edc6 --- /dev/null +++ b/middleware/session/cookie.go @@ -0,0 +1,36 @@ +package session + +import ( + "github.com/gorilla/sessions" +) + +type CookieStore interface { + Store +} + +// Keys are defined in pairs to allow key rotation, but the common case is to set a single +// authentication key and optionally an encryption key. +// +// The first key in a pair is used for authentication and the second for encryption. The +// encryption key can be set to nil or omitted in the last pair, but the authentication key +// is required in all pairs. +// +// It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, +// if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes. +func NewCookieStore(keyPairs ...[]byte) CookieStore { + return &cookieStore{sessions.NewCookieStore(keyPairs...)} +} + +type cookieStore struct { + *sessions.CookieStore +} + +func (c *cookieStore) Options(options Options) { + c.CookieStore.Options = &sessions.Options{ + Path: options.Path, + Domain: options.Domain, + MaxAge: options.MaxAge, + Secure: options.Secure, + HttpOnly: options.HttpOnly, + } +} diff --git a/middleware/session/redis.go b/middleware/session/redis.go new file mode 100644 index 0000000..7ab9e5d --- /dev/null +++ b/middleware/session/redis.go @@ -0,0 +1,45 @@ +package session + +import ( + "github.com/boj/redistore" + "github.com/gorilla/sessions" +) + +type RedisStore interface { + Store +} + +// size: maximum number of idle connections. +// network: tcp or udp +// address: host:port +// password: redis-password +// Keys are defined in pairs to allow key rotation, but the common case is to set a single +// authentication key and optionally an encryption key. +// +// The first key in a pair is used for authentication and the second for encryption. The +// encryption key can be set to nil or omitted in the last pair, but the authentication key +// is required in all pairs. +// +// It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, +// if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes. +func NewRedisStore(size int, network, address, password string, keyPairs ...[]byte) (RedisStore, error) { + store, err := redistore.NewRediStore(size, network, address, password, keyPairs...) + if err != nil { + return nil, err + } + return &redisStore{store}, nil +} + +type redisStore struct { + *redistore.RediStore +} + +func (c *redisStore) Options(options Options) { + c.RediStore.Options = &sessions.Options{ + Path: options.Path, + Domain: options.Domain, + MaxAge: options.MaxAge, + Secure: options.Secure, + HttpOnly: options.HttpOnly, + } +} diff --git a/middleware/session/sessions.go b/middleware/session/sessions.go new file mode 100644 index 0000000..1e50d4e --- /dev/null +++ b/middleware/session/sessions.go @@ -0,0 +1,147 @@ +package session + +import ( + "log" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/gorilla/context" + "github.com/gorilla/sessions" +) + +const ( + DefaultKey = "amuz.es/go/mnemonics" + errorFormat = "[sessions] ERROR! %s\n" +) + +type Store interface { + sessions.Store + Options(Options) +} + +// Options stores configuration for a session or session store. +// Fields are a subset of http.Cookie fields. +type Options struct { + Path string + Domain string + // MaxAge=0 means no 'Max-Age' attribute specified. + // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'. + // MaxAge>0 means Max-Age attribute present and given in seconds. + MaxAge int + Secure bool + HttpOnly bool +} + +// Wraps thinly gorilla-session methods. +// Session stores the values and optional configuration for a session. +type Session interface { + // Get returns the session value associated to the given key. + Get(key interface{}) interface{} + // Set sets the session value associated to the given key. + Set(key interface{}, val interface{}) + // Delete removes the session value associated to the given key. + Delete(key interface{}) + // Clear deletes all values in the session. + Clear() + // AddFlash adds a flash message to the session. + // A single variadic argument is accepted, and it is optional: it defines the flash key. + // If not defined "_flash" is used by default. + AddFlash(value interface{}, vars ...string) + // Flashes returns a slice of flash messages from the session. + // A single variadic argument is accepted, and it is optional: it defines the flash key. + // If not defined "_flash" is used by default. + Flashes(vars ...string) []interface{} + // Options sets confuguration for a session. + Options(Options) + // Save saves all sessions used during the current request. + Save() error +} + +func Sessions(name string, store Store) gin.HandlerFunc { + return func(c *gin.Context) { + s := &session{name, c.Request, store, nil, false, c.Writer} + c.Set(DefaultKey, s) + defer context.Clear(c.Request) + c.Next() + } +} + +type session struct { + name string + request *http.Request + store Store + session *sessions.Session + written bool + writer http.ResponseWriter +} + +func (s *session) Get(key interface{}) interface{} { + return s.Session().Values[key] +} + +func (s *session) Set(key interface{}, val interface{}) { + s.Session().Values[key] = val + s.written = true +} + +func (s *session) Delete(key interface{}) { + delete(s.Session().Values, key) + s.written = true +} + +func (s *session) Clear() { + for key := range s.Session().Values { + s.Delete(key) + } +} + +func (s *session) AddFlash(value interface{}, vars ...string) { + s.Session().AddFlash(value, vars...) + s.written = true +} + +func (s *session) Flashes(vars ...string) []interface{} { + s.written = true + return s.Session().Flashes(vars...) +} + +func (s *session) Options(options Options) { + s.Session().Options = &sessions.Options{ + Path: options.Path, + Domain: options.Domain, + MaxAge: options.MaxAge, + Secure: options.Secure, + HttpOnly: options.HttpOnly, + } +} + +func (s *session) Save() error { + if s.Written() { + e := s.Session().Save(s.request, s.writer) + if e == nil { + s.written = false + } + return e + } + return nil +} + +func (s *session) Session() *sessions.Session { + if s.session == nil { + var err error + s.session, err = s.store.Get(s.request, s.name) + if err != nil { + log.Printf(errorFormat, err) + } + } + return s.session +} + +func (s *session) Written() bool { + return s.written +} + +// shortcut to get session +func Default(c *gin.Context) Session { + return c.MustGet(DefaultKey).(Session) +} diff --git a/util/static.go b/middleware/static/middleware.go similarity index 86% rename from util/static.go rename to middleware/static/middleware.go index b77e1d1..cea6d62 100644 --- a/util/static.go +++ b/middleware/static/middleware.go @@ -1,4 +1,4 @@ -package util +package static import ( "net/http" @@ -26,13 +26,13 @@ func (b *binaryFileSystem) Exists(prefix string, filepath string) bool { return false } -func StaticRelease(root string) http.FileSystem { +func NewRelease(root string) http.FileSystem { fs := &assetfs.AssetFS{Asset: static.Asset, AssetDir: static.AssetDir, AssetInfo: static.AssetInfo, Prefix: root} return &binaryFileSystem{ fs, } } -func StaticDebug(root string) http.FileSystem { +func NewDebug(root string) http.FileSystem { return http.Dir("asset/static") } diff --git a/util/renderer.go b/middleware/template/middleware.go similarity index 93% rename from util/renderer.go rename to middleware/template/middleware.go index 798b255..ba0d46f 100644 --- a/util/renderer.go +++ b/middleware/template/middleware.go @@ -1,4 +1,4 @@ -package util +package template import ( "net/http" @@ -32,7 +32,7 @@ type ( Context pongo2.Context ) -func TemplateSourceRelease(path string) *PongoRelease { +func NewRelease(path string) *PongoRelease { ts := pongo2.NewSet(path, &memoryTemplateLoader{loaderFunc: template.Asset}) return &PongoRelease{ts, path} } @@ -46,7 +46,7 @@ func (p PongoRelease) Instance(name string, data interface{}) render.Render { Data: data, } } -func TemplateSourceDebug(path string) *PongoDebug { +func NewDebug(path string) *PongoDebug { return &PongoDebug{path} } diff --git a/route/api.go b/route/api.go index 37cfe66..06b8354 100644 --- a/route/api.go +++ b/route/api.go @@ -1,18 +1,35 @@ package route import ( + "amuz.es/go/mnemonics/middleware/session" "amuz.es/go/mnemonics/util" "github.com/gin-gonic/gin" + //https://github.com/boombuler/barcode ) -func Api(c *gin.Context) { +func Me(c *gin.Context) { content := gin.H{"Hello": "World"} + sess := session.Default(c) + var count int + v := sess.Get("count") + if v == nil { + count = 0 + } else { + count = v.(int) + count += 1 + } + sess.Set("count", count) + sess.Save() + + // util.Log().Errorf(" sess : %d", count) + conn := util.NewAccoauntSource() conn.Connect() defer conn.Close() + + conn.Bind("test", "testpw") conn.Search() - util.Log().Error("hello") c.JSON(200, content) } diff --git a/route/auth.go b/route/auth.go index 4361ec3..474bfd5 100644 --- a/route/auth.go +++ b/route/auth.go @@ -5,9 +5,9 @@ import ( "net/http" - "amuz.es/go/mnemonics/util" + "amuz.es/go/mnemonics/middleware/template" ) func Login(c *gin.Context) { - c.HTML(http.StatusOK, "login.html", util.Context{}) + c.HTML(http.StatusOK, "login.html", template.Context{}) } diff --git a/route/index.go b/route/index.go index 9670172..c977a16 100644 --- a/route/index.go +++ b/route/index.go @@ -5,9 +5,9 @@ import ( "net/http" - "amuz.es/go/mnemonics/util" + "amuz.es/go/mnemonics/middleware/template" ) func Index(c *gin.Context) { - c.HTML(http.StatusOK, "index.html", util.Context{}) + c.HTML(http.StatusOK, "index.html", template.Context{}) } diff --git a/util/ldap.go b/util/ldap.go index b8464f3..b79fec7 100644 --- a/util/ldap.go +++ b/util/ldap.go @@ -2,20 +2,45 @@ package util import ( + "bytes" "crypto/rand" "crypto/sha1" "encoding/base64" + "encoding/binary" + "encoding/hex" "fmt" "strings" + "unicode/utf16" + + "golang.org/x/crypto/md4" "github.com/nmcclain/ldap" ) type pwdStor string +func (p pwdStor) GetPlainPassword() string { + return strings.TrimSpace(string(p)) +} + +func (p pwdStor) GetNtPassword() string { + source := strings.TrimSpace(string(p)) + + utf16Encoded := utf16.Encode([]rune(source)) + + buf := new(bytes.Buffer) + for _, utf16Char := range utf16Encoded { + binary.Write(buf, binary.LittleEndian, utf16Char) + } + + hasher := md4.New() + hasher.Write(buf.Bytes()) + return hex.EncodeToString(hasher.Sum(nil)) + +} + func (p pwdStor) GetSaltedSha1() string { source := strings.TrimSpace(string(p)) - Log().Errorf("source %s", source) var err error salt := make([]byte, 4) @@ -43,9 +68,7 @@ func (p pwdStor) GetSaltedSha1() string { encoded := base64.StdEncoding.EncodeToString(append(hasher.Sum(nil), salt...)) tag := "{SSHA}" - formatted := tag + encoded - Log().Errorf("sha1 %s", formatted) - return formatted + return tag + encoded } type ldapUnit struct { @@ -71,7 +94,7 @@ type AccountSource interface { //ldapSourceBool func (l *ldapSource) Connect() { - l.Bind("test", "testpw") + addr := fmt.Sprintf("%s:%d", l.host, l.port) conn, err := ldap.DialTLS("tcp", addr, nil) if err == nil { @@ -107,7 +130,7 @@ func (l ldapSource) Search() { for _, attr := range v.Attributes { m[attr.Name] = append(m[attr.Name], attr.Values...) } - Log().Error(m) + // Log().Error(m) } } else { Log().Error(err.Error()) @@ -116,14 +139,20 @@ func (l ldapSource) Search() { } func (l ldapSource) Bind(uid string, password pwdStor) { - password.GetSaltedSha1() + dn := fmt.Sprintf("%s=%s,%s", l.user.uniqueAttributeName, uid, l.user.dn) + err := l.connection.Bind(dn, password.GetPlainPassword()).(*ldap.Error) + if err != nil { + Log().Errorf("dddd %d", err.ResultCode) + panic(err) + } } func NewAccoauntSource() AccountSource { user := ldapUnit{ dn: "ou=User,dc=amuz,dc=es", filter: "(objectClass=person)", - uniqueAttributeName: "uid"} + uniqueAttributeName: "uid", + } group := ldapUnit{ dn: "ou=Group,dc=amuz,dc=es", filter: "|((objectClass=groupOfNames)(objectClass=groupOfUniqueNames))", @@ -133,5 +162,4 @@ func NewAccoauntSource() AccountSource { port: 636, user: user, group: group} - } diff --git a/util/logger.go b/util/logger.go index 4b1e50b..7b8b471 100644 --- a/util/logger.go +++ b/util/logger.go @@ -4,12 +4,9 @@ package util import ( - "time" - "os" "github.com/Sirupsen/logrus" - "github.com/gin-gonic/gin" ) var logger = logrus.New() @@ -23,43 +20,3 @@ func Log() *logrus.Logger { // logging framework return logger } - -// Ginrus returns a gin.HandlerFunc (middleware) that logs requests using logrus. -// -// Requests with errors are logged using logrus.Error(). -// Requests without errors are logged using logrus.Info(). -// -// It receives: -// 1. A time package format string (e.g. time.RFC3339). -// 2. A boolean stating whether to use UTC time zone or local. -func Ginrus(timeFormat string, utc bool) gin.HandlerFunc { - return func(c *gin.Context) { - start := time.Now() - // some evil middlewares modify this values - path := c.Request.URL.Path - c.Next() - - end := time.Now() - latency := end.Sub(start) - if utc { - end = end.UTC() - } - - entry := Log().WithFields(logrus.Fields{ - "status": c.Writer.Status(), - "method": c.Request.Method, - "path": path, - "ip": c.ClientIP(), - "latency": latency, - "user-agent": c.Request.UserAgent(), - "time": end.Format(timeFormat), - }) - - if len(c.Errors) > 0 { - // Append error field if this is an erroneous request. - entry.Error(c.Errors.String()) - } else { - entry.Info() - } - } -}