Skip to content

Commit

Permalink
OSS changes for ent pr (#13696)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmmukh authored Jan 19, 2022
1 parent 2f1c191 commit e2d713a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
17 changes: 15 additions & 2 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,12 +928,25 @@ func (c *Client) ReadYourWrites() bool {
// Clone creates a new client with the same configuration. Note that the same
// underlying http.Client is used; modifying the client from more than one
// goroutine at once may not be safe, so modify the client as needed and then
// clone.
// clone. The headers are cloned based on the CloneHeaders property of the
// source config
//
// Also, only the client's config is currently copied; this means items not in
// the api.Config struct, such as policy override and wrapping function
// behavior, must currently then be set as desired on the new client.
func (c *Client) Clone() (*Client, error) {
return c.clone(c.config.CloneHeaders)
}

// CloneWithHeaders creates a new client similar to Clone, with the difference
// being that the headers are always cloned
func (c *Client) CloneWithHeaders() (*Client, error) {
return c.clone(true)
}

// clone creates a new client, with the headers being cloned based on the
// passed in cloneheaders boolean
func (c *Client) clone(cloneHeaders bool) (*Client, error) {
c.modifyLock.RLock()
defer c.modifyLock.RUnlock()

Expand Down Expand Up @@ -964,7 +977,7 @@ func (c *Client) Clone() (*Client, error) {
return nil, err
}

if config.CloneHeaders {
if cloneHeaders {
client.SetHeaders(c.Headers().Clone())
}

Expand Down
21 changes: 7 additions & 14 deletions command/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net"
"net/http"
"os"
"path"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -343,8 +342,12 @@ func (c *AgentCommand) Run(args []string) int {

var method auth.AuthMethod
var sinks []*sink.SinkConfig
var namespace string
var templateNamespace string
if config.AutoAuth != nil {
if client.Headers().Get(consts.NamespaceHeaderName) == "" && config.AutoAuth.Method.Namespace != "" {
client.SetNamespace(config.AutoAuth.Method.Namespace)
}
templateNamespace = client.Headers().Get(consts.NamespaceHeaderName)
for _, sc := range config.AutoAuth.Sinks {
switch sc.Type {
case "file":
Expand All @@ -371,19 +374,9 @@ func (c *AgentCommand) Run(args []string) int {
}
}

// Check if a default namespace has been set
mountPath := config.AutoAuth.Method.MountPath
if cns := config.AutoAuth.Method.Namespace; cns != "" {
namespace = cns
// Only set this value if the env var is empty, otherwise we end up with a nested namespace
if ens := os.Getenv(api.EnvVaultNamespace); ens == "" {
mountPath = path.Join(cns, mountPath)
}
}

authConfig := &auth.AuthConfig{
Logger: c.logger.Named(fmt.Sprintf("auth.%s", config.AutoAuth.Method.Type)),
MountPath: mountPath,
MountPath: config.AutoAuth.Method.MountPath,
Config: config.AutoAuth.Method.Config,
}
switch config.AutoAuth.Method.Type {
Expand Down Expand Up @@ -794,7 +787,7 @@ func (c *AgentCommand) Run(args []string) int {
LogLevel: level,
LogWriter: c.logWriter,
AgentConfig: config,
Namespace: namespace,
Namespace: templateNamespace,
ExitAfterAuth: exitAfterAuth,
})

Expand Down
4 changes: 4 additions & 0 deletions command/agent/auth/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/command/agent/auth"
"github.com/hashicorp/vault/sdk/helper/consts"
)

type certMethod struct {
Expand Down Expand Up @@ -133,6 +134,9 @@ func (c *certMethod) AuthClient(client *api.Client) (*api.Client, error) {
if err != nil {
return nil, err
}
if ns := client.Headers().Get(consts.NamespaceHeaderName); ns != "" {
clientToAuth.SetNamespace(ns)
}

// Cache the client for future use
c.client = clientToAuth
Expand Down
4 changes: 3 additions & 1 deletion command/agent/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,16 @@ func (s *SinkConfig) encryptToken(token string) (string, error) {
}

func (s *SinkConfig) wrapToken(client *api.Client, wrapTTL time.Duration, token string) (string, error) {
wrapClient, err := client.Clone()
wrapClient, err := client.CloneWithHeaders()
if err != nil {
return "", fmt.Errorf("error deriving client for wrapping, not writing out to sink: %w)", err)
}

wrapClient.SetToken(token)
wrapClient.SetWrappingLookupFunc(func(string, string) string {
return wrapTTL.String()
})

secret, err := wrapClient.Logical().Write("sys/wrapping/wrap", map[string]interface{}{
"token": token,
})
Expand Down

0 comments on commit e2d713a

Please sign in to comment.