|
41 | 41 | import com.google.cloud.bigquery.Dataset; |
42 | 42 | import com.google.cloud.bigquery.DatasetId; |
43 | 43 | import com.google.cloud.bigquery.DatasetInfo; |
| 44 | +import com.google.cloud.bigquery.EncryptionConfiguration; |
44 | 45 | import com.google.cloud.bigquery.ExternalTableDefinition; |
45 | 46 | import com.google.cloud.bigquery.ExtractJobConfiguration; |
46 | 47 | import com.google.cloud.bigquery.Field; |
@@ -214,6 +215,7 @@ public class ITBigQueryTest { |
214 | 215 |
|
215 | 216 | private static final Set<String> PUBLIC_DATASETS = ImmutableSet.of("github_repos", "hacker_news", |
216 | 217 | "noaa_gsod", "samples", "usa_names"); |
| 218 | + private static final String KMS_KEY_NAME = "projects/encryption-test-165123/locations/global/keyRings/key1/cryptoKeys/cmek_test1/cryptoKeyVersions/1"; |
217 | 219 |
|
218 | 220 | private static BigQuery bigquery; |
219 | 221 | private static Storage storage; |
@@ -1113,6 +1115,78 @@ public void testCancelNonExistingJob() { |
1113 | 1115 | assertFalse(bigquery.cancel("test_cancel_non_existing_job")); |
1114 | 1116 | } |
1115 | 1117 |
|
| 1118 | + @Test |
| 1119 | + public void testLoadCmek() throws InterruptedException, IOException, TimeoutException { |
| 1120 | + TableId tableId = TableId.of(DATASET, "test_insert_from_file_table_cmek"); |
| 1121 | + |
| 1122 | + LoadJobConfiguration configuration = LoadJobConfiguration.newBuilder( |
| 1123 | + tableId, "gs://" + BUCKET + "/" + JSON_LOAD_FILE, FormatOptions.json()) |
| 1124 | + .setCreateDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED) |
| 1125 | + .setSchema(TABLE_SCHEMA) |
| 1126 | + .setDestinationEncryptionConfiguration( |
| 1127 | + EncryptionConfiguration.newBuilder().setKmsKeyName(KMS_KEY_NAME).build()) |
| 1128 | + .build(); |
| 1129 | + |
| 1130 | + Job job = bigquery.create(JobInfo.of(configuration)); |
| 1131 | + job = job.waitFor(); |
| 1132 | + assertNull(job.getStatus().getError()); |
| 1133 | + |
| 1134 | + Table resultTable = bigquery.getTable(tableId); |
| 1135 | + assertNotNull(resultTable.getEncryptionConfiguration()); |
| 1136 | + assertEquals(KMS_KEY_NAME, resultTable.getEncryptionConfiguration().getKmsKeyName()); |
| 1137 | + } |
| 1138 | + |
| 1139 | + @Test |
| 1140 | + public void testQueryDstTableAndCopyCmek() throws InterruptedException, IOException, TimeoutException { |
| 1141 | + TableId queryDstTableId = TableId.of(DATASET, "test_query_dst_table_cmek"); |
| 1142 | + TableId copyDstTableId = TableId.of(DATASET, "test_copy_dst_table_cmek"); |
| 1143 | + |
| 1144 | + QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder("select 42 as A") |
| 1145 | + .setDestinationTable(queryDstTableId) |
| 1146 | + .setDestinationEncryptionConfiguration( |
| 1147 | + EncryptionConfiguration.newBuilder().setKmsKeyName(KMS_KEY_NAME).build()) |
| 1148 | + .build(); |
| 1149 | + |
| 1150 | + Job job = bigquery.create(JobInfo.of(configuration)); |
| 1151 | + job = job.waitFor(); |
| 1152 | + assertNull(job.getStatus().getError()); |
| 1153 | + |
| 1154 | + Table resultTable = bigquery.getTable(queryDstTableId); |
| 1155 | + assertNotNull(resultTable.getEncryptionConfiguration()); |
| 1156 | + assertEquals(KMS_KEY_NAME, resultTable.getEncryptionConfiguration().getKmsKeyName()); |
| 1157 | + |
| 1158 | + CopyJobConfiguration copyJobConfiguration = |
| 1159 | + CopyJobConfiguration.newBuilder(copyDstTableId, queryDstTableId) |
| 1160 | + .setDestinationEncryptionConfiguration( |
| 1161 | + EncryptionConfiguration.newBuilder().setKmsKeyName(KMS_KEY_NAME).build()) |
| 1162 | + .build(); |
| 1163 | + |
| 1164 | + Job copyJob = bigquery.create(JobInfo.of(copyJobConfiguration)); |
| 1165 | + copyJob = copyJob.waitFor(); |
| 1166 | + assertNull(copyJob.getStatus().getError()); |
| 1167 | + |
| 1168 | + resultTable = bigquery.getTable(copyDstTableId); |
| 1169 | + assertNotNull(resultTable.getEncryptionConfiguration()); |
| 1170 | + assertEquals(KMS_KEY_NAME, resultTable.getEncryptionConfiguration().getKmsKeyName()); |
| 1171 | + } |
| 1172 | + |
| 1173 | + @Test |
| 1174 | + public void testCreateTableCmek() throws InterruptedException, IOException, TimeoutException { |
| 1175 | + String destinationTableName = "test_create_table_cmek"; |
| 1176 | + TableId tableId = TableId.of(DATASET, destinationTableName); |
| 1177 | + |
| 1178 | + Table createdTable = bigquery.create( |
| 1179 | + TableInfo.newBuilder(tableId, StandardTableDefinition.newBuilder().build()) |
| 1180 | + .setEncryptionConfiguration( |
| 1181 | + EncryptionConfiguration.newBuilder().setKmsKeyName(KMS_KEY_NAME).build()) |
| 1182 | + .build()); |
| 1183 | + assertEquals(KMS_KEY_NAME, createdTable.getEncryptionConfiguration().getKmsKeyName()); |
| 1184 | + |
| 1185 | + Table resultTable = bigquery.getTable(tableId); |
| 1186 | + assertNotNull(resultTable.getEncryptionConfiguration()); |
| 1187 | + assertEquals(KMS_KEY_NAME, resultTable.getEncryptionConfiguration().getKmsKeyName()); |
| 1188 | + } |
| 1189 | + |
1116 | 1190 | @Test |
1117 | 1191 | public void testInsertFromFile() throws InterruptedException, IOException, TimeoutException { |
1118 | 1192 | String destinationTableName = "test_insert_from_file_table"; |
|
0 commit comments