Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature configuration changed event and subscribe at encrypt module #26273

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions features/encrypt/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>shardingsphere-infra-merge</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-mode-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shardingsphere</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.shardingsphere.encrypt.event;

import com.google.common.base.Strings;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.event.config.AddEncryptConfigurationEvent;
import org.apache.shardingsphere.encrypt.event.config.AlterEncryptConfigurationEvent;
import org.apache.shardingsphere.encrypt.event.config.DeleteEncryptConfigurationEvent;
import org.apache.shardingsphere.encrypt.event.encryptor.AddEncryptorEvent;
import org.apache.shardingsphere.encrypt.event.encryptor.AlterEncryptorEvent;
import org.apache.shardingsphere.encrypt.event.encryptor.DeleteEncryptorEvent;
import org.apache.shardingsphere.encrypt.metadata.converter.EncryptNodeConverter;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.swapper.YamlEncryptRuleConfigurationSwapper;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.event.config.RuleConfigurationEventBuilder;

import java.util.Optional;

/**
* Encrypt rule configuration event builder.
*/
public final class EncryptRuleConfigurationEventBuilder implements RuleConfigurationEventBuilder {

@Override
public Optional<GovernanceEvent> build(final String databaseName, final DataChangedEvent event) {
if (!EncryptNodeConverter.isEncryptPath(event.getKey()) || Strings.isNullOrEmpty(event.getValue())) {
return Optional.empty();
}
Optional<String> tableName = EncryptNodeConverter.getTableName(event.getKey());
if (tableName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createEncryptConfigEvent(databaseName, tableName.get(), event);
}
Optional<String> encryptorName = EncryptNodeConverter.getEncryptorName(event.getKey());
if (encryptorName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
return createEncryptorEvent(databaseName, encryptorName.get(), event);
}
return Optional.empty();
}

private Optional<GovernanceEvent> createEncryptConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddEncryptConfigurationEvent<>(databaseName, swapEncryptTableRuleConfig(event.getValue())));
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterEncryptConfigurationEvent<>(databaseName, groupName, swapEncryptTableRuleConfig(event.getValue())));
}
return Optional.of(new DeleteEncryptConfigurationEvent(databaseName, groupName));
}

private EncryptTableRuleConfiguration swapEncryptTableRuleConfig(final String yamlContext) {
return new YamlEncryptRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlEncryptRuleConfiguration.class)).getTables().iterator().next();
}

private Optional<GovernanceEvent> createEncryptorEvent(final String databaseName, final String encryptorName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
return Optional.of(new AddEncryptorEvent<>(databaseName, encryptorName, swapToAlgorithmConfig(event.getValue())));
}
if (Type.UPDATED == event.getType()) {
return Optional.of(new AlterEncryptorEvent<>(databaseName, encryptorName, swapToAlgorithmConfig(event.getValue())));
}
return Optional.of(new DeleteEncryptorEvent(databaseName, encryptorName));
}

private AlgorithmConfiguration swapToAlgorithmConfig(final String yamlContext) {
return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlAlgorithmConfiguration.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.shardingsphere.encrypt.event.config;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;

/**
* Add encrypt configuration event.
*
* @param <T> encrypt configuration
*/
@RequiredArgsConstructor
@Getter
public final class AddEncryptConfigurationEvent<T> implements GovernanceEvent {

private final String databaseName;

private final T config;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.shardingsphere.encrypt.event.config;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;

/**
* Alter encrypt configuration event.
*
* @param <T> encrypt configuration
*/
@RequiredArgsConstructor
@Getter
public final class AlterEncryptConfigurationEvent<T> implements GovernanceEvent {

private final String databaseName;

private final String tableName;

private final T config;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.shardingsphere.encrypt.event.config;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;

/**
* Delete encrypt configuration event.
*/
@RequiredArgsConstructor
@Getter
public final class DeleteEncryptConfigurationEvent implements GovernanceEvent {

private final String databaseName;

private final String tableName;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.shardingsphere.encrypt.event.encryptor;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;

/**
* Add encryptor event.
*
* @param <T> encryptor configuration
*/
@RequiredArgsConstructor
@Getter
public final class AddEncryptorEvent<T> implements GovernanceEvent {

private final String databaseName;

private final String encryptorName;

private final T config;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.shardingsphere.encrypt.event.encryptor;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;

/**
* Alter encryptor event.
*
* @param <T> encryptor configuration
*/
@RequiredArgsConstructor
@Getter
public final class AlterEncryptorEvent<T> implements GovernanceEvent {

private final String databaseName;

private final String encryptorName;

private final T config;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.shardingsphere.encrypt.event.encryptor;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;

/**
* Delete encryptor event.
*/
@RequiredArgsConstructor
@Getter
public final class DeleteEncryptorEvent implements GovernanceEvent {

private final String databaseName;

private final String encryptorName;
}
Loading