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:
Jo
2022-08-10 13:37:51 +00:00
committed by GitHub
parent 4926767737
commit 062d255124
140 changed files with 462 additions and 492 deletions
+5 -5
View File
@@ -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)
}
}
+6 -5
View File
@@ -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 -3
View File
@@ -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 -2
View File
@@ -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))
})
+2 -2
View File
@@ -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))
}