Add support for SSH private key passphrases (#2331)
This adds support for encrypted SSH keys via passphrase. Co-authored-by: Louis Ruch <louisruch@gmail.com> Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com>
This commit is contained in:
45 files changed
+1921
-382
No files matched your search
@@ -126,9 +126,11 @@ type UsernamePassword interface {
|
||||
Password() Password
|
||||
}
|
||||
|
||||
// SshPrivateKey is a credential containing a username and a SSH private key.
|
||||
// SshPrivateKey is a credential containing a username an SSH private key and
|
||||
// an optional private key passphrase.
|
||||
type SshPrivateKey interface {
|
||||
Credential
|
||||
Username() string
|
||||
PrivateKey() PrivateKey
|
||||
PrivateKeyPassphrase() []byte
|
||||
}
|
||||
@@ -2,9 +2,10 @@ package static
|
||||
|
||||
// These constants are the field names used in the static related field masks.
|
||||
const (
|
||||
nameField = "Name"
|
||||
descriptionField = "Description"
|
||||
usernameField = "Username"
|
||||
passwordField = "Password"
|
||||
privateKeyField = "PrivateKey"
|
||||
nameField = "Name"
|
||||
descriptionField = "Description"
|
||||
usernameField = "Username"
|
||||
passwordField = "Password"
|
||||
privateKeyField = "PrivateKey"
|
||||
PrivateKeyPassphraseField = "PrivateKeyPassphrase"
|
||||
)
|
||||
@@ -14,10 +14,11 @@ type Option func(*options)
|
||||
|
||||
// options = how options are represented
|
||||
type options struct {
|
||||
withName string
|
||||
withDescription string
|
||||
withLimit int
|
||||
withPublicId string
|
||||
withName string
|
||||
withDescription string
|
||||
withLimit int
|
||||
withPublicId string
|
||||
withPrivateKeyPassphrase []byte
|
||||
}
|
||||
|
||||
func getDefaultOptions() options {
|
||||
@@ -53,3 +54,10 @@ func WithPublicId(name string) Option {
|
||||
o.withPublicId = name
|
||||
}
|
||||
}
|
||||
|
||||
// WithPrivateKeyPassphrase provides an optional SSH private key passphrase to use.
|
||||
func WithPrivateKeyPassphrase(with []byte) Option {
|
||||
return func(o *options) {
|
||||
o.withPrivateKeyPassphrase = with
|
||||
}
|
||||
}
|
||||
@@ -26,4 +26,11 @@ func Test_GetOpts(t *testing.T) {
|
||||
testOpts.withLimit = 5
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
t.Run("WithPrivateKeyPassphrase", func(t *testing.T) {
|
||||
opts := getOpts(WithPrivateKeyPassphrase([]byte("my-pass")))
|
||||
testOpts := getDefaultOptions()
|
||||
assert.NotEqual(t, opts, testOpts)
|
||||
testOpts.withPrivateKeyPassphrase = []byte("my-pass")
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
}
|
||||
@@ -106,8 +106,10 @@ func (r *Repository) CreateUsernamePasswordCredential(
|
||||
// assigned by this method. c must contain a valid StoreId.
|
||||
//
|
||||
// The private key is encrypted and a HmacSha256 of the private key is
|
||||
// calculated. Only the PrivateKeyHmac is returned, the plain-text and encrypted
|
||||
// private key is not returned.
|
||||
// calculated. If a passphrase is supplied, it is also encrypted and an HmacSha256
|
||||
// of passphrase is calculated. Only the PrivateKeyHmac (and
|
||||
// PrivateKeyPassphraseHmac) is returned, the plain-text and encrypted private
|
||||
// key and passphrase are not returned.
|
||||
//
|
||||
// Both c.Name and c.Description are optional. If c.Name is set, it must be
|
||||
// unique within c.ScopeId. Both c.CreateTime and c.UpdateTime are ignored.
|
||||
@@ -183,6 +185,10 @@ func (r *Repository) CreateSshPrivateKeyCredential(
|
||||
newCred.PrivateKeyEncrypted = nil
|
||||
newCred.PrivateKey = nil
|
||||
|
||||
// Clear passphrase fields, only PrivateKeyPassphraseHmac should be returned if it exists
|
||||
newCred.PrivateKeyPassphraseEncrypted = nil
|
||||
newCred.PrivateKeyPassphrase = nil
|
||||
|
||||
return newCred, nil
|
||||
}
|
||||
|
||||
@@ -224,6 +230,9 @@ func (r *Repository) LookupCredential(ctx context.Context, publicId string, _ ..
|
||||
// Clear private key fields, only privateKeyHmac should be returned
|
||||
spkCred.PrivateKeyEncrypted = nil
|
||||
spkCred.PrivateKey = nil
|
||||
// Clear passphrase fields, only PrivateKeyPassphraseHmac should be returned if it exists
|
||||
spkCred.PrivateKeyPassphraseEncrypted = nil
|
||||
spkCred.PrivateKeyPassphrase = nil
|
||||
cred = spkCred
|
||||
}
|
||||
|
||||
@@ -349,9 +358,9 @@ func (r *Repository) UpdateUsernamePasswordCredential(ctx context.Context,
|
||||
// new SshPrivateKeyCredential containing the updated values and a count of the
|
||||
// number of records updated. c is not changed.
|
||||
//
|
||||
// c must contain a valid PublicId. Only Name, Description, Username and
|
||||
// PrivateKey can be changed. If c.Name is set to a non-empty string, it must be
|
||||
// unique within c.ScopeId.
|
||||
// c must contain a valid PublicId. Only Name, Description, Username,
|
||||
// PrivateKey and PrivateKeyPassphrase can be changed. If c.Name is set to a non-empty string, it
|
||||
// must be unique within c.ScopeId.
|
||||
//
|
||||
// An attribute of c will be set to NULL in the database if the attribute in c
|
||||
// is the zero value and it is included in fieldMaskPaths.
|
||||
@@ -389,16 +398,18 @@ func (r *Repository) UpdateSshPrivateKeyCredential(ctx context.Context,
|
||||
case strings.EqualFold(descriptionField, f):
|
||||
case strings.EqualFold(usernameField, f):
|
||||
case strings.EqualFold(privateKeyField, f):
|
||||
case strings.EqualFold(PrivateKeyPassphraseField, f):
|
||||
default:
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, f)
|
||||
}
|
||||
}
|
||||
dbMask, nullFields := dbcommon.BuildUpdatePaths(
|
||||
map[string]interface{}{
|
||||
nameField: c.Name,
|
||||
descriptionField: c.Description,
|
||||
usernameField: c.Username,
|
||||
privateKeyField: c.PrivateKey,
|
||||
nameField: c.Name,
|
||||
descriptionField: c.Description,
|
||||
usernameField: c.Username,
|
||||
privateKeyField: c.PrivateKey,
|
||||
PrivateKeyPassphraseField: c.PrivateKeyPassphrase,
|
||||
},
|
||||
fieldMaskPaths,
|
||||
nil,
|
||||
@@ -407,19 +418,30 @@ func (r *Repository) UpdateSshPrivateKeyCredential(ctx context.Context,
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.EmptyFieldMask, op, "missing field mask")
|
||||
}
|
||||
|
||||
var performedEncryption bool
|
||||
for _, f := range fieldMaskPaths {
|
||||
if strings.EqualFold(privateKeyField, f) {
|
||||
// Private key has been updated, re-encrypt and recalculate hmac
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
if err := c.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
if strings.EqualFold(privateKeyField, f) || strings.EqualFold(PrivateKeyPassphraseField, f) {
|
||||
if !performedEncryption {
|
||||
// We don't need to encrypt twice so keep track
|
||||
performedEncryption = true
|
||||
|
||||
databaseWrapper, err := r.kms.GetWrapper(ctx, scopeId, kms.KeyPurposeDatabase)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
|
||||
}
|
||||
if err := c.encrypt(ctx, databaseWrapper); err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
|
||||
// Set PrivateKeyHmac and PrivateKeyEncrypted masks for update.
|
||||
dbMask = append(dbMask, "PrivateKeyHmac", "PrivateKeyEncrypted")
|
||||
if strings.EqualFold(privateKeyField, f) {
|
||||
// Set PrivateKeyHmac and PrivateKeyEncrypted masks for update.
|
||||
dbMask = append(dbMask, "PrivateKeyHmac", "PrivateKeyEncrypted")
|
||||
}
|
||||
if strings.EqualFold(PrivateKeyPassphraseField, f) {
|
||||
// Set PassphraseHmac and PassphraseEncrypted masks for update.
|
||||
dbMask = append(dbMask, "PrivateKeyPassphraseHmac", "PrivateKeyPassphraseEncrypted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -456,6 +478,10 @@ func (r *Repository) UpdateSshPrivateKeyCredential(ctx context.Context,
|
||||
returnedCredential.PrivateKeyEncrypted = nil
|
||||
returnedCredential.PrivateKey = nil
|
||||
|
||||
// Clear passphrase fields, only PrivateKeyPassphraseHmac should be returned if it exists
|
||||
returnedCredential.PrivateKeyPassphraseEncrypted = nil
|
||||
returnedCredential.PrivateKeyPassphrase = nil
|
||||
|
||||
return returnedCredential, rowsUpdated, nil
|
||||
}
|
||||
|
||||
@@ -499,6 +525,10 @@ func (r *Repository) ListCredentials(ctx context.Context, storeId string, opt ..
|
||||
// Clear private key fields, only PrivateKeyHmac should be returned
|
||||
c.PrivateKeyEncrypted = nil
|
||||
c.PrivateKey = nil
|
||||
|
||||
// Clear passphrase fields, only PrivateKeyPassphraseHmac should be returned if it exists
|
||||
c.PrivateKeyPassphraseEncrypted = nil
|
||||
c.PrivateKeyPassphrase = nil
|
||||
ret = append(ret, c)
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/crypto/ssh/testdata"
|
||||
)
|
||||
|
||||
func TestRepository_CreateUsernamePasswordCredential(t *testing.T) {
|
||||
@@ -276,6 +277,18 @@ func TestRepository_CreateSshPrivateKeyCredential(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-with-passphrase",
|
||||
scopeId: prj.PublicId,
|
||||
cred: &SshPrivateKeyCredential{
|
||||
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
|
||||
Username: "my-user",
|
||||
PrivateKey: []byte(TestSshPrivateKeyPem),
|
||||
StoreId: cs.PublicId,
|
||||
PrivateKeyPassphrase: []byte("passphrase"),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
@@ -298,8 +311,10 @@ func TestRepository_CreateSshPrivateKeyCredential(t *testing.T) {
|
||||
assert.Equal(tt.cred.Username, got.Username)
|
||||
assert.Nil(got.PrivateKey)
|
||||
assert.Nil(got.PrivateKeyEncrypted)
|
||||
assert.Nil(got.PrivateKeyPassphrase)
|
||||
assert.Nil(got.PrivateKeyPassphraseEncrypted)
|
||||
|
||||
// Validate password
|
||||
// Validate private key and passphrase
|
||||
lookupCred := allocSshPrivateKeyCredential()
|
||||
lookupCred.PublicId = got.PublicId
|
||||
require.NoError(rw.LookupById(ctx, lookupCred))
|
||||
@@ -318,6 +333,15 @@ func TestRepository_CreateSshPrivateKeyCredential(t *testing.T) {
|
||||
require.NoError(err)
|
||||
assert.Equal([]byte(hm), got.PrivateKeyHmac)
|
||||
|
||||
// Validate passphrase
|
||||
assert.Equal(tt.cred.PrivateKeyPassphrase, lookupCred.PrivateKeyPassphrase)
|
||||
if len(tt.cred.PrivateKeyPassphrase) > 0 {
|
||||
assert.NotEmpty(got.PrivateKeyPassphraseHmac)
|
||||
hm, err := crypto.HmacSha256(ctx, tt.cred.PrivateKeyPassphrase, databaseWrapper, []byte(tt.cred.StoreId), nil)
|
||||
require.NoError(err)
|
||||
assert.Equal([]byte(hm), got.PrivateKeyPassphraseHmac)
|
||||
}
|
||||
|
||||
// Validate oplog
|
||||
assert.NoError(db.TestVerifyOplog(t, rw, got.PublicId, db.WithOperation(oplog.OpType_OP_TYPE_CREATE), db.WithCreateNotBefore(10*time.Second)))
|
||||
})
|
||||
@@ -372,6 +396,8 @@ func TestRepository_LookupCredential(t *testing.T) {
|
||||
store := TestCredentialStore(t, conn, wrapper, prj.PublicId)
|
||||
upCred := TestUsernamePasswordCredential(t, conn, wrapper, "username", "password", store.PublicId, prj.PublicId)
|
||||
spkCred := TestSshPrivateKeyCredential(t, conn, wrapper, "username", TestSshPrivateKeyPem, store.PublicId, prj.PublicId)
|
||||
spkCredWithPass := TestSshPrivateKeyCredential(t, conn, wrapper, "username", string(testdata.PEMEncryptedKeys[0].PEMBytes),
|
||||
store.PublicId, prj.PublicId, WithPrivateKeyPassphrase([]byte(testdata.PEMEncryptedKeys[0].EncryptionKey)))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -389,6 +415,11 @@ func TestRepository_LookupCredential(t *testing.T) {
|
||||
id: spkCred.GetPublicId(),
|
||||
want: spkCred,
|
||||
},
|
||||
{
|
||||
name: "spk-valid-with-passphrase",
|
||||
id: spkCredWithPass.GetPublicId(),
|
||||
want: spkCredWithPass,
|
||||
},
|
||||
{
|
||||
name: "empty-public-id",
|
||||
id: "",
|
||||
@@ -433,6 +464,14 @@ func TestRepository_LookupCredential(t *testing.T) {
|
||||
assert.Empty(v.PrivateKey)
|
||||
assert.Empty(v.PrivateKeyEncrypted)
|
||||
assert.NotEmpty(v.PrivateKeyHmac)
|
||||
|
||||
want, ok := tt.want.(*SshPrivateKeyCredential)
|
||||
require.True(ok)
|
||||
assert.Empty(v.PrivateKeyPassphrase)
|
||||
assert.Empty(v.PrivateKeyPassphraseEncrypted)
|
||||
if len(want.PrivateKeyPassphrase) > 0 {
|
||||
assert.NotEmpty(v.PrivateKeyPassphraseHmac)
|
||||
}
|
||||
default:
|
||||
require.Fail("unknown type")
|
||||
}
|
||||
@@ -1175,6 +1214,13 @@ Hdtbe1Kk0rHxN0yIKqXNAAAACWplZmZAYXJjaAECAwQ=
|
||||
}
|
||||
}
|
||||
|
||||
changePrivateKeyPassphrase := func(d string) func(*SshPrivateKeyCredential) *SshPrivateKeyCredential {
|
||||
return func(c *SshPrivateKeyCredential) *SshPrivateKeyCredential {
|
||||
c.PrivateKeyPassphrase = []byte(d)
|
||||
return c
|
||||
}
|
||||
}
|
||||
|
||||
combine := func(fns ...func(cs *SshPrivateKeyCredential) *SshPrivateKeyCredential) func(*SshPrivateKeyCredential) *SshPrivateKeyCredential {
|
||||
return func(c *SshPrivateKeyCredential) *SshPrivateKeyCredential {
|
||||
for _, fn := range fns {
|
||||
@@ -1385,7 +1431,7 @@ Hdtbe1Kk0rHxN0yIKqXNAAAACWplZmZAYXJjaAECAwQ=
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-private-keyu",
|
||||
name: "change-private-key",
|
||||
orig: &SshPrivateKeyCredential{
|
||||
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
|
||||
Username: "user",
|
||||
@@ -1402,6 +1448,26 @@ Hdtbe1Kk0rHxN0yIKqXNAAAACWplZmZAYXJjaAECAwQ=
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-private-key-and-passphrase",
|
||||
orig: &SshPrivateKeyCredential{
|
||||
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
|
||||
Username: "user",
|
||||
PrivateKey: []byte(TestSshPrivateKeyPem),
|
||||
PrivateKeyPassphrase: []byte("foobar"),
|
||||
},
|
||||
},
|
||||
chgFn: combine(changePrivateKey(testSecondarySshPrivateKeyPem), changePrivateKeyPassphrase("barfoo")),
|
||||
masks: []string{"PrivateKeyPassphrase"},
|
||||
want: &SshPrivateKeyCredential{
|
||||
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
|
||||
Username: "user",
|
||||
PrivateKey: []byte(testSecondarySshPrivateKeyPem),
|
||||
PrivateKeyPassphrase: []byte("barfoo"),
|
||||
},
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-username-and-private-key",
|
||||
orig: &SshPrivateKeyCredential{
|
||||
@@ -1620,3 +1686,76 @@ Hdtbe1Kk0rHxN0yIKqXNAAAACWplZmZAYXJjaAECAwQ=
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSshPrivateKeyConstraints(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrapper := db.TestWrapper(t)
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
ctx := context.Background()
|
||||
kkms := kms.TestKms(t, conn, wrapper)
|
||||
repo, err := NewRepository(ctx, rw, rw, kkms)
|
||||
assert.NoError(err)
|
||||
require.NotNil(repo)
|
||||
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
store := TestCredentialStore(t, conn, wrapper, prj.GetPublicId())
|
||||
|
||||
// Base case: should work fine
|
||||
cred, err := NewSshPrivateKeyCredential(
|
||||
ctx,
|
||||
store.PublicId,
|
||||
"foobar",
|
||||
credential.PrivateKey(testdata.PEMEncryptedKeys[0].PEMBytes),
|
||||
WithPrivateKeyPassphrase([]byte(testdata.PEMEncryptedKeys[0].EncryptionKey)))
|
||||
require.NoError(err)
|
||||
cred.PublicId, err = credential.NewSshPrivateKeyCredentialId(ctx)
|
||||
require.NoError(err)
|
||||
databaseWrapper, err := kkms.GetWrapper(ctx, prj.PublicId, kms.KeyPurposeDatabase)
|
||||
require.NoError(cred.encrypt(ctx, databaseWrapper))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
nilEncrypted bool
|
||||
nilHmac bool
|
||||
}{
|
||||
{
|
||||
name: "valid",
|
||||
},
|
||||
{
|
||||
name: "nil-encrypted",
|
||||
nilEncrypted: true,
|
||||
},
|
||||
{
|
||||
name: "nil-hmac",
|
||||
nilHmac: true,
|
||||
},
|
||||
{
|
||||
name: "valid-both-nil",
|
||||
nilEncrypted: true,
|
||||
nilHmac: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := cred.clone()
|
||||
if tt.nilEncrypted {
|
||||
c.PrivateKeyPassphraseEncrypted = nil
|
||||
}
|
||||
if tt.nilHmac {
|
||||
c.PrivateKeyPassphraseHmac = nil
|
||||
}
|
||||
c.PublicId, err = credential.NewSshPrivateKeyCredentialId(ctx)
|
||||
require.NoError(err)
|
||||
err := rw.Create(ctx, c)
|
||||
switch {
|
||||
case !tt.nilEncrypted && !tt.nilHmac, tt.nilEncrypted && tt.nilHmac:
|
||||
assert.NoError(err)
|
||||
default:
|
||||
assert.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,9 @@ func TestRepository_Retrieve(t *testing.T) {
|
||||
upCred2 := TestUsernamePasswordCredential(t, conn, wrapper, "different user", "better password", staticStore.GetPublicId(), prj.GetPublicId())
|
||||
spkCred1 := TestSshPrivateKeyCredential(t, conn, wrapper, "final user", string(testdata.PEMBytes["ed25519"]), staticStore.GetPublicId(), prj.GetPublicId())
|
||||
spkCred2 := TestSshPrivateKeyCredential(t, conn, wrapper, "last user", string(testdata.PEMBytes["rsa-openssh-format"]), staticStore.GetPublicId(), prj.GetPublicId())
|
||||
spkCredWithPass := TestSshPrivateKeyCredential(t, conn, wrapper, "another last user",
|
||||
string(testdata.PEMEncryptedKeys[0].PEMBytes), staticStore.GetPublicId(), prj.GetPublicId(),
|
||||
WithPrivateKeyPassphrase([]byte(testdata.PEMEncryptedKeys[0].EncryptionKey)))
|
||||
|
||||
type args struct {
|
||||
credIds []string
|
||||
@@ -95,20 +98,20 @@ func TestRepository_Retrieve(t *testing.T) {
|
||||
name: "valid-multiple-ssh-pk-creds",
|
||||
args: args{
|
||||
scopeId: prj.GetPublicId(),
|
||||
credIds: []string{spkCred1.GetPublicId(), spkCred2.GetPublicId()},
|
||||
credIds: []string{spkCred1.GetPublicId(), spkCred2.GetPublicId(), spkCredWithPass.GetPublicId()},
|
||||
},
|
||||
wantCreds: []credential.Static{
|
||||
spkCred1, spkCred2,
|
||||
spkCred1, spkCred2, spkCredWithPass,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-mixed-creds",
|
||||
args: args{
|
||||
scopeId: prj.GetPublicId(),
|
||||
credIds: []string{upCred1.GetPublicId(), spkCred1.GetPublicId(), spkCred2.GetPublicId(), upCred2.GetPublicId()},
|
||||
credIds: []string{upCred1.GetPublicId(), spkCred1.GetPublicId(), spkCredWithPass.GetPublicId(), spkCred2.GetPublicId(), upCred2.GetPublicId()},
|
||||
},
|
||||
wantCreds: []credential.Static{
|
||||
upCred1, spkCred1, spkCred2, upCred2,
|
||||
upCred1, spkCred1, spkCred2, upCred2, spkCredWithPass,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -130,6 +133,7 @@ func TestRepository_Retrieve(t *testing.T) {
|
||||
UsernamePasswordCredential{}, store.UsernamePasswordCredential{},
|
||||
SshPrivateKeyCredential{}, store.SshPrivateKeyCredential{}),
|
||||
cmpopts.IgnoreTypes(×tamp.Timestamp{}),
|
||||
cmpopts.IgnoreFields(SshPrivateKeyCredential{}, "PassphraseUnneeded"),
|
||||
cmpopts.SortSlices(func(x, y credential.Static) bool {
|
||||
return x.GetPublicId() < y.GetPublicId()
|
||||
}),
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/libs/crypto"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
|
||||
"github.com/hashicorp/go-kms-wrapping/v2/extras/structwrapping"
|
||||
"golang.org/x/crypto/ssh"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
@@ -20,7 +19,8 @@ var _ credential.Static = (*SshPrivateKeyCredential)(nil)
|
||||
// It is owned by a credential store.
|
||||
type SshPrivateKeyCredential struct {
|
||||
*store.SshPrivateKeyCredential
|
||||
tableName string `gorm:"-"`
|
||||
tableName string `gorm:"-"`
|
||||
PassphraseUnneeded bool `gorm:"-"`
|
||||
}
|
||||
|
||||
// NewSshPrivateKeyCredential creates a new in memory static Credential containing a
|
||||
@@ -35,8 +35,14 @@ func NewSshPrivateKeyCredential(
|
||||
) (*SshPrivateKeyCredential, error) {
|
||||
const op = "static.NewSshPrivateKeyCredential"
|
||||
|
||||
opts := getOpts(opt...)
|
||||
if len(privateKey) != 0 {
|
||||
_, err := ssh.ParsePrivateKey(privateKey)
|
||||
var err error
|
||||
if len(opts.withPrivateKeyPassphrase) == 0 {
|
||||
_, err = ssh.ParsePrivateKey(privateKey)
|
||||
} else {
|
||||
_, err = ssh.ParsePrivateKeyWithPassphrase(privateKey, opts.withPrivateKeyPassphrase)
|
||||
}
|
||||
switch {
|
||||
case err == nil:
|
||||
case err.Error() == (&ssh.PassphraseMissingError{}).Error():
|
||||
@@ -46,16 +52,27 @@ func NewSshPrivateKeyCredential(
|
||||
}
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
l := &SshPrivateKeyCredential{
|
||||
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
|
||||
StoreId: storeId,
|
||||
Name: opts.withName,
|
||||
Description: opts.withDescription,
|
||||
Username: username,
|
||||
PrivateKey: privateKey,
|
||||
StoreId: storeId,
|
||||
Name: opts.withName,
|
||||
Description: opts.withDescription,
|
||||
Username: username,
|
||||
PrivateKey: privateKey,
|
||||
PrivateKeyPassphrase: opts.withPrivateKeyPassphrase,
|
||||
},
|
||||
}
|
||||
|
||||
// If a private key was given and no passphrase was given and everything is
|
||||
// okay, we can nil out any passphrase we have, so set this as a hint
|
||||
if len(privateKey) != 0 && len(opts.withPrivateKeyPassphrase) == 0 {
|
||||
l.PassphraseUnneeded = true
|
||||
// These shouldn't be set, but safety
|
||||
l.PrivateKeyPassphrase = nil
|
||||
l.PrivateKeyPassphraseEncrypted = nil
|
||||
l.PrivateKeyPassphraseHmac = nil
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
@@ -99,28 +116,77 @@ func (c *SshPrivateKeyCredential) oplog(op oplog.OpType) oplog.Metadata {
|
||||
|
||||
func (c *SshPrivateKeyCredential) encrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
const op = "static.(SshPrivateKeyCredential).encrypt"
|
||||
|
||||
if len(c.PrivateKey) == 0 {
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "no private key defined")
|
||||
}
|
||||
if err := structwrapping.WrapStruct(ctx, cipher, c.SshPrivateKeyCredential, nil); err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
|
||||
}
|
||||
|
||||
keyId, err := cipher.KeyId(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt), errors.WithMsg("error reading cipher key id"))
|
||||
}
|
||||
c.KeyId = keyId
|
||||
|
||||
// Encrypt private key
|
||||
blobInfo, err := cipher.Encrypt(ctx, c.PrivateKey)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
|
||||
}
|
||||
protoBytes, err := proto.Marshal(blobInfo)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encode))
|
||||
}
|
||||
c.PrivateKeyEncrypted = protoBytes
|
||||
if err := c.hmacPrivateKey(ctx, cipher); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
if len(c.PrivateKeyPassphrase) > 0 {
|
||||
// Encrypt passphrase
|
||||
blobInfo, err := cipher.Encrypt(ctx, c.PrivateKeyPassphrase)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encrypt))
|
||||
}
|
||||
protoBytes, err := proto.Marshal(blobInfo)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Encode))
|
||||
}
|
||||
c.PrivateKeyPassphraseEncrypted = protoBytes
|
||||
if err := c.hmacPrivateKeyPassphrase(ctx, cipher); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCredential) decrypt(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
const op = "static.(SshPrivateKeyCredential).decrypt"
|
||||
if err := structwrapping.UnwrapStruct(ctx, cipher, c.SshPrivateKeyCredential, nil); err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
|
||||
|
||||
if len(c.PrivateKeyEncrypted) > 0 {
|
||||
dec := new(wrapping.BlobInfo)
|
||||
if err := proto.Unmarshal(c.PrivateKeyEncrypted, dec); err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decode))
|
||||
}
|
||||
pt, err := cipher.Decrypt(ctx, dec)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
|
||||
}
|
||||
c.PrivateKey = pt
|
||||
}
|
||||
|
||||
if len(c.PrivateKeyPassphraseEncrypted) > 0 {
|
||||
dec := new(wrapping.BlobInfo)
|
||||
if err := proto.Unmarshal(c.PrivateKeyPassphraseEncrypted, dec); err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decode))
|
||||
}
|
||||
pt, err := cipher.Decrypt(ctx, dec)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.Decrypt))
|
||||
}
|
||||
c.PrivateKeyPassphrase = pt
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -136,3 +202,16 @@ func (c *SshPrivateKeyCredential) hmacPrivateKey(ctx context.Context, cipher wra
|
||||
c.PrivateKeyHmac = []byte(hm)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *SshPrivateKeyCredential) hmacPrivateKeyPassphrase(ctx context.Context, cipher wrapping.Wrapper) error {
|
||||
const op = "static.(SshPrivateKeyCredential).hmacPrivateKeyPassphrase"
|
||||
if cipher == nil {
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "missing cipher")
|
||||
}
|
||||
hm, err := crypto.HmacSha256(ctx, c.PrivateKeyPassphrase, cipher, []byte(c.StoreId), nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
c.PrivateKeyPassphraseHmac = []byte(hm)
|
||||
return nil
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/libs/crypto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/crypto/ssh/testdata"
|
||||
"google.golang.org/protobuf/testing/protocmp"
|
||||
)
|
||||
|
||||
@@ -24,6 +25,8 @@ func TestSshPrivateKeyCredential_New(t *testing.T) {
|
||||
rw := db.New(conn)
|
||||
|
||||
privKey := []byte(TestSshPrivateKeyPem)
|
||||
ePrivKey := testdata.PEMEncryptedKeys[0].PEMBytes
|
||||
ePrivKeyPass := testdata.PEMEncryptedKeys[0].EncryptionKey
|
||||
|
||||
_, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
||||
cs := TestCredentialStore(t, conn, wrapper, prj.PublicId)
|
||||
@@ -129,6 +132,33 @@ func TestSshPrivateKeyCredential_New(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid-passphrase",
|
||||
args: args{
|
||||
username: "test-user",
|
||||
sshPrivateKey: privKey,
|
||||
storeId: cs.PublicId,
|
||||
options: []Option{WithPrivateKeyPassphrase([]byte(ePrivKeyPass))},
|
||||
},
|
||||
wantAllocError: true,
|
||||
},
|
||||
{
|
||||
name: "valid-with-passphrase",
|
||||
args: args{
|
||||
username: "test-user",
|
||||
sshPrivateKey: ePrivKey,
|
||||
storeId: cs.PublicId,
|
||||
options: []Option{WithPrivateKeyPassphrase([]byte(ePrivKeyPass))},
|
||||
},
|
||||
want: &SshPrivateKeyCredential{
|
||||
SshPrivateKeyCredential: &store.SshPrivateKeyCredential{
|
||||
Username: "test-user",
|
||||
PrivateKey: ePrivKey,
|
||||
StoreId: cs.PublicId,
|
||||
PrivateKeyPassphrase: []byte(ePrivKeyPass),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -184,12 +214,20 @@ func TestSshPrivateKeyCredential_New(t *testing.T) {
|
||||
// KeyId is allocated via kms no need to validate in this test
|
||||
tt.want.KeyId = got2.KeyId
|
||||
got2.PrivateKeyEncrypted = nil
|
||||
got2.PrivateKeyPassphraseEncrypted = nil
|
||||
|
||||
// encrypt also calculates the hmac, validate it is correct
|
||||
hm, err := crypto.HmacSha256(ctx, got.PrivateKey, databaseWrapper, []byte(got.StoreId), nil)
|
||||
require.NoError(err)
|
||||
tt.want.PrivateKeyHmac = []byte(hm)
|
||||
|
||||
if len(got.PrivateKeyPassphrase) > 0 {
|
||||
// encrypt also calculates the hmac, validate it is correct
|
||||
hm, err = crypto.HmacSha256(ctx, got.PrivateKeyPassphrase, databaseWrapper, []byte(got.StoreId), nil)
|
||||
require.NoError(err)
|
||||
tt.want.PrivateKeyPassphraseHmac = []byte(hm)
|
||||
}
|
||||
|
||||
assert.Empty(cmp.Diff(tt.want, got2.clone(), protocmp.Transform()))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -347,6 +347,19 @@ type SshPrivateKeyCredential struct {
|
||||
// It must be set.
|
||||
// @inject_tag: `gorm:"not_null"`
|
||||
KeyId string `protobuf:"bytes,12,opt,name=key_id,json=keyId,proto3" json:"key_id,omitempty" gorm:"not_null"`
|
||||
// private_key_passphrase is the plain-text of the passphrase of the SSH private
|
||||
// key associated with the credential. We are not storing this plain-text
|
||||
// private key passphrase in the database.
|
||||
// @inject_tag: `gorm:"-"`
|
||||
PrivateKeyPassphrase []byte `protobuf:"bytes,13,opt,name=private_key_passphrase,json=privateKeyPassphrase,proto3" json:"private_key_passphrase,omitempty" gorm:"-"`
|
||||
// private_key_passphrase_encrypted is the ciphertext of the private key passphrase. It is stored in
|
||||
// the database.
|
||||
// @inject_tag: `gorm:"column:private_key_passphrase_encrypted;not_null"`
|
||||
PrivateKeyPassphraseEncrypted []byte `protobuf:"bytes,14,opt,name=private_key_passphrase_encrypted,json=privateKeyPassphraseEncrypted,proto3" json:"private_key_passphrase_encrypted,omitempty" gorm:"column:private_key_passphrase_encrypted;not_null"`
|
||||
// private_key_passphrase_hmac is a sha256-hmac of the unencrypted private key passphrase. It is recalculated
|
||||
// everytime the private key passphrase is updated.
|
||||
// @inject_tag: `gorm:"not_null"`
|
||||
PrivateKeyPassphraseHmac []byte `protobuf:"bytes,15,opt,name=private_key_passphrase_hmac,json=privateKeyPassphraseHmac,proto3" json:"private_key_passphrase_hmac,omitempty" gorm:"not_null"`
|
||||
}
|
||||
|
||||
func (x *SshPrivateKeyCredential) Reset() {
|
||||
@@ -465,6 +478,27 @@ func (x *SshPrivateKeyCredential) GetKeyId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SshPrivateKeyCredential) GetPrivateKeyPassphrase() []byte {
|
||||
if x != nil {
|
||||
return x.PrivateKeyPassphrase
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *SshPrivateKeyCredential) GetPrivateKeyPassphraseEncrypted() []byte {
|
||||
if x != nil {
|
||||
return x.PrivateKeyPassphraseEncrypted
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (x *SshPrivateKeyCredential) GetPrivateKeyPassphraseHmac() []byte {
|
||||
if x != nil {
|
||||
return x.PrivateKeyPassphraseHmac
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var File_controller_storage_credential_static_store_v1_static_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_controller_storage_credential_static_store_v1_static_proto_rawDesc = []byte{
|
||||
@@ -543,7 +577,7 @@ var file_controller_storage_credential_static_store_v1_static_proto_rawDesc = []
|
||||
0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x5f, 0x68, 0x6d, 0x61, 0x63, 0x52, 0x0c, 0x70, 0x61, 0x73,
|
||||
0x73, 0x77, 0x6f, 0x72, 0x64, 0x48, 0x6d, 0x61, 0x63, 0x12, 0x15, 0x0a, 0x06, 0x6b, 0x65, 0x79,
|
||||
0x5f, 0x69, 0x64, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6b, 0x65, 0x79, 0x49, 0x64,
|
||||
0x22, 0xa1, 0x05, 0x0a, 0x17, 0x53, 0x73, 0x68, 0x50, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b,
|
||||
0x22, 0xe7, 0x07, 0x0a, 0x17, 0x53, 0x73, 0x68, 0x50, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b,
|
||||
0x65, 0x79, 0x43, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x12, 0x1b, 0x0a, 0x09,
|
||||
0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
|
||||
0x08, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x49, 0x64, 0x12, 0x4b, 0x0a, 0x0b, 0x63, 0x72, 0x65,
|
||||
@@ -585,12 +619,32 @@ var file_controller_storage_credential_static_store_v1_static_proto_rawDesc = []
|
||||
0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x5f, 0x68, 0x6d, 0x61, 0x63, 0x52, 0x0e, 0x70,
|
||||
0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x48, 0x6d, 0x61, 0x63, 0x12, 0x15, 0x0a,
|
||||
0x06, 0x6b, 0x65, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6b,
|
||||
0x65, 0x79, 0x49, 0x64, 0x42, 0x46, 0x5a, 0x44, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63,
|
||||
0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f, 0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75,
|
||||
0x6e, 0x64, 0x61, 0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x63,
|
||||
0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63,
|
||||
0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x33,
|
||||
0x65, 0x79, 0x49, 0x64, 0x12, 0x73, 0x0a, 0x16, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f,
|
||||
0x6b, 0x65, 0x79, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x18, 0x0d,
|
||||
0x20, 0x01, 0x28, 0x0c, 0x42, 0x3d, 0xc2, 0xdd, 0x29, 0x39, 0x0a, 0x14, 0x50, 0x72, 0x69, 0x76,
|
||||
0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65,
|
||||
0x12, 0x21, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x69,
|
||||
0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72,
|
||||
0x61, 0x73, 0x65, 0x52, 0x14, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50,
|
||||
0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x12, 0x47, 0x0a, 0x20, 0x70, 0x72, 0x69,
|
||||
0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72,
|
||||
0x61, 0x73, 0x65, 0x5f, 0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x18, 0x0e, 0x20,
|
||||
0x01, 0x28, 0x0c, 0x52, 0x1d, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50,
|
||||
0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x45, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74,
|
||||
0x65, 0x64, 0x12, 0x85, 0x01, 0x0a, 0x1b, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b,
|
||||
0x65, 0x79, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x5f, 0x68, 0x6d,
|
||||
0x61, 0x63, 0x18, 0x0f, 0x20, 0x01, 0x28, 0x0c, 0x42, 0x46, 0xc2, 0xdd, 0x29, 0x42, 0x0a, 0x18,
|
||||
0x50, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50, 0x61, 0x73, 0x73, 0x70, 0x68,
|
||||
0x72, 0x61, 0x73, 0x65, 0x48, 0x6d, 0x61, 0x63, 0x12, 0x26, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62,
|
||||
0x75, 0x74, 0x65, 0x73, 0x2e, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79,
|
||||
0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x5f, 0x68, 0x6d, 0x61, 0x63,
|
||||
0x52, 0x18, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50, 0x61, 0x73, 0x73,
|
||||
0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x48, 0x6d, 0x61, 0x63, 0x42, 0x46, 0x5a, 0x44, 0x67, 0x69,
|
||||
0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f,
|
||||
0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65,
|
||||
0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x63, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f,
|
||||
0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f,
|
||||
0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
@@ -13,34 +13,36 @@ type (
|
||||
// extractFunc attempts to extract the username and private key
|
||||
// from sd using the provided attribute names, using a known
|
||||
// Vault data response format.
|
||||
extractFunc func(sd data, usernameAttr, privateKeyAttr string) (string, credential.PrivateKey)
|
||||
extractFunc func(sd data, usernameAttr, privateKeyAttr, passphraseAttr string) (string, credential.PrivateKey, []byte)
|
||||
)
|
||||
|
||||
// Extract attempts to extract the values of the username and private key
|
||||
// stored within the provided data using the given attribute names.
|
||||
// Extract attempts to extract the values of the username, private key and optional
|
||||
// passphrase stored within the provided data using the given attribute names.
|
||||
//
|
||||
// Extract does not return partial results, i.e. if one of the attributes
|
||||
// were extracted but not the other ("", nil) will be returned.
|
||||
func Extract(d data, usernameAttr, privateKeyAttr string) (string, credential.PrivateKey) {
|
||||
func Extract(d data, usernameAttr, privateKeyAttr, passphraseAttr string) (string, credential.PrivateKey, []byte) {
|
||||
for _, f := range []extractFunc{
|
||||
defaultExtract,
|
||||
kv2Extract,
|
||||
} {
|
||||
username, privateKey := f(d, usernameAttr, privateKeyAttr)
|
||||
username, privateKey, passphrase := f(d, usernameAttr, privateKeyAttr, passphraseAttr)
|
||||
if username != "" && privateKey != nil {
|
||||
// got valid username and privateKey from secret
|
||||
return username, privateKey
|
||||
return username, privateKey, passphrase
|
||||
}
|
||||
}
|
||||
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
// defaultExtract looks for the usernameAttr and privateKeyAttr in the data map
|
||||
func defaultExtract(sd data, usernameAttr, privateKeyAttr string) (username string, privateKey credential.PrivateKey) {
|
||||
func defaultExtract(sd data, usernameAttr, privateKeyAttr, passphraseAttr string) (
|
||||
username string, privateKey credential.PrivateKey, passphrase []byte,
|
||||
) {
|
||||
if sd == nil {
|
||||
// nothing to do return early
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
var u any
|
||||
@@ -49,9 +51,8 @@ func defaultExtract(sd data, usernameAttr, privateKeyAttr string) (username stri
|
||||
var err error
|
||||
u, err = pointerstructure.Get(sd, usernameAttr)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
default:
|
||||
u = sd[usernameAttr]
|
||||
}
|
||||
@@ -59,24 +60,37 @@ func defaultExtract(sd data, usernameAttr, privateKeyAttr string) (username stri
|
||||
username = u
|
||||
}
|
||||
|
||||
var p any
|
||||
var pk any
|
||||
switch {
|
||||
case strings.HasPrefix(privateKeyAttr, "/"):
|
||||
var err error
|
||||
p, err = pointerstructure.Get(sd, privateKeyAttr)
|
||||
pk, err = pointerstructure.Get(sd, privateKeyAttr)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
default:
|
||||
p = sd[privateKeyAttr]
|
||||
pk = sd[privateKeyAttr]
|
||||
}
|
||||
|
||||
if p, ok := p.(string); ok {
|
||||
if p, ok := pk.(string); ok {
|
||||
privateKey = credential.PrivateKey(p)
|
||||
}
|
||||
|
||||
return username, privateKey
|
||||
var pass any
|
||||
switch {
|
||||
case strings.HasPrefix(passphraseAttr, "/"):
|
||||
var err error
|
||||
pass, err = pointerstructure.Get(sd, passphraseAttr)
|
||||
if err != nil {
|
||||
return "", nil, nil
|
||||
}
|
||||
default:
|
||||
pass = sd[passphraseAttr]
|
||||
}
|
||||
if p, ok := pass.(string); ok {
|
||||
passphrase = []byte(p)
|
||||
}
|
||||
|
||||
return username, privateKey, passphrase
|
||||
}
|
||||
|
||||
// kv2Extract looks for the the usernameAttr and privateKeyAttr in the embedded
|
||||
@@ -89,10 +103,12 @@ func defaultExtract(sd data, usernameAttr, privateKeyAttr string) (username stri
|
||||
// }
|
||||
// If the format does not match, it returns ("", ""). See:
|
||||
// https://www.vaultproject.io/api/secret/kv/kv-v2#sample-response-1
|
||||
func kv2Extract(sd data, usernameAttr, privateKeyAttr string) (username string, privateKey credential.PrivateKey) {
|
||||
func kv2Extract(sd data, usernameAttr, privateKeyAttr, passphraseAttr string) (
|
||||
username string, privateKey credential.PrivateKey, passphrase []byte,
|
||||
) {
|
||||
if sd == nil {
|
||||
// nothing to do return early
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
var data, metadata map[string]any
|
||||
@@ -102,22 +118,22 @@ func kv2Extract(sd data, usernameAttr, privateKeyAttr string) (username string,
|
||||
var ok bool
|
||||
if data, ok = v.(map[string]any); !ok {
|
||||
// data field should be of type map[string]any in KV-v2
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
case "metadata":
|
||||
var ok bool
|
||||
if metadata, ok = v.(map[string]any); !ok {
|
||||
// metadata field should be of type map[string]any in KV-v2
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
default:
|
||||
// secretData contains a non valid KV-v2 top level field
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
}
|
||||
if data == nil || metadata == nil {
|
||||
// missing required KV-v2 field
|
||||
return "", nil
|
||||
return "", nil, nil
|
||||
}
|
||||
|
||||
if u, ok := data[usernameAttr]; ok {
|
||||
@@ -130,6 +146,11 @@ func kv2Extract(sd data, usernameAttr, privateKeyAttr string) (username string,
|
||||
privateKey = credential.PrivateKey(p)
|
||||
}
|
||||
}
|
||||
if p, ok := data[passphraseAttr]; ok {
|
||||
if p, ok := p.(string); ok {
|
||||
passphrase = []byte(p)
|
||||
}
|
||||
}
|
||||
|
||||
return username, privateKey
|
||||
return username, privateKey, passphrase
|
||||
}
|
||||
@@ -15,13 +15,15 @@ func TestExtract(t *testing.T) {
|
||||
rsaKey := testdata.PEMBytes["rsa"]
|
||||
|
||||
type args struct {
|
||||
s data
|
||||
uAttr string
|
||||
pAttr string
|
||||
s data
|
||||
uAttr string
|
||||
pkAttr string
|
||||
pAttr string
|
||||
}
|
||||
type sshpk struct {
|
||||
user string
|
||||
privateKey credential.PrivateKey
|
||||
passphrase []byte
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -30,20 +32,20 @@ func TestExtract(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "nil-input",
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "no-input",
|
||||
given: args{},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "no-secret",
|
||||
given: args{
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "no-match-username-secret",
|
||||
@@ -52,10 +54,10 @@ func TestExtract(t *testing.T) {
|
||||
"username-wrong": "user",
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "no-match-private-key-secret",
|
||||
@@ -64,10 +66,10 @@ func TestExtract(t *testing.T) {
|
||||
"username": "user",
|
||||
"private_key-wrong": string(edKey),
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "valid-default",
|
||||
@@ -76,22 +78,24 @@ func TestExtract(t *testing.T) {
|
||||
"username": "user",
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "user", privateKey: edKey},
|
||||
want: sshpk{user: "user", privateKey: edKey, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "valid-default-private-key-string",
|
||||
name: "valid-default-with-passphrase",
|
||||
given: args{
|
||||
s: data{
|
||||
"username": "user",
|
||||
"private_key": string(edKey),
|
||||
"passphrase": "my-pass",
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
pAttr: "passphrase",
|
||||
},
|
||||
want: sshpk{user: "user", privateKey: edKey},
|
||||
want: sshpk{user: "user", privateKey: edKey, passphrase: []byte("my-pass")},
|
||||
},
|
||||
{
|
||||
name: "no-match-username-secret-kv2",
|
||||
@@ -103,10 +107,10 @@ func TestExtract(t *testing.T) {
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "no-match-private-key-secret-kv2",
|
||||
@@ -118,10 +122,10 @@ func TestExtract(t *testing.T) {
|
||||
"private_key-wrong": string(edKey),
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "valid-kv2",
|
||||
@@ -133,25 +137,27 @@ func TestExtract(t *testing.T) {
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "user", privateKey: edKey},
|
||||
want: sshpk{user: "user", privateKey: edKey, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "valid-kv2-private-key-string",
|
||||
name: "valid-kv2-with-passphrase",
|
||||
given: args{
|
||||
s: data{
|
||||
"metadata": map[string]interface{}{},
|
||||
"data": map[string]interface{}{
|
||||
"username": "user",
|
||||
"private_key": string(edKey),
|
||||
"passphrase": "my-pass",
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
pAttr: "passphrase",
|
||||
},
|
||||
want: sshpk{user: "user", privateKey: edKey},
|
||||
want: sshpk{user: "user", privateKey: edKey, passphrase: []byte("my-pass")},
|
||||
},
|
||||
{
|
||||
name: "no-metadata-kv2",
|
||||
@@ -162,10 +168,10 @@ func TestExtract(t *testing.T) {
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "invalid-metadata-kv2",
|
||||
@@ -177,10 +183,10 @@ func TestExtract(t *testing.T) {
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "invalid-field-kv2",
|
||||
@@ -193,10 +199,10 @@ func TestExtract(t *testing.T) {
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "", privateKey: nil},
|
||||
want: sshpk{user: "", privateKey: nil, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "valid-order-default-first",
|
||||
@@ -210,10 +216,10 @@ func TestExtract(t *testing.T) {
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "default-user", privateKey: rsaKey},
|
||||
want: sshpk{user: "default-user", privateKey: rsaKey, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "default-user-json-pointer-pk",
|
||||
@@ -224,10 +230,10 @@ func TestExtract(t *testing.T) {
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
},
|
||||
uAttr: "username",
|
||||
pAttr: "/testing/private_key",
|
||||
uAttr: "username",
|
||||
pkAttr: "/testing/private_key",
|
||||
},
|
||||
want: sshpk{user: "default-user", privateKey: edKey},
|
||||
want: sshpk{user: "default-user", privateKey: edKey, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "default-pk-json-pointer-user",
|
||||
@@ -238,13 +244,13 @@ func TestExtract(t *testing.T) {
|
||||
"special": "not-so-special",
|
||||
},
|
||||
},
|
||||
uAttr: "/testing/special",
|
||||
pAttr: "private_key",
|
||||
uAttr: "/testing/special",
|
||||
pkAttr: "private_key",
|
||||
},
|
||||
want: sshpk{user: "not-so-special", privateKey: edKey},
|
||||
want: sshpk{user: "not-so-special", privateKey: edKey, passphrase: nil},
|
||||
},
|
||||
{
|
||||
name: "both-json-pointer",
|
||||
name: "all-json-pointer",
|
||||
given: args{
|
||||
s: data{
|
||||
"first-path": map[string]interface{}{
|
||||
@@ -255,20 +261,25 @@ func TestExtract(t *testing.T) {
|
||||
"testing": map[string]interface{}{
|
||||
"private_key": string(edKey),
|
||||
},
|
||||
"hidden": map[string]interface{}{
|
||||
"pass": "my-pass",
|
||||
},
|
||||
},
|
||||
uAttr: "/first-path/deeper-path/my-special-user",
|
||||
pAttr: "/testing/private_key",
|
||||
uAttr: "/first-path/deeper-path/my-special-user",
|
||||
pkAttr: "/testing/private_key",
|
||||
pAttr: "/hidden/pass",
|
||||
},
|
||||
want: sshpk{user: "you-found-me", privateKey: edKey},
|
||||
want: sshpk{user: "you-found-me", privateKey: edKey, passphrase: []byte("my-pass")},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
user, privateKey := Extract(tt.given.s, tt.given.uAttr, tt.given.pAttr)
|
||||
user, privateKey, passphrase := Extract(tt.given.s, tt.given.uAttr, tt.given.pkAttr, tt.given.pAttr)
|
||||
assert.Equal(tt.want.user, user)
|
||||
assert.Equal(tt.want.privateKey, privateKey)
|
||||
assert.Equal(tt.want.passphrase, passphrase)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -110,14 +110,15 @@ type SshPrivateKeyOverride struct {
|
||||
var _ MappingOverride = (*SshPrivateKeyOverride)(nil)
|
||||
|
||||
// NewSshPrivateKeyOverride creates a new in memory SshPrivateKeyOverride.
|
||||
// WithOverrideUsernameAttribute and WithOverridePasswordAttribute are the
|
||||
// only valid options. All other options are ignored.
|
||||
// WithOverrideUsernameAttribute, WithOverridePrivateKeyAttribute and WithOverridePrivateKeyPassphraseAttribute
|
||||
// are the only valid options. All other options are ignored.
|
||||
func NewSshPrivateKeyOverride(opt ...Option) *SshPrivateKeyOverride {
|
||||
opts := getOpts(opt...)
|
||||
o := &SshPrivateKeyOverride{
|
||||
SshPrivateKeyOverride: &store.SshPrivateKeyOverride{
|
||||
UsernameAttribute: sanitize.String(opts.withOverrideUsernameAttribute),
|
||||
PrivateKeyAttribute: sanitize.String(opts.withOverridePrivateKeyAttribute),
|
||||
UsernameAttribute: sanitize.String(opts.withOverrideUsernameAttribute),
|
||||
PrivateKeyAttribute: sanitize.String(opts.withOverridePrivateKeyAttribute),
|
||||
PrivateKeyPassphraseAttribute: sanitize.String(opts.withOverridePrivateKeyPassphraseAttribute),
|
||||
},
|
||||
}
|
||||
return o
|
||||
@@ -147,6 +148,9 @@ func (o *SshPrivateKeyOverride) sanitize() {
|
||||
if sentinel.Is(o.PrivateKeyAttribute) {
|
||||
o.PrivateKeyAttribute = ""
|
||||
}
|
||||
if sentinel.Is(o.PrivateKeyPassphraseAttribute) {
|
||||
o.PrivateKeyPassphraseAttribute = ""
|
||||
}
|
||||
}
|
||||
|
||||
// TableName returns the table name.
|
||||
|
||||
@@ -29,10 +29,11 @@ type options struct {
|
||||
withRequestBody []byte
|
||||
withCredentialType credential.Type
|
||||
|
||||
withOverrideUsernameAttribute string
|
||||
withOverridePasswordAttribute string
|
||||
withOverridePrivateKeyAttribute string
|
||||
withMappingOverride MappingOverride
|
||||
withOverrideUsernameAttribute string
|
||||
withOverridePasswordAttribute string
|
||||
withOverridePrivateKeyAttribute string
|
||||
withOverridePrivateKeyPassphraseAttribute string
|
||||
withMappingOverride MappingOverride
|
||||
}
|
||||
|
||||
func getDefaultOptions() options {
|
||||
@@ -158,6 +159,14 @@ func WithOverridePrivateKeyAttribute(s string) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// WithOverridePrivateKeyPassphraseAttribute provides the name of an attribute in the
|
||||
// Data field of a Vault api.Secret that maps to a passphrase value.
|
||||
func WithOverridePrivateKeyPassphraseAttribute(s string) Option {
|
||||
return func(o *options) {
|
||||
o.withOverridePrivateKeyPassphraseAttribute = s
|
||||
}
|
||||
}
|
||||
|
||||
// WithMappingOverride provides an optional mapping override to use for
|
||||
// mapping the Data fields of a Vault api.Secret to a credential.
|
||||
func WithMappingOverride(m MappingOverride) Option {
|
||||
|
||||
@@ -114,6 +114,13 @@ func Test_GetOpts(t *testing.T) {
|
||||
testOpts.withOverridePrivateKeyAttribute = "test"
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
t.Run("WithOverridePrivateKeyPassphraseAttribute", func(t *testing.T) {
|
||||
opts := getOpts(WithOverridePrivateKeyPassphraseAttribute("test"))
|
||||
testOpts := getDefaultOptions()
|
||||
assert.NotEqual(t, opts, testOpts)
|
||||
testOpts.withOverridePrivateKeyPassphraseAttribute = "test"
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
t.Run("WithMappingOverride", func(t *testing.T) {
|
||||
opts := getOpts(WithMappingOverride(unknownMapper(1)))
|
||||
testOpts := getDefaultOptions()
|
||||
|
||||
@@ -91,10 +91,12 @@ type sshPrivateKeyCred struct {
|
||||
*baseCred
|
||||
username string
|
||||
privateKey credential.PrivateKey
|
||||
passphrase []byte
|
||||
}
|
||||
|
||||
func (c *sshPrivateKeyCred) Username() string { return c.username }
|
||||
func (c *sshPrivateKeyCred) PrivateKey() credential.PrivateKey { return c.privateKey }
|
||||
func (c *sshPrivateKeyCred) PrivateKeyPassphrase() []byte { return c.passphrase }
|
||||
|
||||
func baseToSshPriKey(ctx context.Context, bc *baseCred) (*sshPrivateKeyCred, error) {
|
||||
switch {
|
||||
@@ -106,14 +108,17 @@ func baseToSshPriKey(ctx context.Context, bc *baseCred) (*sshPrivateKeyCred, err
|
||||
return nil, errors.E(ctx, errors.WithCode(errors.InvalidParameter), errors.WithMsg("invalid credential type"))
|
||||
}
|
||||
|
||||
uAttr, pAttr := bc.lib.UsernameAttribute, bc.lib.PrivateKeyAttribute
|
||||
uAttr, pkAttr, pAttr := bc.lib.UsernameAttribute, bc.lib.PrivateKeyAttribute, bc.lib.PrivateKeyPassphraseAttribute
|
||||
if uAttr == "" {
|
||||
uAttr = "username"
|
||||
}
|
||||
if pAttr == "" {
|
||||
pAttr = "private_key"
|
||||
if pkAttr == "" {
|
||||
pkAttr = "private_key"
|
||||
}
|
||||
username, pk := sshprivatekey.Extract(bc.secretData, uAttr, pAttr)
|
||||
if pAttr == "" {
|
||||
pAttr = "private_key_passphrase"
|
||||
}
|
||||
username, pk, pass := sshprivatekey.Extract(bc.secretData, uAttr, pkAttr, pAttr)
|
||||
if username == "" || pk == nil {
|
||||
return nil, errors.E(ctx, errors.WithCode(errors.VaultInvalidCredentialMapping))
|
||||
}
|
||||
@@ -122,6 +127,7 @@ func baseToSshPriKey(ctx context.Context, bc *baseCred) (*sshPrivateKeyCred, err
|
||||
baseCred: bc,
|
||||
username: username,
|
||||
privateKey: pk,
|
||||
passphrase: pass,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -130,72 +136,74 @@ var _ credential.Library = (*privateLibrary)(nil)
|
||||
// A privateLibrary contains all the values needed to connect to Vault and
|
||||
// retrieve credentials.
|
||||
type privateLibrary struct {
|
||||
PublicId string `gorm:"primary_key"`
|
||||
StoreId string
|
||||
CredType string `gorm:"column:credential_type"`
|
||||
UsernameAttribute string
|
||||
PasswordAttribute string
|
||||
PrivateKeyAttribute string
|
||||
Name string
|
||||
Description string
|
||||
CreateTime *timestamp.Timestamp
|
||||
UpdateTime *timestamp.Timestamp
|
||||
Version uint32
|
||||
ScopeId string
|
||||
VaultPath string
|
||||
HttpMethod string
|
||||
HttpRequestBody []byte
|
||||
VaultAddress string
|
||||
Namespace string
|
||||
CaCert []byte
|
||||
TlsServerName string
|
||||
TlsSkipVerify bool
|
||||
WorkerFilter string
|
||||
TokenHmac []byte
|
||||
Token TokenSecret
|
||||
CtToken []byte
|
||||
TokenKeyId string
|
||||
ClientCert []byte
|
||||
ClientKey KeySecret
|
||||
CtClientKey []byte
|
||||
ClientKeyId string
|
||||
Purpose credential.Purpose `gorm:"-"`
|
||||
PublicId string `gorm:"primary_key"`
|
||||
StoreId string
|
||||
CredType string `gorm:"column:credential_type"`
|
||||
UsernameAttribute string
|
||||
PasswordAttribute string
|
||||
PrivateKeyAttribute string
|
||||
PrivateKeyPassphraseAttribute string
|
||||
Name string
|
||||
Description string
|
||||
CreateTime *timestamp.Timestamp
|
||||
UpdateTime *timestamp.Timestamp
|
||||
Version uint32
|
||||
ScopeId string
|
||||
VaultPath string
|
||||
HttpMethod string
|
||||
HttpRequestBody []byte
|
||||
VaultAddress string
|
||||
Namespace string
|
||||
CaCert []byte
|
||||
TlsServerName string
|
||||
TlsSkipVerify bool
|
||||
WorkerFilter string
|
||||
TokenHmac []byte
|
||||
Token TokenSecret
|
||||
CtToken []byte
|
||||
TokenKeyId string
|
||||
ClientCert []byte
|
||||
ClientKey KeySecret
|
||||
CtClientKey []byte
|
||||
ClientKeyId string
|
||||
Purpose credential.Purpose `gorm:"-"`
|
||||
}
|
||||
|
||||
func (pl *privateLibrary) clone() *privateLibrary {
|
||||
// The 'append(a[:0:0], a...)' comes from
|
||||
// https://github.com/go101/go101/wiki/How-to-perfectly-clone-a-slice%3F
|
||||
return &privateLibrary{
|
||||
PublicId: pl.PublicId,
|
||||
StoreId: pl.StoreId,
|
||||
CredType: pl.CredType,
|
||||
UsernameAttribute: pl.UsernameAttribute,
|
||||
PasswordAttribute: pl.PasswordAttribute,
|
||||
PrivateKeyAttribute: pl.PrivateKeyAttribute,
|
||||
Name: pl.Name,
|
||||
Description: pl.Description,
|
||||
CreateTime: proto.Clone(pl.CreateTime).(*timestamp.Timestamp),
|
||||
UpdateTime: proto.Clone(pl.UpdateTime).(*timestamp.Timestamp),
|
||||
Version: pl.Version,
|
||||
ScopeId: pl.ScopeId,
|
||||
VaultPath: pl.VaultPath,
|
||||
HttpMethod: pl.HttpMethod,
|
||||
HttpRequestBody: append(pl.HttpRequestBody[:0:0], pl.HttpRequestBody...),
|
||||
VaultAddress: pl.VaultAddress,
|
||||
Namespace: pl.Namespace,
|
||||
CaCert: append(pl.CaCert[:0:0], pl.CaCert...),
|
||||
TlsServerName: pl.TlsServerName,
|
||||
TlsSkipVerify: pl.TlsSkipVerify,
|
||||
WorkerFilter: pl.WorkerFilter,
|
||||
TokenHmac: append(pl.TokenHmac[:0:0], pl.TokenHmac...),
|
||||
Token: append(pl.Token[:0:0], pl.Token...),
|
||||
CtToken: append(pl.CtToken[:0:0], pl.CtToken...),
|
||||
TokenKeyId: pl.TokenKeyId,
|
||||
ClientCert: append(pl.ClientCert[:0:0], pl.ClientCert...),
|
||||
ClientKey: append(pl.ClientKey[:0:0], pl.ClientKey...),
|
||||
CtClientKey: append(pl.CtClientKey[:0:0], pl.CtClientKey...),
|
||||
ClientKeyId: pl.ClientKeyId,
|
||||
Purpose: pl.Purpose,
|
||||
PublicId: pl.PublicId,
|
||||
StoreId: pl.StoreId,
|
||||
CredType: pl.CredType,
|
||||
UsernameAttribute: pl.UsernameAttribute,
|
||||
PasswordAttribute: pl.PasswordAttribute,
|
||||
PrivateKeyAttribute: pl.PrivateKeyAttribute,
|
||||
PrivateKeyPassphraseAttribute: pl.PrivateKeyPassphraseAttribute,
|
||||
Name: pl.Name,
|
||||
Description: pl.Description,
|
||||
CreateTime: proto.Clone(pl.CreateTime).(*timestamp.Timestamp),
|
||||
UpdateTime: proto.Clone(pl.UpdateTime).(*timestamp.Timestamp),
|
||||
Version: pl.Version,
|
||||
ScopeId: pl.ScopeId,
|
||||
VaultPath: pl.VaultPath,
|
||||
HttpMethod: pl.HttpMethod,
|
||||
HttpRequestBody: append(pl.HttpRequestBody[:0:0], pl.HttpRequestBody...),
|
||||
VaultAddress: pl.VaultAddress,
|
||||
Namespace: pl.Namespace,
|
||||
CaCert: append(pl.CaCert[:0:0], pl.CaCert...),
|
||||
TlsServerName: pl.TlsServerName,
|
||||
TlsSkipVerify: pl.TlsSkipVerify,
|
||||
WorkerFilter: pl.WorkerFilter,
|
||||
TokenHmac: append(pl.TokenHmac[:0:0], pl.TokenHmac...),
|
||||
Token: append(pl.Token[:0:0], pl.Token...),
|
||||
CtToken: append(pl.CtToken[:0:0], pl.CtToken...),
|
||||
TokenKeyId: pl.TokenKeyId,
|
||||
ClientCert: append(pl.ClientCert[:0:0], pl.ClientCert...),
|
||||
ClientKey: append(pl.ClientKey[:0:0], pl.ClientKey...),
|
||||
CtClientKey: append(pl.CtClientKey[:0:0], pl.CtClientKey...),
|
||||
ClientKeyId: pl.ClientKeyId,
|
||||
Purpose: pl.Purpose,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -227,12 +227,30 @@ func TestRepository_getPrivateLibraries(t *testing.T) {
|
||||
req := credential.Request{SourceId: lib.GetPublicId(), Purpose: credential.BrokeredPurpose}
|
||||
requests = append(requests, req)
|
||||
}
|
||||
{
|
||||
opts := []Option{
|
||||
WithCredentialType(credential.SshPrivateKeyType),
|
||||
WithMappingOverride(NewSshPrivateKeyOverride(
|
||||
WithOverridePrivateKeyPassphraseAttribute("test-passphrase"),
|
||||
)),
|
||||
}
|
||||
libIn, err := NewCredentialLibrary(origStore.GetPublicId(), "/vault/path", opts...)
|
||||
assert.NoError(err)
|
||||
require.NotNil(libIn)
|
||||
lib, err := repo.CreateCredentialLibrary(ctx, prj.GetPublicId(), libIn)
|
||||
assert.NoError(err)
|
||||
require.NotNil(lib)
|
||||
libs[lib.GetPublicId()] = lib
|
||||
req := credential.Request{SourceId: lib.GetPublicId(), Purpose: credential.BrokeredPurpose}
|
||||
requests = append(requests, req)
|
||||
}
|
||||
{
|
||||
opts := []Option{
|
||||
WithCredentialType(credential.SshPrivateKeyType),
|
||||
WithMappingOverride(NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("test-username"),
|
||||
WithOverridePrivateKeyAttribute("test-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("test-passphrase"),
|
||||
)),
|
||||
}
|
||||
libIn, err := NewCredentialLibrary(origStore.GetPublicId(), "/vault/path", opts...)
|
||||
@@ -273,6 +291,7 @@ func TestRepository_getPrivateLibraries(t *testing.T) {
|
||||
case *SshPrivateKeyOverride:
|
||||
assert.Equal(w.UsernameAttribute, got.UsernameAttribute)
|
||||
assert.Equal(w.PrivateKeyAttribute, got.PrivateKeyAttribute)
|
||||
assert.Equal(w.PrivateKeyPassphraseAttribute, got.PrivateKeyPassphraseAttribute)
|
||||
default:
|
||||
assert.Fail("unknown mapping override type")
|
||||
}
|
||||
@@ -806,6 +825,24 @@ func TestBaseToSshPriKey(t *testing.T) {
|
||||
privateKey: credential.PrivateKey("my-pk"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-default-attributes-with-passphrase",
|
||||
given: &baseCred{
|
||||
lib: &privateLibrary{
|
||||
CredType: string(credential.SshPrivateKeyType),
|
||||
},
|
||||
secretData: map[string]interface{}{
|
||||
"username": "my-username",
|
||||
"private_key": "my-pk",
|
||||
"private_key_passphrase": "my-pass",
|
||||
},
|
||||
},
|
||||
want: &sshPrivateKeyCred{
|
||||
username: "my-username",
|
||||
privateKey: credential.PrivateKey("my-pk"),
|
||||
passphrase: []byte("my-pass"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-override-attributes",
|
||||
given: &baseCred{
|
||||
@@ -826,6 +863,30 @@ func TestBaseToSshPriKey(t *testing.T) {
|
||||
privateKey: credential.PrivateKey("override-pk"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-override-attributes-with-passphrase",
|
||||
given: &baseCred{
|
||||
lib: &privateLibrary{
|
||||
CredType: string(credential.SshPrivateKeyType),
|
||||
UsernameAttribute: "test-username",
|
||||
PrivateKeyAttribute: "test-pk",
|
||||
PrivateKeyPassphraseAttribute: "test-pass",
|
||||
},
|
||||
secretData: map[string]interface{}{
|
||||
"username": "default-username",
|
||||
"private_key": "default-pk",
|
||||
"passphrase": "default-pass",
|
||||
"test-username": "override-username",
|
||||
"test-pk": "override-pk",
|
||||
"test-pass": "override-pass",
|
||||
},
|
||||
},
|
||||
want: &sshPrivateKeyCred{
|
||||
username: "override-username",
|
||||
privateKey: credential.PrivateKey("override-pk"),
|
||||
passphrase: []byte("override-pass"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-default-username-override-pk",
|
||||
given: &baseCred{
|
||||
@@ -1018,6 +1079,27 @@ func TestBaseToSshPriKey(t *testing.T) {
|
||||
privateKey: credential.PrivateKey("default-pk"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-kv2-default-attributes-with-passphrase",
|
||||
given: &baseCred{
|
||||
lib: &privateLibrary{
|
||||
CredType: string(credential.SshPrivateKeyType),
|
||||
},
|
||||
secretData: map[string]interface{}{
|
||||
"metadata": map[string]interface{}{},
|
||||
"data": map[string]interface{}{
|
||||
"username": "default-username",
|
||||
"private_key": "default-pk",
|
||||
"private_key_passphrase": "default-pass",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &sshPrivateKeyCred{
|
||||
username: "default-username",
|
||||
privateKey: credential.PrivateKey("default-pk"),
|
||||
passphrase: []byte("default-pass"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-kv2-override-attributes",
|
||||
given: &baseCred{
|
||||
@@ -1041,6 +1123,33 @@ func TestBaseToSshPriKey(t *testing.T) {
|
||||
privateKey: credential.PrivateKey("override-pk"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-kv2-override-attributes-with-passphrase",
|
||||
given: &baseCred{
|
||||
lib: &privateLibrary{
|
||||
CredType: string(credential.SshPrivateKeyType),
|
||||
UsernameAttribute: "test-username",
|
||||
PrivateKeyAttribute: "test-pk",
|
||||
PrivateKeyPassphraseAttribute: "test-pass",
|
||||
},
|
||||
secretData: map[string]interface{}{
|
||||
"metadata": map[string]interface{}{},
|
||||
"data": map[string]interface{}{
|
||||
"username": "default-username",
|
||||
"private_key": "default-pk",
|
||||
"passphrase": "default-pass",
|
||||
"test-username": "override-username",
|
||||
"test-pk": "override-pk",
|
||||
"test-pass": "override-pass",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: &sshPrivateKeyCred{
|
||||
username: "override-username",
|
||||
privateKey: credential.PrivateKey("override-pk"),
|
||||
passphrase: []byte("override-pass"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-kv2-default-username-override-pk",
|
||||
given: &baseCred{
|
||||
|
||||
@@ -345,20 +345,21 @@ func (r *Repository) LookupCredentialLibrary(ctx context.Context, publicId strin
|
||||
// mapping overrides. It does not include encrypted data and is safe to
|
||||
// return external to boundary.
|
||||
type publicLibrary struct {
|
||||
PublicId string `gorm:"primary_key"`
|
||||
StoreId string
|
||||
Name string
|
||||
Description string
|
||||
CreateTime *timestamp.Timestamp
|
||||
UpdateTime *timestamp.Timestamp
|
||||
Version uint32
|
||||
VaultPath string
|
||||
HttpMethod string
|
||||
HttpRequestBody []byte
|
||||
CredentialType string
|
||||
UsernameAttribute string
|
||||
PasswordAttribute string
|
||||
PrivateKeyAttribute string
|
||||
PublicId string `gorm:"primary_key"`
|
||||
StoreId string
|
||||
Name string
|
||||
Description string
|
||||
CreateTime *timestamp.Timestamp
|
||||
UpdateTime *timestamp.Timestamp
|
||||
Version uint32
|
||||
VaultPath string
|
||||
HttpMethod string
|
||||
HttpRequestBody []byte
|
||||
CredentialType string
|
||||
UsernameAttribute string
|
||||
PasswordAttribute string
|
||||
PrivateKeyAttribute string
|
||||
PrivateKeyPassphraseAttribute string
|
||||
}
|
||||
|
||||
func allocPublicLibrary() *publicLibrary {
|
||||
@@ -390,11 +391,12 @@ func (pl *publicLibrary) toCredentialLibrary() *CredentialLibrary {
|
||||
cl.MappingOverride = up
|
||||
}
|
||||
case string(credential.SshPrivateKeyType):
|
||||
if pl.UsernameAttribute != "" || pl.PrivateKeyAttribute != "" {
|
||||
if pl.UsernameAttribute != "" || pl.PrivateKeyAttribute != "" || pl.PrivateKeyPassphraseAttribute != "" {
|
||||
pk := allocSshPrivateKeyOverride()
|
||||
pk.LibraryId = pl.PublicId
|
||||
pk.UsernameAttribute = pl.UsernameAttribute
|
||||
pk.PrivateKeyAttribute = pl.PrivateKeyAttribute
|
||||
pk.PrivateKeyPassphraseAttribute = pl.PrivateKeyPassphraseAttribute
|
||||
pk.sanitize()
|
||||
cl.MappingOverride = pk
|
||||
}
|
||||
|
||||
@@ -381,11 +381,37 @@ func TestRepository_CreateCredentialLibrary(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-ssh-private-keyt-credential-type-with-username-and-private-key-override",
|
||||
name: "valid-ssh-private-key-credential-type-with-passphrase-override",
|
||||
in: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverridePrivateKeyPassphraseAttribute("passtest"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
StoreId: cs.GetPublicId(),
|
||||
HttpMethod: "GET",
|
||||
VaultPath: "/some/path",
|
||||
CredentialType: string(credential.SshPrivateKeyType),
|
||||
},
|
||||
},
|
||||
want: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverridePrivateKeyPassphraseAttribute("passtest"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
StoreId: cs.GetPublicId(),
|
||||
HttpMethod: "GET",
|
||||
VaultPath: "/some/path",
|
||||
CredentialType: string(credential.SshPrivateKeyType),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-ssh-private-keyt-credential-type-with-all-override",
|
||||
in: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("utest"),
|
||||
WithOverridePrivateKeyAttribute("ptest"),
|
||||
WithOverridePrivateKeyAttribute("pktest"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("ptest"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
StoreId: cs.GetPublicId(),
|
||||
@@ -397,7 +423,8 @@ func TestRepository_CreateCredentialLibrary(t *testing.T) {
|
||||
want: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("utest"),
|
||||
WithOverridePrivateKeyAttribute("ptest"),
|
||||
WithOverridePrivateKeyAttribute("pktest"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("ptest"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
StoreId: cs.GetPublicId(),
|
||||
@@ -456,6 +483,7 @@ func TestRepository_CreateCredentialLibrary(t *testing.T) {
|
||||
require.True(ok)
|
||||
assert.Equal(w.UsernameAttribute, g.UsernameAttribute)
|
||||
assert.Equal(w.PrivateKeyAttribute, g.PrivateKeyAttribute)
|
||||
assert.Equal(w.PrivateKeyPassphraseAttribute, g.PrivateKeyPassphraseAttribute)
|
||||
|
||||
// verify it was persisted in the database
|
||||
override := allocSshPrivateKeyOverride()
|
||||
@@ -1302,11 +1330,10 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) {
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "ssh-private-key-attributes-change-username-and-private-key-attributes",
|
||||
name: "ssh-private-key-attributes-change-passphrase-attribute",
|
||||
orig: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("orig-username"),
|
||||
WithOverridePrivateKeyAttribute("orig-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("orig-passphrase"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
HttpMethod: "GET",
|
||||
@@ -1317,15 +1344,13 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) {
|
||||
},
|
||||
chgFn: changeMappingOverride(
|
||||
NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("changed-username"),
|
||||
WithOverridePrivateKeyAttribute("changed-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("changed-passphrase"),
|
||||
),
|
||||
),
|
||||
masks: []string{"MappingOverride"},
|
||||
want: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("changed-username"),
|
||||
WithOverridePrivateKeyAttribute("changed-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("changed-passphrase"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
HttpMethod: "GET",
|
||||
@@ -1337,7 +1362,45 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) {
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "no-mapping-override-change-username-and-private-key-attributes",
|
||||
name: "ssh-private-key-attributes-change-all-attributes",
|
||||
orig: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("orig-username"),
|
||||
WithOverridePrivateKeyAttribute("orig-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("orig-passphrase"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
HttpMethod: "GET",
|
||||
VaultPath: "/some/path",
|
||||
Name: "test-ssh-private-key-repo",
|
||||
CredentialType: string(credential.SshPrivateKeyType),
|
||||
},
|
||||
},
|
||||
chgFn: changeMappingOverride(
|
||||
NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("changed-username"),
|
||||
WithOverridePrivateKeyAttribute("changed-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("changed-passphrase"),
|
||||
),
|
||||
),
|
||||
masks: []string{"MappingOverride"},
|
||||
want: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("changed-username"),
|
||||
WithOverridePrivateKeyAttribute("changed-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("changed-passphrase"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
HttpMethod: "GET",
|
||||
VaultPath: "/some/path",
|
||||
Name: "test-ssh-private-key-repo",
|
||||
CredentialType: string(credential.SshPrivateKeyType),
|
||||
},
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "no-mapping-override-change-all-attributes",
|
||||
orig: &CredentialLibrary{
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
HttpMethod: "GET",
|
||||
@@ -1350,6 +1413,7 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) {
|
||||
NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("changed-username"),
|
||||
WithOverridePrivateKeyAttribute("changed-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("changed-passphrase"),
|
||||
),
|
||||
),
|
||||
masks: []string{"MappingOverride"},
|
||||
@@ -1357,6 +1421,7 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) {
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("changed-username"),
|
||||
WithOverridePrivateKeyAttribute("changed-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("changed-passphrase"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
HttpMethod: "GET",
|
||||
@@ -1373,6 +1438,7 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) {
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("orig-username"),
|
||||
WithOverridePrivateKeyAttribute("orig-private-key"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("orig-passphrase"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
HttpMethod: "GET",
|
||||
@@ -1424,7 +1490,7 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) {
|
||||
assert.Nil(got)
|
||||
return
|
||||
}
|
||||
assert.NoError(err)
|
||||
require.NoError(err)
|
||||
assert.Empty(tt.orig.PublicId)
|
||||
require.NotNil(got)
|
||||
assertPublicId(t, CredentialLibraryPrefix, got.GetPublicId())
|
||||
@@ -1466,6 +1532,7 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) {
|
||||
require.True(ok)
|
||||
assert.Equal(w.UsernameAttribute, g.UsernameAttribute)
|
||||
assert.Equal(w.PrivateKeyAttribute, g.PrivateKeyAttribute)
|
||||
assert.Equal(w.PrivateKeyPassphraseAttribute, g.PrivateKeyPassphraseAttribute)
|
||||
default:
|
||||
assert.Fail("Unknown mapping override")
|
||||
}
|
||||
@@ -1705,11 +1772,26 @@ func TestRepository_LookupCredentialLibrary(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-ssh-private-key-credential-type-with-username-and-private-key-override",
|
||||
name: "valid-ssh-private-key-credential-type-with-passphrase-override",
|
||||
in: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverridePrivateKeyPassphraseAttribute("ptest"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
StoreId: cs.GetPublicId(),
|
||||
HttpMethod: "GET",
|
||||
VaultPath: "/some/path",
|
||||
CredentialType: string(credential.SshPrivateKeyType),
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-ssh-private-key-credential-type-with-all-overrides",
|
||||
in: &CredentialLibrary{
|
||||
MappingOverride: NewSshPrivateKeyOverride(
|
||||
WithOverrideUsernameAttribute("utest"),
|
||||
WithOverridePrivateKeyAttribute("ptest"),
|
||||
WithOverridePrivateKeyAttribute("pktest"),
|
||||
WithOverridePrivateKeyPassphraseAttribute("ptest"),
|
||||
),
|
||||
CredentialLibrary: &store.CredentialLibrary{
|
||||
StoreId: cs.GetPublicId(),
|
||||
@@ -1756,6 +1838,7 @@ func TestRepository_LookupCredentialLibrary(t *testing.T) {
|
||||
require.True(ok)
|
||||
assert.Equal(w.UsernameAttribute, g.UsernameAttribute)
|
||||
assert.Equal(w.PrivateKeyAttribute, g.PrivateKeyAttribute)
|
||||
assert.Equal(w.PrivateKeyPassphraseAttribute, g.PrivateKeyPassphraseAttribute)
|
||||
default:
|
||||
assert.Fail("Unknown mapping override")
|
||||
}
|
||||
|
||||
@@ -904,6 +904,16 @@ type SshPrivateKeyOverride struct {
|
||||
//
|
||||
// @inject_tag: `gorm:"default:null"`
|
||||
PrivateKeyAttribute string `protobuf:"bytes,3,opt,name=private_key_attribute,json=privateKeyAttribute,proto3" json:"private_key_attribute,omitempty" gorm:"default:null"`
|
||||
// private_key_passphrase_attribute is the name of the attribute in the Data
|
||||
// field of a Vault api.Secret that maps to a private key passphrase. If set,
|
||||
// it overrides any default attribute names the system uses to find a
|
||||
// private_key_passphrase attribute.
|
||||
//
|
||||
// See
|
||||
// https://github.com/hashicorp/vault/blob/5e505ec039177e8212cbbab74ccb644c46e62e63/api/secret.go#L25
|
||||
//
|
||||
// @inject_tag: `gorm:"default:null"`
|
||||
PrivateKeyPassphraseAttribute string `protobuf:"bytes,4,opt,name=private_key_passphrase_attribute,json=privateKeyPassphraseAttribute,proto3" json:"private_key_passphrase_attribute,omitempty" gorm:"default:null"`
|
||||
}
|
||||
|
||||
func (x *SshPrivateKeyOverride) Reset() {
|
||||
@@ -959,6 +969,13 @@ func (x *SshPrivateKeyOverride) GetPrivateKeyAttribute() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *SshPrivateKeyOverride) GetPrivateKeyPassphraseAttribute() string {
|
||||
if x != nil {
|
||||
return x.PrivateKeyPassphraseAttribute
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_controller_storage_credential_vault_store_v1_vault_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_controller_storage_credential_vault_store_v1_vault_proto_rawDesc = []byte{
|
||||
@@ -1171,7 +1188,7 @@ var file_controller_storage_credential_vault_store_v1_vault_proto_rawDesc = []by
|
||||
0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x12, 0x2d, 0x0a, 0x12, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f,
|
||||
0x72, 0x64, 0x5f, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x18, 0x03, 0x20, 0x01,
|
||||
0x28, 0x09, 0x52, 0x11, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x41, 0x74, 0x74, 0x72,
|
||||
0x69, 0x62, 0x75, 0x74, 0x65, 0x22, 0x99, 0x01, 0x0a, 0x15, 0x53, 0x73, 0x68, 0x50, 0x72, 0x69,
|
||||
0x69, 0x62, 0x75, 0x74, 0x65, 0x22, 0xe2, 0x01, 0x0a, 0x15, 0x53, 0x73, 0x68, 0x50, 0x72, 0x69,
|
||||
0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x4f, 0x76, 0x65, 0x72, 0x72, 0x69, 0x64, 0x65, 0x12,
|
||||
0x1d, 0x0a, 0x0a, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x09, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x49, 0x64, 0x12, 0x2d,
|
||||
@@ -1181,11 +1198,16 @@ var file_controller_storage_credential_vault_store_v1_vault_proto_rawDesc = []by
|
||||
0x15, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79, 0x5f, 0x61, 0x74, 0x74,
|
||||
0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x13, 0x70, 0x72,
|
||||
0x69, 0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74,
|
||||
0x65, 0x42, 0x45, 0x5a, 0x43, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
|
||||
0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f, 0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61,
|
||||
0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x63, 0x72, 0x65, 0x64,
|
||||
0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f, 0x76, 0x61, 0x75, 0x6c, 0x74, 0x2f, 0x73, 0x74, 0x6f,
|
||||
0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x65, 0x12, 0x47, 0x0a, 0x20, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74, 0x65, 0x5f, 0x6b, 0x65, 0x79,
|
||||
0x5f, 0x70, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73, 0x65, 0x5f, 0x61, 0x74, 0x74, 0x72,
|
||||
0x69, 0x62, 0x75, 0x74, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x1d, 0x70, 0x72, 0x69,
|
||||
0x76, 0x61, 0x74, 0x65, 0x4b, 0x65, 0x79, 0x50, 0x61, 0x73, 0x73, 0x70, 0x68, 0x72, 0x61, 0x73,
|
||||
0x65, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x42, 0x45, 0x5a, 0x43, 0x67, 0x69,
|
||||
0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x69, 0x63, 0x6f,
|
||||
0x72, 0x70, 0x2f, 0x62, 0x6f, 0x75, 0x6e, 0x64, 0x61, 0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65,
|
||||
0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x63, 0x72, 0x65, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x2f,
|
||||
0x76, 0x61, 0x75, 0x6c, 0x74, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x3b, 0x73, 0x74, 0x6f, 0x72,
|
||||
0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
||||
Reference in new issue
Block a user