Search in sources :

Example 41 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project nifi by apache.

the class TemplateDeserializer method deserialize.

public static TemplateDTO deserialize(final StreamSource source) {
    try {
        JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
        XMLStreamReader xsr = XmlUtils.createSafeReader(source);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(xsr, TemplateDTO.class);
        return templateElement.getValue();
    } catch (final JAXBException | XMLStreamException e) {
        throw new FlowSerializationException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) TemplateDTO(org.apache.nifi.web.api.dto.TemplateDTO) JAXBException(javax.xml.bind.JAXBException) FlowSerializationException(org.apache.nifi.controller.serialization.FlowSerializationException) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 42 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project nifi by apache.

the class HeartbeatPayload method unmarshal.

public static HeartbeatPayload unmarshal(final InputStream is) throws ProtocolException {
    try {
        final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
        final XMLStreamReader xsr = XmlUtils.createSafeReader(is);
        return (HeartbeatPayload) unmarshaller.unmarshal(xsr);
    } catch (final JAXBException | XMLStreamException e) {
        throw new ProtocolException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 43 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project nifi by apache.

the class JaxbProtocolContext method createUnmarshaller.

@Override
public ProtocolMessageUnmarshaller<T> createUnmarshaller() {
    return new ProtocolMessageUnmarshaller<T>() {

        @Override
        public T unmarshal(final InputStream is) throws IOException {
            try {
                final DataInputStream dis = new DataInputStream(is);
                // check for the presence of the message protocol sentinel
                final byte sentinel = (byte) dis.read();
                if (sentinel == -1) {
                    throw new EOFException();
                }
                if (MESSAGE_PROTOCOL_START_SENTINEL != sentinel) {
                    throw new IOException("Failed reading protocol message due to malformed header");
                }
                // read the message size
                final int msgBytesSize = dis.readInt();
                // read the message
                final ByteBuffer buffer = ByteBuffer.allocate(msgBytesSize);
                int totalBytesRead = 0;
                do {
                    final int bytesToRead;
                    if ((msgBytesSize - totalBytesRead) >= BUF_SIZE) {
                        bytesToRead = BUF_SIZE;
                    } else {
                        bytesToRead = msgBytesSize - totalBytesRead;
                    }
                    totalBytesRead += dis.read(buffer.array(), totalBytesRead, bytesToRead);
                } while (totalBytesRead < msgBytesSize);
                // unmarshall message and return
                final Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
                final byte[] msg = new byte[totalBytesRead];
                buffer.get(msg);
                final XMLStreamReader xsr = XmlUtils.createSafeReader(new ByteArrayInputStream(msg));
                return (T) unmarshaller.unmarshal(xsr);
            } catch (final JAXBException | XMLStreamException e) {
                throw new IOException("Failed unmarshalling protocol message due to: " + e, e);
            }
        }
    };
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer) ProtocolMessageUnmarshaller(org.apache.nifi.cluster.protocol.ProtocolMessageUnmarshaller) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) Unmarshaller(javax.xml.bind.Unmarshaller) ProtocolMessageUnmarshaller(org.apache.nifi.cluster.protocol.ProtocolMessageUnmarshaller)

Example 44 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader 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 45 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project nifi by apache.

the class FileAccessPolicyProvider method convertLegacyAuthorizedUsers.

/**
 * Unmarshalls an existing authorized-users.xml and converts the object model to the new model.
 *
 * @param authorizations the current Authorizations instance that policies 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 Authorizations authorizations) throws AuthorizerCreationException, JAXBException {
    final File authorizedUsersFile = new File(legacyAuthorizedUsersFile);
    if (!authorizedUsersFile.exists()) {
        throw new AuthorizerCreationException("Legacy Authorized Users File '" + legacyAuthorizedUsersFile + "' does not exists");
    }
    final Unmarshaller unmarshaller = JAXB_USERS_CONTEXT.createUnmarshaller();
    unmarshaller.setSchema(usersSchema);
    final XMLStreamReader xsr;
    try {
        xsr = XmlUtils.createSafeReader(new StreamSource(authorizedUsersFile));
    } catch (XMLStreamException e) {
        logger.error("Encountered an error reading authorized users file: ", e);
        throw new JAXBException("Error reading authorized users file", e);
    }
    final JAXBElement<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;
    }
    // get all the user DNs into a list
    List<String> userIdentities = new ArrayList<>();
    for (org.apache.nifi.user.generated.User legacyUser : users.getUser()) {
        userIdentities.add(IdentityMappingUtil.mapIdentity(legacyUser.getDn(), identityMappings));
    }
    // sort the list and pull out the first identity
    Collections.sort(userIdentities);
    final String seedIdentity = userIdentities.get(0);
    // create mapping from Role to access policies
    final Map<Role, Set<RoleAccessPolicy>> roleAccessPolicies = RoleAccessPolicy.getMappings(rootGroupId);
    final List<Policy> allPolicies = new ArrayList<>();
    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);
        final User user = userGroupProvider.getUserByIdentity(legacyUserDn);
        if (user == null) {
            throw new AuthorizerCreationException("Unable to locate legacy user " + legacyUserDn + " to seed policies.");
        }
        // create policies based on the given role
        for (org.apache.nifi.user.generated.Role jaxbRole : legacyUser.getRole()) {
            Role role = Role.valueOf(jaxbRole.getName());
            Set<RoleAccessPolicy> policies = roleAccessPolicies.get(role);
            for (RoleAccessPolicy roleAccessPolicy : policies) {
                // get the matching policy, or create a new one
                Policy policy = getOrCreatePolicy(allPolicies, seedIdentity, roleAccessPolicy.getResource(), roleAccessPolicy.getAction());
                // add the user to the policy if it doesn't exist
                addUserToPolicy(user.getIdentifier(), policy);
            }
        }
    }
    // convert any access controls on ports to the appropriate policies
    for (PortDTO portDTO : ports) {
        final Resource resource;
        if (portDTO.getType() != null && portDTO.getType().equals("inputPort")) {
            resource = ResourceFactory.getDataTransferResource(ResourceFactory.getComponentResource(ResourceType.InputPort, portDTO.getId(), portDTO.getName()));
        } else {
            resource = ResourceFactory.getDataTransferResource(ResourceFactory.getComponentResource(ResourceType.OutputPort, portDTO.getId(), portDTO.getName()));
        }
        if (portDTO.getUserAccessControl() != null) {
            for (String userAccessControl : portDTO.getUserAccessControl()) {
                // need to perform the identity mapping on the access control so it matches the identities in the User objects
                final String mappedUserAccessControl = IdentityMappingUtil.mapIdentity(userAccessControl, identityMappings);
                final User foundUser = userGroupProvider.getUserByIdentity(mappedUserAccessControl);
                // couldn't find the user matching the access control so log a warning and skip
                if (foundUser == null) {
                    logger.warn("Found port with user access control for {} but no user exists with this identity, skipping...", new Object[] { mappedUserAccessControl });
                    continue;
                }
                // we found the user so create the appropriate policy and add the user to it
                Policy policy = getOrCreatePolicy(allPolicies, seedIdentity, resource.getIdentifier(), WRITE_CODE);
                addUserToPolicy(foundUser.getIdentifier(), policy);
            }
        }
        if (portDTO.getGroupAccessControl() != null) {
            for (String groupAccessControl : portDTO.getGroupAccessControl()) {
                // find a group where the name is the groupAccessControl
                Group foundGroup = null;
                for (Group group : userGroupProvider.getGroups()) {
                    if (group.getName().equals(groupAccessControl)) {
                        foundGroup = group;
                        break;
                    }
                }
                // couldn't find the group matching the access control so log a warning and skip
                if (foundGroup == null) {
                    logger.warn("Found port with group access control for {} but no group exists with this name, skipping...", new Object[] { groupAccessControl });
                    continue;
                }
                // we found the group so create the appropriate policy and add all the users to it
                Policy policy = getOrCreatePolicy(allPolicies, seedIdentity, resource.getIdentifier(), WRITE_CODE);
                addGroupToPolicy(IdentifierUtil.getIdentifier(groupAccessControl), policy);
            }
        }
    }
    authorizations.getPolicies().getPolicy().addAll(allPolicies);
}
Also used : Policy(org.apache.nifi.authorization.file.generated.Policy) XMLStreamReader(javax.xml.stream.XMLStreamReader) Set(java.util.Set) HashSet(java.util.HashSet) Users(org.apache.nifi.user.generated.Users) ArrayList(java.util.ArrayList) Users(org.apache.nifi.user.generated.Users) Unmarshaller(javax.xml.bind.Unmarshaller) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) PortDTO(org.apache.nifi.web.api.dto.PortDTO) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(javax.xml.bind.JAXBException) XMLStreamException(javax.xml.stream.XMLStreamException) File(java.io.File)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)1074 Test (org.junit.Test)486 InputStream (java.io.InputStream)451 ByteArrayInputStream (java.io.ByteArrayInputStream)379 ByteArrayOutputStream (java.io.ByteArrayOutputStream)334 Document (org.w3c.dom.Document)311 XMLStreamException (javax.xml.stream.XMLStreamException)288 ArrayList (java.util.ArrayList)270 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)242 XMLInputFactory (javax.xml.stream.XMLInputFactory)211 QName (javax.xml.namespace.QName)208 DOMSource (javax.xml.transform.dom.DOMSource)206 StringReader (java.io.StringReader)196 SecretKey (javax.crypto.SecretKey)188 StreamResult (javax.xml.transform.stream.StreamResult)183 DocumentBuilder (javax.xml.parsers.DocumentBuilder)178 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)160 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)155 IOException (java.io.IOException)144 Key (java.security.Key)103