fix:完善多机证书导入功能
This commit is contained in:
parent
55a9cfbdbe
commit
d3ff2e3646
29
pom.xml
29
pom.xml
@ -95,6 +95,33 @@
|
|||||||
<version>2.13.1</version>
|
<version>2.13.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.apache.commons</groupId>-->
|
||||||
|
<!-- <artifactId>commons-collections4</artifactId>-->
|
||||||
|
<!-- <version>4.5.0</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
<!-- <!– https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 –>-->
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.apache.commons</groupId>-->
|
||||||
|
<!-- <artifactId>commons-lang3</artifactId>-->
|
||||||
|
<!-- <version>3.17.0</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.slf4j</groupId>-->
|
||||||
|
<!-- <artifactId>slf4j-api</artifactId>-->
|
||||||
|
<!-- <version>1.7.36</version>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
<!-- <!– https://mvnrepository.com/artifact/ch.qos.logback/logback-classic –>-->
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>ch.qos.logback</groupId>-->
|
||||||
|
<!-- <artifactId>logback-classic</artifactId>-->
|
||||||
|
<!-- <version>1.3.15</version>-->
|
||||||
|
<!-- <scope>compile</scope>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
<!--<dependency>-->
|
<!--<dependency>-->
|
||||||
<!--<groupId>club.fullstack</groupId>-->
|
<!--<groupId>club.fullstack</groupId>-->
|
||||||
<!--<artifactId>serialize</artifactId>-->
|
<!--<artifactId>serialize</artifactId>-->
|
||||||
@ -231,7 +258,7 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
<mainClass>com.Sample.TestDemo</mainClass>
|
<mainClass>com.Sample.TestCert</mainClass>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
<descriptorRefs>
|
<descriptorRefs>
|
||||||
|
|||||||
84
src/main/java/com/Sample/TestCert.java
Normal file
84
src/main/java/com/Sample/TestCert.java
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package com.Sample;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.StopWatch;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class TestCert {
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
StopWatch stopWatch = new StopWatch();
|
||||||
|
stopWatch.start();
|
||||||
|
System.out.println("-----------start---------------");
|
||||||
|
SydApi4j sydApi4j = new SydApi4j();
|
||||||
|
String ipInfo = "/app/test/ipInfo.txt";
|
||||||
|
String snmpInfo = "/app/test/snmp.txt";
|
||||||
|
String certInfo = "/app/test/cert.txt";
|
||||||
|
Map<String, String> map = readFileToMap(ipInfo);
|
||||||
|
String ipInfos = map.get("ipList");
|
||||||
|
List<String> ipList = Arrays.asList(ipInfos.split(","));
|
||||||
|
int timeout = Integer.parseInt(map.get("timout"));
|
||||||
|
String cert = readFileAsString(certInfo);
|
||||||
|
String bkCode = map.get("bkcode");
|
||||||
|
Map<String, String> snmpMap = readFileToMap(snmpInfo);
|
||||||
|
Gson gson = new Gson();
|
||||||
|
try {
|
||||||
|
sydApi4j.importCertToServers(ipList, timeout, cert, bkCode, gson.toJson(snmpMap));
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println();
|
||||||
|
System.out.println();
|
||||||
|
System.out.println();
|
||||||
|
System.out.println("---------------------证书导入失败日志----------------------");
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
System.out.println("------------end-----------------------");
|
||||||
|
stopWatch.stop();
|
||||||
|
System.out.println("耗时:" + stopWatch.getTotalTimeMillis() + "ms");
|
||||||
|
|
||||||
|
}
|
||||||
|
public static Map<String, String> readFileToMap(String filePath) {
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
// 跳过空行和注释行(假设以#开头的为注释)
|
||||||
|
if (line.trim().isEmpty() || line.startsWith("#")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按分隔符分割键值(例如"="或":")
|
||||||
|
String[] parts = line.split("=", 2); // 使用第二个参数限制分割次数
|
||||||
|
if (parts.length == 2) {
|
||||||
|
String key = parts[0].trim();
|
||||||
|
String value = parts[1].trim();
|
||||||
|
map.put(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String readFileAsString(String filePath) {
|
||||||
|
StringBuilder content = new StringBuilder();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
content.append(line).append("\n");
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return content.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -120,5 +120,5 @@ public interface SydApi extends
|
|||||||
|
|
||||||
public void setBindObject(Object bindObject);
|
public void setBindObject(Object bindObject);
|
||||||
|
|
||||||
void importCertToServers(List<String> addressList, int timeout, String cert);
|
void importCertToServers(List<String> addressList, int timeout, String cert, String bkCode, String snmpConfig);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -137,6 +137,16 @@ public class SydApiException extends RuntimeException{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public SydApiException(String msg, int retCode, boolean flag){
|
||||||
|
super(String.format(" %04X", retCode) + ":" + ( null == msg ? transMsg(retCode) : msg ));
|
||||||
|
this.retCode = retCode;
|
||||||
|
if (null == msg || "".equals(msg)){
|
||||||
|
transMsg();
|
||||||
|
}else {
|
||||||
|
this.msg = msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public SydApiException(String msg, int ret, Throwable e) {
|
public SydApiException(String msg, int ret, Throwable e) {
|
||||||
super("SydApiException(code =" + String.format(" %04x", ret) + " : " + "msg =" + (msg == null ? "未定义的错误" : msg) + ")", e);
|
super("SydApiException(code =" + String.format(" %04x", ret) + " : " + "msg =" + (msg == null ? "未定义的错误" : msg) + ")", e);
|
||||||
this.retCode = ret;
|
this.retCode = ret;
|
||||||
|
|||||||
@ -11,10 +11,8 @@ import java.util.Map;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
public class SnmpTrapSender {
|
public class SnmpTrapSender implements AutoCloseable{
|
||||||
private static volatile SnmpTrapSender instance;
|
|
||||||
private Snmp snmp;
|
private Snmp snmp;
|
||||||
private Properties config;
|
|
||||||
|
|
||||||
// 配置参数
|
// 配置参数
|
||||||
private String ip;
|
private String ip;
|
||||||
@ -23,50 +21,17 @@ public class SnmpTrapSender {
|
|||||||
private String community;
|
private String community;
|
||||||
private int version;
|
private int version;
|
||||||
|
|
||||||
|
public SnmpTrapSender(Map<String, String> map) throws IOException {
|
||||||
private SnmpTrapSender() throws IOException {
|
loadConfig(map);
|
||||||
// loadConfig();
|
|
||||||
ip = "172.16.18.113";
|
|
||||||
port = 162;
|
|
||||||
enterpriseOid = "1.3.6.1.4.1.2021.251.1";
|
|
||||||
community = "public";
|
|
||||||
version = 1;
|
|
||||||
initializeSNMP();
|
initializeSNMP();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SnmpTrapSender getInstance() throws IOException {
|
|
||||||
if (instance == null) {
|
|
||||||
synchronized (SnmpTrapSender.class) {
|
|
||||||
if (instance == null) {
|
|
||||||
instance = new SnmpTrapSender();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadConfig() {
|
|
||||||
try (InputStream input = getClass().getClassLoader().getResourceAsStream("test.properties")) {
|
|
||||||
config = new Properties();
|
|
||||||
config.load(input);
|
|
||||||
|
|
||||||
ip = config.getProperty("snmp.target.ip");
|
|
||||||
port = Integer.parseInt(config.getProperty("snmp.target.port"));
|
|
||||||
enterpriseOid = config.getProperty("enterprise.oid");
|
|
||||||
community = config.getProperty("snmp.community");
|
|
||||||
version = Integer.parseInt(config.getProperty("snmp.version"));
|
|
||||||
|
|
||||||
} catch (IOException | NumberFormatException e) {
|
|
||||||
throw new RuntimeException("加载配置文件失败", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadConfig(Map<String, String> map) {
|
public void loadConfig(Map<String, String> map) {
|
||||||
ip = map.getOrDefault("snmp.target.ip", null);
|
ip = map.getOrDefault("snmp.target.ip", null);
|
||||||
// port = Integer.parseInt(map.getOrDefault("snmp.target.port", "0"));
|
port = Integer.parseInt(map.getOrDefault("snmp.target.port", "162"));
|
||||||
enterpriseOid = map.getOrDefault("enterprise.oid", null);
|
enterpriseOid = map.getOrDefault("enterprise.oid", null);
|
||||||
community = map.getOrDefault("snmp.community", null);
|
community = map.getOrDefault("snmp.community", "public");
|
||||||
// version = Integer.parseInt(map.getOrDefault("snmp.version", "1"));
|
version = Integer.parseInt(map.getOrDefault("snmp.version", "1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeSNMP() throws IOException {
|
private void initializeSNMP() throws IOException {
|
||||||
@ -109,10 +74,10 @@ public class SnmpTrapSender {
|
|||||||
pdu.add(new VariableBinding(SnmpConstants.sysUpTime,
|
pdu.add(new VariableBinding(SnmpConstants.sysUpTime,
|
||||||
new TimeTicks(System.currentTimeMillis()/1000)));
|
new TimeTicks(System.currentTimeMillis()/1000)));
|
||||||
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID,
|
pdu.add(new VariableBinding(SnmpConstants.snmpTrapOID,
|
||||||
new OID(enterpriseOid + ".0.1")));
|
new OID(enterpriseOid + ".1.1")));
|
||||||
pdu.add(new VariableBinding(new OID(enterpriseOid + ".1.1"),
|
pdu.add(new VariableBinding(new OID(enterpriseOid + ".2.1"),
|
||||||
new OctetString(message)));
|
new OctetString(message)));
|
||||||
pdu.add(new VariableBinding(new OID(enterpriseOid + ".1.2"),
|
pdu.add(new VariableBinding(new OID(enterpriseOid + ".2.2"),
|
||||||
new Integer32(severity)));
|
new Integer32(severity)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,9 +91,11 @@ public class SnmpTrapSender {
|
|||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if (snmp != null) {
|
if (snmp != null) {
|
||||||
snmp.close();
|
snmp.close();
|
||||||
|
snmp = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -94,8 +94,9 @@ public class AsyncTaskExecutor {
|
|||||||
private <T> CompletableFuture<T> handleRetry(TaskContext<T> context, Throwable ex) {
|
private <T> CompletableFuture<T> handleRetry(TaskContext<T> context, Throwable ex) {
|
||||||
if (context.canRetry()) {
|
if (context.canRetry()) {
|
||||||
context.decreaseRetry();
|
context.decreaseRetry();
|
||||||
System.out.printf("[重试调度] ID: %s | 延迟: %ss | 剩余重试: %d | 错误: %s%n",
|
System.out.printf("[重试调度] ID: %s| IP:%s | 延迟: %ss | 剩余重试: %d | 错误: %s%n",
|
||||||
context.getTaskId(),
|
context.getTaskId(),
|
||||||
|
context.getAddress(),
|
||||||
context.getRetryDelay().getSeconds(),
|
context.getRetryDelay().getSeconds(),
|
||||||
context.getRetriesLeft(),
|
context.getRetriesLeft(),
|
||||||
ex.getCause().getMessage());
|
ex.getCause().getMessage());
|
||||||
|
|||||||
@ -25,7 +25,6 @@ import com.sunyard.trans.FullMode;
|
|||||||
import com.sunyard.trans.PacketSN;
|
import com.sunyard.trans.PacketSN;
|
||||||
import com.sunyard.trans.RoundMode;
|
import com.sunyard.trans.RoundMode;
|
||||||
import com.sunyard.util.*;
|
import com.sunyard.util.*;
|
||||||
import org.apache.commons.collections4.CollectionUtils;
|
|
||||||
import org.bouncycastle.asn1.*;
|
import org.bouncycastle.asn1.*;
|
||||||
import org.bouncycastle.asn1.cms.ContentInfo;
|
import org.bouncycastle.asn1.cms.ContentInfo;
|
||||||
import org.bouncycastle.cms.SydCmsUtil;
|
import org.bouncycastle.cms.SydCmsUtil;
|
||||||
@ -52,6 +51,7 @@ import java.security.cert.CertificateFactory;
|
|||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -1304,44 +1304,50 @@ public class SydApi4j implements SydApi {
|
|||||||
return dn;
|
return dn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initSnmpTrapConfig(String config) {
|
|
||||||
Gson gson = new Gson();
|
|
||||||
Map<String, String> map =
|
|
||||||
gson.fromJson(config, new TypeToken<Map<String, String>>() {}.getType());
|
|
||||||
try {
|
|
||||||
SnmpTrapSender.getInstance().loadConfig(map);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new SydApiException("snmp配置错误", -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void importCertToServers(List<String> addressList, int timeout, String cert) {
|
public void importCertToServers(List<String> addressList, int timeout, String cert, String bkCode, String snmpConfig) {
|
||||||
if (CollectionUtils.isEmpty(addressList)) {
|
if (null == addressList || addressList.isEmpty()) {
|
||||||
throw new RuntimeException("签名服务器地址不能为空");
|
throw new IllegalArgumentException("签名服务器地址不能为空");
|
||||||
|
}
|
||||||
|
if (null == cert || cert.length() == 0) {
|
||||||
|
throw new IllegalArgumentException("证书不能为空");
|
||||||
|
}
|
||||||
|
if (null == snmpConfig || snmpConfig.length() == 0) {
|
||||||
|
throw new IllegalArgumentException("snmp配置不能为空");
|
||||||
}
|
}
|
||||||
List<TaskContext<String>> taskContexts =
|
List<TaskContext<String>> taskContexts =
|
||||||
addressList.stream().map(p -> createTaskContext(p, timeout, cert)).collect(Collectors.toList());
|
addressList.stream().map(p -> createTaskContext(p, timeout, cert)).collect(Collectors.toList());
|
||||||
AsyncTaskExecutor executor = new AsyncTaskExecutor();
|
AsyncTaskExecutor executor = new AsyncTaskExecutor();
|
||||||
List<TaskOutcome<String>> taskOutcomes = executor.execBatchTasks(taskContexts);
|
List<TaskOutcome<String>> taskOutcomes = executor.execBatchTasks(taskContexts);
|
||||||
//失败的记录日志
|
//失败的记录日志
|
||||||
List<String> failures = taskOutcomes.stream()
|
List<String> failures = new ArrayList<>();
|
||||||
.filter(out -> !out.isSuccess()).map(task -> task.getContext().getAddress())
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
.collect(Collectors.toList());
|
stringBuilder.append("日期:").append(LocalDateTime.now()).append("\n");
|
||||||
if (CollectionUtils.isNotEmpty(failures)) {
|
for (TaskOutcome<String> taskOutcome : taskOutcomes) {
|
||||||
|
if (!taskOutcome.isSuccess()) {
|
||||||
|
failures.add(taskOutcome.getContext().getAddress());
|
||||||
|
stringBuilder.append("ip:").append(taskOutcome.getContext().getAddress()).append("/证书导入失败:").append(taskOutcome.getError().getMessage()).append("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!failures.isEmpty()) {
|
||||||
|
//记录失败日志
|
||||||
|
stringBuilder.append("行号:").append(bkCode).append("\n").append("证书:").append(cert);
|
||||||
//异步发送snmp trap消息
|
//异步发送snmp trap消息
|
||||||
String result = "[" + failures.stream()
|
String result = "[" + failures.stream()
|
||||||
.map(Object::toString)
|
.map(Object::toString)
|
||||||
.collect(Collectors.joining(",")) + "]";
|
.collect(Collectors.joining(",")) + "]";
|
||||||
System.out.println(result);
|
String message = "[SJJ1511] Certificate import failed - Servers:" + result + "| bkCode:[" + bkCode + "]";
|
||||||
String message = "servers:" + result + ",import cert:" + cert + ",failed!";
|
|
||||||
try {
|
Gson gson = new Gson();
|
||||||
SnmpTrapSender.getInstance().sendTrap(message, 1);
|
Map<String, String> map = gson.fromJson(snmpConfig, new TypeToken<Map<String, String>>() {}.getType());
|
||||||
} catch (Exception e) {
|
try (SnmpTrapSender sender = new SnmpTrapSender(map)) {
|
||||||
System.out.println("消息发送失败!");
|
sender.sendTrap(message, 2);
|
||||||
|
System.out.println("snmp告警消息发送成功:" + message);
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("snmp告警消息发送失败:" + e.getMessage());
|
||||||
}
|
}
|
||||||
|
throw new SydApiException(String.valueOf(stringBuilder), 56300, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -2548,6 +2554,7 @@ public class SydApi4j implements SydApi {
|
|||||||
while (this.retryWhenNetError > 0) {
|
while (this.retryWhenNetError > 0) {
|
||||||
try {
|
try {
|
||||||
println("Net error retry on " + this.retryWhenNetError);
|
println("Net error retry on " + this.retryWhenNetError);
|
||||||
|
System.out.println("Net error retry on ");
|
||||||
Socket socket = new Socket();
|
Socket socket = new Socket();
|
||||||
SocketAddress address = new InetSocketAddress(ip, port);
|
SocketAddress address = new InetSocketAddress(ip, port);
|
||||||
|
|
||||||
|
|||||||
@ -111,6 +111,6 @@ public class SimpleTest {
|
|||||||
map.put("snmp.community", "public");
|
map.put("snmp.community", "public");
|
||||||
map.put("snmp.version", "1");
|
map.put("snmp.version", "1");
|
||||||
// SnmpTrapSender.getInstance().loadConfig(map);
|
// SnmpTrapSender.getInstance().loadConfig(map);
|
||||||
SnmpTrapSender.getInstance().sendTrap("serve ip[127.0.0.1], import cert failed", 1);
|
// SnmpTrapSender.getInstance().sendTrap("serve ip[127.0.0.1], import cert failed", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,7 +22,7 @@ public class CertTest {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
//System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
|
||||||
SydApi4j api = (SydApi4j) new SydApi4j().connect("192.168.1.129", 8889, null, 1000);
|
SydApi4j api = (SydApi4j) new SydApi4j().connect("172.16.18.5", 8889, null, 1000);
|
||||||
try{
|
try{
|
||||||
List<String>dns = api.getAllCert();
|
List<String>dns = api.getAllCert();
|
||||||
System.out.println(Arrays.toString( dns.toArray() ) );
|
System.out.println(Arrays.toString( dns.toArray() ) );
|
||||||
|
|||||||
@ -1,23 +1,21 @@
|
|||||||
package sample;
|
package sample;
|
||||||
|
|
||||||
|
import cn.hutool.core.date.StopWatch;
|
||||||
|
import com.google.gson.Gson;
|
||||||
import com.sunyard.SydApiException;
|
import com.sunyard.SydApiException;
|
||||||
import com.sunyard.entity.ServerConfig;
|
|
||||||
import com.sunyard.proto.Util;
|
import com.sunyard.proto.Util;
|
||||||
import com.sunyard.snmp.SnmpTrapSender;
|
import com.sunyard.snmp.SnmpTrapSender;
|
||||||
import com.sunyard.util.CertUtil;
|
|
||||||
import com.sunyard.util.SYMUtil;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import racal.sunyard.main.SydApi4j;
|
import racal.sunyard.main.SydApi4j;
|
||||||
import racal.sunyard.main.SydApiBuilder;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.Map;
|
||||||
|
|
||||||
import static com.sunyard.util.CertUtil.convertToX509Cert;
|
import static com.sunyard.util.CertUtil.convertToX509Cert;
|
||||||
|
|
||||||
@ -29,26 +27,26 @@ public class Test4NongxinCW {
|
|||||||
|
|
||||||
byte[] orgData = new byte[100];
|
byte[] orgData = new byte[100];
|
||||||
|
|
||||||
// @Before
|
@Before
|
||||||
// public void before() {
|
public void before() {
|
||||||
// // 通过设置debug的属性开启debug,如果不设置则默认不打开
|
// 通过设置debug的属性开启debug,如果不设置则默认不打开
|
||||||
// api = (SydApi4j) new SydApi4j().connect("172.16.17.34", 8889, null, 1000);
|
api = (SydApi4j) new SydApi4j().connect("172.16.18.6", 8889, null, 1000);
|
||||||
//
|
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @Test
|
@Test
|
||||||
// public void connection() {
|
public void connection() {
|
||||||
//
|
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// @After
|
@After
|
||||||
// public void after() {
|
public void after() {
|
||||||
// try {
|
try {
|
||||||
// this.api.disconnect();
|
this.api.disconnect();
|
||||||
// } catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// e.printStackTrace();
|
e.printStackTrace();
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nakeSign(){
|
public void nakeSign(){
|
||||||
@ -99,6 +97,20 @@ public class Test4NongxinCW {
|
|||||||
"jY4S64AdH/IWiO+aUyfPAiAu+9MrvCfd27KGZQKurf/djfbe5xBQg2CG6ae+Tax9lQ==\n" +
|
"jY4S64AdH/IWiO+aUyfPAiAu+9MrvCfd27KGZQKurf/djfbe5xBQg2CG6ae+Tax9lQ==\n" +
|
||||||
"-----END CERTIFICATE-----";
|
"-----END CERTIFICATE-----";
|
||||||
|
|
||||||
|
String cert = "-----BEGIN CERTIFICATE-----\n" +
|
||||||
|
"MIICITCCAcWgAwIBAgIGAZbD4M8uMAwGCCqBHM9VAYN1BQAwSzELMAkGA1UEBhMC\n" +
|
||||||
|
"Q04xDjAMBgNVBAoTBUdNU1NMMRAwDgYDVQQLEwdQS0kvU00yMRowGAYDVQQDExFN\n" +
|
||||||
|
"aWRkbGVDQSBmb3IgVGVzdDAiGA8yMDI1MDUxMTE2MDAwMFoYDzIwMjYwNTExMTYw\n" +
|
||||||
|
"MDAwWjBQMQswCQYDVQQGDAJDTjERMA8GA1UECAwIaGFuZ3pob3UxETAPBgNVBAcM\n" +
|
||||||
|
"CGhhbmd6aG91MQwwCgYDVQQKDANzc3MxDTALBgNVBAMMBHNzc3MwWTATBgcqhkjO\n" +
|
||||||
|
"PQIBBggqgRzPVQGCLQNCAARGnQQLJSiMkoH2iCqbsTpGkZmHPCP7ThQQmzf1m8jr\n" +
|
||||||
|
"NjgMzBJ0p2rXJQSglaVqaVcaL/ox5wfV8okIavToNQjao4GJMIGGMBsGA1UdIwQU\n" +
|
||||||
|
"MBKAEPl/VbQnlDNiplbKb8xdGv8wGQYDVR0OBBIEEMu/aP+Gw3PMakxFSVzC5MYw\n" +
|
||||||
|
"MQYIKwYBBQUHAQEEJTAjMCEGCCsGAQUFBzABhhVodHRwczovL29jc3AuZ21zc2wu\n" +
|
||||||
|
"Y24wCQYDVR0TBAIwADAOBgNVHQ8BAf8EBAMCAMAwDAYIKoEcz1UBg3UFAANIADBF\n" +
|
||||||
|
"AiEAnoN1iWj1Cv6M4eUtSt2XEoKpswLTEy7gczIn/ZTI+JYCIB2but+jsuUD6Udp\n" +
|
||||||
|
"uFp7xVvOp8IKQyazXx9bA4N9PbLo\n" +
|
||||||
|
"-----END CERTIFICATE-----";
|
||||||
|
|
||||||
//将证书导入设备,获取证书主题
|
//将证书导入设备,获取证书主题
|
||||||
String dn = api.importCertAndGetDN(certBySign);
|
String dn = api.importCertAndGetDN(certBySign);
|
||||||
@ -174,33 +186,64 @@ public class Test4NongxinCW {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void importAllCert() {
|
public void importAllCert() {
|
||||||
|
StopWatch stopWatch = new StopWatch();
|
||||||
|
stopWatch.start();
|
||||||
|
String cert = "-----BEGIN CERTIFICATE-----\n" +
|
||||||
|
"MIICMDCCAdagAwIBAgIJAOWoGwJCnjlLMAoGCCqBHM9VAYN1MGcxCzAJBgNVBAYT\n" +
|
||||||
|
"AkNOMRAwDgYDVQQIDAdCZWlqaW5nMRAwDgYDVQQHDAdIYWlEaWFuMRMwEQYDVQQK\n" +
|
||||||
|
"DApHTUNlcnQub3JnMR8wHQYDVQQDDBZHTUNlcnQgR00gUm9vdCBDQSAtIDAxMB4X\n" +
|
||||||
|
"DTI1MDUxMzAyMzUxM1oXDTI2MDUxMzAyMzUxM1owRTELMAkGA1UEBgwCQ04xCzAJ\n" +
|
||||||
|
"BgNVBAgMAnp6MQswCQYDVQQHDAJ6ejEMMAoGA1UECgwDenp6MQ4wDAYDVQQDDAV6\n" +
|
||||||
|
"enp6ejBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABK5fyRSACWPIBBr/3U+qvjvp\n" +
|
||||||
|
"niropv2PrA3ZLU1bG8vQpwAEMA0kwyqGgXURBPbvWdAhN7s9wfvHbJyMd9CMZGqj\n" +
|
||||||
|
"gYwwgYkwDAYDVR0TAQH/BAIwADALBgNVHQ8EBAMCB4AwLAYJYIZIAYb4QgENBB8W\n" +
|
||||||
|
"HUdNQ2VydC5vcmcgU2lnbmVkIENlcnRpZmljYXRlMB0GA1UdDgQWBBSQB7UUqEuG\n" +
|
||||||
|
"kl4mrx3MAhKTptfbYjAfBgNVHSMEGDAWgBR/Wl47AIRZKg+YvqEObzmVQxBNBzAK\n" +
|
||||||
|
"BggqgRzPVQGDdQNIADBFAiEA4kgTdn/iA/XYfjTEeOcELwBXd0tTFghkpfP8DhBF\n" +
|
||||||
|
"KFQCIAXZTL6VPtW67tM/DF4ZgFt5S/YIiFombKa2bWOfL81u\n" +
|
||||||
|
"-----END CERTIFICATE-----";
|
||||||
|
String cert1 = "-----BEGIN CERTIFICATE-----\n" +
|
||||||
|
"MIIDNTCCAh2gAwIBAgIFE2lVIzcwDQYJKoZIhvcNAQEFBQAwITELMAkGA1UEBhMC\n" +
|
||||||
|
"Q04xEjAQBgNVBAoTCUNGQ0EgT0NBMTAeFw0yMjA0MjIwODIyMTVaFw0yNzA0MjIw\n" +
|
||||||
|
"ODIyMTVaMGQxCzAJBgNVBAYTAkNOMRIwEAYDVQQKEwlDRkNBIE9DQTExDzANBgNV\n" +
|
||||||
|
"BAsTBlRQQy1TMzEVMBMGA1UECxMMSW5kaXZpZHVhbC0xMRkwFwYDVQQDExBBUkNV\n" +
|
||||||
|
"NTIwMDAyNTI3MDg1MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC74cciiBbs\n" +
|
||||||
|
"UcfmBxVlMWtsILsc85erRngknMaMW6wFyBsfJGrcL0TC7EO5XIsJXRrJN32vWdoo\n" +
|
||||||
|
"6Qm8WUGour+hrSNDzJ7cVtNA1ragPIjlkmMgbDLD0UsEbjHmA902079Fktv35L4l\n" +
|
||||||
|
"Hqe/vBygXcUoWJsGY5+8ykmI8W0LYPRkVwIDAQABo4G0MIGxMB8GA1UdIwQYMBaA\n" +
|
||||||
|
"FNHb6YiC5d0aj0yqAIy+fPKrG/bZMDgGA1UdHwQxMC8wLaAroCmGJ2h0dHA6Ly9j\n" +
|
||||||
|
"cmwuY2ZjYS5jb20uY24vUlNBL2NybDMwMDgyLmNybDALBgNVHQ8EBAMCA+gwHQYD\n" +
|
||||||
|
"VR0OBBYEFMM/joQIWvRFdLOx0HarTXAtZk7RMCgGA1UdJQQhMB8GCCsGAQUFBwMC\n" +
|
||||||
|
"BggrBgEFBQcDBAYJKoZIhvcvAQEFMA0GCSqGSIb3DQEBBQUAA4IBAQANEhHR34gg\n" +
|
||||||
|
"iivRj9VTO2lS0StsHQy4A4seq9YmaQa6QZDUEndFwcxr5DN6CAf6xA1YiHG5FoYd\n" +
|
||||||
|
"yH4sLORUhdfzQUWle7ZTPn4TfFjddPh3RDHXMXOh+pHNgrSjXpdACwadHedJO5e4\n" +
|
||||||
|
"HqXI8wEoNovDA8HT4ucMmsZHbIyQMhxW9k+DgTHx8nvltqv3reoDou3ekQ1tSWvW\n" +
|
||||||
|
"nUteKp64vop1ktgiIKjLshKRaUmZO4/fo9EcJHCH51J6f/2TAM7S+IaFzXR/VWf3\n" +
|
||||||
|
"JaiaGbT8kAJI854SmKWqRE8HVzMo/NYNdP0M7xZ2kt68PVE/UIl20xWii9P2/7Oh\n" +
|
||||||
|
"KYAnLX1nKjbg\n" +
|
||||||
|
"-----END CERTIFICATE-----";
|
||||||
List<String> masterNodes = new ArrayList<>();
|
List<String> masterNodes = new ArrayList<>();
|
||||||
masterNodes.add("192.168.55.80:3336");
|
// masterNodes.add("172.16.18.99:8889");
|
||||||
masterNodes.add("172.16.128.2:8889");
|
masterNodes.add("172.16.18.5:8889");
|
||||||
masterNodes.add("172.16.128.3:8889");
|
masterNodes.add("172.16.18.6:8889");
|
||||||
masterNodes.add("172.16.128.4:8889");
|
// masterNodes.add("172.16.18.7:8889");
|
||||||
|
// masterNodes.add("172.16.18.8:8889");
|
||||||
int timeout = 1000;
|
int timeout = 1000;
|
||||||
String pwd = null;
|
Map<String, String> map = new HashMap<>();
|
||||||
String cert = "";
|
map.put("snmp.target.ip", "172.16.18.113");
|
||||||
|
map.put("snmp.target.port", "162");
|
||||||
|
map.put("enterprise.oid", "1.3.6.1.4.1.9999");
|
||||||
|
map.put("snmp.community", "public");
|
||||||
|
map.put("snmp.version", "1");
|
||||||
SydApi4j sydApi4j = new SydApi4j();
|
SydApi4j sydApi4j = new SydApi4j();
|
||||||
// sydApi4j.initSnmpTrapConfig("");
|
sydApi4j.importCertToServers(masterNodes, timeout, cert1, "12345678", new Gson().toJson(map));
|
||||||
sydApi4j.importCertToServers(masterNodes, timeout, cert);
|
stopWatch.stop();
|
||||||
|
System.out.println(stopWatch.getTotalTimeSeconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void snmpTest() {
|
public void snmpTest() {
|
||||||
try {
|
|
||||||
SnmpTrapSender sender = SnmpTrapSender.getInstance();
|
|
||||||
|
|
||||||
// 发送不同级别的日志
|
|
||||||
sender.sendTrap("Application started", 0);
|
|
||||||
sender.sendTrap("High CPU usage", 1);
|
|
||||||
sender.sendTrap("Database connection lost", 5);
|
|
||||||
|
|
||||||
sender.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user