|
| 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.security.cert.Certificate; |
| 22 | +import java.security.cert.X509Certificate; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collection; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.concurrent.ConcurrentLinkedQueue; |
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import org.apache.ignite.Ignite; |
| 29 | +import org.apache.ignite.IgniteCheckedException; |
| 30 | +import org.apache.ignite.cluster.ClusterNode; |
| 31 | +import org.apache.ignite.configuration.IgniteConfiguration; |
| 32 | +import org.apache.ignite.internal.GridKernalContext; |
| 33 | +import org.apache.ignite.internal.IgniteEx; |
| 34 | +import org.apache.ignite.internal.processors.security.impl.TestSecurityData; |
| 35 | +import org.apache.ignite.internal.processors.security.impl.TestSecurityPluginProvider; |
| 36 | +import org.apache.ignite.internal.processors.security.impl.TestSecurityProcessor; |
| 37 | +import org.apache.ignite.internal.util.typedef.G; |
| 38 | +import org.apache.ignite.plugin.security.SecurityCredentials; |
| 39 | +import org.apache.ignite.plugin.security.SecurityPermissionSet; |
| 40 | +import org.apache.ignite.spi.discovery.tcp.internal.TcpDiscoveryNode; |
| 41 | +import org.apache.ignite.testframework.GridTestUtils; |
| 42 | +import org.junit.Test; |
| 43 | + |
| 44 | +import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 45 | +import static org.apache.ignite.events.EventType.EVT_CLIENT_NODE_RECONNECTED; |
| 46 | +import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_NODE_CERTIFICATES; |
| 47 | +import static org.apache.ignite.internal.IgniteNodeAttributes.ATTR_SECURITY_CREDENTIALS; |
| 48 | +import static org.apache.ignite.plugin.security.SecurityPermission.JOIN_AS_SERVER; |
| 49 | +import static org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.NO_PERMISSIONS; |
| 50 | +import static org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.systemPermissions; |
| 51 | + |
| 52 | +/** */ |
| 53 | +public class NodeConnectionCertificateCapturingTest extends AbstractSecurityTest { |
| 54 | + /** */ |
| 55 | + private static final Collection<AuthenticationEvent> NODE_AUTHENTICATION_EVENTS = new ConcurrentLinkedQueue<>(); |
| 56 | + |
| 57 | + /** */ |
| 58 | + @Test |
| 59 | + public void testNodeConnectionCertificateCapturing() throws Exception { |
| 60 | + checkNewNodeAuthenticationByClusterNodes(0, false, 1); |
| 61 | + checkNewNodeAuthenticationByClusterNodes(1, false, 2); |
| 62 | + checkNewNodeAuthenticationByClusterNodes(2, false, 3); |
| 63 | + checkNewNodeAuthenticationByClusterNodes(3, true, 3); |
| 64 | + |
| 65 | + // Checks nodes restart. |
| 66 | + stopGrid(2); |
| 67 | + stopGrid(3); |
| 68 | + |
| 69 | + checkNewNodeAuthenticationByClusterNodes(3, true, 2); |
| 70 | + checkNewNodeAuthenticationByClusterNodes(2, false, 3); |
| 71 | + |
| 72 | + // Checks client node reconnect. |
| 73 | + NODE_AUTHENTICATION_EVENTS.clear(); |
| 74 | + |
| 75 | + CountDownLatch cliNodeReconnectedLatch = new CountDownLatch(1); |
| 76 | + |
| 77 | + grid(3).events().localListen(evt -> { |
| 78 | + cliNodeReconnectedLatch.countDown(); |
| 79 | + |
| 80 | + return true; |
| 81 | + }, EVT_CLIENT_NODE_RECONNECTED); |
| 82 | + |
| 83 | + grid(0).context().discovery().failNode(grid(3).localNode().id(), "test"); |
| 84 | + |
| 85 | + assertTrue(cliNodeReconnectedLatch.await(getTestTimeout(), MILLISECONDS)); |
| 86 | + |
| 87 | + checkNodeAuthenticationByClusterNodes(3, grid(3).localNode().id(), true, 3); |
| 88 | + } |
| 89 | + |
| 90 | + /** */ |
| 91 | + private void checkNewNodeAuthenticationByClusterNodes(int authNodeIdx, boolean isClient, int expAuthCnt) throws Exception { |
| 92 | + NODE_AUTHENTICATION_EVENTS.clear(); |
| 93 | + |
| 94 | + UUID authNodeId = startGrid(authNodeIdx, isClient).cluster().localNode().id(); |
| 95 | + |
| 96 | + checkNodeAuthenticationByClusterNodes(authNodeIdx, authNodeId, isClient, expAuthCnt); |
| 97 | + } |
| 98 | + |
| 99 | + /** */ |
| 100 | + private void checkNodeAuthenticationByClusterNodes(int authNodeIdx, UUID authNodeId, boolean isClient, int expAuthCnt) { |
| 101 | + assertEquals(expAuthCnt, NODE_AUTHENTICATION_EVENTS.size()); |
| 102 | + |
| 103 | + for (AuthenticationEvent auth : NODE_AUTHENTICATION_EVENTS) { |
| 104 | + if (auth.clusterNodeId.equals(authNodeId)) |
| 105 | + assertNull(auth.certs); |
| 106 | + else { |
| 107 | + assertEquals(2, auth.certs.length); |
| 108 | + |
| 109 | + X509Certificate cert = (X509Certificate)auth.certs[0]; |
| 110 | + |
| 111 | + assertEquals(isClient ? "CN=client" : "CN=node0" + (authNodeIdx + 1), cert.getSubjectDN().getName()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + for (Ignite ignite : G.allGrids()) { |
| 116 | + for (ClusterNode node : ignite.cluster().nodes()) { |
| 117 | + assertNull(((TcpDiscoveryNode)node).getAttributes().get(ATTR_SECURITY_CREDENTIALS)); |
| 118 | + assertNull(((TcpDiscoveryNode)node).getAttributes().get(ATTR_NODE_CERTIFICATES)); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** */ |
| 124 | + private IgniteEx startGrid(int idx, boolean isClient) throws Exception { |
| 125 | + String login = getTestIgniteInstanceName(idx); |
| 126 | + |
| 127 | + IgniteConfiguration cfg = getConfiguration( |
| 128 | + login, |
| 129 | + new SecurityPluginProvider( |
| 130 | + login, |
| 131 | + "", |
| 132 | + isClient ? NO_PERMISSIONS : systemPermissions(JOIN_AS_SERVER), |
| 133 | + null, |
| 134 | + true |
| 135 | + )).setClientMode(isClient); |
| 136 | + |
| 137 | + cfg.setSslContextFactory(GridTestUtils.sslTrustedFactory(isClient ? "client" : "node0" + (idx + 1), "trustboth")); |
| 138 | + |
| 139 | + return startGrid(cfg); |
| 140 | + } |
| 141 | + |
| 142 | + /** */ |
| 143 | + private static class SecurityPluginProvider extends TestSecurityPluginProvider { |
| 144 | + /** */ |
| 145 | + SecurityPluginProvider( |
| 146 | + String login, |
| 147 | + String pwd, |
| 148 | + SecurityPermissionSet perms, |
| 149 | + Permissions sandboxPerms, |
| 150 | + boolean globalAuth, |
| 151 | + TestSecurityData... clientData |
| 152 | + ) { |
| 153 | + super(login, pwd, perms, sandboxPerms, globalAuth, clientData); |
| 154 | + } |
| 155 | + |
| 156 | + /** {@inheritDoc} */ |
| 157 | + @Override protected GridSecurityProcessor securityProcessor(GridKernalContext ctx) { |
| 158 | + return new TestSecurityProcessor( |
| 159 | + ctx, |
| 160 | + new TestSecurityData(login, pwd, perms, sandboxPerms), |
| 161 | + Arrays.asList(clientData), |
| 162 | + globalAuth |
| 163 | + ) { |
| 164 | + @Override public SecurityContext authenticateNode( |
| 165 | + ClusterNode node, |
| 166 | + SecurityCredentials cred |
| 167 | + ) throws IgniteCheckedException { |
| 168 | + NODE_AUTHENTICATION_EVENTS.add(new AuthenticationEvent(ctx.localNodeId(), node.attribute(ATTR_NODE_CERTIFICATES))); |
| 169 | + |
| 170 | + return super.authenticateNode(node, cred); |
| 171 | + } |
| 172 | + }; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** */ |
| 177 | + private static class AuthenticationEvent { |
| 178 | + /** */ |
| 179 | + UUID clusterNodeId; |
| 180 | + |
| 181 | + /** */ |
| 182 | + Certificate[] certs; |
| 183 | + |
| 184 | + /** */ |
| 185 | + AuthenticationEvent(UUID clusterNodeId, Certificate[] certs) { |
| 186 | + this.clusterNodeId = clusterNodeId; |
| 187 | + this.certs = certs; |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments