|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.internal.processors.security; |
| 19 | + |
| 20 | +import java.security.Permissions; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | +import java.util.function.Function; |
| 27 | +import com.google.common.collect.ImmutableSet; |
| 28 | +import org.apache.ignite.Ignition; |
| 29 | +import org.apache.ignite.client.ClientAuthorizationException; |
| 30 | +import org.apache.ignite.client.ClientCache; |
| 31 | +import org.apache.ignite.client.ClientException; |
| 32 | +import org.apache.ignite.client.IgniteClient; |
| 33 | +import org.apache.ignite.client.IgniteClientFuture; |
| 34 | +import org.apache.ignite.configuration.CacheConfiguration; |
| 35 | +import org.apache.ignite.configuration.ClientConfiguration; |
| 36 | +import org.apache.ignite.configuration.ClientConnectorConfiguration; |
| 37 | +import org.apache.ignite.configuration.IgniteConfiguration; |
| 38 | +import org.apache.ignite.internal.IgniteEx; |
| 39 | +import org.apache.ignite.internal.IgniteInternalFuture; |
| 40 | +import org.apache.ignite.internal.TestRecordingCommunicationSpi; |
| 41 | +import org.apache.ignite.internal.processors.cache.distributed.GridCacheModuloAffinityFunction; |
| 42 | +import org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsSingleMessage; |
| 43 | +import org.apache.ignite.internal.processors.security.impl.TestSecurityData; |
| 44 | +import org.apache.ignite.internal.processors.security.impl.TestSecurityPluginProvider; |
| 45 | +import org.apache.ignite.internal.util.typedef.X; |
| 46 | +import org.apache.ignite.plugin.security.SecurityPermissionSet; |
| 47 | +import org.apache.ignite.testframework.GridTestUtils; |
| 48 | +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; |
| 49 | +import org.junit.Test; |
| 50 | +import org.junit.runner.RunWith; |
| 51 | +import org.junit.runners.Parameterized; |
| 52 | + |
| 53 | +import static java.util.Collections.singletonMap; |
| 54 | +import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL; |
| 55 | +import static org.apache.ignite.internal.processors.cache.distributed.GridCacheModuloAffinityFunction.IDX_ATTR; |
| 56 | +import static org.apache.ignite.plugin.security.SecurityPermission.ADMIN_CLUSTER_STATE; |
| 57 | +import static org.apache.ignite.plugin.security.SecurityPermission.CACHE_CREATE; |
| 58 | +import static org.apache.ignite.plugin.security.SecurityPermission.CACHE_PUT; |
| 59 | +import static org.apache.ignite.plugin.security.SecurityPermission.CACHE_READ; |
| 60 | +import static org.apache.ignite.plugin.security.SecurityPermission.CACHE_REMOVE; |
| 61 | +import static org.apache.ignite.plugin.security.SecurityPermission.JOIN_AS_SERVER; |
| 62 | +import static org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.NO_PERMISSIONS; |
| 63 | +import static org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.create; |
| 64 | + |
| 65 | +/** */ |
| 66 | +@RunWith(Parameterized.class) |
| 67 | +public class SecurityContextInternalFuturePropagationTest extends GridCommonAbstractTest { |
| 68 | + /** */ |
| 69 | + private static final int PRELOADED_KEY_CNT = 100; |
| 70 | + |
| 71 | + /** */ |
| 72 | + private static final AtomicInteger KEY_CNTR = new AtomicInteger(); |
| 73 | + |
| 74 | + /** */ |
| 75 | + @Parameterized.Parameter() |
| 76 | + public Function<ClientCache<Object, Object>, IgniteClientFuture<?>> op; |
| 77 | + |
| 78 | + /** */ |
| 79 | + @Parameterized.Parameters() |
| 80 | + public static List<Function<ClientCache<Object, Object>, IgniteClientFuture<?>>> data() { |
| 81 | + return Arrays.asList( |
| 82 | + cache -> cache.getAllAsync(ImmutableSet.of(nextKey(), nextKey())), // 0 |
| 83 | + cache -> cache.getAndPutAsync(nextKey(), 0), // 1 |
| 84 | + cache -> cache.getAndPutIfAbsentAsync(nextKey(), 0), // 2 |
| 85 | + cache -> cache.getAndPutIfAbsentAsync(PRELOADED_KEY_CNT + nextKey(), 0), // 3 |
| 86 | + cache -> cache.getAndRemoveAsync(nextKey()), // 4 |
| 87 | + cache -> cache.getAndReplaceAsync(nextKey(), 0), // 5 |
| 88 | + cache -> cache.getAsync(nextKey()), // 6 |
| 89 | + cache -> cache.putAllAsync(new HashMap<>() {{ put(nextKey(), 0); put(nextKey(), 0); }}), // 7 |
| 90 | + cache -> cache.putAsync(nextKey(), 0), // 8 |
| 91 | + cache -> cache.putIfAbsentAsync(PRELOADED_KEY_CNT + nextKey(), 0), // 9 |
| 92 | + cache -> cache.removeAsync(nextKey()), // 10 |
| 93 | + cache -> { |
| 94 | + int key = nextKey(); |
| 95 | + return cache.removeAsync(key, key); |
| 96 | + }, // 11 |
| 97 | + cache -> cache.replaceAsync(nextKey(), 0), // 12 |
| 98 | + cache -> { |
| 99 | + int key = nextKey(); |
| 100 | + |
| 101 | + return cache.replaceAsync(key, key, 0); |
| 102 | + } // 13 |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /** {@inheritDoc} */ |
| 107 | + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { |
| 108 | + return super.getConfiguration(igniteInstanceName) |
| 109 | + .setCommunicationSpi(new TestRecordingCommunicationSpi()) |
| 110 | + .setUserAttributes(singletonMap(IDX_ATTR, getTestIgniteInstanceIndex(igniteInstanceName))) |
| 111 | + .setClientConnectorConfiguration(new ClientConnectorConfiguration() |
| 112 | + .setThreadPoolSize(1)) |
| 113 | + .setPluginProviders(new TestSecurityPluginProvider( |
| 114 | + igniteInstanceName, |
| 115 | + "", |
| 116 | + create() |
| 117 | + .defaultAllowAll(false) |
| 118 | + .appendSystemPermissions(JOIN_AS_SERVER, ADMIN_CLUSTER_STATE) |
| 119 | + .appendCachePermissions(DEFAULT_CACHE_NAME, CACHE_CREATE) |
| 120 | + .build(), |
| 121 | + null, |
| 122 | + false, |
| 123 | + userData("forbidden_client", NO_PERMISSIONS), |
| 124 | + userData("allowed_client", create() |
| 125 | + .defaultAllowAll(false) |
| 126 | + .appendCachePermissions(DEFAULT_CACHE_NAME, CACHE_READ, CACHE_PUT, CACHE_REMOVE) |
| 127 | + .build()) |
| 128 | + )); |
| 129 | + } |
| 130 | + |
| 131 | + /** {@inheritDoc} */ |
| 132 | + @Override protected void afterTest() throws Exception { |
| 133 | + super.afterTest(); |
| 134 | + |
| 135 | + stopAllGrids(); |
| 136 | + } |
| 137 | + |
| 138 | + /** */ |
| 139 | + @Test |
| 140 | + public void testSecurityContextInternalFuturePropagation() throws Exception { |
| 141 | + IgniteEx ignite = startGrids(2); |
| 142 | + |
| 143 | + prepareCache(ignite); |
| 144 | + |
| 145 | + TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi.spi(grid(1)); |
| 146 | + |
| 147 | + spi.blockMessages(GridDhtPartitionsSingleMessage.class, ignite.name()); |
| 148 | + |
| 149 | + IgniteInternalFuture<IgniteEx> joinFut = GridTestUtils.runAsync(() -> startGrid(2)); |
| 150 | + |
| 151 | + spi.waitForBlocked(); |
| 152 | + |
| 153 | + try ( |
| 154 | + IgniteClient allowedCli = startClient("allowed_client"); |
| 155 | + IgniteClient forbiddenCli = startClient("forbidden_client") |
| 156 | + ) { |
| 157 | + ClientCache<Object, Object> allowedCliCache = allowedCli.cache(DEFAULT_CACHE_NAME); |
| 158 | + ClientCache<Object, Object> forbiddenCliCache = forbiddenCli.cache(DEFAULT_CACHE_NAME); |
| 159 | + |
| 160 | + IgniteClientFuture<?> op0Fut = op.apply(allowedCliCache); |
| 161 | + |
| 162 | + // The following operation previously was executed with security context associated with the joining node user. |
| 163 | + IgniteClientFuture<?> op1Fut = op.apply(allowedCliCache); |
| 164 | + |
| 165 | + // Simulates failure of a chained operation. |
| 166 | + IgniteClientFuture<?> op2Fut = op.apply(forbiddenCliCache); |
| 167 | + |
| 168 | + // The failure of one operation in a chain should not leave the entire chain in a broken state. |
| 169 | + IgniteClientFuture<?> op3Fut = op.apply(allowedCliCache); |
| 170 | + |
| 171 | + spi.stopBlock(); |
| 172 | + |
| 173 | + joinFut.get(getTestTimeout(), TimeUnit.MILLISECONDS); |
| 174 | + |
| 175 | + op0Fut.get(getTestTimeout(), TimeUnit.MILLISECONDS); |
| 176 | + op1Fut.get(getTestTimeout(), TimeUnit.MILLISECONDS); |
| 177 | + |
| 178 | + try { |
| 179 | + op2Fut.get(getTestTimeout(), TimeUnit.MILLISECONDS); |
| 180 | + |
| 181 | + fail(); |
| 182 | + } |
| 183 | + catch (Exception e) { |
| 184 | + assertTrue(X.hasCause(e, "Authorization failed", ClientException.class) |
| 185 | + || X.hasCause(e, "User is not authorized to perform this operation", ClientAuthorizationException.class)); |
| 186 | + } |
| 187 | + |
| 188 | + op3Fut.get(getTestTimeout(), TimeUnit.MILLISECONDS); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** */ |
| 193 | + private void prepareCache(IgniteEx ignite) throws Exception { |
| 194 | + ignite.createCache(new CacheConfiguration<>() |
| 195 | + .setName(DEFAULT_CACHE_NAME) |
| 196 | + .setAtomicityMode(TRANSACTIONAL) |
| 197 | + .setReadFromBackup(false) |
| 198 | + .setBackups(1) |
| 199 | + .setAffinity(new GridCacheModuloAffinityFunction(2, 1))); |
| 200 | + |
| 201 | + awaitPartitionMapExchange(); |
| 202 | + |
| 203 | + try (IgniteClient cli = startClient("allowed_client")) { |
| 204 | + for (int i = 0; i < PRELOADED_KEY_CNT; i++) |
| 205 | + cli.cache(DEFAULT_CACHE_NAME).put(i, i); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + /** */ |
| 210 | + private static int nextKey() { |
| 211 | + return KEY_CNTR.incrementAndGet(); |
| 212 | + } |
| 213 | + |
| 214 | + /** */ |
| 215 | + private static IgniteClient startClient(String login) { |
| 216 | + return Ignition.startClient(new ClientConfiguration() |
| 217 | + .setAddresses("127.0.0.1:10800") |
| 218 | + .setUserName(login) |
| 219 | + .setUserPassword("")); |
| 220 | + } |
| 221 | + |
| 222 | + /** */ |
| 223 | + private static TestSecurityData userData(String login, SecurityPermissionSet perms) { |
| 224 | + return new TestSecurityData( |
| 225 | + login, |
| 226 | + "", |
| 227 | + perms, |
| 228 | + new Permissions() |
| 229 | + ); |
| 230 | + } |
| 231 | +} |
0 commit comments