Files
threshold/internal/credential/vault/vault_test.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

291 lines
7.1 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package vault
import (
"context"
"fmt"
"net/http"
"path"
"testing"
"time"
"github.com/hashicorp/boundary/internal/errors"
vault "github.com/hashicorp/vault/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_newClient(t *testing.T) {
t.Parallel()
v := NewTestVaultServer(t)
ctx := context.Background()
tests := []struct {
name string
wantErr bool
clientConfig *clientConfig
}{
{
name: "nil-config",
wantErr: true,
},
{
name: "empty-addr",
clientConfig: &clientConfig{
Token: TokenSecret(v.RootToken),
},
wantErr: true,
},
{
name: "empty-token",
clientConfig: &clientConfig{
Addr: v.Addr,
},
wantErr: true,
},
{
name: "valid-config",
clientConfig: &clientConfig{
Addr: v.Addr,
Token: TokenSecret(v.RootToken),
},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
client, err := newClient(ctx, tt.clientConfig)
if tt.wantErr {
require.Error(err)
assert.Nil(client)
return
}
require.NoError(err)
assert.NotNil(client)
})
}
}
func TestClient_RenewToken(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert := assert.New(t)
v := NewTestVaultServer(t)
_, token := v.CreateToken(t)
secretLookup := v.LookupToken(t, token)
// need to sleep so the expiration times will be different
time.Sleep(100 * time.Millisecond)
client := v.ClientUsingToken(t, token)
renewedToken, err := client.renewToken(ctx)
require.NoError(t, err)
assert.NotNil(renewedToken)
renewedLookup := v.LookupToken(t, token)
t1, t2 := tokenExpirationTime(t, secretLookup), tokenExpirationTime(t, renewedLookup)
assert.False(t1.Equal(t2))
assert.True(t2.After(t1))
}
func tokenExpirationTime(t *testing.T, s *vault.Secret) time.Time {
t.Helper()
require := require.New(t)
require.NotNil(s)
d1 := s.Data
require.NotNil(d1)
exp := d1["expire_time"]
require.NotEmpty(exp)
et, err := time.Parse(time.RFC3339, exp.(string))
require.NoError(err)
return et
}
func TestClient_LookupToken(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert, require := assert.New(t), require.New(t)
v := NewTestVaultServer(t)
_, token := v.CreateToken(t)
secretLookup := v.LookupToken(t, token)
client := v.ClientUsingToken(t, token)
tokenLookup, err := client.lookupToken(ctx)
assert.NoError(err)
require.NotNil(tokenLookup)
t1, t2 := tokenExpirationTime(t, secretLookup), tokenExpirationTime(t, tokenLookup)
assert.True(t1.Equal(t2))
}
func TestClient_RevokeToken(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert, require := assert.New(t), require.New(t)
v := NewTestVaultServer(t)
_, token := v.CreateToken(t)
client := v.ClientUsingToken(t, token)
tokenLookup, err := client.lookupToken(ctx)
assert.NoError(err)
assert.NotNil(tokenLookup)
require.NoError(client.revokeToken(ctx))
// An attempt to lookup should now fail with a 403
tokenLookup, err = client.lookupToken(ctx)
require.Error(err)
assert.Nil(tokenLookup)
var respErr *vault.ResponseError
ok := errors.As(err, &respErr)
require.True(ok)
assert.Equal(http.StatusForbidden, respErr.StatusCode)
}
func TestClient_Get(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert := assert.New(t)
v := NewTestVaultServer(t, WithDockerNetwork(true))
v.MountDatabase(t)
client := v.client(t)
cred, err := client.get(ctx, path.Join("database", "creds", "opened"))
assert.NoError(err)
assert.NotNil(cred)
}
func TestClient_Post(t *testing.T) {
t.Parallel()
ctx := context.Background()
v := NewTestVaultServer(t, WithTestVaultTLS(TestServerTLS))
v.MountPKI(t)
client := v.client(t)
credPath := path.Join("pki", "issue", "boundary")
t.Run("post-body", func(t *testing.T) {
assert := assert.New(t)
credData := []byte(`{"common_name":"boundary.com"}`)
cred, err := client.post(ctx, credPath, credData)
assert.NoError(err)
assert.NotNil(cred)
})
t.Run("nil-body", func(t *testing.T) {
assert := assert.New(t)
cred, err := client.post(ctx, credPath, nil)
assert.Error(err)
assert.Contains(err.Error(), "common_name field is required")
assert.Nil(cred)
})
}
func TestClient_RenewLease(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert, require := assert.New(t), require.New(t)
v := NewTestVaultServer(t, WithDockerNetwork(true))
v.MountDatabase(t)
_, token := v.CreateToken(t, WithPolicies([]string{"boundary-controller", "database"}))
client := v.ClientUsingToken(t, token)
// Create secret
cred, err := client.get(ctx, path.Join("database", "creds", "opened"))
assert.NoError(err)
require.NotNil(cred)
leaseLookup := v.LookupLease(t, cred.LeaseID)
require.NotNil(leaseLookup)
require.NotNil(leaseLookup.Data)
// Verify lease has not been renewed
assert.Empty(leaseLookup.Data["last_renewal"])
renewedLease, err := client.renewLease(ctx, cred.LeaseID, time.Hour)
assert.NoError(err)
require.NotNil(renewedLease)
assert.Equal(cred.LeaseID, renewedLease.LeaseID)
leaseLookup = v.LookupLease(t, cred.LeaseID)
require.NotNil(leaseLookup)
require.NotNil(leaseLookup.Data)
// Verify lease been renewed
assert.NotEmpty(leaseLookup.Data["last_renewal"])
}
func TestClient_capabilities(t *testing.T) {
t.Parallel()
ctx := context.Background()
v := NewTestVaultServer(t)
tests := []struct {
name string
polices []string
require pathCapabilities
wantMissing pathCapabilities
}{
{
polices: []string{"default"},
require: requiredCapabilities,
wantMissing: pathCapabilities{"sys/leases/revoke": updateCapability},
},
{
polices: []string{"default", "boundary-controller"},
require: requiredCapabilities,
},
{
polices: []string{"boundary-controller"},
require: requiredCapabilities,
},
}
for _, tt := range tests {
tt := tt
if tt.name == "" {
tt.name = fmt.Sprintf("%v", tt.polices)
}
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
_, token := v.CreateToken(t, WithPolicies(tt.polices))
client := v.ClientUsingToken(t, token)
have, err := client.capabilities(ctx, tt.require.paths())
assert.NoError(err)
got := have.missing(tt.require)
assert.Equalf(tt.wantMissing, got, "pathCapabilities: want: {%s} got: {%s}", tt.wantMissing, got)
})
}
}
func TestClient_revokeLease(t *testing.T) {
t.Parallel()
ctx := context.Background()
assert, require := assert.New(t), require.New(t)
v := NewTestVaultServer(t, WithDockerNetwork(true), WithTestVaultTLS(TestClientTLS))
testDatabase := v.MountDatabase(t)
_, token := v.CreateToken(t, WithPolicies([]string{"boundary-controller", "database"}))
client := v.ClientUsingToken(t, token)
cred, err := client.get(ctx, path.Join("database", "creds", "opened"))
assert.NoError(err)
require.NotNil(cred)
// verify the database credentials work
assert.NoError(testDatabase.ValidateCredential(t, cred))
// revoke the database credentials
assert.NoError(client.revokeLease(ctx, cred.LeaseID))
// verify the database credentials no longer work
assert.Error(testDatabase.ValidateCredential(t, cred))
}