bbende 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_r336044845
########## 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: These are some good points. I think you are right that this one of those cases where you can say "composition over inheritance", since the abstract class wasn't really for polymorphism. I like the idea of the `IdentityMapper` interface and I think we can inject that into the authorizer from the factory the same we are injecting the `DataSource` and `NiFiRegistryProperties` currently (I agree on possibly changing how that works in the future). I would rather do the custom injection in the factory rather then add the methods to the `AuthorizerConfigurationContext`, only because the context class is part of the API and used across all impls, and an application may not have a `DataSource` to provide. The `IdentityMapper` could make sense there since that interface could be part of the API, but it also blurs the lines a little on what the config context is, currently it represents the properties from the config file. Long story short, I'll work on some updates around the suggestions here. ---------------------------------------------------------------- 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
