Merge pull request #35509 from cpuguy83/splunk_dont_readall

Splunk: limit the reader size on error responses
This commit is contained in:
Yong Tang 2018-01-05 12:34:35 -08:00 committed by GitHub
commit 39faf2a3a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,6 +22,7 @@ import (
"github.com/docker/docker/daemon/logger"
"github.com/docker/docker/daemon/logger/loggerutils"
"github.com/docker/docker/pkg/pools"
"github.com/docker/docker/pkg/urlutil"
"github.com/sirupsen/logrus"
)
@ -55,6 +56,8 @@ const (
defaultBufferMaximum = 10 * defaultPostMessagesBatchSize
// Number of messages allowed to be queued in the channel
defaultStreamChannelSize = 4 * defaultPostMessagesBatchSize
// maxResponseSize is the max amount that will be read from an http response
maxResponseSize = 1024
)
const (
@ -500,20 +503,22 @@ func (l *splunkLogger) tryPostMessages(ctx context.Context, messages []*splunkMe
if l.gzipCompression {
req.Header.Set("Content-Encoding", "gzip")
}
res, err := l.client.Do(req)
resp, err := l.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
var body []byte
body, err = ioutil.ReadAll(res.Body)
defer func() {
pools.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
rdr := io.LimitReader(resp.Body, maxResponseSize)
body, err := ioutil.ReadAll(rdr)
if err != nil {
return err
}
return fmt.Errorf("%s: failed to send event - %s - %s", driverName, res.Status, body)
return fmt.Errorf("%s: failed to send event - %s - %s", driverName, resp.Status, string(body))
}
io.Copy(ioutil.Discard, res.Body)
return nil
}
@ -596,20 +601,22 @@ func verifySplunkConnection(l *splunkLogger) error {
if err != nil {
return err
}
res, err := l.client.Do(req)
resp, err := l.client.Do(req)
if err != nil {
return err
}
if res.Body != nil {
defer res.Body.Close()
}
if res.StatusCode != http.StatusOK {
var body []byte
body, err = ioutil.ReadAll(res.Body)
defer func() {
pools.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
rdr := io.LimitReader(resp.Body, maxResponseSize)
body, err := ioutil.ReadAll(rdr)
if err != nil {
return err
}
return fmt.Errorf("%s: failed to verify connection - %s - %s", driverName, res.Status, body)
return fmt.Errorf("%s: failed to verify connection - %s - %s", driverName, resp.Status, string(body))
}
return nil
}