From 1eb4387dbe8964862316d9a2ce2e999651e2124b Mon Sep 17 00:00:00 2001 From: joewitt Date: Mon, 27 Apr 2015 21:29:06 -0400 Subject: [PATCH] NIFI-271 --- .../nifi-file-authorization-provider/pom.xml | 8 ++ .../FileAuthorizationProvider.java | 90 +------------------ .../FileAuthorizationProviderTest.java | 63 ++++++------- 3 files changed, 41 insertions(+), 120 deletions(-) diff --git a/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/pom.xml b/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/pom.xml index d79b0ca6e9..b5cde8d692 100644 --- a/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/pom.xml +++ b/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/pom.xml @@ -49,6 +49,14 @@ ${project.build.directory}/generated-sources/jaxb + + org.apache.maven.plugins + maven-checkstyle-plugin + + **/user/generated/*.java + + + diff --git a/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/src/main/java/org/apache/nifi/authorization/FileAuthorizationProvider.java b/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/src/main/java/org/apache/nifi/authorization/FileAuthorizationProvider.java index 5657369ff8..9c2cad5fdb 100644 --- a/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/src/main/java/org/apache/nifi/authorization/FileAuthorizationProvider.java +++ b/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/src/main/java/org/apache/nifi/authorization/FileAuthorizationProvider.java @@ -167,23 +167,10 @@ public class FileAuthorizationProvider implements AuthorityProvider { public void preDestruction() { } - /** - * Determines if this provider has a default role. - * - * @return - */ private boolean hasDefaultRoles() { return !defaultAuthorities.isEmpty(); } - /** - * Determines if the specified dn is known to this authority provider. When - * this provider is configured to have default role(s), all dn are - * considered to exist. - * - * @param dn - * @return True if he dn is known, false otherwise - */ @Override public boolean doesDnExist(String dn) throws AuthorityAccessException { if (hasDefaultRoles()) { @@ -194,21 +181,11 @@ public class FileAuthorizationProvider implements AuthorityProvider { return user != null; } - /** - * Loads the authorities for the specified user. If this provider is - * configured for default user role(s) and a non existent dn is specified, a - * new user will be automatically created with the default role(s). - * - * @param dn - * @return - * @throws UnknownIdentityException - * @throws AuthorityAccessException - */ @Override public synchronized Set getAuthorities(String dn) throws UnknownIdentityException, AuthorityAccessException { final Set authorities = EnumSet.noneOf(Authority.class); - // get the user + // get the user final User user = getUser(dn); // ensure the user was located @@ -234,16 +211,6 @@ public class FileAuthorizationProvider implements AuthorityProvider { return authorities; } - /** - * Adds the specified authorities to the specified user. Regardless of - * whether this provider is configured for a default user role, when a non - * existent dn is specified, an UnknownIdentityException will be thrown. - * - * @param dn - * @param authorities - * @throws UnknownIdentityException - * @throws AuthorityAccessException - */ @Override public synchronized void setAuthorities(String dn, Set authorities) throws UnknownIdentityException, AuthorityAccessException { // get the user @@ -265,12 +232,6 @@ public class FileAuthorizationProvider implements AuthorityProvider { } } - /** - * Adds the specified authorities to the specified user. - * - * @param user - * @param authorities - */ private void setUserAuthorities(final User user, final Set authorities) { // clear the existing rules user.getRole().clear(); @@ -286,15 +247,6 @@ public class FileAuthorizationProvider implements AuthorityProvider { } } - /** - * Adds the specified user. If this provider is configured with default - * role(s) they will be added to the new user. - * - * @param dn - * @param group - * @throws UnknownIdentityException - * @throws AuthorityAccessException - */ @Override public synchronized void addUser(String dn, String group) throws IdentityAlreadyExistsException, AuthorityAccessException { final User user = getUser(dn); @@ -334,13 +286,6 @@ public class FileAuthorizationProvider implements AuthorityProvider { } } - /** - * Gets the users for the specified authority. - * - * @param authority - * @return - * @throws AuthorityAccessException - */ @Override public synchronized Set getUsers(Authority authority) throws AuthorityAccessException { final Set userSet = new HashSet<>(); @@ -354,15 +299,6 @@ public class FileAuthorizationProvider implements AuthorityProvider { return userSet; } - /** - * Removes the specified user. Regardless of whether this provider is - * configured for a default user role, when a non existent dn is specified, - * an UnknownIdentityException will be thrown. - * - * @param dn - * @throws UnknownIdentityException - * @throws AuthorityAccessException - */ @Override public synchronized void revokeUser(String dn) throws UnknownIdentityException, AuthorityAccessException { // get the user @@ -496,24 +432,12 @@ public class FileAuthorizationProvider implements AuthorityProvider { /** * Grants access to download content regardless of FlowFile attributes. - * - * @param dnChain - * @param attributes - * @return - * @throws UnknownIdentityException - * @throws AuthorityAccessException */ @Override public DownloadAuthorization authorizeDownload(List dnChain, Map attributes) throws UnknownIdentityException, AuthorityAccessException { return DownloadAuthorization.approved(); } - /** - * Locates the user with the specified DN. - * - * @param dn - * @return - */ private User getUser(String dn) throws UnknownIdentityException { // ensure the DN was specified if (dn == null) { @@ -532,13 +456,6 @@ public class FileAuthorizationProvider implements AuthorityProvider { return desiredUser; } - /** - * Locates all users that are part of the specified group. - * - * @param group - * @return - * @throws UnknownIdentityException - */ private Collection getUserGroup(String group) throws UnknownIdentityException { // ensure the DN was specified if (group == null) { @@ -559,11 +476,6 @@ public class FileAuthorizationProvider implements AuthorityProvider { return userGroup; } - /** - * Saves the users file. - * - * @throws Exception - */ private void save() throws Exception { final Marshaller marshaller = JAXB_CONTEXT.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); diff --git a/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/src/test/java/org/apache/nifi/authorization/FileAuthorizationProviderTest.java b/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/src/test/java/org/apache/nifi/authorization/FileAuthorizationProviderTest.java index d02d4d7919..74285003b4 100644 --- a/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/src/test/java/org/apache/nifi/authorization/FileAuthorizationProviderTest.java +++ b/nifi/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-file-authorization-provider/src/test/java/org/apache/nifi/authorization/FileAuthorizationProviderTest.java @@ -23,105 +23,106 @@ import org.apache.nifi.authorization.exception.ProviderCreationException; import org.apache.nifi.util.file.FileUtils; import org.apache.nifi.util.NiFiProperties; import org.junit.After; +import static org.junit.Assert.assertEquals; import org.junit.Before; import org.junit.Test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; import org.junit.Ignore; import org.mockito.Mockito; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @Ignore public class FileAuthorizationProviderTest { - + private FileAuthorizationProvider provider; - + private File primary; - + private File restore; - + private NiFiProperties mockProperties; - + private AuthorityProviderConfigurationContext mockConfigurationContext; - + @Before public void setup() throws IOException { - + primary = new File("target/primary/users.txt"); restore = new File("target/restore/users.txt"); - + System.out.println("absolute path: " + primary.getAbsolutePath()); - + mockProperties = mock(NiFiProperties.class); when(mockProperties.getRestoreDirectory()).thenReturn(restore.getParentFile()); - + mockConfigurationContext = mock(AuthorityProviderConfigurationContext.class); when(mockConfigurationContext.getProperty(Mockito.eq("Authorized Users File"))).thenReturn(primary.getPath()); - + provider = new FileAuthorizationProvider(); provider.setNiFiProperties(mockProperties); provider.initialize(null); - } - + } + @After public void cleanup() throws Exception { deleteFile(primary); deleteFile(restore); } - + private boolean deleteFile(final File file) { - if(file.isDirectory()) { + if (file.isDirectory()) { FileUtils.deleteFilesInDir(file, null, null, true, true); } return FileUtils.deleteFile(file, null, 10); } - + @Test public void testPostContructionWhenRestoreDoesNotExist() throws Exception { - + byte[] primaryBytes = "".getBytes(); FileOutputStream fos = new FileOutputStream(primary); fos.write(primaryBytes); fos.close(); - + provider.onConfigured(mockConfigurationContext); assertEquals(primary.length(), restore.length()); } - + @Test public void testPostContructionWhenPrimaryDoesNotExist() throws Exception { - + byte[] restoreBytes = "".getBytes(); FileOutputStream fos = new FileOutputStream(restore); fos.write(restoreBytes); fos.close(); - + provider.onConfigured(mockConfigurationContext); assertEquals(restore.length(), primary.length()); - + } - + @Test(expected = ProviderCreationException.class) public void testPostContructionWhenPrimaryDifferentThanRestore() throws Exception { - + byte[] primaryBytes = "".getBytes(); FileOutputStream fos = new FileOutputStream(primary); fos.write(primaryBytes); fos.close(); - + byte[] restoreBytes = "".getBytes(); fos = new FileOutputStream(restore); fos.write(restoreBytes); fos.close(); - + provider.onConfigured(mockConfigurationContext); } - + @Test public void testPostContructionWhenPrimaryAndBackupDoNotExist() throws Exception { - + provider.onConfigured(mockConfigurationContext); assertEquals(0, restore.length()); assertEquals(restore.length(), primary.length()); } - + }