55 "crypto/tls"
66 "time"
77
8+ "github.com/couchbase/gocbcorex/cbhttpx"
9+ "github.com/couchbase/gocbcorex/cbmgmtx"
810 "github.com/couchbase/goprotostellar/genproto/admin_query_v1"
911 "github.com/couchbase/goprotostellar/genproto/admin_search_v1"
1012 "github.com/couchbase/goprotostellar/genproto/kv_v1"
@@ -20,6 +22,13 @@ import (
2022
2123func (s * GatewayOpsTestSuite ) TestClientCertAuth () {
2224 testutils .SkipIfNoDinoCluster (s .T ())
25+
26+ s .Run ("KvService" , s .KvService )
27+
28+ s .Run ("ClientCertAuthDisabled" , s .ClientCertConfiguration )
29+ }
30+
31+ func (s * GatewayOpsTestSuite ) KvService () {
2332 dino := testutils .StartDinoTesting (s .T (), false )
2433
2534 indexClient := admin_search_v1 .NewSearchAdminServiceClient (s .gatewayConn )
@@ -169,12 +178,129 @@ func (s *GatewayOpsTestSuite) TestClientCertAuth() {
169178 }
170179 requireRpcSuccess (s .T (), resp , err )
171180 return true
172- }, time .Second * 30 , time .Second )
181+ }, time .Second * 30 , time .Second * 5 )
173182 })
174183 })
175184 }
176185}
177186
187+ func (s * GatewayOpsTestSuite ) ClientCertConfiguration () {
188+ dino := testutils .StartDinoTesting (s .T (), false )
189+ username := "certConfig"
190+ conn := s .newClientCertConn (dino , username )
191+ kvClient := kv_v1 .NewKvServiceClient (conn )
192+
193+ getFn := func () (* kv_v1.GetResponse , error ) {
194+ return kvClient .Get (context .Background (), & kv_v1.GetRequest {
195+ BucketName : s .bucketName ,
196+ ScopeName : s .scopeName ,
197+ CollectionName : s .collectionName ,
198+ Key : s .testDocId (),
199+ })
200+ }
201+
202+ enableReq := & cbmgmtx.ConfigureClientCertAuthRequest {
203+ State : "enable" ,
204+ Prefixes : []cbmgmtx.Prefix {
205+ {
206+ Path : "san.email" ,
207+ Prefix : "" ,
208+ Delimiter : "@" ,
209+ },
210+ },
211+ }
212+
213+ dino .AddWriteUser (username )
214+
215+ // Check that client cert auth is working as expected.
216+ s .Run ("InitialSuccess" , func () {
217+ resp , err := getFn ()
218+ requireRpcSuccess (s .T (), resp , err )
219+ })
220+
221+ ep , err := s .testClusterInfo .AdminClient .GetMgmtEndpoint (context .Background ())
222+ require .NoError (s .T (), err )
223+ mgmt := cbmgmtx.Management {
224+ Transport : ep .RoundTripper ,
225+ UserAgent : "useragent" ,
226+ Endpoint : ep .Endpoint ,
227+ Auth : & cbhttpx.BasicAuth {
228+ Username : ep .Username ,
229+ Password : ep .Password ,
230+ },
231+ }
232+
233+ // Change the path that cbauth will try and get the name from and check
234+ // that the old cert fails
235+ err = mgmt .ConfigureClientCertAuth (context .Background (), & cbmgmtx.ConfigureClientCertAuthRequest {
236+ State : "enable" ,
237+ Prefixes : []cbmgmtx.Prefix {
238+ {
239+ Path : "subject.cn" ,
240+ Prefix : "" ,
241+ Delimiter : "" ,
242+ },
243+ },
244+ })
245+ assert .NoError (s .T (), err )
246+
247+ s .Run ("IncorrectUsernamePath" , func () {
248+ require .Eventually (s .T (), func () bool {
249+ _ , err := getFn ()
250+ if err == nil {
251+ return false
252+ }
253+
254+ assertRpcStatus (s .T (), err , codes .PermissionDenied )
255+ return assert .Contains (s .T (), err .Error (), "Your certificate is invalid" )
256+ }, time .Second * 30 , time .Second * 5 )
257+ })
258+
259+ // Restore intial settings and check that the original cert works again.
260+ err = mgmt .ConfigureClientCertAuth (context .Background (), enableReq )
261+ assert .NoError (s .T (), err )
262+
263+ s .Run ("SuccessAfterSettingsReset" , func () {
264+ require .Eventually (s .T (), func () bool {
265+ resp , err := getFn ()
266+ if err != nil {
267+ return false
268+ }
269+
270+ requireRpcSuccess (s .T (), resp , err )
271+ return true
272+ }, time .Second * 30 , time .Second * 5 )
273+ })
274+
275+ // Disable client cert auth on the cluster and make sure op fails.
276+ err = mgmt .ConfigureClientCertAuth (context .Background (), & cbmgmtx.ConfigureClientCertAuthRequest {
277+ State : "disable" ,
278+ Prefixes : []cbmgmtx.Prefix {
279+ {
280+ Path : "san.email" ,
281+ Prefix : "" ,
282+ Delimiter : "@" ,
283+ },
284+ },
285+ })
286+ assert .NoError (s .T (), err )
287+
288+ s .Run ("CertAuthDisabled" , func () {
289+ require .Eventually (s .T (), func () bool {
290+ _ , err := getFn ()
291+ if err == nil {
292+ return false
293+ }
294+
295+ assertRpcStatus (s .T (), err , codes .Unauthenticated )
296+ return assert .Contains (s .T (), err .Error (), "Client cert auth disabled on the cluster" )
297+ }, time .Second * 30 , time .Second * 5 )
298+ })
299+
300+ err = mgmt .ConfigureClientCertAuth (context .Background (), enableReq )
301+ assert .NoError (s .T (), err )
302+ }
303+
178304func (s * GatewayOpsTestSuite ) newClientCertConn (dino * testutils.DinoController , username string ) * grpc.ClientConn {
179305 res := dino .GetClientCert (username )
180306
0 commit comments