diff --git a/receiver/gitlabreceiver/README.md b/receiver/gitlabreceiver/README.md index 31bef56cc80e..adab4983cd3b 100644 --- a/receiver/gitlabreceiver/README.md +++ b/receiver/gitlabreceiver/README.md @@ -32,7 +32,8 @@ The WebHook configuration exposes the following settings: * `path`: (default = `/events`) - The path for Action events to be sent to. * `health_path`: (default = `/health`) - The path for health checks. * `secret`: (optional) - The secret used to [validate the payload](https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#custom-headers). -* `required_header`: (optional) - The required header key and value for incoming requests. +* `required_headers`: (optional) - One or more required header key and value pairs for incoming requests. +* `gitlab_headers`: (default = see GitLab headers in [config.go](./config.go)) - One or more required header keys set by GitLab for incoming requests. The WebHook configuration block also accepts all the [confighttp](https://pkg.go.dev/go.opentelemetry.io/collector/config/confighttp#ServerConfig) settings. @@ -47,9 +48,10 @@ receivers: path: /events health_path: /health secret: ${env:SECRET_STRING_VAR} - required_header: - key: "X-GitLab-Event" - value: "pipeline" + required_headers: + WAF-Header: "value" + gitlab_headers: + - "X-Gitlab-Event" ``` For tracing, all configuration is set under the `webhook` key. The full set diff --git a/receiver/gitlabreceiver/config.go b/receiver/gitlabreceiver/config.go index 2247bf36f889..a10405af64a3 100644 --- a/receiver/gitlabreceiver/config.go +++ b/receiver/gitlabreceiver/config.go @@ -16,9 +16,21 @@ import ( const ( defaultReadTimeout = 500 * time.Millisecond defaultWriteTimeout = 500 * time.Millisecond - defaultPath = "/events" - defaultHealthPath = "/health" - defaultEndpoint = "localhost:8080" + + defaultEndpoint = "localhost:8080" + + defaultPath = "/events" + defaultHealthPath = "/health" +) + +const ( + // GitLab default headers: https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#delivery-headers + defaultUserAgentHeader = "User-Agent" + defaultGitlabInstanceHeader = "X-Gitlab-Instance" + defaultGitlabWebhookUUIDHeader = "X-Gitlab-Webhook-UUID" + defaultGitlabEventHeader = "X-Gitlab-Event" + defaultGitlabEventUUIDHeader = "X-Gitlab-Event-UUID" + defaultIdempotencyKeyHeader = "Idempotency-Key" ) var ( @@ -34,11 +46,15 @@ type Config struct { } type WebHook struct { - confighttp.ServerConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct - Path string `mapstructure:"path"` // path for data collection. Default is /events - HealthPath string `mapstructure:"health_path"` // path for health check api. Default is /health_check - RequiredHeaders map[string]configopaque.String `mapstructure:"required_headers"` // optional setting to set one or more required headers for all requests to have - Secret string `mapstructure:"secret"` // secret for webhook + confighttp.ServerConfig `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct + + Path string `mapstructure:"path"` // path for data collection. default is /events + HealthPath string `mapstructure:"health_path"` // path for health check api. default is /health_check + + RequiredHeaders map[string]configopaque.String `mapstructure:"required_headers"` // optional setting to set one or more required headers for all requests to have (except the health check) + GitlabHeaders []string `mapstructure:"gitlab_headers"` // optional setting to overwrite the by default required GitLab headers for all requests to have (except the health check) + + Secret string `mapstructure:"secret"` // secret for webhook } func createDefaultConfig() component.Config { @@ -49,6 +65,14 @@ func createDefaultConfig() component.Config { ReadTimeout: defaultReadTimeout, WriteTimeout: defaultWriteTimeout, }, + GitlabHeaders: []string{ + defaultUserAgentHeader, + defaultGitlabInstanceHeader, + defaultGitlabWebhookUUIDHeader, + defaultGitlabEventHeader, + defaultGitlabEventUUIDHeader, + defaultIdempotencyKeyHeader, + }, Path: defaultPath, HealthPath: defaultHealthPath, }, diff --git a/receiver/gitlabreceiver/config_test.go b/receiver/gitlabreceiver/config_test.go index 420746b69683..b9cce59ab99d 100644 --- a/receiver/gitlabreceiver/config_test.go +++ b/receiver/gitlabreceiver/config_test.go @@ -33,6 +33,14 @@ func TestCreateDefaultConfig(t *testing.T) { }, Path: defaultPath, HealthPath: defaultHealthPath, + GitlabHeaders: []string{ + defaultUserAgentHeader, + defaultGitlabInstanceHeader, + defaultGitlabWebhookUUIDHeader, + defaultGitlabEventHeader, + defaultGitlabEventUUIDHeader, + defaultIdempotencyKeyHeader, + }, }, }, cfg, "failed to create default config") @@ -64,7 +72,15 @@ func TestLoadConfig(t *testing.T) { Path: "some/path", HealthPath: "health/path", RequiredHeaders: map[string]configopaque.String{ - "key-present": "value-present", + "key1-present": "value1-present", + }, + GitlabHeaders: []string{ + defaultUserAgentHeader, + defaultGitlabInstanceHeader, + defaultGitlabWebhookUUIDHeader, + defaultGitlabEventHeader, + defaultGitlabEventUUIDHeader, + defaultIdempotencyKeyHeader, }, }, } @@ -73,11 +89,17 @@ func TestLoadConfig(t *testing.T) { assert.Equal(t, expectedConfig, r0) + // r1 requires multiple headers and overwrites gitlab default headers expectedConfig.WebHook.RequiredHeaders = map[string]configopaque.String{ - "key-present": "value-present", + "key1-present": "value1-present", "key2-present": "value2-present", } + expectedConfig.WebHook.GitlabHeaders = []string{ + "header1", + "header2", + } + r1 := cfg.Receivers[component.NewIDWithName(metadata.Type, "customname")].(*Config) assert.Equal(t, expectedConfig, r1) diff --git a/receiver/gitlabreceiver/metadata.yaml b/receiver/gitlabreceiver/metadata.yaml index 185134234551..4df4990025da 100644 --- a/receiver/gitlabreceiver/metadata.yaml +++ b/receiver/gitlabreceiver/metadata.yaml @@ -10,4 +10,4 @@ status: tests: config: webhook: - endpoint: "localhost:8080" \ No newline at end of file + endpoint: "localhost:0" #dynamic port allocation to avoid falky tests \ No newline at end of file diff --git a/receiver/gitlabreceiver/testdata/config.yaml b/receiver/gitlabreceiver/testdata/config.yaml index 0f3ed2377b71..5bce8950b8ae 100644 --- a/receiver/gitlabreceiver/testdata/config.yaml +++ b/receiver/gitlabreceiver/testdata/config.yaml @@ -7,7 +7,7 @@ receivers: path: "some/path" health_path: "health/path" required_headers: - key-present: value-present + key1-present: "value1-present" gitlab/customname: webhook: @@ -17,9 +17,12 @@ receivers: path: "some/path" health_path: "health/path" required_headers: - key-present: value-present - key2-present: value2-present - + key1-present: "value1-present" + key2-present: "value2-present" + gitlab_headers: + - "header1" + - "header2" + processors: nop: