github.com/blend/go-sdk@v1.20220411.3/vault/config.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package vault
     9  
    10  import (
    11  	"context"
    12  	"time"
    13  
    14  	"github.com/blend/go-sdk/configutil"
    15  )
    16  
    17  // Config is the secrets config object.
    18  type Config struct {
    19  	// Addr is the remote address of the secret store.
    20  	Addr string `json:"addr" yaml:"addr" env:"VAULT_ADDR"`
    21  	// Mount is the default mount path, it prefixes any paths.
    22  	Mount string `json:"mount" yaml:"mount" env:"VAULT_MOUNT"`
    23  	// Token is the authentication token used to talk to the secret store.
    24  	Token string `json:"token" yaml:"token" env:"VAULT_TOKEN"`
    25  	// Timeout is the dial timeout for requests to the secrets store.
    26  	Timeout time.Duration `json:"timeout" yaml:"timeout" env:"VAULT_TIMEOUT"`
    27  	// RootCAs is a list of certificate authority paths.
    28  	RootCAs []string `json:"rootCAs" yaml:"rootCAs" env:"VAULT_CA_CERT,csv"`
    29  }
    30  
    31  // IsZero returns if the config is set or not.
    32  func (c Config) IsZero() bool {
    33  	return len(c.Token) == 0
    34  }
    35  
    36  // Resolve reads the environment into the config on configutil.Read(...)
    37  func (c *Config) Resolve(ctx context.Context) error {
    38  	return configutil.Resolve(ctx,
    39  		configutil.SetString(&c.Addr, configutil.String(c.Addr), configutil.Env(EnvVarVaultAddr)),
    40  		configutil.SetString(&c.Mount, configutil.String(c.Mount), configutil.Env(EnvVarVaultMount)),
    41  		configutil.SetString(&c.Token, configutil.String(c.Token), configutil.Env(EnvVarVaultToken)),
    42  		configutil.SetStrings(&c.RootCAs, configutil.Strings(c.RootCAs), configutil.Env(EnvVarVaultCertAuthorityPath)),
    43  		configutil.SetDuration(&c.Timeout, configutil.Duration(c.Timeout), configutil.Env(EnvVarVaultTimeout)),
    44  	)
    45  }
    46  
    47  // AddrOrDefault returns the client addr.
    48  func (c Config) AddrOrDefault() string {
    49  	if c.Addr != "" {
    50  		return c.Addr
    51  	}
    52  	return DefaultAddr
    53  }
    54  
    55  // TimeoutOrDefault returns the client timeout.
    56  func (c Config) TimeoutOrDefault() time.Duration {
    57  	if c.Timeout > 0 {
    58  		return c.Timeout
    59  	}
    60  	return DefaultTimeout
    61  }
    62  
    63  // MountOrDefault returns secrets mount or a default.
    64  func (c Config) MountOrDefault() string {
    65  	if c.Mount != "" {
    66  		return c.Mount
    67  	}
    68  	return DefaultMount
    69  }