vault/command/base_test.go

112 lines
2.4 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"net/http"
"reflect"
"testing"
Add HCP subcommand -- integrate with HCP library (#23897) * Add HCP engine token logic * Update documentation * Fix content check * Add changelog entry * Update changelog/23897.txt Co-authored-by: Nick Cabatoff <ncabatoff@hashicorp.com> * Update api/client.go Co-authored-by: Chris Capurso <1036769+ccapurso@users.noreply.github.com> * Add error when HCP commands failed to be initialize * Add tests for initHCPcommand function * Update lib dependency * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Fix docs nav data * Improve docs sections * Update hcp lib dependency * Fix content formatting * Update lib dependency * Fix HCPtokenhelper problem * Fix HCPtokenhelper problem * Remove HCP env variables * Remove hcp helper token * Add error treatment for no valid credential source * Update website/content/docs/commands/hcp/index.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/disconnect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/connect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/disconnect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/index.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp/connect.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Modify hcp tests --------- Co-authored-by: Nick Cabatoff <ncabatoff@hashicorp.com> Co-authored-by: Chris Capurso <1036769+ccapurso@users.noreply.github.com> Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com>
2024-01-09 09:29:30 -05:00
hcpvlib "github.com/hashicorp/vault-hcp-lib"
Add HCP subcommand -- integrate with HCP library (#23897) * Add HCP engine token logic * Update documentation * Fix content check * Add changelog entry * Update changelog/23897.txt Co-authored-by: Nick Cabatoff <ncabatoff@hashicorp.com> * Update api/client.go Co-authored-by: Chris Capurso <1036769+ccapurso@users.noreply.github.com> * Add error when HCP commands failed to be initialize * Add tests for initHCPcommand function * Update lib dependency * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Fix docs nav data * Improve docs sections * Update hcp lib dependency * Fix content formatting * Update lib dependency * Fix HCPtokenhelper problem * Fix HCPtokenhelper problem * Remove HCP env variables * Remove hcp helper token * Add error treatment for no valid credential source * Update website/content/docs/commands/hcp/index.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/disconnect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/connect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/disconnect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/index.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp/connect.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Modify hcp tests --------- Co-authored-by: Nick Cabatoff <ncabatoff@hashicorp.com> Co-authored-by: Chris Capurso <1036769+ccapurso@users.noreply.github.com> Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com>
2024-01-09 09:29:30 -05:00
"github.com/hashicorp/vault/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func getDefaultCliHeaders(t *testing.T) http.Header {
bc := &BaseCommand{}
cli, err := bc.Client()
if err != nil {
t.Fatal(err)
}
return cli.Headers()
}
func TestClient_FlagHeader(t *testing.T) {
defaultHeaders := getDefaultCliHeaders(t)
cases := []struct {
Input map[string]string
Valid bool
}{
{
map[string]string{},
true,
},
{
map[string]string{"foo": "bar", "header2": "value2"},
true,
},
{
map[string]string{"X-Vault-foo": "bar", "header2": "value2"},
false,
},
}
for _, tc := range cases {
expectedHeaders := defaultHeaders.Clone()
for key, val := range tc.Input {
expectedHeaders.Add(key, val)
}
bc := &BaseCommand{flagHeader: tc.Input}
cli, err := bc.Client()
if err == nil && !tc.Valid {
t.Errorf("No error for input[%#v], but not valid", tc.Input)
continue
}
if err != nil {
if tc.Valid {
t.Errorf("Error[%v] with input[%#v], but valid", err, tc.Input)
}
continue
}
if cli == nil {
t.Error("client should not be nil")
}
actualHeaders := cli.Headers()
if !reflect.DeepEqual(expectedHeaders, actualHeaders) {
t.Errorf("expected [%#v] but got [%#v]", expectedHeaders, actualHeaders)
}
}
}
Add HCP subcommand -- integrate with HCP library (#23897) * Add HCP engine token logic * Update documentation * Fix content check * Add changelog entry * Update changelog/23897.txt Co-authored-by: Nick Cabatoff <ncabatoff@hashicorp.com> * Update api/client.go Co-authored-by: Chris Capurso <1036769+ccapurso@users.noreply.github.com> * Add error when HCP commands failed to be initialize * Add tests for initHCPcommand function * Update lib dependency * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Fix docs nav data * Improve docs sections * Update hcp lib dependency * Fix content formatting * Update lib dependency * Fix HCPtokenhelper problem * Fix HCPtokenhelper problem * Remove HCP env variables * Remove hcp helper token * Add error treatment for no valid credential source * Update website/content/docs/commands/hcp/index.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/disconnect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/connect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/disconnect.mdx Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com> * Update website/content/docs/commands/hcp/index.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Update website/content/docs/commands/hcp/connect.mdx Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> * Modify hcp tests --------- Co-authored-by: Nick Cabatoff <ncabatoff@hashicorp.com> Co-authored-by: Chris Capurso <1036769+ccapurso@users.noreply.github.com> Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com> Co-authored-by: Yoko Hyakuna <yoko@hashicorp.com>
2024-01-09 09:29:30 -05:00
// 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())
}
})
}
}