You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2020/10/15 09:46:47 UTC

[GitHub] [ignite] NSAmelchev commented on a change in pull request #7941: IGNITE-12843

NSAmelchev commented on a change in pull request #7941:
URL: https://github.com/apache/ignite/pull/7941#discussion_r505408315



##########
File path: modules/core/src/main/java/org/apache/ignite/internal/managers/encryption/GroupKeyChangeProcess.java
##########
@@ -0,0 +1,352 @@
+/*
+ * 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
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.ignite.internal.managers.encryption;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.IgniteFeatures;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import org.apache.ignite.internal.managers.encryption.GridEncryptionManager.EmptyResult;
+import org.apache.ignite.internal.managers.encryption.GridEncryptionManager.KeyChangeFuture;
+import org.apache.ignite.internal.processors.cache.CacheGroupDescriptor;
+import org.apache.ignite.internal.processors.cache.DynamicCacheDescriptor;
+import org.apache.ignite.internal.util.distributed.DistributedProcess;
+import org.apache.ignite.internal.util.future.GridFinishedFuture;
+import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.future.IgniteFinishedFutureImpl;
+import org.apache.ignite.internal.util.future.IgniteFutureImpl;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.lang.IgniteFuture;
+import org.apache.ignite.lang.IgniteFutureCancelledException;
+
+import static org.apache.ignite.internal.IgniteFeatures.CACHE_GROUP_KEY_CHANGE;
+import static org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType.CACHE_GROUP_KEY_CHANGE_FINISH;
+import static org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType.CACHE_GROUP_KEY_CHANGE_PREPARE;
+
+/**
+ * A two-phase distributed process that rotates the encryption keys of specified cache groups and initiates
+ * re-encryption of those cache groups.
+ */
+class GroupKeyChangeProcess {
+    /** Grid kernal context. */
+    private final GridKernalContext ctx;
+
+    /** Cache group encyption key change prepare phase. */
+    private final DistributedProcess<ChangeCacheEncryptionRequest, EmptyResult> prepareGKChangeProc;
+
+    /** Cache group encyption key change perform phase. */
+    private final DistributedProcess<ChangeCacheEncryptionRequest, EmptyResult> performGKChangeProc;
+
+    /** Group encryption keys. */
+    private final CacheGroupEncryptionKeys keys;
+
+    /** Cache group key change future. */
+    private volatile GroupKeyChangeFuture fut;
+
+    /** Cache group key change request. */
+    private volatile ChangeCacheEncryptionRequest req;
+
+    /**
+     * @param ctx Grid kernal context.
+     * @param keys Cache group encryption keys.
+     */
+    GroupKeyChangeProcess(GridKernalContext ctx, CacheGroupEncryptionKeys keys) {
+        this.ctx = ctx;
+        this.keys = keys;
+
+        prepareGKChangeProc =
+            new DistributedProcess<>(ctx, CACHE_GROUP_KEY_CHANGE_PREPARE, this::prepare, this::finishPrepare);
+        performGKChangeProc =
+            new DistributedProcess<>(ctx, CACHE_GROUP_KEY_CHANGE_FINISH, this::perform, this::finishPerform);
+    }
+
+    /**
+     * @return {@code True} if operation is still in progress.
+     */
+    public boolean inProgress() {
+        return req != null;
+    }
+
+    /**
+     * @param msg Error message.
+     */
+    public void cancel(String msg) {
+        GridFutureAdapter<Void> keyChangeFut = fut;
+
+        if (keyChangeFut != null && !keyChangeFut.isDone())
+            keyChangeFut.onDone(new IgniteFutureCancelledException(msg));
+    }
+
+    /**
+     * Starts cache group encryption key change process.
+     *
+     * @param cacheOrGrpNames Cache or group names.
+     */
+    public IgniteFuture<Void> start(Collection<String> cacheOrGrpNames) {
+        if (ctx.clientNode())
+            throw new UnsupportedOperationException("Client and daemon nodes can not perform this operation.");
+
+        if (!IgniteFeatures.allNodesSupports(ctx.grid().cluster().nodes(), CACHE_GROUP_KEY_CHANGE))
+            throw new IllegalStateException("Not all nodes in the cluster support this operation.");
+
+        if (!ctx.state().clusterState().state().active())
+            throw new IgniteException("Operation was rejected. The cluster is inactive.");
+
+        IgniteInternalFuture<Void> fut0 = fut;
+
+        if (fut0 != null && !fut0.isDone()) {
+            return new IgniteFinishedFutureImpl<>(new IgniteException("Cache group key change was rejected. " +
+                "The previous change was not completed."));
+        }
+
+        int[] grpIds = new int[cacheOrGrpNames.size()];
+        byte[] keyIds = new byte[grpIds.length];
+
+        int n = 0;
+
+        for (String cacheOrGroupName : cacheOrGrpNames) {
+            CacheGroupDescriptor grpDesc = ctx.cache().cacheGroupDescriptor(CU.cacheId(cacheOrGroupName));
+
+            if (grpDesc == null) {
+                DynamicCacheDescriptor cacheDesc = ctx.cache().cacheDescriptor(cacheOrGroupName);
+
+                if (cacheDesc == null) {
+                    throw new IgniteException("Cache group key change was rejected. " +
+                        "Cache or group \"" + cacheOrGroupName + "\" doesn't exists");
+                }
+
+                int grpId = cacheDesc.groupId();
+
+                grpDesc = ctx.cache().cacheGroupDescriptor(grpId);
+
+                if (grpDesc.sharedGroup()) {
+                    throw new IgniteException("Cache group key change was rejected. " +
+                        "Cache or group \"" + cacheOrGroupName + "\" is a part of group \"" +
+                        grpDesc.groupName() + "\". Provide group name instead of cache name for shared groups.");
+                }
+            }
+
+            if (!grpDesc.config().isEncryptionEnabled()) {
+                throw new IgniteException("Cache group key change was rejected. " +
+                    "Cache or group \"" + cacheOrGroupName + "\" is not encrypted.");
+            }
+
+            if (ctx.encryption().reencryptionInProgress(grpDesc.groupId())) {
+                throw new IgniteException("Cache group key change was rejected. " +
+                    "Cache group reencryption is in progress [grp=" + cacheOrGroupName + "]");
+            }
+
+            grpIds[n] = grpDesc.groupId();
+            keyIds[n] = (byte)(ctx.encryption().groupKey(grpDesc.groupId()).unsignedId() + 1);
+
+            n += 1;
+        }
+
+        byte[][] keys = ctx.encryption().createKeys(grpIds.length).get1().toArray(new byte[grpIds.length][]);
+
+        ChangeCacheEncryptionRequest req =
+            new ChangeCacheEncryptionRequest(grpIds, keys, keyIds, ctx.config().getEncryptionSpi().getMasterKeyName());

Review comment:
       Lets send a MK digest instead of a name




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org