2121import java .util .List ;
2222import java .util .Map ;
2323import java .util .Objects ;
24+ import java .util .Optional ;
2425import java .util .Set ;
2526import java .util .function .Consumer ;
2627import java .util .function .Supplier ;
@@ -77,7 +78,13 @@ enum UpdateType {
7778 private EncryptingFileIO encryptingFileIO ;
7879 private String tableKeyId ;
7980 private int encryptionDekLength ;
80- private List <EncryptedKey > encryptedKeysFromMetadata ;
81+
82+ // keys loaded from the latest metadata
83+ private Optional <List <EncryptedKey >> encryptedKeysFromMetadata = Optional .empty ();
84+
85+ // keys added to EM (e.g. as a result of a FileAppend) but not committed into the latest metadata
86+ // yet
87+ private Optional <List <EncryptedKey >> encryptedKeysPending = Optional .empty ();
8188
8289 RESTTableOperations (
8390 RESTClient client ,
@@ -290,9 +297,12 @@ public EncryptionManager encryption() {
290297 TableProperties .ENCRYPTION_DEK_LENGTH ,
291298 String .valueOf (encryptionDekLength ));
292299
300+ List <EncryptedKey > keys = Lists .newLinkedList ();
301+ encryptedKeysFromMetadata .ifPresent (keys ::addAll );
302+ encryptedKeysPending .ifPresent (keys ::addAll );
303+
293304 encryptionManager =
294- EncryptionUtil .createEncryptionManager (
295- encryptedKeysFromMetadata , encryptionProperties , kmsClient );
305+ EncryptionUtil .createEncryptionManager (keys , encryptionProperties , kmsClient );
296306 } else {
297307 return PlaintextEncryptionManager .instance ();
298308 }
@@ -342,7 +352,25 @@ private void encryptionPropsFromMetadata(TableMetadata metadata) {
342352 return ;
343353 }
344354
345- encryptedKeysFromMetadata = metadata .encryptionKeys ();
355+ encryptedKeysFromMetadata = Optional .ofNullable (metadata .encryptionKeys ());
356+
357+ if (encryptionManager != null ) {
358+ encryptedKeysPending = Optional .of (Lists .newLinkedList ());
359+
360+ Set <String > keyIdsFromMetadata =
361+ encryptedKeysFromMetadata .orElseGet (Lists ::newLinkedList ).stream ()
362+ .map (EncryptedKey ::keyId )
363+ .collect (Collectors .toSet ());
364+
365+ for (EncryptedKey keyFromEM : EncryptionUtil .encryptionKeys (encryptionManager ).values ()) {
366+ if (!keyIdsFromMetadata .contains (keyFromEM .keyId ())) {
367+ encryptedKeysPending .get ().add (keyFromEM );
368+ }
369+ }
370+
371+ } else {
372+ encryptedKeysPending = Optional .empty ();
373+ }
346374
347375 // Refresh encryption-related table properties on new/refreshed metadata
348376 Map <String , String > tableProperties = metadata .properties ();
@@ -368,7 +396,7 @@ private TableMetadata updateCurrentMetadata(LoadTableResponse response) {
368396 if (current == null
369397 || !Objects .equals (current .metadataFileLocation (), response .metadataLocation ())) {
370398 this .current = checkUUID (current , response .tableMetadata ());
371- encryptionPropsFromMetadata (this . current );
399+ encryptionPropsFromMetadata (current );
372400 }
373401
374402 return current ;
0 commit comments