Skip to content

Commit cb3bd02

Browse files
disallow setting Content-Type and Authorization headers
1 parent 6d68094 commit cb3bd02

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

internal/validations/observability/outputs/validate.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func Validate(context internalcontext.ForwarderContext) {
2929
messages = append(messages, validateHttpContentTypeHeaders(out)...)
3030
case obs.OutputTypeLokiStack, obs.OutputTypeOTLP:
3131
messages = append(messages, ValidateTechPreviewAnnotation(out, context)...)
32+
case obs.OutputTypeElasticsearch:
33+
messages = append(messages, validateElasticsearchHeaders(out)...)
3234
}
3335
// Set condition
3436
if len(messages) > 0 {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package outputs
2+
3+
import (
4+
"fmt"
5+
log "github.com/ViaQ/logerr/v2/log/static"
6+
obs "github.com/openshift/cluster-logging-operator/api/observability/v1"
7+
"strings"
8+
)
9+
10+
// validateElasticsearchHeaders will validate Elasticsearch custom headers
11+
// it's not allowed to pass "Authorization" and "Content-Type" headers
12+
func validateElasticsearchHeaders(output obs.OutputSpec) (results []string) {
13+
if output.Type == obs.OutputTypeElasticsearch && output.Elasticsearch != nil && len(output.Elasticsearch.Headers) > 0 {
14+
var invalidHeaders []string
15+
for headerName := range output.Elasticsearch.Headers {
16+
if strings.ToLower(headerName) == "authorization" || strings.ToLower(headerName) == "content-type" {
17+
invalidHeaders = append(invalidHeaders, headerName)
18+
}
19+
if len(invalidHeaders) > 0 {
20+
log.V(3).Info("validateElasticsearchHeaders failed", "reason", "invalid headers found: ", strings.Join(invalidHeaders, ","))
21+
results = append(results, fmt.Sprintf("invalid headers found: %s", strings.Join(invalidHeaders, ",")))
22+
}
23+
}
24+
}
25+
return results
26+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package outputs
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
"github.com/openshift/cluster-logging-operator/api/observability/v1"
7+
)
8+
9+
var _ = Describe("[internal][validations] ClusterLogForwarder will validate headers in Elasticsearch Output", func() {
10+
var (
11+
es *v1.Elasticsearch
12+
spec v1.OutputSpec
13+
)
14+
BeforeEach(func() {
15+
es = &v1.Elasticsearch{}
16+
spec = v1.OutputSpec{
17+
Name: "esOutput",
18+
Type: v1.OutputTypeElasticsearch,
19+
Elasticsearch: es,
20+
}
21+
})
22+
23+
Context("#validateElasticsearchHeaders", func() {
24+
25+
It("should pass validation with empty headers", func() {
26+
Expect(validateElasticsearchHeaders(spec)).To(BeEmpty())
27+
})
28+
It("should pass validation when no invalid headers set", func() {
29+
spec.Elasticsearch.Headers = map[string]string{
30+
"Accept": "application/json",
31+
}
32+
Expect(validateElasticsearchHeaders(spec)).To(BeEmpty())
33+
})
34+
It("should fail validation when the Content-Type header is set", func() {
35+
spec.Elasticsearch.Headers = map[string]string{
36+
"Content-Type": "application/json",
37+
}
38+
Expect(validateElasticsearchHeaders(spec)).ToNot(BeEmpty())
39+
})
40+
It("should fail validation when the Authorization header is set", func() {
41+
spec.Elasticsearch.Headers = map[string]string{
42+
"Authorization": "test",
43+
}
44+
Expect(validateElasticsearchHeaders(spec)).ToNot(BeEmpty())
45+
})
46+
It("should pass validation when no Elasticsearch Output", func() {
47+
spec = v1.OutputSpec{
48+
Name: "esOutput",
49+
Type: v1.OutputTypeElasticsearch,
50+
Elasticsearch: &v1.Elasticsearch{},
51+
}
52+
Expect(validateElasticsearchHeaders(spec)).To(BeEmpty())
53+
})
54+
})
55+
})

0 commit comments

Comments
 (0)