From 38389c477898dcddf59d54493c779f73ee80d242 Mon Sep 17 00:00:00 2001 From: postmannen Date: Fri, 10 Jan 2025 08:35:07 +0100 Subject: [PATCH] changed logError to use structured logging --- central_auth_acl_handling.go | 101 ++++++++++++----------------------- errorkernel.go | 7 ++- message_readers.go | 18 +++---- node_auth.go | 16 ++---- requests_copy.go | 3 +- server.go | 6 +-- 6 files changed, 52 insertions(+), 99 deletions(-) diff --git a/central_auth_acl_handling.go b/central_auth_acl_handling.go index de9e584..d3d2d96 100644 --- a/central_auth_acl_handling.go +++ b/central_auth_acl_handling.go @@ -91,21 +91,18 @@ func newSchemaMain(configuration *Configuration, errorKernel *errorKernel) *sche fh, err := os.Open(s.ACLMapFilePath) if err != nil { - er := fmt.Errorf("error: newSchemaMain: failed to open file for reading %v: %v", s.ACLMapFilePath, err) - errorKernel.logError(er) + errorKernel.logError("newSchemaMain: failed to open file for reading", "file", s.ACLMapFilePath, "error", err) } b, err := io.ReadAll(fh) if err != nil { - er := fmt.Errorf("error: newSchemaMain: failed to ReadAll file %v: %v", s.ACLMapFilePath, err) - errorKernel.logError(er) + errorKernel.logError("newSchemaMain: failed to ReadAll", "file", s.ACLMapFilePath, "error", err) } // Unmarshal the data read from disk. err = json.Unmarshal(b, &s.ACLMap) if err != nil { - er := fmt.Errorf("error: newSchemaMain: failed to unmarshal content from file %v: %v", s.ACLMapFilePath, err) - errorKernel.logError(er) + errorKernel.logError("newSchemaMain: failed to unmarshal content from", "file", s.ACLMapFilePath, "error", err) } // Generate the aclGenerated map happens in the function where this function is called. @@ -224,66 +221,61 @@ func (c *centralAuth) aclAddCommand(host Node, source Node, cmd command) { // err := a.generateJSONForHostOrGroup(n) err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: addCommandForFromNode: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("generateACLsForAllNodes", "error", err) } - - // fmt.Printf(" * DEBUG: aclNodeFromnodeCommandAdd: a.schemaMain.ACLMap=%v\n", a.schemaMain.ACLMap) - } // aclDeleteCommand will delete the specified command from the fromnode. -func (c *centralAuth) aclDeleteCommand(host Node, source Node, cmd command) error { +func (c *centralAuth) aclDeleteCommand(host Node, source Node, cmd command) { c.accessLists.schemaMain.mu.Lock() defer c.accessLists.schemaMain.mu.Unlock() // Check if node exists in map. if _, ok := c.accessLists.schemaMain.ACLMap[host]; !ok { - return fmt.Errorf("authSchema: no such node=%v to delete on in schema exists", host) + c.errorKernel.logError("authSchema: no such node to delete on in schema exists", "node", host) + return } if _, ok := c.accessLists.schemaMain.ACLMap[host][source]; !ok { - return fmt.Errorf("authSchema: no such fromnode=%v to delete on in schema for node=%v exists", source, host) + c.errorKernel.logError("authSchema: no such fromnode to delete on in schema for node exists", "fromnode", source, "node", host) + return } if _, ok := c.accessLists.schemaMain.ACLMap[host][source][cmd]; !ok { - return fmt.Errorf("authSchema: no such command=%v from fromnode=%v to delete on in schema for node=%v exists", cmd, source, host) + c.errorKernel.logError("authSchema: no such command=%v from fromnode=%v to delete on in schema for node=%v exists", "command", cmd, "fromNode", source, "node", host) + return } delete(c.accessLists.schemaMain.ACLMap[host][source], cmd) err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: aclNodeFromNodeCommandDelete: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("error: aclNodeFromNodeCommandDelete", "error", err) } - - return nil } // aclDeleteSource will delete specified source node and all commands specified for it. -func (c *centralAuth) aclDeleteSource(host Node, source Node) error { +func (c *centralAuth) aclDeleteSource(host Node, source Node) { c.accessLists.schemaMain.mu.Lock() defer c.accessLists.schemaMain.mu.Unlock() // Check if node exists in map. if _, ok := c.accessLists.schemaMain.ACLMap[host]; !ok { - return fmt.Errorf("authSchema: no such node=%v to delete on in schema exists", host) + c.errorKernel.logError("aclDeleteSource: no such node to delete on in schema exists", "node", host) + return } if _, ok := c.accessLists.schemaMain.ACLMap[host][source]; !ok { - return fmt.Errorf("authSchema: no such fromnode=%v to delete on in schema for node=%v exists", source, host) + c.errorKernel.logError("authSchema: no such fromNode to delete on in schema for node exists", "fromNode", source, "node", host) + return } delete(c.accessLists.schemaMain.ACLMap[host], source) err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: aclNodeFromnodeDelete: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("generateACLsForAllNodes", "error", err) } - - return nil } // generateACLsForAllNodes will generate a json encoded representation of the node specific @@ -298,8 +290,7 @@ func (c *centralAuth) generateACLsForAllNodes() error { func() { fh, err := os.OpenFile(c.accessLists.schemaMain.ACLMapFilePath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660) if err != nil { - er := fmt.Errorf("error: generateACLsForAllNodes: opening file for writing: %v, err: %v", c.accessLists.schemaMain.ACLMapFilePath, err) - c.errorKernel.logError(er) + c.errorKernel.logError("generateACLsForAllNodes: opening file for writing", "file", c.accessLists.schemaMain.ACLMapFilePath, "error", err) return } defer fh.Close() @@ -310,8 +301,7 @@ func (c *centralAuth) generateACLsForAllNodes() error { enc.SetEscapeHTML(false) err = enc.Encode(c.accessLists.schemaMain.ACLMap) if err != nil { - er := fmt.Errorf("error: generateACLsForAllNodes: encoding json to file failed: %v, err: %v", c.accessLists.schemaMain.ACLMapFilePath, err) - c.errorKernel.logError(er) + c.errorKernel.logError("generateACLsForAllNodes: encoding json to file failed", "file", c.accessLists.schemaMain.ACLMapFilePath, "error", err) return } }() @@ -354,8 +344,7 @@ func (c *centralAuth) generateACLsForAllNodes() error { // cbor marshal the data of the ACL map to store for the host node. cb, err := cbor.Marshal(m) if err != nil { - er := fmt.Errorf("error: generateACLsForAllNodes: failed to generate cbor for host in schemaGenerated: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("generateACLsForAllNodes: failed to generate cbor for host in schemaGenerated", "error", err) os.Exit(1) } @@ -365,8 +354,7 @@ func (c *centralAuth) generateACLsForAllNodes() error { b, err := cbor.Marshal(sns) if err != nil { - er := fmt.Errorf("error: generateACLsForAllNodes: failed to generate cbor for hash: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("generateACLsForAllNodes: failed to generate cbor for hash", "error", err) return [32]byte{} } @@ -448,15 +436,8 @@ func (a *accessLists) nodeMapToSlice(host Node) sourceNode { // groupNodesAddNode adds a node to a group. If the group does // not exist it will be created. func (c *centralAuth) groupNodesAddNode(ng nodeGroup, n Node) { - //err := c.accessLists.validator.Var(ng, "startswith=grp_nodes_") - //if err != nil { - // log.Printf("error: group name do not start with grp_nodes_: %v\n", err) - // return - //} - if !strings.HasPrefix(string(ng), "grp_nodes_") { - er := fmt.Errorf("error: group name do not start with grp_nodes_") - c.errorKernel.logError(er) + c.errorKernel.logError("group name do not start with grp_nodes_", "nodeGroup", ng) return } @@ -468,12 +449,9 @@ func (c *centralAuth) groupNodesAddNode(ng nodeGroup, n Node) { c.accessLists.schemaMain.NodeGroupMap[ng][n] = struct{}{} - // fmt.Printf(" * groupNodesAddNode: After adding to group node looks like: %+v\n", a.schemaMain.NodeGroupMap) - err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: groupNodesAddNode: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("groupNodesAddNode: generateACLsForAllNodes", "error", err) } } @@ -483,8 +461,7 @@ func (c *centralAuth) groupNodesDeleteNode(ng nodeGroup, n Node) { c.accessLists.schemaMain.mu.Lock() defer c.accessLists.schemaMain.mu.Unlock() if _, ok := c.accessLists.schemaMain.NodeGroupMap[ng][n]; !ok { - er := fmt.Errorf("info: no such node with name=%v found in group=%v", ng, n) - c.errorKernel.logError(er) + c.errorKernel.logError("groupNodesDeleteNode: no such node found in group", "node", ng, "group", n) return } @@ -494,8 +471,7 @@ func (c *centralAuth) groupNodesDeleteNode(ng nodeGroup, n Node) { err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: groupNodesDeleteNode: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("groupNodesDeleteNode", "error", err) } } @@ -505,8 +481,8 @@ func (c *centralAuth) groupNodesDeleteGroup(ng nodeGroup) { c.accessLists.schemaMain.mu.Lock() defer c.accessLists.schemaMain.mu.Unlock() if _, ok := c.accessLists.schemaMain.NodeGroupMap[ng]; !ok { - er := fmt.Errorf("info: no such group found: %v", ng) - c.errorKernel.logError(er) + c.errorKernel.logError("groupCommandDeleteGroup: no such group found", "group", ng) + return } @@ -516,8 +492,7 @@ func (c *centralAuth) groupNodesDeleteGroup(ng nodeGroup) { err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: groupNodesDeleteGroup: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("groupNodesDeleteGroup: generateACLsForAllNodes", "error", err) } } @@ -534,8 +509,7 @@ func (c *centralAuth) groupCommandsAddCommand(cg commandGroup, cmd command) { // } if !strings.HasPrefix(string(cg), "grp_commands_") { - er := fmt.Errorf("error: group name do not start with grp_commands_") - c.errorKernel.logError(er) + c.errorKernel.logError("groupCommandsAddCommand: group name do not start with grp_commands_", "commandGroup", cg) return } @@ -551,8 +525,7 @@ func (c *centralAuth) groupCommandsAddCommand(cg commandGroup, cmd command) { err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: groupCommandsAddCommand: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("groupCommandsAddCommand", "error", err) } } @@ -562,8 +535,7 @@ func (c *centralAuth) groupCommandsDeleteCommand(cg commandGroup, cmd command) { c.accessLists.schemaMain.mu.Lock() defer c.accessLists.schemaMain.mu.Unlock() if _, ok := c.accessLists.schemaMain.CommandGroupMap[cg][cmd]; !ok { - er := fmt.Errorf("info: no such command with name=%v found in group=%v", c, cg) - c.errorKernel.logError(er) + c.errorKernel.logError("groupCommandsDeleteCommand: no such command with namefound in group", "name", c, "group", cg) return } @@ -573,8 +545,7 @@ func (c *centralAuth) groupCommandsDeleteCommand(cg commandGroup, cmd command) { err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: groupCommandsDeleteCommand: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("groupCommandsDeleteCommand", "error", err) } } @@ -584,8 +555,7 @@ func (c *centralAuth) groupCommandDeleteGroup(cg commandGroup) { c.accessLists.schemaMain.mu.Lock() defer c.accessLists.schemaMain.mu.Unlock() if _, ok := c.accessLists.schemaMain.CommandGroupMap[cg]; !ok { - er := fmt.Errorf("info: no such group found: %v", cg) - c.errorKernel.logError(er) + c.errorKernel.logError("groupCommandDeleteGroup: no such group found", "group", cg) return } @@ -595,8 +565,7 @@ func (c *centralAuth) groupCommandDeleteGroup(cg commandGroup) { err := c.generateACLsForAllNodes() if err != nil { - er := fmt.Errorf("error: groupCommandDeleteGroup: %v", err) - c.errorKernel.logError(er) + c.errorKernel.logError("groupCommandDeleteGroup: generateACLsForAllNodes", "error", err) } } diff --git a/errorkernel.go b/errorkernel.go index e43094b..5eb4e2e 100644 --- a/errorkernel.go +++ b/errorkernel.go @@ -256,7 +256,7 @@ func (e *errorKernel) errSend(proc process, msg Message, err error, logLevel log switch logLevel { case logError: - e.logError(err) + e.logError("error", err) case logInfo: e.logInfo(err) case logWarning: @@ -281,9 +281,8 @@ func (e *errorKernel) infoSend(proc process, msg Message, err error) { e.errorCh <- ev } -func (e *errorKernel) logError(err error) { - slog.Error("error", err) - +func (e *errorKernel) logError(msg string, args ...any) { + slog.Error(msg, args...) } func (e *errorKernel) logInfo(err error) { diff --git a/message_readers.go b/message_readers.go index 76bfa69..fa183a3 100644 --- a/message_readers.go +++ b/message_readers.go @@ -319,16 +319,14 @@ func (s *server) readFolder() { if _, err := os.Stat(s.configuration.ReadFolder); os.IsNotExist(err) { err := os.MkdirAll(s.configuration.ReadFolder, 0770) if err != nil { - er := fmt.Errorf("error: failed to create readfolder folder: %v", err) - s.errorKernel.logError(er) + s.errorKernel.logError("readfolder: failed to create readfolder", "error", err) os.Exit(1) } } watcher, err := fsnotify.NewWatcher() if err != nil { - er := fmt.Errorf("main: failed to create new logWatcher: %v", err) - s.errorKernel.logError(er) + s.errorKernel.logError("readfolder: failed to create new logWatcher", "error", err) os.Exit(1) } @@ -427,8 +425,7 @@ func (s *server) readFolder() { // Add a path. err = watcher.Add(s.configuration.ReadFolder) if err != nil { - er := fmt.Errorf("startLogsWatcher: failed to add watcher: %v", err) - s.errorKernel.logError(er) + s.errorKernel.logError("readFolder: start logs watcher: failed to add watcher", "error", err) os.Exit(1) } } @@ -440,8 +437,7 @@ func (s *server) readFolder() { func (s *server) readTCPListener() { ln, err := net.Listen("tcp", s.configuration.TCPListener) if err != nil { - er := fmt.Errorf("error: readTCPListener: failed to start tcp listener: %v", err) - s.errorKernel.logError(er) + s.errorKernel.logError("readTCPListener: failed to start tcp listener", "error", err) os.Exit(1) } // Loop, and wait for new connections. @@ -547,8 +543,7 @@ func (s *server) readHttpListener() { go func() { n, err := net.Listen("tcp", s.configuration.HTTPListener) if err != nil { - er := fmt.Errorf("error: startMetrics: failed to open prometheus listen port: %v", err) - s.errorKernel.logError(er) + s.errorKernel.logError("readHttpListener: failed to open listen port", "error", err) os.Exit(1) } mux := http.NewServeMux() @@ -556,8 +551,7 @@ func (s *server) readHttpListener() { err = http.Serve(n, mux) if err != nil { - er := fmt.Errorf("error: startMetrics: failed to start http.Serve: %v", err) - s.errorKernel.logError(er) + s.errorKernel.logError("readHttpListener: failed to start http.Serve", "error", err) os.Exit(1) } }() diff --git a/node_auth.go b/node_auth.go index e3120db..017ca96 100644 --- a/node_auth.go +++ b/node_auth.go @@ -54,8 +54,7 @@ func newNodeAuth(configuration *Configuration, errorKernel *errorKernel) *nodeAu err := n.loadSigningKeys() if err != nil { - er := fmt.Errorf("newNodeAuth: %v", err) - errorKernel.logError(er) + errorKernel.logError("newNodeAuth", "error", err) os.Exit(1) } @@ -96,9 +95,7 @@ func newNodeAcl(c *Configuration, errorKernel *errorKernel) *nodeAcl { err := n.loadFromFile() if err != nil { - er := fmt.Errorf("error: newNodeAcl: loading acl's from file: %v", err) - errorKernel.logError(er) - // os.Exit(1) + errorKernel.logError("newNodeAcl: loading acl's from file", "file", err) } return &n @@ -202,9 +199,7 @@ func newPublicKeys(c *Configuration, errorKernel *errorKernel) *publicKeys { err := p.loadFromFile() if err != nil { - er := fmt.Errorf("error: newPublicKeys: loading public keys from file: %v", err) - errorKernel.logError(er) - // os.Exit(1) + errorKernel.logError("newPublicKeys: loading public keys from file", "file", err) } return &p @@ -435,7 +430,7 @@ func (n *nodeAuth) verifySignature(m Message) bool { }() if err != nil { - n.errorKernel.logError(err) + n.errorKernel.logError("verifySignature", "error", err) } er := fmt.Errorf("info: verifySignature, result: %v, fromNode: %v, method: %v", ok, m.FromNode, m.Method) @@ -461,8 +456,7 @@ func (n *nodeAuth) verifyAcl(m Message) bool { cmdMap, ok := n.nodeAcl.aclAndHash.Acl[m.FromNode] if !ok { - er := fmt.Errorf("verifyAcl: The fromNode=%v was not found in the acl", m.FromNode) - n.errorKernel.logError(er) + n.errorKernel.logError("verifyAcl: The fromNode was not found in the acl", "fromNode", m.FromNode) return false } diff --git a/requests_copy.go b/requests_copy.go index 40f1ef6..62ecc4f 100644 --- a/requests_copy.go +++ b/requests_copy.go @@ -143,8 +143,7 @@ func methodCopySrc(proc process, message Message, node string) ([]byte, error) { var err error folderPermission, err = strconv.ParseUint(message.MethodArgs[5], 8, 32) if err != nil { - er := fmt.Errorf("methodCopySrc: failed to parse uint, %v", err) - proc.errorKernel.logError(er) + proc.errorKernel.logError("methodCopySrc: failed to parse uint", "error", err) } er := fmt.Errorf("info: FolderPermission defined in message for socket: %v, converted = %v", message.MethodArgs[5], folderPermission) diff --git a/server.go b/server.go index 0783ddc..9ac0d0c 100644 --- a/server.go +++ b/server.go @@ -576,16 +576,14 @@ func (s *server) routeMessagesToPublisherProcess() { fh, err := os.Open(filePathToOpen) if err != nil { - er := fmt.Errorf("error: routeMessagesToPublisherProcess: failed to open file given as CTRL_FILE argument: %v", err) - s.errorKernel.logError(er) + s.errorKernel.logError("routeMessagesToPublisherProcess: failed to open file given as CTRL_FILE argument", "error", err) return } defer fh.Close() b, err := io.ReadAll(fh) if err != nil { - er := fmt.Errorf("error: routeMessagesToPublisherProcess: failed to read file %v given as CTRL_FILE argument: %v", filePathToOpen, err) - s.errorKernel.logError(er) + s.errorKernel.logError("routeMessagesToPublisherProcess: failed to read file given as CTRL_FILE argument", "file", filePathToOpen, "error", err) return }