ing
This commit is contained in:
parent
45dc0f9920
commit
2fa1e8f5c8
|
@ -1,3 +1,4 @@
|
|||
{
|
||||
"tempKey": "6a9937503f1111e8b0460242ac120002"
|
||||
"tempKey": "7da4563e3fc111e8b0460242ac120002",
|
||||
"acceptedDate": "2018-04-14T17:53:19.102685027+09:00"
|
||||
}
|
|
@ -3,25 +3,20 @@ package auth
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
crpj "git.loafle.net/commons/rpc-go/protocol/json"
|
||||
crr "git.loafle.net/commons/rpc-go/registry"
|
||||
csswc "git.loafle.net/commons/server-go/socket/web/client"
|
||||
occa "git.loafle.net/overflow/commons-go/core/annotation"
|
||||
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
|
||||
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
|
||||
ocpc "git.loafle.net/overflow/commons-go/probe/config"
|
||||
"git.loafle.net/overflow/probe/auth/info"
|
||||
_ "git.loafle.net/overflow/probe/auth/service"
|
||||
"git.loafle.net/overflow/probe/client/central"
|
||||
"git.loafle.net/overflow/probe/config"
|
||||
|
||||
// For service
|
||||
_ "git.loafle.net/overflow/probe/auth/service"
|
||||
)
|
||||
|
||||
type Authenticator struct {
|
||||
|
@ -29,11 +24,6 @@ type Authenticator struct {
|
|||
ConfigDir string
|
||||
|
||||
authConfig ocnc.Auth
|
||||
client *crc.Client
|
||||
|
||||
accpetedChan chan string
|
||||
deniedChan chan struct{}
|
||||
tempKeyChan chan string
|
||||
|
||||
stopChan chan struct{}
|
||||
stopWg sync.WaitGroup
|
||||
|
@ -46,6 +36,7 @@ func (a *Authenticator) EndableStart() (<-chan error, error) {
|
|||
|
||||
authConfigPath := path.Join(a.ConfigDir, ocnc.ConfigFileName)
|
||||
conf := configuration.New()
|
||||
|
||||
if configuration.Exists(authConfigPath) {
|
||||
if err := conf.Load(&a.authConfig, authConfigPath); nil != err {
|
||||
logging.Logger().Errorf("%s %v", err)
|
||||
|
@ -57,15 +48,17 @@ func (a *Authenticator) EndableStart() (<-chan error, error) {
|
|||
return nil, fmt.Errorf("cannot start because this probe have been denied from overFlow at %s", a.authConfig.DeniedDate.String())
|
||||
}
|
||||
|
||||
a.accpetedChan = make(chan string, 1)
|
||||
a.deniedChan = make(chan struct{}, 1)
|
||||
a.tempKeyChan = make(chan string, 1)
|
||||
cdr.RegisterResource("AuthConfig", &a.authConfig)
|
||||
authDoneChan := make(chan error)
|
||||
cdr.RegisterResource("AuthDoneChan", authDoneChan)
|
||||
|
||||
if err := a.initClient(); nil != err {
|
||||
services, err := cdr.GetInstancesByAnnotationType(occa.RPCServiceAnnotationType)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := a.client.Start(); nil != err {
|
||||
client, err := central.NewAuth(a.HandleTempKey, services)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -73,7 +66,7 @@ func (a *Authenticator) EndableStart() (<-chan error, error) {
|
|||
a.stopChan = make(chan struct{})
|
||||
|
||||
a.stopWg.Add(1)
|
||||
go a.handleAuthenticator(endChan)
|
||||
go a.handleAuthenticator(client, authDoneChan, endChan)
|
||||
|
||||
return endChan, nil
|
||||
}
|
||||
|
@ -94,127 +87,35 @@ func (a *Authenticator) logHeader() string {
|
|||
return "Authenticator:"
|
||||
}
|
||||
|
||||
func (a *Authenticator) initService() (crr.RPCInvoker, error) {
|
||||
cdr.RegisterResource("AcceptedChan", a.accpetedChan)
|
||||
cdr.RegisterResource("DeniedChan", a.deniedChan)
|
||||
|
||||
services, err := cdr.GetInstancesByAnnotationType(occa.RPCServiceAnnotationType)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
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
|
||||
}
|
||||
|
||||
rpcRegistry := crr.NewRPCRegistry()
|
||||
rpcRegistry.RegisterServices(services...)
|
||||
|
||||
return rpcRegistry, nil
|
||||
}
|
||||
|
||||
func (a *Authenticator) initClient() error {
|
||||
_connector := a.Config.Central.Connector.Clone()
|
||||
connector := _connector.(*csswc.Connectors)
|
||||
|
||||
header := make(map[string][]string)
|
||||
|
||||
switch a.authConfig.State() {
|
||||
case ocnc.AuthStateTypeRegisterd:
|
||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect}
|
||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey] = []string{*a.authConfig.TempKey}
|
||||
default:
|
||||
rh, err := info.GetRegistHeader(a.Config.Account.APIKey)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist}
|
||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Info] = []string{rh}
|
||||
}
|
||||
|
||||
centralURL := fmt.Sprintf("ws://%s:%d%s", a.Config.Central.Host, a.Config.Central.Port, ocncc.HTTPEntry_Auth)
|
||||
|
||||
logging.Logger().Debugf("%s central %s", a.logHeader(), centralURL)
|
||||
|
||||
connector.Name = "Authenticator"
|
||||
connector.URL = centralURL
|
||||
connector.RequestHeader = header
|
||||
connector.ResponseHandler = func(res *http.Response) {
|
||||
switch a.authConfig.State() {
|
||||
case ocnc.AuthStateTypeNotRegisterd:
|
||||
tempProbeKey := res.Header.Get(ocncc.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
|
||||
a.tempKeyChan <- tempProbeKey
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
rpcInvoker, err := a.initService()
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
codec := crpj.NewClientCodec()
|
||||
|
||||
a.client = &crc.Client{
|
||||
Connector: connector,
|
||||
Codec: codec,
|
||||
RPCInvoker: rpcInvoker,
|
||||
Name: "Authenticator",
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Authenticator) handleAuthenticator(endChan chan<- error) {
|
||||
func (a *Authenticator) handleAuthenticator(client *crc.Client, authDoneChan chan error, endChan chan<- error) {
|
||||
var err error
|
||||
defer func() {
|
||||
if nil != a.client {
|
||||
err = a.client.Stop(context.Background())
|
||||
if nil != client {
|
||||
err = client.Stop(context.Background())
|
||||
}
|
||||
|
||||
a.stopWg.Done()
|
||||
endChan <- err
|
||||
}()
|
||||
|
||||
if err = client.Start(); nil != err {
|
||||
logging.Logger().Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case probeKey, ok := <-a.accpetedChan:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
logging.Logger().Infof("%s accepted by central", a.logHeader())
|
||||
|
||||
n := time.Now()
|
||||
a.authConfig.AcceptedDate = &n
|
||||
err = configuration.Save(a.authConfig, path.Join(a.ConfigDir, ocnc.ConfigFileName), true)
|
||||
if nil != err {
|
||||
logging.Logger().Errorf("%s %v", a.logHeader(), err)
|
||||
}
|
||||
|
||||
a.Config.Probe.Key = &probeKey
|
||||
err = configuration.Save(a.Config, path.Join(a.ConfigDir, ocpc.ConfigFileName), true)
|
||||
if nil != err {
|
||||
logging.Logger().Errorf("%s %v", a.logHeader(), err)
|
||||
}
|
||||
case err = <-authDoneChan:
|
||||
return
|
||||
case _, ok := <-a.deniedChan:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
logging.Logger().Infof("%s denied by central", a.logHeader())
|
||||
n := time.Now()
|
||||
a.authConfig.DeniedDate = &n
|
||||
err = configuration.Save(a.authConfig, path.Join(a.ConfigDir, ocnc.ConfigFileName), true)
|
||||
if nil != err {
|
||||
logging.Logger().Errorf("%s %v", a.logHeader(), err)
|
||||
}
|
||||
return
|
||||
case tempKey, ok := <-a.tempKeyChan:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
logging.Logger().Infof("%s registered by central", a.logHeader())
|
||||
a.authConfig.TempKey = &tempKey
|
||||
err = configuration.Save(a.authConfig, path.Join(a.ConfigDir, ocnc.ConfigFileName), true)
|
||||
if nil != err {
|
||||
logging.Logger().Errorf("%s %v", a.logHeader(), err)
|
||||
return
|
||||
}
|
||||
case <-a.stopChan:
|
||||
return
|
||||
}
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"git.loafle.net/commons/configuration-go"
|
||||
cda "git.loafle.net/commons/di-go/annotation"
|
||||
cdr "git.loafle.net/commons/di-go/registry"
|
||||
logging "git.loafle.net/commons/logging-go"
|
||||
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
|
||||
ocpc "git.loafle.net/overflow/commons-go/probe/config"
|
||||
"git.loafle.net/overflow/probe/config"
|
||||
|
||||
// For annotation
|
||||
_ "git.loafle.net/overflow/commons-go/core/annotation"
|
||||
)
|
||||
|
||||
|
@ -17,16 +27,54 @@ func init() {
|
|||
type NoAuthProbeService struct {
|
||||
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
|
||||
|
||||
AcceptedChan chan<- string `annotation:"@Resource(name='AcceptedChan')"`
|
||||
DeniedChan chan<- struct{} `annotation:"@Resource(name='DeniedChan')"`
|
||||
ConfigDir string `annotation:"@Resource(name='ConfigDir')"`
|
||||
Config *config.Config `annotation:"@Resource(name='Config')"`
|
||||
AuthConfig *ocnc.Auth `annotation:"@Resource(name='AuthConfig')"`
|
||||
AuthDoneChan chan error `annotation:"@Resource(name='AuthDoneChan')"`
|
||||
}
|
||||
|
||||
func (s *NoAuthProbeService) Start() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NoAuthProbeService) Stop(ctx context.Context) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NoAuthProbeService) Accept(probeKey string) error {
|
||||
s.AcceptedChan <- probeKey
|
||||
logging.Logger().Infof("accepted by central")
|
||||
|
||||
n := time.Now()
|
||||
s.AuthConfig.AcceptedDate = &n
|
||||
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err {
|
||||
logging.Logger().Error(err)
|
||||
s.AuthDoneChan <- err
|
||||
return nil
|
||||
}
|
||||
|
||||
s.Config.Probe.Key = &probeKey
|
||||
if err := configuration.Save(s.Config, path.Join(s.ConfigDir, ocpc.ConfigFileName), true); nil != err {
|
||||
logging.Logger().Error(err)
|
||||
s.AuthDoneChan <- err
|
||||
return nil
|
||||
}
|
||||
|
||||
s.AuthDoneChan <- nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NoAuthProbeService) Deny() error {
|
||||
s.DeniedChan <- struct{}{}
|
||||
logging.Logger().Infof("denied by central")
|
||||
n := time.Now()
|
||||
s.AuthConfig.DeniedDate = &n
|
||||
if err := configuration.Save(s.AuthConfig, path.Join(s.ConfigDir, ocnc.ConfigFileName), true); nil != err {
|
||||
logging.Logger().Error(err)
|
||||
s.AuthDoneChan <- err
|
||||
return nil
|
||||
}
|
||||
|
||||
s.AuthDoneChan <- nil
|
||||
return nil
|
||||
}
|
||||
|
|
77
client/central/auth.go
Normal file
77
client/central/auth.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package central
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
cdr "git.loafle.net/commons/di-go/registry"
|
||||
logging "git.loafle.net/commons/logging-go"
|
||||
crc "git.loafle.net/commons/rpc-go/client"
|
||||
cur "git.loafle.net/commons/util-go/reflect"
|
||||
ocnc "git.loafle.net/overflow/commons-go/noauthprobe/config"
|
||||
ocncc "git.loafle.net/overflow/commons-go/noauthprobe/constants"
|
||||
"git.loafle.net/overflow/probe/auth/info"
|
||||
)
|
||||
|
||||
func NewAuth(tempkeyHandler func(tempkey string), services []interface{}) (*crc.Client, error) {
|
||||
config := getConfig()
|
||||
if nil == config {
|
||||
return nil, fmt.Errorf("Config is not available")
|
||||
}
|
||||
authConfig := getAuthConfig()
|
||||
if nil == authConfig {
|
||||
return nil, fmt.Errorf("AuthConfig is not available")
|
||||
}
|
||||
|
||||
connector, err := newConnector("Auth", ocncc.HTTPEntry_Auth)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
connector.RequestHeader = func() http.Header {
|
||||
header := make(map[string][]string)
|
||||
switch authConfig.State() {
|
||||
case ocnc.AuthStateTypeRegisterd:
|
||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Connect}
|
||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_TempProbeKey] = []string{*authConfig.TempKey}
|
||||
default:
|
||||
rh, err := info.GetRegistHeader(config.Account.APIKey)
|
||||
if nil != err {
|
||||
logging.Logger().Error(err)
|
||||
return header
|
||||
}
|
||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Method] = []string{ocncc.HTTPRequestHeaderValue_NoAuthProbe_Method_Regist}
|
||||
header[ocncc.HTTPRequestHeaderKey_NoAuthProbe_Info] = []string{rh}
|
||||
}
|
||||
return header
|
||||
}
|
||||
connector.ResponseHandler = func(res *http.Response) {
|
||||
switch authConfig.State() {
|
||||
case ocnc.AuthStateTypeNotRegisterd:
|
||||
tempProbeKey := res.Header.Get(ocncc.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
|
||||
if nil != tempkeyHandler {
|
||||
tempkeyHandler(tempProbeKey)
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
return newClient("Auth", connector, services), nil
|
||||
}
|
||||
|
||||
func getAuthConfig() *ocnc.Auth {
|
||||
_config, err := cdr.GetInstanceByName("AuthConfig")
|
||||
if nil != err {
|
||||
logging.Logger().Error(err)
|
||||
return nil
|
||||
}
|
||||
config, ok := _config.(*ocnc.Auth)
|
||||
if !ok {
|
||||
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_config))
|
||||
logging.Logger().Errorf("Cannot convert [%s]%s to Config type", pkg, n)
|
||||
return nil
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
75
client/central/cnetral.go
Normal file
75
client/central/cnetral.go
Normal file
|
@ -0,0 +1,75 @@
|
|||
package central
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
"reflect"
|
||||
|
||||
cdr "git.loafle.net/commons/di-go/registry"
|
||||
logging "git.loafle.net/commons/logging-go"
|
||||
crc "git.loafle.net/commons/rpc-go/client"
|
||||
crpj "git.loafle.net/commons/rpc-go/protocol/json"
|
||||
crr "git.loafle.net/commons/rpc-go/registry"
|
||||
csc "git.loafle.net/commons/server-go/client"
|
||||
csswc "git.loafle.net/commons/server-go/socket/web/client"
|
||||
cur "git.loafle.net/commons/util-go/reflect"
|
||||
"git.loafle.net/overflow/probe/config"
|
||||
)
|
||||
|
||||
func newConnector(name string, entryPath string) (*csswc.Connectors, error) {
|
||||
config := getConfig()
|
||||
if nil == config {
|
||||
return nil, fmt.Errorf("Config is not available")
|
||||
}
|
||||
|
||||
u := url.URL{
|
||||
Scheme: "ws",
|
||||
Host: config.Central.Address,
|
||||
}
|
||||
u.Path = path.Join(u.Path, entryPath)
|
||||
|
||||
_connector := config.Central.Connector.Clone()
|
||||
connector, ok := _connector.(*csswc.Connectors)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Cannot convert to Connectors type")
|
||||
}
|
||||
|
||||
connector.Name = name
|
||||
connector.URL = u.String()
|
||||
|
||||
return connector, nil
|
||||
}
|
||||
|
||||
func newClient(name string, connector csc.Connector, services []interface{}) *crc.Client {
|
||||
codec := crpj.NewClientCodec()
|
||||
|
||||
var rpcRegistry crr.RPCRegistry
|
||||
if nil != services && 0 < len(services) {
|
||||
rpcRegistry = crr.NewRPCRegistry()
|
||||
rpcRegistry.RegisterServices(services...)
|
||||
}
|
||||
|
||||
return &crc.Client{
|
||||
Connector: connector,
|
||||
Codec: codec,
|
||||
RPCInvoker: rpcRegistry,
|
||||
Name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func getConfig() *config.Config {
|
||||
_config, err := cdr.GetInstanceByName("Config")
|
||||
if nil != err {
|
||||
logging.Logger().Error(err)
|
||||
return nil
|
||||
}
|
||||
config, ok := _config.(*config.Config)
|
||||
if !ok {
|
||||
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_config))
|
||||
logging.Logger().Errorf("Cannot convert [%s]%s to Config type", pkg, n)
|
||||
return nil
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
1
client/central/data.go
Normal file
1
client/central/data.go
Normal file
|
@ -0,0 +1 @@
|
|||
package central
|
1
client/central/probe.go
Normal file
1
client/central/probe.go
Normal file
|
@ -0,0 +1 @@
|
|||
package central
|
18
config.json
18
config.json
|
@ -4,28 +4,16 @@
|
|||
"apiKey": "52abd6fd57e511e7ac52080027658d13"
|
||||
},
|
||||
"central": {
|
||||
"host": "127.0.0.1",
|
||||
"port": 19091,
|
||||
"address": "127.0.0.1:19091",
|
||||
"connector": {
|
||||
"name": "",
|
||||
"network": "",
|
||||
"address": "",
|
||||
"concurrency": 0,
|
||||
"keepAlive": 0,
|
||||
"handshakeTimeout": 0,
|
||||
"reconnectInterval": 5,
|
||||
"reconnectTryTime": 10,
|
||||
"maxMessageSize": 4096,
|
||||
"readBufferSize": 4096,
|
||||
"writeBufferSize": 4096,
|
||||
"readTimeout": 0,
|
||||
"writeTimeout": 0,
|
||||
"pongTimeout": 60,
|
||||
"pingTimeout": 10,
|
||||
"pingPeriod": 9,
|
||||
"enableCompression": false,
|
||||
"url": "",
|
||||
"subprotocols": null
|
||||
"pingPeriod": 9
|
||||
},
|
||||
"proxy": {
|
||||
"host": "",
|
||||
|
@ -36,7 +24,7 @@
|
|||
}
|
||||
},
|
||||
"probe": {
|
||||
"key": "7691f1a13f1111e8b0460242ac120002"
|
||||
"key": "933a9ccf3fc111e8b0460242ac120002"
|
||||
},
|
||||
"paths": {
|
||||
"root": "/project/overFlow/probe"
|
||||
|
|
9
main.go
9
main.go
|
@ -9,8 +9,8 @@ import (
|
|||
"time"
|
||||
|
||||
"git.loafle.net/commons/configuration-go"
|
||||
cdr "git.loafle.net/commons/di-go/registry"
|
||||
"git.loafle.net/commons/logging-go"
|
||||
|
||||
occi "git.loafle.net/overflow/commons-go/core/interfaces"
|
||||
ocpc "git.loafle.net/overflow/commons-go/probe/config"
|
||||
"git.loafle.net/overflow/probe/auth"
|
||||
|
@ -35,6 +35,8 @@ func main() {
|
|||
if err := configuration.Load(_config, ocpc.ConfigFileName); nil != err {
|
||||
logging.Logger().Panic(err)
|
||||
}
|
||||
cdr.RegisterResource("Config", _config)
|
||||
cdr.RegisterResource("ConfigDir", *configDir)
|
||||
|
||||
var instance interface{}
|
||||
|
||||
|
@ -47,12 +49,15 @@ func main() {
|
|||
doneChan, err := instance.(occi.EndableStarter).EndableStart()
|
||||
if nil != err {
|
||||
logging.Logger().Panic(err)
|
||||
|
||||
return
|
||||
}
|
||||
err = <-doneChan
|
||||
if nil != err {
|
||||
logging.Logger().Panic(err)
|
||||
}
|
||||
if err := instance.(occi.Stopper).Stop(context.Background()); err != nil {
|
||||
logging.Logger().Errorf("error: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user