|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/couchbase/goprotostellar/genproto/kv_v1" |
| 9 | + "github.com/couchbase/stellar-gateway/testutils" |
| 10 | + "github.com/couchbase/stellar-gateway/utils/certificates" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "google.golang.org/grpc" |
| 13 | + "google.golang.org/grpc/codes" |
| 14 | + "google.golang.org/grpc/credentials" |
| 15 | +) |
| 16 | + |
| 17 | +func (s *GatewayOpsTestSuite) TestKvServiceMtls() { |
| 18 | + // TO DO - is there a way to skip if client cert auth is not enabled |
| 19 | + testutils.SkipIfNoDinoCluster(s.T()) |
| 20 | + |
| 21 | + dino := testutils.StartDinoTesting(s.T(), false) |
| 22 | + |
| 23 | + // Create a user without any permissions |
| 24 | + username := "kvUser" |
| 25 | + dino.AddUser(username, false, false) |
| 26 | + |
| 27 | + s.T().Cleanup(func() { |
| 28 | + dino.RemoveUser(username) |
| 29 | + }) |
| 30 | + |
| 31 | + clientCert, err := certificates.GenerateSignedClientCert(s.caCert, s.caKey, username) |
| 32 | + assert.NoError(s.T(), err) |
| 33 | + |
| 34 | + conn, err := grpc.NewClient(s.gwConnAddr, |
| 35 | + grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{ |
| 36 | + RootCAs: s.clientCaCertPool, |
| 37 | + Certificates: []tls.Certificate{*clientCert}, |
| 38 | + InsecureSkipVerify: false, |
| 39 | + }))) |
| 40 | + if err != nil { |
| 41 | + s.T().Fatalf("failed to connect to test gateway: %s", err) |
| 42 | + } |
| 43 | + |
| 44 | + kvClient := kv_v1.NewKvServiceClient(conn) |
| 45 | + |
| 46 | + s.Run("ReadFailure", func() { |
| 47 | + _, err := kvClient.Get(context.Background(), &kv_v1.GetRequest{ |
| 48 | + BucketName: s.bucketName, |
| 49 | + ScopeName: s.scopeName, |
| 50 | + CollectionName: s.collectionName, |
| 51 | + Key: s.testDocId(), |
| 52 | + }) |
| 53 | + assertRpcStatus(s.T(), err, codes.PermissionDenied) |
| 54 | + assert.Contains(s.T(), err.Error(), "No permissions to read documents") |
| 55 | + }) |
| 56 | + |
| 57 | + // Add read permissions to the user and wait for this to take effect |
| 58 | + dino.AddUser(username, true, false) |
| 59 | + time.Sleep(time.Second * 5) |
| 60 | + |
| 61 | + s.Run("ReadSuccess", func() { |
| 62 | + resp, err := kvClient.Get(context.Background(), &kv_v1.GetRequest{ |
| 63 | + BucketName: s.bucketName, |
| 64 | + ScopeName: s.scopeName, |
| 65 | + CollectionName: s.collectionName, |
| 66 | + Key: s.testDocId(), |
| 67 | + }) |
| 68 | + requireRpcSuccess(s.T(), resp, err) |
| 69 | + assertValidCas(s.T(), resp.Cas) |
| 70 | + assert.Equal(s.T(), TEST_CONTENT, resp.GetContentUncompressed()) |
| 71 | + assert.Nil(s.T(), resp.GetContentCompressed()) |
| 72 | + assert.Equal(s.T(), TEST_CONTENT_FLAGS, resp.ContentFlags) |
| 73 | + assert.Nil(s.T(), resp.Expiry) |
| 74 | + }) |
| 75 | + |
| 76 | + s.Run("WriteFailure", func() { |
| 77 | + docId := s.randomDocId() |
| 78 | + _, err := kvClient.Upsert(context.Background(), &kv_v1.UpsertRequest{ |
| 79 | + BucketName: s.bucketName, |
| 80 | + ScopeName: s.scopeName, |
| 81 | + CollectionName: s.collectionName, |
| 82 | + Key: docId, |
| 83 | + Content: &kv_v1.UpsertRequest_ContentUncompressed{ |
| 84 | + ContentUncompressed: TEST_CONTENT, |
| 85 | + }, |
| 86 | + ContentFlags: TEST_CONTENT_FLAGS, |
| 87 | + }) |
| 88 | + assertRpcStatus(s.T(), err, codes.PermissionDenied) |
| 89 | + assert.Contains(s.T(), err.Error(), "No permissions to write documents") |
| 90 | + }) |
| 91 | + |
| 92 | + // Add write permissions to the user |
| 93 | + dino.AddUser(username, false, true) |
| 94 | + time.Sleep(time.Second * 5) |
| 95 | + |
| 96 | + s.Run("WriteSuccess", func() { |
| 97 | + docId := s.randomDocId() |
| 98 | + resp, err := kvClient.Upsert(context.Background(), &kv_v1.UpsertRequest{ |
| 99 | + BucketName: s.bucketName, |
| 100 | + ScopeName: s.scopeName, |
| 101 | + CollectionName: s.collectionName, |
| 102 | + Key: docId, |
| 103 | + Content: &kv_v1.UpsertRequest_ContentUncompressed{ |
| 104 | + ContentUncompressed: TEST_CONTENT, |
| 105 | + }, |
| 106 | + ContentFlags: TEST_CONTENT_FLAGS, |
| 107 | + }) |
| 108 | + requireRpcSuccess(s.T(), resp, err) |
| 109 | + assertValidCas(s.T(), resp.Cas) |
| 110 | + assertValidMutationToken(s.T(), resp.MutationToken, s.bucketName) |
| 111 | + }) |
| 112 | +} |
0 commit comments