1 package cn.home1.cloud.config.server.ssh;
2
3 import static org.apache.commons.lang3.StringUtils.isNotBlank;
4 import static org.springframework.util.FileCopyUtils.copyToString;
5
6 import cn.home1.cloud.config.server.util.Consts;
7 import cn.home1.cloud.config.server.util.ResourceUtils;
8
9 import lombok.SneakyThrows;
10 import lombok.extern.slf4j.Slf4j;
11
12 import org.springframework.cloud.config.server.ssh.SshUriProperties;
13
14 import java.io.File;
15 import java.io.FileReader;
16
17 @Slf4j
18 public class DeployKey {
19
20 private final String privateKeyLocation;
21
22 public DeployKey(final String privateKeyLocation) {
23 this.privateKeyLocation = privateKeyLocation;
24 }
25
26 public static String getPrivateKeyPath(final String privateKeyLocation) {
27 return ResourceUtils.findResourceFile(privateKeyLocation, Consts.DATA_DIRECTORY);
28 }
29
30 public void setUp(final SshUriProperties sshUriProperties) {
31 final String privateKey = this.getPrivateKey();
32
33
34 if (sshUriProperties == null) {
35
36
37
38 System.setProperty("spring.cloud.config.server.git.private-key", privateKey);
39 System.setProperty("spring.cloud.config.server.git.ignore-local-ssh-settings", "true");
40 } else {
41 log.info("ssh uri properties privateKey is not blank: {}", isNotBlank(sshUriProperties.getPrivateKey()));
42 log.info("ssh uri properties is ignore local ssh settings: {}", sshUriProperties.isIgnoreLocalSshSettings());
43 log.info("ssh uri properties is strict host key checking: {}", sshUriProperties.isStrictHostKeyChecking());
44
45 sshUriProperties.setPrivateKey(privateKey);
46 sshUriProperties.setIgnoreLocalSshSettings(true);
47 }
48 }
49
50 @SneakyThrows
51 public String getPrivateKey() {
52 return copyToString(new FileReader(new File(this.getPrivateKeyPath())));
53 }
54
55 public String getPrivateKeyPath() {
56 return getPrivateKeyPath(this.privateKeyLocation);
57 }
58
59 @SneakyThrows
60 public String getPublicKey() {
61 return copyToString(new FileReader(new File(this.getPublicKeyPath())));
62 }
63
64 public String getPublicKeyPath() {
65 return this.getPrivateKeyPath() + ".pub";
66 }
67 }