@@ -19,14 +19,137 @@ import (
1919 "github.com/stretchr/testify/require"
2020)
2121
22- // TestCliTcpTargetConnectRedis uses the boundary cli to connect to a target using `connect redis`
23- func TestCliTcpTargetConnectRedis (t * testing.T ) {
22+ type RedisContainerInfo struct {
23+ Hostname string
24+ Port string
25+ Username string
26+ Password string
27+ }
28+
29+ type BoundaryResources struct {
30+ TargetId string
31+ StoreId string
32+ }
33+
34+ // TestCliTcpTargetConnectRedisWithUsernamePassword uses the boundary cli to connect
35+ // to a target using `connect redis` with a username and password
36+ func TestCliTcpTargetConnectRedisWithUsernamePassword (t * testing.T ) {
2437 e2e .MaybeSkipTest (t )
2538
26- pool , err := dockertest .NewPool ("" )
39+ // Setup
40+ ctx := context .Background ()
41+ redisInfo := setupRedisContainer (t , ctx )
42+ resources := setupBoundaryResources (t , ctx , redisInfo )
43+
44+ credentialId , err := boundary .CreateStaticCredentialUsernamePasswordCli (
45+ t ,
46+ ctx ,
47+ resources .StoreId ,
48+ redisInfo .Username ,
49+ redisInfo .Password ,
50+ )
51+ require .NoError (t , err )
52+
53+ err = boundary .AddBrokeredCredentialSourceToTargetCli (t , ctx , resources .TargetId , credentialId )
2754 require .NoError (t , err )
2855
56+ t .Logf ("Attempting to connect to Redis target %s" , resources .TargetId )
57+
58+ // Validation
59+ cmd := exec .CommandContext (ctx ,
60+ "boundary" ,
61+ "connect" , "redis" ,
62+ "-target-id" , resources .TargetId ,
63+ )
64+
65+ stdin , err := cmd .StdinPipe ()
66+ require .NoError (t , err )
67+ stdout , err := cmd .StdoutPipe ()
68+ require .NoError (t , err )
69+ require .NoError (t , cmd .Start ())
70+
71+ output , err := sendRedisCommand (stdin , stdout , "ACL WHOAMI\r \n " )
72+ require .NoError (t , err )
73+ require .Equal (t , redisInfo .Username , output )
74+
75+ output , err = sendRedisCommand (stdin , stdout , "SET e2etestkey e2etestvalue\r \n " )
76+ require .NoError (t , err )
77+ require .Equal (t , "OK" , output )
78+
79+ output , err = sendRedisCommand (stdin , stdout , "GET e2etestkey\r \n " )
80+ require .NoError (t , err )
81+ require .Equal (t , "e2etestvalue" , output )
82+
83+ output , err = sendRedisCommand (stdin , stdout , "QUIT\r \n " )
84+ require .Equal (t , io .EOF , err )
85+ require .Empty (t , output )
86+
87+ // Confirm that boundary connect has closed
88+ err = cmd .Wait ()
89+ require .NoError (t , err )
90+ }
91+
92+ // TestCliTcpTargetConnectRedisWithPassword uses the boundary cli to connect
93+ // to a target using `connect redis` with a password
94+ func TestCliTcpTargetConnectRedisWithPassword (t * testing.T ) {
95+ e2e .MaybeSkipTest (t )
96+
97+ // Setup
2998 ctx := context .Background ()
99+ redisInfo := setupRedisContainer (t , ctx )
100+ resources := setupBoundaryResources (t , ctx , redisInfo )
101+
102+ credentialId , err := boundary .CreateStaticCredentialPasswordCli (
103+ t ,
104+ ctx ,
105+ resources .StoreId ,
106+ redisInfo .Password ,
107+ )
108+ require .NoError (t , err )
109+
110+ err = boundary .AddBrokeredCredentialSourceToTargetCli (t , ctx , resources .TargetId , credentialId )
111+ require .NoError (t , err )
112+
113+ t .Logf ("Attempting to connect to Redis target %s" , resources .TargetId )
114+
115+ // Validation
116+ cmd := exec .CommandContext (ctx ,
117+ "boundary" ,
118+ "connect" , "redis" ,
119+ "-target-id" , resources .TargetId ,
120+ )
121+
122+ stdin , err := cmd .StdinPipe ()
123+ require .NoError (t , err )
124+ stdout , err := cmd .StdoutPipe ()
125+ require .NoError (t , err )
126+ require .NoError (t , cmd .Start ())
127+
128+ output , err := sendRedisCommand (stdin , stdout , "ACL WHOAMI\r \n " )
129+ require .NoError (t , err )
130+ require .Equal (t , "default" , output )
131+
132+ output , err = sendRedisCommand (stdin , stdout , "SET e2etestkey e2etestvalue\r \n " )
133+ require .NoError (t , err )
134+ require .Equal (t , "OK" , output )
135+
136+ output , err = sendRedisCommand (stdin , stdout , "GET e2etestkey\r \n " )
137+ require .NoError (t , err )
138+ require .Equal (t , "e2etestvalue" , output )
139+
140+ output , err = sendRedisCommand (stdin , stdout , "QUIT\r \n " )
141+ require .Equal (t , io .EOF , err )
142+ require .Empty (t , output )
143+
144+ // Confirm that boundary connect has closed
145+ err = cmd .Wait ()
146+ require .NoError (t , err )
147+ }
148+
149+ // setupRedisContainer starts a Redis container and returns its connection info
150+ func setupRedisContainer (t * testing.T , ctx context.Context ) * RedisContainerInfo {
151+ pool , err := dockertest .NewPool ("" )
152+ require .NoError (t , err )
30153
31154 network , err := pool .NetworksByName ("e2e_cluster" )
32155 require .NoError (t , err , "Failed to get e2e_cluster network" )
@@ -52,12 +175,22 @@ func TestCliTcpTargetConnectRedis(t *testing.T) {
52175 // Wait for Redis to be ready
53176 err = pool .Retry (func () error {
54177 out , e := exec .CommandContext (ctx , "docker" , "exec" , hostname ,
55- "redis-cli" , "-h" , hostname , "-p" , port , "PING" ).CombinedOutput ()
178+ "redis-cli" , "-h" , hostname , "-p" , port , "-a" , pw , " PING" ).CombinedOutput ()
56179 t .Logf ("Redis PING output: %s" , out )
57180 return e
58181 })
59182 require .NoError (t , err , "Redis container failed to start" )
183+ return & RedisContainerInfo {
184+ Hostname : hostname ,
185+ Port : port ,
186+ Username : user ,
187+ Password : pw ,
188+ }
189+ }
60190
191+ // setupBoundaryResources sets up the following Boundary resources:
192+ // Org, Project, Target, Credential Store
193+ func setupBoundaryResources (t * testing.T , ctx context.Context , redisInfo * RedisContainerInfo ) * BoundaryResources {
61194 boundary .AuthenticateAdminCli (t , ctx )
62195
63196 orgId , err := boundary .CreateOrgCli (t , ctx )
@@ -76,55 +209,18 @@ func TestCliTcpTargetConnectRedis(t *testing.T) {
76209 t ,
77210 ctx ,
78211 projectId ,
79- port ,
80- target .WithAddress (hostname ),
212+ redisInfo . Port ,
213+ target .WithAddress (redisInfo . Hostname ),
81214 )
82215 require .NoError (t , err )
83216
84217 storeId , err := boundary .CreateCredentialStoreStaticCli (t , ctx , projectId )
85218 require .NoError (t , err )
86219
87- credentialId , err := boundary .CreateStaticCredentialPasswordCli (
88- t ,
89- ctx ,
90- storeId ,
91- user ,
92- pw ,
93- )
94- require .NoError (t , err )
95-
96- err = boundary .AddBrokeredCredentialSourceToTargetCli (t , ctx , targetId , credentialId )
97- require .NoError (t , err )
98-
99- t .Logf ("Attempting to connect to Redis target %s" , targetId )
100-
101- cmd := exec .CommandContext (ctx ,
102- "boundary" ,
103- "connect" , "redis" ,
104- "-target-id" , targetId ,
105- )
106-
107- stdin , err := cmd .StdinPipe ()
108- require .NoError (t , err )
109- stdout , err := cmd .StdoutPipe ()
110- require .NoError (t , err )
111- require .NoError (t , cmd .Start ())
112-
113- output , err := sendRedisCommand (stdin , stdout , "SET e2etestkey e2etestvalue\r \n " )
114- require .NoError (t , err )
115- require .Equal (t , "OK" , output )
116-
117- output , err = sendRedisCommand (stdin , stdout , "GET e2etestkey\r \n " )
118- require .NoError (t , err )
119- require .Equal (t , "e2etestvalue" , output )
120-
121- output , err = sendRedisCommand (stdin , stdout , "QUIT\r \n " )
122- require .Equal (t , io .EOF , err )
123- require .Empty (t , output )
124-
125- // Confirm that boundary connect has closed
126- err = cmd .Wait ()
127- require .NoError (t , err )
220+ return & BoundaryResources {
221+ TargetId : targetId ,
222+ StoreId : storeId ,
223+ }
128224}
129225
130226func sendRedisCommand (stdin io.WriteCloser , stdout io.ReadCloser , cmdStr string ) (string , error ) {
0 commit comments