1
1
/*
2
- * Licensed to the Apache Software Foundation (ASF) under one
3
- * or more contributor license agreements. See the NOTICE file
4
- * distributed with this work for additional information
5
- * regarding copyright ownership. The ASF licenses this file
6
- * to you under the Apache License, Version 2.0 (the
7
- * "License"); you may not use this file except in compliance
8
- * with the License. You may obtain a copy of the License at
2
+ * Copyright DataStax, Inc.
9
3
*
10
- * http://www.apache.org/licenses/LICENSE-2.0
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
11
9
*
12
10
* Unless required by applicable law or agreed to in writing, software
13
11
* distributed under the License is distributed on an "AS IS" BASIS,
23
21
import java .nio .charset .StandardCharsets ;
24
22
import java .nio .file .Files ;
25
23
import java .nio .file .Path ;
24
+ import java .nio .file .Paths ;
26
25
import java .security .NoSuchAlgorithmException ;
27
26
import java .util .List ;
28
27
import java .util .stream .Collectors ;
34
33
import org .junit .BeforeClass ;
35
34
import org .junit .Test ;
36
35
36
+ import org .apache .cassandra .config .CassandraRelevantProperties ;
37
37
import org .apache .cassandra .crypto .LocalSystemKey ;
38
38
import org .apache .cassandra .crypto .TDEConfigurationProvider ;
39
39
import org .apache .cassandra .db .Keyspace ;
40
40
import org .apache .cassandra .distributed .Cluster ;
41
41
import org .apache .cassandra .distributed .api .ConsistencyLevel ;
42
+ import org .apache .cassandra .distributed .api .NodeToolResult ;
42
43
import org .apache .cassandra .io .sstable .Component ;
43
44
import org .apache .cassandra .io .sstable .format .SSTableFormat ;
44
45
import org .apache .cassandra .io .sstable .format .bti .BtiFormat ;
@@ -60,22 +61,20 @@ public class SSTableEncryptionTest extends TestBaseImpl
60
61
private static final String KEYSPACE_PREFIX = "ks" ;
61
62
private static final String TABLE_PREFIX = "tbl" ;
62
63
private static final String SENSITIVE_KEY = "Key with sensitive information" ;
63
- private static final int ROWS_COUNT = 10000 ;
64
+ private static final int ROWS_COUNT = 20000 ;
64
65
65
- private static String defaultSystemKeyDirectory ;
66
66
67
67
@ BeforeClass
68
68
public static void beforeAll () throws IOException
69
69
{
70
- defaultSystemKeyDirectory = TDEConfigurationProvider .getConfiguration ().systemKeyDirectory ;
71
70
Path systemKeyDirectory = Files .createTempDirectory ("system_key_directory" );
72
- TDEConfigurationProvider . setSystemKeyDirectoryProperty (systemKeyDirectory .toString ());
71
+ CassandraRelevantProperties . SYSTEM_KEY_DIRECTORY . setString (systemKeyDirectory .toString ());
73
72
}
74
73
75
74
@ AfterClass
76
75
public static void tearDown ()
77
76
{
78
- TDEConfigurationProvider . setSystemKeyDirectoryProperty ( defaultSystemKeyDirectory );
77
+ CassandraRelevantProperties . SYSTEM_KEY_DIRECTORY . reset ( );
79
78
}
80
79
81
80
@ Test
@@ -87,7 +86,7 @@ public void shouldCreateQueryableEncryptedSSTables() throws Throwable
87
86
{
88
87
// given a table with data encrypted using local key
89
88
String keyspace = createKeyspace (cluster );
90
- Path secretKey = createLocalSecretKey ();
89
+ Path secretKey = createLocalSecretKey (cluster );
91
90
String table = createEncryptedTable (cluster , keyspace , secretKey );
92
91
int numberOfRows = 10 ;
93
92
@@ -141,7 +140,7 @@ public void shouldEncryptSensitiveData() throws Exception
141
140
// given tables with and without encryption
142
141
String keyspace = createKeyspace (cluster );
143
142
TestTable nonEncryptedTable = createTableWithSampleData (cluster , keyspace , "" );
144
- Path secretKey = createLocalSecretKey ();
143
+ Path secretKey = createLocalSecretKey (cluster );
145
144
TestTable encryptedTable = createTableWithSampleData (cluster , keyspace , localSystemKeyEncryptionCompressionSuffix ("Encryptor" , secretKey .toAbsolutePath ().toString ()));
146
145
147
146
// then
@@ -156,6 +155,7 @@ public void shouldEncryptSensitiveData() throws Exception
156
155
157
156
// indexes with encryption should pass the checksum check
158
157
assertThat (checkEncryptionCrc (encryptedTable .partitionIndexBytes )).isTrue ();
158
+ assertThat (encryptedTable .rowIndexBytes .length ).isGreaterThan (0 );
159
159
assertThat (checkEncryptionCrc (encryptedTable .rowIndexBytes )).isTrue ();
160
160
// indexes without encryption should fail the checksum check
161
161
assertThat (checkEncryptionCrc (nonEncryptedTable .partitionIndexBytes )).isFalse ();
@@ -193,7 +193,7 @@ public void shouldNotReadRowsFromEncryptedTableWithoutTheSecretKey() throws Exce
193
193
194
194
// given a table with data encrypted using local key
195
195
String keyspace = createKeyspace (cluster );
196
- Path secretKey = createLocalSecretKey ();
196
+ Path secretKey = createLocalSecretKey (cluster );
197
197
String encryptedTableName = createEncryptedTable (cluster , keyspace , secretKey );
198
198
String nonEncryptedTableName = createTable (cluster , keyspace );
199
199
int numberOfRows = 10 ;
@@ -230,7 +230,7 @@ public void shouldFailWhenReadingWithDifferentKey() throws Exception
230
230
231
231
// given a table with data encrypted using local key
232
232
String keyspace = createKeyspace (cluster );
233
- Path secretKey = createLocalSecretKey ();
233
+ Path secretKey = createLocalSecretKey (cluster );
234
234
String encryptedTableName = createEncryptedTable (cluster , keyspace , secretKey );
235
235
String nonEncryptedTableName = createTable (cluster , keyspace );
236
236
int numberOfRows = 10 ;
@@ -342,10 +342,15 @@ private String createKeyspace(Cluster cluster)
342
342
return randomKeyspaceName ;
343
343
}
344
344
345
- private Path createLocalSecretKey () throws IOException , NoSuchAlgorithmException , NoSuchPaddingException
345
+ private Path createLocalSecretKey (Cluster cluster )
346
346
{
347
347
String keyPath = "system_key_" + RandomStringUtils .random (10 , true , true );
348
- return createLocalSecretKey (keyPath );
348
+ Path keyFullPath = Paths .get (TDEConfigurationProvider .getConfiguration ().systemKeyDirectory ).resolve (keyPath );
349
+ assertThat (Files .exists (keyFullPath )).isFalse ();
350
+ NodeToolResult result = cluster .get (1 ).nodetoolResult ("createsystemkey" , "AES/CBC/PKCS5Padding" , "256" , keyPath );
351
+ result .asserts ().success ();
352
+ assertThat (Files .exists (keyFullPath )).isTrue ();
353
+ return keyFullPath ;
349
354
}
350
355
351
356
private Path createLocalSecretKey (String keyPath ) throws IOException , NoSuchAlgorithmException , NoSuchPaddingException
0 commit comments