@@ -5,9 +5,14 @@ package static_test
5
5
6
6
import (
7
7
"context"
8
+ "crypto/rand"
9
+ "crypto/rsa"
10
+ "crypto/x509"
8
11
"encoding/json"
12
+ "encoding/pem"
9
13
"fmt"
10
14
"os"
15
+ "strings"
11
16
"testing"
12
17
"time"
13
18
@@ -21,6 +26,12 @@ import (
21
26
"github.com/stretchr/testify/require"
22
27
)
23
28
29
+ const (
30
+ testCredentialsFile = "testdata/credential.json"
31
+ testPemFile = "testdata/private-key.pem"
32
+ testPassword = "password"
33
+ )
34
+
24
35
// TestCliStaticCredentialStore validates various credential-store operations using the cli
25
36
func TestCliStaticCredentialStore (t * testing.T ) {
26
37
e2e .MaybeSkipTest (t )
@@ -43,9 +54,19 @@ func TestCliStaticCredentialStore(t *testing.T) {
43
54
boundary .AddHostToHostSetCli (t , ctx , newHostSetId , newHostId )
44
55
newTargetId := boundary .CreateNewTargetCli (t , ctx , newProjectId , c .TargetPort )
45
56
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
46
66
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 )
49
70
50
71
// Get credentials for target (expect empty)
51
72
output := e2e .RunCommand (ctx , "boundary" ,
@@ -57,6 +78,9 @@ func TestCliStaticCredentialStore(t *testing.T) {
57
78
require .NoError (t , err )
58
79
require .True (t , newSessionAuthorizationResult .Item .Credentials == nil )
59
80
81
+ // Add credentials to target
82
+ boundary .AddCredentialSourceToTargetCli (t , ctx , newTargetId , privateKeyCredentialsId )
83
+ boundary .AddCredentialSourceToTargetCli (t , ctx , newTargetId , jsonCredentialsId )
60
84
boundary .AddCredentialSourceToTargetCli (t , ctx , newTargetId , pwCredentialsId )
61
85
62
86
// Get credentials for target
@@ -67,13 +91,29 @@ func TestCliStaticCredentialStore(t *testing.T) {
67
91
err = json .Unmarshal (output .Stdout , & newSessionAuthorizationResult )
68
92
require .NoError (t , err )
69
93
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 )
77
117
78
118
// Delete credential store
79
119
output = e2e .RunCommand (ctx , "boundary" ,
@@ -111,6 +151,26 @@ func TestCliStaticCredentialStore(t *testing.T) {
111
151
t .Logf ("Successfully deleted credential store" )
112
152
}
113
153
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
+
114
174
// TestApiStaticCredentialStore uses the Go api to create a credential using
115
175
// boundary's built-in credential store. The test then attaches that credential to a target.
116
176
func TestApiStaticCredentialStore (t * testing.T ) {
0 commit comments