|
| 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.util; |
| 19 | + |
| 20 | +import java.security.Permissions; |
| 21 | +import org.apache.ignite.Ignite; |
| 22 | +import org.apache.ignite.configuration.IgniteConfiguration; |
| 23 | +import org.apache.ignite.events.EventType; |
| 24 | +import org.apache.ignite.internal.processors.security.impl.TestSecurityData; |
| 25 | +import org.apache.ignite.internal.processors.security.impl.TestSecurityPluginProvider; |
| 26 | +import org.apache.ignite.internal.util.typedef.F; |
| 27 | +import org.apache.ignite.internal.util.typedef.G; |
| 28 | +import org.apache.ignite.internal.util.typedef.internal.U; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_OK; |
| 32 | +import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_UNEXPECTED_ERROR; |
| 33 | +import static org.apache.ignite.internal.commandline.SecurityCommandHandlerPermissionsTest.DEFAULT_PWD; |
| 34 | +import static org.apache.ignite.internal.commandline.SecurityCommandHandlerPermissionsTest.TEST_NO_PERMISSIONS_LOGIN; |
| 35 | +import static org.apache.ignite.internal.commandline.SecurityCommandHandlerPermissionsTest.enrichWithConnectionArguments; |
| 36 | +import static org.apache.ignite.plugin.security.SecurityPermission.ADMIN_OPS; |
| 37 | +import static org.apache.ignite.plugin.security.SecurityPermission.EVENTS_DISABLE; |
| 38 | +import static org.apache.ignite.plugin.security.SecurityPermission.EVENTS_ENABLE; |
| 39 | +import static org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.ALL_PERMISSIONS; |
| 40 | +import static org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.NO_PERMISSIONS; |
| 41 | +import static org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.systemPermissions; |
| 42 | +import static org.junit.Assume.assumeTrue; |
| 43 | + |
| 44 | +/** */ |
| 45 | +public class GridCommandHandlerEventTest extends GridCommandHandlerAbstractTest { |
| 46 | + /** */ |
| 47 | + private static final String LOGIN_EVT_ENABLE = "test_login_evt_enable"; |
| 48 | + |
| 49 | + /** */ |
| 50 | + private static final String LOGIN_EVT_DISABLE = "test_login_evt_disable"; |
| 51 | + |
| 52 | + /** */ |
| 53 | + private static final String LOGIN_EVT_STATUS = "test_login_evt_status"; |
| 54 | + |
| 55 | + /** */ |
| 56 | + @Override protected void afterTest() throws Exception { |
| 57 | + super.afterTest(); |
| 58 | + |
| 59 | + stopAllGrids(); |
| 60 | + } |
| 61 | + |
| 62 | + /** */ |
| 63 | + @Test |
| 64 | + public void testEventEnableDIsable() throws Exception { |
| 65 | + startGrids(2); |
| 66 | + startClientGrid("client"); |
| 67 | + |
| 68 | + assertEvents(false, EventType.EVT_CACHE_STARTED, EventType.EVT_CACHE_STOPPED); |
| 69 | + |
| 70 | + assertEquals(EXIT_CODE_OK, execute("--event", "enable", "EVT_CACHE_STARTED,EVT_CACHE_STOPPED")); |
| 71 | + |
| 72 | + assertEvents(true, EventType.EVT_CACHE_STARTED, EventType.EVT_CACHE_STOPPED); |
| 73 | + |
| 74 | + injectTestSystemOut(); |
| 75 | + |
| 76 | + assertEquals(EXIT_CODE_OK, execute("--event", "status")); |
| 77 | + |
| 78 | + String out = testOut.toString(); |
| 79 | + |
| 80 | + assertTrue(out.contains("EVT_CACHE_STARTED")); |
| 81 | + assertTrue(out.contains("EVT_CACHE_STOPPED")); |
| 82 | + |
| 83 | + assertEquals(EXIT_CODE_OK, execute("--event", "disable", "EVT_CACHE_STARTED,EVT_CACHE_STOPPED")); |
| 84 | + |
| 85 | + assertEvents(false, EventType.EVT_CACHE_STARTED, EventType.EVT_CACHE_STOPPED); |
| 86 | + } |
| 87 | + |
| 88 | + /** */ |
| 89 | + @Test |
| 90 | + public void testDifferentSetOfEventsOnNodes() throws Exception { |
| 91 | + startGrid(getConfiguration(getTestIgniteInstanceName(0)) |
| 92 | + .setIncludeEventTypes(EventType.EVT_TX_STARTED, EventType.EVT_CACHE_STARTED)); |
| 93 | + |
| 94 | + startGrid(getConfiguration(getTestIgniteInstanceName(1)) |
| 95 | + .setIncludeEventTypes(EventType.EVT_TX_STARTED, EventType.EVT_CACHE_STOPPED)); |
| 96 | + |
| 97 | + injectTestSystemOut(); |
| 98 | + |
| 99 | + assertEquals(EXIT_CODE_OK, execute("--event", "status")); |
| 100 | + |
| 101 | + String out = testOut.toString(); |
| 102 | + |
| 103 | + assertTrue(out.contains("EVT_TX_STARTED")); |
| 104 | + assertTrue(out.contains("Warning: Event EVT_CACHE_STARTED is enabled only on part of nodes")); |
| 105 | + assertTrue(out.contains("Warning: Event EVT_CACHE_STOPPED is enabled only on part of nodes")); |
| 106 | + } |
| 107 | + |
| 108 | + /** */ |
| 109 | + @Test |
| 110 | + public void testUnknownEvent() throws Exception { |
| 111 | + startGrids(2); |
| 112 | + |
| 113 | + assertEquals(EXIT_CODE_UNEXPECTED_ERROR, execute("--event", "enable", "EVT_NO_SUCH_EVENT")); |
| 114 | + } |
| 115 | + |
| 116 | + /** */ |
| 117 | + @Test |
| 118 | + public void testEventList() throws Exception { |
| 119 | + startGrids(2); |
| 120 | + |
| 121 | + injectTestSystemOut(); |
| 122 | + |
| 123 | + assertEquals(EXIT_CODE_OK, execute("--event", "list")); |
| 124 | + |
| 125 | + String out = testOut.toString(); |
| 126 | + |
| 127 | + for (String evt : U.gridEventNames().values()) |
| 128 | + assertTrue(out.contains("EVT_" + evt)); |
| 129 | + } |
| 130 | + |
| 131 | + /** */ |
| 132 | + @Test |
| 133 | + public void testEventWithSecurity() throws Exception { |
| 134 | + assumeTrue(cliCommandHandler()); |
| 135 | + |
| 136 | + startGrid(getConfigurationWithSecurity(getTestIgniteInstanceName(0))); |
| 137 | + startGrid(getConfigurationWithSecurity(getTestIgniteInstanceName(1))); |
| 138 | + |
| 139 | + assertExecuteWithSecurity(LOGIN_EVT_ENABLE, "--event", "enable", "EVT_CACHE_STARTED,EVT_CACHE_STOPPED"); |
| 140 | + |
| 141 | + assertEvents(true, EventType.EVT_CACHE_STARTED, EventType.EVT_CACHE_STOPPED); |
| 142 | + |
| 143 | + assertExecuteWithSecurity(LOGIN_EVT_STATUS, "--event", "status"); |
| 144 | + |
| 145 | + assertExecuteWithSecurity(LOGIN_EVT_DISABLE, "--event", "disable", "EVT_CACHE_STARTED,EVT_CACHE_STOPPED"); |
| 146 | + |
| 147 | + assertEvents(false, EventType.EVT_CACHE_STARTED, EventType.EVT_CACHE_STOPPED); |
| 148 | + } |
| 149 | + |
| 150 | + /** */ |
| 151 | + private IgniteConfiguration getConfigurationWithSecurity(String igniteInstanceName) throws Exception { |
| 152 | + IgniteConfiguration cfg = getConfiguration(igniteInstanceName); |
| 153 | + |
| 154 | + cfg.setPluginProviders( |
| 155 | + new TestSecurityPluginProvider( |
| 156 | + igniteInstanceName, |
| 157 | + DEFAULT_PWD, |
| 158 | + ALL_PERMISSIONS, |
| 159 | + false, |
| 160 | + new TestSecurityData(TEST_NO_PERMISSIONS_LOGIN, DEFAULT_PWD, NO_PERMISSIONS, new Permissions()), |
| 161 | + new TestSecurityData(LOGIN_EVT_ENABLE, DEFAULT_PWD, systemPermissions(EVENTS_ENABLE), new Permissions()), |
| 162 | + new TestSecurityData(LOGIN_EVT_DISABLE, DEFAULT_PWD, systemPermissions(EVENTS_DISABLE), new Permissions()), |
| 163 | + new TestSecurityData(LOGIN_EVT_STATUS, DEFAULT_PWD, systemPermissions(ADMIN_OPS), new Permissions())) |
| 164 | + ); |
| 165 | + |
| 166 | + return cfg; |
| 167 | + } |
| 168 | + |
| 169 | + /** */ |
| 170 | + private void assertExecuteWithSecurity(String allowedLogin, String... cmdArgs) { |
| 171 | + assertEquals(EXIT_CODE_UNEXPECTED_ERROR, execute(enrichWithConnectionArguments(F.asList(cmdArgs), |
| 172 | + TEST_NO_PERMISSIONS_LOGIN))); |
| 173 | + |
| 174 | + assertEquals(EXIT_CODE_OK, execute(enrichWithConnectionArguments(F.asList(cmdArgs), allowedLogin))); |
| 175 | + } |
| 176 | + |
| 177 | + /** */ |
| 178 | + private void assertEvents(boolean enabled, int... evts) { |
| 179 | + for (Ignite ignite : G.allGrids()) { |
| 180 | + for (int i = 0; i < evts.length; i++) |
| 181 | + assertEquals(enabled, ignite.events().isEnabled(evts[i])); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments