From eb0c6549c4e5be4cb2ae7c135c468917c998cdd6 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 12 May 2024 18:10:03 +0200 Subject: [PATCH] micro optimization Signed-off-by: Nicola Murino --- go.mod | 12 ++++++------ go.sum | 24 ++++++++++++------------ internal/common/eventmanager.go | 8 ++++---- internal/dataprovider/dataprovider.go | 4 ++-- internal/httpd/webclient.go | 6 +++--- internal/sftpd/server.go | 2 +- internal/util/util.go | 14 ++++++++++++++ 7 files changed, 42 insertions(+), 28 deletions(-) diff --git a/go.mod b/go.mod index aa6316de..e443af0c 100644 --- a/go.mod +++ b/go.mod @@ -10,10 +10,10 @@ require ( github.com/alexedwards/argon2id v1.0.0 github.com/amoghe/go-crypt v0.0.0-20220222110647-20eada5f5964 github.com/aws/aws-sdk-go-v2 v1.26.1 - github.com/aws/aws-sdk-go-v2/config v1.27.12 - github.com/aws/aws-sdk-go-v2/credentials v1.17.12 + github.com/aws/aws-sdk-go-v2/config v1.27.13 + github.com/aws/aws-sdk-go-v2/credentials v1.17.13 github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 - github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.16 + github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.17 github.com/aws/aws-sdk-go-v2/service/marketplacemetering v1.21.5 github.com/aws/aws-sdk-go-v2/service/s3 v1.53.2 github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.28.7 @@ -33,7 +33,7 @@ require ( github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/google/uuid v1.6.0 github.com/hashicorp/go-hclog v1.6.3 - github.com/hashicorp/go-plugin v1.6.0 + github.com/hashicorp/go-plugin v1.6.1 github.com/hashicorp/go-retryablehttp v0.7.6 github.com/jackc/pgx/v5 v5.5.5 github.com/jlaffaye/ftp v0.2.0 @@ -73,7 +73,7 @@ require ( golang.org/x/sys v0.20.0 golang.org/x/term v0.20.0 golang.org/x/time v0.5.0 - google.golang.org/api v0.179.0 + google.golang.org/api v0.180.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) @@ -96,7 +96,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.5 // indirect github.com/aws/aws-sdk-go-v2/service/sso v1.20.6 // indirect - github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.5 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.0 // indirect github.com/aws/smithy-go v1.20.2 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/boombuler/barcode v1.0.1 // indirect diff --git a/go.sum b/go.sum index a86c2732..44cfb9b0 100644 --- a/go.sum +++ b/go.sum @@ -41,14 +41,14 @@ github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+ github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to= github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg= -github.com/aws/aws-sdk-go-v2/config v1.27.12 h1:vq88mBaZI4NGLXk8ierArwSILmYHDJZGJOeAc/pzEVQ= -github.com/aws/aws-sdk-go-v2/config v1.27.12/go.mod h1:IOrsf4IiN68+CgzyuyGUYTpCrtUQTbbMEAtR/MR/4ZU= -github.com/aws/aws-sdk-go-v2/credentials v1.17.12 h1:PVbKQ0KjDosI5+nEdRMU8ygEQDmkJTSHBqPjEX30lqc= -github.com/aws/aws-sdk-go-v2/credentials v1.17.12/go.mod h1:jlWtGFRtKsqc5zqerHZYmKmRkUXo3KPM14YJ13ZEjwE= +github.com/aws/aws-sdk-go-v2/config v1.27.13 h1:WbKW8hOzrWoOA/+35S5okqO/2Ap8hkkFUzoW8Hzq24A= +github.com/aws/aws-sdk-go-v2/config v1.27.13/go.mod h1:XLiyiTMnguytjRER7u5RIkhIqS8Nyz41SwAWb4xEjxs= +github.com/aws/aws-sdk-go-v2/credentials v1.17.13 h1:XDCJDzk/u5cN7Aple7D/MiAhx1Rjo/0nueJ0La8mRuE= +github.com/aws/aws-sdk-go-v2/credentials v1.17.13/go.mod h1:FMNcjQrmuBYvOTZDtOLCIu0esmxjF7RuA/89iSXWzQI= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 h1:FVJ0r5XTHSmIHJV6KuDmdYhEpvlHpiSd38RQWhut5J4= github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1/go.mod h1:zusuAeqezXzAB24LGuzuekqMAEgWkVYukBec3kr3jUg= -github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.16 h1:n4k5rgvy0M748NadpDlGLOQ/KCVbNHCQsfI895wLrt0= -github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.16/go.mod h1:Rd5rkCdq44ZC5rS4CbF3Wc8FiWo7f9brp7qeIwWsJaU= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.17 h1:9b1Os1s11mF5qTIKLgSsyPG810di2+ySSLIIt9bwe9I= +github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.16.17/go.mod h1:9Wp7tDOMhv0+sb/FTRAkbHNQ7abYDnoJRzm5AAtCnTc= github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 h1:aw39xVGeRWlWx9EzGVnhOR4yOjQDHPQ6o6NmBlscyQg= github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5/go.mod h1:FSaRudD0dXiMPK2UjknVwwTYyZMRsHv3TtkabsZih5I= github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 h1:PG1F3OD1szkuQPzDw3CIQsRIrtTlUC3lP84taWzHlq0= @@ -73,8 +73,8 @@ github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.28.7 h1:4cziOtpDwtgcb+wTY github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.28.7/go.mod h1:3Ba++UwWd154xtP4FRX5pUK3Gt4up5sDHCve6kVfE+g= github.com/aws/aws-sdk-go-v2/service/sso v1.20.6 h1:o5cTaeunSpfXiLTIBx5xo2enQmiChtu1IBbzXnfU9Hs= github.com/aws/aws-sdk-go-v2/service/sso v1.20.6/go.mod h1:qGzynb/msuZIE8I75DVRCUXw3o3ZyBmUvMwQ2t/BrGM= -github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.5 h1:Ciiz/plN+Z+pPO1G0W2zJoYIIl0KtKzY0LJ78NXYTws= -github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.5/go.mod h1:mUYPBhaF2lGiukDEjJX2BLRRKTmoUSitGDUgM4tRxak= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.0 h1:Qe0r0lVURDDeBQJ4yP+BOrJkvkiCo/3FH/t+wY11dmw= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.0/go.mod h1:mUYPBhaF2lGiukDEjJX2BLRRKTmoUSitGDUgM4tRxak= github.com/aws/aws-sdk-go-v2/service/sts v1.28.7 h1:et3Ta53gotFR4ERLXXHIHl/Uuk1qYpP5uU7cvNql8ns= github.com/aws/aws-sdk-go-v2/service/sts v1.28.7/go.mod h1:FZf1/nKNEkHdGGJP/cI2MoIMquumuRK6ol3QQJNDxmw= github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q= @@ -219,8 +219,8 @@ github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB1 github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= -github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A= -github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI= +github.com/hashicorp/go-plugin v1.6.1 h1:P7MR2UP6gNKGPp+y7EZw2kOiq4IR9WiqLvp0XOsVdwI= +github.com/hashicorp/go-plugin v1.6.1/go.mod h1:XPHFku2tFo3o3QKFgSYo+cghcUhw1NA1hZyMK0PWAw0= github.com/hashicorp/go-retryablehttp v0.7.6 h1:TwRYfx2z2C4cLbXmT8I5PgP/xmuqASDyiVuGYfs9GZM= github.com/hashicorp/go-retryablehttp v0.7.6/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= @@ -509,8 +509,8 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU= golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90= -google.golang.org/api v0.179.0 h1:QyHDLm/HqM7ysaHgGO0wu7P4NbwbimnOoKxu5Cfdx8s= -google.golang.org/api v0.179.0/go.mod h1:51AiyoEg1MJPSZ9zvklA8VnRILPXxn1iVen9v25XHAE= +google.golang.org/api v0.180.0 h1:M2D87Yo0rGBPWpo1orwfCLehUUL6E7/TYe5gvMQWDh4= +google.golang.org/api v0.180.0/go.mod h1:51AiyoEg1MJPSZ9zvklA8VnRILPXxn1iVen9v25XHAE= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= diff --git a/internal/common/eventmanager.go b/internal/common/eventmanager.go index 2c4d45fa..de9cbf64 100644 --- a/internal/common/eventmanager.go +++ b/internal/common/eventmanager.go @@ -1286,9 +1286,9 @@ func writeHTTPPart(m *multipart.Writer, part dataprovider.HTTPPart, h textproto. if strings.Contains(strings.ToLower(cType), "application/json") { replacements := params.getStringReplacements(addObjectData, true) jsonReplacer := strings.NewReplacer(replacements...) - _, err = partWriter.Write([]byte(replaceWithReplacer(part.Body, jsonReplacer))) + _, err = partWriter.Write(util.StringToBytes(replaceWithReplacer(part.Body, jsonReplacer))) } else { - _, err = partWriter.Write([]byte(replaceWithReplacer(part.Body, replacer))) + _, err = partWriter.Write(util.StringToBytes(replaceWithReplacer(part.Body, replacer))) } if err != nil { eventManagerLog(logger.LevelError, "unable to write part %q, err: %v", part.Name, err) @@ -2501,7 +2501,7 @@ func executeAdminCheckAction(c *dataprovider.EventActionIDPAccountCheck, params data := replaceWithReplacer(c.TemplateAdmin, replacer) var newAdmin dataprovider.Admin - err = json.Unmarshal([]byte(data), &newAdmin) + err = json.Unmarshal(util.StringToBytes(data), &newAdmin) if err != nil { return nil, err } @@ -2533,7 +2533,7 @@ func executeUserCheckAction(c *dataprovider.EventActionIDPAccountCheck, params * data := replaceWithReplacer(c.TemplateUser, replacer) var newUser dataprovider.User - err = json.Unmarshal([]byte(data), &newUser) + err = json.Unmarshal(util.StringToBytes(data), &newUser) if err != nil { return nil, err } diff --git a/internal/dataprovider/dataprovider.go b/internal/dataprovider/dataprovider.go index 36512a47..16d1dada 100644 --- a/internal/dataprovider/dataprovider.go +++ b/internal/dataprovider/dataprovider.go @@ -3429,7 +3429,7 @@ func checkUserAndTLSCertificate(user *User, protocol string, tlsCert *x509.Certi switch protocol { case protocolFTP, protocolWebDAV: for _, cert := range user.Filters.TLSCerts { - derBlock, _ := pem.Decode([]byte(cert)) + derBlock, _ := pem.Decode(util.StringToBytes(cert)) if derBlock != nil && bytes.Equal(derBlock.Bytes, tlsCert.Raw) { return *user, nil } @@ -3551,7 +3551,7 @@ func checkUserAndPubKey(user *User, pubKey []byte, isSSHCert bool) (User, string return *user, "", ErrInvalidCredentials } for idx, key := range user.PublicKeys { - storedKey, comment, _, _, err := ssh.ParseAuthorizedKey([]byte(key)) + storedKey, comment, _, _, err := ssh.ParseAuthorizedKey(util.StringToBytes(key)) if err != nil { providerLog(logger.LevelError, "error parsing stored public key %d for user %s: %v", idx, user.Username, err) return *user, "", err diff --git a/internal/httpd/webclient.go b/internal/httpd/webclient.go index 07ad7a37..10fd2c49 100644 --- a/internal/httpd/webclient.go +++ b/internal/httpd/webclient.go @@ -885,7 +885,7 @@ func (s *httpdServer) handleWebClientDownloadZip(w http.ResponseWriter, r *http. name := connection.User.GetCleanedPath(r.URL.Query().Get("path")) files := r.Form.Get("files") var filesList []string - err = json.Unmarshal([]byte(files), &filesList) + err = json.Unmarshal(util.StringToBytes(files), &filesList) if err != nil { s.renderClientBadRequestPage(w, r, err) return @@ -932,7 +932,7 @@ func (s *httpdServer) handleClientSharePartialDownload(w http.ResponseWriter, r } files := r.Form.Get("files") var filesList []string - err = json.Unmarshal([]byte(files), &filesList) + err = json.Unmarshal(util.StringToBytes(files), &filesList) if err != nil { s.renderClientBadRequestPage(w, r, err) return @@ -1393,7 +1393,7 @@ func (s *httpdServer) handleClientAddShareGet(w http.ResponseWriter, r *http.Req if _, ok := r.URL.Query()["files"]; ok { files := r.URL.Query().Get("files") var filesList []string - err := json.Unmarshal([]byte(files), &filesList) + err := json.Unmarshal(util.StringToBytes(files), &filesList) if err != nil { s.renderClientBadRequestPage(w, r, err) return diff --git a/internal/sftpd/server.go b/internal/sftpd/server.go index 94e18881..5b4455a3 100644 --- a/internal/sftpd/server.go +++ b/internal/sftpd/server.go @@ -589,7 +589,7 @@ func (c *Configuration) AcceptInboundConnection(conn net.Conn, config *ssh.Serve var user dataprovider.User // Unmarshal cannot fails here and even if it fails we'll have a user with no permissions - json.Unmarshal([]byte(sconn.Permissions.Extensions["sftpgo_user"]), &user) //nolint:errcheck + json.Unmarshal(util.StringToBytes(sconn.Permissions.Extensions["sftpgo_user"]), &user) //nolint:errcheck loginType := sconn.Permissions.Extensions["sftpgo_login_method"] connectionID := hex.EncodeToString(sconn.SessionID()) diff --git a/internal/util/util.go b/internal/util/util.go index 8c9041c1..3014db55 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -290,6 +290,8 @@ func ParseBytes(s string) (int64, error) { } // BytesToString converts []byte to string without allocations. +// https://github.com/kubernetes/kubernetes/blob/e4b74dd12fa8cb63c174091d5536a10b8ec19d34/staging/src/k8s.io/apiserver/pkg/authentication/token/cache/cached_token_authenticator.go#L278 +// Use only if strictly required, this method uses unsafe. func BytesToString(b []byte) string { // unsafe.SliceData relies on cap whereas we want to rely on len if len(b) == 0 { @@ -299,6 +301,18 @@ func BytesToString(b []byte) string { return unsafe.String(unsafe.SliceData(b), len(b)) } +// StringToBytes convert string to []byte without allocations. +// https://github.com/kubernetes/kubernetes/blob/e4b74dd12fa8cb63c174091d5536a10b8ec19d34/staging/src/k8s.io/apiserver/pkg/authentication/token/cache/cached_token_authenticator.go#L289 +// Use only if strictly required, this method uses unsafe. +func StringToBytes(s string) []byte { + // unsafe.StringData is unspecified for the empty string, so we provide a strict interpretation + if s == "" { + return nil + } + // https://github.com/golang/go/blob/4ed358b57efdad9ed710be7f4fc51495a7620ce2/src/os/file.go#L300 + return unsafe.Slice(unsafe.StringData(s), len(s)) +} + // GetIPFromRemoteAddress returns the IP from the remote address. // If the given remote address cannot be parsed it will be returned unchanged func GetIPFromRemoteAddress(remoteAddress string) string {