diff --git a/pom.xml b/pom.xml index 6c59803a875e5e60ca30b4297b7699f354cddbb3..49ecfbab973d4fa4f967f660b62650319bc7fa2a 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,9 @@ UTF-8 3.8.1 + 1.18.20 + 2.0.9 + 3.1.0 3.1.0 3.1.0 @@ -70,6 +73,27 @@ + + + org.projectlombok + lombok + ${lombok.version} + provided + true + + + + org.powermock + powermock-api-mockito2 + ${powermock.version} + + + org.powermock + powermock-module-junit4 + ${powermock.version} + + + diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfInteractive.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfInteractive.java index dfd07faeeb4f2456ea7d0db2b670b93d27474ced..2723562b3900b353b4b1356d7fc15c7dda37774a 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfInteractive.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/PluginOneselfInteractive.java @@ -20,6 +20,7 @@ import com.gitee.starblues.common.PackageStructure; import com.gitee.starblues.core.descriptor.DevPluginDescriptorLoader; import com.gitee.starblues.core.descriptor.InsidePluginDescriptor; import com.gitee.starblues.core.descriptor.PluginDescriptorLoader; +import com.gitee.starblues.core.descriptor.decrypt.EmptyPluginDescriptorDecrypt; import com.gitee.starblues.core.launcher.plugin.PluginInteractive; import com.gitee.starblues.integration.AutoIntegrationConfiguration; import com.gitee.starblues.integration.IntegrationConfiguration; @@ -80,7 +81,8 @@ public class PluginOneselfInteractive implements PluginInteractive { } private InsidePluginDescriptor createPluginDescriptor(){ - try (PluginDescriptorLoader pluginDescriptorLoader = new DevPluginDescriptorLoader()){ + EmptyPluginDescriptorDecrypt descriptorDecrypt = new EmptyPluginDescriptorDecrypt(); + try (PluginDescriptorLoader pluginDescriptorLoader = new DevPluginDescriptorLoader(descriptorDecrypt)){ Path classesPath = Paths.get(this.getClass().getResource("/").toURI()).getParent(); String metaInf = FilesUtils.joiningFilePath(classesPath.toString(), PackageStructure.META_INF_NAME); InsidePluginDescriptor pluginDescriptor = pluginDescriptorLoader.load(Paths.get(metaInf)); diff --git a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/DefaultMainEnvironmentProvider.java b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/DefaultMainEnvironmentProvider.java index 8487d3664dd6c607272685cccc47dd56471c2aaf..f71a363c300da964609be23baf3ff280501803e7 100644 --- a/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/DefaultMainEnvironmentProvider.java +++ b/spring-brick-bootstrap/src/main/java/com/gitee/starblues/bootstrap/realize/DefaultMainEnvironmentProvider.java @@ -18,7 +18,9 @@ package com.gitee.starblues.bootstrap.realize; import com.gitee.starblues.loader.utils.ObjectUtils; import com.gitee.starblues.spring.MainApplicationContext; +import com.gitee.starblues.utils.MapValueGetter; +import java.util.Collections; import java.util.Map; import java.util.function.Function; @@ -54,57 +56,32 @@ public class DefaultMainEnvironmentProvider implements MainEnvironmentProvider{ @Override public String getString(String name) { - return getValue(name, String::valueOf); + return getMapValueGetter(name).getString(name); } @Override public Integer getInteger(String name) { - return getValue(name, value -> { - if(value instanceof Integer){ - return (Integer) value; - } - return Integer.parseInt(String.valueOf(value)); - }); + return getMapValueGetter(name).getInteger(name); } @Override public Long getLong(String name) { - return getValue(name, value -> { - if(value instanceof Long){ - return (Long) value; - } - return Long.parseLong(String.valueOf(value)); - }); + return getMapValueGetter(name).getLong(name); } @Override public Double getDouble(String name) { - return getValue(name, value -> { - if(value instanceof Double){ - return (Double) value; - } - return Double.parseDouble(String.valueOf(value)); - }); + return getMapValueGetter(name).getDouble(name); } @Override public Float getFloat(String name) { - return getValue(name, value -> { - if(value instanceof Float){ - return (Float) value; - } - return Float.parseFloat(String.valueOf(value)); - }); + return getMapValueGetter(name).getFloat(name); } @Override public Boolean getBoolean(String name) { - return getValue(name, value -> { - if(value instanceof Boolean){ - return (Boolean) value; - } - return Boolean.parseBoolean(String.valueOf(value)); - }); + return getMapValueGetter(name).getBoolean(name); } @Override @@ -112,12 +89,19 @@ public class DefaultMainEnvironmentProvider implements MainEnvironmentProvider{ return mainApplicationContext.getConfigurableEnvironment(); } - private T getValue(String name, Function function){ - Object value = getValue(name); - if(value == null){ - return null; + private MapValueGetter getMapValueGetter(String name) { + Map> configurableEnvironment = mainApplicationContext.getConfigurableEnvironment(); + if(ObjectUtils.isEmpty(configurableEnvironment)){ + return new MapValueGetter(Collections.emptyMap()); + } + for (Map.Entry> entry : configurableEnvironment.entrySet()) { + Map value = entry.getValue(); + if(value.containsKey(name)){ + return new MapValueGetter(value); + } } - return function.apply(value); + return new MapValueGetter(Collections.emptyMap()); } + } diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/AbstractPluginCipher.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/AbstractPluginCipher.java new file mode 100644 index 0000000000000000000000000000000000000000..a2bd2063154f3baa761e45e4b375702643baf98d --- /dev/null +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/AbstractPluginCipher.java @@ -0,0 +1,57 @@ +package com.gitee.starblues.common.cipher; + +import com.gitee.starblues.utils.MapValueGetter; +import com.gitee.starblues.utils.ObjectUtils; + +import java.util.Map; + +/** + * 抽象的插件解密 + * + * @author starBlues + * @version 3.0.1 + */ +public abstract class AbstractPluginCipher implements PluginCipher{ + + protected MapValueGetter parameters; + + protected AbstractPluginCipher(){ + } + + public void initParams(Map params){ + parameters = new MapValueGetter(params); + } + + @Override + public String encrypt(String sourceStr) throws Exception { + if(ObjectUtils.isEmpty(sourceStr)){ + return ""; + } + return encryptImpl(sourceStr); + } + + /** + * 加密实现 + * @param sourceStr 原始字符串 + * @return 加密后的字节 + * @throws Exception 加密异常 + */ + protected abstract String encryptImpl(String sourceStr) throws Exception; + + + @Override + public String decrypt(String cryptoStr) throws Exception { + if(ObjectUtils.isEmpty(cryptoStr)){ + return ""; + } + return decryptImpl(cryptoStr); + } + + /** + * 解密实现 + * @param cryptoStr 解密字符串 + * @return 解密后的字符 + * @throws Exception 解密异常 + */ + protected abstract String decryptImpl(String cryptoStr) throws Exception; +} diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/PluginCipher.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/PluginCipher.java new file mode 100644 index 0000000000000000000000000000000000000000..257d09e58719142b2d2aab6d4591c078176fccc2 --- /dev/null +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/PluginCipher.java @@ -0,0 +1,44 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.common.cipher; + +/** + * 插件密码接口 + * + * @author starBlues + * @version 3.0.1 + */ +public interface PluginCipher { + + /** + * 加密 + * @param sourceStr 原始字符 + * @return 加密后的字节 + * @throws Exception 加密异常 + */ + String encrypt(String sourceStr) throws Exception; + + /** + * 解密 + * @param cryptoStr 加密的字符 + * @return 解密后的字符 + * @throws Exception 解密异常 + */ + String decrypt(String cryptoStr) throws Exception; + + +} diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/RsaPluginCipher.java b/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/RsaPluginCipher.java new file mode 100644 index 0000000000000000000000000000000000000000..b5d17daf717ce8b5b87d0914ed6d10d5874870b8 --- /dev/null +++ b/spring-brick-common/src/main/java/com/gitee/starblues/common/cipher/RsaPluginCipher.java @@ -0,0 +1,111 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.common.cipher; + +import com.gitee.starblues.utils.Assert; +import lombok.AllArgsConstructor; +import lombok.Getter; + +import javax.crypto.Cipher; +import java.nio.charset.StandardCharsets; +import java.security.*; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; +import java.util.Map; + +/** + * 非对称插件加解密 + * + * @author starBlues + * @version 3.0.1 + */ +public class RsaPluginCipher extends AbstractPluginCipher{ + + public final static String PUBLIC_KEY = "publicKey"; + public final static String PRIVATE_KEY = "privateKey"; + + public RsaPluginCipher(){ + } + + @Override + protected String encryptImpl(String sourceStr) throws Exception { + String publicKey = super.parameters.getString(PUBLIC_KEY); + Assert.isNotEmpty(publicKey, PUBLIC_KEY + " 不能为空"); + byte[] decoded = Base64.getDecoder().decode(publicKey); + RSAPublicKey pubKey = (RSAPublicKey) KeyFactory + .getInstance("RSA") + .generatePublic(new X509EncodedKeySpec(decoded)); + //RSA加密 + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, pubKey); + return Base64.getEncoder().encodeToString(cipher.doFinal(sourceStr.getBytes(StandardCharsets.UTF_8))); + } + + @Override + protected String decryptImpl(String cryptoStr) throws Exception { + String privateKey = super.parameters.getString(PRIVATE_KEY); + Assert.isNotEmpty(privateKey, PRIVATE_KEY + " 不能为空"); + //64位解码加密后的字符串 + byte[] inputByte = Base64.getDecoder().decode(cryptoStr.getBytes(StandardCharsets.UTF_8)); + //base64编码的私钥 + byte[] decoded = Base64.getDecoder().decode(privateKey); + RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); + //RSA解密 + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.DECRYPT_MODE, priKey); + return new String(cipher.doFinal(inputByte)); + } + + /** + * 生成 512大小 密钥对 + * @return 密钥对对象 + * @throws NoSuchAlgorithmException 生成异常 + */ + public static RsaKey generateKey() throws NoSuchAlgorithmException { + return generateKey(512); + } + + /** + * 生成密钥对 + * @param keySize 密钥对大小 + * @return 密钥对对象 + * @throws NoSuchAlgorithmException 生成异常 + */ + public static RsaKey generateKey(Integer keySize) throws NoSuchAlgorithmException { + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); + keyPairGenerator.initialize(keySize); + KeyPair keyPair = keyPairGenerator.generateKeyPair(); + + RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); + RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); + return new RsaKey( + Base64.getEncoder().encodeToString(rsaPublicKey.getEncoded()), + Base64.getEncoder().encodeToString(rsaPrivateKey.getEncoded()) + ); + } + + @AllArgsConstructor + @Getter + public static class RsaKey{ + private final String publicKey; + private final String privateKey; + } + +} diff --git a/spring-brick-common/src/main/java/com/gitee/starblues/utils/MapValueGetter.java b/spring-brick-common/src/main/java/com/gitee/starblues/utils/MapValueGetter.java new file mode 100644 index 0000000000000000000000000000000000000000..191198e03a3c660833f162307f0c72950b610e26 --- /dev/null +++ b/spring-brick-common/src/main/java/com/gitee/starblues/utils/MapValueGetter.java @@ -0,0 +1,130 @@ +package com.gitee.starblues.utils; + +import java.util.Collections; +import java.util.Map; +import java.util.function.Function; + +/** + * map 值获取者工具类 + * + * @author starBlues + * @version 3.0.1 + */ +public class MapValueGetter { + + private final Map map; + + public MapValueGetter(Map map) { + if(map == null){ + this.map = Collections.emptyMap(); + } else { + this.map = map; + } + } + + /** + * 获取object + * @param key map key + * @return value + */ + public Object getObject(String key) { + return map.get(key); + } + + + /** + * 获取 String 类型值 + * @param key map key + * @return String value + */ + public String getString(String key) { + return getValue(key, String::valueOf); + } + + /** + * 获取 Integer 类型值 + * @param key map key + * @return Integer value + */ + public Integer getInteger(String key) { + return getValue(key, value -> { + if(value instanceof Integer){ + return (Integer) value; + } + return Integer.parseInt(String.valueOf(value)); + }); + } + + /** + * 获取 Long 类型值 + * @param key map key + * @return Long value + */ + public Long getLong(String key) { + return getValue(key, value -> { + if(value instanceof Long){ + return (Long) value; + } + return Long.parseLong(String.valueOf(value)); + }); + } + + /** + * 获取 Double 类型值 + * @param key map key + * @return Double value + */ + public Double getDouble(String key) { + return getValue(key, value -> { + if(value instanceof Double){ + return (Double) value; + } + return Double.parseDouble(String.valueOf(value)); + }); + } + + /** + * 获取 Float 类型值 + * @param key map key + * @return Float value + */ + public Float getFloat(String key) { + return getValue(key, value -> { + if(value instanceof Float){ + return (Float) value; + } + return Float.parseFloat(String.valueOf(value)); + }); + } + + /** + * 获取 Boolean 类型值 + * @param key map key + * @return Boolean value + */ + public Boolean getBoolean(String key) { + return getValue(key, value -> { + if(value instanceof Boolean){ + return (Boolean) value; + } + return Boolean.parseBoolean(String.valueOf(value)); + }); + } + + /** + * 获取值并根据自定义实现转换类型 + * @param key map key + * @param function 自定义类型转换 + * @param 转换后的类型泛型 + * @return 转换后的类型值 + */ + private T getValue(String key, Function function){ + Object value = getObject(key); + if(value == null){ + return null; + } + return function.apply(value); + } + + +} diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/RepackageMojo.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/RepackageMojo.java index 5166f795aba89e0c76abddd61a208bba275a9d68..c786d713c2f80e054cf621e50ca0fffdd7ebc7be 100644 --- a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/RepackageMojo.java +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/RepackageMojo.java @@ -19,6 +19,9 @@ package com.gitee.starblues.plugin.pack; import com.gitee.starblues.common.Constants; import com.gitee.starblues.plugin.pack.dev.DevConfig; import com.gitee.starblues.plugin.pack.dev.DevRepackager; +import com.gitee.starblues.plugin.pack.encrypt.EncryptConfig; +import com.gitee.starblues.plugin.pack.encrypt.RsaConfig; +import com.gitee.starblues.plugin.pack.encrypt.RsaEncryptPlugin; import com.gitee.starblues.plugin.pack.main.MainConfig; import com.gitee.starblues.plugin.pack.main.MainRepackager; import com.gitee.starblues.plugin.pack.prod.ProdConfig; @@ -61,12 +64,20 @@ public class RepackageMojo extends AbstractPackagerMojo { @Parameter(property = "spring-brick-packager.mainLoad") private LoadToMain loadToMain; + @Parameter(property = "spring-brick-packager.encryptConfig") + private EncryptConfig encryptConfig; + private final Set loadToMainSet = new HashSet<>(); @Override protected void pack() throws MojoExecutionException, MojoFailureException { initLoadToMainSet(); String mode = getMode(); + try { + encrypt(); + } catch (Exception e) { + throw new MojoExecutionException("encrypt failed: " + e.getMessage()); + } if(Constant.MODE_PROD.equalsIgnoreCase(mode)){ new ProdRepackager(this).repackage(); } else if(Constant.MODE_DEV.equalsIgnoreCase(mode)){ @@ -102,4 +113,24 @@ public class RepackageMojo extends AbstractPackagerMojo { } } + /** + * 加密 + * @throws Exception 加密异常 + */ + private void encrypt() throws Exception { + if(encryptConfig == null){ + return; + } + RsaConfig rsa = encryptConfig.getRsa(); + if(rsa != null){ + String publicKey = rsa.getPublicKey(); + if(ObjectUtils.isEmpty(publicKey)){ + throw new MojoExecutionException("encryptConfig.rsa.publicKey can't be empty"); + } + RsaEncryptPlugin rsaEncryptPlugin = new RsaEncryptPlugin(publicKey); + PluginInfo encryptPluginInfo = rsaEncryptPlugin.encrypt(getPluginInfo()); + setPluginInfo(encryptPluginInfo); + } + } + } diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/EncryptConfig.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/EncryptConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..acc0404a45c1d4433f3f84a16f1337e1aa341730 --- /dev/null +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/EncryptConfig.java @@ -0,0 +1,32 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.plugin.pack.encrypt; + +import lombok.Data; + +/** + * 加密配置 + * + * @author starBlues + * @version 3.0.1 + */ +@Data +public class EncryptConfig { + + private RsaConfig rsa; + +} diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/EncryptPlugin.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/EncryptPlugin.java new file mode 100644 index 0000000000000000000000000000000000000000..9a64a861474e1a953cc89d5ec80b94cef8de6454 --- /dev/null +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/EncryptPlugin.java @@ -0,0 +1,37 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.plugin.pack.encrypt; + +import com.gitee.starblues.plugin.pack.PluginInfo; + +/** + * 加密插件 + * + * @author starBlues + * @version 3.0.1 + */ +public interface EncryptPlugin { + + /** + * 加密插件信息 + * @param pluginInfo 插件信息 + * @return 加密后的插件信息 + * @throws Exception 加密异常 + */ + PluginInfo encrypt(PluginInfo pluginInfo) throws Exception; + +} diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/RsaConfig.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/RsaConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..18b7e9a1f943cc301108f2f8d4b91c812cfa785b --- /dev/null +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/RsaConfig.java @@ -0,0 +1,32 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.plugin.pack.encrypt; + +import lombok.Data; + +/** + * rsa 加密配置 + * + * @author starBlues + * @version 3.0.1 + */ +@Data +public class RsaConfig { + + private String publicKey; + +} diff --git a/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/RsaEncryptPlugin.java b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/RsaEncryptPlugin.java new file mode 100644 index 0000000000000000000000000000000000000000..3d1554c7351c18b7ddf7218a77e23150b443a6ec --- /dev/null +++ b/spring-brick-maven-packager/src/main/java/com/gitee/starblues/plugin/pack/encrypt/RsaEncryptPlugin.java @@ -0,0 +1,50 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.plugin.pack.encrypt; + +import com.gitee.starblues.common.cipher.AbstractPluginCipher; +import com.gitee.starblues.common.cipher.RsaPluginCipher; +import com.gitee.starblues.plugin.pack.PluginInfo; + +import java.util.HashMap; +import java.util.Map; + +/** + * rsa 算法插件加密 + * + * @author starBlues + * @version 3.0.1 + */ +public class RsaEncryptPlugin implements EncryptPlugin{ + + private final AbstractPluginCipher pluginCipher; + + public RsaEncryptPlugin(String publicKey) { + this.pluginCipher = new RsaPluginCipher(); + Map params = new HashMap<>(); + params.put(RsaPluginCipher.PUBLIC_KEY, publicKey); + this.pluginCipher.initParams(params); + } + + @Override + public PluginInfo encrypt(PluginInfo pluginInfo) throws Exception { + String bootstrapClass = pluginInfo.getBootstrapClass(); + String encrypt = pluginCipher.encrypt(bootstrapClass); + pluginInfo.setBootstrapClass(encrypt); + return pluginInfo; + } +} diff --git a/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml b/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml index 923cc051f41e63a1de62cc756f06bae0b2b05a8f..a0cff0ffe4330182164e4eca3c160e0e74be813e 100644 --- a/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml +++ b/spring-brick-maven-packager/src/main/resources/META-INF/maven/com.gitee.starblues.springboot-plugin-maven-packager/plugin-help.xml @@ -133,6 +133,14 @@ true 加载到主程序的依赖 + + encryptConfig + com.gitee.starblues.plugin.pack.encrypt.EncryptConfig + 3.0.1 + false + true + 加密配置 + diff --git a/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml b/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml index 923cc051f41e63a1de62cc756f06bae0b2b05a8f..a0cff0ffe4330182164e4eca3c160e0e74be813e 100644 --- a/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml +++ b/spring-brick-maven-packager/src/main/resources/META-INF/maven/plugin.xml @@ -133,6 +133,14 @@ true 加载到主程序的依赖 + + encryptConfig + com.gitee.starblues.plugin.pack.encrypt.EncryptConfig + 3.0.1 + false + true + 加密配置 + diff --git a/spring-brick/pom.xml b/spring-brick/pom.xml index 55b844bd9337ef63eb4eb594505e12f39720ac5f..092ba2d2fa268f3b1dd2553b994e6ea75d053681 100644 --- a/spring-brick/pom.xml +++ b/spring-brick/pom.xml @@ -27,7 +27,6 @@ 1.5.2 4.0.1 0.9.0 - 1.18.20 4.11 @@ -99,13 +98,6 @@ provided true - - org.projectlombok - lombok - ${lombok.version} - provided - true - junit junit diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/DefaultRealizeProvider.java b/spring-brick/src/main/java/com/gitee/starblues/core/DefaultRealizeProvider.java index 758d531012a2f564b8822e4cede173f99ca02c2a..cf619fa3b1220afe67a903af3e7d9bde32082fef 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/DefaultRealizeProvider.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/DefaultRealizeProvider.java @@ -63,7 +63,7 @@ public class DefaultRealizeProvider implements RealizeProvider { } setPluginScanner(basePluginScanner); setPluginBasicChecker(new ComposePluginBasicChecker(applicationContext)); - setPluginDescriptorLoader(new ComposeDescriptorLoader(pluginBasicChecker)); + setPluginDescriptorLoader(new ComposeDescriptorLoader(applicationContext, pluginBasicChecker)); setVersionInspector(new SemverVersionInspector()); } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java index 14b11d814e552c2e2672e35bf0be60a04bc5fd39..642721d0d1dd33b24b076a761003249f4e6e1a4c 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/AbstractPluginDescriptorLoader.java @@ -18,14 +18,22 @@ package com.gitee.starblues.core.descriptor; import com.gitee.starblues.common.*; +import com.gitee.starblues.core.descriptor.decrypt.DecryptProperties; +import com.gitee.starblues.core.descriptor.decrypt.DefaultPluginDescriptorDecrypt; +import com.gitee.starblues.core.descriptor.decrypt.EmptyPluginDescriptorDecrypt; +import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; +import com.gitee.starblues.core.exception.PluginDecryptException; import com.gitee.starblues.core.exception.PluginException; +import com.gitee.starblues.integration.IntegrationConfiguration; import com.gitee.starblues.utils.FilesUtils; -import com.gitee.starblues.utils.PropertiesUtils; import com.gitee.starblues.utils.ObjectUtils; +import com.gitee.starblues.utils.SpringBeanUtils; import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; import java.io.File; import java.io.IOException; @@ -35,34 +43,42 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; -import java.util.jar.Attributes; -import java.util.jar.Manifest; -import static com.gitee.starblues.common.PackageStructure.MANIFEST; import static com.gitee.starblues.common.PluginDescriptorKey.*; import static com.gitee.starblues.utils.PropertiesUtils.getValue; /** * 抽象的 PluginDescriptorLoader * @author starBlues - * @version 3.0.0 + * @version 3.0.1 */ +@Slf4j public abstract class AbstractPluginDescriptorLoader implements PluginDescriptorLoader{ private final Logger logger = LoggerFactory.getLogger(this.getClass()); + protected final PluginDescriptorDecrypt pluginDescriptorDecrypt; + + protected AbstractPluginDescriptorLoader(PluginDescriptorDecrypt pluginDescriptorDecrypt) { + this.pluginDescriptorDecrypt = pluginDescriptorDecrypt; + } @Override public InsidePluginDescriptor load(Path location) throws PluginException { PluginMeta pluginMeta = null; try { pluginMeta = getPluginMetaInfo(location); - if(pluginMeta == null || pluginMeta.getPluginMetaInfo() == null){ + if(pluginMeta == null || pluginMeta.getProperties() == null){ logger.debug("路径[{}]没有发现插件配置信息", location); return null; } return create(pluginMeta, location); } catch (Exception e) { + if(e instanceof PluginDecryptException){ + logger.error(e.getMessage(), e); + } else { + logger.debug(e.getMessage(), e); + } return null; } } @@ -81,7 +97,7 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor protected abstract PluginMeta getPluginMetaInfo(Path location) throws Exception; protected DefaultInsidePluginDescriptor create(PluginMeta pluginMeta, Path path) throws Exception{ - Properties properties = pluginMeta.getPluginMetaInfo(); + Properties properties = pluginMeta.getProperties(); DefaultInsidePluginDescriptor descriptor = new DefaultInsidePluginDescriptor( getValue(properties, PLUGIN_ID), getValue(properties, PLUGIN_VERSION), @@ -161,29 +177,20 @@ public abstract class AbstractPluginDescriptorLoader implements PluginDescriptor return pluginLibInfos; } - protected Manifest getManifest(InputStream inputStream) throws Exception{ - Manifest manifest = new Manifest(); - try { - manifest.read(inputStream); - return manifest; - } finally { - inputStream.close(); - } - } - - protected Properties getProperties(InputStream inputStream) throws Exception{ + protected Properties getDecryptProperties(InputStream inputStream) throws Exception{ Properties properties = new Properties(); try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);){ properties.load(reader); - return properties; } + String pluginId = getValue(properties, PLUGIN_ID); + return pluginDescriptorDecrypt.decrypt(pluginId, properties); } @AllArgsConstructor @Getter public static class PluginMeta{ private final String packageType; - private final Properties pluginMetaInfo; + private final Properties properties; } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ComposeDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ComposeDescriptorLoader.java index 475815e0df8d3615ab9ffcca814e6bd0c49ee9a6..fa219810f6f55ade1868340bbca12a011aa93755 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ComposeDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ComposeDescriptorLoader.java @@ -17,7 +17,11 @@ package com.gitee.starblues.core.descriptor; import com.gitee.starblues.core.checker.PluginBasicChecker; +import com.gitee.starblues.core.descriptor.decrypt.EmptyPluginDescriptorDecrypt; +import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; import com.gitee.starblues.core.exception.PluginException; +import com.gitee.starblues.utils.SpringBeanUtils; +import org.springframework.context.ApplicationContext; import java.nio.file.Path; import java.util.ArrayList; @@ -31,19 +35,32 @@ import java.util.List; public class ComposeDescriptorLoader implements PluginDescriptorLoader{ private final List pluginDescriptorLoaders = new ArrayList<>(); - + + private final ApplicationContext applicationContext; private final PluginBasicChecker pluginChecker; - public ComposeDescriptorLoader(PluginBasicChecker pluginChecker) { + + public ComposeDescriptorLoader(ApplicationContext applicationContext, PluginBasicChecker pluginChecker) { + this.applicationContext = applicationContext; this.pluginChecker = pluginChecker; addDefaultLoader(); } protected void addDefaultLoader(){ - addLoader(new DevPluginDescriptorLoader()); - addLoader(new ProdPluginDescriptorLoader()); + PluginDescriptorDecrypt pluginDescriptorDecrypt = getPluginDescriptorDecrypt(applicationContext); + addLoader(new DevPluginDescriptorLoader(pluginDescriptorDecrypt)); + addLoader(new ProdPluginDescriptorLoader(pluginDescriptorDecrypt)); } + protected PluginDescriptorDecrypt getPluginDescriptorDecrypt(ApplicationContext applicationContext){ + PluginDescriptorDecrypt pluginDescriptorDecrypt = + SpringBeanUtils.getExistBean(applicationContext, PluginDescriptorDecrypt.class); + if(pluginDescriptorDecrypt != null){ + return pluginDescriptorDecrypt; + } else { + return new EmptyPluginDescriptorDecrypt(); + } + } public void addLoader(PluginDescriptorLoader descriptorLoader){ if(descriptorLoader != null){ diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DevPluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DevPluginDescriptorLoader.java index 3b94a9fbab69ab0814e09b8fb4d3b668d61b9fef..7c0a1eb1ea271e24c5d2f72c03995bf92f118281 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DevPluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/DevPluginDescriptorLoader.java @@ -18,31 +18,40 @@ package com.gitee.starblues.core.descriptor; import com.gitee.starblues.common.PackageStructure; import com.gitee.starblues.common.PackageType; +import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.ApplicationContext; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Properties; -import java.util.jar.Manifest; /** * 开发环境 PluginDescriptorLoader 加载者 * @author starBlues * @version 3.0.0 */ +@Slf4j public class DevPluginDescriptorLoader extends AbstractPluginDescriptorLoader{ + public DevPluginDescriptorLoader(PluginDescriptorDecrypt pluginDescriptorDecrypt) { + super(pluginDescriptorDecrypt); + } + @Override protected PluginMeta getPluginMetaInfo(Path location) throws Exception { String pluginMetaPath = location.toString() + File.separator + PackageStructure.PLUGIN_META_NAME; File file = new File(pluginMetaPath); if(!file.exists()){ + log.debug("Path: [{}] not exist.", location); return null; } Path path = Paths.get(pluginMetaPath); - Properties properties = super.getProperties(Files.newInputStream(path)); - if(properties.isEmpty()){ + Properties properties = super.getDecryptProperties(Files.newInputStream(path)); + if(properties == null || properties.isEmpty()){ + log.debug("Load plugin properties is empty from '{}'", path); return null; } return new PluginMeta(PackageType.PLUGIN_PACKAGE_TYPE_DEV, properties); diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdDirPluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdDirPluginDescriptorLoader.java index f4547abf777f270089e318813fdbb58229231cfd..8edea7c3893360d8e02ed3f37aaf0d334aae9f97 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdDirPluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdDirPluginDescriptorLoader.java @@ -17,12 +17,13 @@ package com.gitee.starblues.core.descriptor; import com.gitee.starblues.common.ManifestKey; -import com.gitee.starblues.common.PackageType; import com.gitee.starblues.common.PluginDescriptorKey; +import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; import com.gitee.starblues.utils.PropertiesUtils; import com.gitee.starblues.utils.FilesUtils; import com.gitee.starblues.utils.ObjectUtils; import org.apache.commons.io.FileUtils; +import org.springframework.context.ApplicationContext; import java.io.File; import java.io.FileInputStream; @@ -43,10 +44,14 @@ import static com.gitee.starblues.common.PackageStructure.*; * 生产环境目录式插件 PluginDescriptorLoader 加载者 * 解析生产的dir * @author starBlues - * @version 3.0.0 + * @version 3.0.1 */ public class ProdDirPluginDescriptorLoader extends AbstractPluginDescriptorLoader{ + public ProdDirPluginDescriptorLoader(PluginDescriptorDecrypt pluginDescriptorDecrypt) { + super(pluginDescriptorDecrypt); + } + @Override protected PluginMeta getPluginMetaInfo(Path location) throws Exception { File file = new File(FilesUtils.joiningFilePath(location.toString(), resolvePath(PROD_MANIFEST_PATH))); @@ -70,7 +75,7 @@ public class ProdDirPluginDescriptorLoader extends AbstractPluginDescriptorLoade if(!pluginMetaFile.exists()){ return null; } - Properties properties = super.getProperties(new FileInputStream(pluginMetaFile)); + Properties properties = super.getDecryptProperties(new FileInputStream(pluginMetaFile)); if(properties.isEmpty()){ return null; } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPackagePluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPackagePluginDescriptorLoader.java index 15896fcc53ecf285036d9df826ff166a06cf5a19..224796c071ded2ba038c9b216c6fc2baffbb2a79 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPackagePluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPackagePluginDescriptorLoader.java @@ -19,9 +19,11 @@ package com.gitee.starblues.core.descriptor; import com.gitee.starblues.common.ManifestKey; import com.gitee.starblues.common.PackageStructure; +import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; import com.gitee.starblues.utils.PropertiesUtils; import com.gitee.starblues.utils.ObjectUtils; import org.apache.commons.io.IOUtils; +import org.springframework.context.ApplicationContext; import java.io.InputStream; import java.nio.file.Path; @@ -45,7 +47,8 @@ public class ProdPackagePluginDescriptorLoader extends AbstractPluginDescriptorL private PluginResourcesConfig pluginResourcesConfig; - public ProdPackagePluginDescriptorLoader() { + public ProdPackagePluginDescriptorLoader(PluginDescriptorDecrypt pluginDescriptorDecrypt) { + super(pluginDescriptorDecrypt); } @Override @@ -62,7 +65,7 @@ public class ProdPackagePluginDescriptorLoader extends AbstractPluginDescriptorL if(jarEntry == null){ return null; } - Properties properties = super.getProperties(jarFile.getInputStream(jarEntry)); + Properties properties = super.getDecryptProperties(jarFile.getInputStream(jarEntry)); if(properties.isEmpty()){ return null; } diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPluginDescriptorLoader.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPluginDescriptorLoader.java index 41a0583e5f18a223402c212d794450e24f2529d7..c9bfa3749fe3fe824fa303a243261610cd2e15d4 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPluginDescriptorLoader.java +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/ProdPluginDescriptorLoader.java @@ -16,10 +16,14 @@ package com.gitee.starblues.core.descriptor; +import com.gitee.starblues.core.descriptor.decrypt.EmptyPluginDescriptorDecrypt; +import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; import com.gitee.starblues.core.exception.PluginException; import com.gitee.starblues.utils.ResourceUtils; +import com.gitee.starblues.utils.SpringBeanUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; import java.nio.file.Path; @@ -34,14 +38,20 @@ public class ProdPluginDescriptorLoader implements PluginDescriptorLoader{ private PluginDescriptorLoader target; + private final PluginDescriptorDecrypt pluginDescriptorDecrypt; + + public ProdPluginDescriptorLoader(PluginDescriptorDecrypt pluginDescriptorDecrypt) { + this.pluginDescriptorDecrypt = pluginDescriptorDecrypt; + } + @Override public InsidePluginDescriptor load(Path location) throws PluginException { if(ResourceUtils.isJarFile(location)){ - target = new ProdPackagePluginDescriptorLoader(); + target = new ProdPackagePluginDescriptorLoader(pluginDescriptorDecrypt); } else if(ResourceUtils.isZipFile(location)){ - target = new ProdPackagePluginDescriptorLoader(); + target = new ProdPackagePluginDescriptorLoader(pluginDescriptorDecrypt); } else if(ResourceUtils.isDirFile(location)){ - target = new ProdDirPluginDescriptorLoader(); + target = new ProdDirPluginDescriptorLoader(pluginDescriptorDecrypt); } else { logger.warn("不能解析文件: {}", location); return null; diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/DecryptProperties.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/DecryptProperties.java new file mode 100644 index 0000000000000000000000000000000000000000..fb2ea4b9393c680c2a00eb225120adead85b002f --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/DecryptProperties.java @@ -0,0 +1,34 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.core.descriptor.decrypt; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.Reader; +import java.util.Properties; + +/** + * 需解密的 Properties + * + * @author starBlues + * @version 3.0.1 + */ +public class DecryptProperties extends Properties { + + +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/DefaultPluginDescriptorDecrypt.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/DefaultPluginDescriptorDecrypt.java new file mode 100644 index 0000000000000000000000000000000000000000..e273f583e3b4698a44059f33998913552de7c359 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/DefaultPluginDescriptorDecrypt.java @@ -0,0 +1,133 @@ +package com.gitee.starblues.core.descriptor.decrypt; + +import com.gitee.starblues.common.PluginDescriptorKey; +import com.gitee.starblues.common.cipher.AbstractPluginCipher; +import com.gitee.starblues.common.cipher.PluginCipher; +import com.gitee.starblues.core.exception.PluginDecryptException; +import com.gitee.starblues.core.exception.PluginException; +import com.gitee.starblues.integration.IntegrationConfiguration; +import com.gitee.starblues.integration.decrypt.DecryptConfiguration; +import com.gitee.starblues.integration.decrypt.DecryptPluginConfiguration; +import com.gitee.starblues.utils.ObjectUtils; +import com.gitee.starblues.utils.PropertiesUtils; +import org.springframework.context.ApplicationContext; +import org.springframework.util.ClassUtils; + +import java.util.*; + +/** + * 默认的 PluginDescriptorDecrypt + * + * @author starBlues + * @version 3.0.1 + */ +public class DefaultPluginDescriptorDecrypt implements PluginDescriptorDecrypt{ + + private final ApplicationContext applicationContext; + + private final DecryptConfiguration decryptConfig; + private final Map pluginDecryptConfig; + + public DefaultPluginDescriptorDecrypt(ApplicationContext applicationContext, + IntegrationConfiguration configuration) { + this.applicationContext = applicationContext; + + this.decryptConfig = configuration.decrypt(); + List plugins = decryptConfig.getPlugins(); + if(ObjectUtils.isEmpty(plugins)){ + this.pluginDecryptConfig = Collections.emptyMap(); + } else { + this.pluginDecryptConfig = new HashMap<>(plugins.size()); + for (DecryptPluginConfiguration plugin : plugins) { + pluginDecryptConfig.put(plugin.getPluginId(), plugin); + } + } + } + + @Override + public Properties decrypt(String pluginId, Properties properties) { + PluginCipher pluginCipher = getPluginCipher(pluginId); + if(pluginCipher == null){ + return properties; + } + try { + String bootstrapClass = PropertiesUtils.getValue(properties, PluginDescriptorKey.PLUGIN_BOOTSTRAP_CLASS); + String decrypt = pluginCipher.decrypt(bootstrapClass); + properties.setProperty(PluginDescriptorKey.PLUGIN_BOOTSTRAP_CLASS, decrypt); + return properties; + } catch (Exception e) { + throw new PluginDecryptException("插件[" + pluginId + "]解密失败. " + e.getMessage()); + } + } + + protected PluginCipher getPluginCipher(String pluginId){ + if(decryptConfig == null){ + return null; + } + Boolean enable = decryptConfig.getEnable(); + if(enable == null || !enable){ + // 没有启用 + return null; + } + Map props = decryptConfig.getProps(); + if(props == null){ + props = new HashMap<>(); + decryptConfig.setProps(props); + } + String className = decryptConfig.getClassName(); + if(ObjectUtils.isEmpty(pluginDecryptConfig)){ + // 没有配置具体插件的解密配置 + return getPluginCipherBean(className, props); + } + DecryptPluginConfiguration decryptPluginConfiguration = pluginDecryptConfig.get(pluginId); + if(decryptPluginConfiguration == null){ + // 当前插件没有配置解密配置, 说明不启用解密 + return null; + } + Map pluginParam = decryptPluginConfiguration.getProps(); + if(!ObjectUtils.isEmpty(pluginParam)){ + props.putAll(pluginParam); + } + return getPluginCipherBean(className, props); + } + + + protected PluginCipher getPluginCipherBean(String className, Map params){ + ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader(); + try { + if(defaultClassLoader == null){ + defaultClassLoader = this.getClass().getClassLoader(); + } + Class aClass = defaultClassLoader.loadClass(className); + + String error = "解密实现者[" + className + "]没有继承 [" + AbstractPluginCipher.class.getName() + "]"; + + if(aClass.isAssignableFrom(AbstractPluginCipher.class)){ + throw new PluginDecryptException(error); + } + Object bean = getBean(aClass); + if(bean instanceof AbstractPluginCipher){ + AbstractPluginCipher pluginCipher = (AbstractPluginCipher) bean; + pluginCipher.initParams(params); + return pluginCipher; + } else { + throw new PluginDecryptException(error); + } + } catch (ClassNotFoundException e) { + throw new PluginDecryptException("没有发现解密实现者: " + className); + } + } + + protected Object getBean(Class aClass){ + try { + return applicationContext.getBean(aClass); + } catch (Exception e1){ + try { + return aClass.getConstructor().newInstance(); + } catch (Exception e2){ + throw new PluginDecryptException(e2); + } + } + } + +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/EmptyPluginDescriptorDecrypt.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/EmptyPluginDescriptorDecrypt.java new file mode 100644 index 0000000000000000000000000000000000000000..bdf89c13dcdd48bb23f127c79fa8dc9f8c045dac --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/EmptyPluginDescriptorDecrypt.java @@ -0,0 +1,32 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.core.descriptor.decrypt; + +import java.util.Properties; + +/** + * 空的插件解密 + * + * @author starBlues + * @version 3.0.1 + */ +public class EmptyPluginDescriptorDecrypt implements PluginDescriptorDecrypt{ + @Override + public Properties decrypt(String pluginId, Properties properties) { + return properties; + } +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/PluginDescriptorDecrypt.java b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/PluginDescriptorDecrypt.java new file mode 100644 index 0000000000000000000000000000000000000000..df444de8e3ce659e79de46ab58b47c51910040b6 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/descriptor/decrypt/PluginDescriptorDecrypt.java @@ -0,0 +1,41 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.core.descriptor.decrypt; + +import com.gitee.starblues.core.exception.PluginDecryptException; + +import java.util.Properties; + +/** + * 插件描述文件解密器 + * + * @author starBlues + * @version 3.0.1 + */ +public interface PluginDescriptorDecrypt { + + /** + * 解密 properties + * + * @param pluginId 插件id + * @param properties properties + * @return 解密后的 Properties + * @throws PluginDecryptException 插件解密异常 + */ + Properties decrypt(String pluginId, Properties properties) throws PluginDecryptException; + +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/core/exception/PluginDecryptException.java b/spring-brick/src/main/java/com/gitee/starblues/core/exception/PluginDecryptException.java new file mode 100644 index 0000000000000000000000000000000000000000..8b5def0785b7012470b11f43c574b3f481fd8ada --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/core/exception/PluginDecryptException.java @@ -0,0 +1,60 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.core.exception; + +import com.gitee.starblues.core.descriptor.PluginDescriptor; + +/** + * 插件解密异常 + * + * @author starBlues + * @version 3.0.1 + */ +public class PluginDecryptException extends PluginException{ + + public PluginDecryptException() { + super(); + } + + public PluginDecryptException(String message) { + super(message); + } + + public PluginDecryptException(Throwable cause) { + super(cause); + } + + public PluginDecryptException(String message, Throwable cause) { + super(message, cause); + } + + public PluginDecryptException(PluginDescriptor pluginDescriptor, String opType, Throwable cause) { + super(pluginDescriptor, opType, cause); + } + + public PluginDecryptException(PluginDescriptor pluginDescriptor, String message) { + super(pluginDescriptor, message); + } + + public PluginDecryptException(String pluginId, String opType, Throwable cause) { + super(pluginId, opType, cause); + } + + public PluginDecryptException(String pluginId, String message) { + super(pluginId, message); + } +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/AutoIntegrationConfiguration.java b/spring-brick/src/main/java/com/gitee/starblues/integration/AutoIntegrationConfiguration.java index a3fff5c6ed2239103d9fe0d95b9cf42490803fdd..5134ad9517a376a4639b877a1a99a3c9d0819ddc 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/AutoIntegrationConfiguration.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/AutoIntegrationConfiguration.java @@ -17,6 +17,7 @@ package com.gitee.starblues.integration; import com.gitee.starblues.core.RuntimeMode; +import com.gitee.starblues.integration.decrypt.DecryptConfiguration; import com.gitee.starblues.utils.ResourceUtils; import lombok.Data; import lombok.EqualsAndHashCode; @@ -31,7 +32,7 @@ import java.util.Set; /** * 自动集成的配置 * @author starBlues - * @version 3.0.0 + * @version 3.0.1 */ @EqualsAndHashCode(callSuper = true) @Component @@ -126,6 +127,11 @@ public class AutoIntegrationConfiguration extends DefaultIntegrationConfiguratio @Value("${exactVersion:false}") private Boolean exactVersion; + /** + * 对插件启动时进行解密校验配置。默认为不启用 + */ + private DecryptConfiguration decrypt; + @Override public boolean enable() { if(enable == null){ @@ -208,4 +214,11 @@ public class AutoIntegrationConfiguration extends DefaultIntegrationConfiguratio return exactVersion; } + @Override + public DecryptConfiguration decrypt() { + if(decrypt == null){ + return super.decrypt(); + } + return decrypt; + } } diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java b/spring-brick/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java index c80f1bfc68eed38423cf9690557b9f8ef2c1c155..11b8e8a376aabe02472edc0717be01d3f6b4a507 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/DefaultIntegrationConfiguration.java @@ -16,6 +16,7 @@ package com.gitee.starblues.integration; +import com.gitee.starblues.integration.decrypt.DecryptConfiguration; import com.gitee.starblues.utils.Assert; import java.util.ArrayList; @@ -90,6 +91,13 @@ public abstract class DefaultIntegrationConfiguration implements IntegrationConf return false; } + @Override + public DecryptConfiguration decrypt() { + DecryptConfiguration decryptConfiguration = new DecryptConfiguration(); + decryptConfiguration.setEnable(false); + return decryptConfiguration; + } + /** * 检查配置 */ diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/ExtendPointConfiguration.java b/spring-brick/src/main/java/com/gitee/starblues/integration/ExtendPointConfiguration.java index 46bb11a84f46c70aa12efdbd01e8057f103b861f..225df462676830eada1b58919d8433077d7964e0 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/ExtendPointConfiguration.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/ExtendPointConfiguration.java @@ -18,6 +18,8 @@ package com.gitee.starblues.integration; import com.gitee.starblues.core.DefaultRealizeProvider; import com.gitee.starblues.core.RealizeProvider;; +import com.gitee.starblues.core.descriptor.decrypt.DefaultPluginDescriptorDecrypt; +import com.gitee.starblues.core.descriptor.decrypt.PluginDescriptorDecrypt; import com.gitee.starblues.core.launcher.plugin.BasicMainResourcePatternDefiner; import com.gitee.starblues.integration.operator.DefaultPluginOperator; import com.gitee.starblues.integration.operator.PluginOperator; @@ -81,4 +83,10 @@ public class ExtendPointConfiguration { return new BasicMainResourcePatternDefiner(configuration.mainPackage()); } + @Bean + @ConditionalOnMissingBean + public PluginDescriptorDecrypt pluginDescriptorDecrypt(){ + return new DefaultPluginDescriptorDecrypt(applicationContext, configuration); + } + } diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/IntegrationConfiguration.java b/spring-brick/src/main/java/com/gitee/starblues/integration/IntegrationConfiguration.java index ace90166a30961c2ccff00468b72c83d6c49163c..3ee6f6d8d0498ef745749f8e35ca3ef0fa509d58 100644 --- a/spring-brick/src/main/java/com/gitee/starblues/integration/IntegrationConfiguration.java +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/IntegrationConfiguration.java @@ -19,8 +19,8 @@ package com.gitee.starblues.integration; import com.gitee.starblues.common.Constants; import com.gitee.starblues.core.RuntimeMode; +import com.gitee.starblues.integration.decrypt.DecryptConfiguration; import com.gitee.starblues.utils.ObjectUtils; -import org.springframework.http.CacheControl; import java.util.List; import java.util.Set; @@ -118,6 +118,12 @@ public interface IntegrationConfiguration { */ boolean exactVersion(); + /** + * 解密配置 + * @return DecryptConfiguration + */ + DecryptConfiguration decrypt(); + /** * 检查配置 diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/decrypt/DecryptConfiguration.java b/spring-brick/src/main/java/com/gitee/starblues/integration/decrypt/DecryptConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..4edff5abd463f91391af6de41fd183158094bf06 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/decrypt/DecryptConfiguration.java @@ -0,0 +1,57 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.integration.decrypt; + +import com.gitee.starblues.common.cipher.RsaPluginCipher; +import lombok.Data; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 对插件启动时进行解密校验配置。默认为不启用 + * + * @author starBlues + * @version 3.0.1 + */ +@Data +public class DecryptConfiguration { + + /** + * 是否启动. 默认启用 + */ + private Boolean enable = true; + + /** + * 加解密实现类名称. + * 通过类加载器加载, 然后从Spring容器中获取, 获取不到, 对其直接实例化 + * 默认: {@link RsaPluginCipher} + */ + private String className = RsaPluginCipher.class.getName(); + + /** + * 总配置 + */ + private Map props = new HashMap<>(); + + /** + * 指定可生效的插件. 如果不配置, 则默认对全部插件生效 + */ + private List plugins; + +} diff --git a/spring-brick/src/main/java/com/gitee/starblues/integration/decrypt/DecryptPluginConfiguration.java b/spring-brick/src/main/java/com/gitee/starblues/integration/decrypt/DecryptPluginConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..5b769f89d1673f548a179c7fef4c0c394feca621 --- /dev/null +++ b/spring-brick/src/main/java/com/gitee/starblues/integration/decrypt/DecryptPluginConfiguration.java @@ -0,0 +1,43 @@ +/** + * Copyright [2019-2022] [starBlues] + * + * Licensed 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 com.gitee.starblues.integration.decrypt; + +import lombok.Data; + +import java.util.Map; + +/** + * 解密插件配置 + * + * @author starBlues + * @version 3.0.1 + */ +@Data +public class DecryptPluginConfiguration { + + /** + * 插件id + */ + private String pluginId; + + /** + * 对当前插件特定的配置, 可以覆盖 {@link DecryptConfiguration#getProps()} 配置 + */ + private Map props; + + +} diff --git a/spring-brick/src/main/resources/META-INF/spring-configuration-metadata.json b/spring-brick/src/main/resources/META-INF/spring-configuration-metadata.json index 4eec8b41335758607b41fd0d35692123bbfe07dd..67610e7b0fe6bd0ae08c1d01226751432b1b08de 100644 --- a/spring-brick/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/spring-brick/src/main/resources/META-INF/spring-configuration-metadata.json @@ -99,6 +99,38 @@ "sourceType": "com.gitee.starblues.integration.AutoIntegrationConfiguration", "description": "是否完全匹配版本。设置为true表示插件设置的requires的版本号完全匹配version版本号才可允许插件安装, 即: requires=x.y.z; 设置为false表示插件设置的requires的版本号小于等于version值, 插件就可安装, 即requires<=x.y.z", "defaultValue": false + }, + { + "name": "plugin.decrypt", + "type": "com.gitee.starblues.integration.decrypt.DecryptConfiguration", + "sourceType": "com.gitee.starblues.integration.AutoIntegrationConfiguration", + "description": "对插件启动时进行解密校验配置。默认为不启用" + }, + { + "name": "plugin.decrypt.enable", + "type": "java.lang.Boolean", + "sourceType": "com.gitee.starblues.integration.decrypt.DecryptConfiguration", + "description": "解密校验配置是否启用。默认为启用", + "defaultValue": true + }, + { + "name": "plugin.decrypt.className", + "type": "java.lang.String", + "sourceType": "com.gitee.starblues.integration.decrypt.DecryptConfiguration", + "description": "解密实现类名称。默认: com.gitee.starblues.common.cipher.RsaPluginCipher", + "defaultValue": "com.gitee.starblues.common.cipher.RsaPluginCipher" + }, + { + "name": "plugin.decrypt.props", + "type": "java.lang.Map", + "sourceType": "com.gitee.starblues.integration.decrypt.DecryptConfiguration", + "description": "总配置" + }, + { + "name": "plugin.decrypt.plugins", + "type": "java.lang.List", + "sourceType": "com.gitee.starblues.integration.decrypt.DecryptConfiguration", + "description": "指定可生效的插件. 如果不配置, 则默认对全部插件生效" } ] } \ No newline at end of file