diff --git a/Dockerfile b/Dockerfile index b22bfc1..493d689 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,3 @@ -# from platten/alpine-oracle-jre8-docker -# LABEL maintainer = "Webank CTB Team" -# ADD artifacts-core/target/artifacts-core-0.0.1-SNAPSHOT.jar /application/wecube-plugins-artifacts.jar -# ADD build/start.sh /scripts/start.sh -# RUN chmod +x /scripts/start.sh -# ADD build/tomcat_exporter.tar /scripts/ -# CMD ["/bin/sh","-c","/scripts/start.sh $WECUBE_GATEWAY_SERVER_URL $ARTIFACTS_S3_SERVER_URL $ARTIFACTS_S3_ACCESS_KEY $ARTIFACTS_S3_SECRET_KEY $ARTIFACTS_NEXUS_SERVER_URL $ARTIFACTS_NEXUS_USERNAME $ARTIFACTS_NEXUS_PASSWORD $ARTIFACTS_NEXUS_REPOSITORY $CMDB_ARTIFACT_PATH $JWT_SIGNING_KEY"] - from sonatype/nexus3:3.27.0 LABEL maintainer = "Webank CTB Team" USER root diff --git a/Dockerfile_nonexus b/Dockerfile_nonexus new file mode 100644 index 0000000..1a8a264 --- /dev/null +++ b/Dockerfile_nonexus @@ -0,0 +1,19 @@ +FROM python:3.7-slim +LABEL maintainer = "Webank CTB Team" +RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list +RUN sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list +ADD artifacts-corepy/requirements.txt /tmp/requirements.txt +ADD artifacts-corepy/dist/* /tmp/ +RUN mkdir -p /etc/artifacts_corepy/ +RUN mkdir -p /var/log/artifacts_corepy/ +ADD artifacts-corepy/etc/* /etc/artifacts_corepy/ +# Install && Clean up +RUN apt update && apt-get -y install gcc python3-dev && \ + pip3 install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com -r /tmp/requirements.txt && \ + pip3 install /tmp/*.whl && \ + rm -rf /root/.cache && apt autoclean && \ + rm -rf /tmp/* /var/lib/apt/* /var/cache/* && \ + apt purge -y `cat /var/log/apt/history.log|grep 'Install: '|tail -1| sed 's/Install://'| sed 's/\ /\n/g' | sed '/(/d' | sed '/)/d' | sed ':l;N;s/\n/ /;b l'` +ADD build/start_all.sh /scripts/start_all.sh +RUN chmod +x /scripts/start_all.sh +CMD ["/bin/sh","-c","/scripts/start_all.sh"] \ No newline at end of file diff --git a/Makefile b/Makefile index c1e55ea..6ee5189 100644 --- a/Makefile +++ b/Makefile @@ -3,42 +3,7 @@ version=$(shell bash ./build/version.sh) date=$(shell date +%Y%m%d%H%M%S) project_name=$(shell basename "${current_dir}") remote_docker_image_registry=ccr.ccs.tencentyun.com/webankpartners/wecube-plugins-artifacts - - -clean: - rm -rf $(current_dir)/target - -.PHONY:build -build: clean - mkdir -p repository - docker run --rm --name wecube-plugins-artifacts-build -v /data/repository:/usr/src/mymaven/repository -v $(current_dir)/build/maven_settings.xml:/usr/share/maven/ref/settings-docker.xml -v $(current_dir):/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn -U clean install -Dmaven.test.skip=true -DbuildType=plugin -s /usr/share/maven/ref/settings-docker.xml dependency:resolve - -image: build - docker build -t $(project_name):$(version) . - -s3_server_url=http://10.10.10.1:9000 -s3_access_key=access_key -s3_secret_key=secret_key - -.PHONY:package -package: image - rm -rf package - mkdir -p package - cd package && docker save $(project_name):$(version) -o image.tar - cd package && cp ../register.xml . - cd package && sed -i "s~{{REPOSITORY}}~$(project_name)~g" register.xml - cd package && sed -i "s~{{VERSION}}~$(version)~g" register.xml - cd artifacts-ui/dist && zip -r ui.zip . - cd package && cp ../artifacts-ui/dist/ui.zip . - cd package && zip -r $(project_name)-$(version).zip . - docker rmi $(project_name):$(version) - -upload: package - $(eval container_id:=$(shell docker run -v $(current_dir)/package:/package -itd --entrypoint=/bin/sh minio/mc)) - docker exec $(container_id) mc config host add wecubeS3 $(s3_server_url) $(s3_access_key) $(s3_secret_key) wecubeS3 - docker exec $(container_id) mc cp /package/$(project_name)-$(version).zip wecubeS3/wecube-plugin-package-bucket - docker stop $(container_id) - docker rm -f $(container_id) +with_nexus='true' clean_py: rm -rf $(current_dir)/artifacts-corepy/dist/ @@ -53,7 +18,12 @@ build_py: clean_py image_py: build_py wget -O nexus-data.tar.gz https://wecube-1259801214.cos.ap-guangzhou.myqcloud.com/nexus-data/nexus-data.tar.gz - docker build -t $(project_name):$(version) . + @if [ $(with_nexus) == 'true' ]; \ + then \ + docker build -t $(project_name):$(version) .; \ + else \ + docker build -t $(project_name):$(version) -f Dockerfile_nonexus .; \ + fi package_py: image_py rm -rf package diff --git a/artifacts-core/.factorypath b/artifacts-core/.factorypath deleted file mode 100644 index f484f44..0000000 --- a/artifacts-core/.factorypath +++ /dev/null @@ -1,135 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/artifacts-core/.gitignore b/artifacts-core/.gitignore deleted file mode 100644 index 4b9b730..0000000 --- a/artifacts-core/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/target/ -/bin/ -/.apt_generated/ diff --git a/artifacts-core/pom.xml b/artifacts-core/pom.xml deleted file mode 100644 index 0cda6e7..0000000 --- a/artifacts-core/pom.xml +++ /dev/null @@ -1,158 +0,0 @@ - - 4.0.0 - - com.webank - wecube-plugins-artifacts - 0.0.1-SNAPSHOT - - artifacts-core - - - io.jsonwebtoken - jjwt - 0.7.0 - - - org.springframework.boot - spring-boot-starter-data-jpa - - - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.session - spring-session-core - - - - org.springframework.boot - spring-boot-configuration-processor - - - org.springframework.boot - spring-boot-starter-security - - - - org.apache.commons - commons-lang3 - - - - commons-beanutils - commons-beanutils - 1.9.4 - - - - commons-io - commons-io - 2.6 - - - - com.google.guava - guava - 27.0.1-jre - - - org.projectlombok - lombok - - - - io.springfox - springfox-swagger2 - 2.9.2 - compile - - - io.springfox - springfox-swagger-ui - 2.9.2 - compile - - - - mysql - mysql-connector-java - runtime - - - - com.amazonaws - aws-java-sdk-s3 - 1.11.106 - - - - org.apache.poi - poi - 4.1.0 - - - - org.bouncycastle - bcprov-ext-jdk15on - 1.63 - - - - commons-codec - commons-codec - - - - com.github.ulisesbocchio - jasypt-spring-boot-starter - 3.0.3 - - - - com.h2database - h2 - test - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - UTF-8 - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - - - - - - \ No newline at end of file diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/Application.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/Application.java deleted file mode 100644 index ba4a361..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/Application.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.webank.plugins.artifacts; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; - -import springfox.documentation.swagger2.annotations.EnableSwagger2; - -@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) -@SpringBootApplication -@EnableSwagger2 -public class Application { - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/DefaultJwtSsoTokenParser.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/DefaultJwtSsoTokenParser.java deleted file mode 100644 index 3d4d32a..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/DefaultJwtSsoTokenParser.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.webank.plugins.artifacts.auth.filter; - -import org.apache.commons.codec.binary.Base64; - -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.Jws; -import io.jsonwebtoken.JwtParser; -import io.jsonwebtoken.Jwts; - -/** - * - * @author gavinli - * - */ -public class DefaultJwtSsoTokenParser implements JwtSsoTokenParser { - - private static final String SIGNING_KEY = "Platform+Auth+Server+Secret"; - - private String jwtSigningKey; - - private JwtParser jwtParser; - - public DefaultJwtSsoTokenParser(String jwtSigningKey) { - if (jwtSigningKey == null || jwtSigningKey.trim().length() < 1) { - this.jwtSigningKey = SIGNING_KEY; - } else { - this.jwtSigningKey = jwtSigningKey; - } - - this.jwtParser = Jwts.parser().setSigningKey(decodeBase64(getJwtSigningKey())); - } - - @Override - public Jws parseJwt(String token) { - return jwtParser.parseClaimsJws(token); - } - - private String getJwtSigningKey() { - return jwtSigningKey; - } - - private byte[] decodeBase64(String base64String) { - return Base64.decodeBase64(base64String); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/Http401AuthenticationEntryPoint.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/Http401AuthenticationEntryPoint.java deleted file mode 100644 index a409e24..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/Http401AuthenticationEntryPoint.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.webank.plugins.artifacts.auth.filter; - -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.web.AuthenticationEntryPoint; - -import com.fasterxml.jackson.databind.ObjectMapper; - -public class Http401AuthenticationEntryPoint implements AuthenticationEntryPoint { - private static final Logger log = LoggerFactory.getLogger(Http401AuthenticationEntryPoint.class); - - private static String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate"; - - private String headerValue = "Bearer realm=\"Central Authentication Server\";profile=\"JWT\";"; - - private ObjectMapper objectMapper = new ObjectMapper(); - - @Override - public void commence(HttpServletRequest request, HttpServletResponse response, - AuthenticationException authException) throws IOException, ServletException { - - log.info("=== authentication failed === "); - response.setHeader(HEADER_WWW_AUTHENTICATE, translateAuthenticateHeader(authException)); - response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); - - AuthenticationFailedResponse responseBody = AuthenticationFailedResponse.error(authException.getMessage()); - response.getOutputStream().println(objectMapper.writeValueAsString(responseBody)); - response.getOutputStream().flush(); - - } - - protected String translateAuthenticateHeader(AuthenticationException e){ - StringBuilder sb = new StringBuilder(); - sb.append(this.headerValue); - sb.append("error=\"").append(e.getMessage()).append("\";"); - - return sb.toString(); - } - - static class AuthenticationFailedResponse { - public final static String STATUS_OK = "OK"; - public final static String STATUS_ERROR = "ERROR"; - - public final static String OK_MESSAGE = "success"; - - private String status; - private String message; - private Object data; - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public AuthenticationFailedResponse withData(Object data){ - this.data = data; - return this; - } - - public static AuthenticationFailedResponse okay() { - AuthenticationFailedResponse result = new AuthenticationFailedResponse(); - result.setStatus(STATUS_OK); - result.setMessage(OK_MESSAGE); - return result; - } - - public static AuthenticationFailedResponse okayWithData(Object data) { - return okay().withData(data); - } - - public static AuthenticationFailedResponse error(String errorMessage) { - AuthenticationFailedResponse result = new AuthenticationFailedResponse(); - result.setStatus(STATUS_ERROR); - result.setMessage(errorMessage); - return result; - } - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtClientConfig.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtClientConfig.java deleted file mode 100644 index ae4f1e6..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtClientConfig.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.webank.plugins.artifacts.auth.filter; - -public class JwtClientConfig { - private String signingKey; - - public String getSigningKey() { - return signingKey; - } - - public void setSigningKey(String signingKey) { - this.signingKey = signingKey; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtSsoBasedAuthenticationFilter.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtSsoBasedAuthenticationFilter.java deleted file mode 100644 index e7c7e15..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtSsoBasedAuthenticationFilter.java +++ /dev/null @@ -1,193 +0,0 @@ -package com.webank.plugins.artifacts.auth.filter; - -import java.io.IOException; -import java.util.ArrayList; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.security.access.AccessDeniedException; -import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException; -import org.springframework.security.authentication.AuthenticationManager; -import org.springframework.security.authentication.BadCredentialsException; -import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.core.AuthenticationException; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.security.core.authority.SimpleGrantedAuthority; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.web.AuthenticationEntryPoint; -import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; - - -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.ExpiredJwtException; -import io.jsonwebtoken.Jws; -import io.jsonwebtoken.JwtException; - -public class JwtSsoBasedAuthenticationFilter extends BasicAuthenticationFilter { - private static final Logger log = LoggerFactory.getLogger(JwtSsoBasedAuthenticationFilter.class); - - private static String HEADER_AUTHORIZATION = "Authorization"; - private static String PREFIX_BEARER_TOKEN = "Bearer "; - private static String CLAIM_KEY_TYPE = "type"; - private static String CLAIM_KEY_AUTHORITIES = "authority"; - private static String TOKEN_TYPE_ACCESS = "accessToken"; - - private JwtSsoTokenParser jwtParser = null; - - private boolean ignoreFailure = false; - - public JwtSsoBasedAuthenticationFilter(AuthenticationManager authenticationManager, JwtClientConfig jwtClientConfig) { - super(authenticationManager); - this.ignoreFailure = true; - this.jwtParser = new DefaultJwtSsoTokenParser(jwtClientConfig.getSigningKey()); - } - - public JwtSsoBasedAuthenticationFilter(AuthenticationManager authenticationManager, - AuthenticationEntryPoint authenticationEntryPoint, JwtClientConfig jwtClientConfig) { - super(authenticationManager, authenticationEntryPoint); - this.jwtParser = new DefaultJwtSsoTokenParser(jwtClientConfig.getSigningKey()); - } - - @Override - public void afterPropertiesSet() { - super.afterPropertiesSet(); - - if (log.isInfoEnabled()) { - log.info("Filter:{} applied", this.getClass().getSimpleName()); - } - } - - @Override - protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) - throws IOException, ServletException { - if (log.isDebugEnabled()) { - log.debug("=== doFilterInternal ==="); - } - SecurityContextHolder.clearContext(); - - String header = request.getHeader(HEADER_AUTHORIZATION); - if (header == null || !header.startsWith(PREFIX_BEARER_TOKEN)) { - - log.debug("bearer token does not exist"); - - chain.doFilter(request, response); - - return; - } - - if (log.isDebugEnabled()) { - log.debug("start to authenticate with bearer token"); - } - - try { - UsernamePasswordAuthenticationToken authentication = getAuthentication(request); - if (authentication != null && authentication.isAuthenticated()) { - SecurityContextHolder.getContext().setAuthentication(authentication); - } - } catch (AuthenticationException failed) { - log.debug("authentication failed"); - - SecurityContextHolder.clearContext(); - - onUnsuccessfulAuthentication(request, response, failed); - - if (this.ignoreFailure) { - chain.doFilter(request, response); - } else { - this.getAuthenticationEntryPoint().commence(request, response, failed); - } - - return; - - } - executeFilter(request, response, chain); - } - - protected void executeFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) - throws IOException, ServletException { - try { - chain.doFilter(request, response); - } finally { - SecurityContextHolder.clearContext(); - } - } - - protected boolean isIgnoreFailure() { - return this.ignoreFailure; - } - - protected void onUnsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, - AuthenticationException failed) throws IOException { - } - - protected void validateRequestHeader(HttpServletRequest request) { - String header = request.getHeader(HEADER_AUTHORIZATION); - if (header == null || !header.startsWith(PREFIX_BEARER_TOKEN)) { - throw new BadCredentialsException("Access token is required."); - } - } - - protected UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) { - validateRequestHeader(request); - - String sAccessTokenHeader = request.getHeader(HEADER_AUTHORIZATION); - - String sAccessToken = sAccessTokenHeader.substring(PREFIX_BEARER_TOKEN.length()).trim(); - - if (StringUtils.isBlank(sAccessToken)) { - throw new AuthenticationCredentialsNotFoundException("Access token is blank."); - } - - Jws jwt = null; - try { - jwt = jwtParser.parseJwt(sAccessToken); - } catch (ExpiredJwtException e) { - throw new BadCredentialsException("Access token has expired."); - } catch (JwtException e) { - throw new BadCredentialsException("Access token is not available."); - } - - Claims claims = jwt.getBody(); - - String sAuthorities = claims.get(CLAIM_KEY_AUTHORITIES, String.class); - - String username = claims.getSubject(); - - log.info("subject:{}", username); - - String tokenType = claims.get(CLAIM_KEY_TYPE, String.class); - - if (!TOKEN_TYPE_ACCESS.equals(tokenType)) { - throw new AccessDeniedException("Access token is required."); - } - - if (sAuthorities.length() >= 2) { - sAuthorities = sAuthorities.substring(1); - sAuthorities = sAuthorities.substring(0, sAuthorities.length() - 1); - } - - log.info("Authority String:{}", sAuthorities); - - ArrayList authorities = new ArrayList(); - - if (StringUtils.isNotBlank(sAuthorities)) { - String[] aAuthParts = sAuthorities.split(","); - for (String s : aAuthParts) { - GrantedAuthority ga = new SimpleGrantedAuthority(s.trim()); - authorities.add(ga); - } - } - - log.info("Authorities:{}", authorities); - - return new UsernamePasswordAuthenticationToken(username, sAccessTokenHeader, authorities); - - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtSsoTokenParser.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtSsoTokenParser.java deleted file mode 100644 index 0184d62..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/auth/filter/JwtSsoTokenParser.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.webank.plugins.artifacts.auth.filter; - -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.Jws; - -public interface JwtSsoTokenParser { - Jws parseJwt(String token); -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/ApplicationProperties.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/ApplicationProperties.java deleted file mode 100644 index 67a88f7..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/ApplicationProperties.java +++ /dev/null @@ -1,235 +0,0 @@ -package com.webank.plugins.artifacts.commons; - -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -@Component -@ConfigurationProperties(prefix = "plugins") -public class ApplicationProperties { - - private String wecubeGatewayServerUrl = ""; - private String artifactsS3ServerUrl = ""; - private String artifactsS3AccessKey = ""; - private String artifactsS3SecretKey = ""; - private String artifactsS3BucketName = ""; - private String artifactsNexusServerUrl = ""; - private String artifactsNexusUsername = ""; - private String artifactsNexusPassword = ""; - private String artifactsNexusRepository = ""; - private String cmdbArtifactPath = ""; - private Map customHeaders = new LinkedHashMap<>(); - private Set sensitiveHeaders = null; - private String jwtSigningKey = "Platform+Auth+Server+Secret"; - private boolean artifactsLocalEnabled = true; - private boolean artifactsNexusEnabled = true; - private String propertyEncryptKeyPath; - - public String getPropertyEncryptKeyPath() { - return propertyEncryptKeyPath; - } - - public void setPropertyEncryptKeyPath(String propertyEncryptKeyPath) { - this.propertyEncryptKeyPath = propertyEncryptKeyPath; - } - - public String getWecubeGatewayServerUrl() { - return wecubeGatewayServerUrl; - } - - public void setWecubeGatewayServerUrl(String wecubeGatewayServerUrl) { - this.wecubeGatewayServerUrl = wecubeGatewayServerUrl; - } - - public String getArtifactsS3ServerUrl() { - return artifactsS3ServerUrl; - } - - public void setArtifactsS3ServerUrl(String artifactsS3ServerUrl) { - this.artifactsS3ServerUrl = artifactsS3ServerUrl; - } - - public String getArtifactsS3AccessKey() { - return artifactsS3AccessKey; - } - - public void setArtifactsS3AccessKey(String artifactsS3AccessKey) { - this.artifactsS3AccessKey = artifactsS3AccessKey; - } - - public String getArtifactsS3SecretKey() { - return artifactsS3SecretKey; - } - - public void setArtifactsS3SecretKey(String artifactsS3SecretKey) { - this.artifactsS3SecretKey = artifactsS3SecretKey; - } - - public String getArtifactsS3BucketName() { - return artifactsS3BucketName; - } - - public void setArtifactsS3BucketName(String artifactsS3BucketName) { - this.artifactsS3BucketName = artifactsS3BucketName; - } - - public String getArtifactsNexusServerUrl() { - return artifactsNexusServerUrl; - } - - public void setArtifactsNexusServerUrl(String artifactsNexusServerUrl) { - this.artifactsNexusServerUrl = artifactsNexusServerUrl; - } - - public String getArtifactsNexusUsername() { - return artifactsNexusUsername; - } - - public void setArtifactsNexusUsername(String artifactsNexusUsername) { - this.artifactsNexusUsername = artifactsNexusUsername; - } - - public String getArtifactsNexusPassword() { - return artifactsNexusPassword; - } - - public void setArtifactsNexusPassword(String artifactsNexusPassword) { - this.artifactsNexusPassword = artifactsNexusPassword; - } - - public String getArtifactsNexusRepository() { - return artifactsNexusRepository; - } - - public void setArtifactsNexusRepository(String artifactsNexusRepository) { - this.artifactsNexusRepository = artifactsNexusRepository; - } - - public String getCmdbArtifactPath() { - return cmdbArtifactPath; - } - - public void setCmdbArtifactPath(String cmdbArtifactPath) { - this.cmdbArtifactPath = cmdbArtifactPath; - } - - public Map getCustomHeaders() { - return customHeaders; - } - - public void setCustomHeaders(Map customHeaders) { - this.customHeaders = customHeaders; - } - - public Set getSensitiveHeaders() { - return sensitiveHeaders; - } - - public void setSensitiveHeaders(Set sensitiveHeaders) { - this.sensitiveHeaders = sensitiveHeaders; - } - - public String getJwtSigningKey() { - return jwtSigningKey; - } - - public void setJwtSigningKey(String jwtSigningKey) { - this.jwtSigningKey = jwtSigningKey; - } - - public boolean isArtifactsLocalEnabled() { - return artifactsLocalEnabled; - } - - public void setArtifactsLocalEnabled(boolean artifactsLocalEnabled) { - this.artifactsLocalEnabled = artifactsLocalEnabled; - } - - public boolean isArtifactsNexusEnabled() { - return artifactsNexusEnabled; - } - - public void setArtifactsNexusEnabled(boolean artifactsNexusEnabled) { - this.artifactsNexusEnabled = artifactsNexusEnabled; - } - - @ConfigurationProperties(prefix = "plugins") - public class CmdbDataProperties { - private Integer enumCategoryTypeSystem = 1; - private Integer ciTypeIdOfSystemDesign = 37; - private Integer ciTypeIdOfUnitDesign = 39; - private Integer ciTypeIdOfPackage = 45; - private String enumCategoryNameOfDiffConf = "diff_conf"; - private String propertyNameOfFixedDate = "fixed_date"; - private String enumCodeChangeOfCiStateOfCreate = "update"; - private String enumCodeDestroyedOfCiStateOfCreate = "delete"; - - public Integer getEnumCategoryTypeSystem() { - return enumCategoryTypeSystem; - } - - public void setEnumCategoryTypeSystem(Integer enumCategoryTypeSystem) { - this.enumCategoryTypeSystem = enumCategoryTypeSystem; - } - - public Integer getCiTypeIdOfSystemDesign() { - return ciTypeIdOfSystemDesign; - } - - public void setCiTypeIdOfSystemDesign(Integer ciTypeIdOfSystemDesign) { - this.ciTypeIdOfSystemDesign = ciTypeIdOfSystemDesign; - } - - public Integer getCiTypeIdOfUnitDesign() { - return ciTypeIdOfUnitDesign; - } - - public void setCiTypeIdOfUnitDesign(Integer ciTypeIdOfUnitDesign) { - this.ciTypeIdOfUnitDesign = ciTypeIdOfUnitDesign; - } - - public Integer getCiTypeIdOfPackage() { - return ciTypeIdOfPackage; - } - - public void setCiTypeIdOfPackage(Integer ciTypeIdOfPackage) { - this.ciTypeIdOfPackage = ciTypeIdOfPackage; - } - - public String getEnumCategoryNameOfDiffConf() { - return enumCategoryNameOfDiffConf; - } - - public void setEnumCategoryNameOfDiffConf(String enumCategoryNameOfDiffConf) { - this.enumCategoryNameOfDiffConf = enumCategoryNameOfDiffConf; - } - - public String getPropertyNameOfFixedDate() { - return propertyNameOfFixedDate; - } - - public void setPropertyNameOfFixedDate(String propertyNameOfFixedDate) { - this.propertyNameOfFixedDate = propertyNameOfFixedDate; - } - - public String getEnumCodeChangeOfCiStateOfCreate() { - return enumCodeChangeOfCiStateOfCreate; - } - - public void setEnumCodeChangeOfCiStateOfCreate(String enumCodeChangeOfCiStateOfCreate) { - this.enumCodeChangeOfCiStateOfCreate = enumCodeChangeOfCiStateOfCreate; - } - - public String getEnumCodeDestroyedOfCiStateOfCreate() { - return enumCodeDestroyedOfCiStateOfCreate; - } - - public void setEnumCodeDestroyedOfCiStateOfCreate(String enumCodeDestroyedOfCiStateOfCreate) { - this.enumCodeDestroyedOfCiStateOfCreate = enumCodeDestroyedOfCiStateOfCreate; - } - - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/HttpClientProperties.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/HttpClientProperties.java deleted file mode 100644 index 858528c..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/HttpClientProperties.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.webank.plugins.artifacts.commons; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties(prefix = "plugins.httpclient") -public class HttpClientProperties { - private int connectTimeout = 30000; - private int requestTimeout = 30000; - private int socketTimeout = 1200000; - private int maxTotalConnections = 50; - private int poolSizeOfScheduler = 50; - private int defaultKeepAliveTimeMillis = 1200000; - private int closeIdleConnectionWaitTimeSecs = 30; - - public int getConnectTimeout() { - return connectTimeout; - } - - public void setConnectTimeout(int connectTimeout) { - this.connectTimeout = connectTimeout; - } - - public int getRequestTimeout() { - return requestTimeout; - } - - public void setRequestTimeout(int requestTimeout) { - this.requestTimeout = requestTimeout; - } - - public int getSocketTimeout() { - return socketTimeout; - } - - public void setSocketTimeout(int socketTimeout) { - this.socketTimeout = socketTimeout; - } - - public int getMaxTotalConnections() { - return maxTotalConnections; - } - - public void setMaxTotalConnections(int maxTotalConnections) { - this.maxTotalConnections = maxTotalConnections; - } - - public int getPoolSizeOfScheduler() { - return poolSizeOfScheduler; - } - - public void setPoolSizeOfScheduler(int poolSizeOfScheduler) { - this.poolSizeOfScheduler = poolSizeOfScheduler; - } - - public int getDefaultKeepAliveTimeMillis() { - return defaultKeepAliveTimeMillis; - } - - public void setDefaultKeepAliveTimeMillis(int defaultKeepAliveTimeMillis) { - this.defaultKeepAliveTimeMillis = defaultKeepAliveTimeMillis; - } - - public int getCloseIdleConnectionWaitTimeSecs() { - return closeIdleConnectionWaitTimeSecs; - } - - public void setCloseIdleConnectionWaitTimeSecs(int closeIdleConnectionWaitTimeSecs) { - this.closeIdleConnectionWaitTimeSecs = closeIdleConnectionWaitTimeSecs; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/PluginException.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/PluginException.java deleted file mode 100644 index cf896d6..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/commons/PluginException.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.webank.plugins.artifacts.commons; - -public class PluginException extends RuntimeException { - private static final long serialVersionUID = 1L; - - private String errorCode; - private String messageKey; - private boolean applyMessage = false; - private Object[] args; - - public PluginException(String message) { - super(message); - this.applyMessage = true; - } - - public PluginException(String message, Throwable cause) { - super(message, cause); - this.applyMessage = true; - } - - public PluginException(String errorCode, String message, Object... objects) { - this(errorCode, null, message, null, false, objects); - } - - private PluginException(String errorCode, String messageKey, String message, Throwable cause, - boolean applyMessage, Object... objects) { - super(message, cause); - this.errorCode = errorCode; - this.messageKey = messageKey; - if (objects != null && (this.args == null)) { - this.args = new Object[objects.length]; - int index = 0; - for (Object object : objects) { - this.args[index] = object; - index++; - } - } - this.applyMessage = applyMessage; - } - - public PluginException withErrorCode(String errorCode) { - this.errorCode = errorCode; - return this; - } - - public PluginException withErrorCode(String errorCode, Object... objects) { - this.errorCode = errorCode; - if (objects != null && (this.args == null)) { - this.args = new Object[objects.length]; - int index = 0; - for (Object object : objects) { - this.args[index] = object; - index++; - } - } - return this; - } - - public PluginException withErrorCodeAndArgs(String errorCode, Object[] objects) { - this.errorCode = errorCode; - if (objects != null && (this.args == null)) { - this.args = new Object[objects.length]; - int index = 0; - for (Object object : objects) { - this.args[index] = object; - index++; - } - } - return this; - } - - public PluginException withMessageKey(String msgKey) { - this.messageKey = msgKey; - return this; - } - - public PluginException withMessageKey(String msgKey, Object... objects) { - this.messageKey = msgKey; - if (objects != null && (this.args == null)) { - this.args = new Object[objects.length]; - int index = 0; - for (Object object : objects) { - this.args[index] = object; - index++; - } - } - - return this; - } - - public String getErrorCode() { - return errorCode; - } - - public String getMessageKey() { - return messageKey; - } - - public boolean isApplyMessage() { - return applyMessage; - } - - public Object[] getArgs() { - return args; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/HttpClientConfig.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/HttpClientConfig.java deleted file mode 100644 index 0168cf3..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/HttpClientConfig.java +++ /dev/null @@ -1,212 +0,0 @@ -package com.webank.plugins.artifacts.config; - -import java.io.IOException; -import java.security.KeyManagementException; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.util.Collections; -import java.util.concurrent.TimeUnit; - -import org.apache.http.HeaderElement; -import org.apache.http.HeaderElementIterator; -import org.apache.http.HttpResponse; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.config.Registry; -import org.apache.http.config.RegistryBuilder; -import org.apache.http.conn.ConnectionKeepAliveStrategy; -import org.apache.http.conn.socket.ConnectionSocketFactory; -import org.apache.http.conn.socket.PlainConnectionSocketFactory; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.conn.ssl.TrustSelfSignedStrategy; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.apache.http.message.BasicHeaderElementIterator; -import org.apache.http.protocol.HTTP; -import org.apache.http.protocol.HttpContext; -import org.apache.http.ssl.SSLContextBuilder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.web.client.RestTemplateBuilder; -import org.springframework.boot.web.client.RestTemplateCustomizer; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.http.HttpRequest; -import org.springframework.http.client.ClientHttpRequestExecution; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.scheduling.TaskScheduler; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; -import org.springframework.web.client.RestTemplate; - -import com.webank.plugins.artifacts.commons.HttpClientProperties; -import com.webank.plugins.artifacts.interceptor.RestTemplateInterceptor; - -@Configuration -@EnableScheduling -public class HttpClientConfig { - - private static final Logger log = LoggerFactory.getLogger(HttpClientConfig.class); - - @Autowired - private HttpClientProperties httpClientProperties; - - @Autowired - private RestTemplateInterceptor restTemplateInterceptor; - - @Bean - public PoolingHttpClientConnectionManager poolingConnectionManager() { - SSLContextBuilder builder = new SSLContextBuilder(); - try { - builder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); - } catch (NoSuchAlgorithmException | KeyStoreException e) { - log.error("Pooling Connection Manager Initialisation failure because of " + e.getMessage(), e); - } - - SSLConnectionSocketFactory sslsf = null; - try { - sslsf = new SSLConnectionSocketFactory(builder.build()); - } catch (KeyManagementException | NoSuchAlgorithmException e) { - log.error("Pooling Connection Manager Initialisation failure because of " + e.getMessage(), e); - } - - Registry socketFactoryRegistry = RegistryBuilder - .create() - .register("https", sslsf) - .register("http", new PlainConnectionSocketFactory()) - .build(); - - PoolingHttpClientConnectionManager poolingConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); - poolingConnectionManager.setMaxTotal(httpClientProperties.getMaxTotalConnections()); - return poolingConnectionManager; - } - - @Bean - public ConnectionKeepAliveStrategy connectionKeepAliveStrategy() { - return new ConnectionKeepAliveStrategy() { - @Override - public long getKeepAliveDuration(HttpResponse response, HttpContext context) { - HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); - while (it.hasNext()) { - HeaderElement he = it.nextElement(); - String param = he.getName(); - String value = he.getValue(); - - if (value != null && param.equalsIgnoreCase("timeout")) { - return Long.parseLong(value) * 1000; - } - } - return httpClientProperties.getDefaultKeepAliveTimeMillis(); - } - }; - } - - @Bean - public CloseableHttpClient httpClient() { - RequestConfig requestConfig = RequestConfig.custom() - .setConnectionRequestTimeout(httpClientProperties.getRequestTimeout()) - .setConnectTimeout(httpClientProperties.getConnectTimeout()) - .setSocketTimeout(httpClientProperties.getSocketTimeout()) - .build(); - - return HttpClients.custom() - .setDefaultRequestConfig(requestConfig) - .setConnectionManager(poolingConnectionManager()) - .setKeepAliveStrategy(connectionKeepAliveStrategy()) - .build(); - } - - @Bean - public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) { - return new Runnable() { - @Override - @Scheduled(fixedDelay = 10000) - public void run() { - try { - if (connectionManager != null) { - log.trace("run IdleConnectionMonitor - Closing expired and idle connections..."); - connectionManager.closeExpiredConnections(); - connectionManager.closeIdleConnections(httpClientProperties.getCloseIdleConnectionWaitTimeSecs(), TimeUnit.SECONDS); - } else { - log.trace("run IdleConnectionMonitor - Http Client Connection manager is not initialised"); - } - } catch (Exception e) { - log.error("run IdleConnectionMonitor - Exception occurred. msg={}, e={}", e.getMessage(), e); - } - } - }; - } - - @Bean - public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() { - HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(); - clientHttpRequestFactory.setHttpClient(httpClient()); - return clientHttpRequestFactory; - } - - @Bean - public TaskScheduler taskScheduler() { - ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); - scheduler.setThreadNamePrefix("poolScheduler"); - scheduler.setPoolSize(httpClientProperties.getPoolSizeOfScheduler()); - return scheduler; - } - - @Bean("userRestTemplate") - @Primary - public RestTemplate userRestTemplate() { - RestTemplate template = restTemplateBuilder().build(); - template.setInterceptors(Collections.singletonList(restTemplateInterceptor)); - return template; - } - - @Bean("simpleRestTemplate") - public RestTemplate simpleRestTemplate() { - RestTemplate template = restTemplateBuilder().build(); - return template; - } - - @Bean - public RestTemplateBuilder restTemplateBuilder() { - return new RestTemplateBuilder(customRestTemplateCustomizer()); - } - - @Bean - public CustomRestTemplateCustomizer customRestTemplateCustomizer() { - return new CustomRestTemplateCustomizer(); - } - - private class CustomRestTemplateCustomizer implements RestTemplateCustomizer { - @Override - public void customize(RestTemplate restTemplate) { - restTemplate.setRequestFactory(clientHttpRequestFactory()); - restTemplate.getInterceptors().add(new CustomClientHttpRequestInterceptor()); - } - } - - private class CustomClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { - - @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { - logRequestDetails(request); - ClientHttpResponse response = execution.execute(request, body); - logResponseDetails(response); - return response; - } - - private void logRequestDetails(HttpRequest request) { - log.debug("Request Headers: {}", request.getHeaders()); - log.debug("Request Method: {}", request.getMethod()); - log.debug("Request URI: {}", request.getURI()); - } - - private void logResponseDetails(ClientHttpResponse response) { - log.debug("Response Headers: {}", response.getHeaders()); - } - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/SpringAppConfig.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/SpringAppConfig.java deleted file mode 100644 index 3dc6fc2..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/SpringAppConfig.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.webank.plugins.artifacts.config; - -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.context.annotation.Configuration; - -import com.webank.plugins.artifacts.commons.ApplicationProperties.CmdbDataProperties; -import com.webank.plugins.artifacts.commons.HttpClientProperties; - -@Configuration -@EnableConfigurationProperties({ CmdbDataProperties.class, HttpClientProperties.class}) -public class SpringAppConfig { - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/SpringWebConfig.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/SpringWebConfig.java deleted file mode 100644 index f11cc8d..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/config/SpringWebConfig.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.webank.plugins.artifacts.config; - -import javax.servlet.Filter; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties; -import com.webank.plugins.artifacts.auth.filter.Http401AuthenticationEntryPoint; -import com.webank.plugins.artifacts.auth.filter.JwtClientConfig; -import com.webank.plugins.artifacts.auth.filter.JwtSsoBasedAuthenticationFilter; -import com.webank.plugins.artifacts.commons.ApplicationProperties; -import com.webank.plugins.artifacts.interceptor.ApiAccessInterceptor; - -@Configuration -@EnableWebMvc -@EnableWebSecurity -@EnableGlobalMethodSecurity(jsr250Enabled = true, prePostEnabled = true, securedEnabled = true) -@EnableEncryptableProperties -@ComponentScan({ "com.webank.plugins.artifacts.controller" }) -public class SpringWebConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer { - - @Autowired - private ApiAccessInterceptor apiAccessInterceptor; - - @Autowired - private ApplicationProperties applicationProperties; - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(apiAccessInterceptor).addPathPatterns("/**"); - WebMvcConfigurer.super.addInterceptors(registry); - } - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); - registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); - } - - @Override - protected void configure(HttpSecurity http) throws Exception { - configureLocalAuthentication(http); - - } - - protected void configureLocalAuthentication(HttpSecurity http) throws Exception { - http.authorizeRequests() // - .antMatchers("/swagger-ui.html/**", "/swagger-resources/**").permitAll()// - .antMatchers("/webjars/**").permitAll() // - .antMatchers("/v2/api-docs").permitAll() // - .antMatchers("/csrf").permitAll() // - .anyRequest().authenticated() // - .and()// - .addFilter(jwtSsoBasedAuthenticationFilter())// - .csrf()// - .disable() // - .exceptionHandling() // - .authenticationEntryPoint(new Http401AuthenticationEntryPoint()); // - } - - protected Filter jwtSsoBasedAuthenticationFilter() throws Exception { - JwtClientConfig jwtClientConfig = new JwtClientConfig(); - jwtClientConfig.setSigningKey(applicationProperties.getJwtSigningKey()); - JwtSsoBasedAuthenticationFilter f = new JwtSsoBasedAuthenticationFilter(authenticationManager(), - jwtClientConfig); - return (Filter) f; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/constant/ArtifactsConstants.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/constant/ArtifactsConstants.java deleted file mode 100644 index 65cfc19..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/constant/ArtifactsConstants.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.webank.plugins.artifacts.constant; - -public class ArtifactsConstants { - public static final String UPLOAD_NAME = "upload_name"; - - public static final String KEY_OF_TOKEN = "Authorization"; - - public static String PREFIX_BEARER_TOKEN = "Bearer "; - public static final String SIGNING_KEY = "Platform+Auth+Server+Secret"; -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/ArtifactManagementController.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/ArtifactManagementController.java deleted file mode 100644 index 54f3af5..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/ArtifactManagementController.java +++ /dev/null @@ -1,218 +0,0 @@ -package com.webank.plugins.artifacts.controller; - -import static com.google.common.collect.Lists.newArrayList; -import static com.webank.plugins.artifacts.dto.JsonResponse.okay; -import static com.webank.plugins.artifacts.dto.JsonResponse.okayWithData; -import static com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery.defaultQueryObject; -import static com.webank.plugins.artifacts.utils.BooleanUtils.isTrue; -import static org.apache.commons.lang3.StringUtils.isNotEmpty; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.util.List; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.multipart.MultipartFile; - -import com.webank.plugins.artifacts.commons.ApplicationProperties; -import com.webank.plugins.artifacts.commons.ApplicationProperties.CmdbDataProperties; -import com.webank.plugins.artifacts.commons.PluginException; -import com.webank.plugins.artifacts.constant.ArtifactsConstants; -import com.webank.plugins.artifacts.dto.AutoCreateDeployPackageDto; -import com.webank.plugins.artifacts.dto.AutoCreateDeployPackageResultDto; -import com.webank.plugins.artifacts.dto.JsonResponse; -import com.webank.plugins.artifacts.service.ArtifactService; -import com.webank.plugins.artifacts.service.NexusArtifactManagementService; -import com.webank.plugins.artifacts.support.cmdb.CmdbServiceV2Stub; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CatCodeDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.OperateCiDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery; - -@RestController -public class ArtifactManagementController { - - private static final Logger logger = LoggerFactory.getLogger(ArtifactManagementController.class); - @Autowired - private CmdbDataProperties cmdbDataProperties; - - @Autowired - private CmdbServiceV2Stub cmdbServiceV2Stub; - - @Autowired - private ArtifactService artifactService; - - @Autowired - private NexusArtifactManagementService nexusArtifactManagementService; - - @Autowired - private ApplicationProperties applicationProperties; - - @GetMapping("/system-design-versions") - public JsonResponse getSystemDesignVersions() { - return okayWithData(artifactService.getSystemDesignVersions()); - } - - @GetMapping("/system-design-versions/{system-design-id}") - public JsonResponse getSystemDesignVersion(@PathVariable(value = "system-design-id") String systemDesignId) { - return okayWithData(artifactService.getArtifactSystemDesignTree(systemDesignId)); - - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/upload") - public JsonResponse uploadPackage(@PathVariable(value = "unit-design-id") String unitDesignId, - @RequestParam(value = "file", required = false) MultipartFile multipartFile, HttpServletRequest request) { - if(!applicationProperties.isArtifactsLocalEnabled()) { - throw new PluginException("Package uploading is disabled!").withErrorCode("3010"); - } - File file = convertMultiPartToFile(multipartFile); - String url = artifactService.uploadPackageToS3(file); - return okayWithData(artifactService.savePackageToCmdb(file, unitDesignId, - (String) request.getAttribute(ArtifactsConstants.UPLOAD_NAME), url, null)); - - } - - @PostMapping("/packages/auto-create-deploy-package") - public JsonResponse autoCreateDeployPackage(@RequestBody AutoCreateDeployPackageDto autoCreateDeployPackageDto, HttpServletRequest request) { - String uploadName = (String) request.getAttribute(ArtifactsConstants.UPLOAD_NAME); - AutoCreateDeployPackageResultDto result = nexusArtifactManagementService.autoCreateDeployPackage(autoCreateDeployPackageDto, uploadName); - return okayWithData(result); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/queryNexusDirectiry") - public JsonResponse queryNexusPackages(@PathVariable(value = "unit-design-id") String unitDesignId, - @RequestBody PaginationQuery queryObject) { - if(!applicationProperties.isArtifactsNexusEnabled()) { - throw new PluginException("Package uploading is disabled!").withErrorCode("3010"); - } - return okayWithData(nexusArtifactManagementService.queryNexusDirectory(unitDesignId, queryObject)); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/uploadNexusPackage") - public JsonResponse uploadNexusPackage(@PathVariable(value = "unit-design-id") String unitDesignId, - @RequestParam(value = "downloadUrl", required = false) String downloadUrl, HttpServletRequest request) { - if(!applicationProperties.isArtifactsNexusEnabled()) { - throw new PluginException("Package uploading is disabled!").withErrorCode("3010"); - } - nexusArtifactManagementService.asyncUploadNexusPackageToS3(unitDesignId, downloadUrl, - (String) request.getAttribute(ArtifactsConstants.UPLOAD_NAME)); - return okay(); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/{package-id}/deactive") - public JsonResponse deactivePackage(@PathVariable(value = "package-id") String packageId) { - artifactService.deactive(packageId); - return okay(); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/{package-id}/active") - public JsonResponse activePackage(@PathVariable(value = "package-id") String packageId) { - artifactService.active(packageId); - return okay(); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/{package-id}/property-keys/query") - public JsonResponse getKeys(@PathVariable(value = "package-id") String packageId, - @RequestBody Map additionalProperties) { - if (additionalProperties.get("filePath") == null) { - throw new PluginException("3001", "Field 'filePath' is required."); - } - return okayWithData(artifactService.getPropertyKeys(packageId, additionalProperties.get("filePath"))); - } - - @PostMapping("/enum/codes/diff-config/save") - public JsonResponse saveDiffConfigEnumCodes(@RequestBody CatCodeDto code) { - artifactService.saveDiffConfigEnumCodes(code); - return okay(); - } - - @GetMapping("/enum/codes/diff-config/query") - public JsonResponse getDiffConfigEnumCodes() { - return okayWithData(artifactService.getDiffConfigEnumCodes()); - } - - @GetMapping("/getPackageCiTypeId") - public JsonResponse getPackageCiTypeId() { - return okayWithData(cmdbDataProperties.getCiTypeIdOfPackage()); - } - - @PostMapping("/ci/state/operate") - public JsonResponse operateCiForState(@RequestBody List ciIds, - @RequestParam("operation") String operation) { - return okayWithData(artifactService.operateState(ciIds, operation)); - } - - @GetMapping("/ci-types") - public JsonResponse getCiTypes(@RequestParam(name = "group-by", required = false) String groupBy, - @RequestParam(name = "with-attributes", required = false) String withAttributes, - @RequestParam(name = "status", required = false) String status) { - return okayWithData(artifactService.getCiTypes(isTrue(withAttributes), status)); - } - - @PostMapping("/ci-types/{ci-type-id}/ci-data/batch-delete") - public JsonResponse deleteCiData(@PathVariable(value = "ci-type-id") int ciTypeId, - @RequestBody List ciDataIds) { - artifactService.deleteCiData(ciTypeId, ciDataIds); - return okay(); - } - - @PostMapping("/enum/system/codes") - public JsonResponse querySystemEnumCodesWithRefResources(@RequestBody PaginationQuery queryObject) { - return okayWithData(artifactService.querySystemEnumCodesWithRefResources(queryObject)); - } - - @GetMapping("/ci-types/{ci-type-id}/references/by") - public JsonResponse getCiTypeReferenceBy(@PathVariable(value = "ci-type-id") int ciTypeId) { - return okayWithData(artifactService.getCiTypeReferenceBy(ciTypeId)); - } - - @GetMapping("/ci-types/{ci-type-id}/attributes") - public JsonResponse getCiTypeAttributes(@PathVariable(value = "ci-type-id") int ciTypeId, - @RequestParam(name = "accept-input-types", required = false) String acceptInputTypes) { - if (isNotEmpty(acceptInputTypes)) { - return okayWithData(cmdbServiceV2Stub.queryCiTypeAttributes(defaultQueryObject("ciTypeId", ciTypeId) - .addInFilter("inputType", newArrayList(acceptInputTypes.split(","))))); - } else { - return okayWithData(cmdbServiceV2Stub.getCiTypeAttributesByCiTypeId(ciTypeId)); - } - } - - @GetMapping("/static-data/special-connector") - public JsonResponse getSpecialConnector() { - return okayWithData(artifactService.getSpecialConnector()); - } - - private File convertMultiPartToFile(MultipartFile multipartFile) { - if (multipartFile == null) { - return null; - } - - File file = new File(multipartFile.getOriginalFilename()); - try (FileOutputStream fos = new FileOutputStream(file)) { - byte[] buf = new byte[1024]; - int len = 0; - - InputStream is = multipartFile.getInputStream(); - while ((len = is.read(buf)) != -1) { - fos.write(buf, 0, len); - } - - // fos.write(multipartFile.getBytes()); - } catch (Exception e) { - logger.error("errors while convert multipart file.", e); - throw new PluginException("3002", "Failed to convert multipart file to file."); - } - return file; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/PackageConfigManagementController.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/PackageConfigManagementController.java deleted file mode 100644 index 0b25a82..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/PackageConfigManagementController.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.webank.plugins.artifacts.controller; - -import static com.webank.plugins.artifacts.dto.JsonResponse.okay; -import static com.webank.plugins.artifacts.dto.JsonResponse.okayWithData; - -import java.util.List; -import java.util.Map; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -import com.webank.plugins.artifacts.dto.DiffConfigurationUpdateDto; -import com.webank.plugins.artifacts.dto.FileQueryRequestDto; -import com.webank.plugins.artifacts.dto.FileQueryResultItemDto; -import com.webank.plugins.artifacts.dto.JsonResponse; -import com.webank.plugins.artifacts.dto.PackageComparisionRequestDto; -import com.webank.plugins.artifacts.dto.PackageComparisionResultDto; -import com.webank.plugins.artifacts.dto.PackageConfigFilesUpdateRequestDto; -import com.webank.plugins.artifacts.dto.SinglePackageQueryResultDto; -import com.webank.plugins.artifacts.service.ConfigFileManagementService; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQueryResult; - -@RestController -public class PackageConfigManagementController { - - @Autowired - private ConfigFileManagementService configFileManagementService; - - @PostMapping("/artifacts/packages/wecmdb/entities/diff_configuration/update") - public JsonResponse updateDiffConfiguration(List updateRequestDto) { - configFileManagementService.updateDiffConfigurations(updateRequestDto); - return okay(); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/{package-id}/files/query") - public JsonResponse queryPackageFiles(@PathVariable(value = "package-id") String packageId, - @RequestBody FileQueryRequestDto fileQueryRequestDto) { - List results = configFileManagementService.queryDeployConfigFiles(packageId, - fileQueryRequestDto); - return okayWithData(results); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/query") - public JsonResponse queryPackages(@PathVariable(value = "unit-design-id") String unitDesignId, - @RequestBody PaginationQuery queryObject) { - PaginationQueryResult> results = configFileManagementService - .queryDeployPackages(unitDesignId, queryObject); - return okayWithData(results); - } - - @GetMapping("/unit-designs/{unit-design-id}/packages/{package-id}/query") - public JsonResponse querySinglePackage(@PathVariable(value = "unit-design-id") String unitDesignId, - @PathVariable(value = "package-id") String packageId) { - SinglePackageQueryResultDto result = configFileManagementService.querySinglePackage(unitDesignId, packageId); - return okayWithData(result); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/{package-id}/comparison") - public JsonResponse packageComparision(@PathVariable(value = "unit-design-id") String unitDesignId, - @PathVariable(value = "package-id") String packageId, - @RequestBody PackageComparisionRequestDto comparisonReqDto) { - PackageComparisionResultDto result = configFileManagementService.packageComparision(unitDesignId, packageId, - comparisonReqDto); - return okayWithData(result); - } - - @PostMapping("/unit-designs/{unit-design-id}/packages/{package-id}/update") - public JsonResponse updateConfigFilesOfPackage(@PathVariable(value = "unit-design-id") String unitDesignId, - @PathVariable(value = "package-id") String packageId, @RequestBody PackageConfigFilesUpdateRequestDto packageDomain) { - SinglePackageQueryResultDto result = configFileManagementService.updateConfigFilesOfPackage(unitDesignId, packageId, - packageDomain); - return okayWithData(result); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/WecubePlatformApiProxyController.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/WecubePlatformApiProxyController.java deleted file mode 100644 index 5557be0..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/controller/WecubePlatformApiProxyController.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.webank.plugins.artifacts.controller; - -import static org.springframework.web.bind.annotation.RequestMethod.DELETE; -import static org.springframework.web.bind.annotation.RequestMethod.GET; -import static org.springframework.web.bind.annotation.RequestMethod.HEAD; -import static org.springframework.web.bind.annotation.RequestMethod.OPTIONS; -import static org.springframework.web.bind.annotation.RequestMethod.PATCH; -import static org.springframework.web.bind.annotation.RequestMethod.POST; -import static org.springframework.web.bind.annotation.RequestMethod.PUT; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.client.RestTemplate; - -import com.webank.plugins.artifacts.commons.ApplicationProperties; -import com.webank.plugins.artifacts.service.helper.ProxyExchange; - -@RestController -public class WecubePlatformApiProxyController { - - private static final Logger log = LoggerFactory.getLogger(WecubePlatformApiProxyController.class); - - private static final String API_PROXY_PATH = "/platform"; - - @Autowired - private RestTemplate restTemplate; - - @Autowired - private ApplicationProperties applicationProperties; - - @RequestMapping(value = API_PROXY_PATH + "/**", method = { GET, DELETE, OPTIONS, HEAD }) - public void pluginApiProxy(HttpServletRequest request, HttpServletResponse response) { - proxy(createProxyExchange(request, response), request); - } - - @RequestMapping(value = API_PROXY_PATH + "/**", method = { POST, PUT, PATCH }) - public void pluginApiProxy(HttpServletRequest request, HttpServletResponse response, @RequestBody Object body) { - proxy(createProxyExchange(request, response).body(body), request); - } - - private ProxyExchange createProxyExchange(HttpServletRequest request, HttpServletResponse response) { - ProxyExchange proxyExchange = new ProxyExchange(restTemplate, request, response); - proxyExchange.customHttpHeaders(applicationProperties.getCustomHeaders()); - proxyExchange.sensitiveHeaders(applicationProperties.getSensitiveHeaders()); - return proxyExchange; - } - - private void proxy(ProxyExchange proxyExchange, HttpServletRequest request) { - log.info("http {} request comes: {}", request.getMethod(), proxyExchange.path()); - routing(proxyExchange, request); - - proxyExchange.exchange(); - } - - private void routing(ProxyExchange proxyExchange, HttpServletRequest request) { - String targetUri = deriveTargetUrl(request.getScheme(), proxyExchange.path(API_PROXY_PATH)); - - log.info("routing to : " + targetUri); - - proxyExchange.targetUri(targetUri); - } - - private String deriveTargetUrl(String scheme, String path) { - return applicationProperties.getWecubeGatewayServerUrl() + path; - } - -} \ No newline at end of file diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/AutoCreateDeployPackageDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/AutoCreateDeployPackageDto.java deleted file mode 100644 index da153e4..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/AutoCreateDeployPackageDto.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -public class AutoCreateDeployPackageDto { - - private String nexusUrl; - private String baselinePackage; - private String startFilePath; - private String stopFilePath; - private String deployFilePath; - private String diffConfFile; - - public String getNexusUrl() { - return nexusUrl; - } - public void setNexusUrl(String nexusUrl) { - this.nexusUrl = nexusUrl; - } - public String getBaselinePackage() { - return baselinePackage; - } - public void setBaselinePackage(String baselinePackage) { - this.baselinePackage = baselinePackage; - } - public String getStartFilePath() { - return startFilePath; - } - public void setStartFilePath(String startFilePath) { - this.startFilePath = startFilePath; - } - public String getStopFilePath() { - return stopFilePath; - } - public void setStopFilePath(String stopFilePath) { - this.stopFilePath = stopFilePath; - } - public String getDeployFilePath() { - return deployFilePath; - } - public void setDeployFilePath(String deployFilePath) { - this.deployFilePath = deployFilePath; - } - public String getDiffConfFile() { - return diffConfFile; - } - public void setDiffConfFile(String diffConfFile) { - this.diffConfFile = diffConfFile; - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/AutoCreateDeployPackageResultDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/AutoCreateDeployPackageResultDto.java deleted file mode 100644 index 6dde976..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/AutoCreateDeployPackageResultDto.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -public class AutoCreateDeployPackageResultDto { - - private String guid; - - public String getGuid() { - return guid; - } - - public void setGuid(String guid) { - this.guid = guid; - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/ConfigFileDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/ConfigFileDto.java deleted file mode 100644 index a042aec..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/ConfigFileDto.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -import java.util.ArrayList; -import java.util.List; - -public class ConfigFileDto { - private String filename; - private String comparisonResult; - private String md5; - private Boolean isDir; - - private List configKeyInfos = new ArrayList<>(); - - public String getFilename() { - return filename; - } - - public void setFilename(String filename) { - this.filename = filename; - } - - public String getComparisonResult() { - return comparisonResult; - } - - public void setComparisonResult(String comparisonResult) { - this.comparisonResult = comparisonResult; - } - - public List getConfigKeyInfos() { - return configKeyInfos; - } - - public void setConfigKeyInfos(List configKeyInfos) { - this.configKeyInfos = configKeyInfos; - } - - public void addConfigKeyInfo(ConfigKeyInfoDto dto) { - if (dto == null) { - return; - } - - if (this.configKeyInfos == null) { - this.configKeyInfos = new ArrayList(); - } - - this.configKeyInfos.add(dto); - } - - public String getMd5() { - return md5; - } - - public void setMd5(String md5) { - this.md5 = md5; - } - - public Boolean getIsDir() { - return isDir; - } - - public void setIsDir(Boolean isDir) { - this.isDir = isDir; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("ConfigFileDto [filename="); - builder.append(filename); - builder.append(", comparisonResult="); - builder.append(comparisonResult); - builder.append(", md5="); - builder.append(md5); - builder.append(", isDir="); - builder.append(isDir); - builder.append(", configKeyInfos="); - builder.append(configKeyInfos); - builder.append("]"); - return builder.toString(); - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/ConfigKeyInfoDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/ConfigKeyInfoDto.java deleted file mode 100644 index 66ecd72..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/ConfigKeyInfoDto.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -public class ConfigKeyInfoDto { - - public static final String BOUND_YES = "Y"; - public static final String BOUND_NO = "N"; - - private String line; - private String key; - private String type; - - public String getLine() { - return line; - } - - public void setLine(String line) { - this.line = line; - } - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("ConfigKeyInfoDto [line="); - builder.append(line); - builder.append(", key="); - builder.append(key); - builder.append(", type="); - builder.append(type); - builder.append("]"); - return builder.toString(); - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/DiffConfVariableInfoDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/DiffConfVariableInfoDto.java deleted file mode 100644 index fee2755..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/DiffConfVariableInfoDto.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -public class DiffConfVariableInfoDto { - private String key; - private String type; - private String diffConfigGuid; - private Boolean bound; - private String diffExpr; // JSON - private String fixedDate; - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getDiffConfigGuid() { - return diffConfigGuid; - } - - public void setDiffConfigGuid(String diffConfigGuid) { - this.diffConfigGuid = diffConfigGuid; - } - - public Boolean getBound() { - return bound; - } - - public void setBound(Boolean bound) { - this.bound = bound; - } - - public String getDiffExpr() { - return diffExpr; - } - - public void setDiffExpr(String diffExpr) { - this.diffExpr = diffExpr; - } - - public String getFixedDate() { - return fixedDate; - } - - public void setFixedDate(String fixedDate) { - this.fixedDate = fixedDate; - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/DiffConfigurationUpdateDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/DiffConfigurationUpdateDto.java deleted file mode 100644 index 85ffb25..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/DiffConfigurationUpdateDto.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; - -public class DiffConfigurationUpdateDto { - - private String id; - - @JsonProperty("variable_value") - private String diffExpr; - public String getId() { - return id; - } - public void setId(String id) { - this.id = id; - } - public String getDiffExpr() { - return diffExpr; - } - public void setDiffExpr(String diffExpr) { - this.diffExpr = diffExpr; - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/FileQueryRequestDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/FileQueryRequestDto.java deleted file mode 100644 index 9befa81..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/FileQueryRequestDto.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -import java.util.List; - -public class FileQueryRequestDto { - private String baselinePackage;//nullable - private List fileList; //empty means root - private Boolean expandAll = false; - - public String getBaselinePackage() { - return baselinePackage; - } - - public void setBaselinePackage(String baselinePackage) { - this.baselinePackage = baselinePackage; - } - - public List getFileList() { - return fileList; - } - - public void setFileList(List fileList) { - this.fileList = fileList; - } - - public Boolean getExpandAll() { - return expandAll; - } - - public void setExpandAll(Boolean expandAll) { - this.expandAll = expandAll; - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/FileQueryResultItemDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/FileQueryResultItemDto.java deleted file mode 100644 index 9274413..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/FileQueryResultItemDto.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -import java.util.ArrayList; -import java.util.List; - -public class FileQueryResultItemDto { - - private String name; - private String path; - private Boolean isDir; - private String comparisonResult; - private String md5; - private Boolean exists = true; - - private List children = new ArrayList<>(); - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Boolean getIsDir() { - return isDir; - } - - public void setIsDir(Boolean isDir) { - this.isDir = isDir; - } - - public String getComparisonResult() { - return comparisonResult; - } - - public void setComparisonResult(String comparisonResult) { - this.comparisonResult = comparisonResult; - } - - public List getChildren() { - return children; - } - - public void setChildren(List children) { - this.children = children; - } - - public void addFileQueryResultItem(FileQueryResultItemDto item) { - if (item == null) { - return; - } - - if (this.children == null) { - this.children = new ArrayList(); - } - - String itemPath = item.getPath(); - for(FileQueryResultItemDto child : this.children) { - if(child.getPath().equals(itemPath)) { - return; - } - } - - this.children.add(item); - } - - public String getMd5() { - return md5; - } - - public void setMd5(String md5) { - this.md5 = md5; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public Boolean getExists() { - return exists; - } - - public void setExists(Boolean exists) { - this.exists = exists; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/JsonResponse.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/JsonResponse.java deleted file mode 100644 index fbd3f17..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/JsonResponse.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -public class JsonResponse { - public final static String STATUS_OK = "OK"; - public final static String STATUS_ERROR = "ERROR"; - - private String status; - private String message; - private Object data; - - public JsonResponse withData(Object data) { - this.data = data; - return this; - } - - public static JsonResponse okay() { - JsonResponse result = new JsonResponse(); - result.setStatus(STATUS_OK); - result.setMessage("Success"); - return result; - } - - public static JsonResponse okayWithData(Object data) { - return okay().withData(data); - } - - public static JsonResponse error(String errorMessage) { - JsonResponse result = new JsonResponse(); - result.setStatus(STATUS_ERROR); - result.setMessage(errorMessage); - return result; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageComparisionRequestDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageComparisionRequestDto.java deleted file mode 100644 index 310efe6..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageComparisionRequestDto.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -public class PackageComparisionRequestDto { - - private String baselinePackage; - - public String getBaselinePackage() { - return baselinePackage; - } - - public void setBaselinePackage(String baselinePackage) { - this.baselinePackage = baselinePackage; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageComparisionResultDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageComparisionResultDto.java deleted file mode 100644 index 92967d9..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageComparisionResultDto.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonProperty; - -public class PackageComparisionResultDto { - @JsonProperty("start_file_path") - private List startFilePath = new ArrayList<>(); - @JsonProperty("stop_file_path") - private List stopFilePath = new ArrayList<>(); - @JsonProperty("deploy_file_path") - private List deployFilePath = new ArrayList<>(); - @JsonProperty("diff_conf_file") - private List diffConfFile = new ArrayList<>(); - - public List getStartFilePath() { - return startFilePath; - } - public void setStartFilePath(List startFilePath) { - this.startFilePath = startFilePath; - } - public List getStopFilePath() { - return stopFilePath; - } - public void setStopFilePath(List stopFilePath) { - this.stopFilePath = stopFilePath; - } - public List getDeployFilePath() { - return deployFilePath; - } - public void setDeployFilePath(List deployFilePath) { - this.deployFilePath = deployFilePath; - } - public List getDiffConfFile() { - return diffConfFile; - } - public void setDiffConfFile(List diffConfFile) { - this.diffConfFile = diffConfFile; - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageConfigFilesUpdateRequestDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageConfigFilesUpdateRequestDto.java deleted file mode 100644 index 465e691..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/PackageConfigFilesUpdateRequestDto.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonProperty; - -public class PackageConfigFilesUpdateRequestDto { - - @JsonProperty("start_file_path") - private List startFilePath = new ArrayList<>(); - @JsonProperty("stop_file_path") - private List stopFilePath = new ArrayList<>(); - @JsonProperty("deploy_file_path") - private List deployFilePath = new ArrayList<>(); - @JsonProperty("diff_conf_file") - private List diffConfFile = new ArrayList<>(); - - @JsonProperty("is_decompression") - private Boolean isDecompression; - - @JsonProperty("diff_conf_variable") - private List diffConfVariable; - - @JsonProperty("baseline_package") - private String baselinePackage; - - public List getStartFilePath() { - return startFilePath; - } - - public void setStartFilePath(List startFilePath) { - this.startFilePath = startFilePath; - } - - public List getStopFilePath() { - return stopFilePath; - } - - public void setStopFilePath(List stopFilePath) { - this.stopFilePath = stopFilePath; - } - - public List getDeployFilePath() { - return deployFilePath; - } - - public void setDeployFilePath(List deployFilePath) { - this.deployFilePath = deployFilePath; - } - - public List getDiffConfFile() { - return diffConfFile; - } - - public void setDiffConfFile(List diffConfFile) { - this.diffConfFile = diffConfFile; - } - - public Boolean getIsDecompression() { - return isDecompression; - } - - public void setIsDecompression(Boolean isDecompression) { - this.isDecompression = isDecompression; - } - - public List getDiffConfVariable() { - return diffConfVariable; - } - - public void setDiffConfVariable(List diffConfVariable) { - this.diffConfVariable = diffConfVariable; - } - - public String getBaselinePackage() { - return baselinePackage; - } - - public void setBaselinePackage(String baselinePackage) { - this.baselinePackage = baselinePackage; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/SinglePackageQueryResultDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/SinglePackageQueryResultDto.java deleted file mode 100644 index 6ca5fe7..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/dto/SinglePackageQueryResultDto.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.webank.plugins.artifacts.dto; - -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonProperty; - -public class SinglePackageQueryResultDto { - - private String packageId; - @JsonProperty("baseline_package") - private String baselinePackage; - - @JsonProperty("is_decompression") - private Boolean isDecompression; - - @JsonProperty("start_file_path") - private List startFilePath = new ArrayList<>(); - @JsonProperty("stop_file_path") - private List stopFilePath = new ArrayList<>(); - @JsonProperty("deploy_file_path") - private List deployFilePath = new ArrayList<>(); - @JsonProperty("diff_conf_file") - private List diffConfFile = new ArrayList<>(); - - @JsonProperty("diff_conf_variable") - private List diffConfVariable = new ArrayList<>(); - - public String getPackageId() { - return packageId; - } - - public void setPackageId(String packageId) { - this.packageId = packageId; - } - - public List getStartFilePath() { - return startFilePath; - } - - public void setStartFilePath(List startFilePath) { - this.startFilePath = startFilePath; - } - - public List getStopFilePath() { - return stopFilePath; - } - - public void setStopFilePath(List stopFilePath) { - this.stopFilePath = stopFilePath; - } - - public List getDeployFilePath() { - return deployFilePath; - } - - public void setDeployFilePath(List deployFilePath) { - this.deployFilePath = deployFilePath; - } - - public List getDiffConfFile() { - return diffConfFile; - } - - public void setDiffConfFile(List diffConfFile) { - this.diffConfFile = diffConfFile; - } - - public List getDiffConfVariable() { - return diffConfVariable; - } - - public void setDiffConfVariable(List diffConfVariable) { - this.diffConfVariable = diffConfVariable; - } - - public String getBaselinePackage() { - return baselinePackage; - } - - public void setBaselinePackage(String baselinePackage) { - this.baselinePackage = baselinePackage; - } - - public Boolean getIsDecompression() { - return isDecompression; - } - - public void setIsDecompression(Boolean isDecompression) { - this.isDecompression = isDecompression; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("SinglePackageQueryResultDto [packageId="); - builder.append(packageId); - builder.append(", baselinePackage="); - builder.append(baselinePackage); - builder.append(", isDecompression="); - builder.append(isDecompression); - builder.append(", startFilePath="); - builder.append(startFilePath); - builder.append(", stopFilePath="); - builder.append(stopFilePath); - builder.append(", deployFilePath="); - builder.append(deployFilePath); - builder.append(", diffConfFile="); - builder.append(diffConfFile); - builder.append(", diffConfVariable="); - builder.append(diffConfVariable); - builder.append("]"); - return builder.toString(); - } - - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/ApiAccessInterceptor.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/ApiAccessInterceptor.java deleted file mode 100644 index 49fd2db..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/ApiAccessInterceptor.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.webank.plugins.artifacts.interceptor; - -import java.security.Principal; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.security.core.Authentication; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.HandlerInterceptor; - -import com.webank.plugins.artifacts.constant.ArtifactsConstants; - -@Component -public class ApiAccessInterceptor implements HandlerInterceptor { - private static final Logger log = LoggerFactory.getLogger(ApiAccessInterceptor.class); - - @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { - - Principal userPrincipal = request.getUserPrincipal(); - if (userPrincipal != null && (userPrincipal instanceof Authentication)) { - Authentication auth = (Authentication) userPrincipal; - String authorization = (String) auth.getCredentials(); - AuthorizationStorage.getIntance().set(authorization); - request.setAttribute(ArtifactsConstants.UPLOAD_NAME, auth.getName()); - - } else { - log.info("remove token as there is none token presented."); - AuthorizationStorage.getIntance().remove(); - } - return true; - } - - @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) - throws Exception { - AuthorizationStorage.getIntance().remove(); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/AuthorizationStorage.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/AuthorizationStorage.java deleted file mode 100644 index d2183cf..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/AuthorizationStorage.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.webank.plugins.artifacts.interceptor; - -public class AuthorizationStorage extends ThreadLocal { - private static AuthorizationStorage instance = new AuthorizationStorage(); - - public static AuthorizationStorage getIntance() { - return instance; - } -} \ No newline at end of file diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/GlobalExceptionHandler.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/GlobalExceptionHandler.java deleted file mode 100644 index 0f4ed0f..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/GlobalExceptionHandler.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.webank.plugins.artifacts.interceptor; - -import static com.webank.plugins.artifacts.dto.JsonResponse.error; - -import java.util.Locale; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.MessageSource; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseBody; - -import com.webank.plugins.artifacts.commons.PluginException; -import com.webank.plugins.artifacts.dto.JsonResponse; - - -@ControllerAdvice -public class GlobalExceptionHandler { - private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); - - public static final String MSG_ERR_CODE_PREFIX = "artifacts.core.msg.errorcode."; - - public static final Locale DEF_LOCALE = Locale.ENGLISH; - - @Autowired - private MessageSource messageSource; - - - @ExceptionHandler(PluginException.class) - @ResponseBody - public JsonResponse handleWecubeException(HttpServletRequest request, final Exception e, - HttpServletResponse response) { - String errMsg = String.format("Processing failed cause by %s:%s", e.getClass().getSimpleName(), - e.getMessage() == null ? "" : e.getMessage()); - log.error(errMsg + "\n", e); - PluginException wecubeError = (PluginException) e; - - return JsonResponse.error(determineI18nErrorMessage(request, wecubeError)); - } - - @ExceptionHandler(value = Exception.class) - @ResponseBody - public JsonResponse defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { - log.error("errors occurred:", e); - log.error("GlobalExceptionHandler: RequestHost {} invokes url {} ERROR: {}", req.getRemoteHost(), req.getRequestURL(), e.getMessage()); - return error(e.getMessage()); - } - - private String determineI18nErrorMessage(HttpServletRequest request, PluginException e) { - Locale locale = request.getLocale(); - if (locale == null) { - locale = DEF_LOCALE; - } - if (StringUtils.isNoneBlank(e.getErrorCode())) { - String msgCode = MSG_ERR_CODE_PREFIX + e.getErrorCode(); - String msg = messageSource.getMessage(msgCode, e.getArgs(), locale); - return msg; - } else { - return e.getMessage(); - } - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/RestTemplateInterceptor.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/RestTemplateInterceptor.java deleted file mode 100644 index bfc5cef..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/interceptor/RestTemplateInterceptor.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.webank.plugins.artifacts.interceptor; - -import java.io.IOException; -import java.util.List; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpRequest; -import org.springframework.http.client.ClientHttpRequestExecution; -import org.springframework.http.client.ClientHttpRequestInterceptor; -import org.springframework.http.client.ClientHttpResponse; -import org.springframework.stereotype.Component; - -@Component -public class RestTemplateInterceptor implements ClientHttpRequestInterceptor { - private static final Logger log = LoggerFactory.getLogger(RestTemplateInterceptor.class); - - @Override - public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) - throws IOException { - HttpHeaders headers = request.getHeaders(); - - String authorization = AuthorizationStorage.getIntance().get(); - if(!StringUtils.isBlank(authorization)) { - headers.add("Authorization", authorization); - }else { - log.info("NONE authorization presents."); - } - return execution.execute(request, body); - } -} \ No newline at end of file diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/EncryptionException.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/EncryptionException.java deleted file mode 100644 index e552806..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/EncryptionException.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.webank.plugins.artifacts.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/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaBasedStringEncryptor.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaBasedStringEncryptor.java deleted file mode 100644 index 517a1c4..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaBasedStringEncryptor.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.webank.plugins.artifacts.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.artifacts.commons.ApplicationProperties; - -/** - * - * @author gavin - * - */ -@Component("jasyptStringEncryptor") -public class RsaBasedStringEncryptor implements StringEncryptor { - private static final Logger log = LoggerFactory.getLogger(RsaBasedStringEncryptor.class); - - @Autowired - private ApplicationProperties 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/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaEncryptablePropertyDetector.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaEncryptablePropertyDetector.java deleted file mode 100644 index b29792b..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaEncryptablePropertyDetector.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.webank.plugins.artifacts.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/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaEncryptor.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaEncryptor.java deleted file mode 100644 index 62bd25f..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaEncryptor.java +++ /dev/null @@ -1,209 +0,0 @@ -package com.webank.plugins.artifacts.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/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyDetector.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyDetector.java deleted file mode 100644 index c40522f..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyDetector.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.webank.plugins.artifacts.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) { - 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/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyPair.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyPair.java deleted file mode 100644 index db462f7..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyPair.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.webank.plugins.artifacts.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/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyPairBuilder.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyPairBuilder.java deleted file mode 100644 index 3fb916c..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/propenc/RsaKeyPairBuilder.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.webank.plugins.artifacts.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/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/AbstractArtifactService.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/AbstractArtifactService.java deleted file mode 100644 index 43e242e..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/AbstractArtifactService.java +++ /dev/null @@ -1,272 +0,0 @@ -package com.webank.plugins.artifacts.service; - -import java.io.File; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -import org.apache.commons.codec.digest.DigestUtils; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -import com.google.common.collect.ImmutableMap; -import com.webank.plugins.artifacts.commons.ApplicationProperties; -import com.webank.plugins.artifacts.commons.ApplicationProperties.CmdbDataProperties; -import com.webank.plugins.artifacts.dto.ConfigFileDto; -import com.webank.plugins.artifacts.commons.PluginException; -import com.webank.plugins.artifacts.interceptor.AuthorizationStorage; -import com.webank.plugins.artifacts.support.cmdb.CmdbServiceV2Stub; -import com.webank.plugins.artifacts.support.cmdb.StandardCmdbEntityRestClient; -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbDiffConfigDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiDataDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQueryResult; -import com.webank.plugins.artifacts.support.s3.S3Client; -import com.webank.plugins.artifacts.support.saltstack.SaltFileNodeDto; -import com.webank.plugins.artifacts.support.saltstack.SaltFileNodeResultItemDto; -import com.webank.plugins.artifacts.support.saltstack.SaltstackRequest.DefaultSaltstackRequest; -import com.webank.plugins.artifacts.support.saltstack.SaltstackResponse.ResultData; -import com.webank.plugins.artifacts.support.saltstack.SaltstackServiceStub; - -public abstract class AbstractArtifactService { - private static final Logger log = LoggerFactory.getLogger(AbstractArtifactService.class); - public static final String FILE_COMP_DELETED = "deleted"; - public static final String FILE_COMP_NEW = "new"; - public static final String FILE_COMP_CHANGED = "changed"; - public static final String FILE_COMP_SAME = "same"; - - protected static final String CONSTANT_FIX_DATE = "fixed_date"; - protected static final String S3_KEY_DELIMITER = "_"; - protected static final String CONSTANT_CAT_CAT_TYPE = "cat.catType"; - protected static final String CONSTANT_INPUT_TYPE = "inputType"; - protected static final String CONSTANT_CI_TYPE = "ciType"; - - - - @Autowired - protected CmdbServiceV2Stub cmdbServiceV2Stub; - - @Autowired - protected SaltstackServiceStub saltstackServiceStub; - - @Autowired - protected CmdbDataProperties cmdbDataProperties; - - @Autowired - protected ApplicationProperties applicationProperties; - - @Autowired - protected StandardCmdbEntityRestClient standardCmdbEntityRestClient; - - public String uploadPackageToS3(File file) { - if (file == null) { - throw new PluginException("Upload package file is required."); - } - - String s3Key = genMd5Value(file) + S3_KEY_DELIMITER + file.getName(); - String url = new S3Client(applicationProperties.getArtifactsS3ServerUrl(), - applicationProperties.getArtifactsS3AccessKey(), applicationProperties.getArtifactsS3SecretKey()) - .uploadFile(applicationProperties.getArtifactsS3BucketName(), s3Key, file); - return url.substring(0, url.indexOf("?")); - } - - public List savePackageToCmdb(File file, String unitDesignId, String uploadUser, String deployPackageUrl, - String authorization) { - Map pkg = ImmutableMap.builder().put("name", file.getName()) - .put("deploy_package_url", deployPackageUrl).put("md5_value", genMd5Value(file)) - .put("description", file.getName()).put("upload_user", uploadUser) - .put("upload_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())) - .put("unit_design", unitDesignId).build(); - - if (StringUtils.isNoneBlank(authorization)) { - AuthorizationStorage.getIntance().set(authorization); - } - return cmdbServiceV2Stub.createCiData(cmdbDataProperties.getCiTypeIdOfPackage(), pkg); - } - - protected void updatePackageCi(Map packageUpdateParamsMap){ - cmdbServiceV2Stub.updateCiData(cmdbDataProperties.getCiTypeIdOfPackage(), packageUpdateParamsMap); - } - - protected String genMd5Value(File file) { - if (file == null) { - return null; - } - - String md5Value = null; - - try { - md5Value = DigestUtils.md5Hex(FileUtils.readFileToByteArray(file)); - } catch (Exception e) { - String msg = String.format("Failed to generateMd5 value for file [%s].", file.getName()); - log.error(msg, e); - throw new PluginException("3004", msg, file.getName()); - } - return md5Value; - } - - @SuppressWarnings("rawtypes") - protected String retrieveS3EndpointWithKeyByPackageId(String packageCiGuid) { - Map pkg = retrievePackageCiByGuid(packageCiGuid); - String s3Key = pkg.get("md5_value") + S3_KEY_DELIMITER + pkg.get("name"); - String endpointWithKey = applicationProperties.getArtifactsS3ServerUrl() + "/" - + applicationProperties.getArtifactsS3BucketName() + "/" + s3Key; - return endpointWithKey; - } - - public String retrieveS3EndpointWithKeyByPackageCiMap(Map pkgCiMap) { - String s3Key = pkgCiMap.get("md5_value") + S3_KEY_DELIMITER + pkgCiMap.get("name"); - String endpointWithKey = applicationProperties.getArtifactsS3ServerUrl() + "/" - + applicationProperties.getArtifactsS3BucketName() + "/" + s3Key; - return endpointWithKey; - } - - @SuppressWarnings("unchecked") - protected Map retrievePackageCiByGuid(String packageCiGuid){ - PaginationQuery queryObject = PaginationQuery.defaultQueryObject().addEqualsFilter("guid", packageCiGuid); - PaginationQueryResult result = cmdbServiceV2Stub.queryCiData(cmdbDataProperties.getCiTypeIdOfPackage(), - queryObject); - if (result == null || result.getContents().isEmpty()) { - throw new PluginException(String.format("Package with ID [%s] not found.", packageCiGuid)).withErrorCode("3008", packageCiGuid); - } - - Map pkgData = (Map) result.getContents().get(0); - Map pkg = (Map) pkgData.get("data"); - log.info("Got package data with guid {} {}", packageCiGuid, pkg); - return pkg; - } - - public List listFilesOfCurrentDirs(String currentDir, String endpoint) { - DefaultSaltstackRequest request = new DefaultSaltstackRequest(); - List> inputParamMaps = new ArrayList>(); - Map inputParamMap = new HashMap(); - inputParamMap.put("endpoint", endpoint); - inputParamMap.put("accessKey", applicationProperties.getArtifactsS3AccessKey()); - inputParamMap.put("secretKey", applicationProperties.getArtifactsS3SecretKey()); - inputParamMap.put("currentDir", currentDir); - - inputParamMaps.add(inputParamMap); - request.setInputs(inputParamMaps); - ResultData fileNodesResultItemData = saltstackServiceStub - .getReleasedPackageFilesByCurrentDir(applicationProperties.getWecubeGatewayServerUrl(), request); - - List resultItemDtos = fileNodesResultItemData.getOutputs(); - if(resultItemDtos == null || resultItemDtos.isEmpty()) { - return Collections.emptyList(); - } - - List fileNodes = resultItemDtos.get(0).getFiles(); - if(fileNodes == null) { - return Collections.emptyList(); - } - - return fileNodes; - } - - protected Boolean convertCmdbObjectToBoolean(Object obj) { - if (obj == null) { - return null; - } - - if (obj instanceof Boolean) { - return (Boolean) obj; - } - - if (obj instanceof String) { - if ("true".equalsIgnoreCase((String) obj)) { - return true; - } else { - return false; - } - } - - return null; - } - - @SuppressWarnings("unchecked") - protected Set getBoundDiffConfVarGuidsFromPackage(Map packageCiMap) { - Set guids = new HashSet(); - - List boundDiffConfVariables = (List) packageCiMap.get("diff_conf_variable"); - if (boundDiffConfVariables == null || boundDiffConfVariables.isEmpty()) { - return guids; - } - - for (Object obj : boundDiffConfVariables) { - if (obj == null) { - continue; - } - - if (obj instanceof Map) { - Map boundDiffMap = (Map) obj; - guids.add((String) boundDiffMap.get("guid")); - } - - if (obj instanceof String) { - guids.add((String) obj); - } - } - - return guids; - } - - @SuppressWarnings("unchecked") - protected boolean verifyIfBoundToCurrentPackage(CmdbDiffConfigDto cmdbDiffConfig, - List boundDiffConfVariables) { - if (boundDiffConfVariables == null || boundDiffConfVariables.isEmpty()) { - return false; - } - for (Object boundDiffConfVariable : boundDiffConfVariables) { - String diffConfVarGuid = null; - if (boundDiffConfVariable instanceof Map) { - Map boundDiffConfVariableMap = (Map) boundDiffConfVariable; - diffConfVarGuid = (String) boundDiffConfVariableMap.get("guid"); - } else { - diffConfVarGuid = (String) boundDiffConfVariable; - } - if (diffConfVarGuid.equals(cmdbDiffConfig.getGuid())) { - return true; - } - } - return false; - } - - @SuppressWarnings("unchecked") - protected String getBaselinePackageGuidFromCiMap(Map packageCiMap) { - Map baselinePackageCiMap = (Map) packageCiMap.get("baseline_package"); - if(baselinePackageCiMap == null) { - return null; - } - - String baselinePackageGuid = (String) baselinePackageCiMap.get("guid"); - - return baselinePackageGuid; - } - - - protected String assembleFileItemsToString(List dtos) { - if (dtos == null ) { - return null; - } - - if(dtos.isEmpty()) { - return ""; - } - - String configFileStr = String.join("|", dtos.stream().map(dto -> { - return dto.getFilename(); - }).collect(Collectors.toList())); - - return configFileStr; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ArtifactService.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ArtifactService.java deleted file mode 100644 index 6cd8b03..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ArtifactService.java +++ /dev/null @@ -1,205 +0,0 @@ -package com.webank.plugins.artifacts.service; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -import com.webank.plugins.artifacts.commons.PluginException; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CatCodeDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CategoryDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiDataTreeDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiTypeAttrDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiTypeDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.SpecialConnectorDtoResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.OperateCiDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery.Dialect; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery.Sorting; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQueryResult; -import com.webank.plugins.artifacts.support.saltstack.SaltstackRequest.DefaultSaltstackRequest; - -@Service -public class ArtifactService extends AbstractArtifactService { - private static final Logger log = LoggerFactory.getLogger(ArtifactService.class); - - public void deactive(String packageId) { - updateState(packageId, cmdbDataProperties.getEnumCodeDestroyedOfCiStateOfCreate()); - } - - public void active(String packageId) { - updateState(packageId, cmdbDataProperties.getEnumCodeChangeOfCiStateOfCreate()); - } - - public Object operateState(List operateCiDtos, String operation) { - return cmdbServiceV2Stub.operateCiForState(operateCiDtos, operation); - } - - public Object getCurrentDirs(String packageId, String currentDir) { - DefaultSaltstackRequest request = new DefaultSaltstackRequest(); - List> inputParamMaps = new ArrayList>(); - Map inputParamMap = new HashMap(); - inputParamMap.put("endpoint", retrieveS3EndpointWithKeyByPackageId(packageId)); - inputParamMap.put("accessKey", applicationProperties.getArtifactsS3AccessKey()); - inputParamMap.put("secretKey", applicationProperties.getArtifactsS3SecretKey()); - inputParamMap.put("currentDir", currentDir); - - inputParamMaps.add(inputParamMap); - request.setInputs(inputParamMaps); - return saltstackServiceStub - .getReleasedPackageFilesByCurrentDir(applicationProperties.getWecubeGatewayServerUrl(), request); - } - - public Object getPropertyKeys(String packageId, String filePath) { - DefaultSaltstackRequest request = new DefaultSaltstackRequest(); - List> inputParamMaps = new ArrayList<>(); - Map inputParamMap = new HashMap(); - inputParamMap.put("endpoint", retrieveS3EndpointWithKeyByPackageId(packageId)); - inputParamMap.put("accessKey", applicationProperties.getArtifactsS3AccessKey()); - inputParamMap.put("secretKey", applicationProperties.getArtifactsS3SecretKey()); - inputParamMap.put("filePath", filePath); - - inputParamMaps.add(inputParamMap); - request.setInputs(inputParamMaps); - return saltstackServiceStub - .getReleasedPackagePropertyKeysByFilePath(applicationProperties.getWecubeGatewayServerUrl(), request); - } - - public Object getArtifactSystemDesignTree(String systemDesignId) { - List tree = new ArrayList<>(); - PaginationQuery queryObject = new PaginationQuery(); - Dialect dialect = new Dialect(); - dialect.setShowCiHistory(true); - queryObject.setDialect(dialect); - queryObject.addEqualsFilter("guid", systemDesignId); - PaginationQueryResult ciData = cmdbServiceV2Stub - .queryCiData(cmdbDataProperties.getCiTypeIdOfSystemDesign(), queryObject); - - if (ciData == null || ciData.getContents() == null || ciData.getContents().isEmpty()) { - throw new PluginException(String.format("Can not find ci data for guid [%s]", systemDesignId)); - } - - Object fixedDate = ((Map) ((Map) ciData.getContents().get(0)).get("data")).get(CONSTANT_FIX_DATE); - if (fixedDate != null) { - List dtos = cmdbServiceV2Stub.getCiDataDetailForVersion( - cmdbDataProperties.getCiTypeIdOfSystemDesign(), cmdbDataProperties.getCiTypeIdOfUnitDesign(), - fixedDate.toString()); - - dtos.forEach(dto -> { - if (systemDesignId.equals(((Map) dto.getData()).get("guid"))) { - tree.add(dto); - } - }); - } - return tree; - } - - public PaginationQueryResult getSystemDesignVersions() { - PaginationQueryResult queryResult = new PaginationQueryResult<>(); - - PaginationQuery queryObject = new PaginationQuery(); - Dialect dialect = new Dialect(); - dialect.setShowCiHistory(true); - queryObject.setDialect(dialect); - queryObject.addNotNullFilter(CONSTANT_FIX_DATE); - queryObject.addNotEqualsFilter(CONSTANT_FIX_DATE, ""); - queryObject.setSorting(new Sorting(false, CONSTANT_FIX_DATE)); - - PaginationQueryResult ciDatas = cmdbServiceV2Stub - .queryCiData(cmdbDataProperties.getCiTypeIdOfSystemDesign(), queryObject); - - queryResult.setContents(extractedLatestVersionSystemDesigns(ciDatas)); - - return queryResult; - } - - public void saveDiffConfigEnumCodes(CatCodeDto requestCode) { - CategoryDto cat = cmdbServiceV2Stub.getEnumCategoryByName(cmdbDataProperties.getEnumCategoryNameOfDiffConf()); - if (cat == null) { - String msg = String.format("Can not find category with name [%s].", - cmdbDataProperties.getEnumCategoryNameOfDiffConf()); - throw new PluginException("3005", msg, cmdbDataProperties.getEnumCategoryNameOfDiffConf()); - } - - CatCodeDto code = new CatCodeDto(); - code.setCatId(cat.getCatId()); - code.setCode(requestCode.getCode()); - code.setValue(requestCode.getValue()); - cmdbServiceV2Stub.createEnumCodes(code); - } - - public List getDiffConfigEnumCodes() { - CategoryDto cat = cmdbServiceV2Stub.getEnumCategoryByName(cmdbDataProperties.getEnumCategoryNameOfDiffConf()); - if (cat == null) { - String msg = String.format("Can not find category with name [%s].", - cmdbDataProperties.getEnumCategoryNameOfDiffConf()); - throw new PluginException("3005", msg, cmdbDataProperties.getEnumCategoryNameOfDiffConf()); - } - return cmdbServiceV2Stub.getEnumCodesByCategoryId(cat.getCatId()); - } - - public List getCiTypes(Boolean withAttributes, String status) { - return cmdbServiceV2Stub.getAllCiTypes(withAttributes, status); - } - - public PaginationQueryResult querySystemEnumCodesWithRefResources(PaginationQuery queryObject) { - queryObject.addEqualsFilter(CONSTANT_CAT_CAT_TYPE, cmdbDataProperties.getEnumCategoryTypeSystem()); - queryObject.addReferenceResource("cat"); - queryObject.addReferenceResource(CONSTANT_CAT_CAT_TYPE); - return cmdbServiceV2Stub.queryEnumCodes(queryObject); - } - - public void deleteCiData(int ciTypeId, List ids) { - cmdbServiceV2Stub.deleteCiData(ciTypeId, ids); - - } - - public List getCiTypeReferenceBy(Integer ciTypeId) { - PaginationQuery queryObject = new PaginationQuery().addEqualsFilter("referenceId", ciTypeId) - .addInFilter(CONSTANT_INPUT_TYPE, Arrays.asList("ref", "multiRef")) - .addReferenceResource(CONSTANT_CI_TYPE); - queryObject.addReferenceResource(CONSTANT_CI_TYPE); - return cmdbServiceV2Stub.queryCiTypeAttributes(queryObject); - } - - public List getSpecialConnector() { - return cmdbServiceV2Stub.getSpecialConnector(); - } - - private List extractedLatestVersionSystemDesigns(PaginationQueryResult ciDatas) { - List finalCiDatas = new ArrayList<>(); - ciDatas.getContents().forEach(ciData -> { - if (ciData instanceof Map) { - Map map = (Map) ciData; - if (!isExist(finalCiDatas, map.get("data"))) { - finalCiDatas.add(ciData); - } - } - }); - return finalCiDatas; - } - - private boolean isExist(List results, Object systemName) { - for (Object result : results) { - Map m = (Map) result; - Object existRguid = ((Map) m.get("data")).get("r_guid"); - Object newRguid = ((Map) systemName).get("r_guid"); - if (existRguid != null && existRguid.equals(newRguid)) { - return true; - } - } - return false; - } - - private void updateState(String packageId, String operation) { - List operateCiDtos = new ArrayList<>(); - operateCiDtos.add(new OperateCiDto(packageId, cmdbDataProperties.getCiTypeIdOfPackage())); - cmdbServiceV2Stub.operateCiForState(operateCiDtos, operation); - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ConfigFileManagementService.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ConfigFileManagementService.java deleted file mode 100644 index 4fc0f32..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ConfigFileManagementService.java +++ /dev/null @@ -1,1260 +0,0 @@ -package com.webank.plugins.artifacts.service; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.stream.Collectors; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Service; - -import com.webank.plugins.artifacts.commons.PluginException; -import com.webank.plugins.artifacts.dto.ConfigFileDto; -import com.webank.plugins.artifacts.dto.ConfigKeyInfoDto; -import com.webank.plugins.artifacts.dto.DiffConfVariableInfoDto; -import com.webank.plugins.artifacts.dto.DiffConfigurationUpdateDto; -import com.webank.plugins.artifacts.dto.FileQueryRequestDto; -import com.webank.plugins.artifacts.dto.FileQueryResultItemDto; -import com.webank.plugins.artifacts.dto.PackageComparisionRequestDto; -import com.webank.plugins.artifacts.dto.PackageComparisionResultDto; -import com.webank.plugins.artifacts.dto.PackageConfigFilesUpdateRequestDto; -import com.webank.plugins.artifacts.dto.SinglePackageQueryResultDto; -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbDiffConfigDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQueryResult; -import com.webank.plugins.artifacts.support.saltstack.SaltConfigFileDto; -import com.webank.plugins.artifacts.support.saltstack.SaltConfigKeyInfoDto; -import com.webank.plugins.artifacts.support.saltstack.SaltFileNodeDto; -import com.webank.plugins.artifacts.support.saltstack.SaltFileNotExistException; -import com.webank.plugins.artifacts.support.saltstack.SaltstackRemoteCallException; -import com.webank.plugins.artifacts.support.saltstack.SaltstackRequest.DefaultSaltstackRequest; -import com.webank.plugins.artifacts.support.saltstack.SaltstackResponse.ResultData; - -@Service -public class ConfigFileManagementService extends AbstractArtifactService { - private static final Logger log = LoggerFactory.getLogger(ConfigFileManagementService.class); - - public void updateDiffConfigurations(List diffConfsToUpdate) { - if (diffConfsToUpdate == null || diffConfsToUpdate.isEmpty()) { - return; - } - List> requestParamsMaps = new ArrayList>(); - for (DiffConfigurationUpdateDto dto : diffConfsToUpdate) { - Map requstParamsMap = new HashMap(); - requstParamsMap.put("id", dto.getId()); - requstParamsMap.put("variable_value", dto.getDiffExpr()); - - requestParamsMaps.add(requstParamsMap); - } - - standardCmdbEntityRestClient.updateDiffConfigurationCi(requestParamsMaps); - - } - - public SinglePackageQueryResultDto updateConfigFilesOfPackage(String unitDesignId, String packageCiGuid, - PackageConfigFilesUpdateRequestDto packageReqDto) { - - SinglePackageQueryResultDto packageUpdateResult = new SinglePackageQueryResultDto(); - packageUpdateResult.setPackageId(packageCiGuid); - - Map packageCiMap = retrievePackageCiByGuid(packageCiGuid); -// String baselinePackageGuid = getBaselinePackageGuidFromCiMap(packageCiMap); -// packageUpdateResult.setBaselinePackage(baselinePackageGuid); - List oldDiffConfFiles = getDiffConfFilesAsStringList(packageCiMap); - List newDiffConfFiles = packageReqDto.getDiffConfFile(); - - List allCmdbDiffConfigs = getAllCmdbDiffConfigs(); - - List diffConfVariables = packageReqDto.getDiffConfVariable(); - - if (oldDiffConfFiles.isEmpty()) { - processDiffConfFilesIfNotExistDiffConfFiles(packageUpdateResult, packageCiMap, newDiffConfFiles, - allCmdbDiffConfigs); - } else { - processDiffConfFilesIfExistDiffConfFiles(packageUpdateResult, packageCiMap, newDiffConfFiles, - oldDiffConfFiles, allCmdbDiffConfigs, diffConfVariables); - } - - updateConfigFilesToPackageCi(packageUpdateResult, packageReqDto); - -// packageUpdateResult.setIsDecompression(packageReqDto.getIsDecompression()); - packageUpdateResult = querySinglePackage(unitDesignId, packageCiGuid); - - return packageUpdateResult; - } - - public PackageComparisionResultDto packageComparision(String unitDesignId, String packageGuid, - PackageComparisionRequestDto comparisonReqDto) { - String baselinePackageGuid = comparisonReqDto.getBaselinePackage(); - if (StringUtils.isBlank(baselinePackageGuid)) { - throw new PluginException("Baseline package should provide."); - } - - Map packageCiMap = retrievePackageCiByGuid(packageGuid); - Map baselinePackageCiMap = retrievePackageCiByGuid(baselinePackageGuid); - - PackageComparisionResultDto result = buildPackageComparisionResult(packageGuid, packageCiMap, baselinePackageCiMap, false); - PackageComparisionResultDto baselineResult = buildPackageComparisionResult(baselinePackageGuid, - baselinePackageCiMap,baselinePackageCiMap, true); - - performPackageConfigComparison(result, baselineResult); - - return result; - } - - public List queryDeployConfigFiles(String packageCiGuid, - FileQueryRequestDto fileQueryRequestDto) { - List inputFilepathList = fileQueryRequestDto.getFileList(); - String baselinePackageGuid = fileQueryRequestDto.getBaselinePackage(); - if (inputFilepathList == null) { - throw new PluginException("File list cannot be null to query files."); - } - - boolean expendAll = fileQueryRequestDto.getExpandAll(); - - Map packageCiMap = retrievePackageCiByGuid(packageCiGuid); - - log.info("packageCiMap:{}", packageCiMap); - List filepathList = new ArrayList(); - // - if (inputFilepathList.isEmpty()) { -// String rootDirName = getDeployPackageRootDirName(packageCiMap); - filepathList.add(""); - } else { - filepathList.addAll(inputFilepathList); - } - - List fileQueryResultItems = doQueryDeployConfigFiles(packageCiGuid, filepathList, - packageCiMap, expendAll); - - Map baselinePackageCiMap = null; - if (StringUtils.isNoneBlank(baselinePackageGuid)) { - baselinePackageCiMap = retrievePackageCiByGuid(baselinePackageGuid); - } - if (baselinePackageCiMap != null) { - List baselinePackageFileQueryResultItems = doQueryDeployConfigFiles( - baselinePackageGuid, filepathList, baselinePackageCiMap, expendAll); - doExecuteFileQueryComparison(fileQueryResultItems, baselinePackageFileQueryResultItems); - } - - return fileQueryResultItems; - } - - @SuppressWarnings("unchecked") - public SinglePackageQueryResultDto querySinglePackage(String unitDesignId, String packageId) { - Map packageCiMap = retrievePackageCiByGuid(packageId); - log.info("currPackageCi:{}", packageCiMap); - log.info("baseline_package:{}", packageCiMap.get("baseline_package")); - String baselinePackageGuid = getBaselinePackageGuidFromCiMap(packageCiMap); - - SinglePackageQueryResultDto result = new SinglePackageQueryResultDto(); - result.setPackageId(packageId); - result.setBaselinePackage(baselinePackageGuid); - ConfigFilesSaltInfoEnricher currentEnricher = new ConfigFilesSaltInfoEnricher(packageId, packageCiMap, - this); - - Object isDecompression = packageCiMap.get("is_decompression"); - result.setIsDecompression(convertCmdbObjectToBoolean(isDecompression)); - result.setStartFilePath(currentEnricher.enrichFileInfoBySalt(getStartFileInfos(packageCiMap))); - result.setDeployFilePath(currentEnricher.enrichFileInfoBySalt(getDeployFileInfos(packageCiMap))); - result.setStopFilePath(currentEnricher.enrichFileInfoBySalt(getStopFileInfos(packageCiMap))); - result.setDiffConfFile(currentEnricher.enrichFileInfoBySalt(getDiffConfFileInfos(packageCiMap))); - - SinglePackageQueryResultDto baselineResult = null; - if (StringUtils.isNoneBlank(baselinePackageGuid)) { - Map baselinePackageCiMap = retrievePackageCiByGuid(baselinePackageGuid); - ConfigFilesSaltInfoEnricher baselineEnricher = new ConfigFilesSaltInfoEnricher(baselinePackageGuid, baselinePackageCiMap, - this); - baselineResult = new SinglePackageQueryResultDto(); - baselineResult.setPackageId(baselinePackageGuid); - baselineResult.setBaselinePackage(null); - - Object isDecompressionBaseline = baselinePackageCiMap.get("is_decompression"); - baselineResult.setIsDecompression(convertCmdbObjectToBoolean(isDecompressionBaseline)); - - baselineResult.setStartFilePath(baselineEnricher.enrichFileInfoBySalt(getStartFileInfos(packageCiMap))); - baselineResult.setDeployFilePath(baselineEnricher.enrichFileInfoBySalt(getDeployFileInfos(packageCiMap))); - baselineResult.setStopFilePath(baselineEnricher.enrichFileInfoBySalt(getStopFileInfos(packageCiMap))); - baselineResult.setDiffConfFile(baselineEnricher.enrichFileInfoBySalt(getDiffConfFileInfos(packageCiMap))); - - doCompareFilesWithBaselineFiles(result, baselineResult); - } - - String s3EndpointOfPackageId = retrieveS3EndpointWithKeyByPackageCiMap(packageCiMap); - List allCmdbDiffConfigs = getAllCmdbDiffConfigs(); - - Set allDiffConfigKeys = new HashSet(); - // process all diff files - for (ConfigFileDto configFile : result.getDiffConfFile()) { - List saltConfigKeyInfos = calculatePropertyKeys(packageId, configFile.getFilename(), - s3EndpointOfPackageId); - for (SaltConfigKeyInfoDto saltConfigInfo : saltConfigKeyInfos) { - ConfigKeyInfoDto configKeyInfo = new ConfigKeyInfoDto(); - configKeyInfo.setKey(saltConfigInfo.getKey()); - configKeyInfo.setLine(saltConfigInfo.getLine()); - configKeyInfo.setType(saltConfigInfo.getType()); - - configFile.addConfigKeyInfo(configKeyInfo); - - allDiffConfigKeys.add(saltConfigInfo.getKey()); - } - } - - List boundDiffConfVariables = (List) packageCiMap.get("diff_conf_variable"); - - List diffConfVariables = new ArrayList(); - for (String diffConfigKey : allDiffConfigKeys) { - CmdbDiffConfigDto cmdbDiffConfig = findoutFromCmdbDiffConfigsByKey(diffConfigKey, allCmdbDiffConfigs); - if (cmdbDiffConfig == null) { - log.info("Cannot find cmdb diff config key:{}", diffConfigKey); - continue; - } - Boolean bound = verifyIfBoundToCurrentPackage(cmdbDiffConfig, boundDiffConfVariables); - - DiffConfVariableInfoDto diffVarInfo = new DiffConfVariableInfoDto(); - - diffVarInfo.setBound(bound); - diffVarInfo.setDiffConfigGuid(cmdbDiffConfig.getGuid()); - diffVarInfo.setDiffExpr(cmdbDiffConfig.getDiffExpr()); - diffVarInfo.setKey(cmdbDiffConfig.getKey()); - diffVarInfo.setFixedDate(cmdbDiffConfig.getFixedDate()); - - diffConfVariables.add(diffVarInfo); - } - - result.setDiffConfVariable(diffConfVariables); - - return result; - } - - private void doCompareFilesWithBaselineFiles(SinglePackageQueryResultDto newResult, - SinglePackageQueryResultDto oldResult) { - List newStartFiles = newResult.getStartFilePath(); - List newStopFiles = newResult.getStopFilePath(); - List newDeployFiles = newResult.getDeployFilePath(); - List newDiffConfFiles = newResult.getDiffConfFile(); - - List oldStartFiles = newConfigFileDtoList(oldResult.getStartFilePath()); - List oldStopFiles = newConfigFileDtoList(oldResult.getStopFilePath()); - List oldDeployFiles = newConfigFileDtoList(oldResult.getDeployFilePath()); - List oldDiffConfFiles = newConfigFileDtoList(oldResult.getDiffConfFile()); - - performFileComparison(newStartFiles, oldStartFiles); - -// if (!oldStartFiles.isEmpty()) { -// for (ConfigFileDto f : oldStartFiles) { -// f.setComparisonResult(FILE_COMP_DELETED); -// newResult.getStartFilePath().add(f); -// } -// } - - performFileComparison(newStopFiles, oldStopFiles); -// if (!oldStopFiles.isEmpty()) { -// for (ConfigFileDto f : oldStopFiles) { -// f.setComparisonResult(FILE_COMP_DELETED); -// newResult.getStopFilePath().add(f); -// } -// } - - performFileComparison(newDeployFiles, oldDeployFiles); -// if (!oldDeployFiles.isEmpty()) { -// for (ConfigFileDto f : oldDeployFiles) { -// f.setComparisonResult(FILE_COMP_DELETED); -// newResult.getDeployFilePath().add(f); -// } -// } - - performFileComparison(newDiffConfFiles, oldDiffConfFiles); -// if (!oldDiffConfFiles.isEmpty()) { -// for (ConfigFileDto f : oldDiffConfFiles) { -// f.setComparisonResult(FILE_COMP_DELETED); -// newResult.getDiffConfFile().add(f); -// } -// } - - } - - private CmdbDiffConfigDto findoutFromCmdbDiffConfigsByKey(String key, List allCmdbDiffConfigs) { - if (allCmdbDiffConfigs == null || allCmdbDiffConfigs.isEmpty()) { - return null; - } - - for (CmdbDiffConfigDto dto : allCmdbDiffConfigs) { - if (key.equalsIgnoreCase(dto.getKey())) { - return dto; - } - } - - return null; - } - - private List getAllCmdbDiffConfigs() { - List diffConfigs = new ArrayList(); - List> diffConfigMaps = standardCmdbEntityRestClient.queryDiffConfigurations(); - for (Map diffConfigMap : diffConfigMaps) { - CmdbDiffConfigDto dto = new CmdbDiffConfigDto(); - dto.setDiffExpr((String) diffConfigMap.get("variable_value")); - dto.setGuid((String) diffConfigMap.get("guid")); - dto.setKey((String) diffConfigMap.get("code")); - dto.setDisplayName((String) diffConfigMap.get("displayName")); - dto.setFixedDate((String) diffConfigMap.get("fixed_date")); - - diffConfigs.add(dto); - } - - return diffConfigs; - } - - private List getStartFileInfos(Map packageCiMap) { - String filePathsStr = (String) packageCiMap.get("start_file_path"); - List files = parseFilePathString(filePathsStr); - return files; - } - - private List getStopFileInfos(Map packageCiMap) { - String filePathsStr = (String) packageCiMap.get("stop_file_path"); - List files = parseFilePathString(filePathsStr); - return files; - } - - private List getDeployFileInfos(Map packageCiMap) { - String filePathsStr = (String) packageCiMap.get("deploy_file_path"); - List files = parseFilePathString(filePathsStr); - return files; - } - - private List getDiffConfFileInfos(Map packageCiMap) { - String filePathsStr = (String) packageCiMap.get("diff_conf_file"); - List files = parseFilePathString(filePathsStr); - - return files; - } - - public PaginationQueryResult> queryDeployPackages(String unitDesignId, - PaginationQuery queryObject) { - queryObject.addEqualsFilter("unit_design", unitDesignId); - PaginationQueryResult result = cmdbServiceV2Stub.queryCiData(cmdbDataProperties.getCiTypeIdOfPackage(), - queryObject); - PaginationQueryResult> refinededResult = refineQueryDeployPackagesResult(result); - return refinededResult; - } - - @SuppressWarnings("unchecked") - private PaginationQueryResult> refineQueryDeployPackagesResult( - PaginationQueryResult result) { - PaginationQueryResult> refinededResult = new PaginationQueryResult>(); - refinededResult.setPageInfo(result.getPageInfo()); - - List> refinedContents = new ArrayList>(); - for (Object contentObj : result.getContents()) { - if (!(contentObj instanceof Map)) { - log.error("Bad data type,expected:{},but:{}", Map.class.getSimpleName(), - contentObj.getClass().getSimpleName()); - throw new PluginException("Bad data type.").withErrorCode("3009", Map.class.getSimpleName(), - contentObj.getClass().getSimpleName()); - } - - Map contentMap = (Map) contentObj; - Map refinedMap = refineQueryDeployPackagesResultContentMap(contentMap); - - refinedContents.add(refinedMap); - } - - refinededResult.setContents(refinedContents); - - return refinededResult; - } - - @SuppressWarnings("unchecked") - private Map refineQueryDeployPackagesResultContentMap(Map contentMap) { - Map refinedMap = new HashMap(); - for (Entry entry : contentMap.entrySet()) { - String key = entry.getKey(); - if ("data".equals(key)) { - Object dataObj = entry.getValue(); - Map dataMap = (Map) dataObj; - Map refinedDataMap = refineQueryDeployPackagesResultDataMap(dataMap); - - refinedMap.put("data", refinedDataMap); - } else { - refinedMap.put(key, entry.getValue()); - } - } - - return refinedMap; - } - - private Map refineQueryDeployPackagesResultDataMap(Map dataMap) { - Map refinedDataMap = new HashMap(); - for (Entry dataEntry : dataMap.entrySet()) { - String dataKey = (String) dataEntry.getKey(); - if ("deploy_file_path".equals(dataKey)) { - refinedDataMap.put(dataKey, parseFilePathString((String) dataEntry.getValue())); - } else if ("start_file_path".equals(dataKey)) { - refinedDataMap.put(dataKey, parseFilePathString((String) dataEntry.getValue())); - } else if ("stop_file_path".equals(dataKey)) { - refinedDataMap.put(dataKey, parseFilePathString((String) dataEntry.getValue())); - } else if ("diff_conf_file".equals(dataKey)) { - refinedDataMap.put(dataKey, parseFilePathString((String) dataEntry.getValue())); - } else { - refinedDataMap.put(dataKey, dataEntry.getValue()); - } - } - - return refinedDataMap; - } - - private List parseFilePathString(String filePathString) { - List files = new ArrayList(); - if (StringUtils.isBlank(filePathString)) { - return files; - } - - String[] fileStringParts = filePathString.split("\\|"); - for (String fileStringPart : fileStringParts) { - ConfigFileDto fileDto = new ConfigFileDto(); - fileDto.setFilename(fileStringPart); - - files.add(fileDto); - - } - - return files; - } - - private List calculatePropertyKeys(String packageId, String filePath, - String s3EndpointOfPackageId) { - DefaultSaltstackRequest request = new DefaultSaltstackRequest(); - List> inputParamMaps = new ArrayList<>(); - Map inputParamMap = new HashMap(); - inputParamMap.put("endpoint", s3EndpointOfPackageId); - inputParamMap.put("accessKey", applicationProperties.getArtifactsS3AccessKey()); - inputParamMap.put("secretKey", applicationProperties.getArtifactsS3SecretKey()); - inputParamMap.put("filePath", filePath); - - inputParamMaps.add(inputParamMap); - request.setInputs(inputParamMaps); - ResultData resultData = null; - try { - resultData = saltstackServiceStub.getReleasedPackagePropertyKeysByFilePath( - applicationProperties.getWecubeGatewayServerUrl(), request); - } catch (SaltstackRemoteCallException e) { - log.info("errors to get conf key from {},error:{}", filePath, e.getMessage()); - return Collections.emptyList(); - } - - List saltConfigFileDtos = resultData.getOutputs(); - if (saltConfigFileDtos == null || saltConfigFileDtos.isEmpty()) { - return Collections.emptyList(); - } - - SaltConfigFileDto saltConfigFileDto = saltConfigFileDtos.get(0); - - List saltConfigKeyInfos = saltConfigFileDto.getConfigKeyInfos(); - if (saltConfigKeyInfos == null || saltConfigKeyInfos.isEmpty()) { - return Collections.emptyList(); - } - - return saltConfigKeyInfos; - } - - private void performPackageConfigComparison(PackageComparisionResultDto newResult, - PackageComparisionResultDto oldResult) { - List newStartFiles = newResult.getStartFilePath(); - List newStopFiles = newResult.getStopFilePath(); - List newDeployFiles = newResult.getDeployFilePath(); - List newDiffConfFiles = newResult.getDiffConfFile(); - - List oldStartFiles = newConfigFileDtoList(oldResult.getStartFilePath()); - List oldStopFiles = newConfigFileDtoList(oldResult.getStopFilePath()); - List oldDeployFiles = newConfigFileDtoList(oldResult.getDeployFilePath()); - List oldDiffConfFiles = newConfigFileDtoList(oldResult.getDiffConfFile()); - - performFileComparison(newStartFiles, oldStartFiles); - - if (!oldStartFiles.isEmpty()) { - for (ConfigFileDto f : oldStartFiles) { - f.setComparisonResult(FILE_COMP_DELETED); - newResult.getStartFilePath().add(f); - } - } - - performFileComparison(newStopFiles, oldStopFiles); - if (!oldStopFiles.isEmpty()) { - for (ConfigFileDto f : oldStopFiles) { - f.setComparisonResult(FILE_COMP_DELETED); - newResult.getStopFilePath().add(f); - } - } - - performFileComparison(newDeployFiles, oldDeployFiles); - if (!oldDeployFiles.isEmpty()) { - for (ConfigFileDto f : oldDeployFiles) { - f.setComparisonResult(FILE_COMP_DELETED); - newResult.getDeployFilePath().add(f); - } - } - - performFileComparison(newDiffConfFiles, oldDiffConfFiles); - if (!oldDiffConfFiles.isEmpty()) { - for (ConfigFileDto f : oldDiffConfFiles) { - f.setComparisonResult(FILE_COMP_DELETED); - newResult.getDiffConfFile().add(f); - } - } - - } - - private void performFileComparison(List newFiles, List oldFiles) { - for (ConfigFileDto newFile : newFiles) { - ConfigFileDto oldFile = peekIfFoundConfigFileByFilename(newFile.getFilename(), oldFiles); - if (oldFile == null) { - if(FILE_COMP_DELETED.equals(newFile.getComparisonResult())) { - //nothing - }else { - newFile.setComparisonResult(FILE_COMP_NEW); - } - } else { - if(FILE_COMP_DELETED.equals(oldFile.getComparisonResult())) { - if(FILE_COMP_DELETED.equals(newFile.getComparisonResult())) { - //nothing - }else { - newFile.setComparisonResult(FILE_COMP_NEW); - } - }else { - if(FILE_COMP_DELETED.equals(newFile.getComparisonResult())) { - //nothing - }else{ - if (newFile.getMd5() != null && newFile.getMd5().equals(oldFile.getMd5())) { - newFile.setComparisonResult(FILE_COMP_SAME); - } else { - newFile.setComparisonResult(FILE_COMP_CHANGED); - } - } - - } - } - } - } - - private ConfigFileDto peekIfFoundConfigFileByFilename(String filename, List oldFiles) { - if (oldFiles == null || oldFiles.isEmpty()) { - return null; - } -// String newFilenameWithoutPrefix = filename.substring(filename.indexOf("/")); - ConfigFileDto foundFile = null; - for (ConfigFileDto oldFile : oldFiles) { - String oldFilename = oldFile.getFilename(); -// String oldFilenameWithoutPrefix = oldFilename.substring(oldFilename.indexOf("/")); - - if (filename.equals(oldFilename)) { - foundFile = oldFile; - break; - } - } - - if (foundFile != null) { - oldFiles.remove(foundFile); - } - - return foundFile; - } - - private List newConfigFileDtoList(List files) { - List newFiles = new ArrayList<>(); - newFiles.addAll(files); - - return newFiles; - } - - private SaltFileNodeDto getSaltFileNodeBySingleFilepath(String packageGuid, Map packageCiMap, - String filepath, String rootDirName) { - String s3EndpointOfPackageId = retrieveS3EndpointWithKeyByPackageCiMap(packageCiMap); - List saltFileNodes = null; - if (rootDirName.equals(filepath)) { - try { - saltFileNodes = listFilesOfCurrentDirs(filepath, s3EndpointOfPackageId); - SaltFileNodeDto fileNode = new SaltFileNodeDto(); - fileNode.setIsDir(true); - fileNode.setMd5(null); - fileNode.setName(filepath); - fileNode.setPath(filepath); - return fileNode; - } catch (SaltFileNotExistException e) { - log.info("file not exist:{}", filepath); - return null; - } - } - - if(filepath.startsWith("/") && filepath.length() > 1) { - filepath = filepath.substring(1); - } - - String baseDirName = ""; - String filename = filepath; - if(filepath.indexOf("/") > 0) { - baseDirName = filepath.substring(0, filepath.lastIndexOf("/")); - filename = filepath.substring(filepath.lastIndexOf("/") + 1); - } - try { - saltFileNodes = listFilesOfCurrentDirs(baseDirName, s3EndpointOfPackageId); - } catch (SaltFileNotExistException e) { - log.info("file not exist:{}", filepath); - return null; - } - for (SaltFileNodeDto dto : saltFileNodes) { - if (filename.equals(dto.getName())) { - return dto; - } - } - - return null; - } - - private PackageComparisionResultDto buildPackageComparisionResult(String packageGuid, - Map packageCiMap, Map baselinePackageCiMap, boolean isBaseline) { - Map configFilesSrcPackageMap = null; - if (isBaseline) { - configFilesSrcPackageMap = packageCiMap; - }else { - configFilesSrcPackageMap = baselinePackageCiMap; - } - PackageComparisionResultDto result = new PackageComparisionResultDto(); - ConfigFilesSaltInfoEnricher enricher = new ConfigFilesSaltInfoEnricher(packageGuid, packageCiMap, this); - List deployFiles = enricher.enrichFileInfoBySalt(getDeployFileInfos(configFilesSrcPackageMap)); - result.setDeployFilePath(deployFiles); - - List diffConfFiles = enricher.enrichFileInfoBySalt(getDiffConfFileInfos(configFilesSrcPackageMap)); - result.setDiffConfFile(diffConfFiles); - - List startFiles = enricher.enrichFileInfoBySalt(getStartFileInfos(configFilesSrcPackageMap)); - result.setStartFilePath(startFiles); - - List stopFiles = enricher.enrichFileInfoBySalt(getStopFileInfos(configFilesSrcPackageMap)); - result.setStopFilePath(stopFiles); - -// String s3EndpointOfPackageId = retrieveS3EndpointWithKeyByPackageCiMap(packageCiMap); -// for (ConfigFileDto configFile : result.getDiffConfFile()) { -// try { -// List saltConfigKeyInfos = calculatePropertyKeys(packageGuid, -// configFile.getFilename(), s3EndpointOfPackageId); -// for (SaltConfigKeyInfoDto saltConfigInfo : saltConfigKeyInfos) { -// ConfigKeyInfoDto configKeyInfo = new ConfigKeyInfoDto(); -// configKeyInfo.setKey(saltConfigInfo.getKey()); -// configKeyInfo.setLine(saltConfigInfo.getLine()); -// configKeyInfo.setType(saltConfigInfo.getType()); -// -// configFile.addConfigKeyInfo(configKeyInfo); -// -// } -// } catch (SaltstackRemoteCallException e) { -// log.info("errors to get keys from {},error:{}", configFile.getFilename(), e.getMessage()); -// } -// } - - return result; - } - - private void doExecuteFileQueryComparison(List fileQueryResultItems, - List baselinePackageFileQueryResultItems) { - if (fileQueryResultItems == null || fileQueryResultItems.isEmpty()) { - return; - } - - int size = fileQueryResultItems.size(); - - for (int idx = 0; idx < size; idx++) { - FileQueryResultItemDto rootItem = fileQueryResultItems.get(idx); - FileQueryResultItemDto rootBaselineItem = findoutFileQueryResultItemByName(rootItem.getName(), baselinePackageFileQueryResultItems); - - if(rootBaselineItem != null) { - compareRootFileQueryResultItemDto(rootItem, rootBaselineItem); - }else { - if(FILE_COMP_DELETED.equals(rootItem.getComparisonResult())) { - //nothing - }else { - rootItem.setComparisonResult(FILE_COMP_NEW); - } - } - } - - } - - private FileQueryResultItemDto findoutFileQueryResultItemByName(String name,List baselinePackageFileQueryResultItems) { - if(baselinePackageFileQueryResultItems == null || baselinePackageFileQueryResultItems.isEmpty()) { - return null; - } - - for(FileQueryResultItemDto item : baselinePackageFileQueryResultItems) { - if(name.equals(item.getName())) { - return item; - } - } - - return null; - } - - private void compareRootFileQueryResultItemDto(FileQueryResultItemDto rootItem, - FileQueryResultItemDto rootBaselineItem) { - - Map fileItems = transformFileQueryResultItemDtoToMap(rootItem); - Map baselineFileItems = transformFileQueryResultItemDtoToMap(rootBaselineItem); - - for (Entry fileItemEntry : fileItems.entrySet()) { - FileQueryResultItemDto baselineItem = baselineFileItems.get(fileItemEntry.getKey()); - FileQueryResultItemDto item = fileItemEntry.getValue(); - if (baselineItem == null) { - item.setComparisonResult(FILE_COMP_NEW); - } else { - if (FILE_COMP_DELETED.equals(item.getComparisonResult())) { - continue; - } - - if (item.getIsDir() != null && item.getIsDir()) { - item.setComparisonResult(FILE_COMP_SAME); - } else { - String md5 = item.getMd5(); - String baseMd5 = baselineItem.getMd5(); - if (md5 != null && md5.equals(baseMd5)) { - item.setComparisonResult(FILE_COMP_SAME); - } else { - item.setComparisonResult(FILE_COMP_CHANGED); - } - } - } - } - - findoutDeletedFiles(rootItem, rootBaselineItem, fileItems, baselineFileItems); - - } - - private void findoutDeletedFiles(FileQueryResultItemDto rootItem, FileQueryResultItemDto rootBaselineItem, - Map fileItems, Map baselineFileItems) { - List deletedRelativeFiles = new ArrayList(); - for (Entry entry : baselineFileItems.entrySet()) { - FileQueryResultItemDto dto = fileItems.get(entry.getKey()); - if (dto == null) { - deletedRelativeFiles.add(entry.getValue()); - } - } - - for (FileQueryResultItemDto deletedRelativeFile : deletedRelativeFiles) { - String[] relativePaths = deletedRelativeFile.getPath().split("/"); - String pathKey = null; - FileQueryResultItemDto parentItem = rootItem; - for (String relativePath : relativePaths) { - if (pathKey == null) { - pathKey = relativePath; - } else { - pathKey = pathKey + "/" + relativePath; - } - - FileQueryResultItemDto item = fileItems.get(pathKey); - if (item == null) { - item = new FileQueryResultItemDto(); - item.setComparisonResult(FILE_COMP_DELETED); - item.setExists(deletedRelativeFile.getExists()); - item.setIsDir(deletedRelativeFile.getIsDir()); - item.setMd5(deletedRelativeFile.getMd5()); - item.setName(deletedRelativeFile.getName()); - item.setPath(deletedRelativeFile.getPath()); - - parentItem.addFileQueryResultItem(item); - } - - parentItem = item; - } - } - } - - private Map transformFileQueryResultItemDtoToMap(FileQueryResultItemDto rootItem) { - Map fileItems = new HashMap(); - travelTreeItems(fileItems, rootItem); - - return fileItems; - } - - private void travelTreeItems(Map fileItems, FileQueryResultItemDto item) { - if (item == null) { - return; - } - - fileItems.put(item.getPath(), item); - if (item.getChildren() == null || item.getChildren().isEmpty()) { - return; - } - - for (FileQueryResultItemDto childItem : item.getChildren()) { - travelTreeItems(fileItems, childItem); - } - } - - private String getDeployPackageRootDirName(Map packageCiMap) { - String packageEndpoint = retrieveS3EndpointWithKeyByPackageCiMap(packageCiMap); - List saltFileNodes = listFilesOfCurrentDirs("", packageEndpoint); - return saltFileNodes.get(0).getName(); - } - - private List doQueryDeployConfigFiles(String packageCiGuid, List filePathList, - Map packageCiMap, boolean expendAll) { - List fileQueryResultItems = new ArrayList(); - Map pathAndFileQueryResultItems = new HashMap(); - String packageEndpoint = retrieveS3EndpointWithKeyByPackageCiMap(packageCiMap); - for (String filepath : filePathList) { - log.info("handle filepath:{}", filepath); - queryFilesForSingleFilepath(packageCiGuid, filepath, packageCiMap, packageEndpoint, fileQueryResultItems, - pathAndFileQueryResultItems, expendAll); - - } - return fileQueryResultItems; - } - - private FileQueryResultItemDto buildFileQueryResultItemDto(SaltFileNodeDto saltFileNode) { - FileQueryResultItemDto fileQueryResultItem = new FileQueryResultItemDto(); - fileQueryResultItem.setIsDir(saltFileNode.getIsDir()); - fileQueryResultItem.setExists(true); - fileQueryResultItem.setMd5(fileQueryResultItem.getMd5()); - fileQueryResultItem.setName(saltFileNode.getName()); - - return fileQueryResultItem; - } - - private void queryFilesForRootDir(String packageEndpoint, List fileQueryResultItems, - Map pathAndFileQueryResultItems) { - List saltFileNodes = listFilesOfCurrentDirs("", packageEndpoint); - if (saltFileNodes == null) { - return; - } - - for (SaltFileNodeDto saltFileNode : saltFileNodes) { - FileQueryResultItemDto fileQueryResultItem = pathAndFileQueryResultItems.get(saltFileNode.getName()); - if (fileQueryResultItem == null) { - fileQueryResultItem = buildFileQueryResultItemDto(saltFileNode); - fileQueryResultItem.setPath(saltFileNode.getName()); - - fileQueryResultItems.add(fileQueryResultItem); - pathAndFileQueryResultItems.put(fileQueryResultItem.getPath(), fileQueryResultItem); - } - } - } - - private void queryFilesForSingleFilepathExpendAll(String packageCiGuid, String filepath, - Map packageCiMap, String packageEndpoint, List fileQueryResultItems, - Map pathAndFileQueryResultItems) { - queryFilesForRootDir(packageEndpoint, fileQueryResultItems, pathAndFileQueryResultItems); - String[] filepathParts = filepath.split("/"); - - String rootFilepath = filepathParts[0]; - FileQueryResultItemDto rootResultItem = pathAndFileQueryResultItems.get(rootFilepath); - if (rootResultItem == null) { - rootResultItem = new FileQueryResultItemDto(); - rootResultItem.setIsDir(true); - rootResultItem.setName(rootFilepath); - rootResultItem.setPath(rootFilepath); - - fileQueryResultItems.add(rootResultItem); - pathAndFileQueryResultItems.put(rootResultItem.getPath(), rootResultItem); - } - - List saltFileNodes = new ArrayList(); - if(rootResultItem != null && rootResultItem.getIsDir()) { - try { - saltFileNodes = listFilesOfCurrentDirs(rootFilepath, packageEndpoint); - for (SaltFileNodeDto saltFileNode : saltFileNodes) { - String childFilePath = rootFilepath + "/" + saltFileNode.getName(); - FileQueryResultItemDto childRootResultItem = pathAndFileQueryResultItems.get(childFilePath); - if (childRootResultItem == null) { - childRootResultItem = new FileQueryResultItemDto(); - childRootResultItem.setExists(true); - childRootResultItem.setIsDir(saltFileNode.getIsDir()); - childRootResultItem.setMd5(saltFileNode.getMd5()); - childRootResultItem.setName(saltFileNode.getName()); - childRootResultItem.setPath(childFilePath); - - rootResultItem.addFileQueryResultItem(childRootResultItem); - pathAndFileQueryResultItems.put(childRootResultItem.getPath(), childRootResultItem); - } - } - } catch (SaltFileNotExistException e) { - log.info("File does not exist,filename:{}", rootResultItem); - rootResultItem.setExists(false); - rootResultItem.setComparisonResult(FILE_COMP_DELETED); - if(filepathParts.length == 1) { - rootResultItem.setIsDir(false); - } - } - } - - String parentPath = rootFilepath; - FileQueryResultItemDto parentFileQueryResultItem = rootResultItem; - for (int index = 1; index < filepathParts.length; index++) { - String filepathPart = filepathParts[index]; - - String fullFilepath = parentPath + "/" + filepathPart; - FileQueryResultItemDto currentResultItem = pathAndFileQueryResultItems.get(fullFilepath); - if (currentResultItem == null) { - currentResultItem = new FileQueryResultItemDto(); - currentResultItem.setPath(fullFilepath); - currentResultItem.setName(filepathPart); - currentResultItem.setComparisonResult(FILE_COMP_DELETED); - currentResultItem.setExists(false); - if(index == (filepathParts.length -1)) { - currentResultItem.setIsDir(false); - }else { - currentResultItem.setIsDir(true); - } - - parentFileQueryResultItem.addFileQueryResultItem(currentResultItem); - pathAndFileQueryResultItems.put(currentResultItem.getPath(), currentResultItem); - } else { - if (currentResultItem.getExists() && currentResultItem.getIsDir()) { - List childSaltFileNodes = new ArrayList(); - try { - childSaltFileNodes = listFilesOfCurrentDirs(fullFilepath, packageEndpoint); - for (SaltFileNodeDto saltFileNode : childSaltFileNodes) { - String childFilePath = fullFilepath + "/" + saltFileNode.getName(); - FileQueryResultItemDto childRootResultItem = pathAndFileQueryResultItems.get(childFilePath); - if (childRootResultItem == null) { - childRootResultItem = new FileQueryResultItemDto(); - childRootResultItem.setExists(true); - childRootResultItem.setIsDir(saltFileNode.getIsDir()); - childRootResultItem.setMd5(saltFileNode.getMd5()); - childRootResultItem.setName(saltFileNode.getName()); - childRootResultItem.setPath(childFilePath); - - currentResultItem.addFileQueryResultItem(childRootResultItem); - pathAndFileQueryResultItems.put(childRootResultItem.getPath(), childRootResultItem); - } - } - } catch (SaltFileNotExistException e) { - log.info("File does not exist,filename:{}", rootResultItem); - currentResultItem.setExists(false); - currentResultItem.setComparisonResult(FILE_COMP_DELETED); - } - } - } - - parentFileQueryResultItem = currentResultItem; - parentPath = fullFilepath; - - } - } - - private void queryFilesForSingleFilepathNotExpendAll(String packageCiGuid, String filepath, - Map packageCiMap, String packageEndpoint, List fileQueryResultItems, - Map pathAndFileQueryResultItems) { - - List saltFileNodes = listFilesOfCurrentDirs(filepath, packageEndpoint); - for (SaltFileNodeDto saltFileNode : saltFileNodes) { - String childFilePath = filepath + "/" + saltFileNode.getName(); - FileQueryResultItemDto childRootResultItem = pathAndFileQueryResultItems.get(childFilePath); - if (childRootResultItem == null) { - childRootResultItem = new FileQueryResultItemDto(); - childRootResultItem.setExists(true); - childRootResultItem.setIsDir(saltFileNode.getIsDir()); - childRootResultItem.setMd5(saltFileNode.getMd5()); - childRootResultItem.setName(saltFileNode.getName()); - childRootResultItem.setPath(childFilePath); - - fileQueryResultItems.add(childRootResultItem); - pathAndFileQueryResultItems.put(childRootResultItem.getPath(), childRootResultItem); - } - } - - } - - private void queryFilesForSingleFilepath(String packageCiGuid, String filepath, Map packageCiMap, - String packageEndpoint, List fileQueryResultItems, - Map pathAndFileQueryResultItems, boolean expendAll) { - - if ("".equals(filepath)) { - queryFilesForRootDir(packageEndpoint, fileQueryResultItems, pathAndFileQueryResultItems); - return; - } - - if (expendAll) { - queryFilesForSingleFilepathExpendAll(packageCiGuid, filepath, packageCiMap, packageEndpoint, - fileQueryResultItems, pathAndFileQueryResultItems); - - return; - } else { - queryFilesForSingleFilepathNotExpendAll(packageCiGuid, filepath, packageCiMap, packageEndpoint, - fileQueryResultItems, pathAndFileQueryResultItems); - - return; - } - } - - private boolean checkIfSameDiffConfFiles(List newDiffConfFiles, List oldDiffConfFiles) { - if (newDiffConfFiles.size() != oldDiffConfFiles.size()) { - return false; - } - - for (ConfigFileDto configFile : newDiffConfFiles) { - if (!oldDiffConfFiles.contains(configFile.getFilename())) { - return false; - } - } - - return true; - } - - private void processDiffConfFilesIfSameDiffConfFiles(SinglePackageQueryResultDto packageUpdateResult, - Map packageCiMap, List newDiffConfFiles, List oldDiffConfFiles, - List allCmdbDiffConfigs, List diffConfVariables) { - Set toBindDiffConfVarGuids = new HashSet(); - - if (diffConfVariables == null) { - throw new PluginException("Diff conf variables cannot be null."); - } - - for (DiffConfVariableInfoDto diffConfVarInfo : diffConfVariables) { - if ((diffConfVarInfo.getBound() != null) && (diffConfVarInfo.getBound() == true)) { - toBindDiffConfVarGuids.add(diffConfVarInfo.getDiffConfigGuid()); - } - } - - this.updateDiffConfVariablesToPackageCi(packageUpdateResult.getPackageId(), toBindDiffConfVarGuids); - } - - private void processDiffConfFilesIfModifiedDiffConfFiles(SinglePackageQueryResultDto packageUpdateResult, - Map packageCiMap, List newDiffConfFiles, List oldDiffConfFiles, - List allCmdbDiffConfigs, List diffConfVariables) { - String packageGuid = packageUpdateResult.getPackageId(); - List sameDiffFiles = new ArrayList(); - List newDiffFiles = new ArrayList(); - - for (ConfigFileDto configFile : newDiffConfFiles) { - String filename = configFile.getFilename(); - if (oldDiffConfFiles.contains(filename)) { - sameDiffFiles.add(configFile); - } else { - newDiffFiles.add(configFile); - } - } - - Set boundDiffConfVarGuids = getBoundDiffConfVarGuidsFromPackage(packageCiMap); - Set toBindDiffConfVarGuids = new HashSet(); - - String s3EndpointOfPackageId = retrieveS3EndpointWithKeyByPackageCiMap(packageCiMap); - - for (ConfigFileDto configFile : sameDiffFiles) { - List saltConfigKeyInfos = calculatePropertyKeys(packageGuid, configFile.getFilename(), - s3EndpointOfPackageId); - for (SaltConfigKeyInfoDto saltConfigKeyInfo : saltConfigKeyInfos) { - ConfigKeyInfoDto configKeyInfo = new ConfigKeyInfoDto(); - configKeyInfo.setKey(saltConfigKeyInfo.getKey()); - configKeyInfo.setLine(saltConfigKeyInfo.getLine()); - configKeyInfo.setType(saltConfigKeyInfo.getType()); - - configFile.addConfigKeyInfo(configKeyInfo); - - CmdbDiffConfigDto cmdbDiffConfig = findoutFromCmdbDiffConfigsByKey(saltConfigKeyInfo.getKey(), - allCmdbDiffConfigs); - - if (cmdbDiffConfig != null && StringUtils.isNoneBlank(cmdbDiffConfig.getGuid()) - && boundDiffConfVarGuids.contains(cmdbDiffConfig.getGuid())) { - toBindDiffConfVarGuids.add(cmdbDiffConfig.getGuid()); - } - } - } - - String rootDirName = getDeployPackageRootDirName(packageCiMap); - - Set configFileKeys = new HashSet(); - for (ConfigFileDto configFile : newDiffFiles) { - SaltFileNodeDto saltFileNode = getSaltFileNodeBySingleFilepath(packageGuid, packageCiMap, - configFile.getFilename(), rootDirName); - if (saltFileNode == null) { - configFile.setComparisonResult(FILE_COMP_DELETED); - continue; -// throw new PluginException( -// String.format("Diff configuration file %s does not exist.", configFile.getFilename())); - } - String filepath = configFile.getFilename(); - if (saltFileNode.getIsDir()) { - log.info("filepath {} is directory", filepath); - continue; - } - List saltConfigKeyInfos = calculatePropertyKeys(packageGuid, configFile.getFilename(), - s3EndpointOfPackageId); - - for (SaltConfigKeyInfoDto saltConfigKeyInfo : saltConfigKeyInfos) { - ConfigKeyInfoDto configKeyInfo = new ConfigKeyInfoDto(); - configKeyInfo.setKey(saltConfigKeyInfo.getKey()); - configKeyInfo.setLine(saltConfigKeyInfo.getLine()); - configKeyInfo.setType(saltConfigKeyInfo.getType()); - - configFile.addConfigKeyInfo(configKeyInfo); - - configFileKeys.add(saltConfigKeyInfo.getKey()); - } - } - - for (String configFileKey : configFileKeys) { - CmdbDiffConfigDto cmdbDiffConfig = findoutFromCmdbDiffConfigsByKey(configFileKey, allCmdbDiffConfigs); - if (cmdbDiffConfig == null) { - CmdbDiffConfigDto newCmdbDiffConfig = this.standardCmdbEntityRestClient - .createDiffConfigurationCi(configFileKey, null); - if (newCmdbDiffConfig == null) { - throw new PluginException("Failed to create new Diff configuration key:{}", configFileKey); - } - - allCmdbDiffConfigs.add(newCmdbDiffConfig); - toBindDiffConfVarGuids.add(newCmdbDiffConfig.getGuid()); - } else { - toBindDiffConfVarGuids.add(cmdbDiffConfig.getGuid()); - } - } - - this.updateDiffConfVariablesToPackageCi(packageGuid, toBindDiffConfVarGuids); - } - - private void processDiffConfFilesIfExistDiffConfFiles(SinglePackageQueryResultDto packageUpdateResult, - Map packageCiMap, List newDiffConfFiles, List oldDiffConfFiles, - List allCmdbDiffConfigs, List diffConfVariables) { - if (checkIfSameDiffConfFiles(newDiffConfFiles, oldDiffConfFiles)) { - if(diffConfVariables == null) { - log.debug("variables to bind is null."); - return; - } - processDiffConfFilesIfSameDiffConfFiles(packageUpdateResult, packageCiMap, newDiffConfFiles, - oldDiffConfFiles, allCmdbDiffConfigs, diffConfVariables); - - return; - } else { - processDiffConfFilesIfModifiedDiffConfFiles(packageUpdateResult, packageCiMap, newDiffConfFiles, - oldDiffConfFiles, allCmdbDiffConfigs, diffConfVariables); - return; - } - - } - - private void processDiffConfFilesIfNotExistDiffConfFiles(SinglePackageQueryResultDto packageUpdateResult, - Map packageCiMap, List newDiffConfFiles, - List allCmdbDiffConfigs) { - if (newDiffConfFiles == null || newDiffConfFiles.isEmpty()) { - return; - } - - String packageGuid = packageUpdateResult.getPackageId(); - String s3EndpointOfPackageId = retrieveS3EndpointWithKeyByPackageCiMap(packageCiMap); - String rootDirName = getDeployPackageRootDirName(packageCiMap); - Set configFileKeys = new HashSet(); - for (ConfigFileDto configFile : newDiffConfFiles) { - SaltFileNodeDto saltFileNode = getSaltFileNodeBySingleFilepath(packageGuid, packageCiMap, - configFile.getFilename(), rootDirName); - if (saltFileNode == null) { - configFile.setComparisonResult(FILE_COMP_DELETED); - continue; - } - String filepath = configFile.getFilename(); - if (saltFileNode.getIsDir()) { - log.info("filepath {} is directory", filepath); - continue; - } - - List saltConfigKeyInfos = calculatePropertyKeys(packageGuid, filepath, - s3EndpointOfPackageId); - - for (SaltConfigKeyInfoDto saltConfigKeyInfo : saltConfigKeyInfos) { - ConfigKeyInfoDto configKeyInfo = new ConfigKeyInfoDto(); - configKeyInfo.setKey(saltConfigKeyInfo.getKey()); - configKeyInfo.setLine(saltConfigKeyInfo.getLine()); - configKeyInfo.setType(saltConfigKeyInfo.getType()); - - configFile.addConfigKeyInfo(configKeyInfo); - - configFileKeys.add(saltConfigKeyInfo.getKey()); - } - } - - List toBoundDiffConfVariableGuids = new ArrayList(); - for (String configFileKey : configFileKeys) { - CmdbDiffConfigDto cmdbDiffConfig = findoutFromCmdbDiffConfigsByKey(configFileKey, allCmdbDiffConfigs); - if (cmdbDiffConfig == null) { - CmdbDiffConfigDto newCmdbDiffConfig = this.standardCmdbEntityRestClient - .createDiffConfigurationCi(configFileKey, null); - if (newCmdbDiffConfig == null) { - throw new PluginException("Failed to create new Diff configuration key:{}", configFileKey); - } - - allCmdbDiffConfigs.add(newCmdbDiffConfig); - toBoundDiffConfVariableGuids.add(newCmdbDiffConfig.getGuid()); - } else { - if (!toBoundDiffConfVariableGuids.contains(cmdbDiffConfig.getGuid())) { - toBoundDiffConfVariableGuids.add(cmdbDiffConfig.getGuid()); - } - } - } - - this.updateDiffConfVariablesToPackageCi(packageGuid, toBoundDiffConfVariableGuids); - - } - - private String assembleDiffConfigFileString(List diffConfFiles) { - String diffConfigFileStr = String.join("|", diffConfFiles.stream().map(dto -> { - return dto.getFilename(); - }).collect(Collectors.toList())); - - return diffConfigFileStr; - } - - private void updateDiffConfVariablesToPackageCi(String packageCiGuid, Collection diffConfVariableGuids) { - List guids = new ArrayList(); - guids.addAll(diffConfVariableGuids); - Map packageUpdateParams = new HashMap(); - packageUpdateParams.put("guid", packageCiGuid); - packageUpdateParams.put("diff_conf_variable", guids); - this.updatePackageCi(packageUpdateParams); - } - - private void updateConfigFilesToPackageCi(SinglePackageQueryResultDto packageUpdateResult, - PackageConfigFilesUpdateRequestDto packageReqDto) { - List newDiffConfFiles = packageReqDto.getDiffConfFile(); - String diffConfigFileStr = assembleDiffConfigFileString(newDiffConfFiles); - - Map packageUpdateParams = new HashMap(); - packageUpdateParams.put("guid", packageUpdateResult.getPackageId()); - packageUpdateParams.put("deploy_file_path", assembleFileItemsToString(packageReqDto.getDeployFilePath())); - packageUpdateParams.put("start_file_path", assembleFileItemsToString(packageReqDto.getStartFilePath())); - packageUpdateParams.put("stop_file_path", assembleFileItemsToString(packageReqDto.getStopFilePath())); - packageUpdateParams.put("is_decompression", convertCmdbBooleanToString(packageReqDto.getIsDecompression())); - packageUpdateParams.put("diff_conf_file", diffConfigFileStr); - packageUpdateParams.put("baseline_package", packageReqDto.getBaselinePackage()); - - this.updatePackageCi(packageUpdateParams); - } - - private String convertCmdbBooleanToString(Boolean bool) { - if (bool == null) { - return null; - } - - return String.valueOf(bool); - } - - private List getDiffConfFilesAsStringList(Map packageCiMap) { - String diffConfFileStr = (String) packageCiMap.get("diff_conf_file"); - List diffConfFiles = new ArrayList(); - - if (StringUtils.isBlank(diffConfFileStr)) { - return diffConfFiles; - } - - String[] diffConfFileStrParts = diffConfFileStr.split("\\|"); - for (String diffConfFileStrPart : diffConfFileStrParts) { - diffConfFiles.add(diffConfFileStrPart); - } - - return diffConfFiles; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ConfigFilesSaltInfoEnricher.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ConfigFilesSaltInfoEnricher.java deleted file mode 100644 index 66f4185..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/ConfigFilesSaltInfoEnricher.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.webank.plugins.artifacts.service; - -import java.util.List; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.webank.plugins.artifacts.dto.ConfigFileDto; -import com.webank.plugins.artifacts.support.saltstack.SaltFileNodeDto; -import com.webank.plugins.artifacts.support.saltstack.SaltFileNotExistException; -import com.webank.plugins.artifacts.support.saltstack.SaltstackRemoteCallException; - -public class ConfigFilesSaltInfoEnricher { - private static final Logger log = LoggerFactory.getLogger(ConfigFilesSaltInfoEnricher.class); - private String packageGuid; - private Map packageCiMap; - private ConfigFileManagementService service; - - public ConfigFilesSaltInfoEnricher(String packageGuid, Map packageCiMap, - ConfigFileManagementService service) { - super(); - this.packageGuid = packageGuid; - this.packageCiMap = packageCiMap; - this.service = service; - } - - public List enrichFileInfoBySalt(List files) { - if (files == null || files.isEmpty()) { - return files; - } - - for (ConfigFileDto f : files) { - SaltFileNodeDto nodeDto = getSaltFileNodeBySingleFilepath(packageGuid, packageCiMap, f.getFilename()); - if (nodeDto == null) { - log.info("Cannot find salt file node for {}", f.getFilename()); - f.setComparisonResult(AbstractArtifactService.FILE_COMP_DELETED); - continue; - } - - f.setMd5(nodeDto.getMd5()); - f.setIsDir(nodeDto.getIsDir()); - } - - return files; - } - - private SaltFileNodeDto getSaltFileNodeBySingleFilepath(String packageGuid, Map packageCiMap, - String filepath) { - String s3EndpointOfPackageId = service.retrieveS3EndpointWithKeyByPackageCiMap(packageCiMap); - if(filepath.startsWith("/") && filepath.length() > 1) { - filepath = filepath.substring(1); - } - - String baseDirName = ""; - String filename = filepath; - if(filepath.indexOf("/") > 0) { - baseDirName = filepath.substring(0, filepath.lastIndexOf("/")); - filename = filepath.substring(filepath.lastIndexOf("/") + 1); - } - - try { - List saltFileNodes = service.listFilesOfCurrentDirs(baseDirName, s3EndpointOfPackageId); - for (SaltFileNodeDto dto : saltFileNodes) { - if (filename.equals(dto.getName())) { - return dto; - } - } - } catch (SaltFileNotExistException | SaltstackRemoteCallException e) { - log.info("errors while list files from salt:"+e.getMessage()); - } - - return null; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/NexusArtifactManagementService.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/NexusArtifactManagementService.java deleted file mode 100644 index 7d9d479..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/NexusArtifactManagementService.java +++ /dev/null @@ -1,443 +0,0 @@ -package com.webank.plugins.artifacts.service; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.configurationprocessor.json.JSONException; -import org.springframework.boot.configurationprocessor.json.JSONObject; -import org.springframework.stereotype.Service; -import org.springframework.web.util.UriComponentsBuilder; - -import com.webank.plugins.artifacts.commons.PluginException; -import com.webank.plugins.artifacts.dto.AutoCreateDeployPackageDto; -import com.webank.plugins.artifacts.dto.AutoCreateDeployPackageResultDto; -import com.webank.plugins.artifacts.dto.ConfigKeyInfoDto; -import com.webank.plugins.artifacts.interceptor.AuthorizationStorage; -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbDiffConfigDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiDataDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQueryResult; -import com.webank.plugins.artifacts.support.nexus.NexusAssetItemInfo; -import com.webank.plugins.artifacts.support.nexus.NexusClient; -import com.webank.plugins.artifacts.support.nexus.NexusDirectiryDto; -import com.webank.plugins.artifacts.support.nexus.NexusSearchAssetResponse; -import com.webank.plugins.artifacts.support.saltstack.SaltConfigFileDto; -import com.webank.plugins.artifacts.support.saltstack.SaltConfigKeyInfoDto; -import com.webank.plugins.artifacts.support.saltstack.SaltstackRemoteCallException; -import com.webank.plugins.artifacts.support.saltstack.SaltstackRequest.DefaultSaltstackRequest; -import com.webank.plugins.artifacts.support.saltstack.SaltstackResponse.ResultData; -import com.webank.plugins.artifacts.utils.Base64Utils; - -@Service -public class NexusArtifactManagementService extends AbstractArtifactService{ - private static final Logger log = LoggerFactory.getLogger(NexusArtifactManagementService.class); - - private static final String NEXUS_SEARCH_ASSET_API_PATH = "/service/rest/v1/search/assets"; - - @Autowired - private NexusClient nexusClient; - - public AutoCreateDeployPackageResultDto autoCreateDeployPackage(AutoCreateDeployPackageDto autoCreateDeployPackageDto, String uploadName) { - if(StringUtils.isBlank(autoCreateDeployPackageDto.getNexusUrl())) { - throw new PluginException("Nexus URL of deploy package cannot be empty.").withErrorCode("3011"); - } - - if(StringUtils.isBlank(autoCreateDeployPackageDto.getBaselinePackage())) { - throw new PluginException("Baseline package GUID cannot be empty.").withErrorCode("3012"); - } - - String baselinePackageGuid = autoCreateDeployPackageDto.getBaselinePackage(); - Map baselinePackageCi = retrievePackageCiByGuid(baselinePackageGuid); - - @SuppressWarnings("unchecked") - Map unitDesignMap = (Map) baselinePackageCi.get("unit_design"); - - if(unitDesignMap == null) { - throw new PluginException("Unit design does not exist for {}.", baselinePackageGuid) ; - } - - String unitDesignGuid = (String) unitDesignMap.get("guid"); - String isDecompression = (String) baselinePackageCi.get("is_decompression"); - - String nexusBaseUrl = applicationProperties.getArtifactsNexusServerUrl(); - String nexusRepository = applicationProperties.getArtifactsNexusRepository(); - - String nexusArtifactUrl = nexusBaseUrl+ "/repository/" +nexusRepository+"/"+autoCreateDeployPackageDto.getNexusUrl(); - - log.info("About to upload artifact to S3:{}", nexusArtifactUrl); - File artifactFile = convertNexusPackageToFile(nexusArtifactUrl,nexusArtifactUrl.substring(nexusArtifactUrl.lastIndexOf("/") + 1)); - String artifactS3URL = uploadPackageToS3(artifactFile); -// savePackageToCmdb(file, unitDesignId, uploadName, url, authorization); - - - log.info("Finished uploading artifact to S3:{}", nexusArtifactUrl); - - Map newPackageCi = new HashMap(); - - newPackageCi.put("name", artifactFile.getName()); - newPackageCi.put("deploy_package_url", artifactS3URL); - newPackageCi.put("md5_value", genMd5Value(artifactFile)); - newPackageCi.put("description", artifactFile.getName()); - newPackageCi.put("upload_user", uploadName); - newPackageCi.put("upload_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); - newPackageCi.put("unit_design", unitDesignGuid); - - newPackageCi.put("deploy_file_path", calNewPackageDeployFilePath(autoCreateDeployPackageDto, baselinePackageCi)); - newPackageCi.put("start_file_path", calNewPackageStartFilePath(autoCreateDeployPackageDto, baselinePackageCi)); - newPackageCi.put("stop_file_path", calNewPackageStopFilePath(autoCreateDeployPackageDto, baselinePackageCi)); - newPackageCi.put("diff_conf_file", calNewPackageDiffConfFile(autoCreateDeployPackageDto, baselinePackageCi)); - newPackageCi.put("is_decompression", isDecompression); - newPackageCi.put("baseline_package", baselinePackageGuid); - - List createdPackageCis = cmdbServiceV2Stub.createCiData(cmdbDataProperties.getCiTypeIdOfPackage(), newPackageCi); - if(createdPackageCis == null || createdPackageCis.isEmpty()) { - throw new PluginException("Failed to create package CI.").withErrorCode("3013"); - } - Map createdPackageCiMap = (Map) createdPackageCis.get(0); - String createPackageCiGuid = (String) createdPackageCiMap.get("guid"); - AutoCreateDeployPackageResultDto resultDto = new AutoCreateDeployPackageResultDto(); - resultDto.setGuid(createPackageCiGuid); - - Collection toBindDiffConfVarGuids = calToBindDiffConfVarGuids(newPackageCi, baselinePackageCi); - updateDiffConfVariablesToPackageCi(createPackageCiGuid, toBindDiffConfVarGuids); - log.info("Finished auto creating deploy packge:{}", createPackageCiGuid); - return resultDto; - } - - private Collection calToBindDiffConfVarGuids(Map newPackageCi, Map baselinePackageCi){ - List allCmdbDiffConfigs = getAllCmdbDiffConfigs(); - String s3EndpointOfPackageId = retrieveS3EndpointWithKeyByPackageCiMap(newPackageCi); - Set toBindDiffConfVarGuids = new HashSet(); - - Set configFileKeys = new HashSet(); - String diffConfFilePath = (String) newPackageCi.get("diff_conf_file"); - log.info("To process diff conf file path:{}", diffConfFilePath); - if(StringUtils.isBlank(diffConfFilePath)) { - return toBindDiffConfVarGuids; - } - String [] diffConfFileParts = diffConfFilePath.split("\\|"); - for (String diffConfFilePart : diffConfFileParts) { - - List saltConfigKeyInfos = calculatePropertyKeys(diffConfFilePart, - s3EndpointOfPackageId); - - for (SaltConfigKeyInfoDto saltConfigKeyInfo : saltConfigKeyInfos) { - ConfigKeyInfoDto configKeyInfo = new ConfigKeyInfoDto(); - configKeyInfo.setKey(saltConfigKeyInfo.getKey()); - configKeyInfo.setLine(saltConfigKeyInfo.getLine()); - configKeyInfo.setType(saltConfigKeyInfo.getType()); - - configFileKeys.add(saltConfigKeyInfo.getKey()); - } - } - - for (String configFileKey : configFileKeys) { - CmdbDiffConfigDto cmdbDiffConfig = findoutFromCmdbDiffConfigsByKey(configFileKey, allCmdbDiffConfigs); - if (cmdbDiffConfig == null) { - CmdbDiffConfigDto newCmdbDiffConfig = this.standardCmdbEntityRestClient - .createDiffConfigurationCi(configFileKey, null); - if (newCmdbDiffConfig == null) { - throw new PluginException("Failed to create new Diff configuration key:{}", configFileKey); - } - - allCmdbDiffConfigs.add(newCmdbDiffConfig); - toBindDiffConfVarGuids.add(newCmdbDiffConfig.getGuid()); - } else { - if (!toBindDiffConfVarGuids.contains(cmdbDiffConfig.getGuid())) { - toBindDiffConfVarGuids.add(cmdbDiffConfig.getGuid()); - } - } - } - - log.info("Total {} diff conf variables bound successfully.", toBindDiffConfVarGuids.size()); - - return toBindDiffConfVarGuids; - } - - private List calculatePropertyKeys(String filePath, - String s3EndpointOfPackageId) { - DefaultSaltstackRequest request = new DefaultSaltstackRequest(); - List> inputParamMaps = new ArrayList<>(); - Map inputParamMap = new HashMap(); - inputParamMap.put("endpoint", s3EndpointOfPackageId); - inputParamMap.put("accessKey", applicationProperties.getArtifactsS3AccessKey()); - inputParamMap.put("secretKey", applicationProperties.getArtifactsS3SecretKey()); - inputParamMap.put("filePath", filePath); - - inputParamMaps.add(inputParamMap); - request.setInputs(inputParamMaps); - ResultData resultData = null; - try { - resultData = saltstackServiceStub.getReleasedPackagePropertyKeysByFilePath( - applicationProperties.getWecubeGatewayServerUrl(), request); - } catch (SaltstackRemoteCallException e) { - log.info("errors to get conf key from {},error:{}", filePath, e.getMessage()); - return Collections.emptyList(); - } - - List saltConfigFileDtos = resultData.getOutputs(); - if (saltConfigFileDtos == null || saltConfigFileDtos.isEmpty()) { - return Collections.emptyList(); - } - - SaltConfigFileDto saltConfigFileDto = saltConfigFileDtos.get(0); - - List saltConfigKeyInfos = saltConfigFileDto.getConfigKeyInfos(); - if (saltConfigKeyInfos == null || saltConfigKeyInfos.isEmpty()) { - return Collections.emptyList(); - } - - return saltConfigKeyInfos; - } - - private CmdbDiffConfigDto findoutFromCmdbDiffConfigsByKey(String key, List allCmdbDiffConfigs) { - if (allCmdbDiffConfigs == null || allCmdbDiffConfigs.isEmpty()) { - return null; - } - - for (CmdbDiffConfigDto dto : allCmdbDiffConfigs) { - if (key.equalsIgnoreCase(dto.getKey())) { - return dto; - } - } - - return null; - } - - - private List getAllCmdbDiffConfigs() { - List diffConfigs = new ArrayList(); - List> diffConfigMaps = standardCmdbEntityRestClient.queryDiffConfigurations(); - for (Map diffConfigMap : diffConfigMaps) { - CmdbDiffConfigDto dto = new CmdbDiffConfigDto(); - dto.setDiffExpr((String) diffConfigMap.get("variable_value")); - dto.setGuid((String) diffConfigMap.get("guid")); - dto.setKey((String) diffConfigMap.get("code")); - dto.setDisplayName((String) diffConfigMap.get("displayName")); - dto.setFixedDate((String) diffConfigMap.get("fixed_date")); - - diffConfigs.add(dto); - } - - return diffConfigs; - } - - private void updateDiffConfVariablesToPackageCi(String packageCiGuid, Collection diffConfVariableGuids) { - if(diffConfVariableGuids == null || diffConfVariableGuids.isEmpty()) { - log.info("none diff conf variables to bind for package:{}", packageCiGuid); - return; - } - List guids = new ArrayList(); - guids.addAll(diffConfVariableGuids); - Map packageUpdateParams = new HashMap(); - packageUpdateParams.put("guid", packageCiGuid); - packageUpdateParams.put("diff_conf_variable", guids); - this.updatePackageCi(packageUpdateParams); - } - - private String calNewPackageDeployFilePath(AutoCreateDeployPackageDto autoCreateDeployPackageDto,Map baselinePackageCi) { - if(StringUtils.isNoneBlank(autoCreateDeployPackageDto.getDeployFilePath())) { - return autoCreateDeployPackageDto.getDeployFilePath(); - } - - String baselinePackageDeployPath = (String) baselinePackageCi.get("deploy_file_path"); - return baselinePackageDeployPath; - } - - private String calNewPackageStartFilePath(AutoCreateDeployPackageDto autoCreateDeployPackageDto,Map baselinePackageCi) { - String startFilePath = null; - if(StringUtils.isNoneBlank(autoCreateDeployPackageDto.getStartFilePath())) { - startFilePath = autoCreateDeployPackageDto.getStartFilePath(); - }else { - startFilePath = (String) baselinePackageCi.get("start_file_path"); - } - - return startFilePath; - } - - private String calNewPackageStopFilePath(AutoCreateDeployPackageDto autoCreateDeployPackageDto,Map baselinePackageCi) { - String stopFilePath = null; - if(StringUtils.isNoneBlank(autoCreateDeployPackageDto.getStopFilePath())) { - stopFilePath = autoCreateDeployPackageDto.getStopFilePath(); - }else { - stopFilePath = (String) baselinePackageCi.get("stop_file_path"); - } - - return stopFilePath; - } - - private String calNewPackageDiffConfFile(AutoCreateDeployPackageDto autoCreateDeployPackageDto,Map baselinePackageCi) { - if(StringUtils.isNoneBlank(autoCreateDeployPackageDto.getDiffConfFile())) { - return autoCreateDeployPackageDto.getDiffConfFile(); - } - - String diffConfFile = (String) baselinePackageCi.get("diff_conf_file"); - return diffConfFile; - } - - public List queryNexusDirectory(String unitDesignId, PaginationQuery queryObject){ - String artifactPath = calculateArtifactPath(unitDesignId, queryObject); - return doQueryNexusDirectory(artifactPath); - } - - public void asyncUploadNexusPackageToS3(String unitDesignId, String downloadUrl,String uploadName) { - ExecutorService executor = Executors.newFixedThreadPool(1); - String authorization = AuthorizationStorage.getIntance().get(); - executor.submit(new Runnable() { - @Override - public void run() { - try { - log.info("sync upload NEXUS package to S3 begin"); - File file = convertNexusPackageToFile(downloadUrl,downloadUrl.substring(downloadUrl.lastIndexOf("/") + 1)); - String url = uploadPackageToS3(file); - savePackageToCmdb(file, unitDesignId, uploadName, url, authorization); - log.info("sync upload NEXUS package to S3 end"); - } catch (Exception e) { - log.info("sync upload NEXUS package to S3 failed ,", e); - } - } - }); - } - - private File convertNexusPackageToFile(String downloadUrl,String fileName) { - File file = new File(fileName); - try { - URL url = new URL(downloadUrl); - URLConnection connection = url.openConnection(); - connection.setConnectTimeout(5 * 1000); - connection.setRequestProperty("Authorization","Basic " + Base64Utils.getBASE64(applicationProperties.getArtifactsNexusUsername() + ":" + applicationProperties.getArtifactsNexusPassword())); - InputStream inputStream = connection.getInputStream(); - byte[] byteArr = new byte[1024]; - int len; - FileOutputStream fos = new FileOutputStream(file); - while ((len = inputStream.read(byteArr)) != -1) { - fos.write(byteArr, 0, len); - } - fos.close(); - inputStream.close(); - } catch (IOException e) { - log.error("errors while convert nexus package file.", e); - throw new PluginException("3003", "Failed to convert Nexus package to file."); - } - return file; - } - - private List doQueryNexusDirectory(String artifactPath) { - if (artifactPath == null || artifactPath.isEmpty()) { - throw new PluginException("Upload artifact path is required."); - } - - // configuration parameters - // String filter = "jar,zip"; - - String nexusBaseUrl = applicationProperties.getArtifactsNexusServerUrl(); - String nexusRepository = applicationProperties.getArtifactsNexusRepository(); - String nexusSearchAssetApiUrl = nexusBaseUrl + NEXUS_SEARCH_ASSET_API_PATH; - UriComponentsBuilder b = UriComponentsBuilder.fromHttpUrl(nexusSearchAssetApiUrl); - b = b.queryParam("repository", nexusRepository); - String group = null; - if (StringUtils.isNoneBlank(artifactPath)) { - group = artifactPath; - if (!artifactPath.startsWith("/")) { - group = "/" + group; - } - - b = b.queryParam("group", group); - } - - List results = new ArrayList<>(); - NexusSearchAssetResponse nexusSearchAssetResponse = nexusClient.searchAsset(b.build().toUri(), - applicationProperties.getArtifactsNexusUsername(), applicationProperties.getArtifactsNexusPassword()); - - String continuationToken = nexusSearchAssetResponse.getContinuationToken(); - List assetItems = nexusSearchAssetResponse.getItems(); - for (NexusAssetItemInfo assetItem : assetItems) { - if (assetItem.getPath().endsWith("jar") || assetItem.getPath().endsWith("zip") - || assetItem.getPath().endsWith("tar") || assetItem.getPath().endsWith("gz") - || assetItem.getPath().endsWith("tgz")) { - NexusDirectiryDto directiryDto = new NexusDirectiryDto(); - directiryDto.setDownloadUrl(assetItem.getDownloadUrl()); - directiryDto - .setName(assetItem.getDownloadUrl().substring(assetItem.getDownloadUrl().lastIndexOf("/") + 1)); - results.add(directiryDto); - } - } - - while (StringUtils.isNoneBlank(continuationToken)) { - b = UriComponentsBuilder.fromHttpUrl(nexusSearchAssetApiUrl); - b = b.queryParam("repository", nexusRepository); - if(StringUtils.isNoneBlank(group)){ - b = b.queryParam("group", group); - } - b = b.queryParam("continuationToken", continuationToken); - - nexusSearchAssetResponse = nexusClient.searchAsset(b.build().toUri(), - applicationProperties.getArtifactsNexusUsername(), - applicationProperties.getArtifactsNexusPassword()); - - List queryAssetItems = nexusSearchAssetResponse.getItems(); - for (NexusAssetItemInfo assetItem : queryAssetItems) { - if (assetItem.getPath().endsWith("jar") || assetItem.getPath().endsWith("zip") - || assetItem.getPath().endsWith("tar") || assetItem.getPath().endsWith("gz") - || assetItem.getPath().endsWith("tgz")) { - NexusDirectiryDto directiryDto = new NexusDirectiryDto(); - directiryDto.setDownloadUrl(assetItem.getDownloadUrl()); - directiryDto.setName( - assetItem.getDownloadUrl().substring(assetItem.getDownloadUrl().lastIndexOf("/") + 1)); - results.add(directiryDto); - } - } - - continuationToken = nexusSearchAssetResponse.getContinuationToken(); - } - return results; - - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - private String calculateArtifactPath(String unitDesignId, PaginationQuery queryObject) { - if (StringUtils.isBlank(unitDesignId)) { - throw new PluginException("Unit design ID cannot be blank."); - } - String artifactPath = null; - queryObject.addEqualsFilter("guid", unitDesignId); - PaginationQueryResult objectPaginationQueryResult = cmdbServiceV2Stub - .queryCiData(cmdbDataProperties.getCiTypeIdOfUnitDesign(), queryObject); - - if (objectPaginationQueryResult == null || objectPaginationQueryResult.getContents() == null - || objectPaginationQueryResult.getContents().size() <= 0) { - return artifactPath; - } - try { - Map ResultMap = (Map) objectPaginationQueryResult.getContents().get(0); - JSONObject responseJson = (JSONObject) JSONObject.wrap(ResultMap.get("data")); - // JSONObject unit_design = - // responseJson.getJSONObject(applicationProperties.getCmdbArtifactPath()); - artifactPath = responseJson.getString(applicationProperties.getCmdbArtifactPath()); - } catch (JSONException e) { - log.error("Can not parse CMDB Response json", e); - throw new PluginException("3006","Cannot find Nexus path from CMDB.Please configure Nexus path in CMDB."); - } - return artifactPath; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/helper/HttpServletResponseWriter.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/helper/HttpServletResponseWriter.java deleted file mode 100644 index 0e05f36..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/helper/HttpServletResponseWriter.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.webank.plugins.artifacts.service.helper; - -import java.util.List; -import java.util.Map; - -import javax.servlet.ServletOutputStream; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.http.HttpHeaders; -import org.springframework.http.ResponseEntity; - -import com.webank.plugins.artifacts.commons.PluginException; - -public class HttpServletResponseWriter { - - private HttpServletResponse response; - - public HttpServletResponseWriter(HttpServletResponse response) { - this.response = response; - } - - public void writeHttpResponse(ResponseEntity responseEntity) { - response.setStatus(responseEntity.getStatusCodeValue()); - writeHeaders(responseEntity.getHeaders()); - writeBody(responseEntity.getBody()); - } - - private void writeHeaders(HttpHeaders headers) { - if (headers == null || headers.isEmpty()) - return; - for (Map.Entry> headerEntry : headers.entrySet()) { - String headerName = headerEntry.getKey(); - List headerValues = headerEntry.getValue(); - if (headerValues != null && !headerValues.isEmpty()) { - headerValues.forEach(headerValue -> response.addHeader(headerName, headerValue)); - } - } - } - - private void writeBody(byte[] body) { - if (body == null) - return; - - ServletOutputStream outputStream = null; - try { - outputStream = response.getOutputStream(); - outputStream.write(body); - outputStream.flush(); - } catch (Exception e) { - throw new PluginException(String.format("Failed to write http servlet response data due to %s ", e.getMessage())); - } finally { - try { - if (outputStream != null) { - outputStream.close(); - } - } catch (Exception e) { - } - } - } - -} \ No newline at end of file diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/helper/ProxyExchange.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/helper/ProxyExchange.java deleted file mode 100644 index f3559f4..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/service/helper/ProxyExchange.java +++ /dev/null @@ -1,165 +0,0 @@ -package com.webank.plugins.artifacts.service.helper; - -import static org.springframework.http.HttpMethod.valueOf; -import static org.springframework.http.RequestEntity.method; - -import java.net.URI; -import java.net.URISyntaxException; -import java.util.Arrays; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.HttpHeaders; -import org.springframework.http.RequestEntity; -import org.springframework.http.RequestEntity.BodyBuilder; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; - -public class ProxyExchange { - private static Set DEFAULT_SENSITIVE = new HashSet<>(Arrays.asList("cookie", "authorization")); - - private RestTemplate rest; - - private HttpServletRequest request; - private HttpServletResponse response; - private Object body; - private Set sensitiveHeaders; - private HttpHeaders httpHeaders; - private URI proxyUri; - private URI targetUri; - - public ProxyExchange(RestTemplate rest, HttpServletRequest request, HttpServletResponse response) { - this.rest = rest; - this.request = request; - this.response = response; - - this.proxyUri = toURI(request.getRequestURL().toString() + getQueryString(request)); - this.httpHeaders = readRequestHeaders(request); - } - - private String getQueryString(HttpServletRequest request2) { - if (StringUtils.isNotBlank(request.getQueryString())) { - return "?" + request.getQueryString(); - } - return ""; - } - - public ProxyExchange body(Object body) { - this.body = body; - return this; - } - - public ProxyExchange customHttpHeaders(Map headerMap) { - for (Map.Entry header : headerMap.entrySet()) { - httpHeaders.set(header.getKey(), header.getValue()); - } - return this; - } - - public ProxyExchange header(String name, String... value) { - this.httpHeaders.put(name, Arrays.asList(value)); - return this; - } - - public ProxyExchange sensitiveHeaders(Set sensitiveHeaders) { - this.sensitiveHeaders = sensitiveHeaders; - return this; - } - - public ProxyExchange targetUri(String uri) { - this.targetUri = toURI(uri); - return this; - } - - public String path() { - return this.request.getServletPath(); - } - - public String path(String prefix) { - String path = this.path() + getQueryString(this.request); - if (!path.startsWith(prefix)) { - throw new IllegalArgumentException("Path does not start with prefix (" + prefix + "): " + path); - } else { - return path; - } - } - - public void exchange() { - BodyBuilder httpBodyBuilder = method(valueOf(request.getMethod()), this.targetUri); - applyHeaders(httpBodyBuilder); - RequestEntity requestEntity = this.body == null ? httpBodyBuilder.build() : httpBodyBuilder.body(this.body); - ResponseEntity responseEntity = this.rest.exchange(requestEntity, ParameterizedTypeReference.forType(byte[].class)); - new HttpServletResponseWriter(response).writeHttpResponse(responseEntity); - } - - private void applyHeaders(BodyBuilder builder) { - Set sensitive = this.sensitiveHeaders; - if (sensitive == null) { - sensitive = DEFAULT_SENSITIVE; - } - this.appendForwarded(proxyUri); - this.appendXForwarded(proxyUri); - - for (String name : this.httpHeaders.keySet()) { - if (!sensitive.contains(name.toLowerCase())) { - builder.header(name, (String[]) this.httpHeaders.get(name).toArray(new String[0])); - } - } - } - - private HttpHeaders readRequestHeaders(HttpServletRequest request) { - HttpHeaders headers = new HttpHeaders(); - Enumeration headerNames = request.getHeaderNames(); - while (headerNames.hasMoreElements()) { - String headerName = headerNames.nextElement(); - headers.put(headerName, Collections.list(request.getHeaders(headerName))); - } - return headers; - } - - private void appendXForwarded(URI uri) { - boolean success = appendHeader("x-forwarded-host", uri.getHost(), true); - if (success) { - appendHeader("x-forwarded-proto", uri.getScheme(), true); - } - } - - private void appendForwarded(URI uri) { - appendHeader("forwarded", this.forwarded(uri), false); - } - - private boolean appendHeader(String headerName, String appendHeaderValue, boolean skipIfAbsent) { - String headerValue = this.httpHeaders.getFirst(headerName); - if (headerValue != null) { - this.httpHeaders.set(headerName, headerValue + "," + appendHeaderValue); - return true; - } else { - if (skipIfAbsent) { - return false; - } else { - this.httpHeaders.set(headerName, appendHeaderValue); - return true; - } - } - } - - private String forwarded(URI uri) { - return "http".equals(uri.getScheme()) ? "host=" + uri.getHost() : String.format("host=%s;proto=%s", uri.getHost(), uri.getScheme()); - } - - private URI toURI(String uri) { - try { - return new URI(uri); - } catch (URISyntaxException e) { - throw new IllegalStateException("Cannot create URI : " + uri, e); - } - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/RemoteCallException.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/RemoteCallException.java deleted file mode 100644 index 2befe8b..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/RemoteCallException.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.webank.plugins.artifacts.support; - -public abstract class RemoteCallException extends RuntimeException { - - public RemoteCallException(String message) { - super(message); - } - - public RemoteCallException(String message, Throwable cause) { - super(message, cause); - } - - public abstract String getErrorMessage(); - - public abstract Object getErrorData(); -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbDataNotFoundException.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbDataNotFoundException.java deleted file mode 100644 index 567b696..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbDataNotFoundException.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb; - -public class CmdbDataNotFoundException extends CmdbRemoteCallException { - private static final long serialVersionUID = 1L; - - public CmdbDataNotFoundException(String message) { - super(message); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbRemoteCallException.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbRemoteCallException.java deleted file mode 100644 index 7be8344..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbRemoteCallException.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb; - -import com.webank.plugins.artifacts.support.RemoteCallException; -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbResponse; - -public class CmdbRemoteCallException extends RemoteCallException { - - private transient CmdbResponse cmdbResponse; - - public CmdbRemoteCallException(String message) { - super(message); - } - - public CmdbRemoteCallException(String message, CmdbResponse cmdbResponse) { - super(message); - this.cmdbResponse = cmdbResponse; - } - - public CmdbRemoteCallException(String message, CmdbResponse cmdbResponse, Throwable cause) { - super(message, cause); - this.cmdbResponse = cmdbResponse; - } - - public CmdbResponse getCmdbResponse() { - return cmdbResponse; - } - - @Override - public String getErrorMessage() { - return String.format("%s (CMDB_ERROR_CODE: %s)", this.getMessage(), getStatusCode(cmdbResponse)); - } - - @Override - public Object getErrorData() { - return cmdbResponse == null ? null : cmdbResponse.getData(); - } - - private String getStatusCode(CmdbResponse cmdbResponse) { - return cmdbResponse == null ? null : cmdbResponse.getStatusCode(); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbRestTemplate.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbRestTemplate.java deleted file mode 100644 index 89f7693..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbRestTemplate.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb; - -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbResponse; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.InputStreamSource; -import org.springframework.http.*; -import org.springframework.stereotype.Component; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; -import org.springframework.web.client.RestTemplate; - -import java.util.Arrays; - -import static org.springframework.http.HttpMethod.GET; - -@Slf4j -@Component -public class CmdbRestTemplate { - - @Autowired - private RestTemplate restTemplate; - - @SuppressWarnings("unchecked") - public D get(String targetUrl, Class responseType) { - log.info("About to call {} ", targetUrl); - R cmdbResponse = restTemplate.getForObject(targetUrl, responseType); - log.info("CMDB response: {} ", cmdbResponse); - validateCmdbResponse(cmdbResponse); - return (D) cmdbResponse.getData(); - } - - public D postForResponse(String targetUrl, Class responseType) { - return postForResponse(targetUrl, null, responseType); - } - - @SuppressWarnings("unchecked") - public D postForResponse(String targetUrl, Object postObject, Class responseType) { - log.info("About to POST {} with postObject {}", targetUrl, postObject); - R cmdbResponse = restTemplate.postForObject(targetUrl, postObject, responseType); - - log.info("CMDB response: {} ", cmdbResponse); - validateCmdbResponse(cmdbResponse); - return (D) cmdbResponse.getData(); - } - - public D uploadSingleFile(String targetUrl, String fileProperty, InputStreamSource inputStreamSource, Class responseType) { - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.MULTIPART_FORM_DATA); - - MultiValueMap body = new LinkedMultiValueMap<>(); - body.add(fileProperty, inputStreamSource); - - return postForResponse(targetUrl, new HttpEntity<>(body, headers), responseType); - } - - public ResponseEntity downloadSingleFile(String targetUrl) { - HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM)); - - ResponseEntity response = restTemplate.exchange(targetUrl, GET, new HttpEntity<>(headers), byte[].class); - validateCmdbResponse(response); - return response; - } - - private void validateCmdbResponse(CmdbResponse cmdbResponse) { - validateCmdbResponse(cmdbResponse, true); - } - - private void validateCmdbResponse(CmdbResponse cmdbResponse, boolean dataRequired) { - if (cmdbResponse == null) { - throw new CmdbRemoteCallException("CMDB call failure due to no response."); - } - if (!CmdbResponse.STATUS_CODE_OK.equalsIgnoreCase(cmdbResponse.getStatusCode())) { - throw new CmdbRemoteCallException("CMDB Error: " + cmdbResponse.getStatusMessage(), cmdbResponse); - } - if (dataRequired && cmdbResponse.getData() == null) { - throw new CmdbRemoteCallException("CMDB call failure due to unexpected empty response.", cmdbResponse); - } - } - - private void validateCmdbResponse(ResponseEntity cmdbResponse) { - validateCmdbResponse(cmdbResponse, true); - } - - private void validateCmdbResponse(ResponseEntity cmdbResponse, boolean dataRequired) { - if (cmdbResponse == null) { - throw new CmdbRemoteCallException("CMDB call failure due to no response."); - } - if (!HttpStatus.OK.equals(cmdbResponse.getStatusCode())) { - throw new CmdbRemoteCallException("CMDB call failure due to unexpected response - " + cmdbResponse); - } - if (dataRequired && cmdbResponse.getBody() == null) { - throw new CmdbRemoteCallException("CMDB call failure due to unexpected empty response."); - } - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbServiceV2Stub.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbServiceV2Stub.java deleted file mode 100644 index a803ddf..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/CmdbServiceV2Stub.java +++ /dev/null @@ -1,208 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb; - -import static com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery.defaultQueryObject; -import static org.apache.commons.collections4.CollectionUtils.isNotEmpty; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.Resource; -import org.springframework.stereotype.Service; - -import com.webank.plugins.artifacts.commons.ApplicationProperties; -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbResponse.DefaultCmdbResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CatCodeDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CategoryDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiDataDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiDataTreeDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiTypeAttrDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CiTypeDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.CiDataQueryResultResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.CiDataVersionDetailResultResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.CiTypeAttributeQueryResultResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.CiTypeQueryResultResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.EnumCategoryQueryResultResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.EnumCodeListResultResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.EnumCodeQueryResultResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.CmdbResponses.SpecialConnectorDtoResponse; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.ImageInfoDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.OperateCiDto; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQuery; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.PaginationQueryResult; - -@Service -public class CmdbServiceV2Stub { - - private static final String pluginContextPath = "/wecmdb"; - - private static final String CONSTANT_CI_TYPE_ID = "ciTypeId"; - - private static final String CONSTANT_CAT_ID = "catId"; - private static final String CONSTANT_SEQ_NO = "seqNo"; - private static final String CONSTANT_ATTRIBUTES = "attributes"; - private static final String CONSTANT_OPERATION = "?operation="; - - private static final String API_VERSION = "/api/v2"; - - private static final String FILE_UPLOAD = "/image/upload"; - - private static final String ENUM_CATEGORY_QUERY = "/enum/cats/retrieve"; - - private static final String ENUM_CODE_CREATE = "/enum/codes/create"; - private static final String ENUM_CODE_QUERY = "/enum/codes/retrieve"; - - private static final String CITYPE_ATTRIBUTE_QUERY = "/ciTypeAttrs/retrieve"; - - - private static final String CITYPE_QUERY = "/ciTypes/retrieve"; - - private static final String CIDATA_CREATE = "/ci/%d/create"; - private static final String CIDATA_QUERY = "/ci/%d/retrieve"; - private static final String CIDATA_UPDATE = "/ci/%d/update"; - private static final String CIDATA_DELETE = "/ci/%d/delete"; - private static final String CIDATA_STATE_OPERATE = "/ci/state/operate"; - private static final String CIDATA_RETRIEVE_VERSION_DETAIL = "/ci/from/%d/to/%d/versions/%s/retrieve"; - private static final String QUERY_SPECIAL_CONNECTOR = "/static-data/special-connector"; - - @Autowired - private CmdbRestTemplate template; - - @Autowired - private ApplicationProperties applicationProperties; - - public Integer uploadFile(Resource inputStreamSource) { - ImageInfoDto imageInfo = template.uploadSingleFile(asCmdbUrl(FILE_UPLOAD), "img", inputStreamSource, - CmdbResponses.ImageInfoResponse.class); - return imageInfo.getId(); - } - - public CategoryDto getEnumCategoryByName(String categoryName) { - return findFirst(ENUM_CATEGORY_QUERY, "catName", categoryName, EnumCategoryQueryResultResponse.class); - } - - public List createEnumCodes(CatCodeDto... catCodeDtos) { - return create(ENUM_CODE_CREATE, catCodeDtos, EnumCodeListResultResponse.class); - } - - public PaginationQueryResult queryEnumCodes(PaginationQuery queryObject) { - return query(ENUM_CODE_QUERY, queryObject, EnumCodeQueryResultResponse.class); - } - - public List getEnumCodesByCategoryId(Integer categoryId) { - PaginationQueryResult queryResult = query(ENUM_CODE_QUERY, - defaultQueryObject(CONSTANT_CAT_ID, categoryId).ascendingSortBy(CONSTANT_SEQ_NO), EnumCodeQueryResultResponse.class); - return queryResult.getContents(); - } - - public List getAllCiTypes(boolean withAttributes, String status) { - PaginationQuery paginationQuery = defaultQueryObject(); - - if (status != null) { - paginationQuery = paginationQuery.addInFilter("status", Arrays.asList(status.split(","))); - } - - if (withAttributes) { - paginationQuery = paginationQuery.addReferenceResource(CONSTANT_ATTRIBUTES); - if (status != null) { - paginationQuery = paginationQuery.addInFilter("attributes.status", Arrays.asList(status.split(","))); - } - } - - PaginationQueryResult queryResult = query(CITYPE_QUERY, paginationQuery.ascendingSortBy(CONSTANT_SEQ_NO), - CiTypeQueryResultResponse.class); - return queryResult.getContents(); - } - - public List createCiData(Integer ciTypeId, Object ciData) { - ArrayList ciDatas = new ArrayList(); - ciDatas.add(ciData); - return create(formatString(CIDATA_CREATE, ciTypeId), ciDatas.toArray(), DefaultCmdbResponse.class); - } - - public PaginationQueryResult queryCiData(Integer ciTypeId, PaginationQuery queryObject) { - return query(formatString(CIDATA_QUERY, ciTypeId), queryObject, CiDataQueryResultResponse.class); - } - - public List getCiDataDetailForVersion(int fromCiTypeId, int toCiTypeId, String version) { - return query(formatString(CIDATA_RETRIEVE_VERSION_DETAIL, fromCiTypeId, toCiTypeId, version), - defaultQueryObject(), CiDataVersionDetailResultResponse.class); - } - - public List updateCiData(Integer ciTypeId, Map ciData) { - List> ciDatas = new ArrayList>(); - ciDatas.add(ciData); - return update(formatString(CIDATA_UPDATE, ciTypeId), ciDatas.toArray(), DefaultCmdbResponse.class); - } - - public Object deleteCiData(Integer ciTypeId, List ids) { - return delete(formatString(CIDATA_DELETE, ciTypeId), ids.toArray(), DefaultCmdbResponse.class); - } - - public Object operateCiForState(List operateCiObject, String operation) { - return template.postForResponse(asCmdbUrl(CIDATA_STATE_OPERATE) + CONSTANT_OPERATION + operation, operateCiObject, DefaultCmdbResponse.class); - } - - private > D findFirst(String url, String field, Object value, Class responseType) { - return findFirst(url, defaultQueryObject(field, value), responseType, true); - } - - private > D findFirst(String url, PaginationQuery queryObject, Class responseType, boolean dataRequired) { - String targetUrl = asCmdbUrl(url); - PaginationQueryResult result = template.postForResponse(targetUrl, queryObject, responseType); - List dataContent = result.getContents(); - if (isNotEmpty(dataContent)) - return dataContent.get(0); - if (dataRequired) - throw new CmdbDataNotFoundException(String.format("Data not found in location [%s] with query parameter %s", url, queryObject)); - return null; - } - - private > D create(String url, Object[] createObject, Class responseType) { - return template.postForResponse(asCmdbUrl(url), createObject, responseType); - } - - private > D query(String url, PaginationQuery queryObject, Class responseType) { - return template.postForResponse(asCmdbUrl(url), queryObject, responseType); - } - - private > D update(String url, Object[] updateObject, Class responseType) { - return template.postForResponse(asCmdbUrl(url), updateObject, responseType); - } - - private > D delete(String url, Object[] ids, Class responseType) { - return template.postForResponse(asCmdbUrl(url), ids, responseType); - } - - private String asCmdbUrl(String path, Object... pathVariables) { - if (pathVariables != null && pathVariables.length > 0) { - path = String.format(path, pathVariables); - } - return applicationProperties.getWecubeGatewayServerUrl() + pluginContextPath + API_VERSION + path; - } - - private String formatString(String path, Object... pathVariables) { - if (pathVariables != null && pathVariables.length > 0) { - path = String.format(path, pathVariables); - } - return path; - } - public List queryCiTypeAttributes(PaginationQuery queryObject) { - PaginationQueryResult queryResult = query(CITYPE_ATTRIBUTE_QUERY, queryObject, CiTypeAttributeQueryResultResponse.class); - return queryResult.getContents(); - } - - public List getCiTypeAttributesByCiTypeId(Integer ciTypeId) { - return queryCiTypeAttributes(defaultQueryObject(CONSTANT_CI_TYPE_ID, ciTypeId).ascendingSortBy("displaySeqNo")); - } - - public List getSpecialConnector() { - List queryResult = template.get(asCmdbUrl(QUERY_SPECIAL_CONNECTOR), SpecialConnectorDtoResponse.class); - return queryResult; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/StandardCmdbEntityRestClient.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/StandardCmdbEntityRestClient.java deleted file mode 100644 index 347edde..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/StandardCmdbEntityRestClient.java +++ /dev/null @@ -1,168 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb; - -import java.net.URI; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.webank.plugins.artifacts.commons.ApplicationProperties; -import com.webank.plugins.artifacts.commons.PluginException; -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbDiffConfigDto; -import com.webank.plugins.artifacts.support.cmdb.dto.EntityQuerySpecification; -import com.webank.plugins.artifacts.support.cmdb.dto.StandardCmdbEntityResponseDto; - -@Service -public class StandardCmdbEntityRestClient { - private static final Logger log = LoggerFactory.getLogger(StandardCmdbEntityRestClient.class); - private static final String QUERY_DIFF_CONFIGURATION = "/wecmdb/entities/diff_configuration/query"; - private static final String CREATE_REQUEST_URL = "/wecmdb/entities/{entity-name}/create"; - private static final String UPDATE_REQUEST_URL = "/wecmdb/entities/{entity-name}/update"; - - @Autowired - private RestTemplate restTemplate; - - @Autowired - private ApplicationProperties applicationProperties; - - private ObjectMapper objectMapper = new ObjectMapper(); - - public void updateDiffConfigurationCi(List> ciAttrsMapToUpdate ) { - String entityName = "diff_configuration"; - this.updateEntity(entityName, ciAttrsMapToUpdate); - } - - @SuppressWarnings("unchecked") - public CmdbDiffConfigDto createDiffConfigurationCi(String varName, String varValue) { - String entityName = "diff_configuration"; - List> requestParams = new ArrayList<>(); - Map requestDataMap = new HashMap<>(); - requestDataMap.put("code", varName); - requestDataMap.put("variable_name", varName); - if(StringUtils.isNoneBlank(varValue)){ - requestDataMap.put("variable_value", varValue); - } - - requestParams.add(requestDataMap); - Object result = createEntity(entityName, requestParams); - if(result == null){ - return null; - } - if (result instanceof List) { - List resultList = (List) result; - if (resultList.size() > 0) { - Map diffConfigMap = (Map) resultList.get(0); - CmdbDiffConfigDto dto = new CmdbDiffConfigDto(); - dto.setDiffExpr((String) diffConfigMap.get("variable_value")); - dto.setGuid((String) diffConfigMap.get("guid")); - dto.setKey((String) diffConfigMap.get("code")); - dto.setDisplayName((String) diffConfigMap.get("displayName")); - return dto; - } - - }else if(result instanceof Map){ - Map diffConfigMap = (Map) result; - CmdbDiffConfigDto dto = new CmdbDiffConfigDto(); - dto.setDiffExpr((String) diffConfigMap.get("variable_value")); - dto.setGuid((String) diffConfigMap.get("guid")); - dto.setKey((String) diffConfigMap.get("code")); - dto.setDisplayName((String) diffConfigMap.get("displayName")); - return dto; - } - - return null; - } - - public Object updateEntity(String entityName, List> requestParams) { - String requestUriStr = String.format("%s%s", applicationProperties.getWecubeGatewayServerUrl(), - UPDATE_REQUEST_URL); - URI requestUri = restTemplate.getUriTemplateHandler().expand(requestUriStr, entityName); - - long timeMilliSeconds = System.currentTimeMillis(); - if (log.isInfoEnabled()) { - log.info("SEND QUERY post [{}] url={}, request={}", timeMilliSeconds, requestUri.toString(), - toJson(requestParams)); - } - StandardCmdbEntityResponseDto result = restTemplate.postForObject(requestUri, requestParams, - StandardCmdbEntityResponseDto.class); - - if (log.isInfoEnabled()) { - log.info("RECEIVE QUERY post [{}] url={},result={}", timeMilliSeconds, requestUri.toString(), result); - } - - if (StandardCmdbEntityResponseDto.STATUS_ERROR.equals(result.getStatus())) { - throw new PluginException(result.getMessage()); - } - - return result.getData(); - } - - public Object createEntity(String entityName, List> requestParams) { - String requestUriStr = String.format("%s%s", applicationProperties.getWecubeGatewayServerUrl(), - CREATE_REQUEST_URL); - URI requestUri = restTemplate.getUriTemplateHandler().expand(requestUriStr, entityName); - - long timeMilliSeconds = System.currentTimeMillis(); - if (log.isInfoEnabled()) { - log.info("SEND QUERY post [{}] url={}, request={}", timeMilliSeconds, requestUri.toString(), - toJson(requestParams)); - } - StandardCmdbEntityResponseDto result = restTemplate.postForObject(requestUri, requestParams, - StandardCmdbEntityResponseDto.class); - - if (log.isInfoEnabled()) { - log.info("RECEIVE QUERY post [{}] url={},result={}", timeMilliSeconds, requestUri.toString(), result); - } - - if (StandardCmdbEntityResponseDto.STATUS_ERROR.equals(result.getStatus())) { - throw new PluginException(result.getMessage()); - } - - return result.getData(); - } - - @SuppressWarnings("unchecked") - public List> queryDiffConfigurations() { - EntityQuerySpecification spec = new EntityQuerySpecification(); - StandardCmdbEntityResponseDto respDto = query(spec); - log.info("data:{}", respDto.getData()); - return (List>) respDto.getData(); - } - - protected StandardCmdbEntityResponseDto query(EntityQuerySpecification querySpec) { - String requestUriStr = String.format("%s%s", applicationProperties.getWecubeGatewayServerUrl(), - QUERY_DIFF_CONFIGURATION); - URI requestUri = restTemplate.getUriTemplateHandler().expand(requestUriStr); - - long timeMilliSeconds = System.currentTimeMillis(); - if (log.isInfoEnabled()) { - log.info("SEND QUERY post [{}] url={}, request={}", timeMilliSeconds, requestUri.toString(), - toJson(querySpec)); - } - StandardCmdbEntityResponseDto result = restTemplate.postForObject(requestUri, querySpec, - StandardCmdbEntityResponseDto.class); - if (log.isDebugEnabled()) { - log.debug("RECEIVE QUERY post [{}] url={},result={}", timeMilliSeconds, requestUri.toString(), result); - } - return result; - } - - private String toJson(Object value) { - try { - String json = objectMapper.writeValueAsString(value); - return json; - } catch (JsonProcessingException e) { - log.info("errors to convert json object", e); - return ""; - } - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/CmdbDiffConfigDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/CmdbDiffConfigDto.java deleted file mode 100644 index 0d824b2..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/CmdbDiffConfigDto.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto; - -public class CmdbDiffConfigDto { - private String guid; - private String key; - private String diffExpr; - private String displayName; - private String fixedDate; - - public String getGuid() { - return guid; - } - - public void setGuid(String guid) { - this.guid = guid; - } - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String getDiffExpr() { - return diffExpr; - } - - public void setDiffExpr(String diffExpr) { - this.diffExpr = diffExpr; - } - - public String getDisplayName() { - return displayName; - } - - public void setDisplayName(String displayName) { - this.displayName = displayName; - } - - - - public String getFixedDate() { - return fixedDate; - } - - public void setFixedDate(String fixedDate) { - this.fixedDate = fixedDate; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("CmdbDiffConfigDto [guid="); - builder.append(guid); - builder.append(", key="); - builder.append(key); - builder.append(", diffExpr="); - builder.append(diffExpr); - builder.append(", displayName="); - builder.append(displayName); - builder.append("]"); - return builder.toString(); - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/CmdbResponse.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/CmdbResponse.java deleted file mode 100644 index 30b0cf5..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/CmdbResponse.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto; - -import lombok.Data; - -import java.util.List; - -@Data -public class CmdbResponse { - public static final String STATUS_CODE_OK = "OK"; - - private String statusCode; - private String statusMessage; - - private DATATYPE data; - - public static class DefaultCmdbResponse extends CmdbResponse { - } - - public static class IntegerCmdbResponse extends CmdbResponse { - } - - public static class ListDataResponse extends CmdbResponse { - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQueryCriteria.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQueryCriteria.java deleted file mode 100644 index d18a28b..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQueryCriteria.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto; - -import com.fasterxml.jackson.annotation.JsonInclude; - -@JsonInclude(JsonInclude.Include.NON_NULL) -public class EntityQueryCriteria { - - private String attrName; - private String condition; - - public EntityQueryCriteria() { - super(); - } - - public EntityQueryCriteria(String attrName, String condition) { - super(); - this.attrName = attrName; - this.condition = condition; - } - - public String getAttrName() { - return attrName; - } - - public void setAttrName(String attrName) { - this.attrName = attrName; - } - - public String getCondition() { - return condition; - } - - public void setCondition(String condition) { - this.condition = condition; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("[attrName="); - builder.append(attrName); - builder.append(", condition="); - builder.append(condition); - builder.append("]"); - return builder.toString(); - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQueryFilter.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQueryFilter.java deleted file mode 100644 index 22b1cb9..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQueryFilter.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto; - -import com.fasterxml.jackson.annotation.JsonInclude; - -/** - * - * @author gavin - * - */ -@JsonInclude(JsonInclude.Include.NON_NULL) -public class EntityQueryFilter { - public static final String OP_EQUALS = "eq"; - public static final String OP_NOT_EQUALS = "neq"; - public static final String OP_IN = "in"; - public static final String OP_LIKE = "like"; - public static final String OP_GREAT_THAN = "gt"; - public static final String OP_LESS_THAN = "lt"; - public static final String OP_IS = "is"; - public static final String OP_IS_NOT = "isnot"; - private String attrName; - private String op; - private Object condition; - - public String getOp() { - return op; - } - - public void setOp(String op) { - this.op = op; - } - - public Object getCondition() { - return condition; - } - - public void setCondition(Object condition) { - this.condition = condition; - } - - - - public String getAttrName() { - return attrName; - } - - public void setAttrName(String attrName) { - this.attrName = attrName; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("[attrName="); - builder.append(attrName); - builder.append(", op="); - builder.append(op); - builder.append(", condition="); - builder.append(condition); - builder.append("]"); - return builder.toString(); - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQuerySpecification.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQuerySpecification.java deleted file mode 100644 index a419992..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/EntityQuerySpecification.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto; - -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonInclude; - -/** - * - * @author gavin - * - */ -@JsonInclude(JsonInclude.Include.NON_NULL) -public class EntityQuerySpecification { - - - private EntityQueryCriteria criteria; - private List additionalFilters = new ArrayList<>(); - - public EntityQueryCriteria getCriteria() { - return criteria; - } - - public void setCriteria(EntityQueryCriteria criteria) { - this.criteria = criteria; - } - - public List getAdditionalFilters() { - return additionalFilters; - } - - public void setAdditionalFilters(List additionalFilters) { - this.additionalFilters = additionalFilters; - } - - public EntityQuerySpecification addAdditionalFilters(EntityQueryFilter... additionalFilters) { - if (this.additionalFilters == null) { - this.additionalFilters = new ArrayList<>(); - } - - for (EntityQueryFilter f : additionalFilters) { - this.additionalFilters.add(f); - } - - return this; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("[criteria="); - builder.append(criteria); - builder.append(", additionalFilters="); - builder.append(additionalFilters); - builder.append("]"); - return builder.toString(); - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/StandardCmdbEntityResponseDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/StandardCmdbEntityResponseDto.java deleted file mode 100755 index 26189f8..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/StandardCmdbEntityResponseDto.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto; - -public class StandardCmdbEntityResponseDto { - public final static String STATUS_OK = "OK"; - public final static String STATUS_ERROR = "ERROR"; - - private String status; - private String message; - private Object data; - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public StandardCmdbEntityResponseDto withData(Object data){ - this.data = data; - return this; - } - - public static StandardCmdbEntityResponseDto okay() { - StandardCmdbEntityResponseDto result = new StandardCmdbEntityResponseDto(); - result.setStatus(STATUS_OK); - result.setMessage("Success"); - return result; - } - - public static StandardCmdbEntityResponseDto okayWithData(Object data) { - return okay().withData(data); - } - - public static StandardCmdbEntityResponseDto error(String errorMessage) { - StandardCmdbEntityResponseDto result = new StandardCmdbEntityResponseDto(); - result.setStatus(STATUS_ERROR); - result.setMessage(errorMessage); - return result; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("[status="); - builder.append(status); - builder.append(", message="); - builder.append(message); - builder.append(", data="); - builder.append(data); - builder.append("]"); - return builder.toString(); - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/AbstractResourceDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/AbstractResourceDto.java deleted file mode 100644 index 7de0669..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/AbstractResourceDto.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; - -import lombok.EqualsAndHashCode; - -@EqualsAndHashCode -@JsonInclude(Include.NON_EMPTY) -public abstract class AbstractResourceDto { - private String callbackId; - private String errorMessage; - - public String getCallbackId() { - return callbackId; - } - - public void setCallbackId(String callbackId) { - this.callbackId = callbackId; - } - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CatCodeDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CatCodeDto.java deleted file mode 100644 index 46ce730..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CatCodeDto.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; - -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -@JsonInclude(Include.NON_EMPTY) -public class CatCodeDto extends AbstractResourceDto { - private String code; - private String value; - private Integer seqNo; - private Integer codeId; - private String codeDescription; - // groupCodeId can be integer (for update) or map (query result form cmdb) - private Object groupCodeId; - private String groupName; - private Integer catId; - private String status; - - private List ciTypes = new ArrayList<>(); - private CategoryDto cat; - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CatTypeDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CatTypeDto.java deleted file mode 100644 index f5ebd8f..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CatTypeDto.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -import java.util.LinkedList; -import java.util.List; - -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -@JsonInclude(Include.NON_EMPTY) -public class CatTypeDto extends AbstractResourceDto { - private Integer catTypeId; - private Integer ciTypeId; - private String catTypeName; - private String description; - private List cats = new LinkedList<>(); - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CategoryDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CategoryDto.java deleted file mode 100644 index ccbf30e..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CategoryDto.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -import java.util.LinkedList; -import java.util.List; - -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -@JsonInclude(Include.NON_EMPTY) -public class CategoryDto extends AbstractResourceDto { - private Integer catId; - private String catName; - private String description; - private Integer groupTypeId; - private Integer catTypeId; - private List codes = new LinkedList<>(); - private CatTypeDto catType; -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiDataDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiDataDto.java deleted file mode 100644 index a37b401..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiDataDto.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import java.util.HashMap; - -import org.apache.commons.beanutils.BeanMap; - -@SuppressWarnings({ "rawtypes", "unchecked" }) -public class CiDataDto extends HashMap { - private static final long serialVersionUID = 1L; - - public CiDataDto(Object ciObj) { - super(new BeanMap(ciObj)); - } -} \ No newline at end of file diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiDataTreeDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiDataTreeDto.java deleted file mode 100644 index 05b7a0a..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiDataTreeDto.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import com.fasterxml.jackson.annotation.JsonInclude; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.LinkedList; -import java.util.List; - -@Data -@JsonInclude(JsonInclude.Include.NON_EMPTY) -@NoArgsConstructor -@AllArgsConstructor -public class CiDataTreeDto { - private Integer ciTypeId; - private String rootGuid; - private String guid; - private Object data; - private List children = new LinkedList<>(); -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiTypeAttrDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiTypeAttrDto.java deleted file mode 100644 index 4dfc8f6..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiTypeAttrDto.java +++ /dev/null @@ -1,247 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; - -import lombok.Data; - -@JsonInclude(Include.NON_NULL) -@Data -public class CiTypeAttrDto extends AbstractResourceDto { - private Integer ciTypeAttrId; - private Integer ciTypeId; - private String description; - private Integer displaySeqNo; - private Boolean isDisplayed; - private Boolean isEditable; - private Boolean isHidden; - private Boolean isNullable; - private Boolean isUnique; - private String inputType; - private Boolean isDefunct; - private Boolean isSystem; - private Integer length; - private String name; - private String propertyName; - private String propertyType; - private Integer referenceId; - private Integer searchSeqNo; - private String specialLogic; - private String status; - private String referenceName; - private Integer referenceType; - private Boolean isAccessControlled; - private Boolean isAuto; - private String autoFillRule; - private String filterRule; - private Boolean isRefreshable; - - private CiTypeDto ciType; - - public CiTypeAttrDto() { - } - - public CiTypeAttrDto(Integer ciTypeAttrId) { - this.ciTypeAttrId = ciTypeAttrId; - } - - public Integer getCiTypeAttrId() { - return ciTypeAttrId; - } - - public void setCiTypeAttrId(Integer admCiTypeAttrId) { - this.ciTypeAttrId = admCiTypeAttrId; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Integer getDisplaySeqNo() { - return displaySeqNo; - } - - public void setDisplaySeqNo(Integer displaySeqNo) { - this.displaySeqNo = displaySeqNo; - } - - public Boolean getIsEditable() { - return isEditable; - } - - public void setIsEditable(Boolean isEditable) { - this.isEditable = isEditable; - } - - public Boolean getIsHidden() { - return isHidden; - } - - public void setIsHidden(Boolean isHidden) { - this.isHidden = isHidden; - } - - public Boolean getIsNullable() { - return isNullable; - } - - public void setIsNullable(Boolean isNullable) { - this.isNullable = isNullable; - } - - public Boolean getIsUnique() { - return isUnique; - } - - public void setIsUnique(Boolean isUnique) { - this.isUnique = isUnique; - } - - public String getInputType() { - return inputType; - } - - public void setInputType(String inputType) { - this.inputType = inputType; - } - - public Boolean getIsDefunct() { - return isDefunct; - } - - public void setIsDefunct(Boolean isDefunct) { - this.isDefunct = isDefunct; - } - - public Boolean getIsSystem() { - return isSystem; - } - - public void setIsSystem(Boolean isSystem) { - this.isSystem = isSystem; - } - - public Integer getLength() { - return length; - } - - public void setLength(Integer length) { - this.length = length; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getPropertyName() { - return propertyName; - } - - public void setPropertyName(String propertyName) { - this.propertyName = propertyName; - } - - public String getPropertyType() { - return propertyType; - } - - public void setPropertyType(String propertyType) { - this.propertyType = propertyType; - } - - public Integer getReferenceId() { - return referenceId; - } - - public void setReferenceId(Integer referenceId) { - this.referenceId = referenceId; - } - - public Integer getSearchSeqNo() { - return searchSeqNo; - } - - public void setSearchSeqNo(Integer searchSeqNo) { - this.searchSeqNo = searchSeqNo; - } - - public String getSpecialLogic() { - return specialLogic; - } - - public void setSpecialLogic(String specialLogic) { - this.specialLogic = specialLogic; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public String getReferenceName() { - return referenceName; - } - - public void setReferenceName(String referenceName) { - this.referenceName = referenceName; - } - - public Integer getReferenceType() { - return referenceType; - } - - public void setReferenceType(Integer referenceType) { - this.referenceType = referenceType; - } - - public Boolean getIsAccessControlled() { - return isAccessControlled; - } - - public void setIsAccessControlled(Boolean isAccessControlled) { - this.isAccessControlled = isAccessControlled; - } - - public CiTypeDto getCiType() { - return ciType; - } - - public void setCiType(CiTypeDto ciType) { - this.ciType = ciType; - } - - public Boolean getIsAuto() { - return isAuto; - } - - public void setIsAuto(Boolean isAuto) { - this.isAuto = isAuto; - } - - public Boolean getIsDisplayed() { - return isDisplayed; - } - - public void setIsDisplayed(Boolean isDisplayed) { - this.isDisplayed = isDisplayed; - } - - public Boolean getIsRefreshable() { - return isRefreshable; - } - - public void setIsRefreshable(Boolean isRefreshable) { - this.isRefreshable = isRefreshable; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiTypeDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiTypeDto.java deleted file mode 100644 index 1002031..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CiTypeDto.java +++ /dev/null @@ -1,131 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -import lombok.Data; - -import java.util.LinkedList; -import java.util.List; - -@Data -@JsonInclude(Include.NON_EMPTY) -public class CiTypeDto extends AbstractResourceDto { - private Integer ciTypeId; - private Integer ciGlobalUniqueId; - private Integer catalogId; - private String description; - private Integer tenementId; - private Integer layerId; - private String name; - private Integer seqNo; - private String tableName; - private String status; - private Integer zoomLevelId; - private Integer imageFileId; - private List attributes = new LinkedList<>(); - - public void setCiTypeId(Integer ciTypeId) { - this.ciTypeId = ciTypeId; - } - - public Integer getCiGlobalUniqueId() { - return ciGlobalUniqueId; - } - - public void setCiGlobalUniqueId(Integer ciGlobalUniqueId) { - this.ciGlobalUniqueId = ciGlobalUniqueId; - } - - public Integer getCatalogId() { - return catalogId; - } - - public void setCatalogId(Integer ciType) { - this.catalogId = ciType; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Integer getTenementId() { - return tenementId; - } - - public void setTenementId(Integer idTenement) { - this.tenementId = idTenement; - } - - public Integer getLayerId() { - return layerId; - } - - public void setLayerId(Integer layerId) { - this.layerId = layerId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Integer getSeqNo() { - return seqNo; - } - - public void setSeqNo(Integer seqNo) { - this.seqNo = seqNo; - } - - public String getTableName() { - return tableName; - } - - public void setTableName(String tableName) { - this.tableName = tableName; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public Integer getZoomLevelId() { - return zoomLevelId; - } - - public void setZoomLevelId(Integer zoomLevelId) { - this.zoomLevelId = zoomLevelId; - } - - public List getAttributes() { - return attributes; - } - - public void setAttributes(List attributes) { - this.attributes = attributes; - } - - public Integer getCiTypeId() { - return ciTypeId; - } - - public Integer getImageFileId() { - return imageFileId; - } - - public void setImageFileId(Integer imageFileId) { - this.imageFileId = imageFileId; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CmdbResponses.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CmdbResponses.java deleted file mode 100644 index fc91466..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/CmdbResponses.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import java.util.List; -import java.util.Map; - -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbResponse; - -public class CmdbResponses { - - public static class ImageInfoResponse extends CmdbResponse { - } - - public static class ConstantsReferenceTypesResponse extends CmdbResponse> { - } - - public static class ConstantsCiStatusResponse extends CmdbResponse> { - } - - public static class EnumCategoryTypeQueryResultResponse extends CmdbResponse> { - } - - public static class EnumCategoryTypeListResultResponse extends CmdbResponse> { - } - - public static class EnumCategoryQueryResultResponse extends CmdbResponse> { - } - - public static class EnumCategoryListResultResponse extends CmdbResponse> { - } - - public static class EnumCodeQueryResultResponse extends CmdbResponse> { - } - - public static class EnumCodeListResultResponse extends CmdbResponse> { - } - - public static class CiTypeQueryResultResponse extends CmdbResponse> { - } - - public static class CiTypeListResultResponse extends CmdbResponse> { - } - - public static class CiTypeAttributeQueryResultResponse extends CmdbResponse> { - } - - public static class CiTypeAttributeListResultResponse extends CmdbResponse> { - } - - public static class CiTypeDataQueryResultResponse extends CmdbResponse> { - } - - public static class CiTypeDataListResultResponse extends CmdbResponse> { - } - - public static class CiDataQueryResultResponse extends CmdbResponse> { - } - - public static class CiDataListResultResponse extends CmdbResponse> { - } - - public static class CiReferenceDataListResultResponse extends CmdbResponse> { - } - - public static class CiDataVersionsQueryResultResponse extends CmdbResponse> { - } - - public static class CiDataVersionDetailResultResponse extends CmdbResponse> { - } - - public static class IntQueryExecuteDataResponse extends CmdbResponse>> { - } - - public static class AdhocIntegrationQueryDataResponse extends CmdbResponse>> { - } - - public static class QueryOperationDataResponse extends CmdbResponse> { - } - - public static class SpecialConnectorDtoResponse extends CmdbResponse>> { - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/ImageInfoDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/ImageInfoDto.java deleted file mode 100644 index f3b76fc..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/ImageInfoDto.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; - -@JsonInclude(Include.NON_EMPTY) -public class ImageInfoDto { - private Integer id; - private String contentType; - private byte[] content; - - public ImageInfoDto() { - } - - public ImageInfoDto(int id) { - this.id = id; - } - - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getContentType() { - return contentType; - } - - public void setContentType(String contentType) { - this.contentType = contentType; - } - - public byte[] getContent() { - return content; - } - - public void setContent(byte[] content) { - this.content = content; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/OperateCiDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/OperateCiDto.java deleted file mode 100644 index c1e0c05..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/OperateCiDto.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import lombok.Data; - -@Data -public class OperateCiDto { - private String guid; - private int ciTypeId; - - public OperateCiDto(String guid, int ciTypeId) { - this.guid = guid; - this.ciTypeId = ciTypeId; - } - - public OperateCiDto() { - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/PaginationQuery.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/PaginationQuery.java deleted file mode 100644 index 6394f1a..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/PaginationQuery.java +++ /dev/null @@ -1,152 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -@Data -@NoArgsConstructor -@AllArgsConstructor -public class PaginationQuery { - private boolean paging; - private Pageable pageable; - private String filterRs; - private List filters = new ArrayList<>(); - private Sorting sorting; - private List groupBys = new ArrayList<>(); - private List refResources = new ArrayList<>(); - private List resultColumns; - private Dialect dialect = new Dialect(); - - public static class Dialect { - private boolean showCiHistory = false; - private Map data; - - public boolean getShowCiHistory() { - return showCiHistory; - } - - public void setShowCiHistory(boolean showCiHistory) { - this.showCiHistory = showCiHistory; - } - - public Map getData() { - return data; - } - - public void setData(Map data) { - this.data = data; - } - - } - - public PaginationQuery addEqualsFilter(String name, Object value) { - filters.add(new Filter(name, "eq", value)); - return this; - } - - public PaginationQuery setFiltersRelationship(String relationship) { - filterRs = relationship; - return this; - } - - public PaginationQuery addEqualsFilters(Map filterObject) { - filterObject.entrySet().forEach(entry -> filters.add(new Filter(entry.getKey(), "eq", entry.getValue()))); - return this; - } - - public PaginationQuery addInFilters(Map filterObject) { - filterObject.entrySet().forEach(entry -> filters.add(new Filter(entry.getKey(), "in", entry.getValue()))); - return this; - } - - public PaginationQuery addNotEqualsFilter(String name, Object value) { - filters.add(new Filter(name, "ne", value)); - return this; - } - - public PaginationQuery addInFilter(String name, List values) { - filters.add(new Filter(name, "in", values)); - return this; - } - - public PaginationQuery addContainsFilter(String name, String value) { - filters.add(new Filter(name, "contains", value)); - return this; - } - - public PaginationQuery addNotNullFilter(String name) { - filters.add(new Filter(name, "notNull", null)); - return this; - } - - public PaginationQuery addNullFilter(String name) { - filters.add(new Filter(name, "null", null)); - return this; - } - - public PaginationQuery ascendingSortBy(String field) { - sorting = new Sorting(true, field); - return this; - } - - public PaginationQuery descendingSortBy(String field) { - sorting = new Sorting(false, field); - return this; - } - - public PaginationQuery addReferenceResource(String refResource) { - refResources.add(refResource); - return this; - } - - public PaginationQuery withResultColumns(List resultColumns) { - this.resultColumns = resultColumns; - return this; - } - - @Data - @NoArgsConstructor - @AllArgsConstructor - public static class Filter { - private String name; - private String operator; - private Object value; - } - - @Data - @NoArgsConstructor - @AllArgsConstructor - public static class Sorting { - private boolean asc; - private String field; - } - - @Data - @NoArgsConstructor - @AllArgsConstructor - public static class Pageable { - private int startIndex; - private int pageSize; - } - - public static PaginationQuery defaultQueryObject() { - return new PaginationQuery(); - } - - public static PaginationQuery defaultQueryObject(String name, Object value) { - return defaultQueryObject().addEqualsFilter(name, value); - } - - @Override - public String toString() { - return "PaginationQuery [paging=" + paging + ", pageable=" + pageable + ", filters=" + filters + ", sorting=" - + sorting + ", groupBys=" + groupBys + ", refResources=" + refResources + ", resultColumns=" - + resultColumns + "]"; - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/PaginationQueryResult.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/PaginationQueryResult.java deleted file mode 100644 index a38fd61..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/cmdb/dto/v2/PaginationQueryResult.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb.dto.v2; - -import java.util.List; - -public class PaginationQueryResult { - private PageInfo pageInfo; - private List contents; - - public PageInfo getPageInfo() { - return pageInfo; - } - - public void setPageInfo(PageInfo pageInfo) { - this.pageInfo = pageInfo; - } - - public List getContents() { - return contents; - } - - public void setContents(List contents) { - this.contents = contents; - } - - public static class PageInfo { - private int startIndex; - private int pageSize; - private int totalRows; - - public int getStartIndex() { - return startIndex; - } - - public void setStartIndex(int startIndex) { - this.startIndex = startIndex; - } - - public int getPageSize() { - return pageSize; - } - - public void setPageSize(int pageSize) { - this.pageSize = pageSize; - } - - public int getTotalRows() { - return totalRows; - } - - public void setTotalRows(int totalRows) { - this.totalRows = totalRows; - } - - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusAssetItemInfo.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusAssetItemInfo.java deleted file mode 100644 index d773b4c..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusAssetItemInfo.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.webank.plugins.artifacts.support.nexus; - -public class NexusAssetItemInfo { - private String downloadUrl; - private String path; - private String id; - private String repository; - private String format; - - public String getDownloadUrl() { - return downloadUrl; - } - - public void setDownloadUrl(String downloadUrl) { - this.downloadUrl = downloadUrl; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getRepository() { - return repository; - } - - public void setRepository(String repository) { - this.repository = repository; - } - - public String getFormat() { - return format; - } - - public void setFormat(String format) { - this.format = format; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("NexusAssetItemInfo [downloadUrl="); - builder.append(downloadUrl); - builder.append(", path="); - builder.append(path); - builder.append(", id="); - builder.append(id); - builder.append(", repository="); - builder.append(repository); - builder.append(", format="); - builder.append(format); - builder.append("]"); - return builder.toString(); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusClient.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusClient.java deleted file mode 100644 index a589476..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusClient.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.webank.plugins.artifacts.support.nexus; - -import static org.springframework.http.HttpMethod.GET; - -import java.net.URI; -import java.util.Arrays; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; - -@Component -public class NexusClient { - private static final Logger log = LoggerFactory.getLogger(NexusClient.class); - - @Autowired - @Qualifier("simpleRestTemplate") - private RestTemplate restTemplate; - - - public NexusSearchAssetResponse searchAsset(URI targetUri, String nexusUsername, String nexusPassword) { - log.info("About to call {} ", targetUri.toString()); - HttpHeaders headers = new HttpHeaders(); - headers.setBasicAuth(nexusUsername,nexusPassword); - headers.setContentType(MediaType.APPLICATION_JSON); - headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); - - ResponseEntity responseEntity = restTemplate.exchange(targetUri, GET, new HttpEntity<>(headers), NexusSearchAssetResponse.class); - return responseEntity.getBody(); - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public D get(String targetUrl, String nexusUsername, String nexusPassword, Class responseType) { - log.info("About to call {} ", targetUrl); - HttpHeaders headers = new HttpHeaders(); - headers.setBasicAuth(nexusUsername,nexusPassword); - ResponseEntity rResponseEntity = restTemplate.exchange(targetUrl, GET, new HttpEntity<>(headers), responseType); - log.info("Nexus response: {} ", rResponseEntity); - return (D)rResponseEntity.getBody().getItems(); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusDirectiryDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusDirectiryDto.java deleted file mode 100644 index 1264858..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusDirectiryDto.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.webank.plugins.artifacts.support.nexus; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.webank.plugins.artifacts.support.cmdb.dto.v2.AbstractResourceDto; -import lombok.Data; - -@JsonInclude(JsonInclude.Include.NON_NULL) -@Data -public class NexusDirectiryDto extends AbstractResourceDto { - - private String name; - private String downloadUrl; -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusResponse.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusResponse.java deleted file mode 100644 index 945b4e4..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusResponse.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.webank.plugins.artifacts.support.nexus; - -import lombok.Data; - -@Data -public class NexusResponse { - public static final String STATUS_CODE_OK = "OK"; - - private DATATYPE items; - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusSearchAssetResponse.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusSearchAssetResponse.java deleted file mode 100644 index 931d447..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/nexus/NexusSearchAssetResponse.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.webank.plugins.artifacts.support.nexus; - -import java.util.ArrayList; -import java.util.List; - -public class NexusSearchAssetResponse { - private String continuationToken; - private List items = new ArrayList(); - - public String getContinuationToken() { - return continuationToken; - } - - public void setContinuationToken(String continuationToken) { - this.continuationToken = continuationToken; - } - - public List getItems() { - return items; - } - - public void setItems(List items) { - this.items = items; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/s3/S3Client.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/s3/S3Client.java deleted file mode 100644 index 6f97444..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/s3/S3Client.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.webank.plugins.artifacts.support.s3; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.net.URL; - -import com.amazonaws.ClientConfiguration; -import com.amazonaws.auth.AWSStaticCredentialsProvider; -import com.amazonaws.auth.BasicAWSCredentials; -import com.amazonaws.client.builder.AwsClientBuilder; -import com.amazonaws.regions.Regions; -import com.amazonaws.services.s3.AmazonS3; -import com.amazonaws.services.s3.AmazonS3ClientBuilder; -import com.amazonaws.services.s3.model.CannedAccessControlList; -import com.amazonaws.services.s3.model.CreateBucketRequest; -import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; -import com.amazonaws.services.s3.model.GetObjectRequest; -import com.amazonaws.services.s3.model.ObjectListing; -import com.amazonaws.services.s3.model.PutObjectRequest; -import com.amazonaws.services.s3.model.S3Object; -import com.amazonaws.services.s3.model.S3ObjectInputStream; -import com.amazonaws.services.s3.model.S3ObjectSummary; -import com.webank.plugins.artifacts.commons.PluginException; - -import lombok.extern.slf4j.Slf4j; - -@Slf4j -public class S3Client { - - private AmazonS3 s3Client; - private String endpoint; - - public AmazonS3 getS3Client() { - return this.s3Client; - } - - public S3Client(String endpoint, String accessKey, String secretKey) { - ClientConfiguration clientConfiguration = new ClientConfiguration(); - clientConfiguration.setSignerOverride("AWSS3V4SignerType"); - AmazonS3 newClient = AmazonS3ClientBuilder.standard() - .withEndpointConfiguration( - new AwsClientBuilder.EndpointConfiguration(endpoint, Regions.US_EAST_1.name())) - .withPathStyleAccessEnabled(true) - .withClientConfiguration(clientConfiguration) - .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey))) - .build(); - this.s3Client = newClient; - this.endpoint = endpoint; - } - - private boolean checkFileExists(String bucketName, String fileName) { - ObjectListing objects = s3Client.listObjects(bucketName); - - for (S3ObjectSummary ob : objects.getObjectSummaries()) { - if (ob.getKey() == fileName) { - return true; - } - } - return false; - } - - public String uploadBinaryToS3(String bucketName, String s3keyName, byte[] binary) - throws PluginException, IOException { - String urlStr = endpoint + "/" + bucketName + "/" + s3keyName; - - File tempFile = File.createTempFile("temp", ",tmp", null); - - try (FileOutputStream fos = new FileOutputStream(tempFile)) { - fos.write(binary); - uploadFile(bucketName, s3keyName, tempFile); - if (!tempFile.delete()) { - throw new PluginException("Delete temporary file failed"); - } - } catch (IOException e) { - log.error("upload file to S3/MinIO meet error: ", e); - } - return urlStr; - } - - public String uploadFile(String bucketName, String s3KeyName, File file) { - if (!(s3Client.doesBucketExist(bucketName))) { - s3Client.createBucket(new CreateBucketRequest(bucketName)); - } - - if (checkFileExists(bucketName, s3KeyName)) { - throw new PluginException(String.format("File[%s] already exists", s3KeyName)); - } - - s3Client.putObject( - new PutObjectRequest(bucketName, s3KeyName, file).withCannedAcl(CannedAccessControlList.Private)); - GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, s3KeyName); - URL url = s3Client.generatePresignedUrl(urlRequest); - - log.info("uploaded File [{}] to S3. url = [{}]", file.getAbsolutePath(), url); - return url.toString(); - } - - public S3ObjectInputStream downFile(String bucketName, String key) { - GetObjectRequest request = new GetObjectRequest(bucketName, key); - S3Object object = s3Client.getObject(request); - S3ObjectInputStream inputStream = object.getObjectContent(); - log.info("downloaded file [{}] from s3 , url {} , ", key, inputStream.getHttpRequest().getURI()); - return inputStream; - } - - public void downFile(String bucketName, String key, String localPath) { - GetObjectRequest request = new GetObjectRequest(bucketName, key); - s3Client.getObject(request, new File(localPath)); - } - - public String getUrlFromS3(String bucketName, String s3KeyName) { - GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(bucketName, s3KeyName); - String url = s3Client.generatePresignedUrl(httpRequest).toString(); - return url; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltConfigFileDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltConfigFileDto.java deleted file mode 100644 index 81bb601..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltConfigFileDto.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -import java.util.ArrayList; -import java.util.List; - -public class SaltConfigFileDto { - private String errorCode; - private String errorMessage; - private String filePath; - - private List configKeyInfos = new ArrayList(); - - public String getErrorCode() { - return errorCode; - } - - public void setErrorCode(String errorCode) { - this.errorCode = errorCode; - } - - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - - public String getFilePath() { - return filePath; - } - - public void setFilePath(String filePath) { - this.filePath = filePath; - } - - public List getConfigKeyInfos() { - return configKeyInfos; - } - - public void setConfigKeyInfos(List configKeyInfos) { - this.configKeyInfos = configKeyInfos; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("SaltConfigFileDto [errorCode="); - builder.append(errorCode); - builder.append(", errorMessage="); - builder.append(errorMessage); - builder.append(", filePath="); - builder.append(filePath); - builder.append(", configKeyInfos="); - builder.append(configKeyInfos); - builder.append("]"); - return builder.toString(); - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltConfigKeyInfoDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltConfigKeyInfoDto.java deleted file mode 100644 index 7c49039..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltConfigKeyInfoDto.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -public class SaltConfigKeyInfoDto { - private String line; - private String key; - private String type; - - private String sourceFilePath; - - public String getLine() { - return line; - } - - public void setLine(String line) { - this.line = line; - } - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getSourceFilePath() { - return sourceFilePath; - } - - public void setSourceFilePath(String sourceFilePath) { - this.sourceFilePath = sourceFilePath; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("SaltConfigKeyInfoDto [line="); - builder.append(line); - builder.append(", key="); - builder.append(key); - builder.append(", type="); - builder.append(type); - builder.append(", sourceFilePath="); - builder.append(sourceFilePath); - builder.append("]"); - return builder.toString(); - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNodeDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNodeDto.java deleted file mode 100644 index 3641ca8..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNodeDto.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -import com.fasterxml.jackson.annotation.JsonProperty; - -public class SaltFileNodeDto { - private String name; - private String path; - @JsonProperty("isDir") - private Boolean isDir; - private String md5; - - public String getName() { - return name; - } - public void setName(String name) { - this.name = name; - } - public String getPath() { - return path; - } - public void setPath(String path) { - this.path = path; - } - public Boolean getIsDir() { - return isDir; - } - public void setIsDir(Boolean isDir) { - this.isDir = isDir; - } - public String getMd5() { - return md5; - } - public void setMd5(String md5) { - this.md5 = md5; - } - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("[name="); - builder.append(name); - builder.append(", path="); - builder.append(path); - builder.append(", isDir="); - builder.append(isDir); - builder.append(", md5="); - builder.append(md5); - builder.append("]"); - return builder.toString(); - } - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNodeResultItemDto.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNodeResultItemDto.java deleted file mode 100644 index 3572172..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNodeResultItemDto.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -import java.util.List; - -public class SaltFileNodeResultItemDto { - - private String errorCode; - private String errorMessage; - private List files; - - public String getErrorCode() { - return errorCode; - } - public void setErrorCode(String errorCode) { - this.errorCode = errorCode; - } - public String getErrorMessage() { - return errorMessage; - } - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } - public List getFiles() { - return files; - } - public void setFiles(List files) { - this.files = files; - } - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("[errorCode="); - builder.append(errorCode); - builder.append(", errorMessage="); - builder.append(errorMessage); - builder.append(", files="); - builder.append(files); - builder.append("]"); - return builder.toString(); - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNotExistException.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNotExistException.java deleted file mode 100644 index d40a229..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltFileNotExistException.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -public class SaltFileNotExistException extends RuntimeException { - - /** - * - */ - private static final long serialVersionUID = 5974905083999742131L; - - public SaltFileNotExistException() { - super(); - } - - public SaltFileNotExistException(String message, Throwable cause, boolean enableSuppression, - boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - - public SaltFileNotExistException(String message, Throwable cause) { - super(message, cause); - } - - public SaltFileNotExistException(String message) { - super(message); - } - - public SaltFileNotExistException(Throwable cause) { - super(cause); - } - - - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackRemoteCallException.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackRemoteCallException.java deleted file mode 100644 index 30bd6e1..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackRemoteCallException.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -import com.webank.plugins.artifacts.support.RemoteCallException; - -public class SaltstackRemoteCallException extends RemoteCallException { - - private transient SaltstackResponse SaltstackResponse; - - public SaltstackRemoteCallException(String message) { - super(message); - } - - public SaltstackRemoteCallException(String message, SaltstackResponse SaltstackResponse) { - super(message); - this.SaltstackResponse = SaltstackResponse; - } - - public SaltstackRemoteCallException(String message, SaltstackResponse SaltstackResponse, Throwable cause) { - super(message, cause); - this.SaltstackResponse = SaltstackResponse; - } - - public SaltstackResponse getSaltstackResponse() { - return SaltstackResponse; - } - - @Override - public String getErrorMessage() { - return String.format("%s (Call Saltstack meet error: %s)", this.getMessage(), getStatusCode(SaltstackResponse)); - } - - @Override - public Object getErrorData() { - return SaltstackResponse == null ? null : SaltstackResponse.getResultData(); - } - - private String getStatusCode(SaltstackResponse SaltstackResponse) { - return SaltstackResponse == null ? null : SaltstackResponse.getResultCode(); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackRequest.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackRequest.java deleted file mode 100644 index d1f0837..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackRequest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -import java.util.List; -import java.util.Map; - -import lombok.Data; - -@Data -public class SaltstackRequest { - private List inputs; - - public SaltstackRequest withInputs(List inputs) { - this.setInputs(inputs); - return this; - } - - public List getInputs() { - return inputs; - } - - public void setInputs(List inputs) { - this.inputs = inputs; - } - - public static class DefaultSaltstackRequest extends SaltstackRequest> { - } -} \ No newline at end of file diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackResponse.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackResponse.java deleted file mode 100644 index 2c4d9c5..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackResponse.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import lombok.Data; - -@Data -public class SaltstackResponse { - public static final String RESULT_CODE_OK = "0"; - - private String resultCode; - private String resultMessage; - @JsonProperty("results") - private ResultData resultData; - - public List getOutputs() { - return (getResultData() == null) ? null : getResultData().getOutputs(); - } - - public ResultData getResultData() { - return resultData; - } - - public void setResultData(ResultData resultData) { - this.resultData = resultData; - } - - public String getResultCode() { - return resultCode; - } - - public void setResultCode(String resultCode) { - this.resultCode = resultCode; - } - - public String getResultMessage() { - return resultMessage; - } - - public void setResultMessage(String resultMessage) { - this.resultMessage = resultMessage; - } - - public static class ResultData { - private List outputs; - - public List getOutputs() { - return outputs; - } - - public void setOutputs(List outputs) { - this.outputs = outputs; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("[outputs="); - builder.append(outputs); - builder.append("]"); - return builder.toString(); - } - - - } - - public static class DefaultSaltstackResponse extends SaltstackResponse { - } - - @Override - public String toString() { - return "[resultCode=" + resultCode + ", resultMessage=" + resultMessage + ", resultData=" + resultData + "]"; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackServiceStub.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackServiceStub.java deleted file mode 100644 index fe064e3..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/support/saltstack/SaltstackServiceStub.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.webank.plugins.artifacts.support.saltstack; - -import java.util.List; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; - -import com.webank.plugins.artifacts.support.saltstack.SaltstackResponse.DefaultSaltstackResponse; -import com.webank.plugins.artifacts.support.saltstack.SaltstackResponse.ResultData; - -@Service -public class SaltstackServiceStub { - private static final Logger log = LoggerFactory.getLogger(SaltstackServiceStub.class); - - @Autowired - private RestTemplate restTemplate; - - private static final String pluginContextPath = "/saltstack"; - private static final String INF_RELEASED_PACKAGE_LIST_DIR = "/v1/released-package/listCurrentDir"; - private static final String INF_RELEASED_PACKAGE_PROPERTY_KEY = "/v1/released-package/getConfigFileKey"; - - public ResultData getReleasedPackageFilesByCurrentDir(String wecubeGatewayUrl, - SaltstackRequest> request) { - String targetUrl = asServerUrl(wecubeGatewayUrl, INF_RELEASED_PACKAGE_LIST_DIR); - - log.info("About to call {} with parameters: {} ", targetUrl, request); - PackageFilesListResponse response = restTemplate.postForObject(targetUrl, request, PackageFilesListResponse.class); - log.info("Saltstack plugin response: {} ", response); -// validateResponse(response, false); - - if (response == null) { - throw new SaltstackRemoteCallException("Saltstack plugin failure due to no response."); - } - if (!SaltstackResponse.RESULT_CODE_OK.equalsIgnoreCase(response.getResultCode())) { - - ResultData resultData = response.getResultData(); - if(resultData != null){ - List itemDtos = resultData.getOutputs(); - if(itemDtos != null && (!itemDtos.isEmpty())){ - if("2".equals(itemDtos.get(0).getErrorCode())){ - throw new SaltFileNotExistException(itemDtos.get(0).getErrorMessage()); - } - } - } - - throw new SaltstackRemoteCallException("Saltstack plugin call error: " + response.getResultMessage(), response); - } - - return response.getResultData(); - - } - - public ResultData getReleasedPackagePropertyKeysByFilePath(String saltstackServerUrl, - SaltstackRequest> request) { - String targetUrl = asServerUrl(saltstackServerUrl, INF_RELEASED_PACKAGE_PROPERTY_KEY); - - log.info("About to call {} with parameters: {} ", targetUrl, request); - PropertyKeysResponse response = restTemplate.postForObject(targetUrl, request, PropertyKeysResponse.class); - log.info("Saltstack plugin response: {} ", response); - validateResponse(response, false); - - return response.getResultData(); - } - - @SuppressWarnings("unused") - private ResultData post(String targetUrl, SaltstackRequest parameters) { - log.info("About to call {} with parameters: {} ", targetUrl, parameters); - SaltstackResponse response = restTemplate.postForObject(targetUrl, parameters, DefaultSaltstackResponse.class); - log.info("Saltstack plugin response: {} ", response); - validateResponse(response, false); - - return response.getResultData(); - } - - private void validateResponse(SaltstackResponse response, boolean dataRequired) { - if (response == null) { - throw new SaltstackRemoteCallException("Saltstack plugin failure due to no response."); - } - if (!SaltstackResponse.RESULT_CODE_OK.equalsIgnoreCase(response.getResultCode())) { - throw new SaltstackRemoteCallException("Saltstack plugin call error: " + response.getResultMessage(), response); - } - if (dataRequired && response.getOutputs() == null) { - throw new SaltstackRemoteCallException("Saltstack plugin call failure due to unexpected empty response.", response); - } - } - - private String asServerUrl(String serverUrl, String originPath, Object... pathVariables) { - String solvedPath = originPath; - if (pathVariables != null && pathVariables.length > 0) { - solvedPath = String.format(originPath, pathVariables); - } - return serverUrl + pluginContextPath + solvedPath; - } - - public static class PropertyKeysResponse extends SaltstackResponse{ - } - - public static class PackageFilesListResponse extends SaltstackResponse{ - - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/Base64Utils.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/Base64Utils.java deleted file mode 100644 index 7004480..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/Base64Utils.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.webank.plugins.artifacts.utils; - -import sun.misc.BASE64Decoder; - -public class Base64Utils { - - public static String getBASE64(String s) { - if (s == null) return null; - return (new sun.misc.BASE64Encoder()).encode(s.getBytes()); - } - - public static String getFromBASE64(String s) { - if (s == null) return null; - BASE64Decoder decoder = new BASE64Decoder(); - try { - byte[] b = decoder.decodeBuffer(s); - return new String(b); - } catch (Exception e) { - return null; - } - } - - public static void main(String[] args) { - //String str = "123456"; - System.out.println(Base64Utils.getBASE64("admin" + ":" + "admin1234")); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/BooleanUtils.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/BooleanUtils.java deleted file mode 100644 index 512962f..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/BooleanUtils.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.webank.plugins.artifacts.utils; - -public class BooleanUtils { - - public static final boolean isTrue(String value) { - Boolean booleanObject = org.apache.commons.lang3.BooleanUtils.toBooleanObject(value); - if (booleanObject == null) { - return false; - } else { - return booleanObject.booleanValue(); - } - } - - public static final T derive(String condition, T trueResult, T failResult) { - return (isTrue(condition)) ? trueResult : failResult; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/CollectionUtils.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/CollectionUtils.java deleted file mode 100644 index 003cdc6..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/CollectionUtils.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.webank.plugins.artifacts.utils; - -import com.webank.plugins.artifacts.commons.PluginException; - -import java.util.*; -import java.util.function.Function; -import java.util.stream.Collectors; - -import static java.util.function.Function.identity; -import static org.apache.commons.collections4.CollectionUtils.isEmpty; - -public class CollectionUtils { - - public static Map asMap(Collection list, Function keyFunction) { - if (list == null || list.isEmpty()) { - return new HashMap<>(); - } else { - return list.stream().collect(Collectors.toMap(keyFunction, identity(), (oldValue, newValue) -> oldValue)); - } - } - - public static List getOrCreateArrayList(Map> map, K key) { - List element = map.get(key); - if (element == null) { - element = new ArrayList<>(); - map.put(key, element); - } - return element; - } - - public static Map getOrCreateHashMap(Map> map, T key) { - Map element = map.get(key); - if (element == null) { - element = new HashMap<>(); - map.put(key, element); - } - return element; - } - - public static void putToMap(Map targetMap, List addingList, Function keyMapper) { - for (V element : addingList) { - targetMap.put(keyMapper.apply(element), element); - } - } - - public static void putToSet(Set targetMap, Collection addingList, Function keyMapper) { - for (V element : addingList) { - targetMap.add(keyMapper.apply(element)); - } - } - - public static List groupUp(List groups, - List elements, - Function keyMapperOfGroup, - Function> childrenMapperOfGroup, - Function parentMapperOfElement) { - if (isEmpty(groups)) - return groups; - if (isEmpty(elements)) - return groups; - if (keyMapperOfGroup == null) - throw new PluginException("Key mapper of Group Object cannot be null for grouping function."); - if (childrenMapperOfGroup == null) - throw new PluginException("Children mapper of Group Object cannot be null for grouping function."); - if (parentMapperOfElement == null) - throw new PluginException("Parent mapper of Element Object cannot be null for grouping function."); - List resultGroups = new ArrayList<>(groups); - Map groupMap = asMap(resultGroups, keyMapperOfGroup); - - elements.forEach(element -> { - Object parentId = parentMapperOfElement.apply(element); - if (parentId == null) - return; - G group = groupMap.get(parentId); - if (group == null) - return; - List children = childrenMapperOfGroup.apply(group); - if (children == null) - throw new PluginException("Children property should not be null."); - children.add(element); - }); - return resultGroups; - } - - public static E pickRandomOne(List collection) { - if (isEmpty(collection)) - return null; - return collection.get(new Random().nextInt(collection.size())); - } - - public static E pickLastOne(List collection, Comparator comparator) { - if (isEmpty(collection)) - return null; - if (comparator != null) - collection.sort(comparator); - return collection.get(collection.size() - 1); - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/JwtUtils.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/JwtUtils.java deleted file mode 100644 index c5d2843..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/JwtUtils.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.webank.plugins.artifacts.utils; - -import org.apache.commons.lang3.StringUtils; - -import com.webank.plugins.artifacts.constant.ArtifactsConstants; - -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.ExpiredJwtException; -import io.jsonwebtoken.Jws; -import io.jsonwebtoken.JwtException; -import io.jsonwebtoken.Jwts; - -public class JwtUtils { - - public static String getAuthUserName(String sAccessTokenHeader) { - String sAccessToken = sAccessTokenHeader.substring(ArtifactsConstants.PREFIX_BEARER_TOKEN.length()).trim(); - - if (StringUtils.isBlank(sAccessToken)) { - throw new JwtException("Access token is blank."); - } - Jws jwt = null; - try { - jwt = Jwts.parser().setSigningKey(StringUtilsEx.decodeBase64(ArtifactsConstants.SIGNING_KEY)).parseClaimsJws(sAccessToken); - } catch (ExpiredJwtException e) { - throw new JwtException("Access token has expired."); - } - - Claims claims = jwt.getBody(); - - String username = claims.getSubject(); - return username; - } -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/StringUtilsEx.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/StringUtilsEx.java deleted file mode 100644 index ff3632f..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/StringUtilsEx.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.webank.plugins.artifacts.utils; - -import org.apache.commons.codec.binary.Base64; - -public class StringUtilsEx { - public static String encodeBase64String(byte[] data){ - return Base64.encodeBase64String(data); - } - - public static byte[] decodeBase64(String base64String){ - return Base64.decodeBase64(base64String); - } - -} diff --git a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/VersionUtils.java b/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/VersionUtils.java deleted file mode 100644 index a4d775b..0000000 --- a/artifacts-core/src/main/java/com/webank/plugins/artifacts/utils/VersionUtils.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.webank.plugins.artifacts.utils; - -import org.apache.commons.lang3.math.NumberUtils; - -public class VersionUtils { - private static final String VERSION_SEPARATORS = "[./-]"; - - public static boolean isLeftVersionGreatThanRightVersion(String leftVersion, String rightVersion) { - return compare(leftVersion, rightVersion) > 0; - } - - public static int compare(String leftVersion, String rightVersion) { - if (leftVersion == null || rightVersion == null) - return 0; - if (leftVersion.equals(rightVersion)) - return 0; - leftVersion = ignoreCharacterV(leftVersion); - rightVersion = ignoreCharacterV(rightVersion); - String[] segmentsOfLeftVersion = separate(leftVersion); - String[] segmentsOfRightVersion = separate(rightVersion); - for (int i = 0; i < segmentsOfLeftVersion.length && i < segmentsOfRightVersion.length; i++) { - String segmentOfLeftVersion = segmentsOfLeftVersion[i]; - String segmentOfRightVersion = segmentsOfRightVersion[i]; - - if (segmentOfLeftVersion.equals(segmentOfRightVersion)) - continue; - - try { - int x = Integer.parseInt(segmentOfLeftVersion); - int y = Integer.parseInt(segmentOfRightVersion); - if (x == y) - continue; - return NumberUtils.compare(x, y); - } catch (NumberFormatException exception) { - return segmentOfLeftVersion.compareTo(segmentOfRightVersion); - } - } - return NumberUtils.compare(segmentsOfLeftVersion.length, segmentsOfRightVersion.length); - } - - private static String ignoreCharacterV(String version) { - if (version.startsWith("v") || version.startsWith("V")) - return version.substring(1); - return version; - } - - private static String[] separate(String version) { - return version.split(VERSION_SEPARATORS); - } - -} diff --git a/artifacts-core/src/main/resources/application.yml b/artifacts-core/src/main/resources/application.yml deleted file mode 100644 index 5e41eb1..0000000 --- a/artifacts-core/src/main/resources/application.yml +++ /dev/null @@ -1,36 +0,0 @@ -server: - port: 18081 - address: localhost - servlet: - context-path: /artifacts - -spring: - profiles: - active: dev - messages: - basename: message/artifacts_core_err_message - encoding: UTF-8 - - servlet: - multipart: - max-file-size: 2GB - max-request-size: 2GB - -plugins: - pacakge-name: artifacts - - wecube-gateway-server-url: http://localhost:19090 - artifacts-nexus-server-url: http://localhost:18888 - artifacts-nexus-username: admin - artifacts-nexus-password: admin123 - artifacts-nexus-Repository: maven-releases - cmdb-artifact-path: artifact_path - - artifacts-s3-server-url: http://localhost:9000 - artifacts-s3-access-key: access_key - artifacts-s3-secret-key: secret_key - artifacts-s3-bucket-name: wecube-artifacts - jwt-signing-key: ${JWT_SIGNING_KEY:} - artifacts-local-enabled: ${UPLOAD_ENABLED:true} - artifacts-nexus-enabled: ${UPLOAD_NEXUS_ENABLED:true} - property-encrypt-key-path: ${PROPERTY_ENCRYPT_KEY_PATH:/data/certs/rsa_key} \ No newline at end of file diff --git a/artifacts-core/src/main/resources/logback-spring.xml b/artifacts-core/src/main/resources/logback-spring.xml deleted file mode 100644 index e9ddbe9..0000000 --- a/artifacts-core/src/main/resources/logback-spring.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - true - - - - - - - %d{yyyy-MM-dd HH:mm:ss} %-5level %-48logger{48} - %msg%n - - - - - - ${logPath}/${logFile}.log - - ${logPath}/${logFile}-%d{yyyyMMdd}.log - - 30 - - - - - UTF-8 - - - - - - - - diff --git a/artifacts-core/src/main/resources/message/artifacts_core_err_message.properties b/artifacts-core/src/main/resources/message/artifacts_core_err_message.properties deleted file mode 100644 index b377047..0000000 --- a/artifacts-core/src/main/resources/message/artifacts_core_err_message.properties +++ /dev/null @@ -1,14 +0,0 @@ -artifacts.core.msg.errorcode.3000=Field 'currentDir' is required. -artifacts.core.msg.errorcode.3001=Field 'filePath' is required. -artifacts.core.msg.errorcode.3002=Failed to convert multipart file to file. -artifacts.core.msg.errorcode.3003=Failed to convert Nexus package to file. -artifacts.core.msg.errorcode.3004=Failed to generate md5 checksum for file {0}. -artifacts.core.msg.errorcode.3005=Cannot find category with name {0}. -artifacts.core.msg.errorcode.3006=Cannot find Nexus path from CMDB.Please configure Nexus path in CMDB. -artifacts.core.msg.errorcode.3007=Cannot parse Nexus response in json. -artifacts.core.msg.errorcode.3008=Package with ID [{0}] not found. -artifacts.core.msg.errorcode.3009=Bad data type,expected:{0},but:{0}. -artifacts.core.msg.errorcode.3010=Package uploading is disabled! -artifacts.core.msg.errorcode.3011=Nexus URL of deploy package cannot be empty. -artifacts.core.msg.errorcode.3012=Baseline package GUID cannot be empty. -artifacts.core.msg.errorcode.3013=Failed to create package CI. \ No newline at end of file diff --git a/artifacts-core/src/test/java/com/webank/plugins/artifacts/support/cmdb/CmdbServiceV2StubTest.java b/artifacts-core/src/test/java/com/webank/plugins/artifacts/support/cmdb/CmdbServiceV2StubTest.java deleted file mode 100644 index 33fcefa..0000000 --- a/artifacts-core/src/test/java/com/webank/plugins/artifacts/support/cmdb/CmdbServiceV2StubTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb; - -import static org.junit.Assert.fail; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class CmdbServiceV2StubTest { - - @Autowired - protected CmdbServiceV2Stub cmdbServiceV2Stub; - - @Test - public void testCreateCiData() { - fail("Not yet implemented"); - } - -} diff --git a/artifacts-core/src/test/java/com/webank/plugins/artifacts/support/cmdb/StandardCmdbEntityRestClientTest.java b/artifacts-core/src/test/java/com/webank/plugins/artifacts/support/cmdb/StandardCmdbEntityRestClientTest.java deleted file mode 100644 index 177d079..0000000 --- a/artifacts-core/src/test/java/com/webank/plugins/artifacts/support/cmdb/StandardCmdbEntityRestClientTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.webank.plugins.artifacts.support.cmdb; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.webank.plugins.artifacts.interceptor.AuthorizationStorage; -import com.webank.plugins.artifacts.support.cmdb.dto.CmdbDiffConfigDto; - -@Ignore -@ActiveProfiles("test") -@RunWith(SpringRunner.class) -@SpringBootTest -public class StandardCmdbEntityRestClientTest { - - @Autowired - private StandardCmdbEntityRestClient client; - - ObjectMapper objectMapper = new ObjectMapper(); - - @Before - public void setUp() { - String token = null; - token = String.format("Bearer %s", token); - - AuthorizationStorage.getIntance().set(token); - } - - @Test - public void testCreateDiffConfigurationCi(){ - String varName = "gl_diff_key_3"; - String varValue = null; - CmdbDiffConfigDto result = client.createDiffConfigurationCi( varName, varValue); - System.out.println(toJson(result)); - } - - - - @Test - public void testCreateEntity() { - String entityName = "diff_configuration"; - List> requestParams = new ArrayList<>(); - Map requestDataMap = new HashMap<>(); - requestDataMap.put("code", "gl_diff_key"); - requestDataMap.put("variable_name", "gl_diff_key"); - requestDataMap.put("variable_value", "ddd"); - - requestParams.add(requestDataMap); - Object result = client.createEntity(entityName, requestParams); - System.out.println(toJson(result)); - } - - @Test - public void testQueryDiffConfigurations() { - List> results = client.queryDiffConfigurations(); - for (Map result : results) { - System.out.println("=========================="); - result.entrySet().forEach(e -> { - System.out.println(String.format("k:%s, v:%s", e.getKey(), e.getValue())); - }); - } - } - - private String toJson(Object object){ - try { - String sJson = objectMapper.writeValueAsString(object); - return sJson; - } catch (JsonProcessingException e) { - throw new RuntimeException("", e); - } - } - -} diff --git a/artifacts-corepy/artifacts_corepy/apps/package/api.py b/artifacts-corepy/artifacts_corepy/apps/package/api.py index 6cfffb5..04c921c 100644 --- a/artifacts-corepy/artifacts_corepy/apps/package/api.py +++ b/artifacts-corepy/artifacts_corepy/apps/package/api.py @@ -4,7 +4,7 @@ import datetime import hashlib -from logging import root +import fnmatch import os import logging import collections @@ -13,7 +13,6 @@ import tempfile import os.path from talos.core import config -from talos.utils import http from talos.core import utils from talos.db import crud from talos.db import converter @@ -25,6 +24,7 @@ from artifacts_corepy.common import s3 from artifacts_corepy.common import wecmdb from artifacts_corepy.common import utils as artifact_utils +from artifacts_corepy.common import constant LOG = logging.getLogger(__name__) CONF = config.CONF @@ -39,14 +39,14 @@ def is_upload_nexus_enabled(): def calculate_md5(fileobj): - m = hashlib.md5() + hasher = hashlib.md5() chunk_size = 64 * 1024 fileobj.seek(0) chunk = fileobj.read(chunk_size) while chunk: - m.update(chunk) + hasher.update(chunk) chunk = fileobj.read(chunk_size) - return m.hexdigest() + return hasher.hexdigest() def calculate_file_md5(filepath): @@ -59,9 +59,12 @@ def convert(self, value): return '|'.join([i.get('filename') for i in value if i.get('filename')]) -class BooleanConverter(converter.NullConverter): +class BooleanNomalizedConverter(converter.NullConverter): + def __init__(self, default=False): + self.fallback_value = default + def convert(self, value): - return 'true' if utils.bool_from_string(value) else 'false' + return 'true' if utils.bool_from_string(value, default=self.fallback_value) else 'false' class WeCubeResource(object): @@ -75,7 +78,7 @@ def get_cmdb_client(self): def list(self, params): pass - def list_by_post(self, filters): + def list_by_post(self, query): pass @@ -106,7 +109,7 @@ def list(self, params): last_version = collections.OrderedDict() for content in resp_json['data']['contents']: r_guid = content['data']['r_guid'] - if (r_guid not in last_version): + if r_guid not in last_version: last_version[r_guid] = content return { 'contents': list(last_version.values()), @@ -239,10 +242,16 @@ def list_by_post(self, query, unit_design_id): query['filters'].append({"name": "unit_design", "operator": "eq", "value": unit_design_id}) resp_json = cmdb_client.retrieve(CONF.wecube.wecmdb.citypes.deploy_package, query) for i in resp_json['data']['contents']: - i['data']['deploy_file_path'] = self.build_file_object(i['data']['deploy_file_path']) - i['data']['start_file_path'] = self.build_file_object(i['data']['start_file_path']) - i['data']['stop_file_path'] = self.build_file_object(i['data']['stop_file_path']) - i['data']['diff_conf_file'] = self.build_file_object(i['data']['diff_conf_file']) + fields = ('deploy_file_path', 'start_file_path', 'stop_file_path', 'diff_conf_file') + for field in fields: + i['data'][field] = self.build_file_object(i['data'].get(field, None)) + # db部署支持 + i['data']['package_type'] = i['data'].get('package_type', + constant.PackageType.default) or constant.PackageType.default + fields = ('db_upgrade_directory', 'db_rollback_directory', 'db_upgrade_file_path', 'db_rollback_file_path', + 'db_deploy_file_path', 'db_diff_conf_file') + for field in fields: + i['data'][field] = self.build_file_object(i['data'].get(field, None)) return resp_json['data'] def build_file_object(self, filenames, spliter='|'): @@ -432,7 +441,48 @@ def _pop_none(d, k): validate_on=['update:O'], rule='1, 36', rule_type='length', - converter=BooleanConverter(), + converter=BooleanNomalizedConverter(True), + nullable=True), + crud.ColumnValidator('packageType', + validate_on=['update:O'], + rule='1, 36', + rule_type='length', + nullable=True), + crud.ColumnValidator('dbUpgradeDirectory', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=True), + crud.ColumnValidator('dbRollbackDirectory', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=True), + crud.ColumnValidator('dbUpgradeFilePath', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=True), + crud.ColumnValidator('dbRollbackFilePath', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=True), + crud.ColumnValidator('dbDeployFilePath', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=True), + crud.ColumnValidator('dbDiffConfFile', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), nullable=True), ] clean_data = crud.ColumnValidator.get_clean_data(validates, data, 'update') @@ -448,24 +498,55 @@ def _pop_none(d, k): '/') + '/repository/' + CONF.wecube.nexus.repository + '/' + clean_data['nexusUrl'].lstrip('/') unit_design_id = baseline_package['data']['unit_design']['guid'] new_pakcage = self.upload_from_nexus(url, unit_design_id)[0] + # db部署支持, 检查是否用户手动指定值 + b_db_upgrade_detect = True + b_db_rollback_detect = True update_data = {} + update_data['baseline_package'] = baseline_package_id + update_data['is_decompression'] = baseline_package['data']['is_decompression'] + update_data['package_type'] = baseline_package['data'].get( + 'package_type', + constant.PackageType.default) if clean_data.get('packageType', None) is None else clean_data['packageType'] keys = [('startFilePath', 'start_file_path'), ('stopFilePath', 'stop_file_path'), - ('deployFilePath', 'deploy_file_path'), ('diffConfFile', 'diff_conf_file')] + ('deployFilePath', 'deploy_file_path'), ('diffConfFile', 'diff_conf_file'), + ('dbUpgradeDirectory', 'db_upgrade_directory'), ('dbRollbackDirectory', 'db_rollback_directory'), + ('dbUpgradeFilePath', 'db_upgrade_file_path'), ('dbRollbackFilePath', 'db_rollback_file_path'), + ('dbDeployFilePath', 'db_deploy_file_path'), ('dbDiffConfFile', 'db_diff_conf_file')] + # 没有指定目录,无法探测 + if (not clean_data.get('db_upgrade_directory', None)) and (not baseline_package['data'].get( + 'db_upgrade_directory', None)): + b_db_upgrade_detect = False + if (not clean_data.get('db_rollback_directory', None)) and (not baseline_package['data'].get( + 'db_rollback_directory', None)): + b_db_rollback_detect = False for s_key, d_key in keys: if s_key in clean_data and clean_data[s_key] is not None: + if d_key == 'db_upgrade_file_path': + b_db_upgrade_detect = False + if d_key == 'db_rollback_file_path': + b_db_rollback_detect = False update_data[d_key] = self.build_file_object(clean_data[s_key]) else: - update_data[d_key] = self.build_file_object(baseline_package['data'][d_key]) - update_data['baseline_package'] = baseline_package_id - update_data['is_decompression'] = baseline_package['data']['is_decompression'] - self.update(update_data, unit_design_id, new_pakcage['guid'], with_detail=False) + update_data[d_key] = self.build_file_object(baseline_package['data'].get(d_key, None)) + self.update(update_data, + unit_design_id, + new_pakcage['guid'], + with_detail=False, + db_upgrade_detect=b_db_upgrade_detect, + db_rollback_detect=b_db_rollback_detect) return {'guid': new_pakcage['guid']} def create(self, data): cmdb_client = self.get_cmdb_client() return cmdb_client.create(CONF.wecube.wecmdb.citypes.deploy_package, data) - def update(self, data, unit_design_id, deploy_package_id, with_detail=True): + def update(self, + data, + unit_design_id, + deploy_package_id, + with_detail=True, + db_upgrade_detect=False, + db_rollback_detect=False): validates = [ crud.ColumnValidator('guid', validate_on=['update:M'], rule='1, 36', rule_type='length', nullable=False), crud.ColumnValidator('baseline_package', validate_on=['update:O'], nullable=True), @@ -500,8 +581,50 @@ def update(self, data, unit_design_id, deploy_package_id, with_detail=True): nullable=False), crud.ColumnValidator('is_decompression', validate_on=['update:O'], - converter=BooleanConverter(), + converter=BooleanNomalizedConverter(True), nullable=True), + crud.ColumnValidator('package_type', validate_on=['update:O'], nullable=True), + crud.ColumnValidator('db_upgrade_directory', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=False), + crud.ColumnValidator('db_rollback_directory', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=False), + crud.ColumnValidator('db_upgrade_file_path', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=False), + crud.ColumnValidator('db_rollback_file_path', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=False), + crud.ColumnValidator('db_deploy_file_path', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=False), + crud.ColumnValidator('db_diff_conf_file', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + converter=FileNameConcater(), + nullable=False), + crud.ColumnValidator('db_diff_conf_variable', + validate_on=['update:O'], + rule=(list, tuple), + rule_type='type', + nullable=False), ] cmdb_client = self.get_cmdb_client() query = {"filters": [{"name": "guid", "operator": "eq", "value": deploy_package_id}], "paging": False} @@ -512,7 +635,37 @@ def update(self, data, unit_design_id, deploy_package_id, with_detail=True): deploy_package = resp_json['data']['contents'][0] data['guid'] = deploy_package_id clean_data = crud.ColumnValidator.get_clean_data(validates, data, 'update') + available_extensions = CONF.diff_conf_extension.split(',') + if 'diff_conf_file' in data: + for item in data['diff_conf_file']: + matched = False + for ext in available_extensions: + if fnmatch.fnmatch(item['filename'], '*' + ext): + matched = True + break + if not matched: + raise exceptions.ValidationError( + message=_('invalid filename extension: %(filename)s from %(field)s, must be %(options)s') % { + 'filename': item['filename'], + 'field': 'diff_conf_file', + 'options': available_extensions + }) + if 'db_diff_conf_file' in data: + for item in data['db_diff_conf_file']: + matched = False + for ext in available_extensions: + if fnmatch.fnmatch(item['filename'], '*' + ext): + matched = True + break + if not matched: + raise exceptions.ValidationError( + message=_('invalid filename extension: %(filename)s from %(field)s, must be %(options)s') % { + 'filename': item['filename'], + 'field': 'db_diff_conf_file', + 'options': available_extensions + }) if 'baseline_package' in clean_data: + # 兼容旧接口传{}/''/null代表清空的情况 if isinstance(clean_data['baseline_package'], dict): if not clean_data['baseline_package']: clean_data['baseline_package'] = None @@ -554,7 +707,7 @@ def update(self, data, unit_design_id, deploy_package_id, with_detail=True): exist_diff_configs.add(finder[diff_conf['key']]['data']['guid']) # 创建新的差异化变量项 bind_variables = list(exist_diff_configs) - if len(new_diff_configs): + if new_diff_configs: resp_json = cmdb_client.create(CONF.wecube.wecmdb.citypes.diff_config, [{ 'variable_name': c, 'description': c @@ -562,11 +715,80 @@ def update(self, data, unit_design_id, deploy_package_id, with_detail=True): bind_variables.extend([c['guid'] for c in resp_json['data']]) if auto_bind: clean_data['diff_conf_variable'] = bind_variables + # db部署支持 + # 根据用户指定进行变量绑定 + db_auto_bind = True + if 'db_diff_conf_variable' in data: + clean_data['db_diff_conf_variable'] = [ + c['diffConfigGuid'] for c in data['db_diff_conf_variable'] if c['bound'] + ] + db_auto_bind = False + # 根据diff_conf_file计算变量进行更新绑定 + if 'db_diff_conf_file' in data: + new_diff_conf_file_list = set([f['filename'] for f in data['db_diff_conf_file']]) + old_diff_conf_file_list = set( + [f['filename'] for f in self.build_file_object(deploy_package['data'].get('db_diff_conf_file', None))]) + # diff_conf_file值并未发生改变,无需下载文件更新变量 + if new_diff_conf_file_list != old_diff_conf_file_list: + package_cached_dir = self.ensure_package_cached(deploy_package['data']['guid'], + deploy_package['data']['deploy_package_url']) + self.update_file_variable(package_cached_dir, data['db_diff_conf_file']) + # 获取所有差异化配置项 + empty_query = {"filters": [], "paging": False} + resp_json = cmdb_client.retrieve(CONF.wecube.wecmdb.citypes.diff_config, empty_query) + all_diff_configs = resp_json['data']['contents'] + finder = artifact_utils.CaseInsensitiveDict() + for conf in all_diff_configs: + finder[conf['data']['variable_name']] = conf + package_diff_configs = [] + new_diff_configs = set() + exist_diff_configs = set() + for conf_file in data['db_diff_conf_file']: + package_diff_configs.extend(conf_file['configKeyInfos']) + for diff_conf in package_diff_configs: + if diff_conf['key'] not in finder: + new_diff_configs.add(diff_conf['key']) + else: + exist_diff_configs.add(finder[diff_conf['key']]['data']['guid']) + # 创建新的差异化变量项 + bind_variables = list(exist_diff_configs) + if new_diff_configs: + resp_json = cmdb_client.create(CONF.wecube.wecmdb.citypes.diff_config, [{ + 'variable_name': c, + 'description': c + } for c in new_diff_configs]) + bind_variables.extend([c['guid'] for c in resp_json['data']]) + if db_auto_bind: + clean_data['db_diff_conf_variable'] = bind_variables + if db_upgrade_detect: + clean_data['db_upgrade_file_path'] = FileNameConcater().convert( + self.find_files_by_status( + clean_data['baseline_package'], deploy_package_id, + clean_data['db_upgrade_directory'].split('|') if clean_data['db_upgrade_directory'] else [], + ['new', 'changed'])) + if db_rollback_detect: + clean_data['db_rollback_file_path'] = FileNameConcater().convert( + self.find_files_by_status( + clean_data['baseline_package'], deploy_package_id, + clean_data['db_rollback_directory'].split('|') if clean_data['db_rollback_directory'] else [], + ['new', 'changed'])) resp_json = cmdb_client.update(CONF.wecube.wecmdb.citypes.deploy_package, [clean_data]) if with_detail: return self.get(unit_design_id, deploy_package_id) return resp_json['data'] + def find_files_by_status(self, baseline_id, package_id, source_dirs, status): + results = [] + files = self.filetree(None, package_id, baseline_id, False, source_dirs, with_dir=False, recursive=True) + for f in files: + if f['exists'] and not f['isDir'] and f['comparisonResult'] in status: + # convert data field + f['filename'] = f.pop('path', None) + f.pop('name', None) + results.append(f) + results.sort(key=lambda x: x['filename'], reverse=False) + return results + def get(self, unit_design_id, deploy_package_id): cmdb_client = self.get_cmdb_client() query = {"filters": [{"name": "guid", "operator": "eq", "value": deploy_package_id}], "paging": False} @@ -582,14 +804,9 @@ def get(self, unit_design_id, deploy_package_id): result = {} result['packageId'] = deploy_package_id result['baseline_package'] = baseline_package.get('guid', None) - - result['is_decompression'] = utils.bool_from_string(deploy_package['data']['is_decompression']) - # |切割为列表 - result['deploy_file_path'] = self.build_file_object(deploy_package['data']['deploy_file_path']) - result['start_file_path'] = self.build_file_object(deploy_package['data']['start_file_path']) - result['stop_file_path'] = self.build_file_object(deploy_package['data']['stop_file_path']) - result['diff_conf_file'] = self.build_file_object(deploy_package['data']['diff_conf_file']) - result['diff_conf_variable'] = deploy_package['data']['diff_conf_variable'] + # db部署支持 + result['package_type'] = deploy_package['data'].get( + 'package_type', constant.PackageType.default) or constant.PackageType.default # 文件对比[same, changed, new, deleted] baseline_cached_dir = None package_cached_dir = None @@ -600,19 +817,40 @@ def get(self, unit_design_id, deploy_package_id): package_cached_dir = self.ensure_package_cached(deploy_package['data']['guid'], deploy_package['data']['deploy_package_url']) # 更新文件的md5,comparisonResult,isDir - self.update_file_status(baseline_cached_dir, package_cached_dir, result['deploy_file_path']) - self.update_file_status(baseline_cached_dir, package_cached_dir, result['start_file_path']) - self.update_file_status(baseline_cached_dir, package_cached_dir, result['stop_file_path']) - self.update_file_status(baseline_cached_dir, package_cached_dir, result['diff_conf_file']) - # 更新差异化配置文件的变量列表 - self.update_file_variable(package_cached_dir, result['diff_conf_file']) - package_diff_configs = [] - for conf_file in result['diff_conf_file']: - package_diff_configs.extend(conf_file['configKeyInfos']) - # 更新差异化变量bound/diffConfigGuid/diffExpr/fixedDate/key/type - result['diff_conf_variable'] = self.update_diff_conf_variable(all_diff_configs, package_diff_configs, - result['diff_conf_variable']) - + result['is_decompression'] = utils.bool_from_string(deploy_package['data']['is_decompression'], default=True) + result['diff_conf_variable'] = deploy_package['data']['diff_conf_variable'] + result['db_diff_conf_variable'] = deploy_package['data'].get('db_diff_conf_variable', []) + # |切割为列表 + fields = ('deploy_file_path', 'start_file_path', 'stop_file_path', 'diff_conf_file') + for field in fields: + result[field] = self.build_file_object(deploy_package['data'][field]) + if result['package_type'] in (constant.PackageType.app, constant.PackageType.mixed): + self.update_file_status(baseline_cached_dir, package_cached_dir, result[field]) + if result['package_type'] in (constant.PackageType.app, constant.PackageType.mixed): + # 更新差异化配置文件的变量列表 + self.update_file_variable(package_cached_dir, result['diff_conf_file']) + package_diff_configs = [] + for conf_file in result['diff_conf_file']: + package_diff_configs.extend(conf_file['configKeyInfos']) + # 更新差异化变量bound/diffConfigGuid/diffExpr/fixedDate/key/type + result['diff_conf_variable'] = self.update_diff_conf_variable(all_diff_configs, package_diff_configs, + result['diff_conf_variable']) + # db部署支持 + fields = ('db_upgrade_directory', 'db_rollback_directory', 'db_upgrade_file_path', 'db_rollback_file_path', + 'db_deploy_file_path', 'db_diff_conf_file') + for field in fields: + result[field] = self.build_file_object(deploy_package['data'].get(field, None)) + if result['package_type'] in (constant.PackageType.db, constant.PackageType.mixed): + self.update_file_status(baseline_cached_dir, package_cached_dir, result[field]) + if result['package_type'] in (constant.PackageType.db, constant.PackageType.mixed): + # 更新差异化配置文件的变量列表 + self.update_file_variable(package_cached_dir, result['db_diff_conf_file']) + package_diff_configs = [] + for conf_file in result['db_diff_conf_file']: + package_diff_configs.extend(conf_file['configKeyInfos']) + # 更新差异化变量bound/diffConfigGuid/diffExpr/fixedDate/key/type + result['db_diff_conf_variable'] = self.update_diff_conf_variable(all_diff_configs, package_diff_configs, + result['db_diff_conf_variable']) return result def baseline_compare(self, unit_design_id, deploy_package_id, baseline_package_id): @@ -629,26 +867,40 @@ def baseline_compare(self, unit_design_id, deploy_package_id, baseline_package_i raise exceptions.NotFoundError(message=_("Can not find ci data for guid [%(rid)s]") % {'rid': baseline_package_id}) baseline_package = resp_json['data']['contents'][0] - - result = {} - # |切割为列表 - result['deploy_file_path'] = self.build_file_object(baseline_package['data']['deploy_file_path']) - result['start_file_path'] = self.build_file_object(baseline_package['data']['start_file_path']) - result['stop_file_path'] = self.build_file_object(baseline_package['data']['stop_file_path']) - result['diff_conf_file'] = self.build_file_object(baseline_package['data']['diff_conf_file']) - # 文件对比[same, changed, new, deleted] baseline_cached_dir = None package_cached_dir = None - # 确认baselin和package文件已下载并解压缓存在本地(加锁) + # 确认baseline和package文件已下载并解压缓存在本地(加锁) baseline_cached_dir = self.ensure_package_cached(baseline_package['data']['guid'], baseline_package['data']['deploy_package_url']) package_cached_dir = self.ensure_package_cached(deploy_package['data']['guid'], deploy_package['data']['deploy_package_url']) - # 更新文件的md5,comparisonResult,isDir - self.update_file_status(baseline_cached_dir, package_cached_dir, result['deploy_file_path']) - self.update_file_status(baseline_cached_dir, package_cached_dir, result['start_file_path']) - self.update_file_status(baseline_cached_dir, package_cached_dir, result['stop_file_path']) - self.update_file_status(baseline_cached_dir, package_cached_dir, result['diff_conf_file']) + package_type = baseline_package['data'].get('package_type', + constant.PackageType.default) or constant.PackageType.default + + result = {} + # |切割为列表 + fields = ('deploy_file_path', 'start_file_path', 'stop_file_path', 'diff_conf_file') + for field in fields: + result[field] = self.build_file_object(baseline_package['data'].get(field, None)) + if package_type in (constant.PackageType.app, constant.PackageType.mixed): + # 更新文件的md5,comparisonResult,isDir + self.update_file_status(baseline_cached_dir, package_cached_dir, result[field]) + # db部署支持 + fields = ('db_upgrade_directory', 'db_rollback_directory', 'db_upgrade_file_path', 'db_rollback_file_path', + 'db_deploy_file_path', 'db_diff_conf_file') + for field in fields: + result[field] = self.build_file_object(baseline_package['data'].get(field, None)) + if package_type in (constant.PackageType.db, constant.PackageType.mixed): + self.update_file_status(baseline_cached_dir, package_cached_dir, result['db_upgrade_directory']) + self.update_file_status(baseline_cached_dir, package_cached_dir, result['db_rollback_directory']) + self.update_file_status(baseline_cached_dir, package_cached_dir, result['db_deploy_file_path']) + self.update_file_status(baseline_cached_dir, package_cached_dir, result['db_diff_conf_file']) + result['db_upgrade_file_path'] = self.find_files_by_status( + baseline_package_id, deploy_package_id, [i['filename'] for i in result['db_upgrade_directory']], + ['new', 'changed']) + result['db_rollback_file_path'] = self.find_files_by_status( + baseline_package_id, deploy_package_id, [i['filename'] for i in result['db_rollback_directory']], + ['new', 'changed']) return result def baseline_files_compare(self, data, unit_design_id, deploy_package_id, baseline_package_id): @@ -711,21 +963,52 @@ def update_tree_status(self, baseline_path, package_path, nodes): self.update_tree_status(None if not baseline_path else os.path.join(baseline_path, subpath), os.path.join(package_path, subpath), n['children']) - def filetree(self, unit_design_id, deploy_package_id, baseline_package_id, expand_all, files): - def _scan_dir(basepath, subpath): + def filetree(self, + unit_design_id, + deploy_package_id, + baseline_package_id, + expand_all, + files, + with_dir=True, + recursive=False): + def _scan_dir(basepath, subpath, with_dir=True, recursive=False): results = [] path = os.path.join(basepath, subpath) - if os.path.exists(path): - for e in os.scandir(path): - results.append({ - 'children': [], - 'comparisonResult': None, - 'exists': None, - 'isDir': e.is_dir(), - 'md5': None, - 'name': e.name, - 'path': e.path[len(basepath) + 1:], - }) + if os.path.exists(path) and os.path.isdir(path): + if recursive: + for _root, _dirs, _files in os.walk(path): + if with_dir: + for d in _dirs: + results.append({ + 'children': [], + 'comparisonResult': None, + 'exists': True, + 'isDir': True, + 'md5': None, + 'name': d, + 'path': os.path.join(_root, d)[len(basepath) + 1:], + }) + for f in _files: + results.append({ + 'children': [], + 'comparisonResult': None, + 'exists': True, + 'isDir': False, + 'md5': None, + 'name': f, + 'path': os.path.join(_root, f)[len(basepath) + 1:], + }) + else: + for e in os.scandir(path): + results.append({ + 'children': [], + 'comparisonResult': None, + 'exists': True, + 'isDir': e.is_dir(), + 'md5': None, + 'name': e.name, + 'path': e.path[len(basepath) + 1:], + }) return results def _add_children_node(filename, subpath, file_list, is_dir=False): @@ -765,7 +1048,8 @@ def _generate_tree_from_list(basepath, file_list): scan_results = _scan_dir(basepath, subpath) expanded_dirs.add((basepath, subpath)) root_nodes.extend(scan_results) - _add_children_node(parts[0], subpath, root_nodes) + if len(parts[0]) > 0: + _add_children_node(parts[0], subpath, root_nodes) # ends with a/b/c/ else: filename = parts.pop(-1) @@ -791,17 +1075,17 @@ def _generate_tree_from_list(basepath, file_list): _add_children_node(filename, subpath, path_nodes) return root_nodes - def _get_file_list(baseline_path, package_path, file_list): + def _get_file_list(baseline_path, package_path, file_list, with_dir, recursive): results = [] for f in file_list: new_f = f.lstrip('/') parts = new_f.split('/') subpath = os.path.join(*[p for p in parts if p not in ('', '.', '..')]) - new_file_list = _scan_dir(package_path, subpath) - self.update_file_status(None if not baseline_path else os.path.join(baseline_path, subpath), - os.path.join(package_path, subpath), + new_file_list = _scan_dir(package_path, subpath, with_dir=with_dir, recursive=recursive) + self.update_file_status(None if not baseline_path else baseline_path, + package_path, new_file_list, - file_key='name') + file_key='path') results.extend(new_file_list) return results @@ -832,7 +1116,11 @@ def _get_file_list(baseline_path, package_path, file_list): results = _generate_tree_from_list(package_cached_dir, files) self.update_tree_status(baseline_cached_dir, package_cached_dir, results) else: - results = _get_file_list(baseline_cached_dir, package_cached_dir, files) + results = _get_file_list(baseline_cached_dir, + package_cached_dir, + files, + with_dir=with_dir, + recursive=recursive) return results def update_file_status(self, baseline_cached_dir, package_cached_dir, files, file_key='filename'): @@ -848,7 +1136,7 @@ def update_file_status(self, baseline_cached_dir, package_cached_dir, files, fil md5 = None if b_exists: i['isDir'] = os.path.isdir(b_filepath) - if not os.path.isdir(b_filepath): + if not i['isDir']: b_md5 = calculate_file_md5(b_filepath) if exists: i['isDir'] = os.path.isdir(filepath) @@ -951,7 +1239,6 @@ def ensure_package_cached(self, guid, url): LOG.info('download complete') LOG.info('unpack package: %s to %s', guid, file_cache_dir) try: - # FIXME: exception for some files artifact_utils.unpack_file(filepath, file_cache_dir) except Exception as e: shutil.rmtree(file_cache_dir, ignore_errors=True) @@ -1013,9 +1300,18 @@ def list_by_post(self, query, unit_design_id): unit_design = resp_json['data']['contents'][0] nexus_client = nexus.NeuxsClient(CONF.wecube.nexus.server, CONF.wecube.nexus.username, CONF.wecube.nexus.password) - return nexus_client.list(CONF.wecube.nexus.repository, - self.get_unit_design_artifact_path(unit_design), - extensions=['.zip', '.tar', '.tar.gz', 'tgz', '.jar']) + results = nexus_client.list(CONF.wecube.nexus.repository, + self.get_unit_design_artifact_path(unit_design), + extensions=artifact_utils.REGISTED_UNPACK_FORMATS) + if not utils.bool_from_string(CONF.nexus_sort_as_string, default=False): + return self.version_sort(results) + return sorted(results, key=lambda x: x['name'], reverse=True) + + def version_sort(self, datas): + def _extract_key(name): + return tuple([int(i) for i in re.findall('\d+', name)]) + + return sorted(datas, key=lambda x: _extract_key(x['name']), reverse=True) class DiffConfig(WeCubeResource): @@ -1031,4 +1327,4 @@ def list(self, params): cmdb_client = self.get_cmdb_client() query = {"dialect": {"showCiHistory": False}, "filters": [], "paging": False} resp_json = cmdb_client.retrieve(CONF.wecube.wecmdb.citypes.diff_config, query) - return [i['data'] for i in resp_json['data']['contents']] \ No newline at end of file + return [i['data'] for i in resp_json['data']['contents']] diff --git a/artifacts-corepy/artifacts_corepy/apps/package/route.py b/artifacts-corepy/artifacts_corepy/apps/package/route.py index 842ed05..921d0ee 100644 --- a/artifacts-corepy/artifacts_corepy/apps/package/route.py +++ b/artifacts-corepy/artifacts_corepy/apps/package/route.py @@ -4,26 +4,54 @@ from talos.core import config from talos.core import utils +from talos.core.i18n import _ from artifacts_corepy.apps.package import controller from artifacts_corepy.common import nexus +from artifacts_corepy.common import wecube +from artifacts_corepy.common import exceptions CONF = config.CONF -class SinkAdapter(object): +class DownloadAdapter(object): def __call__(self, req, resp, repository): if utils.bool_from_string(CONF.use_remote_nexus_only): client = nexus.NeuxsClient(CONF.wecube.nexus.server, CONF.wecube.nexus.username, CONF.wecube.nexus.password) else: client = nexus.NeuxsClient(CONF.nexus.server, CONF.nexus.username, CONF.nexus.password) with client.download_stream(client.server + '/repository/' + repository) as stream: - print(stream.headers) resp.set_stream(stream.raw, int(stream.headers.get('Content-Length', 0))) resp.set_header('Content-Disposition', stream.headers.get('Content-Disposition')) resp.set_header('Content-Type', stream.headers.get('Content-Type')) +class EntityAdapter(object): + def __call__(self, req, resp, package_name, entity_name, action_name): + server = CONF.wecube.server + token = req.auth_token + client = wecube.WeCubeClient(server, token) + url = '/platform/v1/packages/%(package_name)s/entities/%(entity_name)s/%(action_name)s' % { + 'package_name': package_name, + 'entity_name': entity_name, + 'action_name': action_name, + } + if action_name == 'retrieve': + data = client.retrieve(url) + resp.json = {'code': 200, 'status': 'OK', 'data': data['data'], 'message': 'success'} + elif action_name == 'update': + data = client.update(url, req.json) + resp.json = {'code': 200, 'status': 'OK', 'data': data['data'], 'message': 'success'} + else: + raise exceptions.NotFoundError( + _('%(action_name)s for %(package_name)s:%(entity_name)s not supported') % { + 'package_name': package_name, + 'entity_name': entity_name, + 'action_name': action_name, + }) + + def add_routes(api): + # cmdb api forward api.add_route('/artifacts/system-design-versions', controller.CollectionSystemDesign()) api.add_route('/artifacts/system-design-versions/{rid}', controller.ItemSystemDesign()) api.add_route('/artifacts/getPackageCiTypeId', controller.ControllerDeployPackageCiTypeId()) @@ -31,33 +59,40 @@ def add_routes(api): api.add_route('/artifacts/ci-types', controller.CollectionCiTypes()) api.add_route('/artifacts/enum/system/codes', controller.CollectionEnumCodes()) api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/query', controller.CollectionUnitDesignPackages()) - # Nexus + api.add_route('/artifacts/ci-types/{ci_type_id}/references/by', controller.ItemCiReferences()) + api.add_route('/artifacts/ci-types/{ci_type_id}/attributes', controller.ItemCiAttributes()) + api.add_route('/artifacts/ci-types/{ci_type_id}/ci-data/batch-delete', controller.CiDelete()) + api.add_route('/artifacts/ci/state/operate', controller.CiStateAction()) + # platform api forward + api.add_sink( + EntityAdapter(), + r'/artifacts/platform/v1/packages/(?P[-_A-Za-z0-9]+)/entities/(?P[-_A-Za-z0-9]+)/(?P[-_A-Za-z0-9]+)' + ) + # nexus query api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/queryNexusDirectiry', controller.CollectionUnitDesignNexusPackages()) + # nexus upload api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/uploadNexusPackage', controller.CollectionUnitDesignNexusPackageUpload()) - # local upload + # local nexus upload api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/upload', controller.CollectionUnitDesignPackageUpload()) # artifact download - api.add_sink(SinkAdapter(), r'/artifacts/repository/(?P.*)') + api.add_sink(DownloadAdapter(), r'/artifacts/repository/(?P.*)') + # package detail api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/{deploy_package_id}/query', controller.ItemPackage()) + # package baseline full compare api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/{deploy_package_id}/comparison', controller.UnitDesignPackageBaselineCompare()) + # package baseline files compare api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/{deploy_package_id}/files/comparison', controller.UnitDesignPackageBaselineFilesCompare()) + # package files tree api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/{deploy_package_id}/files/query', controller.UnitDesignPackageFileTree()) + # package update api.add_route('/artifacts/unit-designs/{unit_design_id}/packages/{deploy_package_id}/update', controller.ItemPackageUpdate()) - api.add_route('/artifacts/platform/v1/packages/wecmdb/entities/diff_configuration/update', - controller.ItemDiffConfigUpdate()) + # package upload & create with baseline for automation api.add_route('/artifacts/packages/auto-create-deploy-package', controller.ItemUploadAndCreatePackage()) - api.add_route('/artifacts/ci-types/{ci_type_id}/references/by', controller.ItemCiReferences()) - api.add_route('/artifacts/ci-types/{ci_type_id}/attributes', controller.ItemCiAttributes()) - - api.add_route('/artifacts/ci/state/operate', controller.CiStateAction()) - api.add_route('/artifacts/ci-types/{ci_type_id}/ci-data/batch-delete', controller.CiDelete()) - api.add_route('/artifacts/platform/v1/packages/wecmdb/entities/diff_configuration/retrieve', - controller.CollectionDiffConfigs()) diff --git a/artifacts-corepy/artifacts_corepy/apps/plugin/__init__.py b/artifacts-corepy/artifacts_corepy/apps/plugin/__init__.py new file mode 100644 index 0000000..1e9e62b --- /dev/null +++ b/artifacts-corepy/artifacts_corepy/apps/plugin/__init__.py @@ -0,0 +1,5 @@ +# coding=utf-8 + +from __future__ import absolute_import + +from artifacts_corepy.apps.plugin import route diff --git a/artifacts-corepy/artifacts_corepy/apps/plugin/api.py b/artifacts-corepy/artifacts_corepy/apps/plugin/api.py new file mode 100644 index 0000000..7bd76d8 --- /dev/null +++ b/artifacts-corepy/artifacts_corepy/apps/plugin/api.py @@ -0,0 +1,60 @@ +# coding=utf-8 + +from __future__ import absolute_import + +import datetime +import logging +from urllib.parse import urlparse + +from artifacts_corepy.common import wecmdb +from talos.core import config +from talos.utils import scoped_globals + +LOG = logging.getLogger(__name__) +CONF = config.CONF + + +class Package(object): + def create_from_image_name(self, image_name, tag, namespace, md5, nexus_url, connector_port, unit_design_id, + baseline_package, operator): + client = wecmdb.WeCMDBClient(CONF.wecube.server, scoped_globals.GLOBALS.request.auth_token) + url_result = urlparse(nexus_url + or (CONF.wecube.nexus.server if CONF.use_remote_nexus_only else CONF.nexus.server)) + namespace = namespace or '' + deploy_package_url = '%s:%s/%s%s:%s' % ( + url_result.hostname, connector_port or + (CONF.wecube.nexus.connector_port if CONF.use_remote_nexus_only else CONF.nexus.connector_port), + namespace + '/' if namespace else '', image_name, tag) + package_name = '%s-%s' % (image_name, tag) + query = { + "filters": [{ + "name": "name", + "operator": "eq", + "value": package_name + }, { + "name": "unit_design", + "operator": "eq", + "value": unit_design_id + }], + "paging": + False + } + resp_json = client.retrieve(CONF.wecube.wecmdb.citypes.deploy_package, query) + exists = resp_json.get('data', {}).get('contents', []) + package = {'guid': None, 'deploy_package_url': None} + if not exists: + data = { + 'baseline_package': baseline_package or '', + 'unit_design': unit_design_id, + 'name': package_name, + 'deploy_package_url': deploy_package_url, + 'md5_value': md5 or 'N/A', + 'package_type': 'image', + 'upload_user': operator, + 'upload_time': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + } + ret = client.create(CONF.wecube.wecmdb.citypes.deploy_package, [data]) + package = {'guid': ret['data'][0]['guid'], 'deploy_package_url': ret['data'][0]['deploy_package_url']} + else: + package = {'guid': exists[0]['data']['guid'], 'deploy_package_url': exists[0]['data']['deploy_package_url']} + return package diff --git a/artifacts-corepy/artifacts_corepy/apps/plugin/controller.py b/artifacts-corepy/artifacts_corepy/apps/plugin/controller.py new file mode 100644 index 0000000..184821d --- /dev/null +++ b/artifacts-corepy/artifacts_corepy/apps/plugin/controller.py @@ -0,0 +1,142 @@ +# coding=utf-8 +''' +plugin api input/output standard + +input data: +{ + "requestId": "request-001", //仅异步调用需要用到 + "operator": "admin", //操作人 + "inputs": [ + {"callbackParameter": "", "xml define prop": xxx}, + {}, + {} + ] +} + +output data: +{ + "resultCode": "0", //调用插件结果,"0"代表调用成功,"1"代表调用失败 + "resultMessage": "success", //调用结果信息,一般用于调用失败时返回失败信息 + "results": { + "outputs": [ + {"callbackParameter": "", "errorCode": "0", "errorMessage": "", "xml define prop": xxx}, + {}, + {} + ] + } +} +''' + +from __future__ import absolute_import + +import falcon +from talos.db import crud +from talos.core.i18n import _ +from talos.common import controller +from talos.db import validator + +from artifacts_corepy.apps.plugin import api as plugin_api + + +class PackageFromImage(controller.Controller): + allow_methods = ('POST', ) + param_rules = [ + crud.ColumnValidator(field='requestId', + rule=validator.LengthValidator(0, 255), + validate_on=['check:O'], + nullable=True), + crud.ColumnValidator(field='operator', + rule=validator.LengthValidator(0, 255), + validate_on=['check:O'], + nullable=True), + crud.ColumnValidator(field='inputs', + rule=validator.TypeValidator(list), + validate_on=['check:M'], + nullable=False), + ] + input_rules = [ + crud.ColumnValidator(field='callbackParameter', + rule=validator.LengthValidator(0, 255), + validate_on=['check:O'], + nullable=True), + crud.ColumnValidator(field='unit_design', + rule=validator.LengthValidator(1, 255), + validate_on=['check:M'], + nullable=False), + crud.ColumnValidator(field='image_name', + rule=validator.LengthValidator(1, 255), + validate_on=['check:M'], + nullable=False), + crud.ColumnValidator(field='tag', + rule=validator.LengthValidator(1, 255), + validate_on=['check:M'], + nullable=False), + crud.ColumnValidator(field='namespace', + rule=validator.LengthValidator(0, 255), + validate_on=['check:O'], + nullable=True), + crud.ColumnValidator(field='md5', + rule=validator.LengthValidator(0, 255), + validate_on=['check:O'], + nullable=True), + crud.ColumnValidator(field='nexus_url', + rule=validator.LengthValidator(0, 255), + validate_on=['check:O'], + nullable=True), + crud.ColumnValidator(field='connector_port', + rule=validator.LengthValidator(0, 255), + validate_on=['check:O'], + nullable=True), + crud.ColumnValidator(field='baseline_package', + rule=validator.LengthValidator(0, 255), + validate_on=['check:O'], + nullable=True), + ] + + def on_post(self, req, resp, **kwargs): + self._validate_method(req) + self._validate_data(req) + resp.json = self.create(req, req.json, **kwargs) + resp.status = falcon.HTTP_200 + + def create(self, req, data): + result = {'resultCode': '0', 'resultMessage': 'success', 'results': {'outputs': []}} + is_error = False + error_indexes = [] + try: + clean_data = crud.ColumnValidator.get_clean_data(self.param_rules, data, 'check') + operator = clean_data.get('operator', None) or 'N/A' + for idx, item in enumerate(clean_data['inputs']): + single_result = { + 'callbackParameter': item.get('callbackParameter', None), + 'errorCode': '0', + 'errorMessage': 'success', + 'guid': None, + 'deploy_package_url': None + } + try: + clean_item = crud.ColumnValidator.get_clean_data(self.input_rules, item, 'check') + package = plugin_api.Package().create_from_image_name(clean_item['image_name'], clean_item['tag'], + clean_item.get('namespace', None), + clean_item.get('md5', None), + clean_item.get('nexus_url', None), + clean_item.get('connector_port', None), + clean_item['unit_design'], + clean_item.get('baseline_package', + None), operator) + single_result.update(package) + result['results']['outputs'].append(single_result) + except Exception as e: + single_result['errorCode'] = '1' + single_result['errorMessage'] = str(e) + result['results']['outputs'].append(single_result) + is_error = True + error_indexes.append(str(idx + 1)) + except Exception as e: + result['resultCode'] = '1' + result['resultMessage'] = str(e) + if is_error: + result['resultCode'] = '1' + result['resultMessage'] = _('Fail to %(action)s [%(num)s] record, detail error in the data block') % dict( + action='process', num=','.join(error_indexes)) + return result diff --git a/artifacts-corepy/artifacts_corepy/apps/plugin/route.py b/artifacts-corepy/artifacts_corepy/apps/plugin/route.py new file mode 100644 index 0000000..2b1f19f --- /dev/null +++ b/artifacts-corepy/artifacts_corepy/apps/plugin/route.py @@ -0,0 +1,10 @@ +# coding=utf-8 + +from __future__ import absolute_import + +from artifacts_corepy.apps.plugin import controller + + +def add_routes(api): + # plugin create image package + api.add_route('/artifacts/v1/packages/from-image', controller.PackageFromImage()) diff --git a/artifacts-corepy/artifacts_corepy/common/constant.py b/artifacts-corepy/artifacts_corepy/common/constant.py new file mode 100644 index 0000000..f63733c --- /dev/null +++ b/artifacts-corepy/artifacts_corepy/common/constant.py @@ -0,0 +1,17 @@ +# coding=utf-8 + +from __future__ import absolute_import + + +class PackageType(object): + app = 'app' + db = 'db' + mixed = 'mixed' + default = 'mixed' + + +class CompareResult(object): + same = 'same' + new = 'new' + changed = 'changed' + deleted = 'deleted' diff --git a/artifacts-corepy/artifacts_corepy/common/exceptions.py b/artifacts-corepy/artifacts_corepy/common/exceptions.py index fdde3b0..26f4d8a 100644 --- a/artifacts-corepy/artifacts_corepy/common/exceptions.py +++ b/artifacts-corepy/artifacts_corepy/common/exceptions.py @@ -54,7 +54,7 @@ def title(self): @property def message_format(self): - return _('detail: column %(attribute)s validate failed, because: %(msg)s') + return _('column %(attribute)s validate failed, because: %(msg)s') class FieldRequired(PluginError): diff --git a/artifacts-corepy/artifacts_corepy/common/nexus.py b/artifacts-corepy/artifacts_corepy/common/nexus.py index 77c4c77..1c9190b 100644 --- a/artifacts-corepy/artifacts_corepy/common/nexus.py +++ b/artifacts-corepy/artifacts_corepy/common/nexus.py @@ -12,8 +12,6 @@ import requests import requests.auth from requests_toolbelt import MultipartEncoder -from talos.core import exceptions as core_ex -from talos.core.i18n import _ from talos.utils import http LOG = logging.getLogger(__name__) @@ -49,7 +47,6 @@ def list(self, repository, path, extensions=None, continue_token=None, search_ur filtered_items.append(i) else: filtered_items = resp_json['items'] - print(filtered_items) results.extend([{ 'name': i['path'].split('/')[-1], 'downloadUrl': i['downloadUrl'], diff --git a/artifacts-corepy/artifacts_corepy/common/utils.py b/artifacts-corepy/artifacts_corepy/common/utils.py index b6210c5..9adfb98 100644 --- a/artifacts-corepy/artifacts_corepy/common/utils.py +++ b/artifacts-corepy/artifacts_corepy/common/utils.py @@ -23,7 +23,7 @@ from artifacts_corepy.common import exceptions from talos.core import config from talos.core import exceptions as base_ex -from talos.utils import http +from talos.core.i18n import _ try: HAS_FCNTL = True @@ -39,10 +39,14 @@ shutil.register_unpack_format('war', ['.war'], shutil._UNPACK_FORMATS['zip'][1]) shutil.register_unpack_format('apk', ['.apk'], shutil._UNPACK_FORMATS['zip'][1]) +REGISTED_UNPACK_FORMATS = [ + '.tar.bz2', '.tbz2', '.tar.gz', '.tgz', '.tar', '.tar.xz', '.txz', '.zip', '.jar', '.war', '.apk' +] + def variable_parse(content, spliters): variables = [] - rule = re.compile(r'\[(' + '|'.join(spliters) + r')(.+?)\]') + rule = re.compile(r'\[(' + '|'.join(spliters) + r')(' + CONF.variable_expression + r')\]') stream = io.StringIO(content) lineno = 1 for line in stream: @@ -153,23 +157,25 @@ def _json_or_error(url, **kwargs): LOG.error('http error: %s %s, reason: %s', func.__name__.upper(), url, str(e)) raise base_ex.CallBackError(message={ 'code': 50002, - 'title': 'Connection Error', - 'description': 'Failed to establish a new connection' + 'title': _('Connection Error'), + 'description': _('Failed to establish a new connection') }) except requests.Timeout as e: LOG.error('http error: %s %s, reason: %s', func.__name__.upper(), url, str(e)) raise base_ex.CallBackError(message={ 'code': 50004, - 'title': 'Timeout Error', - 'description': 'Server do not respond' + 'title': _('Timeout Error'), + 'description': _('Server do not respond') }) except requests.HTTPError as e: LOG.error('http error: %s %s, reason: %s', func.__name__.upper(), url, str(e)) code = int(e.response.status_code) message = RestfulJson.get_response_json(e.response, default={'code': code}) - if code == 40004: - message['title'] = 'Not Found' - message['description'] = 'The resource you request not exist' + if code == 401: + raise base_ex.AuthError() + if code == 404: + message['title'] = _('Not Found') + message['description'] = _('The resource you request not exist') # 如果后台返回的数据不符合要求,强行修正 if 'code' not in message: message['code'] = code @@ -183,7 +189,7 @@ def _json_or_error(url, **kwargs): message = RestfulJson.get_response_json(e.response, default={ 'code': 500, - 'title': 'Server Error', + 'title': _('Server Error'), 'description': str(e) }) if 'code' not in message: diff --git a/artifacts-corepy/artifacts_corepy/common/wecube.py b/artifacts-corepy/artifacts_corepy/common/wecube.py new file mode 100644 index 0000000..c42ccbd --- /dev/null +++ b/artifacts-corepy/artifacts_corepy/common/wecube.py @@ -0,0 +1,59 @@ +# coding=utf-8 +""" +artifacts_corepy.common.wecube +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +本模块提供项目WeCube Client(Proxy) + +""" +import logging + +from artifacts_corepy.common import exceptions +from talos.core import config +from talos.core.i18n import _ +from artifacts_corepy.common import utils + +LOG = logging.getLogger(__name__) +CONF = config.CONF + + +class WeCubeClient(object): + """WeCube Client""" + def __init__(self, server, token): + self.server = server.rstrip('/') + self.token = token + + def build_headers(self): + return {'Authorization': 'Bearer ' + self.token} + + def check_response(self, resp_json): + if resp_json['status'] != 'OK': + # 当创建/更新条目错误,且仅有一个错误时,返回内部错误信息 + if isinstance(resp_json.get('data', None), list) and len(resp_json['data']) == 1: + if 'message' in resp_json['data'][0]: + raise exceptions.PluginError(message=resp_json['data'][0]['message']) + raise exceptions.PluginError(message=resp_json['message']) + + def get(self, url, param=None): + LOG.info('GET %s', url) + LOG.debug('Request: query - %s, data - None', str(param)) + resp_json = utils.RestfulJson.get(url, headers=self.build_headers(), params=param) + LOG.debug('Response: %s', str(resp_json)) + self.check_response(resp_json) + return resp_json + + def post(self, url, data, param=None): + LOG.info('POST %s', url) + LOG.debug('Request: query - %s, data - %s', str(param), str(data)) + resp_json = utils.RestfulJson.post(url, headers=self.build_headers(), params=param, json=data) + LOG.debug('Response: %s', str(resp_json)) + self.check_response(resp_json) + return resp_json + + def update(self, url_path, data): + url = self.server + url_path + return self.post(url, data) + + def retrieve(self, url_path): + url = self.server + url_path + return self.get(url) diff --git a/artifacts-corepy/artifacts_corepy/middlewares/auth.py b/artifacts-corepy/artifacts_corepy/middlewares/auth.py index f2867b9..7ad4fbd 100644 --- a/artifacts-corepy/artifacts_corepy/middlewares/auth.py +++ b/artifacts-corepy/artifacts_corepy/middlewares/auth.py @@ -23,8 +23,13 @@ def process_request(self, req, resp): if secret: verify_token = True try: - token_info = jwt.decode(token, key=utils.b64decode_key(secret), verify=verify_token) + decoded_secret = utils.b64decode_key(secret) + token_info = jwt.decode(token, key=decoded_secret, verify=verify_token) req.auth_user = token_info['sub'] + if verify_token: + # delay token + token_info['exp'] += 120 + req.auth_token = jwt.encode(token_info, decoded_secret, algorithm='HS512').decode() except jwt.exceptions.ExpiredSignatureError as e: raise base_ex.AuthError() except jwt.exceptions.DecodeError as e: diff --git a/artifacts-corepy/artifacts_corepy/server/scheduler.py b/artifacts-corepy/artifacts_corepy/server/scheduler.py index 84152e1..563db06 100644 --- a/artifacts-corepy/artifacts_corepy/server/scheduler.py +++ b/artifacts-corepy/artifacts_corepy/server/scheduler.py @@ -14,7 +14,7 @@ import time import datetime import logging -from tzlocal import get_localzone +from pytz import timezone from talos.core import config from talos.core import logging as mylogger @@ -92,7 +92,7 @@ def main(): scheduler = BlockingScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults, - timezone=get_localzone()) + timezone=timezone(CONF.timezone)) scheduler.add_job(cleanup_cached_dir, 'cron', hour='*') scheduler.add_job(rotate_log, 'cron', hour=3, minute=5) try: diff --git a/artifacts-corepy/artifacts_corepy/server/wsgi_server.py b/artifacts-corepy/artifacts_corepy/server/wsgi_server.py index debff5f..ae6c4a9 100644 --- a/artifacts-corepy/artifacts_corepy/server/wsgi_server.py +++ b/artifacts-corepy/artifacts_corepy/server/wsgi_server.py @@ -48,7 +48,8 @@ def decrypt_rsa(secret_key, encrypt_text): 'file_variable_prefix', 'default_special_replace', 'artifact_field', 's3_access_key', 's3_secret_key', 'nexus_server', 'nexus_repository', 'nexus_username', 'nexus_password', 'local_nexus_server', 'local_nexus_repository', 'local_nexus_username', 'local_nexus_password', 'gateway_url', - 'jwt_signing_key', 'use_remote_nexus_only') + 'diff_conf_extension', 'variable_expression', 'jwt_signing_key', 'use_remote_nexus_only', + 'nexus_sort_as_string', 'local_nexus_connector_port', 'nexus_connector_port') def get_env_value(value, origin_value): prefix = 'ENV@' encrypt_prefix = 'RSA@' diff --git a/artifacts-corepy/etc/artifacts_corepy.conf b/artifacts-corepy/etc/artifacts_corepy.conf index 6a4526a..edec59f 100644 --- a/artifacts-corepy/etc/artifacts_corepy.conf +++ b/artifacts-corepy/etc/artifacts_corepy.conf @@ -3,6 +3,7 @@ "locale_app": "artifacts_corepy", "locale_path": "/etc/locale/artifacts_corepy", "language": "en", + "timezone": "Asia/Shanghai", "server": { "bind": "0.0.0.0", "port": 9000 @@ -14,8 +15,8 @@ "ci_typeid_unit_design": "ENV@ARTIFACTS_CITYPE_UNIT_DESIGN", "ci_typeid_diff_config": "ENV@ARTIFACTS_CITYPE_DIFF_CONFIG", "ci_typeid_deploy_package": "ENV@ARTIFACTS_CITYPE_DEPLOY_PACKAGE", - "encrypt_variable_prefix": "ENV@ARTIFACTS_ENCRYPT_VARIBLE_PREFIX", - "file_variable_prefix": "ENV@ARTIFACTS_FILE_VARIBLE_PREFIX", + "encrypt_variable_prefix": "ENV@ARTIFACTS_ENCRYPT_VARIABLE_PREFIX", + "file_variable_prefix": "ENV@ARTIFACTS_FILE_VARIABLE_PREFIX", "default_special_replace": "ENV@ARTIFACTS_DEFAULT_SPECIAL_REPLACE", "artifact_field": "ENV@ARTIFACTS_CMDB_ARTIFACT_PATH", "s3_access_key": "ENV@WECUBE_S3_ACCESS_KEY", @@ -30,13 +31,18 @@ "local_nexus_password": "ENV@ARTIFACTS_LOCAL_NEXUS_PASSWORD", "gateway_url": "ENV@WECUBE_GATEWAY_URL", "jwt_signing_key": "ENV@WECUBE_JWT_SIGNING_KEY", - "use_remote_nexus_only": "ENV@ARTIFACTS_USE_REMOTE_NEXUS_ONLY" + "use_remote_nexus_only": "ENV@ARTIFACTS_USE_REMOTE_NEXUS_ONLY", + "diff_conf_extension": "ENV@ARTIFACTS_DIFF_CONF_EXTENSION", + "variable_expression": "ENV@ARTIFACTS_VARIABLE_EXPRESSION", + "nexus_sort_as_string": "ENV@ARTIFACTS_NEXUS_SORT_AS_STRING", + "local_nexus_connector_port": "ENV@ARTIFACTS_LOCAL_NEXUS_CONNECTOR_PORT", + "nexus_connector_port": "ENV@ARTIFACTS_NEXUS_CONNECTOR_PORT" }, "log": { "gunicorn_access": "/var/log/artifacts_corepy/access.log", "gunicorn_error": "/var/log/artifacts_corepy/error.log", "path": "/var/log/artifacts_corepy/server.log", - "level": "INFO", + "level": "DEBUG", "format_string": "%(asctime)s.%(msecs)03d %(process)d %(levelname)s %(name)s:%(lineno)d [-] %(message)s", "date_format_string": "%Y-%m-%d %H:%M:%S" }, @@ -48,7 +54,7 @@ "max_overflow": 5 }, "application": { - "names": ["artifacts_corepy.apps.package"] + "names": ["artifacts_corepy.apps.package", "artifacts_corepy.apps.plugin"] }, "rate_limit": { "enabled": false, @@ -60,12 +66,16 @@ "server": "${local_nexus_server}", "repository": "${local_nexus_repository}", "username": "${local_nexus_username}", - "password": "${local_nexus_password}" + "password": "${local_nexus_password}", + "connector_port": "${local_nexus_connector_port}" }, "encrypt_variable_prefix": "${encrypt_variable_prefix}", "file_variable_prefix": "${file_variable_prefix}", "default_special_replace": "${default_special_replace}", + "diff_conf_extension": "${diff_conf_extension}", + "variable_expression": "${variable_expression}", "use_remote_nexus_only": "${use_remote_nexus_only}", + "nexus_sort_as_string": "${nexus_sort_as_string}", "jwt_signing_key": "${jwt_signing_key}", "pakcage_cache_dir": "/tmp/artifacts/", "wecube": { @@ -78,7 +88,8 @@ "server": "${nexus_server}", "repository": "${nexus_repository}", "username": "${nexus_username}", - "password": "${nexus_password}" + "password": "${nexus_password}", + "connector_port": "${nexus_connector_port}" }, "wecmdb": { "artifact_field": "${artifact_field}", diff --git a/artifacts-corepy/requirements.txt b/artifacts-corepy/requirements.txt index 4bcb4eb..e681fe6 100644 --- a/artifacts-corepy/requirements.txt +++ b/artifacts-corepy/requirements.txt @@ -10,3 +10,4 @@ pycrypto gevent gunicorn apscheduler +pytz diff --git a/artifacts-ui/pom.xml b/artifacts-ui/pom.xml deleted file mode 100644 index 5d5391b..0000000 --- a/artifacts-ui/pom.xml +++ /dev/null @@ -1,98 +0,0 @@ - - 4.0.0 - - com.webank - wecube-plugins-artifacts - 0.0.1-SNAPSHOT - - artifacts-ui - - 1.6 - v10.16.0 - 6.9.0 - - - - build - - true - - - build - - - - plugin - - - buildType - plugin - - - - plugin - - - - - - - com.github.eirslett - frontend-maven-plugin - ${frontend-maven-plugin.version} - - - install node and npm - - install-node-and-npm - - - ${nodejs.version} - ${npm.version} - http://npm.taobao.org/mirrors/node/ - - - - npm install - - npm - - - --registry https://registry.npm.taobao.org install - --unsafe-perm - - - - npm rebuild node-sass - - npm - - - rebuild node-sass - - - - npm save core-js@2 - - npm - - - install --save core-js@2 - - - - npm run build - - npm - - - run ${buildType} - - - - - - - \ No newline at end of file diff --git a/artifacts-ui/src/components/auto-fill.js b/artifacts-ui/src/components/auto-fill.js index 8b90aeb..e408c9b 100644 --- a/artifacts-ui/src/components/auto-fill.js +++ b/artifacts-ui/src/components/auto-fill.js @@ -60,12 +60,6 @@ export default { }, watch: { value () { - if (!this.value && this.rootCiTypeId === 50) { - this.autoFillArray = [{ type: 'rule', value: '[{"ciTypeId":50}]' }] - } - if (!this.value && this.rootCiTypeId === 51) { - this.autoFillArray = [{ type: 'rule', value: '[{"ciTypeId":51}]' }] - } this.initAutoFillArray() }, allCiTypes () { diff --git a/artifacts-ui/src/locale/i18n/en-US.json b/artifacts-ui/src/locale/i18n/en-US.json index 6bde9b5..4154f29 100755 --- a/artifacts-ui/src/locale/i18n/en-US.json +++ b/artifacts-ui/src/locale/i18n/en-US.json @@ -71,10 +71,19 @@ "check_all": "Check All", "variable_select_key_tooltip": "Choose a Key(variable) and use it's Value", "root_ci": "Root CI", - "applications": "Applications", - "db_instance": "DB Instance", + "app": "Applications", + "db": "DB Instance", + "image": "image", + "mixed": "APP&DB", "file_compare": "File Compare", "original_content": "Original Content", "new_content": "New Content", - "comparison_result": "Comparison Result" + "comparison_result": "Comparison Result", + "package_type": "Package Yype", + "db_upgrade_directory": "DB Upgrade Directory", + "db_rollback_directory": "DB Rollback Directory", + "db_upgrade_file_path": "DB Upgrade File Path", + "db_rollback_file_path": "DB Rollback File Path", + "db_deploy_file_path": "DB Deploy File Path", + "detail": "Detail" } diff --git a/artifacts-ui/src/locale/i18n/zh-CN.json b/artifacts-ui/src/locale/i18n/zh-CN.json index adc87e4..30afe41 100755 --- a/artifacts-ui/src/locale/i18n/zh-CN.json +++ b/artifacts-ui/src/locale/i18n/zh-CN.json @@ -71,10 +71,19 @@ "check_all": "全选", "variable_select_key_tooltip": "选择已有的差异化变量使用其填充规则", "root_ci": "根CI", - "applications": "应用实例", - "db_instance": "数据库实例", + "app": "应用", + "db": "数据库", + "image": "镜像", + "mixed": "应用&数据库", "file_compare": "文件对比", "original_content": "原始内容", "new_content": "新内容", - "comparison_result": "比对结果" + "comparison_result": "比对结果", + "package_type": "部署包类型", + "db_upgrade_directory": "DB升级脚本目录", + "db_rollback_directory": "DB回滚脚本目录", + "db_upgrade_file_path": "DB升级脚本", + "db_rollback_file_path": "DB回滚脚本", + "db_deploy_file_path": "DB部署脚本", + "detail": "详情" } diff --git a/artifacts-ui/src/router-plugin.js b/artifacts-ui/src/router-plugin.js index f2c05d8..564784f 100644 --- a/artifacts-ui/src/router-plugin.js +++ b/artifacts-ui/src/router-plugin.js @@ -1,4 +1,4 @@ -import Artifacts from './views/artifacts2.vue' +import Artifacts from './views/artifacts.vue' export default [ { diff --git a/artifacts-ui/src/router.js b/artifacts-ui/src/router.js index b8db95e..fb5927f 100644 --- a/artifacts-ui/src/router.js +++ b/artifacts-ui/src/router.js @@ -1,6 +1,6 @@ import Vue from 'vue' import Router from 'vue-router' -import Artifacts from './views/artifacts2.vue' +import Artifacts from './views/artifacts.vue' Vue.use(Router) diff --git a/artifacts-ui/src/views/artifacts.vue b/artifacts-ui/src/views/artifacts.vue index 02afda1..4167949 100644 --- a/artifacts-ui/src/views/artifacts.vue +++ b/artifacts-ui/src/views/artifacts.vue @@ -1,12 +1,14 @@ diff --git a/artifacts-ui/src/views/artifacts2.vue b/artifacts-ui/src/views/artifacts2.vue deleted file mode 100644 index b3358bd..0000000 --- a/artifacts-ui/src/views/artifacts2.vue +++ /dev/null @@ -1,1583 +0,0 @@ - - - - - diff --git a/artifacts-ui/src/views/compare-file.vue b/artifacts-ui/src/views/compare-file.vue index 8bae181..79c7dc3 100644 --- a/artifacts-ui/src/views/compare-file.vue +++ b/artifacts-ui/src/views/compare-file.vue @@ -1,18 +1,18 @@