You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2016/05/25 22:27:41 UTC
[19/34] incubator-geode git commit: GEODE-17: create muti-user
security DUnit tests
GEODE-17: create muti-user security DUnit tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/2234535a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/2234535a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/2234535a
Branch: refs/heads/feature/GEODE-835
Commit: 2234535a8d2fa18549f34d25aae00313de174f7d
Parents: caa1d15
Author: Jinmei Liao <ji...@pivotal.io>
Authored: Mon May 23 13:42:05 2016 -0700
Committer: Jinmei Liao <ji...@pivotal.io>
Committed: Tue May 24 07:55:23 2016 -0700
----------------------------------------------------------------------
.../cli/commands/CliCommandTestBase.java | 48 +++++--
.../internal/security/MultiUserDUnitTest.java | 142 +++++++++++++++++++
2 files changed, 179 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/2234535a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/commands/CliCommandTestBase.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/commands/CliCommandTestBase.java b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/commands/CliCommandTestBase.java
index a0966f9..dc8045a 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/commands/CliCommandTestBase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/cli/commands/CliCommandTestBase.java
@@ -39,6 +39,7 @@ import com.gemstone.gemfire.management.internal.cli.parser.CommandTarget;
import com.gemstone.gemfire.management.internal.cli.result.CommandResult;
import com.gemstone.gemfire.management.internal.cli.shell.Gfsh;
import com.gemstone.gemfire.management.internal.cli.util.CommandStringBuilder;
+import com.gemstone.gemfire.management.internal.security.JSONAuthorization;
import com.gemstone.gemfire.test.dunit.Host;
import com.gemstone.gemfire.test.dunit.cache.internal.JUnit4CacheTestCase;
@@ -59,9 +60,9 @@ public abstract class CliCommandTestBase extends JUnit4CacheTestCase {
public static final String USE_HTTP_SYSTEM_PROPERTY = "useHTTP";
private boolean useHttpOnConnect = Boolean.getBoolean(USE_HTTP_SYSTEM_PROPERTY);
- private transient int httpPort;
- private transient int jmxPort;
- private transient String jmxHost;
+ protected transient int httpPort;
+ protected transient int jmxPort;
+ protected transient String jmxHost;
protected transient String gfshDir;
@Rule
@@ -102,13 +103,27 @@ public abstract class CliCommandTestBase extends JUnit4CacheTestCase {
*/
@SuppressWarnings("serial")
protected HeadlessGfsh setUpJmxManagerOnVm0ThenConnect(final Properties props) {
- setUpJMXManagerOnVM(0, props);
+ Object[] result = setUpJMXManagerOnVM(0, props);
+ this.jmxHost = (String) result[0];
+ this.jmxPort = (Integer) result[1];
+ this.httpPort = (Integer) result[2];
connect(this.jmxHost, this.jmxPort, this.httpPort, getDefaultShell());
return shell;
}
- protected void setUpJMXManagerOnVM(int vm, final Properties props) {
- Object[] result = (Object[]) Host.getHost(0).getVM(vm).invoke("setUpJmxManagerOnVm"+vm, () -> {
+ protected Object[] setUpJMXManagerOnVM(int vm, final Properties props) {
+ return setUpJMXManagerOnVM(vm, props, null);
+ }
+
+ /**
+ *
+ * @param vm
+ * @param props
+ * @param jsonFile
+ * @return an object array, result[0] is jmxHost(String), result[1] is jmxPort, result[2] is httpPort
+ */
+ protected Object[] setUpJMXManagerOnVM(int vm, final Properties props, String jsonFile) {
+ Object[] result = Host.getHost(0).getVM(vm).invoke("setUpJmxManagerOnVm"+vm, () -> {
final Object[] results = new Object[3];
final Properties localProps = (props != null ? props : new Properties());
@@ -137,16 +152,17 @@ public abstract class CliCommandTestBase extends JUnit4CacheTestCase {
getSystem(localProps);
verifyManagementServiceStarted(getCache());
+ if(jsonFile!=null){
+ JSONAuthorization.setUpWithJsonFile(jsonFile);
+ }
+
results[0] = jmxHost;
results[1] = jmxPort;
results[2] = httpPort;
-
return results;
});
- this.jmxHost = (String) result[0];
- this.jmxPort = (Integer) result[1];
- this.httpPort = (Integer) result[2];
+ return result;
}
/**
@@ -203,6 +219,10 @@ public abstract class CliCommandTestBase extends JUnit4CacheTestCase {
}
protected void connect(final String host, final int jmxPort, final int httpPort, HeadlessGfsh shell){
+ connect(host, jmxPort, httpPort, shell, null, null);
+ }
+
+ protected void connect(final String host, final int jmxPort, final int httpPort, HeadlessGfsh shell, String username, String password){
final CommandStringBuilder command = new CommandStringBuilder(CliStrings.CONNECT);
String endpoint;
@@ -214,13 +234,19 @@ public abstract class CliCommandTestBase extends JUnit4CacheTestCase {
endpoint = host + "[" + jmxPort + "]";
command.addOption(CliStrings.CONNECT__JMX_MANAGER, endpoint);
}
+ if(username!=null) {
+ command.addOption(CliStrings.CONNECT__USERNAME, username);
+ }
+ if(password!=null){
+ command.addOption(CliStrings.CONNECT__PASSWORD, password);
+ }
System.out.println(getClass().getSimpleName()+" using endpoint: "+endpoint);
CommandResult result = executeCommand(shell, command.toString());
if (!shell.isConnectedAndReady()) {
throw new AssertionError(
- "Connect command failed to connect to manager " + endpoint + " result=" + commandResultToString(result));
+ "Connect command failed to connect to manager " + endpoint + " result=" + commandResultToString(result));
}
info("Successfully connected to managing node using " + (useHttpOnConnect ? "HTTP" : "JMX"));
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/2234535a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MultiUserDUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MultiUserDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MultiUserDUnitTest.java
new file mode 100644
index 0000000..5b58b3c
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/management/internal/security/MultiUserDUnitTest.java
@@ -0,0 +1,142 @@
+/*
+ * 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 com.gemstone.gemfire.management.internal.security;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import com.gemstone.gemfire.distributed.internal.DistributionConfig;
+import com.gemstone.gemfire.internal.logging.LogService;
+import com.gemstone.gemfire.management.cli.Result.Status;
+import com.gemstone.gemfire.management.internal.cli.HeadlessGfsh;
+import com.gemstone.gemfire.management.internal.cli.commands.CliCommandTestBase;
+import com.gemstone.gemfire.management.internal.cli.result.CommandResult;
+import com.gemstone.gemfire.management.internal.cli.result.ErrorResultData;
+import com.gemstone.gemfire.management.internal.cli.result.ResultBuilder;
+import com.gemstone.gemfire.test.dunit.AsyncInvocation;
+import com.gemstone.gemfire.test.dunit.Host;
+import com.gemstone.gemfire.test.dunit.IgnoredException;
+import com.gemstone.gemfire.test.dunit.VM;
+import com.gemstone.gemfire.test.junit.categories.DistributedTest;
+import com.jayway.awaitility.Awaitility;
+
+import org.json.JSONException;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category(DistributedTest.class)
+public class MultiUserDUnitTest extends CliCommandTestBase {
+
+ @Test
+ public void testMultiUser() throws IOException, JSONException, InterruptedException {
+ Properties properties = new Properties();
+ properties.put(DistributionConfig.NAME_NAME, MultiUserDUnitTest.class.getSimpleName());
+ properties.put(DistributionConfig.SECURITY_CLIENT_AUTHENTICATOR_NAME, JSONAuthorization.class.getName() + ".create");
+ properties.put(DistributionConfig.SECURITY_CLIENT_ACCESSOR_NAME, JSONAuthorization.class.getName() + ".create");
+
+ // set up vm_0 the secure jmx manager
+ Object[] results = setUpJMXManagerOnVM(0, properties, "cacheServer.json");
+ String gfshDir = this.gfshDir;
+
+ // set up vm_1 as a gfsh vm, data-reader will login and log out constantly in this vm until the test is done.
+ VM vm1 = Host.getHost(0).getVM(1);
+ AsyncInvocation vm1Invoke = vm1.invokeAsync("run as data-reader", () -> {
+ String shellId = getClass().getSimpleName() + "_vm1";
+ HeadlessGfsh shell = new HeadlessGfsh(shellId, 30, gfshDir);
+ while (true) {
+ connect((String) results[0], (Integer) results[1], (Integer) results[2], shell, "data-reader", "1234567");
+ Awaitility.waitAtMost(5, TimeUnit.MILLISECONDS);
+ shell.executeCommand("disconnect");
+ }
+ });
+
+ VM vm2 = Host.getHost(0).getVM(2);
+ // set up vm_2 as a gfsh vm, and then connect as "stranger" and try to execute the commands and assert errors comes back are NotAuthorized
+ AsyncInvocation vm2Invoke = vm2.invokeAsync("run as guest", () -> {
+ String shellId = getClass().getSimpleName() + "_vm2";
+ HeadlessGfsh shell = new HeadlessGfsh(shellId, 30, gfshDir);
+ connect((String) results[0], (Integer) results[1], (Integer) results[2], shell, "stranger", "1234567");
+
+ List<TestCommand> allCommands = TestCommand.getCommands();
+ for (TestCommand command : allCommands) {
+ LogService.getLogger().info("executing: " + command.getCommand());
+ if (command.getPermission() == null) {
+ continue;
+ }
+
+ CommandResult result = executeCommand(shell, command.getCommand());
+
+ int errorCode = ((ErrorResultData) result.getResultData()).getErrorCode();
+
+ // for some commands there are pre execution checks to check for user input error, will skip those commands
+ if (errorCode == ResultBuilder.ERRORCODE_USER_ERROR) {
+ LogService.getLogger().info("Skip user error: " + result.getContent());
+ continue;
+ }
+
+ assertEquals("Not an expected result: " + result.toString(), ResultBuilder.ERRORCODE_UNAUTHORIZED,
+ ((ErrorResultData) result.getResultData()).getErrorCode());
+ String resultMessage = result.getContent().toString();
+ String permString = command.getPermission().toString();
+ assertTrue(resultMessage + " does not contain " + permString, resultMessage.contains("[" + permString + "]"));
+ }
+ LogService.getLogger().info("vm 2 done!");
+ });
+
+
+ VM vm3 = Host.getHost(0).getVM(3);
+ IgnoredException.addIgnoredException("java.lang.IllegalArgumentException: Region doesnt exist: {0}", vm3);
+ IgnoredException.addIgnoredException("java.lang.ClassNotFoundException: myApp.myListener", vm3);
+
+ // set up vm_3 as another gfsh vm, and then connect as "super-user" and try to execute the commands and assert we don't get a NotAuthorized Exception
+ AsyncInvocation vm3Invoke = vm3.invokeAsync("run as superUser", () -> {
+ String shellId = getClass().getSimpleName() + "_vm3";
+ HeadlessGfsh shell = new HeadlessGfsh(shellId, 30, gfshDir);
+ connect((String) results[0], (Integer) results[1], (Integer) results[2], shell, "super-user", "1234567");
+
+ List<TestCommand> allCommands = TestCommand.getCommands();
+ for (TestCommand command : allCommands) {
+ LogService.getLogger().info("executing: " + command.getCommand());
+ if (command.getPermission() == null) {
+ continue;
+ }
+
+ CommandResult result = executeCommand(shell, command.getCommand());
+ if (result.getResultData().getStatus() == Status.OK) {
+ continue;
+ }
+
+ assertNotEquals("Did not expect an Unauthorized exception: " + result.toString(), ResultBuilder.ERRORCODE_UNAUTHORIZED,
+ ((ErrorResultData) result.getResultData()).getErrorCode());
+ }
+
+ LogService.getLogger().info("vm 3 done!");
+ });
+
+ // only wait until vm2 and vm3 are done. vm1 will close when test finishes
+ vm2Invoke.join();
+ vm3Invoke.join();
+
+ IgnoredException.removeAllExpectedExceptions();
+ }
+
+}