Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent: route templating server through cache #10927

Merged
merged 14 commits into from
Feb 23, 2021
Merged
3 changes: 1 addition & 2 deletions command/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ func (c *AgentCommand) Run(args []string) int {
Logger: c.logger.Named("template.server"),
LogLevel: level,
LogWriter: c.logWriter,
VaultConf: config.Vault,
TemplateRetry: config.TemplateRetry,
AgentConfig: config,
Namespace: namespace,
ExitAfterAuth: exitAfterAuth,
})
Expand Down
46 changes: 32 additions & 14 deletions command/agent/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
type ServerConfig struct {
Logger hclog.Logger
// Client *api.Client
VaultConf *config.Vault
AgentConfig *config.Config

ExitAfterAuth bool
TemplateRetry *config.TemplateRetry
Namespace string

// LogLevel is needed to set the internal Consul Template Runner's log level
Expand Down Expand Up @@ -164,12 +164,12 @@ func (ts *Server) Run(ctx context.Context, incoming chan string, templates []*ct
},
}

if ts.config.TemplateRetry != nil && ts.config.TemplateRetry.Enabled {
if ts.config.AgentConfig.TemplateRetry != nil && ts.config.AgentConfig.TemplateRetry.Enabled {
ctv.Vault.Retry = &ctconfig.RetryConfig{
Attempts: &ts.config.TemplateRetry.Attempts,
Backoff: &ts.config.TemplateRetry.Backoff,
MaxBackoff: &ts.config.TemplateRetry.MaxBackoff,
Enabled: &ts.config.TemplateRetry.Enabled,
Attempts: &ts.config.AgentConfig.TemplateRetry.Attempts,
Backoff: &ts.config.AgentConfig.TemplateRetry.Backoff,
MaxBackoff: &ts.config.AgentConfig.TemplateRetry.MaxBackoff,
Enabled: &ts.config.AgentConfig.TemplateRetry.Enabled,
}
} else if ts.testingLimitRetry != 0 {
// If we're testing, limit retries to 3 attempts to avoid
Expand Down Expand Up @@ -239,7 +239,7 @@ func newRunnerConfig(sc *ServerConfig, templates ctconfig.TemplateConfigs) (*ctc
// Always set these to ensure nothing is picked up from the environment
conf.Vault.RenewToken = pointerutil.BoolPtr(false)
conf.Vault.Token = pointerutil.StringPtr("")
conf.Vault.Address = &sc.VaultConf.Address
conf.Vault.Address = &sc.AgentConfig.Vault.Address

if sc.Namespace != "" {
conf.Vault.Namespace = &sc.Namespace
Expand All @@ -255,16 +255,34 @@ func newRunnerConfig(sc *ServerConfig, templates ctconfig.TemplateConfigs) (*ctc
ServerName: pointerutil.StringPtr(""),
}

if strings.HasPrefix(sc.VaultConf.Address, "https") || sc.VaultConf.CACert != "" {
skipVerify := sc.VaultConf.TLSSkipVerify
// Use the cache if available or fallback to the Vault server values.
if sc.AgentConfig.Cache != nil && len(sc.AgentConfig.Listeners) != 0 {
scheme := "unix://"
if sc.AgentConfig.Listeners[0].Type == "tcp" {
scheme = "https://"
if sc.AgentConfig.Listeners[0].TLSDisable {
scheme = "http://"
}
}
address := fmt.Sprintf("%s%s", scheme, sc.AgentConfig.Listeners[0].Address)
conf.Vault.Address = &address

// Skip verification if its using the cache because they're part of the same agent.
if scheme == "https" {
conf.Vault.SSL.Verify = pointerutil.BoolPtr(false)
conf.Vault.SSL.Cert = &sc.AgentConfig.Vault.ClientCert
conf.Vault.SSL.Key = &sc.AgentConfig.Vault.ClientKey
}
} else if strings.HasPrefix(sc.AgentConfig.Vault.Address, "https") || sc.AgentConfig.Vault.CACert != "" {
skipVerify := sc.AgentConfig.Vault.TLSSkipVerify
verify := !skipVerify
conf.Vault.SSL = &ctconfig.SSLConfig{
Enabled: pointerutil.BoolPtr(true),
Verify: &verify,
Cert: &sc.VaultConf.ClientCert,
Key: &sc.VaultConf.ClientKey,
CaCert: &sc.VaultConf.CACert,
CaPath: &sc.VaultConf.CAPath,
Cert: &sc.AgentConfig.Vault.ClientCert,
Key: &sc.AgentConfig.Vault.ClientKey,
CaCert: &sc.AgentConfig.Vault.CACert,
CaPath: &sc.AgentConfig.Vault.CAPath,
}
}

Expand Down
216 changes: 214 additions & 2 deletions command/agent/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

ctconfig "github.com/hashicorp/consul-template/config"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/command/agent/config"
"github.com/hashicorp/vault/internalshared/configutil"
"github.com/hashicorp/vault/sdk/helper/logging"
"github.com/hashicorp/vault/sdk/helper/pointerutil"
)
Expand All @@ -27,6 +29,214 @@ func TestNewServer(t *testing.T) {
}
}

func newAgentConfig(listeners []*configutil.Listener, enableCache bool) *config.Config {
agentConfig := &config.Config{
SharedConfig: &configutil.SharedConfig{
PidFile: "./pidfile",
Listeners: listeners,
},
AutoAuth: &config.AutoAuth{
Method: &config.Method{
Type: "aws",
MountPath: "auth/aws",
Config: map[string]interface{}{
"role": "foobar",
},
},
Sinks: []*config.Sink{
{
Type: "file",
DHType: "curve25519",
DHPath: "/tmp/file-foo-dhpath",
AAD: "foobar",
Config: map[string]interface{}{
"path": "/tmp/file-foo",
},
},
},
},
Vault: &config.Vault{
Address: "http://127.0.0.1:1111",
CACert: "config_ca_cert",
CAPath: "config_ca_path",
TLSSkipVerifyRaw: interface{}("true"),
TLSSkipVerify: true,
ClientCert: "config_client_cert",
ClientKey: "config_client_key",
},
}
if enableCache {
agentConfig.Cache = &config.Cache{UseAutoAuthToken: true}
}

return agentConfig
}

func TestCacheConfigUnix(t *testing.T) {
listeners := []*configutil.Listener{
{
Type: "unix",
Address: "foobar",
TLSDisable: true,
SocketMode: "configmode",
SocketUser: "configuser",
SocketGroup: "configgroup",
},
{
Type: "tcp",
Address: "127.0.0.1:8300",
TLSDisable: true,
},
{
Type: "tcp",
Address: "127.0.0.1:8400",
TLSKeyFile: "/path/to/cakey.pem",
TLSCertFile: "/path/to/cacert.pem",
},
}

agentConfig := newAgentConfig(listeners, true)
serverConfig := ServerConfig{AgentConfig: agentConfig}

ctConfig, err := newRunnerConfig(&serverConfig, ctconfig.TemplateConfigs{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

expected := "unix://foobar"
if *ctConfig.Vault.Address != expected {
t.Fatalf("expected %s, got %s", expected, *ctConfig.Vault.Address)
}
}

func TestCacheConfigHTTP(t *testing.T) {
jasonodonnell marked this conversation as resolved.
Show resolved Hide resolved
listeners := []*configutil.Listener{
{
Type: "tcp",
Address: "127.0.0.1:8300",
TLSDisable: true,
},
{
Type: "unix",
Address: "foobar",
TLSDisable: true,
SocketMode: "configmode",
SocketUser: "configuser",
SocketGroup: "configgroup",
},
{
Type: "tcp",
Address: "127.0.0.1:8400",
TLSKeyFile: "/path/to/cakey.pem",
TLSCertFile: "/path/to/cacert.pem",
},
}

agentConfig := newAgentConfig(listeners, true)
serverConfig := ServerConfig{AgentConfig: agentConfig}

ctConfig, err := newRunnerConfig(&serverConfig, ctconfig.TemplateConfigs{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

expected := "http://127.0.0.1:8300"
if *ctConfig.Vault.Address != expected {
t.Fatalf("expected %s, got %s", expected, *ctConfig.Vault.Address)
}
}

func TestCacheConfigHTTPS(t *testing.T) {
listeners := []*configutil.Listener{
{
Type: "tcp",
Address: "127.0.0.1:8300",
TLSKeyFile: "/path/to/cakey.pem",
TLSCertFile: "/path/to/cacert.pem",
},
{
Type: "unix",
Address: "foobar",
TLSDisable: true,
SocketMode: "configmode",
SocketUser: "configuser",
SocketGroup: "configgroup",
},
{
Type: "tcp",
Address: "127.0.0.1:8400",
TLSDisable: true,
},
}

agentConfig := newAgentConfig(listeners, true)
serverConfig := ServerConfig{AgentConfig: agentConfig}

ctConfig, err := newRunnerConfig(&serverConfig, ctconfig.TemplateConfigs{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

expected := "https://127.0.0.1:8300"
if *ctConfig.Vault.Address != expected {
t.Fatalf("expected %s, got %s", expected, *ctConfig.Vault.Address)
}
}

func TestCacheConfigNoCache(t *testing.T) {
listeners := []*configutil.Listener{
{
Type: "tcp",
Address: "127.0.0.1:8300",
TLSKeyFile: "/path/to/cakey.pem",
TLSCertFile: "/path/to/cacert.pem",
},
{
Type: "unix",
Address: "foobar",
TLSDisable: true,
SocketMode: "configmode",
SocketUser: "configuser",
SocketGroup: "configgroup",
},
{
Type: "tcp",
Address: "127.0.0.1:8400",
TLSDisable: true,
},
}

agentConfig := newAgentConfig(listeners, false)
serverConfig := ServerConfig{AgentConfig: agentConfig}

ctConfig, err := newRunnerConfig(&serverConfig, ctconfig.TemplateConfigs{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

expected := "http://127.0.0.1:1111"
if *ctConfig.Vault.Address != expected {
t.Fatalf("expected %s, got %s", expected, *ctConfig.Vault.Address)
}
}

func TestCacheConfigNoListener(t *testing.T) {
listeners := []*configutil.Listener{}

agentConfig := newAgentConfig(listeners, true)
serverConfig := ServerConfig{AgentConfig: agentConfig}

ctConfig, err := newRunnerConfig(&serverConfig, ctconfig.TemplateConfigs{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

expected := "http://127.0.0.1:1111"
if *ctConfig.Vault.Address != expected {
t.Fatalf("expected %s, got %s", expected, *ctConfig.Vault.Address)
}
}

func TestServerRun(t *testing.T) {
// create http test server
mux := http.NewServeMux()
Expand Down Expand Up @@ -162,8 +372,10 @@ func TestServerRun(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), 20*time.Second)
sc := ServerConfig{
Logger: logging.NewVaultLogger(hclog.Trace),
VaultConf: &config.Vault{
Address: ts.URL,
AgentConfig: &config.Config{
Vault: &config.Vault{
Address: ts.URL,
},
},
LogLevel: hclog.Trace,
LogWriter: hclog.DefaultOutput,
Expand Down