Skip to content

Commit 3e9a358

Browse files
committed
Integrate consul-server-connection-manager library
1 parent 1dc4bee commit 3e9a358

27 files changed

+625
-2963
lines changed

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ copy-bootstrap-config:
2222
unit-tests:
2323
go test ./...
2424

25-
# TODO: Install dependencies before running this target
26-
.PHONY: consul-proto
27-
consul-proto:
28-
buf generate "https://github.com/hashicorp/consul.git#branch=main,subdir=proto-public"
29-
3025
.PHONY: docker-build
3126
docker-build:
32-
docker build --no-cache . -t consul-dataplane
27+
docker build --no-cache . -t consul-dataplane

buf.gen.yaml

Lines changed: 0 additions & 14 deletions
This file was deleted.

cmd/consul-dataplane/main.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ import (
1212
)
1313

1414
var (
15-
addresses string
16-
grpcPort int
15+
addresses string
16+
grpcPort int
17+
serverWatchDisabled bool
18+
19+
tlsDisabled bool
20+
tlsCACertsPath string
21+
tlsServerName string
22+
tlsCertFile string
23+
tlsKeyFile string
24+
tlsInsecureSkipVerify bool
1725

1826
logLevel string
1927
logJSON bool
@@ -24,7 +32,15 @@ var (
2432
namespace string
2533
partition string
2634

27-
token string
35+
credentialType string
36+
token string
37+
loginMethod string
38+
loginNamespace string
39+
loginPartition string
40+
loginDatacenter string
41+
loginBearer string
42+
loginBearerPath string
43+
loginMeta map[string]string
2844

2945
useCentralTelemetryConfig bool
3046

@@ -47,6 +63,8 @@ func init() {
4763

4864
flag.IntVar(&grpcPort, "grpc-port", 8502, "gRPC port on Consul servers.")
4965

66+
flag.BoolVar(&serverWatchDisabled, "server-watch-disabled", false, "Setting this prevents consul-dataplane from consuming the server update stream. This is useful for situations where Consul servers are behind a load balancer.")
67+
5068
flag.StringVar(&logLevel, "log-level", "info", "Log level of the messages to print. "+
5169
"Available log levels are \"trace\", \"debug\", \"info\", \"warn\", and \"error\".")
5270

@@ -58,7 +76,15 @@ func init() {
5876
flag.StringVar(&namespace, "service-namespace", "", "The Consul Enterprise namespace in which the proxy service instance is registered.")
5977
flag.StringVar(&partition, "service-partition", "", "The Consul Enterprise partition in which the proxy service instance is registered.")
6078

79+
flag.StringVar(&credentialType, "credential-type", "", "The type of credentials that will be used to authenticate with Consul servers (static or login).")
6180
flag.StringVar(&token, "static-token", "", "The ACL token used to authenticate requests to Consul servers (when -login-method is set to static).")
81+
flag.StringVar(&loginMethod, "login-method", "", "The auth method that will be used to log in.")
82+
flag.StringVar(&loginNamespace, "login-namespace", "", "The Consul Enterprise namespace containing the auth method.")
83+
flag.StringVar(&loginPartition, "login-partition", "", "The Consul Enterprise partition containing the auth method.")
84+
flag.StringVar(&loginDatacenter, "login-datacenter", "", "The datacenter containing the auth method.")
85+
flag.StringVar(&loginBearer, "login-bearer", "", "The bearer token that will be presented to the auth method.")
86+
flag.StringVar(&loginBearerPath, "login-bearer-path", "", "The path to a file containing the bearer token that will be presented to the auth method.")
87+
flag.Var((*FlagMapValue)(&loginMeta), "login-meta", "An arbitrary set of key/value pairs that will be attached to the ACL token (formatted as key=value, may be given multiple times).")
6288

6389
flag.BoolVar(&useCentralTelemetryConfig, "telemetry-use-central-config", true, "Controls whether the proxy will apply the central telemetry configuration.")
6490

@@ -69,6 +95,13 @@ func init() {
6995

7096
flag.StringVar(&xdsBindAddr, "xds-bind-addr", "127.0.0.1", "The address on which the Envoy xDS server will be available.")
7197
flag.IntVar(&xdsBindPort, "xds-bind-port", 0, "The port on which the Envoy xDS server will be available.")
98+
99+
flag.BoolVar(&tlsDisabled, "tls-disabled", false, "Communicate with Consul servers over a plaintext connection. Useful for testing, but not recommended for production.")
100+
flag.StringVar(&tlsCACertsPath, "ca-certs", "", "The path to a file or directory containing CA certificates that will be used to verify the server's certificate.")
101+
flag.StringVar(&tlsCertFile, "tls-cert", "", "The path to a client certificate file (only required if tls.grpc.verify_incoming is enabled on the server).")
102+
flag.StringVar(&tlsKeyFile, "tls-key", "", "The path to a client private key file (only required if tls.grpc.verify_incoming is enabled on the server).")
103+
flag.StringVar(&tlsServerName, "tls-server-name", "", "The hostname to expect in the server certificate's subject (required if -addresses isn't a DNS name).")
104+
flag.BoolVar(&tlsInsecureSkipVerify, "tls-insecure-skip-verify", false, "Do not verify the server's certificate. Useful for testing, but not recommended for production.")
72105
}
73106

74107
// validateFlags performs semantic validation of the flag values
@@ -91,9 +124,28 @@ func main() {
91124
Addresses: addresses,
92125
GRPCPort: grpcPort,
93126
Credentials: &consuldp.CredentialsConfig{
94-
Static: &consuldp.StaticCredentialsConfig{
127+
Type: consuldp.CredentialsType(credentialType),
128+
Static: consuldp.StaticCredentialsConfig{
95129
Token: token,
96130
},
131+
Login: consuldp.LoginCredentialsConfig{
132+
Method: loginMethod,
133+
Namespace: loginNamespace,
134+
Partition: loginPartition,
135+
Datacenter: loginDatacenter,
136+
Bearer: loginBearer,
137+
BearerPath: loginBearerPath,
138+
Meta: loginMeta,
139+
},
140+
},
141+
ServerWatchDisabled: serverWatchDisabled,
142+
TLS: &consuldp.TLSConfig{
143+
Disabled: tlsDisabled,
144+
CACertsPath: tlsCACertsPath,
145+
ServerName: tlsServerName,
146+
CertFile: tlsCertFile,
147+
KeyFile: tlsKeyFile,
148+
InsecureSkipVerify: tlsInsecureSkipVerify,
97149
},
98150
},
99151
Service: &consuldp.ServiceConfig{

cmd/consul-dataplane/map_flag.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
var _ flag.Value = (*FlagMapValue)(nil)
10+
11+
// FlagMapValue is a flag implementation used to provide key=value semantics
12+
// multiple times.
13+
type FlagMapValue map[string]string
14+
15+
func (h *FlagMapValue) String() string {
16+
return fmt.Sprintf("%v", *h)
17+
}
18+
19+
func (h *FlagMapValue) Set(value string) error {
20+
idx := strings.Index(value, "=")
21+
if idx == -1 {
22+
return fmt.Errorf("Missing \"=\" value in argument: %s", value)
23+
}
24+
25+
key, value := value[0:idx], value[idx+1:]
26+
27+
if *h == nil {
28+
*h = make(map[string]string)
29+
}
30+
31+
headers := *h
32+
headers[key] = value
33+
*h = headers
34+
35+
return nil
36+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestFlagMapValueSet(t *testing.T) {
9+
t.Parallel()
10+
11+
t.Run("missing =", func(t *testing.T) {
12+
13+
f := new(FlagMapValue)
14+
if err := f.Set("foo"); err == nil {
15+
t.Fatal("expected error, got nil")
16+
}
17+
})
18+
19+
t.Run("sets", func(t *testing.T) {
20+
21+
f := new(FlagMapValue)
22+
if err := f.Set("foo=bar"); err != nil {
23+
t.Fatal(err)
24+
}
25+
26+
r, ok := (*f)["foo"]
27+
if !ok {
28+
t.Errorf("missing value: %#v", f)
29+
}
30+
if exp := "bar"; r != exp {
31+
t.Errorf("expected %q to be %q", r, exp)
32+
}
33+
})
34+
35+
t.Run("sets multiple", func(t *testing.T) {
36+
37+
f := new(FlagMapValue)
38+
39+
r := map[string]string{
40+
"foo": "bar",
41+
"zip": "zap",
42+
"cat": "dog",
43+
}
44+
45+
for k, v := range r {
46+
if err := f.Set(fmt.Sprintf("%s=%s", k, v)); err != nil {
47+
t.Fatal(err)
48+
}
49+
}
50+
51+
for k, v := range r {
52+
r, ok := (*f)[k]
53+
if !ok {
54+
t.Errorf("missing value %q: %#v", k, f)
55+
}
56+
if exp := v; r != exp {
57+
t.Errorf("expected %q to be %q", r, exp)
58+
}
59+
}
60+
})
61+
62+
t.Run("overwrites", func(t *testing.T) {
63+
64+
f := new(FlagMapValue)
65+
if err := f.Set("foo=bar"); err != nil {
66+
t.Fatal(err)
67+
}
68+
if err := f.Set("foo=zip"); err != nil {
69+
t.Fatal(err)
70+
}
71+
72+
r, ok := (*f)["foo"]
73+
if !ok {
74+
t.Errorf("missing value: %#v", f)
75+
}
76+
if exp := "zip"; r != exp {
77+
t.Errorf("expected %q to be %q", r, exp)
78+
}
79+
})
80+
}

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,27 @@ go 1.18
44

55
require (
66
github.com/adamthesax/grpc-proxy v0.0.0-20220525203857-13e92d14f87a
7+
github.com/hashicorp/consul/proto-public v0.1.0
78
github.com/hashicorp/go-hclog v1.2.2
8-
github.com/hashicorp/go-netaddrs v0.0.0-20220509001840-90ed9d26ec46
9+
github.com/hashicorp/go-rootcerts v1.0.2
910
github.com/mitchellh/mapstructure v1.5.0
1011
github.com/stretchr/testify v1.8.0
1112
google.golang.org/grpc v1.48.0
1213
google.golang.org/protobuf v1.28.1
1314
)
1415

1516
require (
17+
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
1618
github.com/davecgh/go-spew v1.1.1 // indirect
1719
github.com/fatih/color v1.13.0 // indirect
1820
github.com/golang/protobuf v1.5.2 // indirect
1921
github.com/google/go-cmp v0.5.8 // indirect
22+
github.com/google/uuid v1.1.2 // indirect
23+
github.com/hashicorp/consul-server-connection-manager v0.0.0-20220908112242-b9f43f15d156 // indirect
24+
github.com/hashicorp/go-netaddrs v0.0.0-20220509001840-90ed9d26ec46 // indirect
2025
github.com/mattn/go-colorable v0.1.12 // indirect
2126
github.com/mattn/go-isatty v0.0.14 // indirect
27+
github.com/mitchellh/go-homedir v1.1.0 // indirect
2228
github.com/pmezard/go-difflib v1.0.0 // indirect
2329
github.com/stretchr/objx v0.4.0 // indirect
2430
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
44
github.com/adamthesax/grpc-proxy v0.0.0-20220525203857-13e92d14f87a h1:8fjfNnk9RLn3F4R4XEljSOZARy1+h1f0KTh6xGFefjw=
55
github.com/adamthesax/grpc-proxy v0.0.0-20220525203857-13e92d14f87a/go.mod h1:Aku9EjGILrB1V88F+yfJ8CaIVaKqDeWkW2vkCbY2WSA=
66
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
7+
github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4=
8+
github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
79
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
810
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
911
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
@@ -51,21 +53,34 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
5153
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5254
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
5355
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
56+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
5457
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5558
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
59+
github.com/hashicorp/consul-server-connection-manager v0.0.0-20220908112242-b9f43f15d156 h1:fg/ocjUfyx6JXmYr/UL/OC5pbG1sPjWOF3jSQWvRGKc=
60+
github.com/hashicorp/consul-server-connection-manager v0.0.0-20220908112242-b9f43f15d156/go.mod h1:C3zdIuwQZ6GsU9jpc1iWr/+5UDgE/3p7UPGqTtvHqsY=
61+
github.com/hashicorp/consul/proto-public v0.1.0 h1:O0LSmCqydZi363hsqc6n2v5sMz3usQMXZF6ziK3SzXU=
62+
github.com/hashicorp/consul/proto-public v0.1.0/go.mod h1:vs2KkuWwtjkIgA5ezp4YKPzQp4GitV+q/+PvksrA92k=
63+
github.com/hashicorp/consul/sdk v0.11.0 h1:HRzj8YSCln2yGgCumN5CL8lYlD3gBurnervJRJAZyC4=
64+
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
5665
github.com/hashicorp/go-hclog v1.2.2 h1:ihRI7YFwcZdiSD7SIenIhHfQH3OuDvWerAUBZbeQS3M=
5766
github.com/hashicorp/go-hclog v1.2.2/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
5867
github.com/hashicorp/go-netaddrs v0.0.0-20220509001840-90ed9d26ec46 h1:BysEAd6g+0HNJ0v99u7KbSObjzxC7rfVQ6yVx6HxrvU=
5968
github.com/hashicorp/go-netaddrs v0.0.0-20220509001840-90ed9d26ec46/go.mod h1:TjKbv4FhIra0YJ82mws5+4QXOhzv09eAWs4jtOBI4IU=
69+
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
70+
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
71+
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
6072
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
6173
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
6274
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
6375
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
6476
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
6577
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
6678
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
79+
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
80+
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
6781
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
6882
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
83+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
6984
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7085
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7186
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=

0 commit comments

Comments
 (0)