Search in sources :

Example 1 with Users

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

the class FileUserGroupProvider method convertLegacyAuthorizedUsers.

/**
 * Unmarshalls an existing authorized-users.xml and converts the object model to the new model.
 *
 * @param tenants the current Tenants instance users and groups will be added to
 * @throws AuthorizerCreationException if the legacy authorized users file that was provided does not exist
 * @throws JAXBException if the legacy authorized users file that was provided could not be unmarshalled
 */
private void convertLegacyAuthorizedUsers(final Tenants tenants) throws AuthorizerCreationException, JAXBException {
    final File authorizedUsersFile = new File(legacyAuthorizedUsersFile);
    if (!authorizedUsersFile.exists()) {
        throw new AuthorizerCreationException("Legacy Authorized Users File '" + legacyAuthorizedUsersFile + "' does not exists");
    }
    XMLStreamReader xsr;
    try {
        xsr = XmlUtils.createSafeReader(new StreamSource(authorizedUsersFile));
    } catch (XMLStreamException e) {
        throw new AuthorizerCreationException("Error converting the legacy authorizers file", e);
    }
    final Unmarshaller unmarshaller = JAXB_USERS_CONTEXT.createUnmarshaller();
    unmarshaller.setSchema(usersSchema);
    final JAXBElement<org.apache.nifi.user.generated.Users> element = unmarshaller.unmarshal(xsr, org.apache.nifi.user.generated.Users.class);
    final org.apache.nifi.user.generated.Users users = element.getValue();
    if (users.getUser().isEmpty()) {
        logger.info("Legacy Authorized Users File contained no users, nothing to convert");
        return;
    }
    for (org.apache.nifi.user.generated.User legacyUser : users.getUser()) {
        // create the identifier of the new user based on the DN
        final String legacyUserDn = IdentityMappingUtil.mapIdentity(legacyUser.getDn(), identityMappings);
        org.apache.nifi.authorization.file.tenants.generated.User user = getOrCreateUser(tenants, legacyUserDn);
        // if there was a group name find or create the group and add the user to it
        org.apache.nifi.authorization.file.tenants.generated.Group group = getOrCreateGroup(tenants, legacyUser.getGroup());
        if (group != null) {
            org.apache.nifi.authorization.file.tenants.generated.Group.User groupUser = new org.apache.nifi.authorization.file.tenants.generated.Group.User();
            groupUser.setIdentifier(user.getIdentifier());
            group.getUser().add(groupUser);
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) StreamSource(javax.xml.transform.stream.StreamSource) Users(org.apache.nifi.authorization.file.tenants.generated.Users) XMLStreamException(javax.xml.stream.XMLStreamException) Unmarshaller(javax.xml.bind.Unmarshaller) File(java.io.File)

Example 2 with Users

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

the class FileUserGroupProvider method load.

/**
 * Loads the authorizations file and populates the AuthorizationsHolder, only called during start-up.
 *
 * @throws JAXBException            Unable to reload the authorized users file
 * @throws IllegalStateException    Unable to sync file with restore
 * @throws SAXException             Unable to unmarshall tenants
 */
private synchronized void load() throws JAXBException, IllegalStateException, SAXException {
    final Tenants tenants = unmarshallTenants();
    if (tenants.getUsers() == null) {
        tenants.setUsers(new Users());
    }
    if (tenants.getGroups() == null) {
        tenants.setGroups(new Groups());
    }
    final UserGroupHolder userGroupHolder = new UserGroupHolder(tenants);
    final boolean emptyTenants = userGroupHolder.getAllUsers().isEmpty() && userGroupHolder.getAllGroups().isEmpty();
    final boolean hasLegacyAuthorizedUsers = (legacyAuthorizedUsersFile != null && !StringUtils.isBlank(legacyAuthorizedUsersFile));
    if (emptyTenants) {
        if (hasLegacyAuthorizedUsers) {
            logger.info("Loading users from legacy model " + legacyAuthorizedUsersFile + " into new users file.");
            convertLegacyAuthorizedUsers(tenants);
        }
        populateInitialUsers(tenants);
        // save any changes that were made and repopulate the holder
        saveAndRefreshHolder(tenants);
    } else {
        this.userGroupHolder.set(userGroupHolder);
    }
}
Also used : Groups(org.apache.nifi.authorization.file.tenants.generated.Groups) Tenants(org.apache.nifi.authorization.file.tenants.generated.Tenants) Users(org.apache.nifi.authorization.file.tenants.generated.Users)

Aggregations

Users (org.apache.nifi.authorization.file.tenants.generated.Users)2 File (java.io.File)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 Tenants (org.apache.nifi.authorization.file.tenants.generated.Tenants)1