Search in sources :

Example 6 with Policy

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

the class FileAccessPolicyProvider method getOrCreatePolicy.

/**
 * Finds the Policy matching the resource and action, or creates a new one and adds it to the list of policies.
 *
 * @param policies the policies to search through
 * @param seedIdentity the seedIdentity to use when creating identifiers for new policies
 * @param resource the resource for the policy
 * @param action the action string for the police (R or RW)
 * @return the matching policy or a new policy
 */
private Policy getOrCreatePolicy(final List<Policy> policies, final String seedIdentity, final String resource, final String action) {
    Policy foundPolicy = null;
    // try to find a policy with the same resource and actions
    for (Policy policy : policies) {
        if (policy.getResource().equals(resource) && policy.getAction().equals(action)) {
            foundPolicy = policy;
            break;
        }
    }
    // if a matching policy wasn't found then create one
    if (foundPolicy == null) {
        final String uuidSeed = resource + action + seedIdentity;
        final String policyIdentifier = IdentifierUtil.getIdentifier(uuidSeed);
        foundPolicy = new Policy();
        foundPolicy.setIdentifier(policyIdentifier);
        foundPolicy.setResource(resource);
        foundPolicy.setAction(action);
        policies.add(foundPolicy);
    }
    return foundPolicy;
}
Also used : Policy(org.apache.nifi.authorization.file.generated.Policy)

Example 7 with Policy

use of org.apache.nifi.authorization.file.generated.Policy 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

Policy (org.apache.nifi.authorization.file.generated.Policy)7 Authorizations (org.apache.nifi.authorization.file.generated.Authorizations)3 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 JAXBException (javax.xml.bind.JAXBException)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 Users (org.apache.nifi.user.generated.Users)1 PortDTO (org.apache.nifi.web.api.dto.PortDTO)1