Handle ioutil deprecations (#53526)
* replace ioutil.ReadFile -> os.ReadFile * replace ioutil.ReadAll -> io.ReadAll * replace ioutil.TempFile -> os.CreateTemp * replace ioutil.NopCloser -> io.NopCloser * replace ioutil.WriteFile -> os.WriteFile * replace ioutil.TempDir -> os.MkdirTemp * replace ioutil.Discard -> io.Discard
This commit is contained in:
@@ -11,9 +11,9 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
@@ -188,7 +188,7 @@ func newNotFound(cfg *setting.Cfg) *Avatar {
|
||||
// It's safe to ignore gosec warning G304 since the variable part of the file path comes from a configuration
|
||||
// variable.
|
||||
// nolint:gosec
|
||||
if data, err := ioutil.ReadFile(path); err != nil {
|
||||
if data, err := os.ReadFile(path); err != nil {
|
||||
alog.Error("Failed to read user_profile.png", "path", path)
|
||||
} else {
|
||||
avatar.data = bytes.NewBuffer(data)
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -77,7 +77,7 @@ func TestGetHomeDashboard(t *testing.T) {
|
||||
dash.Meta.IsHome = true
|
||||
dash.Meta.FolderTitle = "General"
|
||||
|
||||
homeDashJSON, err := ioutil.ReadFile(tc.expectedDashboardPath)
|
||||
homeDashJSON, err := os.ReadFile(tc.expectedDashboardPath)
|
||||
require.NoError(t, err, "must be able to read expected dashboard file")
|
||||
hs.Cfg.DefaultHomeDashboardPath = tc.defaultSetting
|
||||
bytes, err := simplejson.NewJson(homeDashJSON)
|
||||
|
||||
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -68,7 +67,7 @@ func logSentryEventScenario(t *testing.T, desc string, event frontendlogging.Fro
|
||||
return nil, errors.New("epic hard drive failure")
|
||||
}
|
||||
if strings.HasSuffix(path, "foo.js.map") {
|
||||
f, err := ioutil.ReadFile("./frontendlogging/test-data/foo.js.map")
|
||||
f, err := os.ReadFile("./frontendlogging/test-data/foo.js.map")
|
||||
require.NoError(t, err)
|
||||
return f, nil
|
||||
}
|
||||
@@ -140,7 +139,7 @@ func logGrafanaJavascriptAgentEventScenario(t *testing.T, desc string, event fro
|
||||
return nil, errors.New("epic hard drive failure")
|
||||
}
|
||||
if strings.HasSuffix(path, "foo.js.map") {
|
||||
f, err := ioutil.ReadFile("./frontendlogging/test-data/foo.js.map")
|
||||
f, err := os.ReadFile("./frontendlogging/test-data/foo.js.map")
|
||||
require.NoError(t, err)
|
||||
return f, nil
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package frontendlogging
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -39,7 +39,7 @@ func ReadSourceMapFromFS(dir string, path string) ([]byte, error) {
|
||||
logger.Error("Failed to close source map file", "err", err)
|
||||
}
|
||||
}()
|
||||
return ioutil.ReadAll(file)
|
||||
return io.ReadAll(file)
|
||||
}
|
||||
|
||||
type SourceMapStore struct {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -61,7 +60,7 @@ func fakeViewIndex(t *testing.T) {
|
||||
}
|
||||
|
||||
func getBody(resp *httptest.ResponseRecorder) (string, error) {
|
||||
responseData, err := ioutil.ReadAll(resp.Body)
|
||||
responseData, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
@@ -89,7 +89,7 @@ func TestGetPluginDashboards(t *testing.T) {
|
||||
resp, err := sendGetPluginDashboardsRequestForSignedInUser(t, s, existingPluginID, user)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
bytes, err := ioutil.ReadAll(resp.Body)
|
||||
bytes, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
var listResp []*plugindashboards.PluginDashboard
|
||||
|
||||
@@ -2,7 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
@@ -39,7 +39,7 @@ func TestPluginMetricsEndpoint(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, resp)
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "http_errors=2", string(body))
|
||||
require.NoError(t, resp.Body.Close())
|
||||
@@ -53,7 +53,7 @@ func TestPluginMetricsEndpoint(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, resp)
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, string(body))
|
||||
require.NoError(t, resp.Body.Close())
|
||||
@@ -106,7 +106,7 @@ func TestPluginMetricsEndpoint(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, resp)
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "http_errors=2", string(body))
|
||||
require.NoError(t, resp.Body.Close())
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
@@ -121,7 +120,7 @@ func (hs *HTTPServer) makePluginResourceRequest(w http.ResponseWriter, req *http
|
||||
proxyutil.ClearCookieHeader(req, keepCookieModel.KeepCookies)
|
||||
proxyutil.PrepareProxyRequest(req)
|
||||
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
body, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read request body: %w", err)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@@ -106,7 +106,7 @@ func (proxy *DataSourceProxy) HandleRequest() {
|
||||
modifyResponse := func(resp *http.Response) error {
|
||||
if resp.StatusCode == 401 {
|
||||
// The data source rejected the request as unauthorized, convert to 400 (bad request)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read data source response body: %w", err)
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func (proxy *DataSourceProxy) HandleRequest() {
|
||||
*resp = http.Response{
|
||||
StatusCode: 400,
|
||||
Status: "Bad Request",
|
||||
Body: ioutil.NopCloser(strings.NewReader(msg)),
|
||||
Body: io.NopCloser(strings.NewReader(msg)),
|
||||
ContentLength: int64(len(msg)),
|
||||
Header: http.Header{},
|
||||
}
|
||||
@@ -324,9 +324,9 @@ func (proxy *DataSourceProxy) logRequest() {
|
||||
|
||||
var body string
|
||||
if proxy.ctx.Req.Body != nil {
|
||||
buffer, err := ioutil.ReadAll(proxy.ctx.Req.Body)
|
||||
buffer, err := io.ReadAll(proxy.ctx.Req.Body)
|
||||
if err == nil {
|
||||
proxy.ctx.Req.Body = ioutil.NopCloser(bytes.NewBuffer(buffer))
|
||||
proxy.ctx.Req.Body = io.NopCloser(bytes.NewBuffer(buffer))
|
||||
body = string(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,11 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -176,7 +177,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
|
||||
proxy.matchedRoute = routes[5]
|
||||
ApplyRoute(proxy.ctx.Req.Context(), req, proxy.proxyPath, proxy.matchedRoute, dsInfo, cfg)
|
||||
|
||||
content, err := ioutil.ReadAll(req.Body)
|
||||
content, err := io.ReadAll(req.Body)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, `{ "url": "https://dynamic.grafana.com", "secret": "123" }`, string(content))
|
||||
})
|
||||
@@ -275,7 +276,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
|
||||
var authorizationHeaderCall2 string
|
||||
|
||||
t.Run("first call should add authorization header with access token", func(t *testing.T) {
|
||||
json, err := ioutil.ReadFile("./test-data/access-token-1.json")
|
||||
json, err := os.ReadFile("./test-data/access-token-1.json")
|
||||
require.NoError(t, err)
|
||||
|
||||
originalClient := client
|
||||
@@ -303,7 +304,7 @@ func TestDataSourceProxy_routeRule(t *testing.T) {
|
||||
assert.True(t, strings.HasPrefix(authorizationHeaderCall1, "Bearer eyJ0e"))
|
||||
|
||||
t.Run("second call to another route should add a different access token", func(t *testing.T) {
|
||||
json2, err := ioutil.ReadFile("./test-data/access-token-2.json")
|
||||
json2, err := os.ReadFile("./test-data/access-token-2.json")
|
||||
require.NoError(t, err)
|
||||
|
||||
req, err := http.NewRequest("GET", "http://localhost/asd", nil)
|
||||
@@ -887,7 +888,7 @@ func (c *httpClientStub) Do(req *http.Request) (*http.Response, error) {
|
||||
body, err := bodyJSON.MarshalJSON()
|
||||
require.NoError(c.t, err)
|
||||
resp := &http.Response{
|
||||
Body: ioutil.NopCloser(bytes.NewReader(body)),
|
||||
Body: io.NopCloser(bytes.NewReader(body)),
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
|
||||
@@ -3,7 +3,7 @@ package pluginproxy
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
@@ -107,9 +107,9 @@ func logAppPluginProxyRequest(appID string, cfg *setting.Cfg, c *models.ReqConte
|
||||
|
||||
var body string
|
||||
if c.Req.Body != nil {
|
||||
buffer, err := ioutil.ReadAll(c.Req.Body)
|
||||
buffer, err := io.ReadAll(c.Req.Body)
|
||||
if err == nil {
|
||||
c.Req.Body = ioutil.NopCloser(bytes.NewBuffer(buffer))
|
||||
c.Req.Body = io.NopCloser(bytes.NewBuffer(buffer))
|
||||
body = string(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package pluginproxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -241,7 +241,7 @@ func TestPluginProxy(t *testing.T) {
|
||||
route,
|
||||
store,
|
||||
)
|
||||
content, err := ioutil.ReadAll(req.Body)
|
||||
content, err := io.ReadAll(req.Body)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, `{ "url": "https://dynamic.grafana.com", "secret": "123" }`, string(content))
|
||||
})
|
||||
|
||||
@@ -3,7 +3,7 @@ package pluginproxy
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"text/template"
|
||||
@@ -78,7 +78,7 @@ func setBodyContent(req *http.Request, route *plugins.Route, data templateData)
|
||||
return err
|
||||
}
|
||||
|
||||
req.Body = ioutil.NopCloser(strings.NewReader(interpolatedBody))
|
||||
req.Body = io.NopCloser(strings.NewReader(interpolatedBody))
|
||||
req.ContentLength = int64(len(interpolatedBody))
|
||||
}
|
||||
|
||||
|
||||
+1
-2
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
@@ -466,7 +465,7 @@ func (hs *HTTPServer) pluginMarkdown(ctx context.Context, pluginId string, name
|
||||
// nolint:gosec
|
||||
// We can ignore the gosec G304 warning since we have cleaned the requested file path and subsequently
|
||||
// use this with a prefix of the plugin's directory, which is set during plugin loading
|
||||
data, err := ioutil.ReadFile(path)
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
@@ -101,9 +100,9 @@ func Test_PluginsInstallAndUninstall(t *testing.T) {
|
||||
func Test_GetPluginAssets(t *testing.T) {
|
||||
pluginID := "test-plugin"
|
||||
pluginDir := "."
|
||||
tmpFile, err := ioutil.TempFile(pluginDir, "")
|
||||
tmpFile, err := os.CreateTemp(pluginDir, "")
|
||||
require.NoError(t, err)
|
||||
tmpFileInParentDir, err := ioutil.TempFile("..", "")
|
||||
tmpFileInParentDir, err := os.CreateTemp("..", "")
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
err := os.RemoveAll(tmpFile.Name())
|
||||
|
||||
@@ -2,7 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -60,7 +60,7 @@ func TestAPIEndpoint_GetCurrentOrgPreferences_LegacyAccessControl(t *testing.T)
|
||||
response := callAPI(sc.server, http.MethodGet, getOrgPreferencesURL, nil, t)
|
||||
assert.Equal(t, http.StatusOK, response.Code)
|
||||
var resp map[string]interface{}
|
||||
b, err := ioutil.ReadAll(response.Body)
|
||||
b, err := io.ReadAll(response.Body)
|
||||
assert.NoError(t, err)
|
||||
assert.NoError(t, json.Unmarshal(b, &resp))
|
||||
assert.Equal(t, "home", resp["homeDashboardUID"])
|
||||
|
||||
Reference in New Issue
Block a user