@@ -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/couchbase/stellar-gateway/utils/certificates"
@@ -18,6 +21,8 @@ func (s *GatewayOpsTestSuite) TestClientCertAuth() {
1821 testutils .SkipIfNoDinoCluster (s .T ())
1922
2023 s .Run ("KvService" , s .KvService )
24+
25+ s .Run ("ClientCertAuthDisabled" , s .ClientCertConfiguration )
2126}
2227
2328func (s * GatewayOpsTestSuite ) KvService () {
@@ -97,15 +102,115 @@ func (s *GatewayOpsTestSuite) KvService() {
97102 })
98103}
99104
105+ func (s * GatewayOpsTestSuite ) ClientCertConfiguration () {
106+ dino := testutils .StartDinoTesting (s .T (), false )
107+ username := "certConfig"
108+ conn := s .newClientCertConn (username )
109+ kvClient := kv_v1 .NewKvServiceClient (conn )
110+
111+ getFn := func () (* kv_v1.GetResponse , error ) {
112+ return kvClient .Get (context .Background (), & kv_v1.GetRequest {
113+ BucketName : s .bucketName ,
114+ ScopeName : s .scopeName ,
115+ CollectionName : s .collectionName ,
116+ Key : s .testDocId (),
117+ })
118+ }
119+
120+ enableReq := & cbmgmtx.ConfigureClientCertAuthRequest {
121+ State : "enable" ,
122+ Prefixes : []cbmgmtx.Prefix {
123+ {
124+ Path : "san.email" ,
125+ Prefix : "" ,
126+ Delimiter : "@" ,
127+ },
128+ },
129+ }
130+
131+ dino .AddWriteUser (username )
132+ time .Sleep (time .Second * 5 )
133+
134+ // Check that client cert auth is working as expected.
135+ s .Run ("InitialSuccess" , func () {
136+ resp , err := getFn ()
137+ requireRpcSuccess (s .T (), resp , err )
138+ })
139+
140+ testConfig := testutils .GetTestConfig (s .T ())
141+ mgmt := cbmgmtx.Management {
142+ Transport : http .DefaultTransport ,
143+ UserAgent : "useragent" ,
144+ Endpoint : "http://" + testConfig .CbConnStr + ":8091" ,
145+ Auth : & cbhttpx.BasicAuth {
146+ Username : testConfig .CbUser ,
147+ Password : testConfig .CbPass ,
148+ },
149+ }
150+
151+ // Change the path that cbauth will try and get the name from and check
152+ // that the old cert fails
153+ err := mgmt .ConfigureClientCertAuth (context .Background (), & cbmgmtx.ConfigureClientCertAuthRequest {
154+ State : "enable" ,
155+ Prefixes : []cbmgmtx.Prefix {
156+ {
157+ Path : "subject.cn" ,
158+ Prefix : "" ,
159+ Delimiter : "" ,
160+ },
161+ },
162+ })
163+ time .Sleep (time .Second * 5 )
164+
165+ // Check that client cert auth is working as expected.
166+ s .Run ("IncorrectUsernamePath" , func () {
167+ _ , err := getFn ()
168+ assertRpcStatus (s .T (), err , codes .PermissionDenied )
169+ assert .Contains (s .T (), err .Error (), "Your certificate is invalid" )
170+ })
171+
172+ // Restore intial settings and check that the original cert works again.
173+ err = mgmt .ConfigureClientCertAuth (context .Background (), enableReq )
174+ assert .NoError (s .T (), err )
175+ time .Sleep (time .Second * 5 )
176+
177+ s .Run ("SuccessAfterSettingsReset" , func () {
178+ resp , err := getFn ()
179+ requireRpcSuccess (s .T (), resp , err )
180+ })
181+
182+ // Disable client cert auth on the cluster and make sure op fails.
183+ err = mgmt .ConfigureClientCertAuth (context .Background (), & cbmgmtx.ConfigureClientCertAuthRequest {
184+ State : "disable" ,
185+ Prefixes : []cbmgmtx.Prefix {
186+ {
187+ Path : "san.email" ,
188+ Prefix : "" ,
189+ Delimiter : "@" ,
190+ },
191+ },
192+ })
193+ assert .NoError (s .T (), err )
194+ time .Sleep (time .Second * 5 )
195+
196+ s .Run ("CertAuthDisabled" , func () {
197+ _ , err := getFn ()
198+ assertRpcStatus (s .T (), err , codes .Unauthenticated )
199+ assert .Contains (s .T (), err .Error (), "Client cert auth disabled on the cluster" )
200+ })
201+
202+ err = mgmt .ConfigureClientCertAuth (context .Background (), enableReq )
203+ assert .NoError (s .T (), err )
204+ }
205+
100206func (s * GatewayOpsTestSuite ) newClientCertConn (username string ) * grpc.ClientConn {
101207 cert , err := certificates .GenerateSignedClientCert (s .caCert , s .caKey , username )
102208 assert .NoError (s .T (), err )
103209
104210 conn , err := grpc .NewClient (s .gwConnAddr ,
105211 grpc .WithTransportCredentials (credentials .NewTLS (& tls.Config {
106- RootCAs : s .clientCaCertPool ,
107- Certificates : []tls.Certificate {* cert },
108- InsecureSkipVerify : false ,
212+ RootCAs : s .clientCaCertPool ,
213+ Certificates : []tls.Certificate {* cert },
109214 })))
110215 if err != nil {
111216 s .T ().Fatalf ("failed to connect to test gateway: %s" , err )
0 commit comments