Skip to content

Commit

Permalink
[chore] add default gitlab headers to config
Browse files Browse the repository at this point in the history
  • Loading branch information
niwoerner committed Dec 18, 2024
1 parent ab25785 commit d9b514e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 19 deletions.
10 changes: 6 additions & 4 deletions receiver/gitlabreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
40 changes: 32 additions & 8 deletions receiver/gitlabreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand All @@ -49,6 +65,14 @@ func createDefaultConfig() component.Config {
ReadTimeout: defaultReadTimeout,
WriteTimeout: defaultWriteTimeout,
},
GitlabHeaders: []string{
defaultUserAgentHeader,
defaultGitlabInstanceHeader,
defaultGitlabWebhookUUIDHeader,
defaultGitlabEventHeader,
defaultGitlabEventUUIDHeader,
defaultIdempotencyKeyHeader,
},
Path: defaultPath,
HealthPath: defaultHealthPath,
},
Expand Down
26 changes: 24 additions & 2 deletions receiver/gitlabreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
},
},
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion receiver/gitlabreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ status:
tests:
config:
webhook:
endpoint: "localhost:8080"
endpoint: "localhost:0" #dynamic port allocation to avoid falky tests
11 changes: 7 additions & 4 deletions receiver/gitlabreceiver/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:

Expand Down

0 comments on commit d9b514e

Please sign in to comment.