Skip to content

Commit

Permalink
Provide a keyId prefix as an option to transit wrappers (#187)
Browse files Browse the repository at this point in the history
* Provide a keyId prefix as an option to transit wrappers

* Basic unit test
  • Loading branch information
sgmiller authored Sep 18, 2023
1 parent 1c42881 commit 6adb2e4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/hashicorp/go-kms-wrapping/v2
go 1.20

require (
github.com/favadi/protoc-go-inject-tag v1.4.0
github.com/hashicorp/go-uuid v1.0.3
github.com/mr-tron/base58 v1.2.0
github.com/stretchr/testify v1.8.2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/favadi/protoc-go-inject-tag v1.4.0 h1:K3KXxbgRw5WT4f43LbglARGz/8jVsDOS7uMjG4oNvXY=
github.com/favadi/protoc-go-inject-tag v1.4.0/go.mod h1:AZ+PK+QDKUOLlBRG0rYiKkUX5Hw7+7GTFzlU99GFSbQ=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
Expand Down
13 changes: 13 additions & 0 deletions wrappers/transit/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func getOpts(opt ...wrapping.Option) (*options, error) {
if err != nil {
return nil, err
}
case "key_id_prefix":
opts.withKeyIdPrefix = v
case "token":
opts.withToken = v
}
Expand Down Expand Up @@ -111,6 +113,7 @@ type options struct {
withTlsServerName string
withTlsSkipVerify bool
withToken string
withKeyIdPrefix string

withLogger hclog.Logger
}
Expand Down Expand Up @@ -248,3 +251,13 @@ func WithLogger(with hclog.Logger) wrapping.Option {
})
}
}

// WithKeyIdPrefix specifies a prefix to prepend to the keyId (key version)
func WithKeyIdPrefix(with string) wrapping.Option {
return func() interface{} {
return OptionFunc(func(o *options) error {
o.withKeyIdPrefix = with
return nil
})
}
}
4 changes: 3 additions & 1 deletion wrappers/transit/transit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Wrapper struct {
logger hclog.Logger
client transitClientEncryptor
currentKeyId *atomic.Value
keyIdPrefix string
}

var _ wrapping.Wrapper = (*Wrapper)(nil)
Expand Down Expand Up @@ -46,6 +47,7 @@ func (s *Wrapper) SetConfig(_ context.Context, opt ...wrapping.Option) (*wrappin
return nil, err
}
s.client = client
s.keyIdPrefix = opts.withKeyIdPrefix

// Send a value to test the wrapper and to set the current key id
if _, err := s.Encrypt(context.Background(), []byte("a")); err != nil {
Expand Down Expand Up @@ -88,7 +90,7 @@ func (s *Wrapper) Encrypt(_ context.Context, plaintext []byte, _ ...wrapping.Opt
if len(splitKey) != 3 {
return nil, errors.New("invalid ciphertext returned")
}
keyId := splitKey[1]
keyId := s.keyIdPrefix + splitKey[1]
s.currentKeyId.Store(keyId)

ret := &wrapping.BlobInfo{
Expand Down
37 changes: 26 additions & 11 deletions wrappers/transit/transit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import (
"github.com/stretchr/testify/require"
)

const (
testWithMountPath = "transit/"
testWithAddress = "http://localhost:8200"
testWithKeyName = "example-key"
testWithDisableRenewal = "true"
testWithNamespace = "ns1/"
testWithToken = "vault-plaintext-root-token"

envVaultNamespace = "VAULT_NAMESPACE"
)

type testTransitClient struct {
keyID string
wrap wrapping.Wrapper
Expand Down Expand Up @@ -86,20 +97,23 @@ func TestTransitWrapper_Lifecycle(t *testing.T) {
if kid != keyId {
t.Fatalf("key id does not match: expected %s, got %s", keyId, kid)
}

// Test keyId prefix (can't use the option/SetConfig however )
s.keyIdPrefix = "test/"
_, err = s.Encrypt(context.Background(), input)
if err != nil {
t.Fatalf("err: %s", err.Error())
}
kid, err = s.KeyId(context.Background())
if err != nil {
t.Fatalf("err: %s", err.Error())
}
if kid != "test/"+keyId {
t.Fatalf("key id does not match: expected %s, got %s", keyId, kid)
}
}

func TestSetConfig(t *testing.T) {
const (
testWithMountPath = "transit/"
testWithAddress = "http://localhost:8200"
testWithKeyName = "example-key"
testWithDisableRenewal = "true"
testWithNamespace = "ns1/"
testWithToken = "vault-plaintext-root-token"

envVaultNamespace = "VAULT_NAMESPACE"
)

tests := []struct {
name string
opts []wrapping.Option
Expand Down Expand Up @@ -292,6 +306,7 @@ func TestSetConfig(t *testing.T) {
WithMountPath(testWithMountPath),
WithKeyName(testWithKeyName),
WithNamespace(testWithNamespace),
WithKeyIdPrefix("test/"),
},
},
}
Expand Down

0 comments on commit 6adb2e4

Please sign in to comment.