Skip to content

Commit

Permalink
Add HCP engine token logic
Browse files Browse the repository at this point in the history
  • Loading branch information
biazmoreira committed Nov 2, 2023
1 parent 6af8bc7 commit 9e0c1d3
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 165 deletions.
34 changes: 33 additions & 1 deletion api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ const (
const (
EnvVaultAgentAddress = "VAULT_AGENT_ADDR"
EnvVaultInsecure = "VAULT_SKIP_VERIFY"

DefaultAddress = "https://127.0.0.1:8200"
)

// WrappingLookupFunc is a function that, given an HTTP verb and a path,
Expand Down Expand Up @@ -248,7 +250,7 @@ type TLSConfig struct {
// If an error is encountered, the Error field on the returned *Config will be populated with the specific error.
func DefaultConfig() *Config {
config := &Config{
Address: "https://127.0.0.1:8200",
Address: DefaultAddress,
HttpClient: cleanhttp.DefaultPooledClient(),
Timeout: time.Second * 60,
MinRetryWait: time.Millisecond * 1000,
Expand Down Expand Up @@ -589,6 +591,7 @@ type Client struct {
requestCallbacks []RequestCallback
responseCallbacks []ResponseCallback
replicationStateStore *replicationStateStore
hcpCookie *http.Cookie
}

// NewClient returns a new client for the given configuration.
Expand Down Expand Up @@ -1025,6 +1028,33 @@ func (c *Client) SetToken(v string) {
c.token = v
}

// HCPCookie returns the cookie being used by this client. It will
// return an empty cookie no cookie set.
func (c *Client) HCPCookie() string {
c.modifyLock.RLock()
defer c.modifyLock.RUnlock()

if c.hcpCookie == nil {
return ""
}
return c.hcpCookie.String()
}

// SetHCPCookie sets the hcp cookie directly. This won't perform any auth
// verification, it simply sets the token properly for future requests.
func (c *Client) SetHCPCookie(v *http.Cookie) error {
c.modifyLock.Lock()
defer c.modifyLock.Unlock()

if err := v.Valid(); err != nil {
return err
}

c.hcpCookie = v

return nil
}

// ClearToken deletes the token if it is set or does nothing otherwise.
func (c *Client) ClearToken() {
c.modifyLock.Lock()
Expand Down Expand Up @@ -1299,6 +1329,8 @@ func (c *Client) NewRequest(method, requestPath string) *Request {
Params: make(map[string][]string),
}

req.HCPCookie = c.hcpCookie

var lookupPath string
switch {
case strings.HasPrefix(requestPath, "/v1/"):
Expand Down
7 changes: 7 additions & 0 deletions api/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ type Request struct {
// EGPs). If set, the override flag will take effect for all policies
// evaluated during the request.
PolicyOverride bool

// HCPCookie is used to set a http cookie when client is connected to HCP
HCPCookie *http.Cookie
}

// SetJSONBody is used to set a request body that is a JSON-encoded value.
Expand Down Expand Up @@ -145,5 +148,9 @@ func (r *Request) toRetryableHTTP() (*retryablehttp.Request, error) {
req.Header.Set("X-Vault-Policy-Override", "true")
}

if r.HCPCookie != nil {
req.AddCookie(r.HCPCookie)
}

return req, nil
}
39 changes: 38 additions & 1 deletion command/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
Expand All @@ -22,6 +23,8 @@ import (
"github.com/mitchellh/cli"
"github.com/pkg/errors"
"github.com/posener/complete"

hcpvlib "github.com/hashicorp/vault-hcp-lib"
)

const (
Expand Down Expand Up @@ -69,7 +72,8 @@ type BaseCommand struct {

flagHeader map[string]string

tokenHelper token.TokenHelper
tokenHelper token.TokenHelper
hcpTokenHelper hcpvlib.HCPTokenHelper

client *api.Client
}
Expand All @@ -79,6 +83,10 @@ type BaseCommand struct {
func (c *BaseCommand) Client() (*api.Client, error) {
// Read the test client if present
if c.client != nil {
if err := c.applyHCPConfig(); err != nil {
return nil, err
}

return c.client, nil
}

Expand Down Expand Up @@ -195,9 +203,38 @@ func (c *BaseCommand) Client() (*api.Client, error) {

c.client = client

if err := c.applyHCPConfig(); err != nil {
return nil, err
}

return client, nil
}

func (c *BaseCommand) applyHCPConfig() error {
hcpToken, err := c.hcpTokenHelper.GetHCPToken()
if err != nil {
return err
}

if hcpToken != nil {
cookie := &http.Cookie{
Name: "hcp_access_token",
Value: hcpToken.AccessToken,
Expires: hcpToken.AccessTokenExpiry,
}

if err := c.client.SetHCPCookie(cookie); err != nil {
return fmt.Errorf("unable to correctly connect to the HCP Vault cluster; please reconnect to HCP: %w", err)
}

if err := c.client.SetAddress(hcpToken.ProxyAddr); err != nil {
return fmt.Errorf("unable to correctly set the HCP address: %w", err)
}
}

return nil
}

// SetAddress sets the token helper on the command; useful for the demo server and other outside cases.
func (c *BaseCommand) SetAddress(addr string) {
c.flagAddress = addr
Expand Down
40 changes: 40 additions & 0 deletions command/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import (
"net/http"
"reflect"
"testing"

"github.com/hashicorp/vault/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

hcpvlib "github.com/hashicorp/vault-hcp-lib"
)

func getDefaultCliHeaders(t *testing.T) http.Header {
Expand Down Expand Up @@ -70,3 +76,37 @@ func TestClient_FlagHeader(t *testing.T) {
}
}
}

// TestClient_HCPConfiguration tests that the HCP configuration is applied correctly when it exists in cache.
func TestClient_HCPConfiguration(t *testing.T) {
cases := map[string]struct {
Valid bool
ExpectedAddr string
}{
"valid hcp configuration": {
Valid: true,
ExpectedAddr: "https://hcp-proxy.addr:8200",
},
"empty hcp configuration": {
Valid: false,
ExpectedAddr: api.DefaultAddress,
},
}

for n, tst := range cases {
t.Run(n, func(t *testing.T) {
bc := &BaseCommand{hcpTokenHelper: &hcpvlib.TestingHCPTokenHelper{tst.Valid}}
cli, err := bc.Client()
assert.NoError(t, err)

if tst.Valid {
require.Equal(t, tst.ExpectedAddr, cli.Address())
require.NotEmpty(t, cli.HCPCookie())
require.Contains(t, cli.HCPCookie(), "hcp_access_token=Test.Access.Token")
} else {
require.Equal(t, tst.ExpectedAddr, cli.Address())
require.Empty(t, cli.HCPCookie())
}
})
}
}
19 changes: 15 additions & 4 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ import (
sr "github.com/hashicorp/vault/serviceregistration"
csr "github.com/hashicorp/vault/serviceregistration/consul"
ksr "github.com/hashicorp/vault/serviceregistration/kubernetes"

hcpvlib "github.com/hashicorp/vault-hcp-lib"
)

const (
Expand Down Expand Up @@ -245,10 +247,11 @@ var (
func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) map[string]cli.CommandFactory {
getBaseCommand := func() *BaseCommand {
return &BaseCommand{
UI: ui,
tokenHelper: runOpts.TokenHelper,
flagAddress: runOpts.Address,
client: runOpts.Client,
UI: ui,
tokenHelper: runOpts.TokenHelper,
flagAddress: runOpts.Address,
client: runOpts.Client,
hcpTokenHelper: &hcpvlib.InternalHCPTokenHelper{},
}
}

Expand Down Expand Up @@ -905,9 +908,17 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) map[string]cli.Co
}

entInitCommands(ui, serverCmdUi, runOpts, commands)
initHCPCommands(ui, commands)

return commands
}

func initHCPCommands(ui cli.Ui, commands map[string]cli.CommandFactory) {
for cmd, cmdFactory := range hcpvlib.InitHCPCommand(ui) {
commands[cmd] = cmdFactory
}
}

// MakeShutdownCh returns a channel that can be used for shutdown
// notifications for commands. This channel will send a message for every
// SIGINT or SIGTERM received.
Expand Down
48 changes: 26 additions & 22 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ module github.com/hashicorp/vault
// semantic related to Go module handling), this comment should be updated to explain that.
//
// Whenever this value gets updated, sdk/go.mod should be updated to the same value.
go 1.20
go 1.21

toolchain go1.21.3

replace github.com/hashicorp/vault/api => ./api

Expand Down Expand Up @@ -41,7 +43,7 @@ require (
github.com/apple/foundationdb/bindings/go v0.0.0-20190411004307-cd5c9d91fad2
github.com/armon/go-metrics v0.4.1
github.com/armon/go-radix v1.0.0
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/aws/aws-sdk-go v1.44.331
github.com/aws/aws-sdk-go-v2/config v1.18.19
github.com/axiomhq/hyperloglog v0.0.0-20220105174342-98591331716a
Expand Down Expand Up @@ -78,7 +80,7 @@ require (
github.com/hashicorp/consul-template v0.33.0
github.com/hashicorp/consul/api v1.23.0
github.com/hashicorp/errwrap v1.1.0
github.com/hashicorp/eventlogger v0.2.5
github.com/hashicorp/eventlogger v0.2.3
github.com/hashicorp/go-bexpr v0.1.12
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-discover v0.0.0-20210818145131-c573d69da192
Expand Down Expand Up @@ -120,12 +122,13 @@ require (
github.com/hashicorp/hcl/v2 v2.16.2
github.com/hashicorp/hcp-link v0.1.0
github.com/hashicorp/hcp-scada-provider v0.2.1
github.com/hashicorp/hcp-sdk-go v0.23.0
github.com/hashicorp/hcp-sdk-go v0.70.1-0.20231027171745-aa8cd4ca3fa0
github.com/hashicorp/nomad/api v0.0.0-20230519153805-2275a83cbfdf
github.com/hashicorp/raft v1.3.10
github.com/hashicorp/raft-autopilot v0.2.0
github.com/hashicorp/raft-boltdb/v2 v2.0.0-20210421194847-a7e34179d62c
github.com/hashicorp/raft-snapshot v1.0.4
github.com/hashicorp/vault-hcp-lib v0.0.0-20231102155729-bf8c13c2e544
github.com/hashicorp/vault-plugin-auth-alicloud v0.16.0
github.com/hashicorp/vault-plugin-auth-azure v0.16.2
github.com/hashicorp/vault-plugin-auth-centrify v0.15.1
Expand All @@ -149,7 +152,7 @@ require (
github.com/hashicorp/vault-plugin-secrets-gcpkms v0.15.1
github.com/hashicorp/vault-plugin-secrets-kubernetes v0.6.0
github.com/hashicorp/vault-plugin-secrets-kv v0.16.2
github.com/hashicorp/vault-plugin-secrets-mongodbatlas v0.10.2
github.com/hashicorp/vault-plugin-secrets-mongodbatlas v0.10.1
github.com/hashicorp/vault-plugin-secrets-openldap v0.11.2
github.com/hashicorp/vault-plugin-secrets-terraform v0.7.3
github.com/hashicorp/vault-testing-stepwise v0.1.4
Expand Down Expand Up @@ -212,17 +215,17 @@ require (
go.opentelemetry.io/otel/trace v1.16.0
go.uber.org/atomic v1.11.0
go.uber.org/goleak v1.2.1
golang.org/x/crypto v0.14.0
golang.org/x/crypto v0.13.0
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
golang.org/x/net v0.17.0
golang.org/x/oauth2 v0.11.0
golang.org/x/net v0.15.0
golang.org/x/oauth2 v0.12.0
golang.org/x/sync v0.3.0
golang.org/x/sys v0.13.0
golang.org/x/term v0.13.0
golang.org/x/sys v0.12.0
golang.org/x/term v0.12.0
golang.org/x/text v0.13.0
golang.org/x/tools v0.10.0
golang.org/x/tools v0.9.1
google.golang.org/api v0.138.0
google.golang.org/grpc v1.58.3
google.golang.org/grpc v1.57.0
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
google.golang.org/protobuf v1.31.0
gopkg.in/ory-am/dockertest.v3 v3.3.4
Expand Down Expand Up @@ -334,8 +337,8 @@ require (
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/envoyproxy/go-control-plane v0.11.1 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.2 // indirect
github.com/envoyproxy/go-control-plane v0.11.1-0.20230524094728-9239064ad72f // indirect
github.com/envoyproxy/protoc-gen-validate v0.10.1 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/form3tech-oss/jwt-go v3.2.5+incompatible // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand All @@ -348,16 +351,16 @@ require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/analysis v0.20.0 // indirect
github.com/go-openapi/errors v0.20.1 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/loads v0.20.2 // indirect
github.com/go-openapi/runtime v0.19.24 // indirect
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-openapi/strfmt v0.20.0 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/runtime v0.25.0 // indirect
github.com/go-openapi/spec v0.20.8 // indirect
github.com/go-openapi/strfmt v0.21.3 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-openapi/validate v0.20.2 // indirect
github.com/go-openapi/validate v0.22.1 // indirect
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
Expand Down Expand Up @@ -455,6 +458,7 @@ require (
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nicolai86/scaleway-sdk v1.10.2-0.20180628010248-798f60e20bb2 // indirect
github.com/nwaples/rardecode v1.1.2 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect
github.com/opencontainers/runc v1.1.6 // indirect
Expand All @@ -469,7 +473,7 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/pquerna/cachecontrol v0.1.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/renier/xmlrpc v0.0.0-20170708154548-ce4a1a486c03 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
Expand Down
Loading

0 comments on commit 9e0c1d3

Please sign in to comment.