This is an automated email from the ASF dual-hosted git repository.

alexpl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new 3569a02  IGNITE-12283 Getting proxied components of Ignite through the 
Ignition interface inside the sandbox - Fixes #7159.
3569a02 is described below

commit 3569a02903750d019c0f685a3e4a0d154b63b00f
Author: sbt-garus-dg <[email protected]>
AuthorDate: Tue Apr 28 14:33:06 2020 +0300

    IGNITE-12283 Getting proxied components of Ignite through the Ignition 
interface inside the sandbox - Fixes #7159.
    
    Signed-off-by: Aleksey Plekhanov <[email protected]>
---
 .../src/main/java/org/apache/ignite/Ignition.java  |  55 ++++--
 .../security/IgniteSecurityProcessor.java          |   7 +
 .../processors/security/SecurityUtils.java         |  17 ++
 .../sandbox/IgnitionComponentProxyTest.java        | 200 +++++++++++++++++++++
 .../ignite/testsuites/SecurityTestSuite.java       |   4 +-
 5 files changed, 271 insertions(+), 12 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/Ignition.java 
b/modules/core/src/main/java/org/apache/ignite/Ignition.java
index a8c0904..d0ae268 100644
--- a/modules/core/src/main/java/org/apache/ignite/Ignition.java
+++ b/modules/core/src/main/java/org/apache/ignite/Ignition.java
@@ -19,19 +19,26 @@ package org.apache.ignite;
 
 import java.io.InputStream;
 import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
 import java.util.List;
 import java.util.Objects;
 import java.util.UUID;
+import java.util.stream.Collectors;
 import org.apache.ignite.client.ClientException;
 import org.apache.ignite.client.IgniteClient;
 import org.apache.ignite.configuration.ClientConfiguration;
 import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.internal.IgnitionEx;
 import org.apache.ignite.internal.client.thin.TcpIgniteClient;
+import 
org.apache.ignite.internal.processors.security.sandbox.SandboxIgniteComponentProxy;
+import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.thread.IgniteThread;
 import org.jetbrains.annotations.Nullable;
 
+import static 
org.apache.ignite.internal.processors.security.SecurityUtils.isInsideSandbox;
+
 /**
  * This class defines a factory for the main Ignite API. It controls Grid life 
cycle
  * and allows listening for grid events.
@@ -300,7 +307,7 @@ public class Ignition {
      */
     public static Ignite start() throws IgniteException {
         try {
-            return IgnitionEx.start();
+            return wrapToProxyIfNeeded(IgnitionEx.start());
         }
         catch (IgniteCheckedException e) {
             throw U.convertException(e);
@@ -318,7 +325,7 @@ public class Ignition {
      */
     public static Ignite start(IgniteConfiguration cfg) throws IgniteException 
{
         try {
-            return IgnitionEx.start(cfg);
+            return wrapToProxyIfNeeded(IgnitionEx.start(cfg));
         }
         catch (IgniteCheckedException e) {
             throw U.convertException(e);
@@ -343,7 +350,7 @@ public class Ignition {
      */
     public static Ignite start(String springCfgPath) throws IgniteException {
         try {
-            return IgnitionEx.start(springCfgPath);
+            return wrapToProxyIfNeeded(IgnitionEx.start(springCfgPath));
         }
         catch (IgniteCheckedException e) {
             throw U.convertException(e);
@@ -368,7 +375,7 @@ public class Ignition {
      */
     public static Ignite start(URL springCfgUrl) throws IgniteException {
         try {
-            return IgnitionEx.start(springCfgUrl);
+            return wrapToProxyIfNeeded(IgnitionEx.start(springCfgUrl));
         }
         catch (IgniteCheckedException e) {
             throw U.convertException(e);
@@ -393,7 +400,7 @@ public class Ignition {
      */
     public static Ignite start(InputStream springCfgStream) throws 
IgniteException {
         try {
-            return IgnitionEx.start(springCfgStream);
+            return wrapToProxyIfNeeded(IgnitionEx.start(springCfgStream));
         }
         catch (IgniteCheckedException e) {
             throw U.convertException(e);
@@ -409,7 +416,7 @@ public class Ignition {
      */
     public static Ignite getOrStart(IgniteConfiguration cfg) throws 
IgniteException {
         try {
-            return IgnitionEx.start(cfg, false);
+            return wrapToProxyIfNeeded(IgnitionEx.start(cfg, false));
         }
         catch (IgniteCheckedException e) {
             throw U.convertException(e);
@@ -481,7 +488,7 @@ public class Ignition {
      *      initialized or grid instance was stopped or was not started.
      */
     public static Ignite ignite() throws IgniteIllegalStateException {
-        return IgnitionEx.grid();
+        return wrapToProxyIfNeeded(IgnitionEx.grid());
     }
 
     /**
@@ -490,7 +497,15 @@ public class Ignition {
      * @return List of all grids started so far.
      */
     public static List<Ignite> allGrids() {
-        return IgnitionEx.allGrids();
+        List<Ignite> res = IgnitionEx.allGrids();
+
+        if (F.isEmpty(res))
+            return res;
+
+        if (isInsideSandbox())
+            return 
res.stream().map(Ignition::wrapToProxy).collect(Collectors.toList());
+
+        return res;
     }
 
     /**
@@ -506,7 +521,7 @@ public class Ignition {
      *      initialized or grid instance was stopped or was not started.
      */
     public static Ignite ignite(UUID locNodeId) throws 
IgniteIllegalStateException {
-        return IgnitionEx.grid(locNodeId);
+        return wrapToProxyIfNeeded(IgnitionEx.grid(locNodeId));
     }
 
     /**
@@ -523,7 +538,7 @@ public class Ignition {
      *      initialized or Ignite instance was stopped or was not started.
      */
     public static Ignite ignite(@Nullable String name) throws 
IgniteIllegalStateException {
-        return IgnitionEx.grid(name);
+        return wrapToProxyIfNeeded(IgnitionEx.grid(name));
     }
 
     /**
@@ -538,7 +553,25 @@ public class Ignition {
      * @throws IllegalArgumentException Thrown if current thread is not an 
{@link IgniteThread}.
      */
     public static Ignite localIgnite() throws IgniteIllegalStateException, 
IllegalArgumentException {
-        return IgnitionEx.localIgnite();
+        return wrapToProxyIfNeeded(IgnitionEx.localIgnite());
+    }
+
+    /**
+     * @param ignite Ignite.
+     * @return Ignite component proxy.
+     */
+    private static Ignite wrapToProxy(Ignite ignite) {
+        return AccessController.doPrivileged((PrivilegedAction<Ignite>)
+            () -> SandboxIgniteComponentProxy.proxy(Ignite.class, ignite)
+        );
+    }
+
+    /**
+     * @param ignite Ignite.
+     * @return Ignite component proxy if the Ignite Sandbox is enabled.
+     */
+    private static Ignite wrapToProxyIfNeeded(Ignite ignite) {
+        return isInsideSandbox() ? wrapToProxy(ignite) : ignite;
     }
 
     /**
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/security/IgniteSecurityProcessor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/security/IgniteSecurityProcessor.java
index 060c5d1..65e8c8f 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/security/IgniteSecurityProcessor.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/security/IgniteSecurityProcessor.java
@@ -73,6 +73,13 @@ public class IgniteSecurityProcessor implements 
IgniteSecurity, GridProcessor {
     /** Number of started nodes with the sandbox enabled. */
     private static final AtomicInteger SANDBOXED_NODES_COUNTER = new 
AtomicInteger();
 
+    /**
+     * @return True if there are nodes with the sandbox enabled.
+     */
+    static boolean hasSandboxedNodes() {
+        return SANDBOXED_NODES_COUNTER.get() > 0;
+    }
+
     /** Current security context. */
     private final ThreadLocal<SecurityContext> curSecCtx = 
ThreadLocal.withInitial(this::localSecurityContext);
 
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/security/SecurityUtils.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/security/SecurityUtils.java
index c83b1d3..4f3302d 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/security/SecurityUtils.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/security/SecurityUtils.java
@@ -20,9 +20,11 @@ package org.apache.ignite.internal.processors.security;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
+import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.AllPermission;
 import java.security.Permissions;
+import java.security.PrivilegedAction;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.Arrays;
@@ -37,6 +39,7 @@ import org.apache.ignite.cluster.ClusterNode;
 import org.apache.ignite.internal.GridInternalWrapper;
 import org.apache.ignite.internal.GridKernalContext;
 import org.apache.ignite.internal.IgniteNodeAttributes;
+import 
org.apache.ignite.internal.processors.security.sandbox.IgniteDomainCombiner;
 import org.apache.ignite.internal.processors.security.sandbox.IgniteSandbox;
 import org.apache.ignite.internal.util.typedef.internal.A;
 import org.apache.ignite.internal.util.typedef.internal.U;
@@ -186,6 +189,20 @@ public class SecurityUtils {
     }
 
     /**
+     * @return True if current thread runs inside the Ignite Sandbox.
+     */
+    public static boolean isInsideSandbox() {
+        if (!IgniteSecurityProcessor.hasSandboxedNodes())
+            return false;
+
+        final AccessControlContext ctx = AccessController.getContext();
+
+        return AccessController.doPrivileged((PrivilegedAction<Boolean>)
+            () -> ctx.getDomainCombiner() instanceof IgniteDomainCombiner
+        );
+    }
+
+    /**
      * @return Proxy of {@code instance} if the sandbox is enabled and class 
of {@code instance} is not a system type
      * otherwise {@code instance}.
      */
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/security/sandbox/IgnitionComponentProxyTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/sandbox/IgnitionComponentProxyTest.java
new file mode 100644
index 0000000..ba9d988
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/security/sandbox/IgnitionComponentProxyTest.java
@@ -0,0 +1,200 @@
+/*
+ * 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.sandbox;
+
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.AllPermission;
+import java.security.Permissions;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedExceptionAction;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.UUID;
+import java.util.function.Supplier;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.Ignition;
+import 
org.apache.ignite.internal.processors.security.impl.TestSecurityPluginProvider;
+import org.apache.ignite.internal.util.typedef.G;
+import org.junit.Test;
+
+import static 
org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.ALLOW_ALL;
+
+/**
+ * If the Ignite Sandbox is enabled, then {@link Ignition} methods that return 
existing instances of Ignite should
+ * return a component proxy.
+ */
+public class IgnitionComponentProxyTest extends AbstractSandboxTest {
+    /** Server node name. */
+    private static final String SRV = "sandbox.IgnitionComponentProxyTest";
+
+    /** Client node name. */
+    private static final String CLNT = "clnt";
+
+    /** {@inheritDoc} */
+    @Override protected void prepareCluster() throws Exception {
+        startGrid(SRV, ALLOW_ALL, false);
+        startGrid(CLNT, ALLOW_ALL, true);
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testLocalIgnite() {
+        Supplier<Ignite> s = new Supplier<Ignite>() {
+            @Override public Ignite get() {
+                return Ignition.localIgnite();
+            }
+        };
+
+        check(s);
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testGetOrStart() {
+        Supplier<Ignite> s = new Supplier<Ignite>() {
+            @Override public Ignite get() {
+                try {
+                    return 
Ignition.getOrStart(Ignition.localIgnite().configuration());
+                }
+                catch (Exception e) {
+                    throw new IgniteException(e);
+                }
+            }
+        };
+
+        check(s);
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testIgniteWithName() {
+        Supplier<Ignite> s = new Supplier<Ignite>() {
+            @Override public Ignite get() {
+                return Ignition.ignite(SRV);
+            }
+        };
+
+        check(s);
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testIgniteWithID() {
+        final UUID srvId = grid(SRV).localNode().id();
+
+        Supplier<Ignite> s = new Supplier<Ignite>() {
+            @Override public Ignite get() {
+                return Ignition.ignite(srvId);
+            }
+        };
+
+        check(s);
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testAllGrids() {
+        Supplier<Collection<Ignite>> s = new Supplier<Collection<Ignite>>() {
+            @Override public Collection<Ignite> get() {
+                return Ignition.allGrids();
+            }
+        };
+
+        checkCollection(s);
+    }
+
+    /**
+     *
+     */
+    @Test
+    public void testStart() {
+        Supplier<Ignite> s = new Supplier<Ignite>() {
+            @Override public Ignite get() {
+                try {
+                    Permissions perms = new Permissions();
+
+                    perms.add(new AllPermission());
+
+                    AccessControlContext acc = AccessController.doPrivileged(
+                        (PrivilegedAction<AccessControlContext>)() -> new 
AccessControlContext(AccessController.getContext(),
+                            new IgniteDomainCombiner(perms)));
+
+                    return 
AccessController.doPrivileged((PrivilegedExceptionAction<Ignite>)
+                        () -> {
+                            String login = "node_" + G.allGrids().size();
+
+                            return Ignition.start(
+                                optimize(getConfiguration(login,
+                                    new TestSecurityPluginProvider(login, "", 
ALLOW_ALL, perms, globalAuth)))
+                            );
+                        }, acc);
+                }
+                catch (Exception e) {
+                    throw new IgniteException(e);
+                }
+            }
+        };
+
+        check(s);
+    }
+
+    /**
+     * @param s Supplier that returns an instance of Ignite.
+     */
+    private void check(final Supplier<Ignite> s) {
+        Supplier<Collection<Ignite>> supplier = new 
Supplier<Collection<Ignite>>() {
+            @Override public Collection<Ignite> get() {
+                return Collections.singleton(s.get());
+            }
+        };
+
+        checkCollection(supplier);
+    }
+
+    /**
+     * @param s Supplier that returns a collection of Ignite instances.
+     */
+    private void checkCollection(Supplier<Collection<Ignite>> s) {
+        Ignite srv = grid(SRV);
+
+        Ignite clnt = grid(CLNT);
+
+        // Checks that inside the sandbox we should get a proxied instance of 
Ignite.
+        clnt.compute(clnt.cluster().forNodeId(srv.cluster().localNode().id()))
+            .broadcast(() -> {
+                Collection<Ignite> nodes = s.get();
+
+                for (Ignite node : nodes) {
+                    // AccessControlException will be thrown if a non-proxy 
instance of Ignite runs compute.
+                    node.compute().call(() -> true);
+                }
+            });
+    }
+}
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testsuites/SecurityTestSuite.java
 
b/modules/core/src/test/java/org/apache/ignite/testsuites/SecurityTestSuite.java
index 3d894cf..252dc2a 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testsuites/SecurityTestSuite.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testsuites/SecurityTestSuite.java
@@ -50,6 +50,7 @@ import 
org.apache.ignite.internal.processors.security.sandbox.ComputeSandboxTest
 import 
org.apache.ignite.internal.processors.security.sandbox.DataStreamerSandboxTest;
 import 
org.apache.ignite.internal.processors.security.sandbox.DoPrivilegedOnRemoteNodeTest;
 import 
org.apache.ignite.internal.processors.security.sandbox.IgniteOperationsInsideSandboxTest;
+import 
org.apache.ignite.internal.processors.security.sandbox.IgnitionComponentProxyTest;
 import 
org.apache.ignite.internal.processors.security.sandbox.SecuritySubjectPermissionsTest;
 import org.apache.ignite.ssl.MultipleSSLContextsTest;
 import org.junit.runner.RunWith;
@@ -93,8 +94,9 @@ import org.junit.runners.Suite;
     ComputeSandboxTest.class,
     DoPrivilegedOnRemoteNodeTest.class,
     IgniteOperationsInsideSandboxTest.class,
-    SecuritySubjectPermissionsTest.class,
     AccessToClassesInsideInternalPackageTest.class,
+    SecuritySubjectPermissionsTest.class,
+    IgnitionComponentProxyTest.class,
     IgniteSecurityProcessorTest.class,
     GridCommandHandlerSslWithSecurityTest.class,
     MultipleSSLContextsTest.class

Reply via email to