From 5bf5ac8bb258d643c4daff04bd221c162936f138 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Sat, 16 Jan 2021 19:06:47 +0100 Subject: [PATCH] added an teardownfunction to stop goroutines from the sites. --- http.go | 6 ++++++ modules/saml/saml.go | 27 ++++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/http.go b/http.go index 2182098..48cc766 100644 --- a/http.go +++ b/http.go @@ -31,6 +31,7 @@ type Server struct { router *gin.Engine config *toml.Tree authhf auth.AuthenticationHandler + sites []Site } // StartServer starts the server as configured and sends the errormessage to the log. @@ -95,16 +96,21 @@ func (s *Server) UseAuthBackend(a auth.AuthenticationHandler) { // Stop Shuts the Server down func (s *Server) Stop(ctx context.Context) { log.Info().Err(s.http.Shutdown(ctx)).Msg("Server Shut down.") + for _, site := range s.sites { + site.Teardown() + } } // Site is an Interface to abstract the modularized group of pages. // The Middleware must be able to detect multiple calls byy itself. Deduplication is not performed. type Site interface { Init(*gin.RouterGroup) + Teardown() } // RegisterSite adds an site to the engine as its own grouo func (s *Server) RegisterSite(path string, site Site) { site.Init(s.router.Group(path)) + s.sites = append(s.sites, site) return } diff --git a/modules/saml/saml.go b/modules/saml/saml.go index 5b92ddf..bc604b3 100644 --- a/modules/saml/saml.go +++ b/modules/saml/saml.go @@ -14,6 +14,7 @@ import ( "github.com/gin-gonic/gin" "github.com/pelletier/go-toml" "github.com/phuslu/log" + "go.sebtobie.de/httpserver" "go.sebtobie.de/httpserver/auth" "gopkg.in/dgrijalva/jwt-go.v3" ) @@ -26,6 +27,7 @@ var ( Domain: "example.com", Cookiename: "ILOVECOOKIES", } + _ httpserver.Site = defaultsaml ) type metadata struct{} @@ -52,22 +54,22 @@ type SAML struct { } // NewSAMLEndpoint creates an endpoint which handles SAML Requests. -func NewSAMLEndpoint(config *toml.Tree) (*SAML, error) { +func NewSAMLEndpoint(config *toml.Tree) (s *SAML, err error) { + s = &(*defaultsaml) + s.config = config log.Trace().Str("config", config.String()).Msg("config") var key interface{} - var err error - var s SAML = *defaultsaml s.config = config - if err := config.Unmarshal(&s); err != nil { + if err = config.Unmarshal(&s); err != nil { log.Error().Err(err).Msg("Error while mapping config to struct") - return nil, err + return } key, err = initcert(s.SPPrivatekey, func(key interface{}) bool { _, ok := key.(*rsa.PrivateKey) return ok }) if err != nil { - return nil, err + return } s.spprivatekey = key.(*rsa.PrivateKey) @@ -76,7 +78,7 @@ func NewSAMLEndpoint(config *toml.Tree) (*SAML, error) { return ok }) if err != nil { - return nil, err + return } s.sppublickey = key.(*x509.Certificate) @@ -85,7 +87,7 @@ func NewSAMLEndpoint(config *toml.Tree) (*SAML, error) { return ok }) if err != nil { - return nil, err + return } s.jwtprivatekey = key.(*rsa.PrivateKey) s.sp = &saml.ServiceProvider{ @@ -95,14 +97,14 @@ func NewSAMLEndpoint(config *toml.Tree) (*SAML, error) { var idpurl *url.URL idpurl, err = url.ParseRequestURI(s.IDP) if err != nil { - return nil, err + return } s.sp.IDPMetadata, err = samlsp.FetchMetadata(context.Background(), &s.HttpClient, *idpurl) if err != nil { - return nil, err + return } s.sp.AuthnNameIDFormat = saml.UnspecifiedNameIDFormat - return &s, nil + return } // Init initalizes the routes @@ -123,6 +125,9 @@ func (s *SAML) Init(router *gin.RouterGroup) { router.POST("/acs", s.acsHF) } +// Teardown is to satisfy the httpserver.Site interface. +func (s *SAML) Teardown() {} + func (s *SAML) metadataHF(c *gin.Context) { m := s.sp.Metadata() log.Debug().Time("Validuntil", m.ValidUntil).Msg("SP MEtadata")