Vladsz83 commented on code in PR #10558:
URL: https://github.com/apache/ignite/pull/10558#discussion_r1124395655


##########
modules/core/src/test/java/org/apache/ignite/internal/processors/security/cluster/ClusterStatePermissionTest.java:
##########
@@ -0,0 +1,344 @@
+/*
+ * 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.processors.security.cluster;
+
+import java.security.Permissions;
+import java.util.Collection;
+import java.util.function.Consumer;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCluster;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.client.ClientAuthorizationException;
+import org.apache.ignite.client.ClientCluster;
+import org.apache.ignite.client.Config;
+import org.apache.ignite.cluster.ClusterState;
+import org.apache.ignite.configuration.ClientConfiguration;
+import org.apache.ignite.configuration.ConnectorConfiguration;
+import org.apache.ignite.configuration.DataRegionConfiguration;
+import org.apache.ignite.configuration.DataStorageConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.client.GridClient;
+import org.apache.ignite.internal.client.GridClientConfiguration;
+import org.apache.ignite.internal.client.GridClientException;
+import org.apache.ignite.internal.client.GridClientFactory;
+import org.apache.ignite.internal.processors.rest.GridRestCommand;
+import org.apache.ignite.internal.processors.security.AbstractSecurityTest;
+import org.apache.ignite.internal.processors.security.impl.TestSecurityData;
+import 
org.apache.ignite.internal.processors.security.impl.TestSecurityPluginProvider;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.plugin.security.SecurityCredentials;
+import org.apache.ignite.plugin.security.SecurityCredentialsBasicProvider;
+import org.apache.ignite.plugin.security.SecurityException;
+import org.apache.ignite.plugin.security.SecurityPermission;
+import org.apache.ignite.plugin.security.SecurityPermissionSetBuilder;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.cluster.ClusterState.INACTIVE;
+import static org.apache.ignite.internal.util.lang.GridFunc.asList;
+import static 
org.apache.ignite.plugin.security.SecurityPermission.ADMIN_CLUSTER_STATE;
+import static 
org.apache.ignite.plugin.security.SecurityPermission.JOIN_AS_SERVER;
+import static 
org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.create;
+import static 
org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause;
+import static org.apache.ignite.testframework.GridTestUtils.cartesianProduct;
+
+/**
+ * Tests permissions of cluster state change.
+ */
+@RunWith(Parameterized.class)
+public class ClusterStatePermissionTest extends AbstractSecurityTest {
+    /** */
+    private static final SecurityPermission[] EMPTY_PERMS = new 
SecurityPermission[0];
+
+    /** */
+    private SecurityPermission[] permissions = EMPTY_PERMS;
+
+    /** From-client flag parameter. */
+    @Parameterized.Parameter
+    public Initiator initiator;
+
+    /** Persistence flag parameter. */
+    @Parameterized.Parameter(1)
+    public boolean persistence;
+
+    /** @return Test parameters. */
+    @Parameterized.Parameters(name = "nodeType={0}, persistence={1}")
+    public static Collection<?> parameters() {
+        return cartesianProduct(asList(Initiator.values()), asList(false, 
true));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
instanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(instanceName);
+
+        if (!cfg.isClientMode()) {
+            cfg.setDataStorageConfiguration(new DataStorageConfiguration()
+                .setDefaultDataRegionConfiguration(new 
DataRegionConfiguration().setPersistenceEnabled(persistence)));
+        }
+
+        SecurityPermission[] srvPerms = EMPTY_PERMS;
+        SecurityPermission[] clientPerms = EMPTY_PERMS;
+
+        if (initiator == Initiator.SERVER)
+            srvPerms = F.concat(srvPerms, permissions);
+        else if (initiator == Initiator.CLIENT)
+            srvPerms = permissions;
+        else if (initiator == Initiator.THIN_CLIENT)
+            clientPerms = permissions;
+        else if (initiator == Initiator.REMOTE_CONTROL) {
+            clientPerms = permissions;
+
+            cfg.setConnectorConfiguration(new ConnectorConfiguration());
+        }
+
+        SecurityPermissionSetBuilder secBuilder = 
create().defaultAllowAll(false)
+            .appendSystemPermissions(F.concat(srvPerms, JOIN_AS_SERVER));
+
+        TestSecurityPluginProvider secPlugin = new TestSecurityPluginProvider(
+            instanceName,
+            "",
+            secBuilder.build(),
+            false,
+            new TestSecurityData(
+                "client",
+                "",
+                
create().defaultAllowAll(false).appendSystemPermissions(clientPerms).build(),
+                new Permissions()
+            )
+        );
+
+        cfg.setPluginProviders(secPlugin);
+
+        cfg.setClusterStateOnStart(INACTIVE);
+
+        return cfg;
+    }
+
+    /**
+     * Tests that all state changes are allowed with the permission.
+     */
+    @Test
+    public void testActivationDeactivationAllowed() throws Exception {
+        permissions = F.asArray(ADMIN_CLUSTER_STATE);
+
+        doTestChangeState(
+            F.asArray(ACTIVE_READ_ONLY, INACTIVE, ACTIVE_READ_ONLY, ACTIVE, 
INACTIVE, ACTIVE, ACTIVE_READ_ONLY),
+            true
+        );
+    }
+
+    /**
+     * Tests that the activation is not allowed without the permission.
+     */
+    @Test
+    public void testActivationNotAllowed() throws Exception {
+        doTestChangeState(F.asArray(ACTIVE), false);
+    }
+
+    /**
+     * Tests that the deactivation is not allowed without the permission.
+     */
+    @Test
+    public void testDeactivationNotAllowed() throws Exception {
+        startAllAllowedNode();
+
+        doTestChangeState(F.asArray(INACTIVE), false);
+    }
+
+    /**
+     * Tests that same-state change is not allowed without the permission.
+     */
+    @Test
+    public void testSameStateNotAllowedWithoutPermission() throws Exception {
+        startAllAllowedNode();

Review Comment:
   If you remove `startAllAllowedNode()` from 
`testSameStateNotAllowedWithoutPermission()`, it becomes 
`testActivationNotAllowed()`, But that it other test. 
`testSameStateNotAllowedWithoutPermission()` sets ACTIVE stame from node, where 
it is allowed. And check that a SecurityException is throw from other node, 
where the permission is not set even if you pass same state. 
`testActivationNotAllowed()` checks that activation in not possible from 
INACTIVE state without the permission.



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

Reply via email to