Skip to content

Commit

Permalink
fix: add validation for protocol field on service-defaults config entry
Browse files Browse the repository at this point in the history
  • Loading branch information
JadhavPoonam committed Aug 15, 2024
1 parent 779d3c3 commit 1b68af1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 7 additions & 2 deletions agent/structs/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"time"

"github.com/miekg/dns"

"github.com/hashicorp/go-multierror"
"github.com/mitchellh/hashstructure"
"github.com/mitchellh/mapstructure"

"github.com/hashicorp/consul-net-rpc/go-msgpack/codec"
"github.com/hashicorp/go-multierror"

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/cache"
Expand Down Expand Up @@ -269,6 +268,12 @@ func (e *ServiceConfigEntry) Validate() error {
validationErr = multierror.Append(validationErr, fmt.Errorf("invalid value for balance_inbound_connections: %v", e.BalanceInboundConnections))
}

switch e.Protocol {
case "", "http", "http2", "grpc", "tcp":
default:
validationErr = multierror.Append(validationErr, fmt.Errorf("invalid value for protocol: %v", e.Protocol))
}

// External endpoints are invalid with an existing service's upstream configuration
if e.UpstreamConfig != nil && e.Destination != nil {
validationErr = multierror.Append(validationErr, errors.New("UpstreamConfig and Destination are mutually exclusive for service defaults"))
Expand Down
10 changes: 9 additions & 1 deletion agent/structs/config_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl"
"github.com/mitchellh/copystructure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hashicorp/consul-net-rpc/go-msgpack/codec"
"github.com/hashicorp/hcl"

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/agent/cache"
Expand Down Expand Up @@ -3225,6 +3225,14 @@ func TestServiceConfigEntry(t *testing.T) {
},
validateErr: `Invalid MutualTLSMode "invalid-mtls-mode". Must be one of "", "strict", or "permissive".`,
},
"validate: invalid Protocol in service-defaults": {
entry: &ServiceConfigEntry{
Kind: ServiceDefaults,
Name: "web",
Protocol: "blah",
},
validateErr: `invalid value for protocol: blah`,
},
}
testConfigEntryNormalizeAndValidate(t, cases)
}
Expand Down

0 comments on commit 1b68af1

Please sign in to comment.