Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion internal/validations/clusterlogforwarder/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ var validations = []func(clf v1.ClusterLogForwarder, k8sClient client.Client, ex
ValidateInputsOutputsPipelines,
validateJsonParsingToElasticsearch,
validateUrlAccordingToTls,
validateHttpContentTypeHeaders,
validateAnnotations,
}
2 changes: 2 additions & 0 deletions internal/validations/observability/outputs/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func Validate(context internalcontext.ForwarderContext) {
switch out.Type {
case obsv1.OutputTypeCloudwatch:
messages = append(messages, ValidateCloudWatchAuth(out)...)
case obsv1.OutputTypeHTTP:
messages = append(messages, validateHttpContentTypeHeaders(out)...)
case obsv1.OutputTypeOTLP:
messages = append(messages, ValidateOtlpAnnotation(context)...)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package outputs

import (
"fmt"
log "github.com/ViaQ/logerr/v2/log/static"
obs "github.com/openshift/cluster-logging-operator/api/observability/v1"
"reflect"
"strings"
)

var validContentTypes = map[string]string{
"application/json": "json",
"application/x-ndjson": "ndjson",
}

// validateHttpContentTypeHeaders will validate Content-Type header in Http Output
// valid content-type are: "application/json" and "application/x-ndjson"
// was introduced in https://github.com/openshift/cluster-logging-operator/pull/1924
// for https://issues.redhat.com/browse/LOG-3784
func validateHttpContentTypeHeaders(output obs.OutputSpec) (results []string) {
if output.Type == obs.OutputTypeHTTP && output.HTTP != nil {
if contentType, found := output.HTTP.Headers["Content-Type"]; found && validContentTypes[strings.ToLower(contentType)] == "" {
validKeys := reflect.ValueOf(validContentTypes).MapKeys()
log.V(3).Info("validateHttpContentTypeHeaders failed", "reason", "not valid content type set in headers",
"content type", contentType, "supported types: ", validKeys)
results = append(results, fmt.Sprintf("not valid content type set in headers: %s , supported types: %s", contentType, validKeys))
}
}
return results
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package outputs

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/openshift/cluster-logging-operator/api/observability/v1"
)

var _ = Describe("[internal][validations] ClusterLogForwarder will validate Content-Type header in HTTP Output", func() {
var (
http *v1.HTTP
spec v1.OutputSpec
)
BeforeEach(func() {
http = &v1.HTTP{}
spec = v1.OutputSpec{
Name: "httpOutput",
Type: v1.OutputTypeHTTP,
HTTP: http,
}
})

Context("#validateHttpContentTypeHeaders", func() {

It("should pass validation with empty headers", func() {
Expect(validateHttpContentTypeHeaders(spec)).To(BeEmpty())
})
It("should pass validation when not Content Type header", func() {
spec.HTTP.Headers = map[string]string{
"Accept": "application/json",
}
Expect(validateHttpContentTypeHeaders(spec)).To(BeEmpty())
})
It("should pass validation when the Content Type header is application/json", func() {
spec.HTTP.Headers = map[string]string{
"Content-Type": "application/json",
}
Expect(validateHttpContentTypeHeaders(spec)).To(BeEmpty())
})
It("should pass validation when the Content Type header is application/x-ndjson", func() {
spec.HTTP.Headers = map[string]string{
"Content-Type": "application/x-ndjson",
}
Expect(validateHttpContentTypeHeaders(spec)).To(BeEmpty())
})
It("should fail validation when not valid content types", func() {
spec.HTTP.Headers = map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}
Expect(validateHttpContentTypeHeaders(spec)).ToNot(BeEmpty())
})
It("should pass validation when not HTTP Output", func() {
spec = v1.OutputSpec{
Name: "esOutput",
Type: v1.OutputTypeElasticsearch,
Elasticsearch: &v1.Elasticsearch{},
}
Expect(validateHttpContentTypeHeaders(spec)).To(BeEmpty())
})
})
})