Add Security Sample Guide and Code.

Resolves gh-81.
This commit is contained in:
Patrick Johnson
2020-04-21 16:43:35 -07:00
committed by John Blum
parent 39535538fa
commit a918c9a8f1
13 changed files with 605 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed 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
*
* https://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 example.app.security.client;
import example.app.security.client.model.Customer;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientCache;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.config.annotation.EnableClusterConfiguration;
import org.springframework.data.gemfire.config.annotation.EnableEntityDefinedRegions;
/**
* The {@link BootGeodeSecurityClientApplication} class is a Spring Boot, Apache Geode {@link ClientCache}
* application that configures security.
*
* @author Patrick Johnson
* @see org.apache.geode.cache.client.ClientCache
* @see org.springframework.boot.SpringApplication
* @see org.springframework.boot.autoconfigure.SpringBootApplication
* @see org.springframework.context.annotation.Bean
* @see org.springframework.data.gemfire.config.annotation.ClientCacheApplication
* @since 1.3.0
*/
// tag::class[]
@SpringBootApplication
@EnableClusterConfiguration
@EnableEntityDefinedRegions
public class BootGeodeSecurityClientApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(BootGeodeSecurityClientApplication.class)
.web(WebApplicationType.SERVLET)
.build()
.run(args);
}
@Bean
ApplicationRunner runner(@Qualifier("Customers") Region<Long, Customer> customers) {
return args -> {
customers.put(2L, Customer.newCustomer(2L, "William Evans"));
System.out.println(String.format("Successfully wrote data to region %s", customers.getName()));
try {
System.out.println(String.format("Attempting to read data from region %s", customers.getName()));
customers.get(2L);
} catch (Exception e) {
System.out.println(String.format("Read failed because \"%s\"", e.getCause().getMessage()));
}
};
}
}
// end::class[]

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed 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
*
* https://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 example.app.security.client.controller;
import example.app.security.client.BootGeodeSecurityClientApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* The {@link SecurityController} class is a RestController used by {@link BootGeodeSecurityClientApplication}
*
* @author Patrick Johnson
* @see org.springframework.web.bind.annotation.RestController
* @since 1.3.0
*/
// tag::class[]
@RestController
public class SecurityController {
@Autowired
Environment env;
@GetMapping("/message")
public String getMessage() {
return "I'm using SSL with this keystore: " + env.getProperty("spring.data.gemfire.security.ssl.keystore");
}
}
// end::class[]

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed 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
*
* https://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 example.app.security.client.model;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.Region;
/**
* An Abstract Data Type (ADT) modeling a {@literal Customer}.
*
* @author John Blum
* @see Id
* @see Region
* @since 1.0.0
*/
// tag::class[]
@Region("Customers")
@EqualsAndHashCode
@ToString(of = "name")
@RequiredArgsConstructor(staticName = "newCustomer")
public class Customer {
@Id @NonNull @Getter
private Long id;
@NonNull @Getter
private String name;
}
// end::class[]

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed 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
*
* https://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 example.app.security.server;
import org.apache.geode.cache.server.CacheServer;
import org.apache.geode.internal.security.shiro.GeodePermissionResolver;
import org.apache.shiro.realm.text.PropertiesRealm;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.data.gemfire.config.annotation.CacheServerApplication;
import org.springframework.data.gemfire.config.annotation.EnableSecurity;
/**
* The {@link BootGeodeSecurityServerApplication} class is a Spring Boot, Apache Geode {@literal peer}
* {@link CacheServer} application serving cache clients.
*
* This Apache Geode peer member server configures security.
*
* @author Patrick Johnson
* @see org.apache.geode.cache.Cache
* @see org.apache.geode.cache.Region
* @see org.apache.geode.cache.server.CacheServer
* @see org.springframework.boot.autoconfigure.SpringBootApplication
* @see org.springframework.context.annotation.Bean
* @see org.springframework.data.gemfire.config.annotation.CacheServerApplication
* @since 1.3.0
*/
// tag::class[]
@SpringBootApplication
public class BootGeodeSecurityServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(BootGeodeSecurityServerApplication.class)
.web(WebApplicationType.NONE)
.build()
.run(args);
}
@CacheServerApplication
@EnableSecurity
static class GeodeServerConfig {
// tag::realm[]
@Bean
PropertiesRealm shiroRealm() {
PropertiesRealm propertiesRealm = new PropertiesRealm();
propertiesRealm.setResourcePath("classpath:shiro.properties");
propertiesRealm.setPermissionResolver(new GeodePermissionResolver());
return propertiesRealm;
}
// end::realm[]
}
}
// end::class[]

View File

@@ -0,0 +1,5 @@
# Spring Boot client application.properties
spring.data.gemfire.security.ssl.keystore.password=s3cr3t
spring.data.gemfire.security.username = jdoe
spring.data.gemfire.security.password = p@55w0rd

View File

@@ -0,0 +1,3 @@
# assign user jdoe the role fo viewer and give him DATA:WRITE permissions
user.jdoe = p@55w0rd, viewer
role.viewer = DATA:WRITE

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2020 the original author or authors.
*
* Licensed 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
*
* https://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 example.app.security;
import example.app.security.client.BootGeodeSecurityClientApplication;
import example.app.security.client.model.Customer;
import example.app.security.server.BootGeodeSecurityServerApplication;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ServerOperationException;
import org.apache.geode.security.NotAuthorizedException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.gemfire.tests.integration.ClientServerIntegrationTestsSupport;
import org.springframework.data.gemfire.tests.process.ProcessWrapper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* Integration Tests for {@link BootGeodeSecurityClientApplication} and {@link BootGeodeSecurityServerApplication}.
*
* @author Patrick Johsnon
* @see org.springframework.boot.test.context.SpringBootTest
* @see org.springframework.data.gemfire.tests.integration.IntegrationTestsSupport
* @see org.springframework.test.context.junit4.SpringRunner
* @see example.app.security.client.BootGeodeSecurityClientApplication
* @since 1.3.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BootGeodeSecurityClientApplication.class)
public class BootGeodeSecurityClientApplicationIntegrationTests extends ClientServerIntegrationTestsSupport {
private static ProcessWrapper geodeServer;
@Resource(name = "Customers")
private Region<Long, Customer> customers;
@BeforeClass
public static void setup() throws IOException {
int availablePort = findAvailablePort();
geodeServer = run(BootGeodeSecurityServerApplication.class,
String.format("-D%s=%d", GEMFIRE_CACHE_SERVER_PORT_PROPERTY, availablePort));
waitForServerToStart("localhost", availablePort);
System.setProperty(GEMFIRE_POOL_SERVERS_PROPERTY, String.format("%s[%d]", "localhost", availablePort));
}
@Test
public void dataReadNotAllowed() {
Exception exception = assertThrows(ServerOperationException.class, () -> customers.get(2L).getName());
assertThat(exception.getCause()).isInstanceOf(NotAuthorizedException.class);
assertThat(exception.getCause().getMessage()).contains("jdoe not authorized for DATA:READ");
}
@Test
public void dataWriteAllowed() {
customers.put(3L, Customer.newCustomer(3L, "Samantha Rogers"));
}
@AfterClass
public static void cleanup() {
stop(geodeServer);
System.clearProperty(GEMFIRE_CACHE_SERVER_PORT_PROPERTY);
}
}