VaultSecurityManager implements SecurityManager { @Override public Object authenticate(Properties properties) throws AuthenticationFailedException { String username = properties.getProperty("security-username"); Assert.hasText(username, "Missing username"); LOG.info("authenticate: username={}", username); String password = properties.getProperty("security-password"); Assert.hasText(password, "Missing password"); LOG.trace("authenticate: username={}, password={}", username, password); VaultResponse vaultResponse = vaultTemplate.read(readPath + username); if (vaultResponse == null) { throw new AuthenticationFailedException(username); } String actualPassword = (String) vaultResponse.getData().get("password"); LOG.trace("authenticate: username={}, password={}, actualPassword={}", username, password, actualPassword); if (!password.equals(actualPassword)) { throw new AuthenticationFailedException(username); } return new UsernamePrincipal(username); } VaultSecurityManager();  VaultSecurityManager(VaultTemplate vaultTemplate, String readPath); @Override Object authenticate(Properties properties); @Override void init(Properties securityProps); @Override boolean authorize(Object principal, ResourcePermission permission); @Override void close();  }
@Test void authenticate_success() { String username = "some-user"; String password = "some-password"; VaultResponse vaultResponse = mock(VaultResponse.class); Map<String, Object> data = new HashMap<>(); data.put("password", password); when(vaultResponse.getData()).thenReturn(data); when(vaultTemplate.read(anyString())).thenReturn(vaultResponse); Properties props = new Properties(); props.setProperty("security-username",username); props.setProperty("security-password",password); vaultSecurityManager.authenticate(props); }
@Test void authenticate_invalidPassword() { String username = "some-user"; String password = "some-password"; VaultResponse vaultResponse = mock(VaultResponse.class); Map<String, Object> data = new HashMap<>(); data.put("password", password); when(vaultResponse.getData()).thenReturn(data); when(vaultTemplate.read(anyString())).thenReturn(vaultResponse); Properties props = new Properties(); props.setProperty("security-username",username); props.setProperty("security-password","some-other-password"); Throwable throwable = catchThrowable(() -> vaultSecurityManager.authenticate(props)); assertThat(throwable).isInstanceOf(AuthenticationFailedException.class); }
@Test void authenticate_missingPassword() { String username = "some-user"; String password = "some-password"; VaultResponse vaultResponse = mock(VaultResponse.class); Map<String, Object> data = new HashMap<>(); data.put("password", password); when(vaultResponse.getData()).thenReturn(data); when(vaultTemplate.read(anyString())).thenReturn(vaultResponse); Properties props = new Properties(); props.setProperty("security-username", username); Throwable throwable = catchThrowable(() -> vaultSecurityManager.authenticate(props)); assertThat(throwable).isInstanceOf(IllegalArgumentException.class); }
@Test void authenticate_missingUsername() { String password = "some-password"; VaultResponse vaultResponse = mock(VaultResponse.class); Map<String, Object> data = new HashMap<>(); data.put("password", password); when(vaultResponse.getData()).thenReturn(data); when(vaultTemplate.read(anyString())).thenReturn(vaultResponse); Properties props = new Properties(); props.setProperty("security-password",password); Throwable throwable = catchThrowable(() -> vaultSecurityManager.authenticate(props)); assertThat(throwable).isInstanceOf(IllegalArgumentException.class); }