Skip to content

Commit 61b0e31

Browse files
author
fulei
committed
public key pem auth
1 parent d1b100f commit 61b0e31

File tree

9 files changed

+180
-55
lines changed

9 files changed

+180
-55
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.sohu.cache.enums;
2+
3+
/**
4+
* ssh授权方式
5+
* @author leifu
6+
* @date 2018年6月15日
7+
* @time 下午5:56:59
8+
*/
9+
public enum SshAuthTypeEnum {
10+
11+
PASSWORD(1, "用户密码"),
12+
PUBLIC_KEY(2, "公钥");
13+
14+
private int value;
15+
16+
private String info;
17+
18+
private SshAuthTypeEnum(int value, String info) {
19+
this.value = value;
20+
this.info = info;
21+
}
22+
23+
public int getValue() {
24+
return value;
25+
}
26+
27+
public String getInfo() {
28+
return info;
29+
}
30+
31+
32+
}

cachecloud-open-common/src/main/java/com/sohu/cache/util/ConstUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.List;
55
import java.util.ResourceBundle;
66

7+
import com.sohu.cache.enums.SshAuthTypeEnum;
8+
79
/**
810
* cachecloud常量
911
* @author leifu
@@ -227,6 +229,12 @@ public static String getRedisMigrateToolDir() {
227229
public static String DEFAULT_CACHECLOUD_BASE_DIR = "/opt";
228230
public static String CACHECLOUD_BASE_DIR = DEFAULT_CACHECLOUD_BASE_DIR;
229231

232+
/**
233+
* MNON_DIR根目录
234+
*/
235+
public static String DEFAULT_NMON_DIR = "/opt/cachecloud";
236+
public static String NMON_DIR = DEFAULT_NMON_DIR;
237+
230238
/**
231239
* 是否定期清理各种统计数据:(详见CleanUpStatisticsJob)
232240
*/
@@ -248,6 +256,18 @@ public static String getRedisMigrateToolDir() {
248256
public static int MACHINE_STATS_CRON_MINUTE = DEFAULT_MACHINE_STATS_CRON_MINUTE;
249257

250258

259+
/**
260+
* ssh授权方式:参考SshAuthTypeEnum
261+
*/
262+
public static int DEFAULT_SSH_AUTH_TYPE = SshAuthTypeEnum.PASSWORD.getValue();
263+
public static int SSH_AUTH_TYPE = DEFAULT_SSH_AUTH_TYPE;
264+
265+
/**
266+
* public key pem
267+
*/
268+
public static String DEFAULT_PUBLIC_KEY_PEM = "";
269+
public static String PUBLIC_KEY_PEM = DEFAULT_PUBLIC_KEY_PEM;
270+
251271
/**
252272
* 网站域名
253273
*/

cachecloud-open-web/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@
159159
<groupId>commons-lang</groupId>
160160
<artifactId>commons-lang</artifactId>
161161
</dependency>
162+
163+
<dependency>
164+
<groupId>commons-io</groupId>
165+
<artifactId>commons-io</artifactId>
166+
</dependency>
162167

163168
<dependency>
164169
<groupId>net.sf.json-lib</groupId>

cachecloud-open-web/src/main/java/com/sohu/cache/server/nmon/NMONService.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.sohu.cache.ssh.SSHTemplate.DefaultLineProcessor;
1111
import com.sohu.cache.ssh.SSHTemplate.Result;
1212
import com.sohu.cache.ssh.SSHTemplate.SSHSession;
13+
import com.sohu.cache.util.ConstUtils;
1314
import com.sohu.cache.util.NMONFileFactory;
1415
import com.sohu.cache.util.OSFactory;
1516
/**
@@ -20,13 +21,7 @@ public class NMONService {
2021
//获取系统版本位数命令
2122
public static final String OS_INFO_CMD = "/bin/uname -a; /bin/cat /etc/issue";
2223
//nmon路径
23-
public static final String NMON_DIR = "/opt/cachecloud/soft/";
24-
//nmon文件名
2524
public static final String NMON = "nmon";
26-
//nmon完整路径
27-
public static final String NMON_FILE = NMON_DIR + NMON;
28-
//获取nmon版本
29-
public static final String NMON_VERSION = "[ -e \""+NMON_FILE+"\" ] && "+NMON_FILE+" -V";
3025
//nmon输出的结果文件
3126
public static final String NMON_LOG = "/tmp/nmon.log";
3227
//nmon输出的老结果文件
@@ -35,12 +30,6 @@ public class NMONService {
3530
public static final String SOCK_LOG = "/tmp/sock.log";
3631
//ulimit输出的结果文件
3732
public static final String ULIMIT_LOG = "/tmp/ulimit.log";
38-
//nmon监控启动
39-
public static final String START_SERVER_COLLECT = NMON_FILE+" -F " + NMON_LOG + " -s0 -c1;" +
40-
"/bin/grep TCP /proc/net/sockstat > " + SOCK_LOG +
41-
";ulimit -n -u > " + ULIMIT_LOG;
42-
//创建nmon路径
43-
public static final String MK_NMON_DIR = "/bin/mkdir -p /opt/cachecloud/soft/";
4433

4534
/**
4635
* 启动nmon收集系统状况
@@ -49,7 +38,7 @@ public class NMONService {
4938
* @return @OSInfo 收集到的操作系统信息
5039
*/
5140
public OSInfo start(String ip, SSHSession session) {
52-
Result startCollectResult = session.executeCommand(START_SERVER_COLLECT);
41+
Result startCollectResult = session.executeCommand(getStartServerCollect());
5342
if(!startCollectResult.isSuccess()) {
5443
logger.error("start nmon "+ip+" err:"+startCollectResult.getResult(),
5544
startCollectResult.getExcetion());
@@ -62,6 +51,7 @@ public OSInfo start(String ip, SSHSession session) {
6251
return null;
6352
}
6453

54+
6555
/**
6656
* 尝试修复启动失败的错误
6757
* @param ip
@@ -75,12 +65,12 @@ private OSInfo initNmon(String ip, SSHSession session) {
7565
OS os = null;
7666
//nmon文件不存在,需要根据操作系统识别是否支持
7767
if(null == version) {
78-
logger.warn("{} not exist {}", ip, NMON_FILE);
68+
logger.warn("{} not exist {}", ip, getNmonFile());
7969
//将原始信息转换为可识别的操作系统
8070
os = OSFactory.getOS(osInfo);
8171
} else {
8272
//nmon存在,但是版本有问题,此时不应该再判断系统信息了,直接用默认的
83-
logger.warn("{} {} version err:"+version, ip, NMON_FILE);
73+
logger.warn("{} {} version err:"+version, ip, getNmonFile());
8474
os = OSFactory.getDefaultOS(osInfo);
8575
}
8676
if(os == null) {
@@ -106,11 +96,11 @@ private OSInfo initNmon(String ip, SSHSession session) {
10696
* @return 存在返回版本,不存在返回null, 执行错误返回异常
10797
*/
10898
private String getNMONVersion(String ip, SSHSession session) {
109-
Result nmonVersionResult = session.executeCommand(NMON_VERSION);
99+
Result nmonVersionResult = session.executeCommand(getNmonVersion());
110100
if(nmonVersionResult.isSuccess()) {
111101
return nmonVersionResult.getResult();
112102
} else {
113-
logger.error(NMON_VERSION+" err:"+nmonVersionResult.getResult(), nmonVersionResult.getExcetion());
103+
logger.error(getNmonVersion()+" err:"+nmonVersionResult.getResult(), nmonVersionResult.getExcetion());
114104
}
115105
return null;
116106
}
@@ -144,16 +134,43 @@ public void process(String line, int lineNum) throws Exception {
144134
* @param nmonFile
145135
*/
146136
private void sendNMONToServer(String ip, SSHSession session, File nmonFile) {
147-
Result mkResult = session.executeCommand(MK_NMON_DIR);
137+
Result mkResult = session.executeCommand(getMkNmonDir());
148138
if(!mkResult.isSuccess()) {
149139
logger.error("mkdir err:"+mkResult.getResult(), mkResult.getExcetion());
150140
return;
151141
}
152-
Result scpRst = session.scpToFile(nmonFile.getAbsolutePath(), NMON, NMON_DIR);
142+
Result scpRst = session.scpToFile(nmonFile.getAbsolutePath(), NMON, getMmonDir());
153143
if(scpRst.isSuccess()) {
154144
logger.info("scp {} to {} success", nmonFile.getAbsolutePath(), ip);
155145
} else {
156146
logger.error("scp to "+ip+" err", scpRst.getExcetion());
157147
}
158148
}
149+
150+
/**
151+
* nmon监控启动
152+
* @return
153+
*/
154+
private String getStartServerCollect() {
155+
return getNmonFile() +" -F " + NMON_LOG + " -s0 -c1;" +
156+
"/bin/grep TCP /proc/net/sockstat > " + SOCK_LOG +
157+
";ulimit -n -u > " + ULIMIT_LOG;
158+
}
159+
160+
private String getMmonDir() {
161+
return ConstUtils.NMON_DIR;
162+
}
163+
164+
private String getNmonFile() {
165+
return getMmonDir() + "/" + NMON;
166+
}
167+
168+
private String getMkNmonDir() {
169+
return "/bin/mkdir -p " + getMmonDir();
170+
}
171+
172+
private String getNmonVersion() {
173+
return "[ -e \""+ getNmonFile() +"\" ] && "+ getNmonFile() +" -V";
174+
}
175+
159176
}

cachecloud-open-web/src/main/java/com/sohu/cache/ssh/SSHTemplate.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sohu.cache.ssh;
22

33
import java.io.BufferedReader;
4+
import java.io.File;
45
import java.io.IOException;
56
import java.io.InputStream;
67
import java.io.InputStreamReader;
@@ -21,6 +22,7 @@
2122
import ch.ethz.ssh2.StreamGobbler;
2223

2324
import com.google.common.util.concurrent.ThreadFactoryBuilder;
25+
import com.sohu.cache.enums.SshAuthTypeEnum;
2426
import com.sohu.cache.exception.SSHException;
2527
import com.sohu.cache.util.ConstUtils;
2628
/**
@@ -77,10 +79,21 @@ private Connection getConnection(String ip, int port,
7779
String username, String password) throws Exception {
7880
Connection conn = new Connection(ip, port);
7981
conn.connect(null, CONNCET_TIMEOUT, CONNCET_TIMEOUT);
80-
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
82+
boolean isAuthenticated = false;
83+
if (ConstUtils.SSH_AUTH_TYPE == SshAuthTypeEnum.PASSWORD.getValue()) {
84+
isAuthenticated = conn.authenticateWithPassword(username, password);
85+
} else if (ConstUtils.SSH_AUTH_TYPE == SshAuthTypeEnum.PUBLIC_KEY.getValue()) {
86+
isAuthenticated = conn.authenticateWithPublicKey(username, new File(ConstUtils.PUBLIC_KEY_PEM), password);
87+
}
8188
if (isAuthenticated == false) {
82-
throw new Exception("SSH authentication failed with [ userName: " +
83-
username + ", password: " + password + "]");
89+
if (ConstUtils.SSH_AUTH_TYPE == SshAuthTypeEnum.PASSWORD.getValue()) {
90+
throw new Exception("SSH authentication failed with [ userName: " +
91+
username + ", password: " + password + "]");
92+
} else if (ConstUtils.SSH_AUTH_TYPE == SshAuthTypeEnum.PUBLIC_KEY.getValue()) {
93+
throw new Exception("SSH authentication failed with [ userName: " +
94+
username + ", pemfile: " + ConstUtils.PUBLIC_KEY_PEM + "]");
95+
}
96+
8497
}
8598
return conn;
8699
}

cachecloud-open-web/src/main/java/com/sohu/cache/web/service/impl/ConfigServiceImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ public void reloadSystemConfig() {
172172
ConstUtils.MACHINE_STATS_CRON_MINUTE = MapUtils.getIntValue(configMap, "cachecloud.machine.stats.cron.minute", ConstUtils.DEFAULT_MACHINE_STATS_CRON_MINUTE);
173173
logger.info("{}: {}", "ConstUtils.MACHINE_STATS_CRON_MINUTE", ConstUtils.MACHINE_STATS_CRON_MINUTE);
174174

175+
//ssh授权方式
176+
ConstUtils.SSH_AUTH_TYPE = MapUtils.getIntValue(configMap, "cachecloud.ssh.auth.type", ConstUtils.DEFAULT_SSH_AUTH_TYPE);
177+
logger.info("{}: {}", "ConstUtils.SSH_AUTH", ConstUtils.SSH_AUTH_TYPE);
178+
179+
//public key pem
180+
ConstUtils.PUBLIC_KEY_PEM = MapUtils.getString(configMap, "cachecloud.public.key.pem", ConstUtils.DEFAULT_PUBLIC_KEY_PEM);
181+
logger.info("{}: {}", "ConstUtils.PUBLIC_KEY_PEM", ConstUtils.PUBLIC_KEY_PEM);
182+
183+
//nmon根目录
184+
ConstUtils.NMON_DIR = MapUtils.getString(configMap, "cachecloud.nmon.dir", ConstUtils.DEFAULT_NMON_DIR);
185+
logger.info("{}: {}", "ConstUtils.NMON_DIR", ConstUtils.NMON_DIR);
175186

176187
logger.info("===========ConfigServiceImpl reload config end============");
177188
}

cachecloud-open-web/src/main/webapp/WEB-INF/jsp/manage/config/initConfigDetail.jsp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@
6464
</option>
6565
</select>
6666
</c:when>
67+
<c:when test="${config.configKey == 'cachecloud.ssh.auth.type'}">
68+
<select name="${config.configKey}" class="form-control">
69+
<option value="1" <c:if test="${config.configValue == '1'}">selected</c:if>>
70+
密码
71+
</option>
72+
<option value="2" <c:if test="${config.configValue == '2'}">selected</c:if>>
73+
public key
74+
</option>
75+
</select>
76+
</c:when>
6777
<c:otherwise>
6878
<input type="text" name="${config.configKey}" class="form-control" value="${config.configValue}" />
6979
</c:otherwise>

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<maven.clean.plugin>2.5</maven.clean.plugin>
3636
<ganymed.ssh.version>build210</ganymed.ssh.version>
3737
<commons.collections.version>3.2.1</commons.collections.version>
38+
<commons.io.version>2.4</commons.io.version>
3839
<guava.version>15.0</guava.version>
3940
<commons-lang.version>2.3</commons-lang.version>
4041
<json-lib.version>2.2.1</json-lib.version>
@@ -142,6 +143,12 @@
142143
<artifactId>commons-collections4</artifactId>
143144
<version>${apache.commons.collections}</version>
144145
</dependency>
146+
147+
<dependency>
148+
<groupId>commons-io</groupId>
149+
<artifactId>commons-io</artifactId>
150+
<version>${commons.io.version}</version>
151+
</dependency>
145152

146153
<dependency>
147154
<groupId>com.google.guava</groupId>

0 commit comments

Comments
 (0)