From 5488389278050461f3d29508c10da99c598ee6fb Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Mon, 17 Oct 2022 15:29:12 -0300 Subject: [PATCH] fix(code): replace calls to ioutil EE-4425 (#7878) --- api/archive/targz_test.go | 17 ++++++++--------- api/archive/zip.go | 6 +++--- api/archive/zip_test.go | 3 ++- api/crypto/aes_test.go | 19 +++++++++---------- api/crypto/tls.go | 4 ++-- api/database/boltdb/db.go | 3 +-- api/datastore/services.go | 6 +++--- api/exec/compose_stack_test.go | 6 +++--- api/filesystem/copy_test.go | 9 ++++----- api/filesystem/write_test.go | 8 ++++---- api/git/azure.go | 3 +-- api/hostmanagement/openamt/authorization.go | 4 ++-- api/hostmanagement/openamt/openamt.go | 6 +++--- api/http/client/client.go | 4 ++-- api/http/handler/backup/backup_test.go | 5 ++--- .../edgegroups/associated_endpoints.go | 2 +- api/http/handler/helm/helm_install.go | 3 +-- .../handler/hostmanagement/openamt/amtrpc.go | 6 +++--- api/http/handler/stacks/stack_delete.go | 3 +-- .../handler/stacks/update_kubernetes_stack.go | 3 +-- api/http/handler/upload/handler.go | 2 +- api/http/handler/users/admin_check.go | 3 ++- api/http/handler/webhooks/webhook_delete.go | 3 ++- api/http/handler/webhooks/webhook_execute.go | 3 ++- api/http/handler/webhooks/webhook_list.go | 3 ++- api/http/handler/websocket/dial_windows.go | 3 ++- api/http/handler/websocket/initdial.go | 2 +- api/http/handler/websocket/proxy.go | 2 +- api/http/handler/websocket/types.go | 2 +- api/http/proxy/factory/docker/build.go | 8 ++++---- api/http/proxy/factory/docker/containers.go | 6 +++--- api/http/proxy/factory/docker/services.go | 6 +++--- api/http/proxy/factory/docker/transport.go | 4 ++-- api/http/proxy/factory/kubernetes/token.go | 4 ++-- .../proxy/factory/kubernetes/transport.go | 4 ++-- api/http/proxy/factory/utils/json.go | 3 +-- api/http/proxy/factory/utils/request.go | 4 ++-- api/http/proxy/factory/utils/response.go | 4 ++-- api/internal/edge/edgestack.go | 2 +- api/internal/edge/endpoint.go | 2 +- api/internal/tag/tag.go | 2 +- api/jwt/jwt_test.go | 3 ++- api/kubernetes/cli/nodes_limits_test.go | 7 ++++--- api/kubernetes/cli/service_account.go | 2 +- api/kubernetes/kubeclusteraccess_service.go | 4 ++-- .../kubeclusteraccess_service_test.go | 4 ++-- api/oauth/oauth.go | 4 ++-- .../deployment_kubernetes_config.go | 5 ++--- 48 files changed, 109 insertions(+), 112 deletions(-) diff --git a/api/archive/targz_test.go b/api/archive/targz_test.go index df17b8f4f..f3694f16f 100644 --- a/api/archive/targz_test.go +++ b/api/archive/targz_test.go @@ -2,7 +2,6 @@ package archive import ( "fmt" - "io/ioutil" "os" "os/exec" "path" @@ -28,10 +27,10 @@ func listFiles(dir string) []string { func Test_shouldCreateArhive(t *testing.T) { tmpdir := t.TempDir() content := []byte("content") - ioutil.WriteFile(path.Join(tmpdir, "outer"), content, 0600) + os.WriteFile(path.Join(tmpdir, "outer"), content, 0600) os.MkdirAll(path.Join(tmpdir, "dir"), 0700) - ioutil.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600) - ioutil.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600) + os.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600) + os.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600) gzPath, err := TarGzDir(tmpdir) assert.Nil(t, err) @@ -48,7 +47,7 @@ func Test_shouldCreateArhive(t *testing.T) { wasExtracted := func(p string) { fullpath := path.Join(extractionDir, p) assert.Contains(t, extractedFiles, fullpath) - copyContent, _ := ioutil.ReadFile(fullpath) + copyContent, _ := os.ReadFile(fullpath) assert.Equal(t, content, copyContent) } @@ -60,10 +59,10 @@ func Test_shouldCreateArhive(t *testing.T) { func Test_shouldCreateArhiveXXXXX(t *testing.T) { tmpdir := t.TempDir() content := []byte("content") - ioutil.WriteFile(path.Join(tmpdir, "outer"), content, 0600) + os.WriteFile(path.Join(tmpdir, "outer"), content, 0600) os.MkdirAll(path.Join(tmpdir, "dir"), 0700) - ioutil.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600) - ioutil.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600) + os.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600) + os.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600) gzPath, err := TarGzDir(tmpdir) assert.Nil(t, err) @@ -80,7 +79,7 @@ func Test_shouldCreateArhiveXXXXX(t *testing.T) { wasExtracted := func(p string) { fullpath := path.Join(extractionDir, p) assert.Contains(t, extractedFiles, fullpath) - copyContent, _ := ioutil.ReadFile(fullpath) + copyContent, _ := os.ReadFile(fullpath) assert.Equal(t, content, copyContent) } diff --git a/api/archive/zip.go b/api/archive/zip.go index 2aaee5a4f..50328ef09 100644 --- a/api/archive/zip.go +++ b/api/archive/zip.go @@ -4,12 +4,12 @@ import ( "archive/zip" "bytes" "fmt" - "github.com/pkg/errors" "io" - "io/ioutil" "os" "path/filepath" "strings" + + "github.com/pkg/errors" ) // UnzipArchive will unzip an archive from bytes into the dest destination folder on disk @@ -36,7 +36,7 @@ func extractFileFromArchive(file *zip.File, dest string) error { } defer f.Close() - data, err := ioutil.ReadAll(f) + data, err := io.ReadAll(f) if err != nil { return err } diff --git a/api/archive/zip_test.go b/api/archive/zip_test.go index 268109358..056bc1c0d 100644 --- a/api/archive/zip_test.go +++ b/api/archive/zip_test.go @@ -1,9 +1,10 @@ package archive import ( - "github.com/stretchr/testify/assert" "path/filepath" "testing" + + "github.com/stretchr/testify/assert" ) func TestUnzipFile(t *testing.T) { diff --git a/api/crypto/aes_test.go b/api/crypto/aes_test.go index b53d0e87d..09dea9c6d 100644 --- a/api/crypto/aes_test.go +++ b/api/crypto/aes_test.go @@ -2,7 +2,6 @@ package crypto import ( "io" - "io/ioutil" "os" "path/filepath" "testing" @@ -20,7 +19,7 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) { ) content := []byte("content") - ioutil.WriteFile(originFilePath, content, 0600) + os.WriteFile(originFilePath, content, 0600) originFile, _ := os.Open(originFilePath) defer originFile.Close() @@ -30,7 +29,7 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) { err := AesEncrypt(originFile, encryptedFileWriter, []byte("passphrase")) assert.Nil(t, err, "Failed to encrypt a file") - encryptedContent, err := ioutil.ReadFile(encryptedFilePath) + encryptedContent, err := os.ReadFile(encryptedFilePath) assert.Nil(t, err, "Couldn't read encrypted file") assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted") @@ -45,7 +44,7 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) { io.Copy(decryptedFileWriter, decryptedReader) - decryptedContent, _ := ioutil.ReadFile(decryptedFilePath) + decryptedContent, _ := os.ReadFile(decryptedFilePath) assert.Equal(t, content, decryptedContent, "Original and decrypted content should match") } @@ -59,7 +58,7 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) { ) content := []byte("content") - ioutil.WriteFile(originFilePath, content, 0600) + os.WriteFile(originFilePath, content, 0600) originFile, _ := os.Open(originFilePath) defer originFile.Close() @@ -69,7 +68,7 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) { err := AesEncrypt(originFile, encryptedFileWriter, []byte("")) assert.Nil(t, err, "Failed to encrypt a file") - encryptedContent, err := ioutil.ReadFile(encryptedFilePath) + encryptedContent, err := os.ReadFile(encryptedFilePath) assert.Nil(t, err, "Couldn't read encrypted file") assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted") @@ -84,7 +83,7 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) { io.Copy(decryptedFileWriter, decryptedReader) - decryptedContent, _ := ioutil.ReadFile(decryptedFilePath) + decryptedContent, _ := os.ReadFile(decryptedFilePath) assert.Equal(t, content, decryptedContent, "Original and decrypted content should match") } @@ -98,7 +97,7 @@ func Test_decryptWithDifferentPassphrase_shouldProduceWrongResult(t *testing.T) ) content := []byte("content") - ioutil.WriteFile(originFilePath, content, 0600) + os.WriteFile(originFilePath, content, 0600) originFile, _ := os.Open(originFilePath) defer originFile.Close() @@ -108,7 +107,7 @@ func Test_decryptWithDifferentPassphrase_shouldProduceWrongResult(t *testing.T) err := AesEncrypt(originFile, encryptedFileWriter, []byte("passphrase")) assert.Nil(t, err, "Failed to encrypt a file") - encryptedContent, err := ioutil.ReadFile(encryptedFilePath) + encryptedContent, err := os.ReadFile(encryptedFilePath) assert.Nil(t, err, "Couldn't read encrypted file") assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted") @@ -123,6 +122,6 @@ func Test_decryptWithDifferentPassphrase_shouldProduceWrongResult(t *testing.T) io.Copy(decryptedFileWriter, decryptedReader) - decryptedContent, _ := ioutil.ReadFile(decryptedFilePath) + decryptedContent, _ := os.ReadFile(decryptedFilePath) assert.NotEqual(t, content, decryptedContent, "Original and decrypted content should NOT match") } diff --git a/api/crypto/tls.go b/api/crypto/tls.go index e46998898..e3418f66e 100644 --- a/api/crypto/tls.go +++ b/api/crypto/tls.go @@ -3,7 +3,7 @@ package crypto import ( "crypto/tls" "crypto/x509" - "io/ioutil" + "os" ) // CreateServerTLSConfiguration creates a basic tls.Config to be used by servers with recommended TLS settings @@ -63,7 +63,7 @@ func CreateTLSConfigurationFromDisk(caCertPath, certPath, keyPath string, skipSe } if !skipServerVerification && caCertPath != "" { - caCert, err := ioutil.ReadFile(caCertPath) + caCert, err := os.ReadFile(caCertPath) if err != nil { return nil, err } diff --git a/api/database/boltdb/db.go b/api/database/boltdb/db.go index a829cedd8..c7fa80bd5 100644 --- a/api/database/boltdb/db.go +++ b/api/database/boltdb/db.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path" "time" @@ -167,7 +166,7 @@ func (connection *DbConnection) ExportRaw(filename string) error { if err != nil { return err } - return ioutil.WriteFile(filename, b, 0600) + return os.WriteFile(filename, b, 0600) } // ConvertToKey returns an 8-byte big endian representation of v. diff --git a/api/datastore/services.go b/api/datastore/services.go index c4bd90003..eb05b3329 100644 --- a/api/datastore/services.go +++ b/api/datastore/services.go @@ -3,7 +3,7 @@ package datastore import ( "encoding/json" "fmt" - "io/ioutil" + "os" "strconv" portainer "github.com/portainer/portainer/api" @@ -607,13 +607,13 @@ func (store *Store) Export(filename string) (err error) { if err != nil { return err } - return ioutil.WriteFile(filename, b, 0600) + return os.WriteFile(filename, b, 0600) } func (store *Store) Import(filename string) (err error) { backup := storeExport{} - s, err := ioutil.ReadFile(filename) + s, err := os.ReadFile(filename) if err != nil { return err } diff --git a/api/exec/compose_stack_test.go b/api/exec/compose_stack_test.go index 733eb7fb2..b1904438a 100644 --- a/api/exec/compose_stack_test.go +++ b/api/exec/compose_stack_test.go @@ -1,7 +1,7 @@ package exec import ( - "io/ioutil" + "io" "os" "path" "testing" @@ -55,7 +55,7 @@ func Test_createEnvFile(t *testing.T) { assert.Equal(t, "stack.env", result) f, _ := os.Open(path.Join(dir, "stack.env")) - content, _ := ioutil.ReadAll(f) + content, _ := io.ReadAll(f) assert.Equal(t, tt.expected, string(content)) } else { @@ -80,7 +80,7 @@ func Test_createEnvFile_mergesDefultAndInplaceEnvVars(t *testing.T) { assert.NoError(t, err) assert.FileExists(t, path.Join(dir, "stack.env")) f, _ := os.Open(path.Join(dir, "stack.env")) - content, _ := ioutil.ReadAll(f) + content, _ := io.ReadAll(f) assert.Equal(t, []byte("VAR1=VAL1\nVAR2=VAL2\n\nVAR1=NEW_VAL1\nVAR3=VAL3\n"), content) } diff --git a/api/filesystem/copy_test.go b/api/filesystem/copy_test.go index a451fdb8f..a91754975 100644 --- a/api/filesystem/copy_test.go +++ b/api/filesystem/copy_test.go @@ -1,7 +1,6 @@ package filesystem import ( - "io/ioutil" "os" "path" "path/filepath" @@ -19,12 +18,12 @@ func Test_copyFile_returnsError_whenSourceDoesNotExist(t *testing.T) { func Test_copyFile_shouldMakeAbackup(t *testing.T) { tmpdir := t.TempDir() content := []byte("content") - ioutil.WriteFile(path.Join(tmpdir, "origin"), content, 0600) + os.WriteFile(path.Join(tmpdir, "origin"), content, 0600) err := copyFile(path.Join(tmpdir, "origin"), path.Join(tmpdir, "copy")) assert.NoError(t, err) - copyContent, _ := ioutil.ReadFile(path.Join(tmpdir, "copy")) + copyContent, _ := os.ReadFile(path.Join(tmpdir, "copy")) assert.Equal(t, content, copyContent) } @@ -59,13 +58,13 @@ func Test_CopyPath_shouldSkipWhenNotExist(t *testing.T) { func Test_CopyPath_shouldCopyFile(t *testing.T) { tmpdir := t.TempDir() content := []byte("content") - ioutil.WriteFile(path.Join(tmpdir, "file"), content, 0600) + os.WriteFile(path.Join(tmpdir, "file"), content, 0600) os.MkdirAll(path.Join(tmpdir, "backup"), 0700) err := CopyPath(path.Join(tmpdir, "file"), path.Join(tmpdir, "backup")) assert.NoError(t, err) - copyContent, err := ioutil.ReadFile(path.Join(tmpdir, "backup", "file")) + copyContent, err := os.ReadFile(path.Join(tmpdir, "backup", "file")) assert.NoError(t, err) assert.Equal(t, content, copyContent) } diff --git a/api/filesystem/write_test.go b/api/filesystem/write_test.go index 89223a20e..17606ace0 100644 --- a/api/filesystem/write_test.go +++ b/api/filesystem/write_test.go @@ -1,7 +1,7 @@ package filesystem import ( - "io/ioutil" + "os" "path" "testing" @@ -16,7 +16,7 @@ func Test_WriteFile_CanStoreContentInANewFile(t *testing.T) { err := WriteToFile(tmpFilePath, content) assert.NoError(t, err) - fileContent, _ := ioutil.ReadFile(tmpFilePath) + fileContent, _ := os.ReadFile(tmpFilePath) assert.Equal(t, content, fileContent) } @@ -31,7 +31,7 @@ func Test_WriteFile_CanOverwriteExistingFile(t *testing.T) { err = WriteToFile(tmpFilePath, content) assert.NoError(t, err) - fileContent, _ := ioutil.ReadFile(tmpFilePath) + fileContent, _ := os.ReadFile(tmpFilePath) assert.Equal(t, content, fileContent) } @@ -43,6 +43,6 @@ func Test_WriteFile_CanWriteANestedPath(t *testing.T) { err := WriteToFile(tmpFilePath, content) assert.NoError(t, err) - fileContent, _ := ioutil.ReadFile(tmpFilePath) + fileContent, _ := os.ReadFile(tmpFilePath) assert.Equal(t, content, fileContent) } diff --git a/api/git/azure.go b/api/git/azure.go index 7aa7b5097..6036fe5ac 100644 --- a/api/git/azure.go +++ b/api/git/azure.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -100,7 +99,7 @@ func (a *azureClient) downloadZipFromAzureDevOps(ctx context.Context, opt cloneO if err != nil { return "", errors.WithMessage(err, "failed to build download url") } - zipFile, err := ioutil.TempFile("", "azure-git-repo-*.zip") + zipFile, err := os.CreateTemp("", "azure-git-repo-*.zip") if err != nil { return "", errors.WithMessage(err, "failed to create temp file") } diff --git a/api/hostmanagement/openamt/authorization.go b/api/hostmanagement/openamt/authorization.go index 5e9d0773f..9d5050d2e 100644 --- a/api/hostmanagement/openamt/authorization.go +++ b/api/hostmanagement/openamt/authorization.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" portainer "github.com/portainer/portainer/api" @@ -33,7 +33,7 @@ func (service *Service) Authorization(configuration portainer.OpenAMTConfigurati if err != nil { return "", err } - responseBody, readErr := ioutil.ReadAll(response.Body) + responseBody, readErr := io.ReadAll(response.Body) if readErr != nil { return "", readErr } diff --git a/api/hostmanagement/openamt/openamt.go b/api/hostmanagement/openamt/openamt.go index 863410d14..8c9902866 100644 --- a/api/hostmanagement/openamt/openamt.go +++ b/api/hostmanagement/openamt/openamt.go @@ -6,7 +6,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "time" @@ -99,7 +99,7 @@ func (service *Service) executeSaveRequest(method string, url string, token stri if err != nil { return nil, err } - responseBody, readErr := ioutil.ReadAll(response.Body) + responseBody, readErr := io.ReadAll(response.Body) if readErr != nil { return nil, readErr } @@ -128,7 +128,7 @@ func (service *Service) executeGetRequest(url string, token string) ([]byte, err if err != nil { return nil, err } - responseBody, readErr := ioutil.ReadAll(response.Body) + responseBody, readErr := io.ReadAll(response.Body) if readErr != nil { return nil, readErr } diff --git a/api/http/client/client.go b/api/http/client/client.go index dcd070375..343e8fa2a 100644 --- a/api/http/client/client.go +++ b/api/http/client/client.go @@ -5,7 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strings" @@ -96,7 +96,7 @@ func Get(url string, timeout int) ([]byte, error) { return nil, errInvalidResponseStatus } - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) if err != nil { return nil, err } diff --git a/api/http/handler/backup/backup_test.go b/api/http/handler/backup/backup_test.go index 8c2fb979f..a35754db3 100644 --- a/api/http/handler/backup/backup_test.go +++ b/api/http/handler/backup/backup_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "io" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -38,7 +37,7 @@ func listFiles(dir string) []string { func contains(t *testing.T, list []string, path string) { assert.Contains(t, list, path) - copyContent, _ := ioutil.ReadFile(path) + copyContent, _ := os.ReadFile(path) assert.Equal(t, "content\n", string(copyContent)) } @@ -58,7 +57,7 @@ func Test_backupHandlerWithoutPassword_shouldCreateATarballArchive(t *testing.T) tmpdir := t.TempDir() archivePath := filepath.Join(tmpdir, "archive.tar.gz") - err := ioutil.WriteFile(archivePath, body, 0600) + err := os.WriteFile(archivePath, body, 0600) if err != nil { t.Fatal("Failed to save downloaded .tar.gz archive: ", err) } diff --git a/api/http/handler/edgegroups/associated_endpoints.go b/api/http/handler/edgegroups/associated_endpoints.go index b34711eda..6c240083c 100644 --- a/api/http/handler/edgegroups/associated_endpoints.go +++ b/api/http/handler/edgegroups/associated_endpoints.go @@ -1,7 +1,7 @@ package edgegroups import ( - "github.com/portainer/portainer/api" + portainer "github.com/portainer/portainer/api" ) type endpointSetType map[portainer.EndpointID]bool diff --git a/api/http/handler/helm/helm_install.go b/api/http/handler/helm/helm_install.go index 248e0659c..33f8f45da 100644 --- a/api/http/handler/helm/helm_install.go +++ b/api/http/handler/helm/helm_install.go @@ -2,7 +2,6 @@ package helm import ( "fmt" - "io/ioutil" "net/http" "os" "strings" @@ -192,7 +191,7 @@ func (handler *Handler) updateHelmAppManifest(r *http.Request, manifest []byte, for _, resource := range yamlResources { resource := resource // https://golang.org/doc/faq#closures_and_goroutines g.Go(func() error { - tmpfile, err := ioutil.TempFile("", "helm-manifest-*") + tmpfile, err := os.CreateTemp("", "helm-manifest-*") if err != nil { return errors.Wrap(err, "failed to create a tmp helm manifest file") } diff --git a/api/http/handler/hostmanagement/openamt/amtrpc.go b/api/http/handler/hostmanagement/openamt/amtrpc.go index fe0c9e162..a0c2ccbf2 100644 --- a/api/http/handler/hostmanagement/openamt/amtrpc.go +++ b/api/http/handler/hostmanagement/openamt/amtrpc.go @@ -4,7 +4,7 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "time" @@ -139,7 +139,7 @@ func pullImage(ctx context.Context, docker *client.Client, imageName string) err } defer out.Close() - outputBytes, err := ioutil.ReadAll(out) + outputBytes, err := io.ReadAll(out) if err != nil { log.Error().Str("image_name", imageName).Err(err).Msg("could not read image pull output") @@ -261,7 +261,7 @@ func runContainer(ctx context.Context, docker *client.Client, imageName, contain return "", err } - outputBytes, err := ioutil.ReadAll(out) + outputBytes, err := io.ReadAll(out) if err != nil { log.Error(). Str("image_name", imageName). diff --git a/api/http/handler/stacks/stack_delete.go b/api/http/handler/stacks/stack_delete.go index ca447c903..d4d353978 100644 --- a/api/http/handler/stacks/stack_delete.go +++ b/api/http/handler/stacks/stack_delete.go @@ -3,7 +3,6 @@ package stacks import ( "context" "fmt" - "io/ioutil" "net/http" "os" "strconv" @@ -200,7 +199,7 @@ func (handler *Handler) deleteStack(userID portainer.UserID, stack *portainer.St //then process the remove operation if stack.IsComposeFormat { fileNames := stackutils.GetStackFilePaths(stack, false) - tmpDir, err := ioutil.TempDir("", "kube_delete") + tmpDir, err := os.MkdirTemp("", "kube_delete") if err != nil { return errors.Wrap(err, "failed to create temp directory for deleting kub stack") } diff --git a/api/http/handler/stacks/update_kubernetes_stack.go b/api/http/handler/stacks/update_kubernetes_stack.go index 0764c58d7..ed939addb 100644 --- a/api/http/handler/stacks/update_kubernetes_stack.go +++ b/api/http/handler/stacks/update_kubernetes_stack.go @@ -2,7 +2,6 @@ package stacks import ( "fmt" - "io/ioutil" "net/http" "os" "strconv" @@ -105,7 +104,7 @@ func (handler *Handler) updateKubernetesStack(r *http.Request, stack *portainer. return httperror.BadRequest("Failed to retrieve user token data", err) } - tempFileDir, _ := ioutil.TempDir("", "kub_file_content") + tempFileDir, _ := os.MkdirTemp("", "kub_file_content") defer os.RemoveAll(tempFileDir) if err := filesystem.WriteToFile(filesystem.JoinPaths(tempFileDir, stack.EntryPoint), []byte(payload.StackFileContent)); err != nil { diff --git a/api/http/handler/upload/handler.go b/api/http/handler/upload/handler.go index dd6a459a1..9c8cca2a4 100644 --- a/api/http/handler/upload/handler.go +++ b/api/http/handler/upload/handler.go @@ -2,7 +2,7 @@ package upload import ( httperror "github.com/portainer/libhttp/error" - "github.com/portainer/portainer/api" + portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/http/security" "net/http" diff --git a/api/http/handler/users/admin_check.go b/api/http/handler/users/admin_check.go index 1925981ee..5fbec62a5 100644 --- a/api/http/handler/users/admin_check.go +++ b/api/http/handler/users/admin_check.go @@ -1,9 +1,10 @@ package users import ( - "github.com/portainer/portainer/api/dataservices/errors" "net/http" + "github.com/portainer/portainer/api/dataservices/errors" + httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/response" portainer "github.com/portainer/portainer/api" diff --git a/api/http/handler/webhooks/webhook_delete.go b/api/http/handler/webhooks/webhook_delete.go index 0b7f64747..3596130f9 100644 --- a/api/http/handler/webhooks/webhook_delete.go +++ b/api/http/handler/webhooks/webhook_delete.go @@ -2,9 +2,10 @@ package webhooks import ( "errors" - "github.com/portainer/portainer/api/http/security" "net/http" + "github.com/portainer/portainer/api/http/security" + httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" diff --git a/api/http/handler/webhooks/webhook_execute.go b/api/http/handler/webhooks/webhook_execute.go index 0168e231d..511448270 100644 --- a/api/http/handler/webhooks/webhook_execute.go +++ b/api/http/handler/webhooks/webhook_execute.go @@ -3,11 +3,12 @@ package webhooks import ( "context" "errors" - "github.com/portainer/portainer/api/internal/registryutils" "io" "net/http" "strings" + "github.com/portainer/portainer/api/internal/registryutils" + dockertypes "github.com/docker/docker/api/types" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" diff --git a/api/http/handler/webhooks/webhook_list.go b/api/http/handler/webhooks/webhook_list.go index 966611010..c2097a47d 100644 --- a/api/http/handler/webhooks/webhook_list.go +++ b/api/http/handler/webhooks/webhook_list.go @@ -1,9 +1,10 @@ package webhooks import ( - "github.com/portainer/portainer/api/http/security" "net/http" + "github.com/portainer/portainer/api/http/security" + httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" diff --git a/api/http/handler/websocket/dial_windows.go b/api/http/handler/websocket/dial_windows.go index e6d725a40..8f181e956 100644 --- a/api/http/handler/websocket/dial_windows.go +++ b/api/http/handler/websocket/dial_windows.go @@ -4,8 +4,9 @@ package websocket import ( - "github.com/Microsoft/go-winio" "net" + + "github.com/Microsoft/go-winio" ) func createDial(scheme, host string) (net.Conn, error) { diff --git a/api/http/handler/websocket/initdial.go b/api/http/handler/websocket/initdial.go index 327753d28..e9160ccf1 100644 --- a/api/http/handler/websocket/initdial.go +++ b/api/http/handler/websocket/initdial.go @@ -5,7 +5,7 @@ import ( "net" "net/url" - "github.com/portainer/portainer/api" + portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/crypto" ) diff --git a/api/http/handler/websocket/proxy.go b/api/http/handler/websocket/proxy.go index 647399760..d1325cc03 100644 --- a/api/http/handler/websocket/proxy.go +++ b/api/http/handler/websocket/proxy.go @@ -8,7 +8,7 @@ import ( "github.com/gorilla/websocket" "github.com/koding/websocketproxy" - "github.com/portainer/portainer/api" + portainer "github.com/portainer/portainer/api" ) func (handler *Handler) proxyEdgeAgentWebsocketRequest(w http.ResponseWriter, r *http.Request, params *webSocketRequestParams) error { diff --git a/api/http/handler/websocket/types.go b/api/http/handler/websocket/types.go index 614143aa0..97b3334f6 100644 --- a/api/http/handler/websocket/types.go +++ b/api/http/handler/websocket/types.go @@ -1,6 +1,6 @@ package websocket -import "github.com/portainer/portainer/api" +import portainer "github.com/portainer/portainer/api" type webSocketRequestParams struct { ID string diff --git a/api/http/proxy/factory/docker/build.go b/api/http/proxy/factory/docker/build.go index 6b5dfa91f..cbe2d0e99 100644 --- a/api/http/proxy/factory/docker/build.go +++ b/api/http/proxy/factory/docker/build.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "errors" - "io/ioutil" + "io" "mime" "net/http" @@ -37,7 +37,7 @@ func buildOperation(request *http.Request) error { var buffer []byte switch mediaType { case "": - body, err := ioutil.ReadAll(request.Body) + body, err := io.ReadAll(request.Body) if err != nil { return err } @@ -81,7 +81,7 @@ func buildOperation(request *http.Request) error { log.Info().Str("filename", hdr.Filename).Int64("size", hdr.Size).Msg("upload the file to build image") - content, err := ioutil.ReadAll(f) + content, err := io.ReadAll(f) if err != nil { return err } @@ -105,7 +105,7 @@ func buildOperation(request *http.Request) error { return nil } - request.Body = ioutil.NopCloser(bytes.NewReader(buffer)) + request.Body = io.NopCloser(bytes.NewReader(buffer)) request.ContentLength = int64(len(buffer)) request.Header.Set("Content-Type", "application/x-tar") diff --git a/api/http/proxy/factory/docker/containers.go b/api/http/proxy/factory/docker/containers.go index 573bfc21d..c137e2606 100644 --- a/api/http/proxy/factory/docker/containers.go +++ b/api/http/proxy/factory/docker/containers.go @@ -5,7 +5,7 @@ import ( "context" "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "strings" @@ -190,7 +190,7 @@ func (transport *Transport) decorateContainerCreationOperation(request *http.Req return nil, err } - body, err := ioutil.ReadAll(request.Body) + body, err := io.ReadAll(request.Body) if err != nil { return nil, err } @@ -229,7 +229,7 @@ func (transport *Transport) decorateContainerCreationOperation(request *http.Req } } - request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + request.Body = io.NopCloser(bytes.NewBuffer(body)) } response, err := transport.executeDockerRequest(request) diff --git a/api/http/proxy/factory/docker/services.go b/api/http/proxy/factory/docker/services.go index 205c48c60..4df1d316a 100644 --- a/api/http/proxy/factory/docker/services.go +++ b/api/http/proxy/factory/docker/services.go @@ -5,7 +5,7 @@ import ( "context" "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "github.com/docker/docker/api/types" @@ -116,7 +116,7 @@ func (transport *Transport) decorateServiceCreationOperation(request *http.Reque return nil, err } - body, err := ioutil.ReadAll(request.Body) + body, err := io.ReadAll(request.Body) if err != nil { return nil, err } @@ -135,7 +135,7 @@ func (transport *Transport) decorateServiceCreationOperation(request *http.Reque } } - request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) + request.Body = io.NopCloser(bytes.NewBuffer(body)) } return transport.replaceRegistryAuthenticationHeader(request) diff --git a/api/http/proxy/factory/docker/transport.go b/api/http/proxy/factory/docker/transport.go index 439988a96..86a252dae 100644 --- a/api/http/proxy/factory/docker/transport.go +++ b/api/http/proxy/factory/docker/transport.go @@ -6,7 +6,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "path" "regexp" @@ -197,7 +197,7 @@ func (transport *Transport) proxyAgentRequest(r *http.Request) (*http.Response, r.Method = http.MethodPost - r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) + r.Body = io.NopCloser(bytes.NewReader(newBody)) r.ContentLength = int64(len(newBody)) } diff --git a/api/http/proxy/factory/kubernetes/token.go b/api/http/proxy/factory/kubernetes/token.go index 997898ea1..f2d620630 100644 --- a/api/http/proxy/factory/kubernetes/token.go +++ b/api/http/proxy/factory/kubernetes/token.go @@ -1,7 +1,7 @@ package kubernetes import ( - "io/ioutil" + "os" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" @@ -28,7 +28,7 @@ func NewTokenManager(kubecli portainer.KubeClient, dataStore dataservices.DataSt } if setLocalAdminToken { - token, err := ioutil.ReadFile(defaultServiceAccountTokenFile) + token, err := os.ReadFile(defaultServiceAccountTokenFile) if err != nil { return nil, err } diff --git a/api/http/proxy/factory/kubernetes/transport.go b/api/http/proxy/factory/kubernetes/transport.go index 86b793eed..1f05092f8 100644 --- a/api/http/proxy/factory/kubernetes/transport.go +++ b/api/http/proxy/factory/kubernetes/transport.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "path" "regexp" @@ -189,7 +189,7 @@ func decorateAgentDockerHubRequest(r *http.Request, dataStore dataservices.DataS } r.Method = http.MethodPost - r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) + r.Body = io.NopCloser(bytes.NewReader(newBody)) r.ContentLength = int64(len(newBody)) return nil diff --git a/api/http/proxy/factory/utils/json.go b/api/http/proxy/factory/utils/json.go index cab676834..7d12567e0 100644 --- a/api/http/proxy/factory/utils/json.go +++ b/api/http/proxy/factory/utils/json.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "mime" "gopkg.in/yaml.v3" @@ -50,7 +49,7 @@ func getBody(body io.ReadCloser, contentType string, isGzip bool) (interface{}, defer reader.Close() - bodyBytes, err := ioutil.ReadAll(reader) + bodyBytes, err := io.ReadAll(reader) if err != nil { return nil, err } diff --git a/api/http/proxy/factory/utils/request.go b/api/http/proxy/factory/utils/request.go index 92724b7fa..9f809fc20 100644 --- a/api/http/proxy/factory/utils/request.go +++ b/api/http/proxy/factory/utils/request.go @@ -2,7 +2,7 @@ package utils import ( "bytes" - "io/ioutil" + "io" "net/http" "strconv" ) @@ -25,7 +25,7 @@ func RewriteRequest(request *http.Request, newData interface{}) error { return err } - body := ioutil.NopCloser(bytes.NewReader(data)) + body := io.NopCloser(bytes.NewReader(data)) request.Body = body request.ContentLength = int64(len(data)) diff --git a/api/http/proxy/factory/utils/response.go b/api/http/proxy/factory/utils/response.go index 674529883..868590295 100644 --- a/api/http/proxy/factory/utils/response.go +++ b/api/http/proxy/factory/utils/response.go @@ -3,7 +3,7 @@ package utils import ( "bytes" "fmt" - "io/ioutil" + "io" "net/http" "strconv" @@ -82,7 +82,7 @@ func RewriteResponse(response *http.Response, newResponseData interface{}, statu return err } - body := ioutil.NopCloser(bytes.NewReader(data)) + body := io.NopCloser(bytes.NewReader(data)) response.StatusCode = statusCode response.Body = body diff --git a/api/internal/edge/edgestack.go b/api/internal/edge/edgestack.go index 9a0837bc2..5e2aa520b 100644 --- a/api/internal/edge/edgestack.go +++ b/api/internal/edge/edgestack.go @@ -3,7 +3,7 @@ package edge import ( "errors" - "github.com/portainer/portainer/api" + portainer "github.com/portainer/portainer/api" ) // EdgeStackRelatedEndpoints returns a list of environments(endpoints) related to this Edge stack diff --git a/api/internal/edge/endpoint.go b/api/internal/edge/endpoint.go index 492c803a7..843561107 100644 --- a/api/internal/edge/endpoint.go +++ b/api/internal/edge/endpoint.go @@ -1,6 +1,6 @@ package edge -import "github.com/portainer/portainer/api" +import portainer "github.com/portainer/portainer/api" // EndpointRelatedEdgeStacks returns a list of Edge stacks related to this Environment(Endpoint) func EndpointRelatedEdgeStacks(endpoint *portainer.Endpoint, endpointGroup *portainer.EndpointGroup, edgeGroups []portainer.EdgeGroup, edgeStacks []portainer.EdgeStack) []portainer.EdgeStackID { diff --git a/api/internal/tag/tag.go b/api/internal/tag/tag.go index 8dd5ad58b..ada130c88 100644 --- a/api/internal/tag/tag.go +++ b/api/internal/tag/tag.go @@ -1,6 +1,6 @@ package tag -import "github.com/portainer/portainer/api" +import portainer "github.com/portainer/portainer/api" type tagSet map[portainer.TagID]bool diff --git a/api/jwt/jwt_test.go b/api/jwt/jwt_test.go index 5a6a5c988..82e559aed 100644 --- a/api/jwt/jwt_test.go +++ b/api/jwt/jwt_test.go @@ -1,10 +1,11 @@ package jwt import ( - i "github.com/portainer/portainer/api/internal/testhelpers" "testing" "time" + i "github.com/portainer/portainer/api/internal/testhelpers" + "github.com/golang-jwt/jwt" portainer "github.com/portainer/portainer/api" "github.com/stretchr/testify/assert" diff --git a/api/kubernetes/cli/nodes_limits_test.go b/api/kubernetes/cli/nodes_limits_test.go index bf880c2ff..25ebe56a4 100644 --- a/api/kubernetes/cli/nodes_limits_test.go +++ b/api/kubernetes/cli/nodes_limits_test.go @@ -1,14 +1,15 @@ package cli import ( + "reflect" + "testing" + portainer "github.com/portainer/portainer/api" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" kfake "k8s.io/client-go/kubernetes/fake" - "reflect" - "testing" ) func newNodes() *v1.NodeList { diff --git a/api/kubernetes/cli/service_account.go b/api/kubernetes/cli/service_account.go index f9c421ec6..a50620b4d 100644 --- a/api/kubernetes/cli/service_account.go +++ b/api/kubernetes/cli/service_account.go @@ -37,7 +37,7 @@ func (kcl *KubeClient) GetServiceAccountBearerToken(userID int) (string, error) // SetupUserServiceAccount will make sure that all the required resources are created inside the Kubernetes // cluster before creating a ServiceAccount and a ServiceAccountToken for the specified Portainer user. -//It will also create required default RoleBinding and ClusterRoleBinding rules. +// It will also create required default RoleBinding and ClusterRoleBinding rules. func (kcl *KubeClient) SetupUserServiceAccount(userID int, teamIDs []int, restrictDefaultNamespace bool) error { serviceAccountName := UserServiceAccountName(userID, kcl.instanceID) diff --git a/api/kubernetes/kubeclusteraccess_service.go b/api/kubernetes/kubeclusteraccess_service.go index 0b1e785d5..d7da7bd2e 100644 --- a/api/kubernetes/kubeclusteraccess_service.go +++ b/api/kubernetes/kubeclusteraccess_service.go @@ -5,7 +5,7 @@ import ( "encoding/base64" "encoding/pem" "fmt" - "io/ioutil" + "os" "strings" portainer "github.com/portainer/portainer/api" @@ -63,7 +63,7 @@ func getCertificateAuthorityData(tlsCertPath string) (string, error) { return "", errTLSCertNotProvided } - data, err := ioutil.ReadFile(tlsCertPath) + data, err := os.ReadFile(tlsCertPath) if err != nil { return "", errors.Wrap(errTLSCertFileMissing, err.Error()) } diff --git a/api/kubernetes/kubeclusteraccess_service_test.go b/api/kubernetes/kubeclusteraccess_service_test.go index 51f421668..71ce50f30 100644 --- a/api/kubernetes/kubeclusteraccess_service_test.go +++ b/api/kubernetes/kubeclusteraccess_service_test.go @@ -2,7 +2,7 @@ package kubernetes import ( "fmt" - "io/ioutil" + "os" "strings" "testing" @@ -38,7 +38,7 @@ const certDataString = "MIIC5TCCAc2gAwIBAgIJAJ+poiEBdsplMA0GCSqGSIb3DQEBCwUAMBQx func createTempFile(filename, content string, t *testing.T) string { tempPath := t.TempDir() filePath := fmt.Sprintf("%s/%s", tempPath, filename) - ioutil.WriteFile(filePath, []byte(content), 0644) + os.WriteFile(filePath, []byte(content), 0644) return filePath } diff --git a/api/oauth/oauth.go b/api/oauth/oauth.go index 3088ca821..59b960d45 100644 --- a/api/oauth/oauth.go +++ b/api/oauth/oauth.go @@ -3,7 +3,7 @@ package oauth import ( "context" "encoding/json" - "io/ioutil" + "io" "mime" "net/http" "net/url" @@ -127,7 +127,7 @@ func getResource(token string, configuration *portainer.OAuthSettings) (map[stri } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } diff --git a/api/stacks/deployments/deployment_kubernetes_config.go b/api/stacks/deployments/deployment_kubernetes_config.go index faf974a8c..b94d6c4b4 100644 --- a/api/stacks/deployments/deployment_kubernetes_config.go +++ b/api/stacks/deployments/deployment_kubernetes_config.go @@ -2,7 +2,6 @@ package deployments import ( "fmt" - "io/ioutil" "os" "github.com/pkg/errors" @@ -41,7 +40,7 @@ func (config *KubernetesStackDeploymentConfig) Deploy() error { manifestFilePaths := make([]string, len(fileNames)) - tmpDir, err := ioutil.TempDir("", "kub_deployment") + tmpDir, err := os.MkdirTemp("", "kub_deployment") if err != nil { return errors.Wrap(err, "failed to create temp kub deployment directory") } @@ -50,7 +49,7 @@ func (config *KubernetesStackDeploymentConfig) Deploy() error { for _, fileName := range fileNames { manifestFilePath := filesystem.JoinPaths(tmpDir, fileName) - manifestContent, err := ioutil.ReadFile(filesystem.JoinPaths(config.stack.ProjectPath, fileName)) + manifestContent, err := os.ReadFile(filesystem.JoinPaths(config.stack.ProjectPath, fileName)) if err != nil { return errors.Wrap(err, "failed to read manifest file") }