2018-04-12 11:54:56 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.loafle.net/commons/configuration-go"
|
|
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
|
|
|
logging "git.loafle.net/commons/logging-go"
|
|
|
|
crc "git.loafle.net/commons/rpc-go/client"
|
|
|
|
occa "git.loafle.net/overflow/commons-go/core/annotation"
|
|
|
|
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
|
2018-04-14 08:57:01 +00:00
|
|
|
"git.loafle.net/overflow/probe/client/central"
|
2018-04-12 11:54:56 +00:00
|
|
|
"git.loafle.net/overflow/probe/config"
|
2018-04-14 08:57:01 +00:00
|
|
|
|
|
|
|
// For service
|
|
|
|
_ "git.loafle.net/overflow/probe/auth/service"
|
2018-04-12 11:54:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Authenticator struct {
|
|
|
|
Config *config.Config
|
|
|
|
ConfigDir string
|
|
|
|
|
|
|
|
authConfig ocnc.Auth
|
|
|
|
|
|
|
|
stopChan chan struct{}
|
|
|
|
stopWg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Authenticator) EndableStart() (<-chan error, error) {
|
|
|
|
if a.stopChan != nil {
|
|
|
|
return nil, fmt.Errorf("authenticator already running. Stop it before starting it again")
|
|
|
|
}
|
|
|
|
|
|
|
|
authConfigPath := path.Join(a.ConfigDir, ocnc.ConfigFileName)
|
|
|
|
conf := configuration.New()
|
2018-04-14 08:57:01 +00:00
|
|
|
|
2018-04-12 11:54:56 +00:00
|
|
|
if configuration.Exists(authConfigPath) {
|
|
|
|
if err := conf.Load(&a.authConfig, authConfigPath); nil != err {
|
|
|
|
logging.Logger().Errorf("%s %v", err)
|
|
|
|
return nil, fmt.Errorf("loading of auth config file[%s] failed", authConfigPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-14 08:57:01 +00:00
|
|
|
services, err := cdr.GetInstancesByAnnotationType(occa.RPCServiceAnnotationType)
|
|
|
|
if nil != err {
|
2018-04-12 11:54:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-14 08:57:01 +00:00
|
|
|
client, err := central.NewAuth(a.HandleTempKey, services)
|
|
|
|
if nil != err {
|
2018-04-12 11:54:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
endChan := make(chan error)
|
|
|
|
a.stopChan = make(chan struct{})
|
|
|
|
|
|
|
|
a.stopWg.Add(1)
|
2018-04-14 08:57:01 +00:00
|
|
|
go a.handleAuthenticator(client, authDoneChan, endChan)
|
2018-04-12 11:54:56 +00:00
|
|
|
|
|
|
|
return endChan, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Authenticator) Stop(ctx context.Context) error {
|
|
|
|
if a.stopChan == nil {
|
|
|
|
return fmt.Errorf("Authenticator: must be started before stopping it")
|
|
|
|
}
|
|
|
|
close(a.stopChan)
|
|
|
|
a.stopWg.Wait()
|
|
|
|
|
|
|
|
a.stopChan = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Authenticator) logHeader() string {
|
|
|
|
return "Authenticator:"
|
|
|
|
}
|
|
|
|
|
2018-04-14 08:57:01 +00:00
|
|
|
func (a *Authenticator) HandleTempKey(tempKey string) {
|
|
|
|
logging.Logger().Infof("%s registered by central", a.logHeader())
|
|
|
|
a.authConfig.TempKey = &tempKey
|
|
|
|
if err := configuration.Save(a.authConfig, path.Join(a.ConfigDir, ocnc.ConfigFileName), true); nil != err {
|
|
|
|
logging.Logger().Errorf("%s %v", a.logHeader(), err)
|
|
|
|
return
|
2018-04-12 11:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-14 08:57:01 +00:00
|
|
|
func (a *Authenticator) handleAuthenticator(client *crc.Client, authDoneChan chan error, endChan chan<- error) {
|
2018-04-12 11:54:56 +00:00
|
|
|
var err error
|
|
|
|
defer func() {
|
2018-04-14 08:57:01 +00:00
|
|
|
if nil != client {
|
|
|
|
err = client.Stop(context.Background())
|
2018-04-12 11:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a.stopWg.Done()
|
|
|
|
endChan <- err
|
|
|
|
}()
|
|
|
|
|
2018-04-14 08:57:01 +00:00
|
|
|
if err = client.Start(); nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-12 11:54:56 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-04-14 08:57:01 +00:00
|
|
|
case err = <-authDoneChan:
|
2018-04-12 11:54:56 +00:00
|
|
|
return
|
|
|
|
case <-a.stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|