Skip to content

Commit

Permalink
kuma-cp: validate access log format that users define in Mesh resource
Browse files Browse the repository at this point in the history
  • Loading branch information
yskopets committed Feb 22, 2020
1 parent f334ee7 commit 1fedea4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/core/resources/apis/mesh/mesh_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package mesh

import (
"fmt"
mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1"
"github.com/Kong/kuma/pkg/core/validators"
"net"
"net/url"

mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1"
"github.com/Kong/kuma/pkg/core/validators"
"github.com/Kong/kuma/pkg/envoy/accesslog"
)

func (m *MeshResource) Validate() error {
Expand Down Expand Up @@ -51,6 +53,9 @@ func validateLoggingBackend(backend *mesh_proto.LoggingBackend) validators.Valid
if backend.Name == "" {
verr.AddViolation("name", "cannot be empty")
}
if err := accesslog.ValidateFormat(backend.Format); err != nil {
verr.AddViolation("format", err.Error())
}
if file, ok := backend.GetType().(*mesh_proto.LoggingBackend_File_); ok {
verr.AddError("file", validateLoggingFile(file))
} else if tcp, ok := backend.GetType().(*mesh_proto.LoggingBackend_Tcp_); ok {
Expand Down
22 changes: 22 additions & 0 deletions pkg/core/resources/apis/mesh/mesh_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ var _ = Describe("Mesh", func() {
- name: file-1
file:
path: /path/to/file
- name: file-2
format: '%START_TIME% %KUMA_SOURCE_SERVICE%'
file:
path: /path/to/file2
- name: tcp-1
tcp:
address: kibana:1234
- name: tcp-2
format: '%START_TIME% %KUMA_DESTINATION_SERVICE%'
tcp:
address: kibana:1234
defaultBackend: tcp-1
tracing:
backends:
Expand Down Expand Up @@ -151,6 +159,20 @@ var _ = Describe("Mesh", func() {
violations:
- field: logging.backends[0].file.path
message: cannot be empty`,
}),
Entry("invalid access log format", testCase{
mesh: `
logging:
backends:
- name: backend-1
format: "%START_TIME% %sent_bytes%"
file:
path: /var/logs
defaultBackend: backend-1`,
expected: `
violations:
- field: logging.backends[0].format
message: 'format string is not valid: expected a command operator to start at position 14, instead got: "%sent_bytes%"'`,
}),
Entry("default backend has to be set to one of the backends", testCase{
mesh: `
Expand Down

0 comments on commit 1fedea4

Please sign in to comment.