Skip to content

Commit

Permalink
check for valid http config rather than connect enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshasselberg committed Sep 13, 2018
1 parent 894e0b5 commit 889fba7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
52 changes: 27 additions & 25 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,28 @@ func LocalConfig(cfg *config.RuntimeConfig) local.Config {
return lc
}

func shouldRunProxyManager(cfg *config.RuntimeConfig) bool {
return !cfg.ConnectTestDisableManagedProxies && cfg.ConnectEnabled
func (a *Agent) setupProxyManager() error {
acfg, err := a.config.APIConfig(true)
if err != nil {
return fmt.Errorf("[INFO] agent: Connect managed proxies are disabled due to providing an invalid HTTP configuration")
}
a.proxyManager = proxy.NewManager()
a.proxyManager.AllowRoot = a.config.ConnectProxyAllowManagedRoot
a.proxyManager.State = a.State
a.proxyManager.Logger = a.logger
if a.config.DataDir != "" {
// DataDir is required for all non-dev mode agents, but we want
// to allow setting the data dir for demos and so on for the agent,
// so do the check above instead.
a.proxyManager.DataDir = filepath.Join(a.config.DataDir, "proxy")

// Restore from our snapshot (if it exists)
if err := a.proxyManager.Restore(a.proxyManager.SnapshotPath()); err != nil {
a.logger.Printf("[WARN] agent: error restoring proxy state: %s", err)
}
}
a.proxyManager.ProxyEnv = acfg.GenerateEnv()
return nil
}

func (a *Agent) Start() error {
Expand Down Expand Up @@ -376,30 +396,12 @@ func (a *Agent) Start() error {
// create the proxy process manager and start it. This is purposely
// done here after the local state above is loaded in so we can have
// a more accurate initial state view.
if shouldRunProxyManager(c) {
a.proxyManager = proxy.NewManager()
a.proxyManager.AllowRoot = a.config.ConnectProxyAllowManagedRoot
a.proxyManager.State = a.State
a.proxyManager.Logger = a.logger
if a.config.DataDir != "" {
// DataDir is required for all non-dev mode agents, but we want
// to allow setting the data dir for demos and so on for the agent,
// so do the check above instead.
a.proxyManager.DataDir = filepath.Join(a.config.DataDir, "proxy")

// Restore from our snapshot (if it exists)
if err := a.proxyManager.Restore(a.proxyManager.SnapshotPath()); err != nil {
a.logger.Printf("[WARN] agent: error restoring proxy state: %s", err)
}
}

acfg, err := a.config.APIConfig(true)
if err != nil {
return err
if !c.ConnectTestDisableManagedProxies {
if err := a.setupProxyManager(); err != nil {
a.logger.Printf(err.Error())
} else {
go a.proxyManager.Run()
}
a.proxyManager.ProxyEnv = acfg.GenerateEnv()

go a.proxyManager.Run()
}

// Start watching for critical services to deregister, based on their
Expand Down
41 changes: 26 additions & 15 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3055,20 +3055,31 @@ func TestAgent_ReLoadProxiesFromConfig(t *testing.T) {
require.Len(proxies, 0)
}

func TestAgent_ShouldRunProxyManager(t *testing.T) {
func TestAgent_SetupProxyManager(t *testing.T) {
t.Parallel()
tests := []struct {
Config config.RuntimeConfig
ShouldRunProxyManager bool
}{
{config.RuntimeConfig{ConnectEnabled: true, ConnectTestDisableManagedProxies: true}, false},
{config.RuntimeConfig{ConnectEnabled: true, ConnectTestDisableManagedProxies: false}, true},
{config.RuntimeConfig{ConnectEnabled: false, ConnectTestDisableManagedProxies: true}, false},
{config.RuntimeConfig{ConnectEnabled: false, ConnectTestDisableManagedProxies: false}, false},
}
for _, tt := range tests {
if shouldRunProxyManager(&tt.Config) != tt.ShouldRunProxyManager {
t.Fatal("Mismatch")
}
}
dataDir := testutil.TempDir(t, "agent") // we manage the data dir
defer os.RemoveAll(dataDir)
hcl := `
ports { http = -1 }
data_dir = "` + dataDir + `"
`
c := TestConfig(
// randomPortsSource(false),
config.Source{Name: t.Name(), Format: "hcl", Data: hcl},
)
a, err := New(c)
require.NoError(t, err)
require.Error(t, a.setupProxyManager(), "setupProxyManager should fail with invalid HTTP API config")

hcl = `
ports { http = 8001 }
data_dir = "` + dataDir + `"
`
c = TestConfig(
// randomPortsSource(false),
config.Source{Name: t.Name(), Format: "hcl", Data: hcl},
)
a, err = New(c)
require.NoError(t, err)
require.NoError(t, a.setupProxyManager())
}

0 comments on commit 889fba7

Please sign in to comment.