Add apache-geode-extensions module containing classes implementing Apache Geode interfaces and SPIs.

These extensions will be used for testing and documentation purposes.

For example, this module contains extensions to Apache Geode's o.a.g.security.SecurityManager and o.a.g.security.AuthInitialize interfaces used in the SBDG samples.
This commit is contained in:
John Blum
2019-04-22 15:23:53 -07:00
parent 74c4edfb2d
commit 57a1ea4840
5 changed files with 394 additions and 0 deletions

View File

@@ -0,0 +1,101 @@
/*
* Copyright 2019 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 org.springframework.geode.security;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Properties;
import org.apache.geode.management.internal.security.ResourceConstants;
import org.junit.Test;
/**
* Unit tests for {@link TestAuthInitialize}.
*
* @author John Blum
* @see java.util.Properties
* @see org.junit.Test
* @see org.springframework.geode.security.TestAuthInitialize
* @since 1.0.0
*/
public class TestAuthInitializeUnitTests {
private final TestAuthInitialize authInitialize = TestAuthInitialize.create();
private boolean isSet(String value) {
return !(value == null || value.trim().isEmpty());
}
private Properties newSecurityProperties(String username, String password) {
Properties securityProperties = new Properties();
if (isSet(username)) {
securityProperties.setProperty(ResourceConstants.USER_NAME, username);
}
if (isSet(password)) {
securityProperties.setProperty(ResourceConstants.PASSWORD, password);
}
return securityProperties;
}
@Test
public void getCredentialsUsesProperties() {
Properties credentials =
this.authInitialize.getCredentials(newSecurityProperties("testUser", "s3cr3t"));
assertThat(credentials).isNotNull();
assertThat(credentials.getProperty(ResourceConstants.USER_NAME)).isEqualTo("testUser");
assertThat(credentials.getProperty(ResourceConstants.PASSWORD)).isEqualTo("s3cr3t");
}
@Test
public void getCredentialsUsesProvidedUsernameAndDefaultPassword() {
Properties credentials =
this.authInitialize.getCredentials(newSecurityProperties("testUser", null));
assertThat(credentials).isNotNull();
assertThat(credentials.getProperty(ResourceConstants.USER_NAME)).isEqualTo("testUser");
assertThat(credentials.getProperty(ResourceConstants.PASSWORD)).isEqualTo("test");
}
@Test
public void getCredentialsUsesProvidedPasswordAndDefaultUsername() {
Properties credentials =
this.authInitialize.getCredentials(newSecurityProperties(null, "s3cr3t"));
assertThat(credentials).isNotNull();
assertThat(credentials.getProperty(ResourceConstants.USER_NAME)).isEqualTo("test");
assertThat(credentials.getProperty(ResourceConstants.PASSWORD)).isEqualTo("s3cr3t");
}
@Test
public void getCredentialsUsesDefaultUsernameAndPassword() {
Properties credentials =
this.authInitialize.getCredentials(newSecurityProperties(null, null));
assertThat(credentials).isNotNull();
assertThat(credentials.getProperty(ResourceConstants.USER_NAME)).isEqualTo("test");
assertThat(credentials.getProperty(ResourceConstants.PASSWORD)).isEqualTo("test");
}
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2019 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 org.springframework.geode.security;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import java.security.Principal;
import java.util.Properties;
import org.apache.geode.management.internal.security.ResourceConstants;
import org.apache.geode.security.AuthenticationFailedException;
import org.apache.geode.security.ResourcePermission;
import org.junit.Test;
/**
* Unit tests for {@link org.springframework.geode.security.TestSecurityManager}.
*
* @author John Blum
* @see org.junit.Test
* @see org.mockito.Mockito
* @see org.springframework.geode.security.TestSecurityManager
* @since 1.0.0
*/
public class TestSecurityManagerUnitTests {
private TestSecurityManager securityManager = new TestSecurityManager();
private Properties newSecurityProperties(String username, String password) {
Properties securityProperties = new Properties();
securityProperties.setProperty(ResourceConstants.USER_NAME, username);
securityProperties.setProperty(ResourceConstants.PASSWORD, password);
return securityProperties;
}
@Test
public void userAuthenticates() {
Object user = this.securityManager.authenticate(newSecurityProperties("test", "test"));
assertThat(user).isInstanceOf(TestSecurityManager.User.class);
assertThat(((TestSecurityManager.User) user).getName()).isEqualTo("test");
}
@Test(expected = AuthenticationFailedException.class)
public void userDoesNotAuthenticateBecauseUsernamePasswordAreCaseSensitive() {
try {
this.securityManager.authenticate(newSecurityProperties("TestUser", "testuser"));
}
catch (AuthenticationFailedException expected) {
assertThat(expected).hasMessage("User [TestUser] could not be authenticated");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test(expected = AuthenticationFailedException.class)
public void userDoesNotAuthenticateWhenUsernamePasswordDoNotMatch() {
try {
this.securityManager.authenticate(newSecurityProperties("testUser", "testPassword"));
}
catch (AuthenticationFailedException expected) {
assertThat(expected).hasMessage("User [testUser] could not be authenticated");
assertThat(expected).hasNoCause();
throw expected;
}
}
@Test
public void userIsAlwaysAuthorized() {
ResourcePermission clusterManage =
new ResourcePermission(ResourcePermission.Resource.CLUSTER, ResourcePermission.Operation.MANAGE);
assertThat(this.securityManager.authorize(null, clusterManage)).isTrue();
assertThat(this.securityManager.authorize(new TestSecurityManager.User("test"), clusterManage)).isTrue();
assertThat(this.securityManager.authorize(mock(Principal.class), clusterManage)).isTrue();
}
}