diff --git a/.gitignore b/.gitignore index e629dd4ec..cf5a93bbc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ target/ +cmdb-core/data/ # Ignore the generated front-end resource **/resources/static/ # Ignore the generated index.html diff --git a/cmdb-core/src/main/resources/application.yml b/cmdb-core/src/main/resources/application.yml index ca8c5883f..04fc42aff 100755 --- a/cmdb-core/src/main/resources/application.yml +++ b/cmdb-core/src/main/resources/application.yml @@ -62,9 +62,3 @@ cmdb: #Effective ONLY when security-enabled set as true whitelist-ip-address: 127.0.0.1 -logging: - level: - root: INFO - com: - webank: - cmdb: INFO diff --git a/cmdb-core/src/main/resources/logback-spring.xml b/cmdb-core/src/main/resources/logback-spring.xml index d4cfcbcfd..c8964e136 100644 --- a/cmdb-core/src/main/resources/logback-spring.xml +++ b/cmdb-core/src/main/resources/logback-spring.xml @@ -1,7 +1,7 @@ - + diff --git a/cmdb-core/src/test/resources/application-test.yml b/cmdb-core/src/test/resources/application-test.yml index 0b6a0e709..86240b028 100755 --- a/cmdb-core/src/test/resources/application-test.yml +++ b/cmdb-core/src/test/resources/application-test.yml @@ -21,12 +21,12 @@ cmdb: enabled: false whitelist-ip-address: 127.0.0.1 -logging: - level: - root: WARN - com: - webank: - cmdb: INFO - org: - hibernate: - SQL: INFO \ No newline at end of file +#logging: +# level: +# root: WARN +# com: +# webank: +# cmdb: INFO +# org: +# hibernate: +# SQL: INFO \ No newline at end of file diff --git a/cmdb-core/src/test/resources/logback-test-spring.xml b/cmdb-core/src/test/resources/logback-test-spring.xml new file mode 100644 index 000000000..4ff4fd1b7 --- /dev/null +++ b/cmdb-core/src/test/resources/logback-test-spring.xml @@ -0,0 +1,44 @@ + + + + + + + + true + + + + + + + %d{yyyy-MM-dd HH:mm:ss} %-5level %-48logger{48} - %msg%n + + + + + + ${logPath}/${logFile}.log + + ${logPath}/${logFile}-%d{yyyyMMdd}.log + + true + 30 + + + + + + UTF-8 + + + + + + + + diff --git a/cmdb-plugin/pom.xml b/cmdb-plugin/pom.xml index 59c12d977..f6b64f8c7 100644 --- a/cmdb-plugin/pom.xml +++ b/cmdb-plugin/pom.xml @@ -1,81 +1,99 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" + xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + 4.0.0 - - com.webank.cmdb - cmdb - ${cmdb.version} - + + com.webank.cmdb + cmdb + ${cmdb.version} + - wecube-plugins-wecmdb - jar + wecube-plugins-wecmdb + jar - - - com.webank.cmdb - cmdb-core - ${cmdb.version} - - - org.springframework.boot - spring-boot-starter-web - - - org.apache.httpcomponents - httpclient - - - io.springfox - springfox-swagger2 - 2.9.2 - - - io.springfox - springfox-swagger-ui - 2.9.2 - + + + com.webank.cmdb + cmdb-core + ${cmdb.version} + + + org.springframework.boot + spring-boot-starter-web + + + org.apache.httpcomponents + httpclient + + + io.springfox + springfox-swagger2 + 2.9.2 + + + io.springfox + springfox-swagger-ui + 2.9.2 + - - org.springframework.boot - spring-boot-starter-test - test - - - junit - junit - test - - - com.alibaba - fastjson - 1.2.47 - - + + org.springframework.boot + spring-boot-starter-test + test + + + junit + junit + test + + + com.alibaba + fastjson + 1.2.47 + + + + org.bouncycastle + bcprov-ext-jdk15on + 1.63 + - - - - org.springframework.boot - spring-boot-maven-plugin - - - org.apache.maven.plugins - maven-surefire-plugin - - 1 - true - -Xmx2048m -XX:MaxPermSize=256m - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + commons-codec + commons-codec + + + + com.github.ulisesbocchio + jasypt-spring-boot-starter + 3.0.3 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + + 1 + true + -Xmx2048m -XX:MaxPermSize=256m + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/cmdb-plugin/register.xml b/cmdb-plugin/register.xml index 67f1a674a..b236284d2 100644 --- a/cmdb-plugin/register.xml +++ b/cmdb-plugin/register.xml @@ -60,7 +60,7 @@ - + @@ -142,5 +142,20 @@ + + + + entityName + guid + attrName + attrVal + + + guid + errorCode + errorMessage + + + diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/Application.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/Application.java index adc92fba9..0b5b941d5 100644 --- a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/Application.java +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/Application.java @@ -4,20 +4,25 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; -import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; +import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; +import com.webank.cmdb.config.ApplicationProperties; import com.webank.cmdb.config.DatabaseConfig; import com.webank.cmdb.config.SpringAppConfig; import com.webank.cmdb.config.SpringWebConfig; +import com.webank.plugins.wecmdb.config.PluginApplicationProperties; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableSwagger2 -@ComponentScan({ "com.webank.plugins.wecmdb.service", "com.webank.plugins.wecmdb.controller", "com.webank.plugins.wecmdb.mvc", "com.webank.cmdb.config", "com.webank.cmdb.repository" }) +@EnableConfigurationProperties({PluginApplicationProperties.class,ApplicationProperties.class}) +@EnableEncryptableProperties +@ComponentScan({ "com.webank.plugins.wecmdb.propenc", "com.webank.plugins.wecmdb.service", "com.webank.plugins.wecmdb.controller", "com.webank.plugins.wecmdb.mvc", "com.webank.cmdb.config", "com.webank.cmdb.repository" }) @Import({ DatabaseConfig.class }) public class Application extends AbstractAnnotationConfigDispatcherServletInitializer { diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/config/PluginApplicationProperties.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/config/PluginApplicationProperties.java new file mode 100644 index 000000000..5eeb77d79 --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/config/PluginApplicationProperties.java @@ -0,0 +1,18 @@ +package com.webank.plugins.wecmdb.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "plugins") +public class PluginApplicationProperties { + private String propertyEncryptKeyPath; + + public String getPropertyEncryptKeyPath() { + return propertyEncryptKeyPath; + } + + public void setPropertyEncryptKeyPath(String propertyEncryptKeyPath) { + this.propertyEncryptKeyPath = propertyEncryptKeyPath; + } + + +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/controller/WecubeAdapterController.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/controller/WecubeAdapterController.java index 66ca460e7..490dcd37d 100644 --- a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/controller/WecubeAdapterController.java +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/controller/WecubeAdapterController.java @@ -111,6 +111,22 @@ public OperateCiJsonResponse confirmBatchCiData(@RequestBody OperateCiDtoInputs } return response; } + + @PostMapping("/data/update") + @ResponseBody + public OperateCiJsonResponse updateCiData(@RequestBody OperateCiDataUpdateDtoInputs inputs) { + List operateCiDataUpdateDtos = inputs.getInputs(); + OperateCiJsonResponse response = new OperateCiJsonResponse(); + List exceptionHolders = new ArrayList(); + List> results = wecubeAdapterService.updateCiDataByGuid(operateCiDataUpdateDtos, exceptionHolders); + + if (exceptionHolders.size() > 0) { + response = OperateCiJsonResponse.errorWithData(String.format("Fail to update [%s] CIs, detail error in the data block", inputs), results); + } else { + response = OperateCiJsonResponse.okayWithData(results); + } + return response; + } @PostMapping("/data/refresh") @ResponseBody diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/dto/wecube/OperateCiDataUpdateDto.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/dto/wecube/OperateCiDataUpdateDto.java new file mode 100644 index 000000000..a28740d51 --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/dto/wecube/OperateCiDataUpdateDto.java @@ -0,0 +1,43 @@ +package com.webank.plugins.wecmdb.dto.wecube; + +public class OperateCiDataUpdateDto { + private String callbackParameter; + private String entityName; + private String guid; + private String attrName; + private Object attrVal; + public String getCallbackParameter() { + return callbackParameter; + } + public void setCallbackParameter(String callbackParameter) { + this.callbackParameter = callbackParameter; + } + + public String getEntityName() { + return entityName; + } + public void setEntityName(String entityName) { + this.entityName = entityName; + } + public String getGuid() { + return guid; + } + public void setGuid(String guid) { + this.guid = guid; + } + public String getAttrName() { + return attrName; + } + public void setAttrName(String attrName) { + this.attrName = attrName; + } + public Object getAttrVal() { + return attrVal; + } + public void setAttrVal(Object attrVal) { + this.attrVal = attrVal; + } + + + +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/dto/wecube/OperateCiDataUpdateDtoInputs.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/dto/wecube/OperateCiDataUpdateDtoInputs.java new file mode 100644 index 000000000..7cbe336af --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/dto/wecube/OperateCiDataUpdateDtoInputs.java @@ -0,0 +1,16 @@ +package com.webank.plugins.wecmdb.dto.wecube; + +import java.util.List; + +public class OperateCiDataUpdateDtoInputs { + + private List inputs; + + public List getInputs() { + return inputs; + } + + public void setInputs(List inputs) { + this.inputs = inputs; + } +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/EncryptionException.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/EncryptionException.java new file mode 100644 index 000000000..2a0cc871e --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/EncryptionException.java @@ -0,0 +1,37 @@ +package com.webank.plugins.wecmdb.propenc; + +/** + * + * @author gavin + * + */ +public class EncryptionException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 115523361878991641L; + + public EncryptionException() { + super(); + } + + public EncryptionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public EncryptionException(String message, Throwable cause) { + super(message, cause); + } + + public EncryptionException(String message) { + super(message); + } + + public EncryptionException(Throwable cause) { + super(cause); + } + + + +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaBasedStringEncryptor.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaBasedStringEncryptor.java new file mode 100644 index 000000000..da0571513 --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaBasedStringEncryptor.java @@ -0,0 +1,80 @@ +package com.webank.plugins.wecmdb.propenc; + +import javax.annotation.PostConstruct; + +import org.apache.commons.lang3.StringUtils; +import org.jasypt.encryption.StringEncryptor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.webank.plugins.wecmdb.config.PluginApplicationProperties; + +/** + * + * @author gavin + * + */ +@Component("jasyptStringEncryptor") +public class RsaBasedStringEncryptor implements StringEncryptor { + private static final Logger log = LoggerFactory.getLogger(RsaBasedStringEncryptor.class); + + @Autowired + private PluginApplicationProperties appConfigProperties; + + private RsaKeyPair propencRsaKeyPair; + + @PostConstruct + public void afterPropertySet(){ + initPropencRsaKeyPair(); + } + + @Override + public String decrypt(String cipherValue) { + if(!isAvailable()) { + return cipherValue; + } + + try { + byte[] data = RsaEncryptor.decryptByPrivateKey(RsaEncryptor.decodeBase64(cipherValue), + propencRsaKeyPair.getPrivateKey()); + String rawValue = new String(data, RsaEncryptor.DEF_CHARSET); + + return rawValue; + } catch (Exception e) { + log.error("errors while decrypt {} with private key:{}", cipherValue, e.getMessage()); + throw new EncryptionException("Failed to decrypt cipher text due to " + e.getMessage()); + } + } + + @Override + public String encrypt(String rawValue) { + byte[] data = RsaEncryptor.encryptByPublicKey(rawValue.getBytes(RsaEncryptor.DEF_CHARSET), + propencRsaKeyPair.getPublicKey()); + return RsaEncryptor.encodeBase64String(data); + } + + protected void initPropencRsaKeyPair() { + RsaKeyDetector keyDetector = new RsaKeyDetector( + appConfigProperties.getPropertyEncryptKeyPath()); + RsaKeyPair keyPair = keyDetector.detectRsaKeyPair(); + if (keyPair != null) { + log.info("Property encryption RSA key prepared!"); + } + this.propencRsaKeyPair = keyPair; + } + + public boolean isAvailable() { + if(propencRsaKeyPair == null) { + return false; + } + + if(StringUtils.isBlank(propencRsaKeyPair.getPrivateKey())) { + return false; + } + + return true; + } + +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaEncryptablePropertyDetector.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaEncryptablePropertyDetector.java new file mode 100644 index 000000000..07682f0a9 --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaEncryptablePropertyDetector.java @@ -0,0 +1,29 @@ +package com.webank.plugins.wecmdb.propenc; + +import org.springframework.stereotype.Component; + +import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyDetector; + +/** + * + * @author gavin + * + */ +@Component("encryptablePropertyDetector") +public class RsaEncryptablePropertyDetector implements EncryptablePropertyDetector { + public static final String DEF_ENC_PREFIX = "RSA@"; + + @Override + public boolean isEncrypted(String value) { + if (value != null) { + return value.startsWith(DEF_ENC_PREFIX); + } + return false; + } + + @Override + public String unwrapEncryptedValue(String value) { + return value.trim().substring(DEF_ENC_PREFIX.length()); + } + +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaEncryptor.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaEncryptor.java new file mode 100644 index 000000000..449d603eb --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaEncryptor.java @@ -0,0 +1,209 @@ +package com.webank.plugins.wecmdb.propenc; + +import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; +import java.security.InvalidKeyException; +import java.security.KeyFactory; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.Security; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; + +import org.apache.commons.codec.binary.Base64; +import org.bouncycastle.jce.provider.BouncyCastleProvider; + +/** + * 512 PKCS#8 + * @author gavin + * + */ +public final class RsaEncryptor { + + public static final String KEY_ALGORITHM = "RSA"; + + public static final String DEF_ENCODING = "UTF-8"; + public static final Charset DEF_CHARSET = Charset.forName(DEF_ENCODING); + + private static final int KEY_SIZE = 512; + + private static final RsaEncryptor _INSTANCE = new RsaEncryptor(); + + private boolean useExternalProvider = true; + + private RsaEncryptor() { + } + + public static String encodeBase64String(byte[] data){ + return Base64.encodeBase64String(data); + } + + public static byte[] decodeBase64(String base64String){ + return Base64.decodeBase64(base64String); + } + + public static RsaKeyPair initKey() { + try { + return _INSTANCE.doInitKey(); + } catch (NoSuchAlgorithmException e) { + throw new EncryptionException(e.getMessage()); + } catch (UnsupportedEncodingException e) { + throw new EncryptionException(e.getMessage()); + } + } + + public static byte[] decryptByPrivateKey(byte[] data, String base64KeyVal){ + return decryptByPrivateKey(data, decodeBase64(base64KeyVal)); + } + + public static byte[] decryptByPrivateKey(byte[] data, byte[] key) { + try { + return _INSTANCE.doDecryptByPrivateKey(data, key); + } catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException | InvalidKeyException + | IllegalBlockSizeException | BadPaddingException e) { + throw new EncryptionException(e.getMessage()); + } + + } + + public static byte[] encryptByPrivateKey(byte[] data, String base64KeyVal){ + return encryptByPrivateKey(data,decodeBase64(base64KeyVal)); + } + + public static byte[] encryptByPrivateKey(byte[] data, byte[] key) { + try { + return _INSTANCE.doEncryptByPrivateKey(data, key); + } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException + | IllegalBlockSizeException | BadPaddingException e) { + throw new EncryptionException(e.getMessage()); + } + } + + public static byte[] decryptByPublicKey(byte[] data, String base64KeyVal){ + return decryptByPublicKey(data, decodeBase64(base64KeyVal)); + } + + public static byte[] decryptByPublicKey(byte[] data, byte[] key) { + try { + return _INSTANCE.doDecryptByPublicKey(data, key); + } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException + | IllegalBlockSizeException | BadPaddingException e) { + throw new EncryptionException(e.getMessage()); + } + } + + public static byte[] encryptByPublicKey(byte[] data, String base64KeyVal){ + return encryptByPublicKey(data, decodeBase64(base64KeyVal)); + } + + public static byte[] encryptByPublicKey(byte[] data, byte[] key) { + try { + return _INSTANCE.doEncryptByPublicKey(data, key); + } catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException + | IllegalBlockSizeException | BadPaddingException e) { + throw new EncryptionException(e.getMessage()); + } + } + + private byte[] doEncryptByPublicKey(byte[] data, byte[] key) + throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, + IllegalBlockSizeException, BadPaddingException { + initProvider(); + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); + + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + + PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); + + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + + return cipher.doFinal(data); + } + + private byte[] doDecryptByPublicKey(byte[] data, byte[] key) + throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, + IllegalBlockSizeException, BadPaddingException { + initProvider(); + X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); + + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + + PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); + + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + + cipher.init(Cipher.DECRYPT_MODE, publicKey); + + return cipher.doFinal(data); + } + + private byte[] doEncryptByPrivateKey(byte[] data, byte[] key) + throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, + IllegalBlockSizeException, BadPaddingException { + initProvider(); + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + + PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); + + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + + cipher.init(Cipher.ENCRYPT_MODE, privateKey); + + return cipher.doFinal(data); + } + + private byte[] doDecryptByPrivateKey(byte[] data, byte[] key) + throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, + IllegalBlockSizeException, BadPaddingException { + initProvider(); + PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); + KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); + + PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); + + Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); + + cipher.init(Cipher.DECRYPT_MODE, privateKey); + + return cipher.doFinal(data); + + } + + private RsaKeyPair doInitKey() throws NoSuchAlgorithmException, UnsupportedEncodingException { + initProvider(); + KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); + keyPairGen.initialize(KEY_SIZE); + KeyPair keyPair = keyPairGen.generateKeyPair(); + + RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); + RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); + + RsaKeyPair aKeyPair = RsaKeyPairBuilder + .withPublicKey(encodeBase64String((publicKey.getEncoded()))) + .withPrivateKey(encodeBase64String(privateKey.getEncoded())).build(); + + return aKeyPair; + } + + private void initProvider() { + if (useExternalProvider && Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) { + Security.addProvider(new BouncyCastleProvider()); + } + } + + +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyDetector.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyDetector.java new file mode 100644 index 000000000..9a997af59 --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyDetector.java @@ -0,0 +1,87 @@ +package com.webank.plugins.wecmdb.propenc; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author gavin + * + */ +public class RsaKeyDetector { + private static final Logger log = LoggerFactory.getLogger(RsaKeyDetector.class); + + + private File rsaKeyFile; + + public RsaKeyDetector() { + + } + + public RsaKeyDetector(String rasKeyFilePath) { + log.info("init {} with {}", RsaKeyDetector.class.getSimpleName(), rasKeyFilePath); + if (rasKeyFilePath == null || rasKeyFilePath.trim().length() < 1) { + rsaKeyFile = null; + return; + } + + rsaKeyFile = new File(rasKeyFilePath.trim()); + } + + public RsaKeyPair detectRsaKeyPair() { + return doDetectRsaKeyPair(); + } + + private RsaKeyPair doDetectRsaKeyPair() { + String rsaPrivKeyString = tryFindPrivateKeyFromExternal(); + if (StringUtils.isBlank(rsaPrivKeyString)) { + return null; + } + + return new RsaKeyPair(null, rsaPrivKeyString); + } + + private String tryFindPrivateKeyFromExternal() { + if (!this.rsaKeyFile.exists()) { + log.info("Private key does not exist,filepath={}", this.rsaKeyFile.getAbsolutePath()); + return null; + } + + try (FileInputStream input = new FileInputStream(rsaKeyFile)) { + return readInputStream(input); + } catch (IOException e) { + log.error("errors while reading private key", e); + String msg = String.format("Failed to read private key {%s}.", this.rsaKeyFile.getAbsolutePath()); + throw new EncryptionException(msg); + } + } + + private String readInputStream(InputStream inputStream) throws IOException { + + if (inputStream == null) { + throw new IllegalArgumentException(); + } + + BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, RsaEncryptor.DEF_CHARSET)); + String sLine = null; + StringBuilder content = new StringBuilder(); + while ((sLine = br.readLine()) != null) { + if (sLine.startsWith("-")) { + continue; + } + + content.append(sLine.trim()); + } + + return content.toString(); + } + +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyPair.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyPair.java new file mode 100644 index 000000000..9dd576ca0 --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyPair.java @@ -0,0 +1,27 @@ +package com.webank.plugins.wecmdb.propenc; + +/** + * + * @author gavin + * + */ +public class RsaKeyPair { + + private final String privateKey; + + private final String publicKey; + + RsaKeyPair(String publicKey, String privateKey) { + super(); + this.privateKey = privateKey; + this.publicKey = publicKey; + } + + public String getPrivateKey() { + return privateKey; + } + + public String getPublicKey() { + return publicKey; + } +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyPairBuilder.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyPairBuilder.java new file mode 100644 index 000000000..b26b30ee5 --- /dev/null +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/propenc/RsaKeyPairBuilder.java @@ -0,0 +1,37 @@ +package com.webank.plugins.wecmdb.propenc; + +/** + * + * @author gavin + * + */ +public class RsaKeyPairBuilder { + + private String privateKey; + private String publicKey; + + public static RsaKeyPairBuilder withPublicKey(String publicKey) { + RsaKeyPairBuilder b = new RsaKeyPairBuilder(); + b.setPublicKey(publicKey); + + return b; + } + + public RsaKeyPairBuilder withPrivateKey(String privateKey) { + this.setPrivateKey(privateKey); + return this; + } + + public RsaKeyPair build() { + return new RsaKeyPair(this.publicKey, this.privateKey); + } + + private void setPrivateKey(String privateKey) { + this.privateKey = privateKey; + } + + private void setPublicKey(String publicKey) { + this.publicKey = publicKey; + } + +} diff --git a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/service/WecubeAdapterService.java b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/service/WecubeAdapterService.java index f17958e3f..810fe5b1d 100644 --- a/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/service/WecubeAdapterService.java +++ b/cmdb-plugin/src/main/java/com/webank/plugins/wecmdb/service/WecubeAdapterService.java @@ -23,6 +23,7 @@ import javax.transaction.Transactional; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -81,7 +82,6 @@ public QueryResponse queryCiTypeAttrs(QueryRequest queryObject) { return staticDtoService.query(CiTypeAttrDto.class, queryObject); } - @Transactional public List> confirmBatchCiData(List operateCiDtos, List exceptionHolders) { List> results = new ArrayList<>(); operateCiDtos.forEach(operateCiDto -> { @@ -394,6 +394,55 @@ public List> updateCiData(String entityName, List> updateCiDataByGuid(List operateCiDataUpdateDtos, List exceptionHolders){ + List> results = new ArrayList<>(); + operateCiDataUpdateDtos.forEach(operateCiDataUpdateDto -> { + Map resultItem = new HashMap<>(); + resultItem.put(CALLBACK_PARAMETER, operateCiDataUpdateDto.getCallbackParameter()); + resultItem.put(ERROR_CODE, SUCCESS); + resultItem.put(ERROR_MESSAGE, ""); + + if (StringUtils.isBlank(operateCiDataUpdateDto.getGuid())) { + String errorMessage = "Field 'guid' is required for CI data update."; + resultItem.put(ERROR_CODE, FAIL); + resultItem.put(ERROR_MESSAGE, errorMessage); + results.add(resultItem); + } + + String guid = operateCiDataUpdateDto.getGuid(); + + try { + updateSingleCiDataByGuid(operateCiDataUpdateDto); + resultItem.put("guid", guid); + resultItem.put(ERROR_CODE, SUCCESS); + resultItem.put(ERROR_MESSAGE, "ok"); + results.add(resultItem); + }catch(Exception e) { + String errorMessage = String.format("Failed to update CI [guid = %s], error = %s", guid, e.getMessage()); + logger.warn(errorMessage, e); + resultItem.put(ERROR_CODE, FAIL); + resultItem.put(ERROR_MESSAGE, errorMessage); + exceptionHolders.add(new ExceptionHolder(operateCiDataUpdateDto.getCallbackParameter(), operateCiDataUpdateDto, errorMessage, null)); + results.add(resultItem); + } + }); + + return results; + } + + private void updateSingleCiDataByGuid(OperateCiDataUpdateDto operateCiDataUpdateDto) { + String entityName = operateCiDataUpdateDto.getEntityName(); + String guid = operateCiDataUpdateDto.getGuid(); + String attrName = operateCiDataUpdateDto.getAttrName(); + Object attrVal = operateCiDataUpdateDto.getAttrVal(); + + Map convertedUpdateReq = new HashMap(); + convertedUpdateReq.put("guid", guid); + convertedUpdateReq.put(attrName, attrVal); + + updateCiData(entityName, Arrays.asList(convertedUpdateReq)); + } private List> convertedRequest(List> originRequest) { List> convertedRequest = new ArrayList<>(); diff --git a/cmdb-plugin/src/main/resources/application.yml b/cmdb-plugin/src/main/resources/application.yml index 960663b68..0bbe942ad 100644 --- a/cmdb-plugin/src/main/resources/application.yml +++ b/cmdb-plugin/src/main/resources/application.yml @@ -16,6 +16,7 @@ server: plugins: pacakge-name: wecmdb + property-encrypt-key-path: ${PROPERTY_ENCRYPT_KEY_PATH:/data/certs/rsa_key} spring: banner: @@ -77,10 +78,10 @@ platform: sub-system-private-key: MockPrivateKey auth-server-public-key: MockPublicKey -logging: - level: - root: INFO - com: - webank: - cmdb: INFO +#logging: +# level: +# root: INFO +# com: +# webank: +# cmdb: INFO \ No newline at end of file diff --git a/cmdb-plugin/src/main/resources/logback-spring.xml b/cmdb-plugin/src/main/resources/logback-spring.xml index facc04ec7..561486108 100644 --- a/cmdb-plugin/src/main/resources/logback-spring.xml +++ b/cmdb-plugin/src/main/resources/logback-spring.xml @@ -1,7 +1,7 @@ - + @@ -36,8 +36,9 @@ - diff --git a/cmdb-plugin/src/test/resources/logback-test-spring.xml b/cmdb-plugin/src/test/resources/logback-test-spring.xml new file mode 100644 index 000000000..28197fe00 --- /dev/null +++ b/cmdb-plugin/src/test/resources/logback-test-spring.xml @@ -0,0 +1,44 @@ + + + + + + + + true + + + + + + + %d{yyyy-MM-dd HH:mm:ss} %-5level %-48logger{48} - %msg%n + + + + + + ${logPath}/${logFile}.log + + ${logPath}/${logFile}-%d{yyyyMMdd}.log + + true + 30 + + + + + + UTF-8 + + + + + + + + + diff --git a/cmdb-ui/src/pages/components/cmdb-table/index.js b/cmdb-ui/src/pages/components/cmdb-table/index.js index 20cadeca5..f89c52ac1 100644 --- a/cmdb-ui/src/pages/components/cmdb-table/index.js +++ b/cmdb-ui/src/pages/components/cmdb-table/index.js @@ -581,6 +581,32 @@ export default { } }) }, + // 优化差异化变量tooltip显示 + managementContent (str) { + let arr = str.split(',') + const sIndex = arr.findIndex(item => item.indexOf('=') > 0) + let keyArr = [] + let valueArr = [] + arr.forEach((item, index) => { + if (index < sIndex) { + keyArr.push(item) + } else if (index === sIndex) { + let splitTag = item.split('=') + keyArr.push(splitTag[0]) + valueArr.unshift(splitTag[1]) + } else { + valueArr.push(item) + } + }) + // 差异化表达式中字段加了奇葩字符,加此空格优化显示 + keyArr[0] = ' ' + keyArr[0] + let res = [] + const len = Math.max(keyArr.length, valueArr.length) + for (let j = 0; j < len; j++) { + res.push((keyArr[j] || '***') + '=' + (valueArr[j] || '')) + } + return res.join('\n') + }, renderCol (col, isLastCol = false) { return { ...col, @@ -603,7 +629,6 @@ export default { } else { content = params.row.weTableForm[col.key] } - const containerId = 'ref' + Math.ceil(Math.random() * 1000000) return h( @@ -617,7 +642,11 @@ export default { ) { this.timer = setTimeout( params => { - this.tipContent = content + if (col.key === 'variable_values') { + this.tipContent = this.managementContent(content) + } else { + this.tipContent = content + } const popcorn = document.querySelector('#' + containerId) const tooltip = document.querySelector('#' + params.randomId) createPopper(popcorn, tooltip, { @@ -737,7 +766,7 @@ export default {
{this.tipContent && (
- {this.tipContent} +

{this.tipContent}

)}
diff --git a/cmdb-ui/src/pages/designing/ci-data.vue b/cmdb-ui/src/pages/designing/ci-data.vue index ffdb06abc..aef4eca3b 100755 --- a/cmdb-ui/src/pages/designing/ci-data.vue +++ b/cmdb-ui/src/pages/designing/ci-data.vue @@ -1,5 +1,5 @@