This is an automated email from the ASF dual-hosted git repository.
namelchev 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 eaabf74e5a5 IGNITE-18980 Fixed REST status code in case of failed
request (#10586)
eaabf74e5a5 is described below
commit eaabf74e5a531adcdc09046e6dba3ef8cbc28548
Author: Nikita Amelchev <[email protected]>
AuthorDate: Fri Mar 10 10:19:42 2023 +0300
IGNITE-18980 Fixed REST status code in case of failed request (#10586)
---
.../rest/JettyRestProcessorAbstractSelfTest.java | 13 +++-
.../processors/rest/GridRestProcessor.java | 37 ++++------
.../rest/handlers/GridRestCommandHandler.java | 3 +-
.../handlers/GridRestCommandHandlerAdapter.java | 19 -----
.../cluster/GridBaselineCommandHandler.java | 86 ++++++++++------------
.../GridChangeClusterStateCommandHandler.java | 46 ++++--------
.../cluster/GridChangeStateCommandHandler.java | 43 ++++-------
.../cluster/GridClusterNameCommandHandler.java | 18 +----
.../handlers/user/UserActionCommandHandler.java | 43 +++++------
.../http/jetty/RestProcessorAuthorizationTest.java | 81 +++++++++++++++++---
10 files changed, 190 insertions(+), 199 deletions(-)
diff --git
a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
index db910230266..f57688657e9 100644
---
a/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
+++
b/modules/clients/src/test/java/org/apache/ignite/internal/processors/rest/JettyRestProcessorAbstractSelfTest.java
@@ -286,10 +286,17 @@ public abstract class JettyRestProcessorAbstractSelfTest
extends JettyRestProces
else
assertTrue("Unexpected error: " + errNode.asText(),
errNode.isNull());
- assertEquals(STATUS_SUCCESS, node.get("successStatus").asInt());
+ if (errorExpected)
+ assertEquals(STATUS_FAILED, node.get("successStatus").asInt());
+ else
+ assertEquals(STATUS_SUCCESS, node.get("successStatus").asInt());
- if (!canBeUnauthenticated)
- assertNotSame(securityEnabled(),
node.get("sessionToken").isNull());
+ if (!canBeUnauthenticated) {
+ if (errorExpected)
+ assertTrue(node.get("sessionToken").isNull());
+ else
+ assertNotSame(securityEnabled(),
node.get("sessionToken").isNull());
+ }
return node.get(errorExpected ? "error" : "response");
}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
index 94c4eae1a4b..aaaa527c0ea 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/GridRestProcessor.java
@@ -17,8 +17,6 @@
package org.apache.ignite.internal.processors.rest;
-import java.io.PrintWriter;
-import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
@@ -322,11 +320,19 @@ public class GridRestProcessor extends
GridProcessorAdapter implements IgniteRes
GridRestCommandHandler hnd = handlers.get(req.command());
- IgniteInternalFuture<GridRestResponse> res = hnd == null ? null :
hnd.handleAsync(req);
-
- if (res == null)
+ if (hnd == null) {
return new GridFinishedFuture<>(
new IgniteCheckedException("Failed to find registered handler
for command: " + req.command()));
+ }
+
+ IgniteInternalFuture<GridRestResponse> res;
+
+ try {
+ res = hnd.handleAsync(req);
+ }
+ catch (Exception e) {
+ res = new GridFinishedFuture<>(e);
+ }
return res.chain(new C1<IgniteInternalFuture<GridRestResponse>,
GridRestResponse>() {
@Override public GridRestResponse
apply(IgniteInternalFuture<GridRestResponse> f) {
@@ -340,7 +346,9 @@ public class GridRestProcessor extends GridProcessorAdapter
implements IgniteRes
catch (Exception e) {
failed = true;
- if (X.hasCause(e, IllegalArgumentException.class)) {
+ if (X.hasCause(e, SecurityException.class))
+ res = new
GridRestResponse(STATUS_SECURITY_CHECK_FAILED, e.getMessage());
+ else if (X.hasCause(e, IllegalArgumentException.class)) {
IllegalArgumentException iae = X.cause(e,
IllegalArgumentException.class);
res = new GridRestResponse(STATUS_ILLEGAL_ARGUMENT,
iae.getMessage());
@@ -367,7 +375,7 @@ public class GridRestProcessor extends GridProcessorAdapter
implements IgniteRes
sb.a(", err=")
.a(e.getMessage() != null ? e.getMessage() :
e.getClass().getName())
.a(", trace=")
- .a(getErrorMessage(e))
+ .a(X.getFullStackTrace(e))
.a(']');
res = new GridRestResponse(STATUS_FAILED,
sb.toString());
@@ -386,21 +394,6 @@ public class GridRestProcessor extends
GridProcessorAdapter implements IgniteRes
});
}
- /**
- * @param th Th.
- * @return Stack trace
- */
- private String getErrorMessage(Throwable th) {
- if (th == null)
- return "";
-
- StringWriter writer = new StringWriter();
-
- th.printStackTrace(new PrintWriter(writer));
-
- return writer.toString();
- }
-
/**
* @param req Request.
* @return Not null session.
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/GridRestCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/GridRestCommandHandler.java
index 7f6d22d8852..d50b92a3dbc 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/GridRestCommandHandler.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/GridRestCommandHandler.java
@@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.rest.handlers;
import java.util.Collection;
+import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.processors.rest.GridRestCommand;
import org.apache.ignite.internal.processors.rest.GridRestResponse;
@@ -36,5 +37,5 @@ public interface GridRestCommandHandler {
* @param req Request.
* @return Future.
*/
- public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest
req);
+ public IgniteInternalFuture<GridRestResponse> handleAsync(GridRestRequest
req) throws IgniteCheckedException;
}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/GridRestCommandHandlerAdapter.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/GridRestCommandHandlerAdapter.java
index aeec0333aff..6a87f1ea59f 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/GridRestCommandHandlerAdapter.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/GridRestCommandHandlerAdapter.java
@@ -19,8 +19,6 @@ package org.apache.ignite.internal.processors.rest.handlers;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.internal.GridKernalContext;
-import org.apache.ignite.internal.util.typedef.X;
-import org.apache.ignite.internal.util.typedef.internal.SB;
/**
* Abstract command handler.
@@ -50,21 +48,4 @@ public abstract class GridRestCommandHandlerAdapter
implements GridRestCommandHa
protected static String missingParameter(String param) {
return "Failed to find mandatory parameter in request: " + param;
}
-
- /**
- * Converts exception to string representation for error in response.
- *
- * @param e Exception.
- * @return String representation of exception for error in response.
- */
- protected static String errorMessage(Exception e) {
- SB sb = new SB();
-
- sb.a(e.getMessage()).a("\n").a("suppressed: \n");
-
- for (Throwable t : X.getSuppressedList(e))
- sb.a(t.getMessage()).a("\n");
-
- return sb.toString();
- }
}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridBaselineCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridBaselineCommandHandler.java
index edd13f1a995..d7d702c34ee 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridBaselineCommandHandler.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridBaselineCommandHandler.java
@@ -65,7 +65,8 @@ public class GridBaselineCommandHandler extends
GridRestCommandHandlerAdapter {
}
/** {@inheritDoc} */
- @Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest req) {
+ @Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest req)
+ throws IgniteCheckedException {
assert req != null;
assert SUPPORTED_COMMANDS.contains(req.command());
@@ -75,74 +76,65 @@ public class GridBaselineCommandHandler extends
GridRestCommandHandlerAdapter {
log.debug("Handling baseline REST request: " + req);
GridRestBaselineRequest req0 = (GridRestBaselineRequest)req;
-
- try {
- IgniteClusterEx cluster = ctx.grid().cluster();
- List<Object> consistentIds = req0.consistentIds();
+ IgniteClusterEx cluster = ctx.grid().cluster();
- switch (req0.command()) {
- case BASELINE_CURRENT_STATE: {
- // No-op.
+ List<Object> consistentIds = req0.consistentIds();
- break;
- }
+ switch (req0.command()) {
+ case BASELINE_CURRENT_STATE: {
+ // No-op.
- case BASELINE_SET: {
- Long topVer = req0.topologyVersion();
+ break;
+ }
- if (topVer == null && consistentIds == null)
- throw new IgniteCheckedException("Failed to handle
request (either topVer or consistentIds should be specified).");
+ case BASELINE_SET: {
+ Long topVer = req0.topologyVersion();
- if (topVer != null)
- cluster.setBaselineTopology(topVer);
- else
-
cluster.setBaselineTopology(filterServerNodesByConsId(consistentIds));
+ if (topVer == null && consistentIds == null)
+ throw new IgniteCheckedException("Failed to handle request
(either topVer or consistentIds should be specified).");
- break;
- }
+ if (topVer != null)
+ cluster.setBaselineTopology(topVer);
+ else
+
cluster.setBaselineTopology(filterServerNodesByConsId(consistentIds));
- case BASELINE_ADD: {
- if (consistentIds == null)
- throw new
IgniteCheckedException(missingParameter("consistentIds"));
+ break;
+ }
- Set<BaselineNode> baselineTop = new
HashSet<>(currentBaseLine());
+ case BASELINE_ADD: {
+ if (consistentIds == null)
+ throw new
IgniteCheckedException(missingParameter("consistentIds"));
-
baselineTop.addAll(filterServerNodesByConsId(consistentIds));
+ Set<BaselineNode> baselineTop = new
HashSet<>(currentBaseLine());
- cluster.setBaselineTopology(baselineTop);
+ baselineTop.addAll(filterServerNodesByConsId(consistentIds));
- break;
- }
+ cluster.setBaselineTopology(baselineTop);
- case BASELINE_REMOVE: {
- if (consistentIds == null)
- throw new
IgniteCheckedException(missingParameter("consistentIds"));
+ break;
+ }
- Collection<BaselineNode> baseline = currentBaseLine();
+ case BASELINE_REMOVE: {
+ if (consistentIds == null)
+ throw new
IgniteCheckedException(missingParameter("consistentIds"));
- Set<BaselineNode> baselineTop = new HashSet<>(baseline);
+ Collection<BaselineNode> baseline = currentBaseLine();
- baselineTop.removeAll(filterNodesByConsId(baseline,
consistentIds));
+ Set<BaselineNode> baselineTop = new HashSet<>(baseline);
- cluster.setBaselineTopology(baselineTop);
+ baselineTop.removeAll(filterNodesByConsId(baseline,
consistentIds));
- break;
- }
+ cluster.setBaselineTopology(baselineTop);
- default:
- assert false : "Invalid command for baseline handler: " +
req;
+ break;
}
- return new GridFinishedFuture<>(new
GridRestResponse(currentState()));
- }
- catch (IgniteCheckedException e) {
- return new GridFinishedFuture<>(e);
- }
- finally {
- if (log.isDebugEnabled())
- log.debug("Handled baseline REST request: " + req);
+ default:
+ assert false : "Invalid command for baseline handler: " + req;
}
+
+ return new GridFinishedFuture<>(new GridRestResponse(currentState()));
}
/**
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridChangeClusterStateCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridChangeClusterStateCommandHandler.java
index a72fee2f4dd..0fa3bd20f9c 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridChangeClusterStateCommandHandler.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridChangeClusterStateCommandHandler.java
@@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.rest.handlers.cluster;
import java.util.Collection;
+import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.processors.rest.GridRestCommand;
@@ -25,7 +26,7 @@ import
org.apache.ignite.internal.processors.rest.GridRestResponse;
import
org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandlerAdapter;
import
org.apache.ignite.internal.processors.rest.request.GridRestClusterStateRequest;
import org.apache.ignite.internal.processors.rest.request.GridRestRequest;
-import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.future.GridFinishedFuture;
import org.apache.ignite.internal.util.typedef.internal.U;
import static
org.apache.ignite.internal.processors.rest.GridRestCommand.CLUSTER_SET_STATE;
@@ -51,43 +52,26 @@ public class GridChangeClusterStateCommandHandler extends
GridRestCommandHandler
}
/** {@inheritDoc} */
- @Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest restReq) {
+ @Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest restReq)
+ throws IgniteCheckedException {
GridRestClusterStateRequest req = (GridRestClusterStateRequest)restReq;
- final GridFutureAdapter<GridRestResponse> fut = new
GridFutureAdapter<>();
+ switch (req.command()) {
+ case CLUSTER_STATE:
+ assert req.isReqCurrentMode() : req;
- final GridRestResponse res = new GridRestResponse();
+ return new GridFinishedFuture<>(new
GridRestResponse(ctx.grid().cluster().state()));
- try {
- switch (req.command()) {
- case CLUSTER_STATE:
- assert req.isReqCurrentMode() : req;
+ default:
+ assert req.state() != null : req;
- res.setResponse(ctx.grid().cluster().state());
+ U.log(log, "Received cluster state change request to " +
req.state() +
+ " state from client node with ID: " + req.clientId());
- break;
+ ctx.state().changeGlobalState(req.state(),
req.forceDeactivation(),
+ ctx.cluster().get().forServers().nodes(), false).get();
- default:
- assert req.state() != null : req;
-
- U.log(log, "Received cluster state change request to " +
req.state() +
- " state from client node with ID: " + req.clientId());
-
- ctx.state().changeGlobalState(req.state(),
req.forceDeactivation(),
- ctx.cluster().get().forServers().nodes(), false).get();
-
- res.setResponse(req.command().key() + " done");
-
- break;
- }
-
- fut.onDone(res);
- }
- catch (Exception e) {
- res.setError(errorMessage(e));
-
- fut.onDone(res);
+ return new GridFinishedFuture<>(new
GridRestResponse(req.command().key() + " done"));
}
- return fut;
}
}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridChangeStateCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridChangeStateCommandHandler.java
index e492398fee4..c5dce31320c 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridChangeStateCommandHandler.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridChangeStateCommandHandler.java
@@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.rest.handlers.cluster;
import java.util.Collection;
+import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.processors.rest.GridRestCommand;
@@ -25,7 +26,7 @@ import
org.apache.ignite.internal.processors.rest.GridRestResponse;
import
org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandlerAdapter;
import
org.apache.ignite.internal.processors.rest.request.GridRestChangeStateRequest;
import org.apache.ignite.internal.processors.rest.request.GridRestRequest;
-import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.future.GridFinishedFuture;
import org.apache.ignite.internal.util.typedef.internal.U;
import static org.apache.ignite.cluster.ClusterState.ACTIVE;
@@ -57,38 +58,24 @@ public class GridChangeStateCommandHandler extends
GridRestCommandHandlerAdapter
}
/** {@inheritDoc} */
- @Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest restRest) {
+ @Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest restRest)
+ throws IgniteCheckedException {
GridRestChangeStateRequest req = (GridRestChangeStateRequest)restRest;
- final GridFutureAdapter<GridRestResponse> fut = new
GridFutureAdapter<>();
+ switch (req.command()) {
+ case CLUSTER_CURRENT_STATE:
+ Boolean currentState = ctx.state().publicApiActiveState(false);
- final GridRestResponse res = new GridRestResponse();
+ return new GridFinishedFuture<>(new
GridRestResponse(currentState));
- try {
- switch (req.command()) {
- case CLUSTER_CURRENT_STATE:
- Boolean currentState =
ctx.state().publicApiActiveState(false);
+ case CLUSTER_ACTIVE:
+ case CLUSTER_INACTIVE:
+ log.warning(req.command().key() + " is deprecated. Use newer
commands.");
+ default:
+ ctx.state().changeGlobalState(req.active() ? ACTIVE :
INACTIVE, req.forceDeactivation(),
+ ctx.cluster().get().forServers().nodes(), false).get();
- res.setResponse(currentState);
- break;
- case CLUSTER_ACTIVE:
- case CLUSTER_INACTIVE:
- log.warning(req.command().key() + " is deprecated. Use
newer commands.");
- default:
- ctx.state().changeGlobalState(req.active() ? ACTIVE :
INACTIVE, req.forceDeactivation(),
- ctx.cluster().get().forServers().nodes(), false).get();
-
- res.setResponse(req.command().key() + " started");
- break;
- }
-
- fut.onDone(res);
- }
- catch (Exception e) {
- res.setError(errorMessage(e));
-
- fut.onDone(res);
+ return new GridFinishedFuture<>(new
GridRestResponse(req.command().key() + " started"));
}
- return fut;
}
}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridClusterNameCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridClusterNameCommandHandler.java
index 2eca474cecd..6f7c338aaa4 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridClusterNameCommandHandler.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/cluster/GridClusterNameCommandHandler.java
@@ -25,7 +25,7 @@ import
org.apache.ignite.internal.processors.rest.GridRestResponse;
import
org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandlerAdapter;
import
org.apache.ignite.internal.processors.rest.request.GridRestClusterNameRequest;
import org.apache.ignite.internal.processors.rest.request.GridRestRequest;
-import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.future.GridFinishedFuture;
import org.apache.ignite.internal.util.typedef.internal.U;
import static
org.apache.ignite.internal.processors.rest.GridRestCommand.CLUSTER_NAME;
@@ -53,20 +53,8 @@ public class GridClusterNameCommandHandler extends
GridRestCommandHandlerAdapter
@Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest restReq) {
assert restReq instanceof GridRestClusterNameRequest : restReq;
- final GridFutureAdapter<GridRestResponse> fut = new
GridFutureAdapter<>();
+ String name = ctx.cluster().clusterName();
- final GridRestResponse res = new GridRestResponse();
-
- try {
- res.setResponse(ctx.cluster().clusterName());
-
- fut.onDone(res);
- }
- catch (Exception e) {
- res.setError(errorMessage(e));
-
- fut.onDone(res);
- }
- return fut;
+ return new GridFinishedFuture<>(new GridRestResponse(name));
}
}
diff --git
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/user/UserActionCommandHandler.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/user/UserActionCommandHandler.java
index bb4341c035c..79e68003c31 100644
---
a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/user/UserActionCommandHandler.java
+++
b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/user/UserActionCommandHandler.java
@@ -18,6 +18,7 @@
package org.apache.ignite.internal.processors.rest.handlers.user;
import java.util.Collection;
+import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.IgniteInternalFuture;
import org.apache.ignite.internal.processors.rest.GridRestCommand;
@@ -53,7 +54,8 @@ public class UserActionCommandHandler extends
GridRestCommandHandlerAdapter {
}
/** {@inheritDoc} */
- @Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest req) {
+ @Override public IgniteInternalFuture<GridRestResponse>
handleAsync(GridRestRequest req)
+ throws IgniteCheckedException {
assert req != null;
if (log.isDebugEnabled())
@@ -61,34 +63,27 @@ public class UserActionCommandHandler extends
GridRestCommandHandlerAdapter {
RestUserActionRequest req0 = (RestUserActionRequest)req;
- try {
- GridRestCommand cmd = req.command();
+ GridRestCommand cmd = req.command();
- IgniteSecurity security = ctx.security();
+ IgniteSecurity security = ctx.security();
- switch (cmd) {
- case ADD_USER:
- security.createUser(req0.user(),
req0.password().toCharArray());
- break;
+ switch (cmd) {
+ case ADD_USER:
+ security.createUser(req0.user(),
req0.password().toCharArray());
+ break;
- case REMOVE_USER:
- security.dropUser(req0.user());
- break;
+ case REMOVE_USER:
+ security.dropUser(req0.user());
+ break;
- case UPDATE_USER:
- security.alterUser(req0.user(),
req0.password().toCharArray());
- break;
- }
-
- if (log.isDebugEnabled())
- log.debug("Handled topology REST request [req=" + req + ']');
-
- return new GridFinishedFuture<>(new GridRestResponse(true));
+ case UPDATE_USER:
+ security.alterUser(req0.user(), req0.password().toCharArray());
+ break;
}
- catch (Throwable e) {
- log.error("Failed to handle REST request [req=" + req + ']', e);
- return new GridFinishedFuture<>(e);
- }
+ if (log.isDebugEnabled())
+ log.debug("Handled topology REST request [req=" + req + ']');
+
+ return new GridFinishedFuture<>(new GridRestResponse(true));
}
}
diff --git
a/modules/rest-http/src/test/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/RestProcessorAuthorizationTest.java
b/modules/rest-http/src/test/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/RestProcessorAuthorizationTest.java
index 7831f2e8cb7..961fd602dd4 100644
---
a/modules/rest-http/src/test/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/RestProcessorAuthorizationTest.java
+++
b/modules/rest-http/src/test/java/org/apache/ignite/internal/processors/rest/protocols/http/jetty/RestProcessorAuthorizationTest.java
@@ -24,11 +24,15 @@ import java.net.URLConnection;
import java.security.Permissions;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
+import java.util.Map;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ignite.cluster.ClusterState;
import org.apache.ignite.internal.GridKernalContext;
import org.apache.ignite.internal.IgniteEx;
import org.apache.ignite.internal.processors.rest.GridRestCommand;
+import org.apache.ignite.internal.processors.rest.GridRestResponse;
import org.apache.ignite.internal.processors.security.GridSecurityProcessor;
import org.apache.ignite.internal.processors.security.SecurityContext;
import
org.apache.ignite.internal.processors.security.client.CommonSecurityCheckTest;
@@ -42,8 +46,14 @@ import org.apache.ignite.plugin.security.SecurityException;
import org.apache.ignite.plugin.security.SecurityPermission;
import org.junit.Test;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
import static
org.apache.ignite.internal.processors.cache.CacheGetRemoveSkipStoreTest.TEST_CACHE;
+import static
org.apache.ignite.internal.processors.rest.GridRestCommand.CLUSTER_ACTIVATE;
+import static
org.apache.ignite.internal.processors.rest.GridRestCommand.CLUSTER_SET_STATE;
+import static
org.apache.ignite.internal.processors.rest.GridRestCommand.DESTROY_CACHE;
+import static
org.apache.ignite.internal.processors.rest.GridRestCommand.GET_OR_CREATE_CACHE;
import static
org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.ALL_PERMISSIONS;
+import static
org.apache.ignite.plugin.security.SecurityPermissionSetBuilder.NO_PERMISSIONS;
/**
* Tests REST processor authorization commands GET_OR_CREATE_CACHE /
DESTROY_CACHE.
@@ -52,6 +62,9 @@ public class RestProcessorAuthorizationTest extends
CommonSecurityCheckTest {
/** */
private static final String LOGIN = "login";
+ /** */
+ private static final String LOGIN_NO_PERMISSIONS = "login_no_permissions";
+
/** */
private static final String PWD = "pwd";
@@ -96,11 +109,11 @@ public class RestProcessorAuthorizationTest extends
CommonSecurityCheckTest {
public void testCacheCreateDestroyPermission() throws Exception {
IgniteEx ignite = startGrid(0);
- ignite.cluster().state(ClusterState.ACTIVE);
+ ignite.cluster().state(ACTIVE);
assertNull(ignite.cache(TEST_CACHE));
- executeCommand(GridRestCommand.GET_OR_CREATE_CACHE, LOGIN, PWD);
+ executeCommand(LOGIN, GET_OR_CREATE_CACHE, F.asMap("cacheName",
TEST_CACHE));
GridTuple3<String, SecurityPermission, SecurityContext> ctx =
authorizationCtxList.get(0);
@@ -112,7 +125,7 @@ public class RestProcessorAuthorizationTest extends
CommonSecurityCheckTest {
authorizationCtxList.clear();
- executeCommand(GridRestCommand.DESTROY_CACHE, LOGIN, PWD);
+ executeCommand(LOGIN, DESTROY_CACHE, F.asMap("cacheName", TEST_CACHE));
ctx = authorizationCtxList.get(0);
@@ -123,23 +136,73 @@ public class RestProcessorAuthorizationTest extends
CommonSecurityCheckTest {
assertNull(ignite.cache(TEST_CACHE));
}
+ /** @throws Exception if failed. */
+ @Test
+ public void testClusterStateChange() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ assertEquals(ClusterState.INACTIVE, ignite.cluster().state());
+
+ GridRestResponse res = executeCommand(LOGIN_NO_PERMISSIONS,
CLUSTER_SET_STATE, F.asMap("state", ACTIVE.name()));
+
+ assertEquals(GridRestResponse.STATUS_SECURITY_CHECK_FAILED,
res.getSuccessStatus());
+
+ assertEquals(ClusterState.INACTIVE, ignite.cluster().state());
+
+ res = executeCommand(LOGIN, CLUSTER_SET_STATE, F.asMap("state",
ACTIVE.name()));
+
+ assertEquals(GridRestResponse.STATUS_SUCCESS, res.getSuccessStatus());
+
+ assertEquals(ACTIVE, ignite.cluster().state());
+ }
+
+ /** @throws Exception if failed. */
+ @Test
+ public void testOldClusterStateChange() throws Exception {
+ IgniteEx ignite = startGrid(0);
+
+ assertEquals(ClusterState.INACTIVE, ignite.cluster().state());
+
+ GridRestResponse res = executeCommand(LOGIN_NO_PERMISSIONS,
CLUSTER_ACTIVATE, Collections.emptyMap());
+
+ assertEquals(GridRestResponse.STATUS_SECURITY_CHECK_FAILED,
res.getSuccessStatus());
+
+ assertEquals(ClusterState.INACTIVE, ignite.cluster().state());
+
+ res = executeCommand(LOGIN, CLUSTER_ACTIVATE, Collections.emptyMap());
+
+ assertEquals(GridRestResponse.STATUS_SUCCESS, res.getSuccessStatus());
+
+ assertEquals(ACTIVE, ignite.cluster().state());
+ }
+
/** */
- private void executeCommand(GridRestCommand cmd, String login, String pwd)
throws IOException {
- String addr = "http://localhost:8080/ignite?cmd=" + cmd.key()
- + "&cacheName=" + TEST_CACHE
- + "&ignite.login=" + login + "&ignite.password=" + pwd;
+ private GridRestResponse executeCommand(
+ String login,
+ GridRestCommand cmd,
+ Map<String, String> params
+ ) throws IOException {
+ StringBuilder addr = new
StringBuilder("http://localhost:8080/ignite?cmd=").append(cmd.key())
+ .append("&ignite.login=").append(login)
+ .append("&ignite.password=").append(PWD);
+
+ for (Map.Entry<String, String> e : params.entrySet())
+
addr.append("&").append(e.getKey()).append("=").append(e.getValue());
- URL url = new URL(addr);
+ URL url = new URL(addr.toString());
URLConnection conn = url.openConnection();
conn.connect();
assertEquals(200, ((HttpURLConnection)conn).getResponseCode());
+
+ return new ObjectMapper().readValue(conn.getInputStream(),
GridRestResponse.class);
}
/** {@inheritDoc} */
@Override protected TestSecurityData[] clientData() {
- return new TestSecurityData[] {new TestSecurityData(LOGIN, PWD,
ALL_PERMISSIONS, new Permissions())};
+ return new TestSecurityData[] {new TestSecurityData(LOGIN, PWD,
ALL_PERMISSIONS, new Permissions()),
+ new TestSecurityData(LOGIN_NO_PERMISSIONS, PWD, NO_PERMISSIONS,
new Permissions())};
}
}