From c56be285a5a682425a2aed18b4146813180d62af Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sat, 14 Dec 2024 14:42:43 +0100 Subject: [PATCH] replace fnv with sha256 Signed-off-by: Nicola Murino --- internal/dataprovider/node.go | 7 ++++--- internal/vfs/sftpfs.go | 27 ++++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/internal/dataprovider/node.go b/internal/dataprovider/node.go index 5bdf0c25..d35370bc 100644 --- a/internal/dataprovider/node.go +++ b/internal/dataprovider/node.go @@ -17,10 +17,11 @@ package dataprovider import ( "bytes" "context" + "crypto/sha256" + "encoding/hex" "encoding/json" "errors" "fmt" - "hash/fnv" "io" "net/http" "strconv" @@ -108,12 +109,12 @@ func (n *NodeData) validate() error { } func (n *NodeData) getNodeName() string { - h := fnv.New64a() + h := sha256.New() var b bytes.Buffer b.WriteString(fmt.Sprintf("%s:%d", n.Host, n.Port)) h.Write(b.Bytes()) - return strconv.FormatUint(h.Sum64(), 10) + return hex.EncodeToString(h.Sum(nil)) } // Node defines a cluster node diff --git a/internal/vfs/sftpfs.go b/internal/vfs/sftpfs.go index 31554c6e..2ea85c01 100644 --- a/internal/vfs/sftpfs.go +++ b/internal/vfs/sftpfs.go @@ -18,9 +18,10 @@ import ( "bufio" "bytes" "crypto/rsa" + "crypto/sha256" + "encoding/hex" "errors" "fmt" - "hash/fnv" "io" "io/fs" "net" @@ -283,8 +284,8 @@ func (c *SFTPFsConfig) ValidateAndEncryptCredentials(additionalData string) erro } // getUniqueID returns an hash of the settings used to connect to the SFTP server -func (c *SFTPFsConfig) getUniqueID(partition int) uint64 { - h := fnv.New64a() +func (c *SFTPFsConfig) getUniqueID(partition int) string { + h := sha256.New() var b bytes.Buffer b.WriteString(c.Endpoint) @@ -301,7 +302,7 @@ func (c *SFTPFsConfig) getUniqueID(partition int) uint64 { b.WriteString(strconv.Itoa(partition)) h.Write(b.Bytes()) - return h.Sum64() + return hex.EncodeToString(h.Sum(nil)) } // SFTPFs is a Fs implementation for SFTP backends @@ -1145,13 +1146,13 @@ func (c *sftpConnection) GetLastActivity() time.Time { type sftpConnectionsCache struct { scheduler *cron.Cron sync.RWMutex - items map[uint64]*sftpConnection + items map[string]*sftpConnection } func newSFTPConnectionCache() *sftpConnectionsCache { c := &sftpConnectionsCache{ scheduler: cron.New(cron.WithLocation(time.UTC), cron.WithLogger(cron.DiscardLogger)), - items: make(map[uint64]*sftpConnection), + items: make(map[string]*sftpConnection), } _, err := c.scheduler.AddFunc("@every 1m", c.Cleanup) util.PanicOnError(err) @@ -1166,13 +1167,13 @@ func (c *sftpConnectionsCache) Get(config *SFTPFsConfig, sessionID string) (*sft c.Lock() defer c.Unlock() - var oldKey uint64 + var oldKey string for { if val, ok := c.items[key]; ok { activeSessions := val.ActiveSessions() if activeSessions < maxSessionsPerConnection || key == oldKey { logger.Debug(logSenderSFTPCache, "", - "reusing connection for session ID %q, key: %d, active sessions %d, active connections: %d", + "reusing connection for session ID %q, key %s, active sessions %d, active connections: %d", sessionID, key, activeSessions+1, len(c.items)) val.AddSession(sessionID) return val, nil @@ -1181,7 +1182,7 @@ func (c *sftpConnectionsCache) Get(config *SFTPFsConfig, sessionID string) (*sft oldKey = key key = config.getUniqueID(partition) logger.Debug(logSenderSFTPCache, "", - "connection full, generated new key for partition: %d, active sessions: %d, key: %d, old key: %d", + "connection full, generated new key for partition: %d, active sessions: %d, key: %s, old key: %s", partition, activeSessions, oldKey, key) } else { conn := newSFTPConnection(config, sessionID) @@ -1192,20 +1193,20 @@ func (c *sftpConnectionsCache) Get(config *SFTPFsConfig, sessionID string) (*sft conn.signer = signer c.items[key] = conn logger.Debug(logSenderSFTPCache, "", - "adding new connection for session ID %q, partition: %d, key: %d, active connections: %d", + "adding new connection for session ID %q, partition: %d, key: %s, active connections: %d", sessionID, partition, key, len(c.items)) return conn, nil } } } -func (c *sftpConnectionsCache) Remove(key uint64) { +func (c *sftpConnectionsCache) Remove(key string) { c.Lock() defer c.Unlock() if conn, ok := c.items[key]; ok { delete(c.items, key) - logger.Debug(logSenderSFTPCache, "", "removed connection with key %d, active connections: %d", key, len(c.items)) + logger.Debug(logSenderSFTPCache, "", "removed connection with key %s, active connections: %d", key, len(c.items)) defer conn.Close() } @@ -1218,7 +1219,7 @@ func (c *sftpConnectionsCache) Cleanup() { if val := conn.GetLastActivity(); val.Before(time.Now().Add(-30 * time.Second)) { logger.Debug(conn.logSender, "", "removing inactive connection, last activity %s", val) - defer func(key uint64) { + defer func(key string) { c.Remove(key) }(k) }