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: add disable_idle_connections configurable #15986

Merged
merged 10 commits into from Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions api/client.go
Expand Up @@ -720,6 +720,42 @@ func (c *Client) SetMaxRetries(retries int) {
c.config.MaxRetries = retries
}

func (c *Client) SetDisableKeepAlives(disable bool) {
jasonodonnell marked this conversation as resolved.
Show resolved Hide resolved
c.modifyLock.RLock()
defer c.modifyLock.RUnlock()
c.config.modifyLock.Lock()
defer c.config.modifyLock.Unlock()

c.config.HttpClient.Transport.(*http.Transport).DisableKeepAlives = disable
}

func (c *Client) DisableKeepAlives() bool {
c.modifyLock.RLock()
defer c.modifyLock.RUnlock()
c.config.modifyLock.Lock()
defer c.config.modifyLock.Unlock()

return c.config.HttpClient.Transport.(*http.Transport).DisableKeepAlives
}

func (c *Client) SetMaxIdleConnections(idle int) {
c.modifyLock.RLock()
defer c.modifyLock.RUnlock()
c.config.modifyLock.Lock()
defer c.config.modifyLock.Unlock()

c.config.HttpClient.Transport.(*http.Transport).MaxIdleConns = idle
}

func (c *Client) MaxIdleConnections() int {
c.modifyLock.RLock()
defer c.modifyLock.RUnlock()
c.config.modifyLock.Lock()
defer c.config.modifyLock.Unlock()

return c.config.HttpClient.Transport.(*http.Transport).MaxIdleConns
}

func (c *Client) MaxRetries() int {
c.modifyLock.RLock()
defer c.modifyLock.RUnlock()
Expand Down
2 changes: 2 additions & 0 deletions changelog/15986.txt
@@ -0,0 +1,2 @@
```release-note:improvement
agent: Added `disable_idle_connections` configuration to disable leaving idle connections open in auto-auth, caching and templating.
38 changes: 32 additions & 6 deletions command/agent.go
Expand Up @@ -368,13 +368,24 @@ func (c *AgentCommand) Run(args []string) int {
client.SetNamespace(config.AutoAuth.Method.Namespace)
}
templateNamespace = client.Headers().Get(consts.NamespaceHeaderName)

sinkClient, err := client.CloneWithHeaders()
if err != nil {
c.UI.Error(fmt.Sprintf("Error cloning client for file sink: %v", err))
return 1
}

if config.DisableIdleConnsAutoAuth {
sinkClient.SetMaxIdleConnections(-1)
swenson marked this conversation as resolved.
Show resolved Hide resolved
}

for _, sc := range config.AutoAuth.Sinks {
switch sc.Type {
case "file":
config := &sink.SinkConfig{
Logger: c.logger.Named("sink.file"),
Config: sc.Config,
Client: client,
Client: sinkClient,
WrapTTL: sc.WrapTTL,
DHType: sc.DHType,
DeriveKey: sc.DeriveKey,
Expand Down Expand Up @@ -490,9 +501,19 @@ func (c *AgentCommand) Run(args []string) int {
if config.Cache != nil {
cacheLogger := c.logger.Named("cache")

proxyClient, err := client.CloneWithHeaders()
if err != nil {
c.UI.Error(fmt.Sprintf("Error cloning client for caching: %v", err))
return 1
}

if config.DisableIdleConnsAutoAuth {
proxyClient.SetMaxIdleConnections(-1)
}

// Create the API proxier
apiProxy, err := cache.NewAPIProxy(&cache.APIProxyConfig{
Client: client,
Client: proxyClient,
Logger: cacheLogger.Named("apiproxy"),
EnforceConsistency: enforceConsistency,
WhenInconsistentAction: whenInconsistent,
Expand All @@ -505,7 +526,7 @@ func (c *AgentCommand) Run(args []string) int {
// Create the lease cache proxier and set its underlying proxier to
// the API proxier.
leaseCache, err = cache.NewLeaseCache(&cache.LeaseCacheConfig{
Client: client,
Client: proxyClient,
BaseContext: ctx,
Proxier: apiProxy,
Logger: cacheLogger.Named("leasecache"),
Expand Down Expand Up @@ -793,14 +814,19 @@ func (c *AgentCommand) Run(args []string) int {

// Auth Handler is going to set its own retry values, so we want to
// work on a copy of the client to not affect other subsystems.
clonedClient, err := c.client.CloneWithHeaders()
ahClient, err := c.client.CloneWithHeaders()
if err != nil {
c.UI.Error(fmt.Sprintf("Error cloning client for auth handler: %v", err))
return 1
}

if config.DisableIdleConnsAutoAuth {
ahClient.SetMaxIdleConnections(-1)
}

ah := auth.NewAuthHandler(&auth.AuthHandlerConfig{
Logger: c.logger.Named("auth.handler"),
Client: clonedClient,
Client: ahClient,
WrapTTL: config.AutoAuth.Method.WrapTTL,
MinBackoff: config.AutoAuth.Method.MinBackoff,
MaxBackoff: config.AutoAuth.Method.MaxBackoff,
Expand All @@ -811,7 +837,7 @@ func (c *AgentCommand) Run(args []string) int {

ss := sink.NewSinkServer(&sink.SinkServerConfig{
Logger: c.logger.Named("sink.server"),
Client: client,
Client: ahClient,
ExitAfterAuth: exitAfterAuth,
})

Expand Down
37 changes: 31 additions & 6 deletions command/agent/config/config.go
Expand Up @@ -24,14 +24,20 @@ import (
type Config struct {
*configutil.SharedConfig `hcl:"-"`

AutoAuth *AutoAuth `hcl:"auto_auth"`
ExitAfterAuth bool `hcl:"exit_after_auth"`
Cache *Cache `hcl:"cache"`
Vault *Vault `hcl:"vault"`
TemplateConfig *TemplateConfig `hcl:"template_config"`
Templates []*ctconfig.TemplateConfig `hcl:"templates"`
AutoAuth *AutoAuth `hcl:"auto_auth"`
ExitAfterAuth bool `hcl:"exit_after_auth"`
Cache *Cache `hcl:"cache"`
Vault *Vault `hcl:"vault"`
TemplateConfig *TemplateConfig `hcl:"template_config"`
Templates []*ctconfig.TemplateConfig `hcl:"templates"`
DisableIdleConns string `hcl:"disable_idle_connections"`
DisableIdleConnsCaching bool `hcl:"-"`
DisableIdleConnsTemplating bool `hcl:"-"`
DisableIdleConnsAutoAuth bool `hcl:"-"`
}

const DisableIdleConnsEnv = "VAULT_AGENT_DISABLE_IDLE_CONNECTIONS"

func (c *Config) Prune() {
for _, l := range c.Listeners {
l.RawConfig = nil
Expand Down Expand Up @@ -260,6 +266,25 @@ func LoadConfig(path string) (*Config, error) {
result.Vault.Retry.NumRetries = 0
}

if disableIdleConnsEnv := os.Getenv(DisableIdleConnsEnv); disableIdleConnsEnv != "" {
result.DisableIdleConns = disableIdleConnsEnv
}

if result.DisableIdleConns != "" {
diableIdleConns := strings.ToLower(result.DisableIdleConns)
if strings.Contains(diableIdleConns, "auto-auth") {
jasonodonnell marked this conversation as resolved.
Show resolved Hide resolved
result.DisableIdleConnsAutoAuth = true
}

if strings.Contains(diableIdleConns, "caching") {
result.DisableIdleConnsCaching = true
}

if strings.Contains(diableIdleConns, "templating") {
result.DisableIdleConnsTemplating = true
}
}

return result, nil
}

Expand Down