fix(code): replace calls to ioutil EE-4425 (#7878)

pull/7880/head
andres-portainer 2022-10-17 15:29:12 -03:00 committed by GitHub
parent 69f498c431
commit 5488389278
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
48 changed files with 109 additions and 112 deletions

View File

@ -2,7 +2,6 @@ package archive
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -28,10 +27,10 @@ func listFiles(dir string) []string {
func Test_shouldCreateArhive(t *testing.T) { func Test_shouldCreateArhive(t *testing.T) {
tmpdir := t.TempDir() tmpdir := t.TempDir()
content := []byte("content") 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) os.MkdirAll(path.Join(tmpdir, "dir"), 0700)
ioutil.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600) os.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600)
ioutil.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600) os.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600)
gzPath, err := TarGzDir(tmpdir) gzPath, err := TarGzDir(tmpdir)
assert.Nil(t, err) assert.Nil(t, err)
@ -48,7 +47,7 @@ func Test_shouldCreateArhive(t *testing.T) {
wasExtracted := func(p string) { wasExtracted := func(p string) {
fullpath := path.Join(extractionDir, p) fullpath := path.Join(extractionDir, p)
assert.Contains(t, extractedFiles, fullpath) assert.Contains(t, extractedFiles, fullpath)
copyContent, _ := ioutil.ReadFile(fullpath) copyContent, _ := os.ReadFile(fullpath)
assert.Equal(t, content, copyContent) assert.Equal(t, content, copyContent)
} }
@ -60,10 +59,10 @@ func Test_shouldCreateArhive(t *testing.T) {
func Test_shouldCreateArhiveXXXXX(t *testing.T) { func Test_shouldCreateArhiveXXXXX(t *testing.T) {
tmpdir := t.TempDir() tmpdir := t.TempDir()
content := []byte("content") 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) os.MkdirAll(path.Join(tmpdir, "dir"), 0700)
ioutil.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600) os.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600)
ioutil.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600) os.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600)
gzPath, err := TarGzDir(tmpdir) gzPath, err := TarGzDir(tmpdir)
assert.Nil(t, err) assert.Nil(t, err)
@ -80,7 +79,7 @@ func Test_shouldCreateArhiveXXXXX(t *testing.T) {
wasExtracted := func(p string) { wasExtracted := func(p string) {
fullpath := path.Join(extractionDir, p) fullpath := path.Join(extractionDir, p)
assert.Contains(t, extractedFiles, fullpath) assert.Contains(t, extractedFiles, fullpath)
copyContent, _ := ioutil.ReadFile(fullpath) copyContent, _ := os.ReadFile(fullpath)
assert.Equal(t, content, copyContent) assert.Equal(t, content, copyContent)
} }

View File

@ -4,12 +4,12 @@ import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"fmt" "fmt"
"github.com/pkg/errors"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/pkg/errors"
) )
// UnzipArchive will unzip an archive from bytes into the dest destination folder on disk // 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() defer f.Close()
data, err := ioutil.ReadAll(f) data, err := io.ReadAll(f)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,9 +1,10 @@
package archive package archive
import ( import (
"github.com/stretchr/testify/assert"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestUnzipFile(t *testing.T) { func TestUnzipFile(t *testing.T) {

View File

@ -2,7 +2,6 @@ package crypto
import ( import (
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -20,7 +19,7 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) {
) )
content := []byte("content") content := []byte("content")
ioutil.WriteFile(originFilePath, content, 0600) os.WriteFile(originFilePath, content, 0600)
originFile, _ := os.Open(originFilePath) originFile, _ := os.Open(originFilePath)
defer originFile.Close() defer originFile.Close()
@ -30,7 +29,7 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) {
err := AesEncrypt(originFile, encryptedFileWriter, []byte("passphrase")) err := AesEncrypt(originFile, encryptedFileWriter, []byte("passphrase"))
assert.Nil(t, err, "Failed to encrypt a file") 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.Nil(t, err, "Couldn't read encrypted file")
assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted") assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted")
@ -45,7 +44,7 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) {
io.Copy(decryptedFileWriter, decryptedReader) io.Copy(decryptedFileWriter, decryptedReader)
decryptedContent, _ := ioutil.ReadFile(decryptedFilePath) decryptedContent, _ := os.ReadFile(decryptedFilePath)
assert.Equal(t, content, decryptedContent, "Original and decrypted content should match") 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") content := []byte("content")
ioutil.WriteFile(originFilePath, content, 0600) os.WriteFile(originFilePath, content, 0600)
originFile, _ := os.Open(originFilePath) originFile, _ := os.Open(originFilePath)
defer originFile.Close() defer originFile.Close()
@ -69,7 +68,7 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) {
err := AesEncrypt(originFile, encryptedFileWriter, []byte("")) err := AesEncrypt(originFile, encryptedFileWriter, []byte(""))
assert.Nil(t, err, "Failed to encrypt a file") 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.Nil(t, err, "Couldn't read encrypted file")
assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted") assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted")
@ -84,7 +83,7 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) {
io.Copy(decryptedFileWriter, decryptedReader) io.Copy(decryptedFileWriter, decryptedReader)
decryptedContent, _ := ioutil.ReadFile(decryptedFilePath) decryptedContent, _ := os.ReadFile(decryptedFilePath)
assert.Equal(t, content, decryptedContent, "Original and decrypted content should match") 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") content := []byte("content")
ioutil.WriteFile(originFilePath, content, 0600) os.WriteFile(originFilePath, content, 0600)
originFile, _ := os.Open(originFilePath) originFile, _ := os.Open(originFilePath)
defer originFile.Close() defer originFile.Close()
@ -108,7 +107,7 @@ func Test_decryptWithDifferentPassphrase_shouldProduceWrongResult(t *testing.T)
err := AesEncrypt(originFile, encryptedFileWriter, []byte("passphrase")) err := AesEncrypt(originFile, encryptedFileWriter, []byte("passphrase"))
assert.Nil(t, err, "Failed to encrypt a file") 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.Nil(t, err, "Couldn't read encrypted file")
assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted") assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted")
@ -123,6 +122,6 @@ func Test_decryptWithDifferentPassphrase_shouldProduceWrongResult(t *testing.T)
io.Copy(decryptedFileWriter, decryptedReader) io.Copy(decryptedFileWriter, decryptedReader)
decryptedContent, _ := ioutil.ReadFile(decryptedFilePath) decryptedContent, _ := os.ReadFile(decryptedFilePath)
assert.NotEqual(t, content, decryptedContent, "Original and decrypted content should NOT match") assert.NotEqual(t, content, decryptedContent, "Original and decrypted content should NOT match")
} }

View File

@ -3,7 +3,7 @@ package crypto
import ( import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"io/ioutil" "os"
) )
// CreateServerTLSConfiguration creates a basic tls.Config to be used by servers with recommended TLS settings // 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 != "" { if !skipServerVerification && caCertPath != "" {
caCert, err := ioutil.ReadFile(caCertPath) caCert, err := os.ReadFile(caCertPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"time" "time"
@ -167,7 +166,7 @@ func (connection *DbConnection) ExportRaw(filename string) error {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(filename, b, 0600) return os.WriteFile(filename, b, 0600)
} }
// ConvertToKey returns an 8-byte big endian representation of v. // ConvertToKey returns an 8-byte big endian representation of v.

View File

@ -3,7 +3,7 @@ package datastore
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "os"
"strconv" "strconv"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
@ -607,13 +607,13 @@ func (store *Store) Export(filename string) (err error) {
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(filename, b, 0600) return os.WriteFile(filename, b, 0600)
} }
func (store *Store) Import(filename string) (err error) { func (store *Store) Import(filename string) (err error) {
backup := storeExport{} backup := storeExport{}
s, err := ioutil.ReadFile(filename) s, err := os.ReadFile(filename)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,7 +1,7 @@
package exec package exec
import ( import (
"io/ioutil" "io"
"os" "os"
"path" "path"
"testing" "testing"
@ -55,7 +55,7 @@ func Test_createEnvFile(t *testing.T) {
assert.Equal(t, "stack.env", result) assert.Equal(t, "stack.env", result)
f, _ := os.Open(path.Join(dir, "stack.env")) f, _ := os.Open(path.Join(dir, "stack.env"))
content, _ := ioutil.ReadAll(f) content, _ := io.ReadAll(f)
assert.Equal(t, tt.expected, string(content)) assert.Equal(t, tt.expected, string(content))
} else { } else {
@ -80,7 +80,7 @@ func Test_createEnvFile_mergesDefultAndInplaceEnvVars(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.FileExists(t, path.Join(dir, "stack.env")) assert.FileExists(t, path.Join(dir, "stack.env"))
f, _ := os.Open(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) assert.Equal(t, []byte("VAR1=VAL1\nVAR2=VAL2\n\nVAR1=NEW_VAL1\nVAR3=VAL3\n"), content)
} }

View File

@ -1,7 +1,6 @@
package filesystem package filesystem
import ( import (
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -19,12 +18,12 @@ func Test_copyFile_returnsError_whenSourceDoesNotExist(t *testing.T) {
func Test_copyFile_shouldMakeAbackup(t *testing.T) { func Test_copyFile_shouldMakeAbackup(t *testing.T) {
tmpdir := t.TempDir() tmpdir := t.TempDir()
content := []byte("content") 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")) err := copyFile(path.Join(tmpdir, "origin"), path.Join(tmpdir, "copy"))
assert.NoError(t, err) assert.NoError(t, err)
copyContent, _ := ioutil.ReadFile(path.Join(tmpdir, "copy")) copyContent, _ := os.ReadFile(path.Join(tmpdir, "copy"))
assert.Equal(t, content, copyContent) assert.Equal(t, content, copyContent)
} }
@ -59,13 +58,13 @@ func Test_CopyPath_shouldSkipWhenNotExist(t *testing.T) {
func Test_CopyPath_shouldCopyFile(t *testing.T) { func Test_CopyPath_shouldCopyFile(t *testing.T) {
tmpdir := t.TempDir() tmpdir := t.TempDir()
content := []byte("content") 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) os.MkdirAll(path.Join(tmpdir, "backup"), 0700)
err := CopyPath(path.Join(tmpdir, "file"), path.Join(tmpdir, "backup")) err := CopyPath(path.Join(tmpdir, "file"), path.Join(tmpdir, "backup"))
assert.NoError(t, err) 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.NoError(t, err)
assert.Equal(t, content, copyContent) assert.Equal(t, content, copyContent)
} }

View File

@ -1,7 +1,7 @@
package filesystem package filesystem
import ( import (
"io/ioutil" "os"
"path" "path"
"testing" "testing"
@ -16,7 +16,7 @@ func Test_WriteFile_CanStoreContentInANewFile(t *testing.T) {
err := WriteToFile(tmpFilePath, content) err := WriteToFile(tmpFilePath, content)
assert.NoError(t, err) assert.NoError(t, err)
fileContent, _ := ioutil.ReadFile(tmpFilePath) fileContent, _ := os.ReadFile(tmpFilePath)
assert.Equal(t, content, fileContent) assert.Equal(t, content, fileContent)
} }
@ -31,7 +31,7 @@ func Test_WriteFile_CanOverwriteExistingFile(t *testing.T) {
err = WriteToFile(tmpFilePath, content) err = WriteToFile(tmpFilePath, content)
assert.NoError(t, err) assert.NoError(t, err)
fileContent, _ := ioutil.ReadFile(tmpFilePath) fileContent, _ := os.ReadFile(tmpFilePath)
assert.Equal(t, content, fileContent) assert.Equal(t, content, fileContent)
} }
@ -43,6 +43,6 @@ func Test_WriteFile_CanWriteANestedPath(t *testing.T) {
err := WriteToFile(tmpFilePath, content) err := WriteToFile(tmpFilePath, content)
assert.NoError(t, err) assert.NoError(t, err)
fileContent, _ := ioutil.ReadFile(tmpFilePath) fileContent, _ := os.ReadFile(tmpFilePath)
assert.Equal(t, content, fileContent) assert.Equal(t, content, fileContent)
} }

View File

@ -6,7 +6,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -100,7 +99,7 @@ func (a *azureClient) downloadZipFromAzureDevOps(ctx context.Context, opt cloneO
if err != nil { if err != nil {
return "", errors.WithMessage(err, "failed to build download url") 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 { if err != nil {
return "", errors.WithMessage(err, "failed to create temp file") return "", errors.WithMessage(err, "failed to create temp file")
} }

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
@ -33,7 +33,7 @@ func (service *Service) Authorization(configuration portainer.OpenAMTConfigurati
if err != nil { if err != nil {
return "", err return "", err
} }
responseBody, readErr := ioutil.ReadAll(response.Body) responseBody, readErr := io.ReadAll(response.Body)
if readErr != nil { if readErr != nil {
return "", readErr return "", readErr
} }

View File

@ -6,7 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"time" "time"
@ -99,7 +99,7 @@ func (service *Service) executeSaveRequest(method string, url string, token stri
if err != nil { if err != nil {
return nil, err return nil, err
} }
responseBody, readErr := ioutil.ReadAll(response.Body) responseBody, readErr := io.ReadAll(response.Body)
if readErr != nil { if readErr != nil {
return nil, readErr return nil, readErr
} }
@ -128,7 +128,7 @@ func (service *Service) executeGetRequest(url string, token string) ([]byte, err
if err != nil { if err != nil {
return nil, err return nil, err
} }
responseBody, readErr := ioutil.ReadAll(response.Body) responseBody, readErr := io.ReadAll(response.Body)
if readErr != nil { if readErr != nil {
return nil, readErr return nil, readErr
} }

View File

@ -5,7 +5,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -96,7 +96,7 @@ func Get(url string, timeout int) ([]byte, error) {
return nil, errInvalidResponseStatus return nil, errInvalidResponseStatus
} }
body, err := ioutil.ReadAll(response.Body) body, err := io.ReadAll(response.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -38,7 +37,7 @@ func listFiles(dir string) []string {
func contains(t *testing.T, list []string, path string) { func contains(t *testing.T, list []string, path string) {
assert.Contains(t, list, path) assert.Contains(t, list, path)
copyContent, _ := ioutil.ReadFile(path) copyContent, _ := os.ReadFile(path)
assert.Equal(t, "content\n", string(copyContent)) assert.Equal(t, "content\n", string(copyContent))
} }
@ -58,7 +57,7 @@ func Test_backupHandlerWithoutPassword_shouldCreateATarballArchive(t *testing.T)
tmpdir := t.TempDir() tmpdir := t.TempDir()
archivePath := filepath.Join(tmpdir, "archive.tar.gz") archivePath := filepath.Join(tmpdir, "archive.tar.gz")
err := ioutil.WriteFile(archivePath, body, 0600) err := os.WriteFile(archivePath, body, 0600)
if err != nil { if err != nil {
t.Fatal("Failed to save downloaded .tar.gz archive: ", err) t.Fatal("Failed to save downloaded .tar.gz archive: ", err)
} }

View File

@ -1,7 +1,7 @@
package edgegroups package edgegroups
import ( import (
"github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
) )
type endpointSetType map[portainer.EndpointID]bool type endpointSetType map[portainer.EndpointID]bool

View File

@ -2,7 +2,6 @@ package helm
import ( import (
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -192,7 +191,7 @@ func (handler *Handler) updateHelmAppManifest(r *http.Request, manifest []byte,
for _, resource := range yamlResources { for _, resource := range yamlResources {
resource := resource // https://golang.org/doc/faq#closures_and_goroutines resource := resource // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error { g.Go(func() error {
tmpfile, err := ioutil.TempFile("", "helm-manifest-*") tmpfile, err := os.CreateTemp("", "helm-manifest-*")
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create a tmp helm manifest file") return errors.Wrap(err, "failed to create a tmp helm manifest file")
} }

View File

@ -4,7 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"time" "time"
@ -139,7 +139,7 @@ func pullImage(ctx context.Context, docker *client.Client, imageName string) err
} }
defer out.Close() defer out.Close()
outputBytes, err := ioutil.ReadAll(out) outputBytes, err := io.ReadAll(out)
if err != nil { if err != nil {
log.Error().Str("image_name", imageName).Err(err).Msg("could not read image pull output") 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 return "", err
} }
outputBytes, err := ioutil.ReadAll(out) outputBytes, err := io.ReadAll(out)
if err != nil { if err != nil {
log.Error(). log.Error().
Str("image_name", imageName). Str("image_name", imageName).

View File

@ -3,7 +3,6 @@ package stacks
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -200,7 +199,7 @@ func (handler *Handler) deleteStack(userID portainer.UserID, stack *portainer.St
//then process the remove operation //then process the remove operation
if stack.IsComposeFormat { if stack.IsComposeFormat {
fileNames := stackutils.GetStackFilePaths(stack, false) fileNames := stackutils.GetStackFilePaths(stack, false)
tmpDir, err := ioutil.TempDir("", "kube_delete") tmpDir, err := os.MkdirTemp("", "kube_delete")
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create temp directory for deleting kub stack") return errors.Wrap(err, "failed to create temp directory for deleting kub stack")
} }

View File

@ -2,7 +2,6 @@ package stacks
import ( import (
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -105,7 +104,7 @@ func (handler *Handler) updateKubernetesStack(r *http.Request, stack *portainer.
return httperror.BadRequest("Failed to retrieve user token data", err) 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) defer os.RemoveAll(tempFileDir)
if err := filesystem.WriteToFile(filesystem.JoinPaths(tempFileDir, stack.EntryPoint), []byte(payload.StackFileContent)); err != nil { if err := filesystem.WriteToFile(filesystem.JoinPaths(tempFileDir, stack.EntryPoint), []byte(payload.StackFileContent)); err != nil {

View File

@ -2,7 +2,7 @@ package upload
import ( import (
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
"github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/http/security"
"net/http" "net/http"

View File

@ -1,9 +1,10 @@
package users package users
import ( import (
"github.com/portainer/portainer/api/dataservices/errors"
"net/http" "net/http"
"github.com/portainer/portainer/api/dataservices/errors"
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"

View File

@ -2,9 +2,10 @@ package webhooks
import ( import (
"errors" "errors"
"github.com/portainer/portainer/api/http/security"
"net/http" "net/http"
"github.com/portainer/portainer/api/http/security"
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"

View File

@ -3,11 +3,12 @@ package webhooks
import ( import (
"context" "context"
"errors" "errors"
"github.com/portainer/portainer/api/internal/registryutils"
"io" "io"
"net/http" "net/http"
"strings" "strings"
"github.com/portainer/portainer/api/internal/registryutils"
dockertypes "github.com/docker/docker/api/types" dockertypes "github.com/docker/docker/api/types"
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"

View File

@ -1,9 +1,10 @@
package webhooks package webhooks
import ( import (
"github.com/portainer/portainer/api/http/security"
"net/http" "net/http"
"github.com/portainer/portainer/api/http/security"
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"

View File

@ -4,8 +4,9 @@
package websocket package websocket
import ( import (
"github.com/Microsoft/go-winio"
"net" "net"
"github.com/Microsoft/go-winio"
) )
func createDial(scheme, host string) (net.Conn, error) { func createDial(scheme, host string) (net.Conn, error) {

View File

@ -5,7 +5,7 @@ import (
"net" "net"
"net/url" "net/url"
"github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/crypto" "github.com/portainer/portainer/api/crypto"
) )

View File

@ -8,7 +8,7 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/koding/websocketproxy" "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 { func (handler *Handler) proxyEdgeAgentWebsocketRequest(w http.ResponseWriter, r *http.Request, params *webSocketRequestParams) error {

View File

@ -1,6 +1,6 @@
package websocket package websocket
import "github.com/portainer/portainer/api" import portainer "github.com/portainer/portainer/api"
type webSocketRequestParams struct { type webSocketRequestParams struct {
ID string ID string

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io"
"mime" "mime"
"net/http" "net/http"
@ -37,7 +37,7 @@ func buildOperation(request *http.Request) error {
var buffer []byte var buffer []byte
switch mediaType { switch mediaType {
case "": case "":
body, err := ioutil.ReadAll(request.Body) body, err := io.ReadAll(request.Body)
if err != nil { if err != nil {
return err 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") 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 { if err != nil {
return err return err
} }
@ -105,7 +105,7 @@ func buildOperation(request *http.Request) error {
return nil return nil
} }
request.Body = ioutil.NopCloser(bytes.NewReader(buffer)) request.Body = io.NopCloser(bytes.NewReader(buffer))
request.ContentLength = int64(len(buffer)) request.ContentLength = int64(len(buffer))
request.Header.Set("Content-Type", "application/x-tar") request.Header.Set("Content-Type", "application/x-tar")

View File

@ -5,7 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io"
"net/http" "net/http"
"strings" "strings"
@ -190,7 +190,7 @@ func (transport *Transport) decorateContainerCreationOperation(request *http.Req
return nil, err return nil, err
} }
body, err := ioutil.ReadAll(request.Body) body, err := io.ReadAll(request.Body)
if err != nil { if err != nil {
return nil, err 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) response, err := transport.executeDockerRequest(request)

View File

@ -5,7 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"io/ioutil" "io"
"net/http" "net/http"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
@ -116,7 +116,7 @@ func (transport *Transport) decorateServiceCreationOperation(request *http.Reque
return nil, err return nil, err
} }
body, err := ioutil.ReadAll(request.Body) body, err := io.ReadAll(request.Body)
if err != nil { if err != nil {
return nil, err 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) return transport.replaceRegistryAuthenticationHeader(request)

View File

@ -6,7 +6,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"path" "path"
"regexp" "regexp"
@ -197,7 +197,7 @@ func (transport *Transport) proxyAgentRequest(r *http.Request) (*http.Response,
r.Method = http.MethodPost r.Method = http.MethodPost
r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) r.Body = io.NopCloser(bytes.NewReader(newBody))
r.ContentLength = int64(len(newBody)) r.ContentLength = int64(len(newBody))
} }

View File

@ -1,7 +1,7 @@
package kubernetes package kubernetes
import ( import (
"io/ioutil" "os"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/dataservices"
@ -28,7 +28,7 @@ func NewTokenManager(kubecli portainer.KubeClient, dataStore dataservices.DataSt
} }
if setLocalAdminToken { if setLocalAdminToken {
token, err := ioutil.ReadFile(defaultServiceAccountTokenFile) token, err := os.ReadFile(defaultServiceAccountTokenFile)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"path" "path"
"regexp" "regexp"
@ -189,7 +189,7 @@ func decorateAgentDockerHubRequest(r *http.Request, dataStore dataservices.DataS
} }
r.Method = http.MethodPost r.Method = http.MethodPost
r.Body = ioutil.NopCloser(bytes.NewReader(newBody)) r.Body = io.NopCloser(bytes.NewReader(newBody))
r.ContentLength = int64(len(newBody)) r.ContentLength = int64(len(newBody))
return nil return nil

View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"mime" "mime"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -50,7 +49,7 @@ func getBody(body io.ReadCloser, contentType string, isGzip bool) (interface{},
defer reader.Close() defer reader.Close()
bodyBytes, err := ioutil.ReadAll(reader) bodyBytes, err := io.ReadAll(reader)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -2,7 +2,7 @@ package utils
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"net/http" "net/http"
"strconv" "strconv"
) )
@ -25,7 +25,7 @@ func RewriteRequest(request *http.Request, newData interface{}) error {
return err return err
} }
body := ioutil.NopCloser(bytes.NewReader(data)) body := io.NopCloser(bytes.NewReader(data))
request.Body = body request.Body = body
request.ContentLength = int64(len(data)) request.ContentLength = int64(len(data))

View File

@ -3,7 +3,7 @@ package utils
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"strconv" "strconv"
@ -82,7 +82,7 @@ func RewriteResponse(response *http.Response, newResponseData interface{}, statu
return err return err
} }
body := ioutil.NopCloser(bytes.NewReader(data)) body := io.NopCloser(bytes.NewReader(data))
response.StatusCode = statusCode response.StatusCode = statusCode
response.Body = body response.Body = body

View File

@ -3,7 +3,7 @@ package edge
import ( import (
"errors" "errors"
"github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
) )
// EdgeStackRelatedEndpoints returns a list of environments(endpoints) related to this Edge stack // EdgeStackRelatedEndpoints returns a list of environments(endpoints) related to this Edge stack

View File

@ -1,6 +1,6 @@
package edge 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) // 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 { func EndpointRelatedEdgeStacks(endpoint *portainer.Endpoint, endpointGroup *portainer.EndpointGroup, edgeGroups []portainer.EdgeGroup, edgeStacks []portainer.EdgeStack) []portainer.EdgeStackID {

View File

@ -1,6 +1,6 @@
package tag package tag
import "github.com/portainer/portainer/api" import portainer "github.com/portainer/portainer/api"
type tagSet map[portainer.TagID]bool type tagSet map[portainer.TagID]bool

View File

@ -1,10 +1,11 @@
package jwt package jwt
import ( import (
i "github.com/portainer/portainer/api/internal/testhelpers"
"testing" "testing"
"time" "time"
i "github.com/portainer/portainer/api/internal/testhelpers"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"

View File

@ -1,14 +1,15 @@
package cli package cli
import ( import (
"reflect"
"testing"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
kfake "k8s.io/client-go/kubernetes/fake" kfake "k8s.io/client-go/kubernetes/fake"
"reflect"
"testing"
) )
func newNodes() *v1.NodeList { func newNodes() *v1.NodeList {

View File

@ -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 // 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. // 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 { func (kcl *KubeClient) SetupUserServiceAccount(userID int, teamIDs []int, restrictDefaultNamespace bool) error {
serviceAccountName := UserServiceAccountName(userID, kcl.instanceID) serviceAccountName := UserServiceAccountName(userID, kcl.instanceID)

View File

@ -5,7 +5,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
@ -63,7 +63,7 @@ func getCertificateAuthorityData(tlsCertPath string) (string, error) {
return "", errTLSCertNotProvided return "", errTLSCertNotProvided
} }
data, err := ioutil.ReadFile(tlsCertPath) data, err := os.ReadFile(tlsCertPath)
if err != nil { if err != nil {
return "", errors.Wrap(errTLSCertFileMissing, err.Error()) return "", errors.Wrap(errTLSCertFileMissing, err.Error())
} }

View File

@ -2,7 +2,7 @@ package kubernetes
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"strings" "strings"
"testing" "testing"
@ -38,7 +38,7 @@ const certDataString = "MIIC5TCCAc2gAwIBAgIJAJ+poiEBdsplMA0GCSqGSIb3DQEBCwUAMBQx
func createTempFile(filename, content string, t *testing.T) string { func createTempFile(filename, content string, t *testing.T) string {
tempPath := t.TempDir() tempPath := t.TempDir()
filePath := fmt.Sprintf("%s/%s", tempPath, filename) filePath := fmt.Sprintf("%s/%s", tempPath, filename)
ioutil.WriteFile(filePath, []byte(content), 0644) os.WriteFile(filePath, []byte(content), 0644)
return filePath return filePath
} }

View File

@ -3,7 +3,7 @@ package oauth
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"io/ioutil" "io"
"mime" "mime"
"net/http" "net/http"
"net/url" "net/url"
@ -127,7 +127,7 @@ func getResource(token string, configuration *portainer.OAuthSettings) (map[stri
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -2,7 +2,6 @@ package deployments
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -41,7 +40,7 @@ func (config *KubernetesStackDeploymentConfig) Deploy() error {
manifestFilePaths := make([]string, len(fileNames)) manifestFilePaths := make([]string, len(fileNames))
tmpDir, err := ioutil.TempDir("", "kub_deployment") tmpDir, err := os.MkdirTemp("", "kub_deployment")
if err != nil { if err != nil {
return errors.Wrap(err, "failed to create temp kub deployment directory") return errors.Wrap(err, "failed to create temp kub deployment directory")
} }
@ -50,7 +49,7 @@ func (config *KubernetesStackDeploymentConfig) Deploy() error {
for _, fileName := range fileNames { for _, fileName := range fileNames {
manifestFilePath := filesystem.JoinPaths(tmpDir, fileName) 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 { if err != nil {
return errors.Wrap(err, "failed to read manifest file") return errors.Wrap(err, "failed to read manifest file")
} }