probe/auth/authenticator.go

110 lines
2.5 KiB
Go
Raw Normal View History

2018-04-12 11:54:56 +00:00
package auth
import (
"context"
"fmt"
"sync"
"git.loafle.net/commons/configuration-go"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
2018-04-26 08:39:32 +00:00
occn "git.loafle.net/overflow/commons-go/config/noauthprobe"
2018-04-17 10:28:37 +00:00
occi "git.loafle.net/overflow/commons-go/core/interfaces"
2018-04-14 08:57:01 +00:00
2018-04-17 14:11:13 +00:00
"git.loafle.net/overflow/probe/auth/annotation"
2018-04-17 10:28:37 +00:00
"git.loafle.net/overflow/probe/auth/service"
2018-04-18 14:56:13 +00:00
"git.loafle.net/overflow/probe/config"
2018-04-12 11:54:56 +00:00
)
type Authenticator struct {
2018-04-26 08:39:32 +00:00
authConfig occn.Auth
2018-04-12 11:54:56 +00:00
2018-04-17 10:28:37 +00:00
services []interface{}
2018-04-17 14:11:13 +00:00
endChan chan error
2018-04-12 11:54:56 +00:00
stopChan chan struct{}
stopWg sync.WaitGroup
}
func (a *Authenticator) EndableStart() (<-chan error, error) {
if a.stopChan != nil {
2018-04-17 14:11:13 +00:00
return nil, fmt.Errorf("already running. Stop it before starting it again")
2018-04-12 11:54:56 +00:00
}
conf := configuration.New()
2018-04-14 08:57:01 +00:00
2018-04-18 14:56:13 +00:00
if configuration.Exists(config.NoAuthProbeConfigFilePath()) {
if err := conf.Load(&a.authConfig, config.NoAuthProbeConfigFilePath()); nil != err {
2018-04-12 11:54:56 +00:00
logging.Logger().Errorf("%s %v", err)
2018-04-18 14:56:13 +00:00
return nil, fmt.Errorf("loading of auth config file[%s] failed", config.NoAuthProbeConfigFilePath())
2018-04-12 11:54:56 +00:00
}
}
2018-04-13 11:59:46 +00:00
if nil != a.authConfig.DeniedDate {
return nil, fmt.Errorf("cannot start because this probe have been denied from overFlow at %s", a.authConfig.DeniedDate.String())
2018-04-12 11:54:56 +00:00
}
2018-04-14 08:57:01 +00:00
cdr.RegisterResource("AuthConfig", &a.authConfig)
authDoneChan := make(chan error)
cdr.RegisterResource("AuthDoneChan", authDoneChan)
2018-04-12 11:54:56 +00:00
2018-04-17 14:11:13 +00:00
services, err := cdr.GetInstancesByAnnotationType(annotation.AuthRPCServiceAnnotationType)
2018-04-14 08:57:01 +00:00
if nil != err {
2018-04-12 11:54:56 +00:00
return nil, err
}
2018-04-17 10:28:37 +00:00
a.services = services
2018-04-12 11:54:56 +00:00
2018-04-17 10:28:37 +00:00
if err := occi.ExecServices(a.services, occi.ServiceMethodInit, service.OrderedServices, false); nil != err {
2018-04-12 11:54:56 +00:00
return nil, err
}
2018-04-17 14:11:13 +00:00
a.endChan = make(chan error)
2018-04-12 11:54:56 +00:00
a.stopChan = make(chan struct{})
a.stopWg.Add(1)
2018-04-17 14:11:13 +00:00
go a.handleAuthenticator(authDoneChan)
2018-04-12 11:54:56 +00:00
2018-04-17 14:11:13 +00:00
return a.endChan, nil
2018-04-12 11:54:56 +00:00
}
func (a *Authenticator) Stop(ctx context.Context) error {
if a.stopChan == nil {
2018-04-17 14:11:13 +00:00
return nil
2018-04-12 11:54:56 +00:00
}
close(a.stopChan)
a.stopWg.Wait()
2018-04-17 14:11:13 +00:00
occi.ExecServices(a.services, occi.ServiceMethodDestroy, service.OrderedServices, true)
2018-04-17 10:28:37 +00:00
2018-04-12 11:54:56 +00:00
a.stopChan = nil
2018-04-17 14:11:13 +00:00
close(a.endChan)
2018-04-12 11:54:56 +00:00
2018-04-17 14:11:13 +00:00
return nil
2018-04-12 11:54:56 +00:00
}
2018-04-17 14:11:13 +00:00
func (a *Authenticator) handleAuthenticator(authDoneChan chan error) {
2018-04-12 11:54:56 +00:00
var err error
defer func() {
a.stopWg.Done()
2018-04-17 14:11:13 +00:00
a.endChan <- err
2018-04-12 11:54:56 +00:00
}()
2018-04-17 10:28:37 +00:00
err = occi.ExecServices(a.services, occi.ServiceMethodStart, service.OrderedServices, false)
if nil != err {
2018-04-14 08:57:01 +00:00
return
}
2018-04-17 10:28:37 +00:00
LOOP:
2018-04-12 11:54:56 +00:00
for {
select {
2018-04-14 08:57:01 +00:00
case err = <-authDoneChan:
2018-04-17 10:28:37 +00:00
break LOOP
2018-04-12 11:54:56 +00:00
case <-a.stopChan:
2018-04-17 10:28:37 +00:00
break LOOP
2018-04-12 11:54:56 +00:00
}
}
2018-04-17 10:28:37 +00:00
2018-04-17 14:11:13 +00:00
occi.ExecServices(a.services, occi.ServiceMethodStop, service.OrderedServices, true)
2018-04-12 11:54:56 +00:00
}