Skip to content

Commit c675dc0

Browse files
authored
test(e2e): Verify different credentials are brokered to target (hashicorp#3149)
* test(e2e): Verify json credentials are brokered back * test(e2e): Get expected json credentials from file * test(e2e): Check private key credentials are brokered to target * test(e2e): Regenerate test .pem file for every test
1 parent c1a61b9 commit c675dc0

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

testing/internal/e2e/tests/static/credential_store_test.go

+69-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@ package static_test
55

66
import (
77
"context"
8+
"crypto/rand"
9+
"crypto/rsa"
10+
"crypto/x509"
811
"encoding/json"
12+
"encoding/pem"
913
"fmt"
1014
"os"
15+
"strings"
1116
"testing"
1217
"time"
1318

@@ -21,6 +26,12 @@ import (
2126
"github.com/stretchr/testify/require"
2227
)
2328

29+
const (
30+
testCredentialsFile = "testdata/credential.json"
31+
testPemFile = "testdata/private-key.pem"
32+
testPassword = "password"
33+
)
34+
2435
// TestCliStaticCredentialStore validates various credential-store operations using the cli
2536
func TestCliStaticCredentialStore(t *testing.T) {
2637
e2e.MaybeSkipTest(t)
@@ -43,9 +54,19 @@ func TestCliStaticCredentialStore(t *testing.T) {
4354
boundary.AddHostToHostSetCli(t, ctx, newHostSetId, newHostId)
4455
newTargetId := boundary.CreateNewTargetCli(t, ctx, newProjectId, c.TargetPort)
4556
boundary.AddHostSourceToTargetCli(t, ctx, newTargetId, newHostSetId)
57+
58+
err = createPrivateKeyPemFile(testPemFile)
59+
require.NoError(t, err)
60+
t.Cleanup(func() {
61+
err := os.Remove(testPemFile)
62+
require.NoError(t, err)
63+
})
64+
65+
// Create static credentials
4666
newCredentialStoreId := boundary.CreateNewCredentialStoreStaticCli(t, ctx, newProjectId)
47-
boundary.CreateNewStaticCredentialPrivateKeyCli(t, ctx, newCredentialStoreId, c.TargetSshUser, c.TargetSshKeyPath)
48-
pwCredentialsId := boundary.CreateNewStaticCredentialPasswordCli(t, ctx, newCredentialStoreId, c.TargetSshUser, "password")
67+
privateKeyCredentialsId := boundary.CreateNewStaticCredentialPrivateKeyCli(t, ctx, newCredentialStoreId, c.TargetSshUser, testPemFile)
68+
pwCredentialsId := boundary.CreateNewStaticCredentialPasswordCli(t, ctx, newCredentialStoreId, c.TargetSshUser, testPassword)
69+
jsonCredentialsId := boundary.CreateNewStaticCredentialJsonCli(t, ctx, newCredentialStoreId, testCredentialsFile)
4970

5071
// Get credentials for target (expect empty)
5172
output := e2e.RunCommand(ctx, "boundary",
@@ -57,6 +78,9 @@ func TestCliStaticCredentialStore(t *testing.T) {
5778
require.NoError(t, err)
5879
require.True(t, newSessionAuthorizationResult.Item.Credentials == nil)
5980

81+
// Add credentials to target
82+
boundary.AddCredentialSourceToTargetCli(t, ctx, newTargetId, privateKeyCredentialsId)
83+
boundary.AddCredentialSourceToTargetCli(t, ctx, newTargetId, jsonCredentialsId)
6084
boundary.AddCredentialSourceToTargetCli(t, ctx, newTargetId, pwCredentialsId)
6185

6286
// Get credentials for target
@@ -67,13 +91,29 @@ func TestCliStaticCredentialStore(t *testing.T) {
6791
err = json.Unmarshal(output.Stdout, &newSessionAuthorizationResult)
6892
require.NoError(t, err)
6993

70-
newSessionAuthorization := newSessionAuthorizationResult.Item
71-
retrievedUser, ok := newSessionAuthorization.Credentials[0].Credential["username"].(string)
72-
require.True(t, ok)
73-
retrievedPassword, ok := newSessionAuthorization.Credentials[0].Credential["password"].(string)
74-
require.True(t, ok)
75-
assert.Equal(t, c.TargetSshUser, retrievedUser)
76-
assert.Equal(t, "password", retrievedPassword)
94+
brokeredCredentials := make([]map[string]any, 0, 3)
95+
for _, credential := range newSessionAuthorizationResult.Item.Credentials {
96+
brokeredCredentials = append(brokeredCredentials, credential.Credential)
97+
}
98+
99+
// Prepare expected credentials
100+
testCredentialsJson, err := os.ReadFile(testCredentialsFile)
101+
require.NoError(t, err)
102+
var expectedJsonCredentials map[string]any
103+
err = json.Unmarshal(testCredentialsJson, &expectedJsonCredentials)
104+
require.NoError(t, err)
105+
106+
sshPrivateKeyFileContent, err := os.ReadFile(testPemFile)
107+
require.NoError(t, err)
108+
sshPrivateKey := strings.TrimSpace(string(sshPrivateKeyFileContent))
109+
110+
expectedCredentials := []map[string]any{
111+
{"username": c.TargetSshUser, "password": testPassword},
112+
{"username": c.TargetSshUser, "private_key": sshPrivateKey},
113+
expectedJsonCredentials,
114+
}
115+
116+
assert.ElementsMatch(t, expectedCredentials, brokeredCredentials)
77117

78118
// Delete credential store
79119
output = e2e.RunCommand(ctx, "boundary",
@@ -111,6 +151,26 @@ func TestCliStaticCredentialStore(t *testing.T) {
111151
t.Logf("Successfully deleted credential store")
112152
}
113153

154+
func createPrivateKeyPemFile(fileName string) error {
155+
key, err := rsa.GenerateKey(rand.Reader, 2048)
156+
if err != nil {
157+
return err
158+
}
159+
160+
pemFile, err := os.Create(fileName)
161+
if err != nil {
162+
return err
163+
}
164+
defer pemFile.Close()
165+
166+
privateKey := &pem.Block{
167+
Type: "RSA PRIVATE KEY",
168+
Bytes: x509.MarshalPKCS1PrivateKey(key),
169+
}
170+
171+
return pem.Encode(pemFile, privateKey)
172+
}
173+
114174
// TestApiStaticCredentialStore uses the Go api to create a credential using
115175
// boundary's built-in credential store. The test then attaches that credential to a target.
116176
func TestApiStaticCredentialStore(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"username": "name-json",
3+
"password": "password-json"
4+
}

0 commit comments

Comments
 (0)