Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add security on MQTT resources #25

Merged
merged 1 commit into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mqtt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
</dependency>

<!--TESTS-->
<dependency>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-web-security</artifactId>
<version>${seed.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seedstack.seed</groupId>
<artifactId>seed-testing</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.seedstack.mqtt.spi.MqttInfo;
import org.seedstack.mqtt.spi.MqttPoolConfiguration;
import org.seedstack.seed.rest.Rel;
import org.seedstack.seed.security.RequiresPermissions;

import javax.inject.Inject;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class ClientResource {
@GET
@Rel(value = Rels.CLIENTS, home = true)
@Produces({MediaType.APPLICATION_JSON, "application/hal+json"})
@RequiresPermissions("seed:monitoring:mqtt:read")
public Response getClients() {
if (mqttInfo.getClientNames() != null && !mqttInfo.getClientNames().isEmpty()) {
return clientListRepresentation();
Expand All @@ -59,6 +61,7 @@ public Response getClients() {
@Path("/{clientId}")
@Rel(value = Rels.CLIENT)
@Produces({MediaType.APPLICATION_JSON, "application/hal+json"})
@RequiresPermissions("seed:monitoring:mqtt:read")
public Response getClient() {
boolean clientExists = clientExists(clientId);
if (clientExists) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#
# Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

[org.seedstack.seed.security.permissions]
seed-monitoring-mqtt.reader = seed:monitoring:mqtt:read
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

public class ClientResourceTest extends AbstractSeedWebIT {

private static final String LOGIN = "reader";

private static final String PASSWORD = "password";

@ArquillianResource
private URL baseURL;

Expand All @@ -48,22 +52,22 @@ public static WebArchive createDeployment() {
@RunAsClient
@Test
public void testGetClients() {
Response response = expect().statusCode(200).when().get(baseURL.toString() + "seed-monitoring/mqtt/clients");
Response response = expect().statusCode(200).given().auth().basic(LOGIN, PASSWORD).when().get(baseURL.toString() + "seed-monitoring/mqtt/clients");
int resultSize = from(response.asString()).get("resultSize");
assertThat(resultSize).isEqualTo(1);
}

@RunAsClient
@Test
public void testGetClient() {
Response response = expect().statusCode(200).when().get(baseURL.toString() + "seed-monitoring/mqtt/clients/client_test");
Response response = expect().statusCode(200).given().auth().basic(LOGIN, PASSWORD).when().get(baseURL.toString() + "seed-monitoring/mqtt/clients/client_test");
String clientId = from(response.asString()).get("clientId");
assertThat(clientId).isEqualTo("client_test");
}

@RunAsClient
@Test
public void testGetClientNotFound() {
expect().statusCode(404).when().get(baseURL.toString() + "seed-monitoring/mqtt/clients/client_fake");
expect().statusCode(404).given().auth().basic(LOGIN, PASSWORD).when().get(baseURL.toString() + "seed-monitoring/mqtt/clients/client_fake");
}
}
15 changes: 15 additions & 0 deletions mqtt/src/test/resources/META-INF/configuration/mqtt.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

[org.seedstack.seed.security.urls]
/** = authcBasic

[org.seedstack.seed.security]
realms = ConfigurationRealm

ConfigurationRealm.role-mapping = ConfigurationRoleMapping
ConfigurationRealm.role-permission-resolver = ConfigurationRolePermissionResolver

[org.seedstack.seed.security.users]
reader = password

[org.seedstack.seed.security.roles]
seed-monitoring-mqtt.reader = *

[org.seedstack.mqtt]
clients = client_test

Expand Down