kevdoran commented on a change in pull request #232: NIFIREG-292 Add DB impls of UserGroupProvider and AccessPolicyProvider URL: https://github.com/apache/nifi-registry/pull/232#discussion_r336050282
########## File path: nifi-registry-core/nifi-registry-framework/src/main/java/org/apache/nifi/registry/security/authorization/database/DatabaseAccessPolicyProvider.java ########## @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.registry.security.authorization.database; + +import org.apache.commons.lang3.StringUtils; +import org.apache.nifi.registry.properties.NiFiRegistryProperties; +import org.apache.nifi.registry.properties.util.IdentityMapping; +import org.apache.nifi.registry.properties.util.IdentityMappingUtil; +import org.apache.nifi.registry.security.authorization.AuthorizerConfigurationContext; +import org.apache.nifi.registry.security.authorization.Group; +import org.apache.nifi.registry.security.authorization.User; +import org.apache.nifi.registry.security.authorization.annotation.AuthorizerContext; +import org.apache.nifi.registry.security.authorization.util.AccessPolicyProviderUtils; +import org.apache.nifi.registry.security.authorization.util.InitialPolicies; +import org.apache.nifi.registry.security.authorization.util.ResourceAndAction; +import org.apache.nifi.registry.security.exception.SecurityProviderCreationException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.CollectionUtils; + +import javax.sql.DataSource; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +/** + * The NiFi Registry specific implementation of an ConfigurableAccessPolicyProvider backed by database. + */ +public class DatabaseAccessPolicyProvider extends AbstractDatabaseAccessPolicyProvider { + + private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseAccessPolicyProvider.class); + + private DataSource dataSource; + private NiFiRegistryProperties properties; + + @AuthorizerContext + public void setDataSource(final DataSource dataSource) { + this.dataSource = dataSource; + } + + @AuthorizerContext + public void setProperties(final NiFiRegistryProperties properties) { + this.properties = properties; + } + + @Override + protected DataSource getDataSource() { + return dataSource; + } + + @Override + protected void populateInitialPolicies(final AuthorizerConfigurationContext configurationContext) { + final List<IdentityMapping> identityMappings = Collections.unmodifiableList(IdentityMappingUtil.getIdentityMappings(properties)); + final List<IdentityMapping> groupMappings = Collections.unmodifiableList(IdentityMappingUtil.getGroupMappings(properties)); + + final String initialAdminIdentity = AccessPolicyProviderUtils.getInitialAdminIdentity(configurationContext, identityMappings); + final Set<String> nifiIdentities = AccessPolicyProviderUtils.getNiFiIdentities(configurationContext, identityMappings); + final String nifiGroupName = AccessPolicyProviderUtils.getNiFiGroupName(configurationContext, groupMappings); + + if (!StringUtils.isBlank(initialAdminIdentity)) { + LOGGER.info("Populating authorizations for Initial Admin: '" + initialAdminIdentity + "'"); + populateInitialAdmin(initialAdminIdentity); + } + + if (!CollectionUtils.isEmpty(nifiIdentities)) { + LOGGER.info("Populating authorizations for NiFi identities: [{}]", StringUtils.join(nifiIdentities, ";")); + populateNiFiIdentities(nifiIdentities); + } + + if (!StringUtils.isBlank(nifiGroupName)) { + LOGGER.info("Populating authorizations for NiFi Group: '" + nifiGroupName + "'"); + populateNiFiGroup(nifiGroupName); + } + } + Review comment: Cool that all sounds good. Good point about the DataSource not always being available depending on the app/context. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
