From c3e442d4974f96264264cca2738570535637936c Mon Sep 17 00:00:00 2001 From: postmannen Date: Sat, 21 May 2022 07:09:35 +0200 Subject: [PATCH] Added initial REQ AclExport --- processes.go | 8 ++++++ requests.go | 5 ++++ requests_acl.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/processes.go b/processes.go index fb00d20..05860c7 100644 --- a/processes.go +++ b/processes.go @@ -182,6 +182,7 @@ func (p *processes) Start(proc process) { proc.startup.subREQAclGroupCommandsAddCommand(proc) proc.startup.subREQAclGroupCommandsDeleteCommand(proc) proc.startup.subREQAclGroupCommandsDeleteGroup(proc) + proc.startup.subREQAclExport(proc) } if proc.configuration.StartSubREQPublicKeysToNode { @@ -447,6 +448,13 @@ func (s startup) subREQAclGroupCommandsDeleteGroup(p process) { go proc.spawnWorker() } +func (s startup) subREQAclExport(p process) { + log.Printf("Starting Acl export subscriber: %#v\n", p.node) + sub := newSubject(REQAclExport, string(p.node)) + proc := newProcess(p.ctx, s.server, sub, processKindSubscriber, nil) + go proc.spawnWorker() +} + func (s startup) subREQToConsole(p process) { log.Printf("Starting Text To Console subscriber: %#v\n", p.node) sub := newSubject(REQToConsole, string(p.node)) diff --git a/requests.go b/requests.go index e4baf0b..7fcf395 100644 --- a/requests.go +++ b/requests.go @@ -144,6 +144,8 @@ const ( REQAclGroupCommandsDeleteCommand = "REQAclGroupCommandsDeleteCommand" // REQAclGroupCommandsDeleteGroup REQAclGroupCommandsDeleteGroup = "REQAclGroupCommandsDeleteGroup" + // REQAclExport + REQAclExport = "REQAclExport" ) // The mapping of all the method constants specified, what type @@ -266,6 +268,9 @@ func (m Method) GetMethodsAvailable() MethodsAvailable { REQAclGroupCommandsDeleteGroup: methodREQAclGroupCommandsDeleteGroup{ event: EventACK, }, + REQAclExport: methodREQAclExport{ + event: EventACK, + }, }, } diff --git a/requests_acl.go b/requests_acl.go index cfd696f..aab2eac 100644 --- a/requests_acl.go +++ b/requests_acl.go @@ -633,3 +633,76 @@ func (m methodREQAclGroupCommandsDeleteGroup) handler(proc process, message Mess ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID)) return ackMsg, nil } + +// --- + +type methodREQAclExport struct { + event Event +} + +func (m methodREQAclExport) getKind() Event { + return m.event +} + +func (m methodREQAclExport) handler(proc process, message Message, node string) ([]byte, error) { + inf := fmt.Errorf("<--- methodREQAclExport received from: %v, containing: %v", message.FromNode, message.MethodArgs) + proc.errorKernel.logConsoleOnlyIfDebug(inf, proc.configuration) + + proc.processes.wg.Add(1) + go func() { + defer proc.processes.wg.Done() + + // switch { + // case len(message.MethodArgs) < 1: + // er := fmt.Errorf("error: methodREQAclImport: got <1 number methodArgs, want 1") + // proc.errorKernel.errSend(proc, message, er) + // + // return + // } + + // Get a context with the timeout specified in message.MethodTimeout. + ctx, cancel := getContextForMethodTimeout(proc.ctx, message) + + outCh := make(chan []byte) + errCh := make(chan error) + + proc.processes.wg.Add(1) + go func() { + defer proc.processes.wg.Done() + + out, err := proc.centralAuth.accessLists.exportACLs() + if err != nil { + errCh <- fmt.Errorf("error: methodREQAclExport failed: %v", err) + return + } + + // outString := fmt.Sprintf("Exported acls sent from: %v\n", message.FromNode) + // out := []byte(outString) + + select { + case outCh <- out: + case <-ctx.Done(): + return + } + }() + + select { + case err := <-errCh: + proc.errorKernel.errSend(proc, message, err) + + case <-ctx.Done(): + cancel() + er := fmt.Errorf("error: methodREQAclExport: method timed out: %v", message.MethodArgs) + proc.errorKernel.errSend(proc, message, er) + + case out := <-outCh: + // Prepare and queue for sending a new message with the output + // of the action executed. + newReplyMessage(proc, message, out) + } + + }() + + ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID)) + return ackMsg, nil +}