overflow_probes/auth/auth.go

182 lines
4.5 KiB
Go
Raw Normal View History

2017-09-21 08:38:05 +00:00
package auth
import (
"context"
2017-09-22 09:20:07 +00:00
"errors"
2017-09-21 08:38:05 +00:00
"fmt"
"net/http"
"path"
2017-09-21 11:04:30 +00:00
"time"
2017-09-21 08:38:05 +00:00
lfcc "git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
2017-09-22 09:20:07 +00:00
"git.loafle.net/overflow/overflow_probes/central/api/module"
2017-09-21 08:38:05 +00:00
"git.loafle.net/overflow/overflow_probes/central/client"
"git.loafle.net/overflow/overflow_probes/commons"
"git.loafle.net/overflow/overflow_probes/config"
2017-09-22 09:20:07 +00:00
opuu "git.loafle.net/overflow/overflow_probes/util/url"
2017-09-21 08:38:05 +00:00
)
const (
2017-09-22 09:20:07 +00:00
noAuthEntryPoint = "/auth"
2017-09-21 08:38:05 +00:00
)
type AuthHandler interface {
commons.Handler
}
type authHandlers struct {
2017-09-22 09:20:07 +00:00
c client.Client
entryURL string
configDir string
2017-09-21 08:38:05 +00:00
noAuthConfigPath string
noAuthConfig config.NoAuthProbeConfig
2017-09-22 09:20:07 +00:00
probeConfigPath string
probeConfig config.ProbeConfig
2017-09-21 08:38:05 +00:00
2017-09-22 09:20:07 +00:00
shutdownChan chan bool
acceptedChan chan bool
deniedChan chan error
2017-09-21 08:38:05 +00:00
}
2017-09-22 09:20:07 +00:00
func New(configDir string) (AuthHandler, error) {
2017-09-21 08:38:05 +00:00
var err error
2017-09-22 09:20:07 +00:00
h := &authHandlers{
configDir: configDir,
shutdownChan: make(chan bool),
acceptedChan: make(chan bool),
deniedChan: make(chan error),
2017-09-21 08:38:05 +00:00
}
2017-09-22 09:20:07 +00:00
if h.entryURL, err = opuu.Join(config.Config.Central.URL, noAuthEntryPoint); nil != err {
return nil, err
}
2017-09-21 08:38:05 +00:00
2017-09-22 09:20:07 +00:00
h.noAuthConfigPath = path.Join(configDir, config.NoAuthProbeConfigFileName)
h.probeConfigPath = path.Join(configDir, config.ProbeConfigFileName)
2017-09-21 08:38:05 +00:00
conf := lfcc.New()
if lfcc.Exists(h.noAuthConfigPath) {
if err = conf.Load(&h.noAuthConfig, h.noAuthConfigPath); nil != err {
2017-09-22 09:20:07 +00:00
return nil, fmt.Errorf("Auth: Loading of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err)
2017-09-21 08:38:05 +00:00
}
} else {
if err = lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
2017-09-22 09:20:07 +00:00
return nil, fmt.Errorf("Auth: Saving of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err)
2017-09-21 08:38:05 +00:00
}
}
2017-09-22 09:20:07 +00:00
return h, nil
2017-09-21 08:38:05 +00:00
}
2017-09-22 09:20:07 +00:00
func (h *authHandlers) Serve() error {
if nil != h.noAuthConfig.DenyDate {
return fmt.Errorf("Cannot start because this probe have been denied from overFlow[%s]", h.noAuthConfig.DenyDate.String())
}
2017-09-21 08:38:05 +00:00
h.c = client.New()
2017-09-22 09:20:07 +00:00
h.c.OnNotify(module.NoAuthProbeService_AcceptNoAuthProbe, h.onNoAuthProbeAccept)
h.c.OnNotify(module.NoAuthProbeService_DenyNoauthProbe, h.onNoAuthProbeDeny)
2017-09-21 08:38:05 +00:00
2017-09-22 09:20:07 +00:00
var err error
if nil != h.noAuthConfig.TempKey && "" != *h.noAuthConfig.TempKey {
err = h.serveConnect(*h.noAuthConfig.TempKey)
2017-09-21 08:38:05 +00:00
} else {
2017-09-22 09:20:07 +00:00
err = h.serveRegistration()
}
if nil != err {
return err
}
ListenLoop:
for {
select {
case <-h.shutdownChan:
return errors.New("Shutting down")
case <-h.acceptedChan:
break ListenLoop
case err := <-h.deniedChan:
2017-09-21 08:38:05 +00:00
return err
}
}
2017-09-22 09:20:07 +00:00
return nil
}
func (h *authHandlers) Shutdown(ctx context.Context) error {
h.shutdownChan <- true
return nil
}
func (h *authHandlers) serveRegistration() error {
var err error
header := http.Header{}
var enc string
if enc, err = getRegistHeader(); nil != err {
return err
}
header[module.NoAuthProbeHeader_NoAuthRegist] = []string{enc}
2017-09-21 08:38:05 +00:00
var res *http.Response
if res, err = h.c.Dial(h.entryURL, header, 4096, 4096); nil != err {
return err
}
2017-09-22 09:20:07 +00:00
tempKey := res.Header.Get(module.NoAuthProbeHeader_SetNoAuthID)
h.noAuthConfig.TempKey = &tempKey
if err = lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
return err
2017-09-21 11:04:30 +00:00
}
2017-09-22 09:20:07 +00:00
return nil
}
func (h *authHandlers) serveConnect(noAuthTempKey string) error {
var err error
header := http.Header{}
header[module.NoAuthProbeHeader_NoAuthID] = []string{noAuthTempKey}
2017-09-21 11:04:30 +00:00
2017-09-22 09:20:07 +00:00
var res *http.Response
if res, err = h.c.Dial(h.entryURL, header, 4096, 4096); nil != err {
return err
2017-09-21 08:38:05 +00:00
}
2017-09-22 09:20:07 +00:00
logging.Logger.Debug(fmt.Sprintf("Auth: Connect HTTP Status[%s]", res.Status))
2017-09-21 08:38:05 +00:00
return nil
}
2017-09-21 11:04:30 +00:00
func (h *authHandlers) onNoAuthProbeAccept(method string, params interface{}) {
var err error
ps := params.([]string)
probeID := ps[0]
if lfcc.Exists(h.probeConfigPath) {
if err = lfcc.Load(&h.probeConfig, h.probeConfigPath); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Loading of Probe config file[%s] failed error[%v]", h.probeConfigPath, err))
}
}
2017-09-22 09:20:07 +00:00
h.probeConfig.ID = &probeID
2017-09-21 11:04:30 +00:00
if err = lfcc.Save(h.probeConfig, h.probeConfigPath, true); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Saving of Probe config file[%s] failed error[%v]", h.probeConfigPath, err))
}
2017-09-22 09:20:07 +00:00
h.acceptedChan <- true
2017-09-21 11:04:30 +00:00
}
func (h *authHandlers) onNoAuthProbeDeny(method string, params interface{}) {
2017-09-22 09:20:07 +00:00
n := time.Now()
h.noAuthConfig.DenyDate = &n
2017-09-21 11:04:30 +00:00
if err := lfcc.Save(h.noAuthConfig, h.noAuthConfigPath, true); nil != err {
logging.Logger.Error(fmt.Sprintf("Auth: Saving of NoAuth config file[%s] failed error[%v]", h.noAuthConfigPath, err))
}
2017-09-22 09:20:07 +00:00
h.deniedChan <- fmt.Errorf("This probe have been denied from overFlow")
2017-09-21 08:38:05 +00:00
}