Search in sources :

Example 71 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class CompositeConfigurableUserGroupProviderTest method testConfigurableUserGroupProviderWithConflictingUserGroupProvider.

@Test
public void testConfigurableUserGroupProviderWithConflictingUserGroupProvider() throws Exception {
    final UserGroupProvider userGroupProvider = initCompositeUserGroupProvider(new CompositeConfigurableUserGroupProvider(), lookup -> {
        when(lookup.getUserGroupProvider(eq(CONFIGURABLE_USER_GROUP_PROVIDER))).thenReturn(getConfigurableUserGroupProvider());
    }, configurationContext -> {
        when(configurationContext.getProperty(PROP_CONFIGURABLE_USER_GROUP_PROVIDER)).thenReturn(new StandardPropertyValue(CONFIGURABLE_USER_GROUP_PROVIDER, null));
    }, getConflictingUserGroupProvider());
    // users and groups
    assertEquals(3, userGroupProvider.getUsers().size());
    assertEquals(2, userGroupProvider.getGroups().size());
    // unknown
    assertNull(userGroupProvider.getUser(NOT_A_REAL_USER_IDENTIFIER));
    assertNull(userGroupProvider.getUserByIdentity(NOT_A_REAL_USER_IDENTITY));
    final UserAndGroups unknownUserAndGroups = userGroupProvider.getUserAndGroups(NOT_A_REAL_USER_IDENTITY);
    assertNotNull(unknownUserAndGroups);
    assertNull(unknownUserAndGroups.getUser());
    assertNull(unknownUserAndGroups.getGroups());
    // providers
    try {
        testConfigurableUserGroupProvider(userGroupProvider);
        assertTrue("Should never get here as we expect the line above to throw an exception", false);
    } catch (Exception e) {
        assertTrue(e instanceof IllegalStateException);
        assertTrue(e.getMessage().contains(USER_1_IDENTITY));
    }
    try {
        testConflictingUserGroupProvider(userGroupProvider);
        assertTrue("Should never get here as we expect the line above to throw an exception", false);
    } catch (Exception e) {
        assertTrue(e instanceof IllegalStateException);
        assertTrue(e.getMessage().contains(USER_1_IDENTITY));
    }
}
Also used : StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) Test(org.junit.Test)

Example 72 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class CompositeUserGroupProviderTest method testUnknownProvider.

@Test(expected = AuthorizerCreationException.class)
public void testUnknownProvider() throws Exception {
    // initialization
    final UserGroupProviderInitializationContext initializationContext = mock(UserGroupProviderInitializationContext.class);
    when(initializationContext.getUserGroupProviderLookup()).thenReturn(new UserGroupProviderLookup() {

        @Override
        public UserGroupProvider getUserGroupProvider(String identifier) {
            return null;
        }
    });
    // configuration
    final AuthorizerConfigurationContext configurationContext = mock(AuthorizerConfigurationContext.class);
    when(configurationContext.getProperty(eq(PROP_USER_GROUP_PROVIDER_PREFIX + "1"))).thenReturn(new StandardPropertyValue(String.valueOf("1"), null));
    mockProperties(configurationContext);
    final CompositeUserGroupProvider compositeUserGroupProvider = new CompositeUserGroupProvider();
    compositeUserGroupProvider.initialize(initializationContext);
    compositeUserGroupProvider.onConfigured(configurationContext);
}
Also used : StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Test(org.junit.Test)

Example 73 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class AbstractConfiguredComponent method getAdditionalClasspathResources.

@Override
public Set<URL> getAdditionalClasspathResources(final List<PropertyDescriptor> propertyDescriptors) {
    final Set<String> modulePaths = new LinkedHashSet<>();
    for (final PropertyDescriptor descriptor : propertyDescriptors) {
        if (descriptor.isDynamicClasspathModifier()) {
            final String value = getProperty(descriptor);
            if (!StringUtils.isEmpty(value)) {
                final StandardPropertyValue propertyValue = new StandardPropertyValue(value, null, variableRegistry);
                modulePaths.add(propertyValue.evaluateAttributeExpressions().getValue());
            }
        }
    }
    final Set<URL> additionalUrls = new LinkedHashSet<>();
    try {
        final URL[] urls = ClassLoaderUtils.getURLsForClasspath(modulePaths, null, true);
        if (urls != null) {
            for (final URL url : urls) {
                additionalUrls.add(url);
            }
        }
    } catch (MalformedURLException mfe) {
        getLogger().error("Error processing classpath resources for " + id + ": " + mfe.getMessage(), mfe);
    }
    return additionalUrls;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MalformedURLException(java.net.MalformedURLException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) URL(java.net.URL)

Example 74 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class NotificationServiceManager method createService.

/**
 * Creates a Notification Service and initializes it. Then returns the service and its configured properties
 *
 * @param serviceElement the XML element from which to build the Notification Service
 * @return a Tuple with the NotificationService as the key and the configured properties as the value, or <code>null</code> if
 *         unable to create the service
 */
private ConfiguredNotificationService createService(final Element serviceElement) {
    final Element idElement = getChild(serviceElement, "id");
    if (idElement == null) {
        logger.error("Found configuration for Notification Service with no 'id' element; this service cannot be referenced so it will not be loaded");
        return null;
    }
    final String serviceId = idElement.getTextContent().trim();
    logger.debug("Loading Notification Service with ID {}", serviceId);
    final Element classElement = getChild(serviceElement, "class");
    if (classElement == null) {
        logger.error("Found configuration for Notification Service with no 'class' element; Service ID is '{}'. This service annot be loaded", serviceId);
        return null;
    }
    final String className = classElement.getTextContent().trim();
    final Class<?> clazz;
    try {
        clazz = Class.forName(className);
    } catch (final Exception e) {
        logger.error("Found configuration for Notification Service with ID '{}' and Class '{}' but could not load class.", serviceId, className);
        logger.error("", e);
        return null;
    }
    if (!NotificationService.class.isAssignableFrom(clazz)) {
        logger.error("Found configuration for Notification Service with ID '{}' and Class '{}' but class is not a Notification Service.", serviceId, className);
        return null;
    }
    final Object serviceObject;
    try {
        serviceObject = clazz.newInstance();
    } catch (final Exception e) {
        logger.error("Found configuration for Notification Service with ID '{}' and Class '{}' but could not instantiate Notification Service.", serviceId, className);
        logger.error("", e);
        return null;
    }
    final Map<String, String> propertyValues = new HashMap<>();
    final List<Element> propertyElements = getChildElementsByTagName(serviceElement, "property");
    for (final Element propertyElement : propertyElements) {
        final String propName = propertyElement.getAttribute("name");
        if (propName == null || propName.trim().isEmpty()) {
            logger.warn("Found configuration for Notification Service with ID '{}' that has property value configured but no name for the property.", serviceId);
            continue;
        }
        final String propValue = propertyElement.getTextContent().trim();
        propertyValues.put(propName, propValue);
    }
    final NotificationService service = (NotificationService) serviceObject;
    try {
        service.initialize(new NotificationInitializationContext() {

            @Override
            public PropertyValue getProperty(final PropertyDescriptor descriptor) {
                final String propName = descriptor.getName();
                String value = propertyValues.get(propName);
                if (value == null) {
                    value = descriptor.getDefaultValue();
                }
                return new StandardPropertyValue(value, null, variableRegistry);
            }

            @Override
            public Map<String, String> getAllProperties() {
                return Collections.unmodifiableMap(propertyValues);
            }

            @Override
            public String getIdentifier() {
                return serviceId;
            }
        });
    } catch (final Exception e) {
        logger.error("Failed to load Notification Service with ID '{}'", serviceId);
        logger.error("", e);
    }
    return new ConfiguredNotificationService(service, propertyValues);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) NotificationService(org.apache.nifi.bootstrap.notification.NotificationService) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) NotificationInitializationContext(org.apache.nifi.bootstrap.notification.NotificationInitializationContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 75 with StandardPropertyValue

use of org.apache.nifi.attribute.expression.language.StandardPropertyValue in project nifi by apache.

the class LdapUserGroupProviderTest method testSearchUsersAndGroupsMembershipThroughUsersAndGroups.

@Test
public void testSearchUsersAndGroupsMembershipThroughUsersAndGroups() throws Exception {
    final AuthorizerConfigurationContext configurationContext = getBaseConfiguration(USER_SEARCH_BASE, GROUP_SEARCH_BASE);
    when(configurationContext.getProperty(PROP_USER_IDENTITY_ATTRIBUTE)).thenReturn(new StandardPropertyValue("uid", null));
    // using description in lieu of memberof
    when(configurationContext.getProperty(PROP_USER_GROUP_ATTRIBUTE)).thenReturn(new StandardPropertyValue("description", null));
    when(configurationContext.getProperty(PROP_GROUP_MEMBER_ATTRIBUTE)).thenReturn(new StandardPropertyValue("member", null));
    when(configurationContext.getProperty(PROP_GROUP_NAME_ATTRIBUTE)).thenReturn(new StandardPropertyValue("cn", null));
    ldapUserGroupProvider.onConfigured(configurationContext);
    assertEquals(8, ldapUserGroupProvider.getUsers().size());
    final Set<Group> groups = ldapUserGroupProvider.getGroups();
    assertEquals(4, groups.size());
    final Group admins = groups.stream().filter(group -> "admins".equals(group.getName())).findFirst().orElse(null);
    assertNotNull(admins);
    assertEquals(2, admins.getUsers().size());
    assertEquals(2, admins.getUsers().stream().map(userIdentifier -> ldapUserGroupProvider.getUser(userIdentifier)).filter(user -> "user1".equals(user.getIdentity()) || "user3".equals(user.getIdentity())).count());
    final Group readOnly = groups.stream().filter(group -> "read-only".equals(group.getName())).findFirst().orElse(null);
    assertNotNull(readOnly);
    assertEquals(1, readOnly.getUsers().size());
    assertEquals(1, readOnly.getUsers().stream().map(userIdentifier -> ldapUserGroupProvider.getUser(userIdentifier)).filter(user -> "user2".equals(user.getIdentity())).count());
    final Group team1 = groups.stream().filter(group -> "team1".equals(group.getName())).findFirst().orElse(null);
    assertNotNull(team1);
    assertEquals(3, team1.getUsers().size());
    assertEquals(3, team1.getUsers().stream().map(userIdentifier -> ldapUserGroupProvider.getUser(userIdentifier)).filter(user -> "user1".equals(user.getIdentity()) || "user4".equals(user.getIdentity()) || "user5".equals(user.getIdentity())).count());
    final Group team2 = groups.stream().filter(group -> "team2".equals(group.getName())).findFirst().orElse(null);
    assertNotNull(team2);
    assertEquals(3, team2.getUsers().size());
    assertEquals(3, team2.getUsers().stream().map(userIdentifier -> ldapUserGroupProvider.getUser(userIdentifier)).filter(user -> "user1".equals(user.getIdentity()) || "user6".equals(user.getIdentity()) || "user7".equals(user.getIdentity())).count());
}
Also used : CreateTransport(org.apache.directory.server.annotations.CreateTransport) PROP_GROUP_NAME_ATTRIBUTE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_GROUP_NAME_ATTRIBUTE) PROP_SYNC_INTERVAL(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_SYNC_INTERVAL) PROP_USER_IDENTITY_ATTRIBUTE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_USER_IDENTITY_ATTRIBUTE) PROP_USER_SEARCH_FILTER(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_USER_SEARCH_FILTER) CreateLdapServer(org.apache.directory.server.annotations.CreateLdapServer) CreatePartition(org.apache.directory.server.core.annotations.CreatePartition) PROP_MANAGER_PASSWORD(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_MANAGER_PASSWORD) PROP_GROUP_SEARCH_BASE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_GROUP_SEARCH_BASE) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) PROP_USER_GROUP_REFERENCED_GROUP_ATTRIBUTE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_USER_GROUP_REFERENCED_GROUP_ATTRIBUTE) PROP_AUTHENTICATION_STRATEGY(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_AUTHENTICATION_STRATEGY) Set(java.util.Set) AuthorizerConfigurationContext(org.apache.nifi.authorization.AuthorizerConfigurationContext) PROP_GROUP_OBJECT_CLASS(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_GROUP_OBJECT_CLASS) UserAndGroups(org.apache.nifi.authorization.UserAndGroups) PROP_GROUP_MEMBER_REFERENCED_USER_ATTRIBUTE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_GROUP_MEMBER_REFERENCED_USER_ATTRIBUTE) Assert.assertFalse(org.junit.Assert.assertFalse) PROP_CONNECT_TIMEOUT(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_CONNECT_TIMEOUT) PROP_GROUP_SEARCH_SCOPE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_GROUP_SEARCH_SCOPE) Mockito.mock(org.mockito.Mockito.mock) UserGroupProviderInitializationContext(org.apache.nifi.authorization.UserGroupProviderInitializationContext) PROP_USER_SEARCH_BASE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_USER_SEARCH_BASE) RunWith(org.junit.runner.RunWith) CreateDS(org.apache.directory.server.core.annotations.CreateDS) Group(org.apache.nifi.authorization.Group) PROP_PAGE_SIZE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_PAGE_SIZE) PROP_REFERRAL_STRATEGY(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_REFERRAL_STRATEGY) PROP_USER_OBJECT_CLASS(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_USER_OBJECT_CLASS) Matchers.anyString(org.mockito.Matchers.anyString) ApplyLdifFiles(org.apache.directory.server.core.annotations.ApplyLdifFiles) PROP_USER_GROUP_ATTRIBUTE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_USER_GROUP_ATTRIBUTE) FrameworkRunner(org.apache.directory.server.core.integ.FrameworkRunner) PROP_READ_TIMEOUT(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_READ_TIMEOUT) Before(org.junit.Before) PROP_USER_SEARCH_SCOPE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_USER_SEARCH_SCOPE) PROP_GROUP_MEMBER_ATTRIBUTE(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_GROUP_MEMBER_ATTRIBUTE) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) Properties(java.util.Properties) PROP_URL(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_URL) Assert.assertNotNull(org.junit.Assert.assertNotNull) PROP_MANAGER_DN(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_MANAGER_DN) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito(org.mockito.Mockito) ReferralStrategy(org.apache.nifi.ldap.ReferralStrategy) NiFiProperties(org.apache.nifi.util.NiFiProperties) AbstractLdapTestUnit(org.apache.directory.server.core.integ.AbstractLdapTestUnit) LdapAuthenticationStrategy(org.apache.nifi.ldap.LdapAuthenticationStrategy) PROP_GROUP_SEARCH_FILTER(org.apache.nifi.ldap.tenants.LdapUserGroupProvider.PROP_GROUP_SEARCH_FILTER) Assert.assertEquals(org.junit.Assert.assertEquals) Group(org.apache.nifi.authorization.Group) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) AuthorizerConfigurationContext(org.apache.nifi.authorization.AuthorizerConfigurationContext) Test(org.junit.Test)

Aggregations

StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)91 Test (org.junit.Test)78 AuthorizerConfigurationContext (org.apache.nifi.authorization.AuthorizerConfigurationContext)33 Matchers.anyString (org.mockito.Matchers.anyString)30 NiFiProperties (org.apache.nifi.util.NiFiProperties)24 PropertyValue (org.apache.nifi.components.PropertyValue)17 HashMap (java.util.HashMap)16 Properties (java.util.Properties)15 Group (org.apache.nifi.authorization.Group)12 Before (org.junit.Before)12 Set (java.util.Set)10 UserAndGroups (org.apache.nifi.authorization.UserAndGroups)10 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)9 CreateLdapServer (org.apache.directory.server.annotations.CreateLdapServer)8 CreateTransport (org.apache.directory.server.annotations.CreateTransport)8 ApplyLdifFiles (org.apache.directory.server.core.annotations.ApplyLdifFiles)8 CreateDS (org.apache.directory.server.core.annotations.CreateDS)8 CreatePartition (org.apache.directory.server.core.annotations.CreatePartition)8 AbstractLdapTestUnit (org.apache.directory.server.core.integ.AbstractLdapTestUnit)8 FrameworkRunner (org.apache.directory.server.core.integ.FrameworkRunner)8