Add http_timeout to audit webhook configurations (#20421)

This commit is contained in:
Sveinn 2024-09-11 22:20:42 +00:00 committed by GitHub
parent bc527eceda
commit 3bae73fb42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 35 deletions

View File

@ -51,6 +51,7 @@ const (
MaxRetry = "max_retry" MaxRetry = "max_retry"
RetryInterval = "retry_interval" RetryInterval = "retry_interval"
Proxy = "proxy" Proxy = "proxy"
httpTimeout = "http_timeout"
KafkaBrokers = "brokers" KafkaBrokers = "brokers"
KafkaTopic = "topic" KafkaTopic = "topic"
@ -89,6 +90,7 @@ const (
EnvAuditWebhookQueueDir = "MINIO_AUDIT_WEBHOOK_QUEUE_DIR" EnvAuditWebhookQueueDir = "MINIO_AUDIT_WEBHOOK_QUEUE_DIR"
EnvAuditWebhookMaxRetry = "MINIO_AUDIT_WEBHOOK_MAX_RETRY" EnvAuditWebhookMaxRetry = "MINIO_AUDIT_WEBHOOK_MAX_RETRY"
EnvAuditWebhookRetryInterval = "MINIO_AUDIT_WEBHOOK_RETRY_INTERVAL" EnvAuditWebhookRetryInterval = "MINIO_AUDIT_WEBHOOK_RETRY_INTERVAL"
EnvAuditWebhookHTTPTimeout = "MINIO_AUDIT_WEBHOOK_HTTP_TIMEOUT"
EnvKafkaEnable = "MINIO_AUDIT_KAFKA_ENABLE" EnvKafkaEnable = "MINIO_AUDIT_KAFKA_ENABLE"
EnvKafkaBrokers = "MINIO_AUDIT_KAFKA_BROKERS" EnvKafkaBrokers = "MINIO_AUDIT_KAFKA_BROKERS"
@ -162,6 +164,10 @@ var (
Key: RetryInterval, Key: RetryInterval,
Value: "3s", Value: "3s",
}, },
config.KV{
Key: httpTimeout,
Value: "5s",
},
} }
DefaultAuditWebhookKVS = config.KVS{ DefaultAuditWebhookKVS = config.KVS{
@ -569,6 +575,7 @@ func lookupAuditWebhookConfig(scfg config.Config, cfg Config) (Config, error) {
if maxRetry < 0 { if maxRetry < 0 {
return cfg, fmt.Errorf("invalid %s max_retry", maxRetryCfgVal) return cfg, fmt.Errorf("invalid %s max_retry", maxRetryCfgVal)
} }
retryIntervalCfgVal := getCfgVal(EnvAuditWebhookRetryInterval, k, kv.Get(RetryInterval)) retryIntervalCfgVal := getCfgVal(EnvAuditWebhookRetryInterval, k, kv.Get(RetryInterval))
retryInterval, err := time.ParseDuration(retryIntervalCfgVal) retryInterval, err := time.ParseDuration(retryIntervalCfgVal)
if err != nil { if err != nil {
@ -577,7 +584,18 @@ func lookupAuditWebhookConfig(scfg config.Config, cfg Config) (Config, error) {
if retryInterval > time.Minute { if retryInterval > time.Minute {
return cfg, fmt.Errorf("maximum allowed value for retry interval is '1m': %s", retryIntervalCfgVal) return cfg, fmt.Errorf("maximum allowed value for retry interval is '1m': %s", retryIntervalCfgVal)
} }
httpTimeoutCfgVal := getCfgVal(EnvAuditWebhookHTTPTimeout, k, kv.Get(httpTimeout))
httpTimeout, err := time.ParseDuration(httpTimeoutCfgVal)
if err != nil {
return cfg, err
}
if httpTimeout < time.Second {
return cfg, fmt.Errorf("minimum value allowed for http_timeout is '1s': %s", httpTimeout)
}
cfg.AuditWebhook[k] = http.Config{ cfg.AuditWebhook[k] = http.Config{
HTTPTimeout: httpTimeout,
Enabled: true, Enabled: true,
Endpoint: url, Endpoint: url,
AuthToken: getCfgVal(EnvAuditWebhookAuthToken, k, kv.Get(AuthToken)), AuthToken: getCfgVal(EnvAuditWebhookAuthToken, k, kv.Get(AuthToken)),

View File

@ -88,6 +88,12 @@ var (
Optional: true, Optional: true,
Type: "duration", Type: "duration",
}, },
config.HelpKV{
Key: httpTimeout,
Description: `defines the maximum duration for each http request`,
Optional: true,
Type: "duration",
},
config.HelpKV{ config.HelpKV{
Key: config.Comment, Key: config.Comment,
Description: config.DefaultComment, Description: config.DefaultComment,
@ -155,6 +161,12 @@ var (
Optional: true, Optional: true,
Type: "duration", Type: "duration",
}, },
config.HelpKV{
Key: httpTimeout,
Description: `defines the maximum duration for each http request`,
Optional: true,
Type: "duration",
},
config.HelpKV{ config.HelpKV{
Key: config.Comment, Key: config.Comment,
Description: config.DefaultComment, Description: config.DefaultComment,

View File

@ -43,8 +43,6 @@ import (
) )
const ( const (
// Timeout for the webhook http call
webhookCallTimeout = 5 * time.Second
// maxWorkers is the maximum number of concurrent http loggers // maxWorkers is the maximum number of concurrent http loggers
maxWorkers = 16 maxWorkers = 16
@ -83,6 +81,7 @@ type Config struct {
RetryIntvl time.Duration `json:"retryInterval"` RetryIntvl time.Duration `json:"retryInterval"`
Proxy string `json:"string"` Proxy string `json:"string"`
Transport http.RoundTripper `json:"-"` Transport http.RoundTripper `json:"-"`
HTTPTimeout time.Duration `json:"httpTimeout"`
// Custom logger // Custom logger
LogOnceIf func(ctx context.Context, err error, id string, errKind ...interface{}) `json:"-"` LogOnceIf func(ctx context.Context, err error, id string, errKind ...interface{}) `json:"-"`
@ -139,6 +138,7 @@ type Target struct {
config Config config Config
client *http.Client client *http.Client
httpTimeout time.Duration
} }
// Name returns the name of the target // Name returns the name of the target
@ -429,7 +429,7 @@ func (h *Target) startQueueProcessor(ctx context.Context, mainWorker bool) {
var err error var err error
if !isDirQueue { if !isDirQueue {
err = h.send(ctx, buf.Bytes(), count, h.payloadType, webhookCallTimeout) err = h.send(ctx, buf.Bytes(), count, h.payloadType, h.httpTimeout)
} else { } else {
_, err = h.store.PutMultiple(entries) _, err = h.store.PutMultiple(entries)
} }
@ -524,6 +524,7 @@ func New(config Config) (*Target, error) {
config: config, config: config,
batchSize: config.BatchSize, batchSize: config.BatchSize,
maxWorkers: int64(maxWorkers), maxWorkers: int64(maxWorkers),
httpTimeout: config.HTTPTimeout,
} }
h.status.Store(statusOffline) h.status.Store(statusOffline)
@ -566,7 +567,7 @@ func (h *Target) SendFromStore(key store.Key) (err error) {
} }
} }
if err := h.send(context.Background(), eventData, count, h.payloadType, webhookCallTimeout); err != nil { if err := h.send(context.Background(), eventData, count, h.payloadType, h.httpTimeout); err != nil {
return err return err
} }