Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
refactor CertManagerOptions to check UseTLS instead of typed nil
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorris committed Nov 15, 2022
1 parent 4c5ee26 commit ea90538
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
1 change: 1 addition & 0 deletions internal/commands/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func RunExec(config ExecConfig) (ret int) {
if err != nil {
return 1
}

options.UseTLS = true
options.TLS = tlsConfig
}
Expand Down
21 changes: 13 additions & 8 deletions internal/commands/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,25 @@ func RunServer(config ServerConfig) int {
controller.SetStore(store)

options := consul.DefaultCertManagerOptions()
options.Addresses = []string{strings.Split(config.ConsulConfig.Address, ":")[0]}
options.GRPCPort = config.ConsulGRPCPort
options.PrimaryDatacenter = config.PrimaryDatacenter

tlsConfig, err := api.SetupTLSConfig(&config.ConsulConfig.TLSConfig)
if err != nil {
return 1
// If either CertFile or CertPEM are set for Consul API client,
// use TLS for CertManager gRPC connections
if !(config.ConsulConfig.TLSConfig.CertFile == "" &&
len(config.ConsulConfig.TLSConfig.CertPEM) == 0) {
tlsConfig, err := api.SetupTLSConfig(&config.ConsulConfig.TLSConfig)
if err != nil {
return 1
}

options.UseTLS = true
options.TLS = tlsConfig
}

certManager := consul.NewCertManager(
config.Logger.Named("cert-manager"),
consul.Config{
Addresses: []string{strings.Split(config.ConsulConfig.Address, ":")[0]},
GRPCPort: config.ConsulGRPCPort,
TLS: tlsConfig,
},
client,
"consul-api-gateway-controller",
options,
Expand Down
17 changes: 5 additions & 12 deletions internal/consul/certmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,12 @@ func TestManage(t *testing.T) {
server := runCertServer(t, service, test.leafFailures, test.rootFailures, test.expirations)

options := DefaultCertManagerOptions()
options.Addresses = []string{server.consulAddress}
options.GRPCPort = server.consulGRPCPort
options.Directory = directory

manager := NewCertManager(
hclog.Default().Named("certmanager"),
Config{
Addresses: []string{server.consulAddress},
GRPCPort: server.consulGRPCPort,
TLS: nil,
},
NewClient(server.consulHTTPClient),
service,
options,
Expand Down Expand Up @@ -128,13 +125,11 @@ func TestManage_Refresh(t *testing.T) {
server := runCertServer(t, service, 0, 0, 2)

options := DefaultCertManagerOptions()
options.Addresses = []string{server.consulAddress}
options.GRPCPort = server.consulGRPCPort

manager := NewCertManager(
hclog.Default().Named("certmanager"),
Config{
Addresses: []string{server.consulAddress},
GRPCPort: server.consulGRPCPort,
TLS: nil,
},
NewClient(server.consulHTTPClient),
service,
options,
Expand Down Expand Up @@ -188,7 +183,6 @@ func TestManage_WaitCancel(t *testing.T) {

err := NewCertManager(
hclog.Default().Named("certmanager"),
Config{},
nil,
"",
nil,
Expand Down Expand Up @@ -455,7 +449,6 @@ func TestRenderSDS(t *testing.T) {
options.Directory = "/certs"
manager := NewCertManager(
hclog.Default().Named("certmanager"),
Config{},
nil,
gwTesting.RandomString(),
options,
Expand Down
6 changes: 6 additions & 0 deletions internal/testing/e2e/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func init() {

type consulTestEnvironment struct {
ca []byte
consulUseTLS bool
consulTLSConfig *tls.Config
consulClient *api.Client
token string
Expand Down Expand Up @@ -219,6 +220,7 @@ func CreateTestConsulContainer(name, namespace string) env.Func {

env := &consulTestEnvironment{
ca: rootCA.CertBytes,
consulUseTLS: true,
consulTLSConfig: consulTLSConfig,
consulClient: consulClient,
httpPort: httpsPort,
Expand Down Expand Up @@ -458,6 +460,10 @@ func consulDeployment(namespace string, httpsPort, grpcPort int) *apps.Deploymen
}
}

func ConsulUseTLS(ctx context.Context) bool {
return mustGetTestEnvironment(ctx).consulUseTLS
}

func ConsulTLSConfig(ctx context.Context) *tls.Config {
return mustGetTestEnvironment(ctx).consulTLSConfig
}
Expand Down
9 changes: 4 additions & 5 deletions internal/testing/e2e/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,13 @@ func (p *gatewayTestEnvironment) run(ctx context.Context, namespace string, cfg

// set up the cert manager
certManagerOptions := consul.DefaultCertManagerOptions()
certManagerOptions.Addresses = []string{"localhost"}
certManagerOptions.GRPCPort = ConsulGRPCPort(ctx)
certManagerOptions.UseTLS = ConsulUseTLS(ctx)
certManagerOptions.TLS = ConsulTLSConfig(ctx)
certManagerOptions.Directory = p.directory
certManager := consul.NewCertManager(
nullLogger,
consul.Config{
Addresses: []string{"localhost"},
GRPCPort: ConsulGRPCPort(ctx),
TLS: ConsulTLSConfig(ctx),
},
client,
"consul-api-gateway",
certManagerOptions,
Expand Down

0 comments on commit ea90538

Please sign in to comment.