Search in sources :

Example 1 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method deleteUser.

@Override
public synchronized User deleteUser(User user) throws AuthorizationAccessException {
    if (user == null) {
        throw new IllegalArgumentException("User cannot be null");
    }
    final UserGroupHolder holder = userGroupHolder.get();
    final Tenants tenants = holder.getTenants();
    final List<org.apache.nifi.authorization.file.tenants.generated.User> users = tenants.getUsers().getUser();
    // for each group iterate over the user references and remove the user reference if it matches the user being deleted
    for (org.apache.nifi.authorization.file.tenants.generated.Group group : tenants.getGroups().getGroup()) {
        Iterator<org.apache.nifi.authorization.file.tenants.generated.Group.User> groupUserIter = group.getUser().iterator();
        while (groupUserIter.hasNext()) {
            org.apache.nifi.authorization.file.tenants.generated.Group.User groupUser = groupUserIter.next();
            if (groupUser.getIdentifier().equals(user.getIdentifier())) {
                groupUserIter.remove();
                break;
            }
        }
    }
    // remove the actual user if it exists
    boolean removedUser = false;
    Iterator<org.apache.nifi.authorization.file.tenants.generated.User> iter = users.iterator();
    while (iter.hasNext()) {
        org.apache.nifi.authorization.file.tenants.generated.User jaxbUser = iter.next();
        if (user.getIdentifier().equals(jaxbUser.getIdentifier())) {
            iter.remove();
            removedUser = true;
            break;
        }
    }
    if (removedUser) {
        saveAndRefreshHolder(tenants);
        return user;
    } else {
        return null;
    }
}
Also used : Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants)

Example 2 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method addUser.

@Override
public synchronized User addUser(User user) throws AuthorizationAccessException {
    if (user == null) {
        throw new IllegalArgumentException("User cannot be null");
    }
    final org.apache.nifi.authorization.file.tenants.generated.User jaxbUser = createJAXBUser(user);
    final UserGroupHolder holder = userGroupHolder.get();
    final Tenants tenants = holder.getTenants();
    tenants.getUsers().getUser().add(jaxbUser);
    saveAndRefreshHolder(tenants);
    return userGroupHolder.get().getUsersById().get(user.getIdentifier());
}
Also used : Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants)

Example 3 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method onConfigured.

@Override
public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
    try {
        final PropertyValue tenantsPath = configurationContext.getProperty(PROP_TENANTS_FILE);
        if (StringUtils.isBlank(tenantsPath.getValue())) {
            throw new AuthorizerCreationException("The users file must be specified.");
        }
        // get the tenants file and ensure it exists
        tenantsFile = new File(tenantsPath.getValue());
        if (!tenantsFile.exists()) {
            logger.info("Creating new users file at {}", new Object[] { tenantsFile.getAbsolutePath() });
            saveTenants(new Tenants());
        }
        final File tenantsFileDirectory = tenantsFile.getAbsoluteFile().getParentFile();
        // the restore directory is optional and may be null
        final File restoreDirectory = properties.getRestoreDirectory();
        if (restoreDirectory != null) {
            // sanity check that restore directory is a directory, creating it if necessary
            FileUtils.ensureDirectoryExistAndCanAccess(restoreDirectory);
            // check that restore directory is not the same as the user's directory
            if (tenantsFileDirectory.getAbsolutePath().equals(restoreDirectory.getAbsolutePath())) {
                throw new AuthorizerCreationException(String.format("Users file directory '%s' is the same as restore directory '%s' ", tenantsFileDirectory.getAbsolutePath(), restoreDirectory.getAbsolutePath()));
            }
            // the restore copy will have same file name, but reside in a different directory
            restoreTenantsFile = new File(restoreDirectory, tenantsFile.getName());
            try {
                // sync the primary copy with the restore copy
                FileUtils.syncWithRestore(tenantsFile, restoreTenantsFile, logger);
            } catch (final IOException | IllegalStateException ioe) {
                throw new AuthorizerCreationException(ioe);
            }
        }
        // extract the identity mappings from nifi.properties if any are provided
        identityMappings = Collections.unmodifiableList(IdentityMappingUtil.getIdentityMappings(properties));
        // get the value of the legacy authorized users file
        final PropertyValue legacyAuthorizedUsersProp = configurationContext.getProperty(FileAuthorizer.PROP_LEGACY_AUTHORIZED_USERS_FILE);
        legacyAuthorizedUsersFile = legacyAuthorizedUsersProp.isSet() ? legacyAuthorizedUsersProp.getValue() : null;
        // extract any node identities
        initialUserIdentities = new HashSet<>();
        for (Map.Entry<String, String> entry : configurationContext.getProperties().entrySet()) {
            Matcher matcher = INITIAL_USER_IDENTITY_PATTERN.matcher(entry.getKey());
            if (matcher.matches() && !StringUtils.isBlank(entry.getValue())) {
                initialUserIdentities.add(IdentityMappingUtil.mapIdentity(entry.getValue(), identityMappings));
            }
        }
        load();
        // if we've copied the authorizations file to a restore directory synchronize it
        if (restoreTenantsFile != null) {
            FileUtils.copyFile(tenantsFile, restoreTenantsFile, false, false, logger);
        }
        logger.info(String.format("Users/Groups file loaded at %s", new Date().toString()));
    } catch (IOException | AuthorizerCreationException | JAXBException | IllegalStateException | SAXException e) {
        throw new AuthorizerCreationException(e);
    }
}
Also used : Matcher(java.util.regex.Matcher) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) JAXBException(javax.xml.bind.JAXBException) PropertyValue(org.apache.nifi.components.PropertyValue) Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants) IOException(java.io.IOException) Date(java.util.Date) SAXException(org.xml.sax.SAXException) File(java.io.File) Map(java.util.Map)

Example 4 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method updateUser.

@Override
public synchronized User updateUser(User user) throws AuthorizationAccessException {
    if (user == null) {
        throw new IllegalArgumentException("User cannot be null");
    }
    final UserGroupHolder holder = userGroupHolder.get();
    final Tenants tenants = holder.getTenants();
    final List<org.apache.nifi.authorization.file.tenants.generated.User> users = tenants.getUsers().getUser();
    // fine the User that needs to be updated
    org.apache.nifi.authorization.file.tenants.generated.User updateUser = null;
    for (org.apache.nifi.authorization.file.tenants.generated.User jaxbUser : users) {
        if (user.getIdentifier().equals(jaxbUser.getIdentifier())) {
            updateUser = jaxbUser;
            break;
        }
    }
    // if user wasn't found return null, otherwise update the user and save changes
    if (updateUser == null) {
        return null;
    } else {
        updateUser.setIdentity(user.getIdentity());
        saveAndRefreshHolder(tenants);
        return userGroupHolder.get().getUsersById().get(user.getIdentifier());
    }
}
Also used : Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants)

Example 5 with Tenants

use of org.apache.nifi.authorization.file.tenants.generated.Tenants in project nifi by apache.

the class FileUserGroupProvider method deleteGroup.

@Override
public synchronized Group deleteGroup(Group group) throws AuthorizationAccessException {
    final UserGroupHolder holder = userGroupHolder.get();
    final Tenants tenants = holder.getTenants();
    final List<org.apache.nifi.authorization.file.tenants.generated.Group> groups = tenants.getGroups().getGroup();
    // now remove the actual group from the top-level list of groups
    boolean removedGroup = false;
    Iterator<org.apache.nifi.authorization.file.tenants.generated.Group> iter = groups.iterator();
    while (iter.hasNext()) {
        org.apache.nifi.authorization.file.tenants.generated.Group jaxbGroup = iter.next();
        if (group.getIdentifier().equals(jaxbGroup.getIdentifier())) {
            iter.remove();
            removedGroup = true;
            break;
        }
    }
    if (removedGroup) {
        saveAndRefreshHolder(tenants);
        return group;
    } else {
        return null;
    }
}
Also used : Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants)

Aggregations

Tenants (org.apache.nifi.authorization.file.tenants.generated.Tenants)9 JAXBException (javax.xml.bind.JAXBException)2 File (java.io.File)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 StreamSource (javax.xml.transform.stream.StreamSource)1 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)1 Groups (org.apache.nifi.authorization.file.tenants.generated.Groups)1 Users (org.apache.nifi.authorization.file.tenants.generated.Users)1 PropertyValue (org.apache.nifi.components.PropertyValue)1 SAXException (org.xml.sax.SAXException)1