Skip to content

Commit

Permalink
Improve the user yaml configuration (apache#10064)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristaZero authored Apr 13, 2021
1 parent eae7b25 commit 1dfce0f
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfigurationConverter;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
import org.apache.shardingsphere.infra.metadata.mapper.event.dcl.impl.CreateUserStatementEvent;
import org.apache.shardingsphere.infra.metadata.mapper.event.dcl.impl.GrantStatementEvent;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
Expand Down Expand Up @@ -192,22 +192,22 @@ private YamlRootRuleConfigurations createYamlRootRuleConfigurations(final String

private void persistUsers(final Collection<ShardingSphereUser> users, final boolean isOverwrite) {
if (!users.isEmpty() && (isOverwrite || !hasUsers())) {
repository.persist(node.getUsersNode(), YamlEngine.marshal(YamlUserConfigurationConverter.convertYamlUserConfigurations(users)));
repository.persist(node.getUsersNode(), YamlEngine.marshal(YamlUsersConfigurationConverter.convertYamlUserConfigurations(users)));
}
}

private void persistNewUsers(final Collection<ShardingSphereUser> users) {
if (!users.isEmpty()) {
Collection<String> yamlUsers = YamlEngine.unmarshal(repository.get(node.getUsersNode()), Collection.class);
Collection<String> newUsers = new LinkedHashSet<>(YamlUserConfigurationConverter.convertYamlUserConfigurations(users));
Collection<String> newUsers = new LinkedHashSet<>(YamlUsersConfigurationConverter.convertYamlUserConfigurations(users));
newUsers.addAll(yamlUsers);
repository.persist(node.getUsersNode(), YamlEngine.marshal(newUsers));
}
}

private void persistChangedPrivilege(final Collection<ShardingSphereUser> users) {
if (!users.isEmpty()) {
repository.persist(node.getPrivilegeNodePath(), YamlEngine.marshal(YamlUserConfigurationConverter.convertYamlUserConfigurations(users)));
repository.persist(node.getPrivilegeNodePath(), YamlEngine.marshal(YamlUsersConfigurationConverter.convertYamlUserConfigurations(users)));
}
}

Expand Down Expand Up @@ -259,12 +259,10 @@ public Collection<RuleConfiguration> loadRuleConfigurations(final String schemaN
/**
* Load users.
*
* @return authority
* @return users
*/
public Collection<ShardingSphereUser> loadUsers() {
return hasUsers()
? YamlConfigurationConverter.convertUsers(repository.get(node.getUsersNode()))
: Collections.emptyList();
return hasUsers() ? YamlConfigurationConverter.convertUsers(repository.get(node.getUsersNode())) : Collections.emptyList();
}

/**
Expand Down Expand Up @@ -461,17 +459,17 @@ public synchronized void renew(final RuleConfigurationCachedEvent event) {
}

/**
* User configuration cached event.
* User configuration event.
*
* @param event user configuration cached event
* @param event user configuration event
*/
@Subscribe
public synchronized void renew(final CreateUserStatementEvent event) {
persistNewUsers(event.getUsers());
}

/**
* User with changed privilege cached event.
* User with changed privilege event.
*
* @param event grant event
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfigurationConverter;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
Expand Down Expand Up @@ -113,7 +113,7 @@ public static Collection<RuleConfiguration> convertRuleConfigurations(final Stri
*/
public static Collection<ShardingSphereUser> convertUsers(final String yamlContent) {
Collection<String> users = YamlEngine.unmarshal(yamlContent, Collection.class);
return YamlUserConfigurationConverter.convertShardingSphereUser(users);
return YamlUsersConfigurationConverter.convertShardingSphereUser(users);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.apache.shardingsphere.infra.metadata.schema.refresher.event.SchemaAlteredEvent;
import org.apache.shardingsphere.infra.metadata.user.Grantee;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfigurationConverter;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
Expand Down Expand Up @@ -318,7 +318,7 @@ public void assertPersistConfigurationForShadow() {
@Test
public void assertPersistGlobalConfiguration() {
RegistryCenter registryCenter = new RegistryCenter(registryRepository);
registryCenter.persistGlobalConfiguration(YamlUserConfigurationConverter.convertShardingSphereUser(YamlEngine.unmarshal(readYAML(USERS_YAML), Collection.class)), createProperties(), true);
registryCenter.persistGlobalConfiguration(YamlUsersConfigurationConverter.convertShardingSphereUser(YamlEngine.unmarshal(readYAML(USERS_YAML), Collection.class)), createProperties(), true);
verify(registryRepository, times(0)).persist("/users", readYAML(USERS_YAML));
verify(registryRepository).persist("/props", PROPS_YAML);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import java.util.stream.Collectors;

/**
* Configuration converter for YAML User content.
* Configuration converter for YAML Users content.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class YamlUserConfigurationConverter {
public final class YamlUsersConfigurationConverter {

private static final UserYamlSwapper USER_YAML_SWAPPER = new UserYamlSwapper();

Expand All @@ -43,7 +43,7 @@ public final class YamlUserConfigurationConverter {
*/
public static Collection<String> convertYamlUserConfigurations(final Collection<ShardingSphereUser> users) {
Collection<String> result = new LinkedList<>();
users.stream().map(user -> USER_YAML_SWAPPER.swapToYamlConfiguration(user)).forEach(user -> result.add(user.toString()));
users.stream().map(USER_YAML_SWAPPER::swapToYamlConfiguration).forEach(user -> result.add(user.toString()));
return result;
}

Expand All @@ -55,7 +55,7 @@ public static Collection<String> convertYamlUserConfigurations(final Collection<
*/
public static Collection<ShardingSphereUser> convertShardingSphereUser(final Collection<String> users) {
Collection<YamlUserConfiguration> yamlUsers = convertYamlUserConfiguration(users);
return yamlUsers.stream().map(yamlUser -> USER_YAML_SWAPPER.swapToObject(yamlUser)).collect(Collectors.toList());
return yamlUsers.stream().map(USER_YAML_SWAPPER::swapToObject).collect(Collectors.toList());
}

/**
Expand All @@ -65,7 +65,7 @@ public static Collection<ShardingSphereUser> convertShardingSphereUser(final Col
* @return yaml user configurations
*/
public static Collection<YamlUserConfiguration> convertYamlUserConfiguration(final Collection<String> users) {
return users.stream().map(user -> convertYamlUserConfiguration(user)).collect(Collectors.toList());
return users.stream().map(YamlUsersConfigurationConverter::convertYamlUserConfiguration).collect(Collectors.toList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfigurationConverter;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
Expand Down Expand Up @@ -68,7 +68,7 @@ private void initConfigurations(final YamlProxyConfiguration yamlConfig) {
governanceFacade.onlineInstance();
} else {
governanceFacade.onlineInstance(getDataSourceConfigurationMap(ruleConfigs),
getRuleConfigurations(ruleConfigs), YamlUserConfigurationConverter.convertShardingSphereUser(serverConfig.getUsers()), serverConfig.getProps());
getRuleConfigurations(ruleConfigs), YamlUsersConfigurationConverter.convertShardingSphereUser(serverConfig.getUsers()), serverConfig.getProps());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.shardingsphere.infra.config.RuleConfiguration;
import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfigurationConverter;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.proxy.config.ProxyConfiguration;
import org.apache.shardingsphere.proxy.config.YamlProxyConfiguration;
Expand Down Expand Up @@ -49,7 +49,7 @@ public ProxyConfiguration swap(final YamlProxyConfiguration yamlConfig) {
Map<String, Map<String, DataSourceParameter>> schemaDataSources = getDataSourceParametersMap(yamlConfig.getRuleConfigurations());
Map<String, Collection<RuleConfiguration>> schemaRules = getRuleConfigurations(yamlConfig.getRuleConfigurations());
Collection<RuleConfiguration> globalRules = new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(yamlConfig.getServerConfiguration().getRules());
Collection<ShardingSphereUser> users = YamlUserConfigurationConverter.convertShardingSphereUser(yamlConfig.getServerConfiguration().getUsers());
Collection<ShardingSphereUser> users = YamlUsersConfigurationConverter.convertShardingSphereUser(yamlConfig.getServerConfiguration().getUsers());
Properties props = yamlConfig.getServerConfiguration().getProps();
return new ProxyConfiguration(schemaDataSources, schemaRules, globalRules, users, props);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfiguration;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUserConfigurationConverter;
import org.apache.shardingsphere.infra.metadata.user.yaml.config.YamlUsersConfigurationConverter;
import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
import org.apache.shardingsphere.proxy.config.yaml.YamlProxyServerConfiguration;
import org.apache.shardingsphere.test.integration.junit.container.ShardingSphereContainer;
Expand Down Expand Up @@ -53,7 +53,7 @@ private YamlUserConfiguration loadAuthentication(final ParameterizedArray parame
ByteStreams.toByteArray(this.getClass().getResourceAsStream("/docker/" + parameterizedArray.getScenario() + "/proxy/conf/server.yaml")),
YamlProxyServerConfiguration.class
);
return YamlUserConfigurationConverter.convertYamlUserConfiguration(configuration.getUsers())
return YamlUsersConfigurationConverter.convertYamlUserConfiguration(configuration.getUsers())
.stream()
.filter(each -> "root".equals(each.getUsername()))
.findFirst()
Expand Down

0 comments on commit 1dfce0f

Please sign in to comment.