duongkame commented on code in PR #4194:
URL: https://github.com/apache/ozone/pull/4194#discussion_r1090250875


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/security/symmetric/SecretKeyManager.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.security.symmetric;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.crypto.KeyGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.time.Duration;
+import java.time.Instant;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.TimeoutException;
+
+import static java.time.Duration.between;
+import static java.util.Comparator.comparing;
+import static java.util.Objects.requireNonNull;
+import static java.util.stream.Collectors.toList;
+
+/**
+ * This component manages symmetric SecretKey life-cycle, including generation,
+ * rotation and destruction.
+ */
+public class SecretKeyManager {
+  private static final Logger LOG =
+      LoggerFactory.getLogger(SecretKeyManager.class);
+
+  private final SecretKeyState state;
+  private boolean pendingInititializedState = false;
+  private final Duration rotationDuration;
+  private final Duration validityDuration;
+  private final SecretKeyStore keyStore;
+
+  private final KeyGenerator keyGenerator;
+
+  public SecretKeyManager(SecretKeyState state,
+                          SecretKeyStore keyStore,
+                          Duration rotationDuration,
+                          Duration validityDuration,
+                          String algorithm) {
+    this.state = requireNonNull(state);
+    this.rotationDuration = requireNonNull(rotationDuration);
+    this.validityDuration = requireNonNull(validityDuration);
+    this.keyStore = requireNonNull(keyStore);
+    this.keyGenerator = createKeyGenerator(algorithm);
+  }
+
+  public SecretKeyManager(SecretKeyState state,
+                          SecretKeyStore keyStore,
+                          SecretKeyConfig config) {
+    this(state, keyStore, config.getRotateDuration(),
+        config.getExpiryDuration(), config.getAlgorithm());
+  }
+
+  /**
+   * Initialize the state from by loading SecretKeys from local file, or
+   * generate new keys if the file doesn't exist.
+   *
+   * @throws TimeoutException can possibly occur when replicating the state.
+   */
+  public synchronized boolean initialize() {
+    if (state.getCurrentKey() != null) {
+      return false;
+    }
+
+    List<ManagedSecretKey> sortedKeys = keyStore.load()
+        .stream()
+        .filter(x -> !x.isExpired())
+        .sorted(comparing(ManagedSecretKey::getCreationTime))
+        .collect(toList());
+
+    ManagedSecretKey currentKey;
+    if (sortedKeys.isEmpty()) {
+      // First start, generate new key as the current key.
+      currentKey = generateSecretKey();
+      sortedKeys.add(currentKey);
+      LOG.info("No keys is loaded, generated new key: {}", currentKey);
+    } else {
+      // For restarts, reload allKeys and take the latest one as current.
+      currentKey = sortedKeys.get(sortedKeys.size() - 1);
+      LOG.info("Key reloaded, current key: {}, all keys: {}", currentKey,
+          sortedKeys);
+    }
+
+    // First, update the SecretKey state to make it visible immediately on the
+    // current instance.
+    state.updateKeysInternal(currentKey, sortedKeys);

Review Comment:
   Yup. As discussed offline, the keys should be agreed upon by all SCM parties 
before being visible to clients. A change has been made. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to