Chore: Disable default golangci-lint filter (#29751)
* Disable default golangci-lint filter Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Chore: Fix linter warnings Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
@@ -111,11 +111,17 @@ func InstallPlugin(pluginName, version string, c utils.CommandLine, client utils
|
||||
if err != nil {
|
||||
return errutil.Wrap("failed to create temporary file", err)
|
||||
}
|
||||
defer os.Remove(tmpFile.Name())
|
||||
defer func() {
|
||||
if err := os.Remove(tmpFile.Name()); err != nil {
|
||||
logger.Warn("Failed to remove temporary file", "file", tmpFile.Name(), "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
err = client.DownloadFile(pluginName, tmpFile, downloadURL, checksum)
|
||||
if err != nil {
|
||||
tmpFile.Close()
|
||||
if err := tmpFile.Close(); err != nil {
|
||||
logger.Warn("Failed to close file", "err", err)
|
||||
}
|
||||
return errutil.Wrap("failed to download plugin archive", err)
|
||||
}
|
||||
err = tmpFile.Close()
|
||||
@@ -228,6 +234,8 @@ func extractFiles(archiveFile string, pluginName string, filePath string, allowS
|
||||
newFile := filepath.Join(filePath, newFileName)
|
||||
|
||||
if zf.FileInfo().IsDir() {
|
||||
// We can ignore gosec G304 here since it makes sense to give all users read access
|
||||
// nolint:gosec
|
||||
if err := os.MkdirAll(newFile, 0755); err != nil {
|
||||
if os.IsPermission(err) {
|
||||
return fmt.Errorf(permissionsDeniedMessage, newFile)
|
||||
@@ -240,6 +248,8 @@ func extractFiles(archiveFile string, pluginName string, filePath string, allowS
|
||||
}
|
||||
|
||||
// Create needed directories to extract file
|
||||
// We can ignore gosec G304 here since it makes sense to give all users read access
|
||||
// nolint:gosec
|
||||
if err := os.MkdirAll(filepath.Dir(newFile), 0755); err != nil {
|
||||
return errutil.Wrap("failed to create directory to extract plugin files", err)
|
||||
}
|
||||
|
||||
@@ -215,7 +215,7 @@ func setupFakePluginsDir(t *testing.T) (string, func()) {
|
||||
err := os.RemoveAll(dirname)
|
||||
require.Nil(t, err)
|
||||
|
||||
err = os.MkdirAll(dirname, 0774)
|
||||
err = os.MkdirAll(dirname, 0750)
|
||||
require.Nil(t, err)
|
||||
|
||||
return dirname, func() {
|
||||
|
||||
@@ -94,14 +94,20 @@ func (client *GrafanaComClient) DownloadFile(pluginName string, tmpFile *os.File
|
||||
if err != nil {
|
||||
return errutil.Wrap("Failed to send request", err)
|
||||
}
|
||||
defer bodyReader.Close()
|
||||
defer func() {
|
||||
if err := bodyReader.Close(); err != nil {
|
||||
logger.Warn("Failed to close body", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
w := bufio.NewWriter(tmpFile)
|
||||
h := md5.New()
|
||||
if _, err = io.Copy(w, io.TeeReader(bodyReader, h)); err != nil {
|
||||
return errutil.Wrap("Failed to compute MD5 checksum", err)
|
||||
}
|
||||
w.Flush()
|
||||
if err := w.Flush(); err != nil {
|
||||
return fmt.Errorf("failed to write to %q: %w", tmpFile.Name(), err)
|
||||
}
|
||||
if len(checksum) > 0 && checksum != fmt.Sprintf("%x", h.Sum(nil)) {
|
||||
return fmt.Errorf("expected MD5 checksum does not match the downloaded archive - please contact security@grafana.com")
|
||||
}
|
||||
@@ -131,7 +137,11 @@ func sendRequestGetBytes(client http.Client, repoUrl string, subPaths ...string)
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
}
|
||||
defer bodyReader.Close()
|
||||
defer func() {
|
||||
if err := bodyReader.Close(); err != nil {
|
||||
logger.Warn("Failed to close stream", "err", err)
|
||||
}
|
||||
}()
|
||||
return ioutil.ReadAll(bodyReader)
|
||||
}
|
||||
|
||||
@@ -182,7 +192,11 @@ func handleResponse(res *http.Response) (io.ReadCloser, error) {
|
||||
|
||||
if res.StatusCode/100 == 4 {
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
defer res.Body.Close()
|
||||
defer func() {
|
||||
if err := res.Body.Close(); err != nil {
|
||||
logger.Warn("Failed to close response body", "err", err)
|
||||
}
|
||||
}()
|
||||
if err != nil || len(body) == 0 {
|
||||
return nil, &BadRequestError{Status: res.Status}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,9 @@ import (
|
||||
|
||||
func TestHandleResponse(t *testing.T) {
|
||||
t.Run("Returns body if status == 200", func(t *testing.T) {
|
||||
resp := makeResponse(200, "test")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 200, "test")
|
||||
bodyReader, err := handleResponse(resp)
|
||||
require.NoError(t, err)
|
||||
body, err := ioutil.ReadAll(bodyReader)
|
||||
@@ -24,54 +25,68 @@ func TestHandleResponse(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Returns ErrorNotFound if status == 404", func(t *testing.T) {
|
||||
resp := makeResponse(404, "")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 404, "")
|
||||
_, err := handleResponse(resp)
|
||||
assert.Equal(t, ErrNotFoundError, err)
|
||||
})
|
||||
|
||||
t.Run("Returns message from body if status == 400", func(t *testing.T) {
|
||||
resp := makeResponse(400, "{ \"message\": \"error_message\" }")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 400, "{ \"message\": \"error_message\" }")
|
||||
_, err := handleResponse(resp)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "error_message", asBadRequestError(t, err).Message)
|
||||
})
|
||||
|
||||
t.Run("Returns body if status == 400 and no message key", func(t *testing.T) {
|
||||
resp := makeResponse(400, "{ \"test\": \"test_message\"}")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 400, "{ \"test\": \"test_message\"}")
|
||||
_, err := handleResponse(resp)
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "{ \"test\": \"test_message\"}", asBadRequestError(t, err).Message)
|
||||
})
|
||||
|
||||
t.Run("Returns Bad request error if status == 400 and no body", func(t *testing.T) {
|
||||
resp := makeResponse(400, "")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 400, "")
|
||||
_, err := handleResponse(resp)
|
||||
require.Error(t, err)
|
||||
_ = asBadRequestError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Returns error with invalid status if status == 500", func(t *testing.T) {
|
||||
resp := makeResponse(500, "")
|
||||
defer resp.Body.Close()
|
||||
// The body gets closed within makeResponse
|
||||
// nolint:bodyclose
|
||||
resp := makeResponse(t, 500, "")
|
||||
_, err := handleResponse(resp)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid status")
|
||||
})
|
||||
}
|
||||
|
||||
func makeResponse(status int, body string) *http.Response {
|
||||
func makeResponse(t *testing.T, status int, body string) *http.Response {
|
||||
t.Helper()
|
||||
|
||||
return &http.Response{
|
||||
StatusCode: status,
|
||||
Body: makeBody(body),
|
||||
Body: makeBody(t, body),
|
||||
}
|
||||
}
|
||||
|
||||
func makeBody(body string) io.ReadCloser {
|
||||
return ioutil.NopCloser(bytes.NewReader([]byte(body)))
|
||||
func makeBody(t *testing.T, body string) io.ReadCloser {
|
||||
t.Helper()
|
||||
|
||||
reader := ioutil.NopCloser(bytes.NewReader([]byte(body)))
|
||||
t.Cleanup(func() {
|
||||
err := reader.Close()
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
return reader
|
||||
}
|
||||
|
||||
func asBadRequestError(t *testing.T, err error) *BadRequestError {
|
||||
|
||||
@@ -108,7 +108,11 @@ func main() {
|
||||
}
|
||||
|
||||
func executeServer(configFile, homePath, pidFile, packaging string, traceDiagnostics *tracingDiagnostics) error {
|
||||
defer log.Close()
|
||||
defer func() {
|
||||
if err := log.Close(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to close log: %s\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if traceDiagnostics.enabled {
|
||||
fmt.Println("diagnostics: tracing enabled", "file", traceDiagnostics.file)
|
||||
@@ -183,7 +187,9 @@ func listenToSystemSignals(s *server.Server) {
|
||||
for {
|
||||
select {
|
||||
case <-sighupChan:
|
||||
log.Reload()
|
||||
if err := log.Reload(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to reload loggers: %s\n", err)
|
||||
}
|
||||
case sig := <-signalChan:
|
||||
s.Shutdown(fmt.Sprintf("System signal: %s", sig))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user