Search in sources :

Example 1 with Authorizations

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

the class FileAccessPolicyProvider method deleteAccessPolicy.

@Override
public synchronized AccessPolicy deleteAccessPolicy(AccessPolicy accessPolicy) throws AuthorizationAccessException {
    if (accessPolicy == null) {
        throw new IllegalArgumentException("AccessPolicy cannot be null");
    }
    final AuthorizationsHolder holder = this.authorizationsHolder.get();
    final Authorizations authorizations = holder.getAuthorizations();
    // find the matching Policy and remove it
    boolean deletedPolicy = false;
    Iterator<Policy> policyIter = authorizations.getPolicies().getPolicy().iterator();
    while (policyIter.hasNext()) {
        final Policy policy = policyIter.next();
        if (policy.getIdentifier().equals(accessPolicy.getIdentifier())) {
            policyIter.remove();
            deletedPolicy = true;
            break;
        }
    }
    // never found a matching Policy so return null
    if (!deletedPolicy) {
        return null;
    }
    saveAndRefreshHolder(authorizations);
    return accessPolicy;
}
Also used : Policy(org.apache.nifi.authorization.file.generated.Policy) Authorizations(org.apache.nifi.authorization.file.generated.Authorizations)

Example 2 with Authorizations

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

the class FileAccessPolicyProvider method unmarshallAuthorizations.

private Authorizations unmarshallAuthorizations() throws JAXBException {
    try {
        final XMLStreamReader xsr = XmlUtils.createSafeReader(new StreamSource(authorizationsFile));
        final Unmarshaller unmarshaller = JAXB_AUTHORIZATIONS_CONTEXT.createUnmarshaller();
        unmarshaller.setSchema(authorizationsSchema);
        final JAXBElement<Authorizations> element = unmarshaller.unmarshal(xsr, Authorizations.class);
        return element.getValue();
    } catch (XMLStreamException e) {
        logger.error("Encountered an error reading authorizations file: ", e);
        throw new JAXBException("Error reading authorizations file", e);
    }
}
Also used : Authorizations(org.apache.nifi.authorization.file.generated.Authorizations) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(javax.xml.bind.JAXBException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 3 with Authorizations

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

the class FileAccessPolicyProvider method onConfigured.

@Override
public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
    try {
        final PropertyValue userGroupProviderIdentifier = configurationContext.getProperty(PROP_USER_GROUP_PROVIDER);
        if (!userGroupProviderIdentifier.isSet()) {
            throw new AuthorizerCreationException("The user group provider must be specified.");
        }
        userGroupProvider = userGroupProviderLookup.getUserGroupProvider(userGroupProviderIdentifier.getValue());
        if (userGroupProvider == null) {
            throw new AuthorizerCreationException("Unable to locate user group provider with identifier " + userGroupProviderIdentifier.getValue());
        }
        final PropertyValue authorizationsPath = configurationContext.getProperty(PROP_AUTHORIZATIONS_FILE);
        if (StringUtils.isBlank(authorizationsPath.getValue())) {
            throw new AuthorizerCreationException("The authorizations file must be specified.");
        }
        // get the authorizations file and ensure it exists
        authorizationsFile = new File(authorizationsPath.getValue());
        if (!authorizationsFile.exists()) {
            logger.info("Creating new authorizations file at {}", new Object[] { authorizationsFile.getAbsolutePath() });
            saveAuthorizations(new Authorizations());
        }
        final File authorizationsFileDirectory = authorizationsFile.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 authorizations directory
            if (authorizationsFileDirectory.getAbsolutePath().equals(restoreDirectory.getAbsolutePath())) {
                throw new AuthorizerCreationException(String.format("Authorizations file directory '%s' is the same as restore directory '%s' ", authorizationsFileDirectory.getAbsolutePath(), restoreDirectory.getAbsolutePath()));
            }
            // the restore copy will have same file name, but reside in a different directory
            restoreAuthorizationsFile = new File(restoreDirectory, authorizationsFile.getName());
            try {
                // sync the primary copy with the restore copy
                FileUtils.syncWithRestore(authorizationsFile, restoreAuthorizationsFile, 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 initial admin identity
        final PropertyValue initialAdminIdentityProp = configurationContext.getProperty(PROP_INITIAL_ADMIN_IDENTITY);
        initialAdminIdentity = initialAdminIdentityProp.isSet() ? IdentityMappingUtil.mapIdentity(initialAdminIdentityProp.getValue(), identityMappings) : null;
        // 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
        nodeIdentities = new HashSet<>();
        for (Map.Entry<String, String> entry : configurationContext.getProperties().entrySet()) {
            Matcher matcher = NODE_IDENTITY_PATTERN.matcher(entry.getKey());
            if (matcher.matches() && !StringUtils.isBlank(entry.getValue())) {
                nodeIdentities.add(IdentityMappingUtil.mapIdentity(entry.getValue(), identityMappings));
            }
        }
        // load the authorizations
        load();
        // if we've copied the authorizations file to a restore directory synchronize it
        if (restoreAuthorizationsFile != null) {
            FileUtils.copyFile(authorizationsFile, restoreAuthorizationsFile, false, false, logger);
        }
        logger.info(String.format("Authorizations file loaded at %s", new Date().toString()));
    } catch (IOException | AuthorizerCreationException | JAXBException | IllegalStateException | SAXException e) {
        throw new AuthorizerCreationException(e);
    }
}
Also used : Authorizations(org.apache.nifi.authorization.file.generated.Authorizations) Matcher(java.util.regex.Matcher) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) JAXBException(javax.xml.bind.JAXBException) PropertyValue(org.apache.nifi.components.PropertyValue) IOException(java.io.IOException) Date(java.util.Date) SAXException(org.xml.sax.SAXException) File(java.io.File) Map(java.util.Map)

Example 4 with Authorizations

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

the class FileAccessPolicyProvider method addAccessPolicy.

@Override
public synchronized AccessPolicy addAccessPolicy(AccessPolicy accessPolicy) throws AuthorizationAccessException {
    if (accessPolicy == null) {
        throw new IllegalArgumentException("AccessPolicy cannot be null");
    }
    // create the new JAXB Policy
    final Policy policy = createJAXBPolicy(accessPolicy);
    // add the new Policy to the top-level list of policies
    final AuthorizationsHolder holder = authorizationsHolder.get();
    final Authorizations authorizations = holder.getAuthorizations();
    authorizations.getPolicies().getPolicy().add(policy);
    saveAndRefreshHolder(authorizations);
    return authorizationsHolder.get().getPoliciesById().get(accessPolicy.getIdentifier());
}
Also used : Policy(org.apache.nifi.authorization.file.generated.Policy) Authorizations(org.apache.nifi.authorization.file.generated.Authorizations)

Example 5 with Authorizations

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

the class FileAccessPolicyProvider method updateAccessPolicy.

@Override
public synchronized AccessPolicy updateAccessPolicy(AccessPolicy accessPolicy) throws AuthorizationAccessException {
    if (accessPolicy == null) {
        throw new IllegalArgumentException("AccessPolicy cannot be null");
    }
    final AuthorizationsHolder holder = this.authorizationsHolder.get();
    final Authorizations authorizations = holder.getAuthorizations();
    // try to find an existing Authorization that matches the policy id
    Policy updatePolicy = null;
    for (Policy policy : authorizations.getPolicies().getPolicy()) {
        if (policy.getIdentifier().equals(accessPolicy.getIdentifier())) {
            updatePolicy = policy;
            break;
        }
    }
    // no matching Policy so return null
    if (updatePolicy == null) {
        return null;
    }
    // update the Policy, save, reload, and return
    transferUsersAndGroups(accessPolicy, updatePolicy);
    saveAndRefreshHolder(authorizations);
    return this.authorizationsHolder.get().getPoliciesById().get(accessPolicy.getIdentifier());
}
Also used : Policy(org.apache.nifi.authorization.file.generated.Policy) Authorizations(org.apache.nifi.authorization.file.generated.Authorizations)

Aggregations

Authorizations (org.apache.nifi.authorization.file.generated.Authorizations)6 Policy (org.apache.nifi.authorization.file.generated.Policy)3 JAXBException (javax.xml.bind.JAXBException)2 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)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 Policies (org.apache.nifi.authorization.file.generated.Policies)1 PropertyValue (org.apache.nifi.components.PropertyValue)1 SAXException (org.xml.sax.SAXException)1