1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-01-07 12:59:15 +00:00

added so the method None can be used to only start a procFunc for startup processes

renamed HandlerFunc to Handler

added so you can define requests without a method, and just have a procFunc.

added startup of central key processes

split up configuration flags for central acl's and keys.
This commit is contained in:
postmannen 2025-01-02 11:16:25 +01:00
parent de74c2531e
commit 9dda8ac68d
7 changed files with 73 additions and 55 deletions

View file

@ -143,8 +143,11 @@ type StartProcesses struct {
// Start subscriber for continously delivery of output from cli commands.
StartSubCliCommandCont bool `comment:"Start subscriber for continously delivery of output from cli commands."`
// IsCentralAuth, enable to make this instance take the role as the central auth server
IsCentralAuth bool `comment:"IsCentralAuth, enable to make this instance take the role as the central auth server"`
// IsCentralKey, will make the node the central key handler for public keys.
IsCentralKey bool
// IsCentralAcl, enable to make this instance take the role as the central
// server that holds all the ACL's, and the handling av the ACL's.
IsCentralAcl bool `comment:"IsCentralAcl, enable to make this instance take the role as the central auth server"`
}
// NewConfiguration will return a *Configuration.
@ -192,7 +195,8 @@ func NewConfiguration() *Configuration {
flag.BoolVar(&c.EnableSocket, "enableSocket", CheckEnv("ENABLE_SOCKET", c.EnableSocket).(bool), "true/false, for enabling the creation of ctrl.sock file")
flag.BoolVar(&c.EnableSignatureCheck, "enableSignatureCheck", CheckEnv("ENABLE_SIGNATURE_CHECK", c.EnableSignatureCheck).(bool), "true/false *TESTING* enable signature checking.")
flag.BoolVar(&c.EnableAclCheck, "enableAclCheck", CheckEnv("ENABLE_ACL_CHECK", c.EnableAclCheck).(bool), "true/false *TESTING* enable Acl checking.")
flag.BoolVar(&c.StartProcesses.IsCentralAuth, "isCentralAuth", CheckEnv("IS_CENTRAL_AUTH", c.StartProcesses.IsCentralAuth).(bool), "true/false, *TESTING* is this the central auth server")
flag.BoolVar(&c.StartProcesses.IsCentralKey, "isCentralKey", CheckEnv("IS_CENTRAL_KEY", c.StartProcesses.IsCentralKey).(bool), "true/false, *TESTING* is this the central public key server")
flag.BoolVar(&c.StartProcesses.IsCentralAcl, "isCentralAcl", CheckEnv("IS_CENTRAL_ACL", c.StartProcesses.IsCentralAcl).(bool), "true/false, *TESTING* is this the central acl server")
flag.BoolVar(&c.EnableDebug, "enableDebug", CheckEnv("ENABLE_DEBUG", c.EnableDebug).(bool), "true/false, will enable debug logging so all messages sent to the errorKernel will also be printed to STDERR")
flag.StringVar(&c.LogLevel, "logLevel", CheckEnv("LOG_LEVEL", c.LogLevel).(string), "error/info/warning/debug/none")
flag.BoolVar(&c.LogConsoleTimestamps, "LogConsoleTimestamps", CheckEnv("LOG_CONSOLE_TIMESTAMPS", c.LogConsoleTimestamps).(bool), "true/false for enabling or disabling timestamps when printing errors and information to stderr")
@ -308,7 +312,8 @@ func newConfigurationDefaults() Configuration {
StartSubHttpGet: true,
StartSubTailFile: true,
StartSubCliCommandCont: true,
IsCentralAuth: false,
IsCentralKey: false,
IsCentralAcl: false,
},
}
return c

View file

@ -115,7 +115,7 @@ func newSubject(method Method, node string) Subject {
ma := method.GetMethodsAvailable()
_, ok := ma.CheckIfExists(method)
//mh, ok := ma.Methodhandlers[method]
if !ok {
if !ok && method != None {
log.Printf("error: newSubject: no Event type specified for the method: %v\n", method)
os.Exit(1)
}

View file

@ -535,7 +535,7 @@ func executeHandler(p process, message Message, thisNode string) {
}
switch {
case !runAsScheduled:
case !runAsScheduled && p.handler != nil:
go func() {
_, err = p.handler(p, message, thisNode)

View file

@ -132,7 +132,8 @@ func (p *processes) Start(proc process) {
}
if proc.configuration.StartProcesses.EnableKeyUpdates {
proc.startup.startProcess(proc, KeysRequestUpdate, procFuncKeysRequestUpdate)
// The key update on the client is only a proc func that publish requests.
proc.startup.startProcess(proc, None, procFuncKeysRequestUpdate)
proc.startup.startProcess(proc, KeysDeliverUpdate, nil)
}
@ -142,7 +143,13 @@ func (p *processes) Start(proc process) {
proc.startup.startProcess(proc, AclDeliverUpdate, nil)
}
if proc.configuration.StartProcesses.IsCentralAuth {
if proc.configuration.StartProcesses.IsCentralKey {
proc.startup.startProcess(proc, KeysRequestUpdate, nil)
proc.startup.startProcess(proc, KeysAllow, nil)
proc.startup.startProcess(proc, KeysDelete, nil)
}
if proc.configuration.StartProcesses.IsCentralAcl {
proc.startup.startProcess(proc, KeysRequestUpdate, nil)
proc.startup.startProcess(proc, KeysAllow, nil)
proc.startup.startProcess(proc, KeysDelete, nil)

View file

@ -124,7 +124,8 @@ const (
// REQPublicKey will get the public ed25519 key from a node.
PublicKey Method = "publicKey"
// REQKeysRequestUpdate will get all the public keys from central if an update is available.
// REQKeysRequestUpdate will receive all the messages of the current hash of all public keys
// a node have stored, and send out an update if needed..
KeysRequestUpdate Method = "keysRequestUpdate"
// REQKeysDeliverUpdate will deliver the public from central to a node.
KeysDeliverUpdate Method = "keysDeliverUpdate"
@ -162,7 +163,7 @@ const (
AclImport = "aclImport"
)
type HandlerFunc func(proc process, message Message, node string) ([]byte, error)
type Handler func(proc process, message Message, node string) ([]byte, error)
// The mapping of all the method constants specified, what type
// it references.
@ -173,50 +174,51 @@ type HandlerFunc func(proc process, message Message, node string) ([]byte, error
func (m Method) GetMethodsAvailable() MethodsAvailable {
ma := MethodsAvailable{
Methodhandlers: map[Method]HandlerFunc{
Initial: HandlerFunc(methodInitial),
OpProcessList: HandlerFunc(methodOpProcessList),
OpProcessStart: HandlerFunc(methodOpProcessStart),
OpProcessStop: HandlerFunc(methodOpProcessStop),
CliCommand: HandlerFunc(methodCliCommand),
CliCommandCont: HandlerFunc(methodCliCommandCont),
Console: HandlerFunc(methodConsole),
FileAppend: HandlerFunc(methodFileAppend),
File: HandlerFunc(methodToFile),
CopySrc: HandlerFunc(methodCopySrc),
CopyDst: HandlerFunc(methodCopyDst),
SUBCopySrc: HandlerFunc(methodSUB),
SUBCopyDst: HandlerFunc(methodSUB),
Hello: HandlerFunc(methodHello),
Methodhandlers: map[Method]Handler{
Initial: Handler(methodInitial),
OpProcessList: Handler(methodOpProcessList),
OpProcessStart: Handler(methodOpProcessStart),
OpProcessStop: Handler(methodOpProcessStop),
CliCommand: Handler(methodCliCommand),
CliCommandCont: Handler(methodCliCommandCont),
Console: Handler(methodConsole),
FileAppend: Handler(methodFileAppend),
File: Handler(methodToFile),
CopySrc: Handler(methodCopySrc),
CopyDst: Handler(methodCopyDst),
SUBCopySrc: Handler(methodSUB),
SUBCopyDst: Handler(methodSUB),
Hello: Handler(methodHello),
// The hello publisher will not subscribe for messages, it will
// only start a procFunc, so we we don't need a handler with a method,
// so we set it to nil.
HelloPublisher: HandlerFunc(nil),
ErrorLog: HandlerFunc(methodErrorLog),
HttpGet: HandlerFunc(methodHttpGet),
HttpGetScheduled: HandlerFunc(methodHttpGetScheduled),
TailFile: HandlerFunc(methodTailFile),
PublicKey: HandlerFunc(methodPublicKey),
KeysRequestUpdate: HandlerFunc(methodKeysRequestUpdate),
KeysDeliverUpdate: HandlerFunc(methodKeysReceiveUpdate),
KeysAllow: HandlerFunc(methodKeysAllow),
KeysDelete: HandlerFunc(methodKeysDelete),
HelloPublisher: Handler(nil),
ErrorLog: Handler(methodErrorLog),
HttpGet: Handler(methodHttpGet),
HttpGetScheduled: Handler(methodHttpGetScheduled),
TailFile: Handler(methodTailFile),
PublicKey: Handler(methodPublicKey),
AclRequestUpdate: HandlerFunc(methodAclRequestUpdate),
AclDeliverUpdate: HandlerFunc(methodAclDeliverUpdate),
KeysRequestUpdate: Handler(methodKeysRequestUpdate),
KeysDeliverUpdate: Handler(methodKeysDeliverUpdate),
KeysAllow: Handler(methodKeysAllow),
KeysDelete: Handler(methodKeysDelete),
AclAddCommand: HandlerFunc(methodAclAddCommand),
AclDeleteCommand: HandlerFunc(methodAclDeleteCommand),
AclDeleteSource: HandlerFunc(methodAclDeleteSource),
AclGroupNodesAddNode: HandlerFunc(methodAclGroupNodesAddNode),
AclGroupNodesDeleteNode: HandlerFunc(methodAclGroupNodesDeleteNode),
AclGroupNodesDeleteGroup: HandlerFunc(methodAclGroupNodesDeleteGroup),
AclGroupCommandsAddCommand: HandlerFunc(methodAclGroupCommandsAddCommand),
AclGroupCommandsDeleteCommand: HandlerFunc(methodAclGroupCommandsDeleteCommand),
AclGroupCommandsDeleteGroup: HandlerFunc(methodAclGroupCommandsDeleteGroup),
AclExport: HandlerFunc(methodAclExport),
AclImport: HandlerFunc(methodAclImport),
Test: HandlerFunc(methodTest),
AclRequestUpdate: Handler(methodAclRequestUpdate),
AclDeliverUpdate: Handler(methodAclDeliverUpdate),
AclAddCommand: Handler(methodAclAddCommand),
AclDeleteCommand: Handler(methodAclDeleteCommand),
AclDeleteSource: Handler(methodAclDeleteSource),
AclGroupNodesAddNode: Handler(methodAclGroupNodesAddNode),
AclGroupNodesDeleteNode: Handler(methodAclGroupNodesDeleteNode),
AclGroupNodesDeleteGroup: Handler(methodAclGroupNodesDeleteGroup),
AclGroupCommandsAddCommand: Handler(methodAclGroupCommandsAddCommand),
AclGroupCommandsDeleteCommand: Handler(methodAclGroupCommandsDeleteCommand),
AclGroupCommandsDeleteGroup: Handler(methodAclGroupCommandsDeleteGroup),
AclExport: Handler(methodAclExport),
AclImport: Handler(methodAclImport),
Test: Handler(methodTest),
},
}
@ -226,7 +228,7 @@ func (m Method) GetMethodsAvailable() MethodsAvailable {
// getHandler will check the methodsAvailable map, and return the
// method handler for the method given
// as input argument.
func (m Method) getHandler(method Method) HandlerFunc {
func (m Method) getHandler(method Method) Handler {
ma := m.GetMethodsAvailable()
mh, _ := ma.CheckIfExists(method)
// mh := ma.Methodhandlers[method]
@ -274,13 +276,13 @@ func methodSUB(proc process, message Message, node string) ([]byte, error) {
// MethodsAvailable holds a map of all the different method types and the
// associated handler to that method type.
type MethodsAvailable struct {
Methodhandlers map[Method]HandlerFunc
Methodhandlers map[Method]Handler
}
// Check if exists will check if the Method is defined. If true the bool
// value will be set to true, and the methodHandler function for that type
// will be returned.
func (ma MethodsAvailable) CheckIfExists(m Method) (HandlerFunc, bool) {
func (ma MethodsAvailable) CheckIfExists(m Method) (Handler, bool) {
// First check if it is a sub process.
if strings.HasPrefix(string(m), "sub") {
// Strip of the uuid after the method name.

View file

@ -46,7 +46,10 @@ func methodPublicKey(proc process, message Message, node string) ([]byte, error)
// ----
// Handler to get all the public ed25519 keys from a central server.
// methodKeysRequestUpdate.
// The nodes publish messages with the hash of all the public keys it currently
// have stored. If the hash is different than the one we currently have on central
// we send out an update with all the current keys to the node.
func methodKeysRequestUpdate(proc process, message Message, node string) ([]byte, error) {
// Get a context with the timeout specified in message.MethodTimeout.
@ -57,6 +60,7 @@ func methodKeysRequestUpdate(proc process, message Message, node string) ([]byte
// time a node had done an update.
ctx, _ := getContextForMethodTimeout(proc.ctx, message)
_ = node
proc.processes.wg.Add(1)
go func() {
@ -163,7 +167,7 @@ func procFuncKeysRequestUpdate(ctx context.Context, proc process, procFuncCh cha
// ----
// Handler to receive the public keys from a central server.
func methodKeysReceiveUpdate(proc process, message Message, node string) ([]byte, error) {
func methodKeysDeliverUpdate(proc process, message Message, node string) ([]byte, error) {
// Get a context with the timeout specified in message.MethodTimeout.
ctx, _ := getContextForMethodTimeout(proc.ctx, message)

View file

@ -77,7 +77,7 @@ func newServerForTesting(addressAndPort string, testFolder string) (*server, *Co
conf.SubscribersDataFolder = testFolder
conf.DatabaseFolder = testFolder
conf.StartProcesses.IsCentralErrorLogger = true
conf.StartProcesses.IsCentralAuth = true
conf.StartProcesses.IsCentralAcl = true
conf.EnableDebug = false
conf.LogLevel = "none"