@@ -3,8 +3,11 @@ package test
33import (
44 "context"
55 "crypto/tls"
6+ "net/http"
67 "time"
78
9+ "github.com/couchbase/gocbcorex/cbhttpx"
10+ "github.com/couchbase/gocbcorex/cbmgmtx"
811 "github.com/couchbase/goprotostellar/genproto/kv_v1"
912 "github.com/couchbase/stellar-gateway/testutils"
1013 "github.com/stretchr/testify/assert"
@@ -17,6 +20,8 @@ func (s *GatewayOpsTestSuite) TestClientCertAuth() {
1720 testutils .SkipIfNoDinoCluster (s .T ())
1821
1922 s .Run ("KvService" , s .KvService )
23+
24+ s .Run ("ClientCertAuthDisabled" , s .ClientCertConfiguration )
2025}
2126
2227func (s * GatewayOpsTestSuite ) KvService () {
@@ -96,6 +101,107 @@ func (s *GatewayOpsTestSuite) KvService() {
96101 })
97102}
98103
104+ func (s * GatewayOpsTestSuite ) ClientCertConfiguration () {
105+ dino := testutils .StartDinoTesting (s .T (), false )
106+ username := "certConfig"
107+ conn := s .newClientCertConn (dino , username )
108+ kvClient := kv_v1 .NewKvServiceClient (conn )
109+
110+ getFn := func () (* kv_v1.GetResponse , error ) {
111+ return kvClient .Get (context .Background (), & kv_v1.GetRequest {
112+ BucketName : s .bucketName ,
113+ ScopeName : s .scopeName ,
114+ CollectionName : s .collectionName ,
115+ Key : s .testDocId (),
116+ })
117+ }
118+
119+ enableReq := & cbmgmtx.ConfigureClientCertAuthRequest {
120+ State : "enable" ,
121+ Prefixes : []cbmgmtx.Prefix {
122+ {
123+ Path : "san.email" ,
124+ Prefix : "" ,
125+ Delimiter : "@" ,
126+ },
127+ },
128+ }
129+
130+ dino .AddWriteUser (username )
131+ time .Sleep (time .Second * 5 )
132+
133+ // Check that client cert auth is working as expected.
134+ s .Run ("InitialSuccess" , func () {
135+ resp , err := getFn ()
136+ requireRpcSuccess (s .T (), resp , err )
137+ })
138+
139+ testConfig := testutils .GetTestConfig (s .T ())
140+ mgmt := cbmgmtx.Management {
141+ Transport : http .DefaultTransport ,
142+ UserAgent : "useragent" ,
143+ Endpoint : "http://" + testConfig .CbConnStr + ":8091" ,
144+ Auth : & cbhttpx.BasicAuth {
145+ Username : testConfig .CbUser ,
146+ Password : testConfig .CbPass ,
147+ },
148+ }
149+
150+ // Change the path that cbauth will try and get the name from and check
151+ // that the old cert fails
152+ err := mgmt .ConfigureClientCertAuth (context .Background (), & cbmgmtx.ConfigureClientCertAuthRequest {
153+ State : "enable" ,
154+ Prefixes : []cbmgmtx.Prefix {
155+ {
156+ Path : "subject.cn" ,
157+ Prefix : "" ,
158+ Delimiter : "" ,
159+ },
160+ },
161+ })
162+ time .Sleep (time .Second * 5 )
163+
164+ // Check that client cert auth is working as expected.
165+ s .Run ("IncorrectUsernamePath" , func () {
166+ _ , err := getFn ()
167+ assertRpcStatus (s .T (), err , codes .PermissionDenied )
168+ assert .Contains (s .T (), err .Error (), "Your certificate is invalid" )
169+ })
170+
171+ // Restore intial settings and check that the original cert works again.
172+ err = mgmt .ConfigureClientCertAuth (context .Background (), enableReq )
173+ assert .NoError (s .T (), err )
174+ time .Sleep (time .Second * 5 )
175+
176+ s .Run ("SuccessAfterSettingsReset" , func () {
177+ resp , err := getFn ()
178+ requireRpcSuccess (s .T (), resp , err )
179+ })
180+
181+ // Disable client cert auth on the cluster and make sure op fails.
182+ err = mgmt .ConfigureClientCertAuth (context .Background (), & cbmgmtx.ConfigureClientCertAuthRequest {
183+ State : "disable" ,
184+ Prefixes : []cbmgmtx.Prefix {
185+ {
186+ Path : "san.email" ,
187+ Prefix : "" ,
188+ Delimiter : "@" ,
189+ },
190+ },
191+ })
192+ assert .NoError (s .T (), err )
193+ time .Sleep (time .Second * 5 )
194+
195+ s .Run ("CertAuthDisabled" , func () {
196+ _ , err := getFn ()
197+ assertRpcStatus (s .T (), err , codes .Unauthenticated )
198+ assert .Contains (s .T (), err .Error (), "Client cert auth disabled on the cluster" )
199+ })
200+
201+ err = mgmt .ConfigureClientCertAuth (context .Background (), enableReq )
202+ assert .NoError (s .T (), err )
203+ }
204+
99205func (s * GatewayOpsTestSuite ) newClientCertConn (dino * testutils.DinoController , username string ) * grpc.ClientConn {
100206 res := dino .GetClientCert (username )
101207
0 commit comments