1
0
Fork 0
mirror of https://github.com/Luzifer/nginx-sso.git synced 2024-12-20 12:51:17 +00:00

Implement config reload on SIGHUP (#12)

closes #11
This commit is contained in:
Knut Ahlers 2018-09-20 15:39:22 +02:00 committed by GitHub
parent 7ed2b8d7cc
commit 8c9a2f6285
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 7 deletions

38
main.go
View file

@ -6,7 +6,9 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"os/signal"
"path" "path"
"syscall"
"github.com/Luzifer/rconfig" "github.com/Luzifer/rconfig"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
@ -83,18 +85,26 @@ func init() {
mainCfg.Listen.Port = 8082 mainCfg.Listen.Port = 8082
} }
func main() { func loadConfiguration() error {
yamlSource, err := ioutil.ReadFile(cfg.ConfigFile) yamlSource, err := ioutil.ReadFile(cfg.ConfigFile)
if err != nil { if err != nil {
log.WithError(err).Fatal("Unable to read configuration file") return fmt.Errorf("Unable to read configuration file: %s", err)
} }
if err := yaml.Unmarshal(yamlSource, &mainCfg); err != nil { if err := yaml.Unmarshal(yamlSource, &mainCfg); err != nil {
log.WithError(err).Fatal("Unable to load configuration file") return fmt.Errorf("Unable to load configuration file: %s", err)
} }
if err := initializeAuthenticators(yamlSource); err != nil { if err := initializeAuthenticators(yamlSource); err != nil {
log.WithError(err).Fatal("Unable to configure authentication") return fmt.Errorf("Unable to configure authentication: %s", err)
}
return nil
}
func main() {
if err := loadConfiguration(); err != nil {
log.WithError(err).Fatal("Unable to load configuration")
} }
cookieStore = sessions.NewCookieStore([]byte(mainCfg.Cookie.AuthKey)) cookieStore = sessions.NewCookieStore([]byte(mainCfg.Cookie.AuthKey))
@ -103,7 +113,25 @@ func main() {
http.HandleFunc("/login", handleLoginRequest) http.HandleFunc("/login", handleLoginRequest)
http.HandleFunc("/logout", handleLogoutRequest) http.HandleFunc("/logout", handleLogoutRequest)
http.ListenAndServe(fmt.Sprintf("%s:%d", mainCfg.Listen.Addr, mainCfg.Listen.Port), context.ClearHandler(http.DefaultServeMux)) go http.ListenAndServe(
fmt.Sprintf("%s:%d", mainCfg.Listen.Addr, mainCfg.Listen.Port),
context.ClearHandler(http.DefaultServeMux),
)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP)
for sig := range sigChan {
switch sig {
case syscall.SIGHUP:
if err := loadConfiguration(); err != nil {
log.WithError(err).Error("Unable to reload configuration")
}
default:
log.Fatalf("Received unexpected signal: %v", sig)
}
}
} }
func handleAuthRequest(res http.ResponseWriter, r *http.Request) { func handleAuthRequest(res http.ResponseWriter, r *http.Request) {

View file

@ -72,12 +72,13 @@ func initializeAuthenticators(yamlSource []byte) error {
authenticatorRegistryMutex.Lock() authenticatorRegistryMutex.Lock()
defer authenticatorRegistryMutex.Unlock() defer authenticatorRegistryMutex.Unlock()
tmp := []authenticator{}
for _, a := range authenticatorRegistry { for _, a := range authenticatorRegistry {
err := a.Configure(yamlSource) err := a.Configure(yamlSource)
switch err { switch err {
case nil: case nil:
activeAuthenticators = append(activeAuthenticators, a) tmp = append(tmp, a)
log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Activated authenticator") log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Activated authenticator")
case errAuthenticatorUnconfigured: case errAuthenticatorUnconfigured:
log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Authenticator unconfigured") log.WithFields(log.Fields{"authenticator": a.AuthenticatorID()}).Debug("Authenticator unconfigured")
@ -87,10 +88,12 @@ func initializeAuthenticators(yamlSource []byte) error {
} }
} }
if len(activeAuthenticators) == 0 { if len(tmp) == 0 {
return fmt.Errorf("No authenticator configurations supplied") return fmt.Errorf("No authenticator configurations supplied")
} }
activeAuthenticators = tmp
return nil return nil
} }