-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature configuration changed event and subscribe at encrypt modu…
…le (#26273)
- Loading branch information
Showing
12 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...in/java/org/apache/shardingsphere/encrypt/event/EncryptRuleConfigurationEventBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ain/java/org/apache/shardingsphere/encrypt/event/config/AddEncryptConfigurationEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
38 changes: 38 additions & 0 deletions
38
...n/java/org/apache/shardingsphere/encrypt/event/config/AlterEncryptConfigurationEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
34 changes: 34 additions & 0 deletions
34
.../java/org/apache/shardingsphere/encrypt/event/config/DeleteEncryptConfigurationEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
38 changes: 38 additions & 0 deletions
38
...re/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/AddEncryptorEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
38 changes: 38 additions & 0 deletions
38
.../src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/AlterEncryptorEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
34 changes: 34 additions & 0 deletions
34
...src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/DeleteEncryptorEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.