Search in sources :

Example 1 with UninheritableAuthorizationsException

use of org.apache.nifi.authorization.exception.UninheritableAuthorizationsException in project nifi by apache.

the class StandardFlowSynchronizer method checkAuthorizerInheritability.

/**
 * If both authorizers are external authorizers, or if the both are internal
 * authorizers with equal fingerprints, then an uniheritable result with no
 * reason is returned to indicate nothing to do.
 *
 * If both are internal authorizers and the current authorizer is empty,
 * then an inheritable result is returned.
 *
 * All other cases return uninheritable with a reason which indicates to
 * throw an exception.
 *
 * @param existingFlow the existing DataFlow
 * @param proposedFlow the proposed DataFlow
 * @return the AuthorizerInheritability result
 */
private AuthorizerInheritability checkAuthorizerInheritability(final Authorizer authorizer, final DataFlow existingFlow, final DataFlow proposedFlow) {
    final byte[] existing = existingFlow.getAuthorizerFingerprint();
    final byte[] proposed = proposedFlow.getAuthorizerFingerprint();
    // both are using external authorizers so nothing to inherit, but we don't want to throw an exception
    if (existing == null && proposed == null) {
        return AuthorizerInheritability.uninheritable(null);
    }
    // current is external, but proposed is internal
    if (existing == null && proposed != null) {
        return AuthorizerInheritability.uninheritable("Current Authorizer is an external Authorizer, but proposed Authorizer is an internal Authorizer");
    }
    // current is internal, but proposed is external
    if (existing != null && proposed == null) {
        return AuthorizerInheritability.uninheritable("Current Authorizer is an internal Authorizer, but proposed Authorizer is an external Authorizer");
    }
    // both are internal, but not the same
    if (!Arrays.equals(existing, proposed)) {
        if (AuthorizerCapabilityDetection.isManagedAuthorizer(authorizer)) {
            final ManagedAuthorizer managedAuthorizer = (ManagedAuthorizer) authorizer;
            try {
                // if the configurations are not equal, see if the manager indicates the proposed configuration is inheritable
                managedAuthorizer.checkInheritability(new String(proposed, StandardCharsets.UTF_8));
                return AuthorizerInheritability.inheritable();
            } catch (final UninheritableAuthorizationsException e) {
                return AuthorizerInheritability.uninheritable("Proposed Authorizations do not match current Authorizations: " + e.getMessage());
            }
        } else {
            // should never hit since the existing is only null when authorizer is not managed
            return AuthorizerInheritability.uninheritable("Proposed Authorizations do not match current Authorizations and are not configured with an internal Authorizer");
        }
    }
    // both are internal and equal
    return AuthorizerInheritability.uninheritable(null);
}
Also used : UninheritableAuthorizationsException(org.apache.nifi.authorization.exception.UninheritableAuthorizationsException) ManagedAuthorizer(org.apache.nifi.authorization.ManagedAuthorizer)

Example 2 with UninheritableAuthorizationsException

use of org.apache.nifi.authorization.exception.UninheritableAuthorizationsException in project nifi by apache.

the class AuthorizerFactory method installIntegrityChecks.

public static Authorizer installIntegrityChecks(final Authorizer baseAuthorizer) {
    Authorizer authorizer;
    if (baseAuthorizer instanceof ManagedAuthorizer) {
        final ManagedAuthorizer baseManagedAuthorizer = (ManagedAuthorizer) baseAuthorizer;
        authorizer = new ManagedAuthorizer() {

            @Override
            public String getFingerprint() throws AuthorizationAccessException {
                return baseManagedAuthorizer.getFingerprint();
            }

            @Override
            public void inheritFingerprint(String fingerprint) throws AuthorizationAccessException {
                baseManagedAuthorizer.inheritFingerprint(fingerprint);
            }

            @Override
            public void checkInheritability(String proposedFingerprint) throws AuthorizationAccessException, UninheritableAuthorizationsException {
                baseManagedAuthorizer.checkInheritability(proposedFingerprint);
            }

            @Override
            public AccessPolicyProvider getAccessPolicyProvider() {
                final AccessPolicyProvider baseAccessPolicyProvider = baseManagedAuthorizer.getAccessPolicyProvider();
                if (baseAccessPolicyProvider instanceof ConfigurableAccessPolicyProvider) {
                    final ConfigurableAccessPolicyProvider baseConfigurableAccessPolicyProvider = (ConfigurableAccessPolicyProvider) baseAccessPolicyProvider;
                    return new ConfigurableAccessPolicyProvider() {

                        @Override
                        public String getFingerprint() throws AuthorizationAccessException {
                            return baseConfigurableAccessPolicyProvider.getFingerprint();
                        }

                        @Override
                        public void inheritFingerprint(String fingerprint) throws AuthorizationAccessException {
                            baseConfigurableAccessPolicyProvider.inheritFingerprint(fingerprint);
                        }

                        @Override
                        public void checkInheritability(String proposedFingerprint) throws AuthorizationAccessException, UninheritableAuthorizationsException {
                            baseConfigurableAccessPolicyProvider.checkInheritability(proposedFingerprint);
                        }

                        @Override
                        public AccessPolicy addAccessPolicy(AccessPolicy accessPolicy) throws AuthorizationAccessException {
                            if (policyExists(baseConfigurableAccessPolicyProvider, accessPolicy)) {
                                throw new IllegalStateException(String.format("Found multiple policies for '%s' with '%s'.", accessPolicy.getResource(), accessPolicy.getAction()));
                            }
                            return baseConfigurableAccessPolicyProvider.addAccessPolicy(accessPolicy);
                        }

                        @Override
                        public boolean isConfigurable(AccessPolicy accessPolicy) {
                            return baseConfigurableAccessPolicyProvider.isConfigurable(accessPolicy);
                        }

                        @Override
                        public AccessPolicy updateAccessPolicy(AccessPolicy accessPolicy) throws AuthorizationAccessException {
                            if (!baseConfigurableAccessPolicyProvider.isConfigurable(accessPolicy)) {
                                throw new IllegalArgumentException("The specified access policy is not support modification.");
                            }
                            return baseConfigurableAccessPolicyProvider.updateAccessPolicy(accessPolicy);
                        }

                        @Override
                        public AccessPolicy deleteAccessPolicy(AccessPolicy accessPolicy) throws AuthorizationAccessException {
                            if (!baseConfigurableAccessPolicyProvider.isConfigurable(accessPolicy)) {
                                throw new IllegalArgumentException("The specified access policy is not support modification.");
                            }
                            return baseConfigurableAccessPolicyProvider.deleteAccessPolicy(accessPolicy);
                        }

                        @Override
                        public Set<AccessPolicy> getAccessPolicies() throws AuthorizationAccessException {
                            return baseConfigurableAccessPolicyProvider.getAccessPolicies();
                        }

                        @Override
                        public AccessPolicy getAccessPolicy(String identifier) throws AuthorizationAccessException {
                            return baseConfigurableAccessPolicyProvider.getAccessPolicy(identifier);
                        }

                        @Override
                        public AccessPolicy getAccessPolicy(String resourceIdentifier, RequestAction action) throws AuthorizationAccessException {
                            return baseConfigurableAccessPolicyProvider.getAccessPolicy(resourceIdentifier, action);
                        }

                        @Override
                        public UserGroupProvider getUserGroupProvider() {
                            final UserGroupProvider baseUserGroupProvider = baseConfigurableAccessPolicyProvider.getUserGroupProvider();
                            if (baseUserGroupProvider instanceof ConfigurableUserGroupProvider) {
                                final ConfigurableUserGroupProvider baseConfigurableUserGroupProvider = (ConfigurableUserGroupProvider) baseUserGroupProvider;
                                return new ConfigurableUserGroupProvider() {

                                    @Override
                                    public String getFingerprint() throws AuthorizationAccessException {
                                        return baseConfigurableUserGroupProvider.getFingerprint();
                                    }

                                    @Override
                                    public void inheritFingerprint(String fingerprint) throws AuthorizationAccessException {
                                        baseConfigurableUserGroupProvider.inheritFingerprint(fingerprint);
                                    }

                                    @Override
                                    public void checkInheritability(String proposedFingerprint) throws AuthorizationAccessException, UninheritableAuthorizationsException {
                                        baseConfigurableUserGroupProvider.checkInheritability(proposedFingerprint);
                                    }

                                    @Override
                                    public User addUser(User user) throws AuthorizationAccessException {
                                        if (tenantExists(baseConfigurableUserGroupProvider, user.getIdentifier(), user.getIdentity())) {
                                            throw new IllegalStateException(String.format("User/user group already exists with the identity '%s'.", user.getIdentity()));
                                        }
                                        return baseConfigurableUserGroupProvider.addUser(user);
                                    }

                                    @Override
                                    public boolean isConfigurable(User user) {
                                        return baseConfigurableUserGroupProvider.isConfigurable(user);
                                    }

                                    @Override
                                    public User updateUser(User user) throws AuthorizationAccessException {
                                        if (tenantExists(baseConfigurableUserGroupProvider, user.getIdentifier(), user.getIdentity())) {
                                            throw new IllegalStateException(String.format("User/user group already exists with the identity '%s'.", user.getIdentity()));
                                        }
                                        if (!baseConfigurableUserGroupProvider.isConfigurable(user)) {
                                            throw new IllegalArgumentException("The specified user does not support modification.");
                                        }
                                        return baseConfigurableUserGroupProvider.updateUser(user);
                                    }

                                    @Override
                                    public User deleteUser(User user) throws AuthorizationAccessException {
                                        if (!baseConfigurableUserGroupProvider.isConfigurable(user)) {
                                            throw new IllegalArgumentException("The specified user does not support modification.");
                                        }
                                        return baseConfigurableUserGroupProvider.deleteUser(user);
                                    }

                                    @Override
                                    public Group addGroup(Group group) throws AuthorizationAccessException {
                                        if (tenantExists(baseConfigurableUserGroupProvider, group.getIdentifier(), group.getName())) {
                                            throw new IllegalStateException(String.format("User/user group already exists with the identity '%s'.", group.getName()));
                                        }
                                        if (!allGroupUsersExist(baseConfigurableUserGroupProvider, group)) {
                                            throw new IllegalStateException(String.format("Cannot create group '%s' with users that don't exist.", group.getName()));
                                        }
                                        return baseConfigurableUserGroupProvider.addGroup(group);
                                    }

                                    @Override
                                    public boolean isConfigurable(Group group) {
                                        return baseConfigurableUserGroupProvider.isConfigurable(group);
                                    }

                                    @Override
                                    public Group updateGroup(Group group) throws AuthorizationAccessException {
                                        if (tenantExists(baseConfigurableUserGroupProvider, group.getIdentifier(), group.getName())) {
                                            throw new IllegalStateException(String.format("User/user group already exists with the identity '%s'.", group.getName()));
                                        }
                                        if (!allGroupUsersExist(baseConfigurableUserGroupProvider, group)) {
                                            throw new IllegalStateException(String.format("Cannot update group '%s' to add users that don't exist.", group.getName()));
                                        }
                                        if (!baseConfigurableUserGroupProvider.isConfigurable(group)) {
                                            throw new IllegalArgumentException("The specified group does not support modification.");
                                        }
                                        return baseConfigurableUserGroupProvider.updateGroup(group);
                                    }

                                    @Override
                                    public Group deleteGroup(Group group) throws AuthorizationAccessException {
                                        if (!baseConfigurableUserGroupProvider.isConfigurable(group)) {
                                            throw new IllegalArgumentException("The specified group does not support modification.");
                                        }
                                        return baseConfigurableUserGroupProvider.deleteGroup(group);
                                    }

                                    @Override
                                    public Set<User> getUsers() throws AuthorizationAccessException {
                                        return baseConfigurableUserGroupProvider.getUsers();
                                    }

                                    @Override
                                    public User getUser(String identifier) throws AuthorizationAccessException {
                                        return baseConfigurableUserGroupProvider.getUser(identifier);
                                    }

                                    @Override
                                    public User getUserByIdentity(String identity) throws AuthorizationAccessException {
                                        return baseConfigurableUserGroupProvider.getUserByIdentity(identity);
                                    }

                                    @Override
                                    public Set<Group> getGroups() throws AuthorizationAccessException {
                                        return baseConfigurableUserGroupProvider.getGroups();
                                    }

                                    @Override
                                    public Group getGroup(String identifier) throws AuthorizationAccessException {
                                        return baseConfigurableUserGroupProvider.getGroup(identifier);
                                    }

                                    @Override
                                    public UserAndGroups getUserAndGroups(String identity) throws AuthorizationAccessException {
                                        return baseConfigurableUserGroupProvider.getUserAndGroups(identity);
                                    }

                                    @Override
                                    public void initialize(UserGroupProviderInitializationContext initializationContext) throws AuthorizerCreationException {
                                        baseConfigurableUserGroupProvider.initialize(initializationContext);
                                    }

                                    @Override
                                    public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
                                        baseConfigurableUserGroupProvider.onConfigured(configurationContext);
                                    }

                                    @Override
                                    public void preDestruction() throws AuthorizerDestructionException {
                                        baseConfigurableUserGroupProvider.preDestruction();
                                    }
                                };
                            } else {
                                return baseUserGroupProvider;
                            }
                        }

                        @Override
                        public void initialize(AccessPolicyProviderInitializationContext initializationContext) throws AuthorizerCreationException {
                            baseConfigurableAccessPolicyProvider.initialize(initializationContext);
                        }

                        @Override
                        public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
                            baseConfigurableAccessPolicyProvider.onConfigured(configurationContext);
                        }

                        @Override
                        public void preDestruction() throws AuthorizerDestructionException {
                            baseConfigurableAccessPolicyProvider.preDestruction();
                        }
                    };
                } else {
                    return baseAccessPolicyProvider;
                }
            }

            @Override
            public AuthorizationResult authorize(AuthorizationRequest request) throws AuthorizationAccessException {
                final AuthorizationResult result = baseAuthorizer.authorize(request);
                // audit the authorization request
                audit(baseAuthorizer, request, result);
                return result;
            }

            @Override
            public void initialize(AuthorizerInitializationContext initializationContext) throws AuthorizerCreationException {
                baseManagedAuthorizer.initialize(initializationContext);
            }

            @Override
            public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
                baseManagedAuthorizer.onConfigured(configurationContext);
                final AccessPolicyProvider accessPolicyProvider = baseManagedAuthorizer.getAccessPolicyProvider();
                final UserGroupProvider userGroupProvider = accessPolicyProvider.getUserGroupProvider();
                // ensure that only one policy per resource-action exists
                for (AccessPolicy accessPolicy : accessPolicyProvider.getAccessPolicies()) {
                    if (policyExists(accessPolicyProvider, accessPolicy)) {
                        throw new AuthorizerCreationException(String.format("Found multiple policies for '%s' with '%s'.", accessPolicy.getResource(), accessPolicy.getAction()));
                    }
                }
                // ensure that only one group exists per identity
                for (User user : userGroupProvider.getUsers()) {
                    if (tenantExists(userGroupProvider, user.getIdentifier(), user.getIdentity())) {
                        throw new AuthorizerCreationException(String.format("Found multiple users/user groups with identity '%s'.", user.getIdentity()));
                    }
                }
                // ensure that only one group exists per identity
                for (Group group : userGroupProvider.getGroups()) {
                    if (tenantExists(userGroupProvider, group.getIdentifier(), group.getName())) {
                        throw new AuthorizerCreationException(String.format("Found multiple users/user groups with name '%s'.", group.getName()));
                    }
                }
            }

            @Override
            public void preDestruction() throws AuthorizerDestructionException {
                baseManagedAuthorizer.preDestruction();
            }
        };
    } else {
        authorizer = new Authorizer() {

            @Override
            public AuthorizationResult authorize(AuthorizationRequest request) throws AuthorizationAccessException {
                final AuthorizationResult result = baseAuthorizer.authorize(request);
                // audit the authorization request
                audit(baseAuthorizer, request, result);
                return result;
            }

            @Override
            public void initialize(AuthorizerInitializationContext initializationContext) throws AuthorizerCreationException {
                baseAuthorizer.initialize(initializationContext);
            }

            @Override
            public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
                baseAuthorizer.onConfigured(configurationContext);
            }

            @Override
            public void preDestruction() throws AuthorizerDestructionException {
                baseAuthorizer.preDestruction();
            }
        };
    }
    // conditionally add support for the audit methods
    if (baseAuthorizer instanceof AuthorizationAuditor) {
        final AuthorizationAuditorInvocationHandler invocationHandler = new AuthorizationAuditorInvocationHandler(authorizer, (AuthorizationAuditor) baseAuthorizer);
        final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(authorizer.getClass());
        interfaceList.add(AuthorizationAuditor.class);
        final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[interfaceList.size()]);
        authorizer = (Authorizer) Proxy.newProxyInstance(authorizer.getClass().getClassLoader(), interfaces, invocationHandler);
    }
    return authorizer;
}
Also used : Set(java.util.Set) AuthorizationAccessException(org.apache.nifi.authorization.exception.AuthorizationAccessException) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) UninheritableAuthorizationsException(org.apache.nifi.authorization.exception.UninheritableAuthorizationsException) AuthorizerDestructionException(org.apache.nifi.authorization.exception.AuthorizerDestructionException)

Example 3 with UninheritableAuthorizationsException

use of org.apache.nifi.authorization.exception.UninheritableAuthorizationsException in project nifi by apache.

the class AbstractPolicyBasedAuthorizer method checkInheritability.

/**
 * Returns whether the proposed fingerprint is inheritable.
 *
 * @param proposedFingerprint the proposed fingerprint
 * @throws AuthorizationAccessException if there was an unexpected error performing the operation
 * @throws UninheritableAuthorizationsException if the proposed fingerprint was uninheritable
 */
@Override
public final void checkInheritability(String proposedFingerprint) throws AuthorizationAccessException, UninheritableAuthorizationsException {
    try {
        // ensure we understand the proposed fingerprint
        parsePoliciesUsersAndGroups(proposedFingerprint);
    } catch (final AuthorizationAccessException e) {
        throw new UninheritableAuthorizationsException("Unable to parse proposed fingerprint: " + e);
    }
    final List<User> users = getSortedUsers();
    final List<Group> groups = getSortedGroups();
    final List<AccessPolicy> accessPolicies = getSortedAccessPolicies();
    // ensure we're in a state to inherit
    if (!users.isEmpty() || !groups.isEmpty() || !accessPolicies.isEmpty()) {
        throw new UninheritableAuthorizationsException("Proposed fingerprint is not inheritable because the current Authorizations is not empty..");
    }
}
Also used : UninheritableAuthorizationsException(org.apache.nifi.authorization.exception.UninheritableAuthorizationsException) AuthorizationAccessException(org.apache.nifi.authorization.exception.AuthorizationAccessException)

Aggregations

UninheritableAuthorizationsException (org.apache.nifi.authorization.exception.UninheritableAuthorizationsException)3 AuthorizationAccessException (org.apache.nifi.authorization.exception.AuthorizationAccessException)2 Set (java.util.Set)1 ManagedAuthorizer (org.apache.nifi.authorization.ManagedAuthorizer)1 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)1 AuthorizerDestructionException (org.apache.nifi.authorization.exception.AuthorizerDestructionException)1