|
27 | 27 | import hudson.plugins.ec2.EC2Cloud; |
28 | 28 | import hudson.plugins.ec2.EC2Computer; |
29 | 29 | import hudson.slaves.OfflineCause; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
30 | 32 | import java.util.Base64; |
31 | 33 |
|
32 | 34 | import java.io.IOException; |
| 35 | +import java.util.Scanner; |
33 | 36 | import java.util.logging.Level; |
34 | 37 | import java.util.logging.Logger; |
35 | 38 |
|
|
47 | 50 | public class CheckStaticStrategy extends SshHostKeyVerificationStrategy { |
48 | 51 | private static final Logger LOGGER = Logger.getLogger(CheckStaticStrategy.class.getName()); |
49 | 52 |
|
| 53 | + private ArrayList<HostKey> getStaticHostKeys(EC2Computer computer) { |
| 54 | + ArrayList<HostKey> hostKeys = new ArrayList<>(); |
| 55 | + |
| 56 | + Scanner scanner = new Scanner(computer.getSlaveTemplate().getStaticHostKeys()); |
| 57 | + while (scanner.hasNextLine()) { |
| 58 | + String hostKeyString = scanner.nextLine(); |
| 59 | + String[] hostKeyParts = hostKeyString.split(" "); |
| 60 | + if (hostKeyParts.length != 2) { |
| 61 | + EC2Cloud.log(LOGGER, Level.WARNING, computer.getListener(), "invalid static SSH key"); |
| 62 | + continue; |
| 63 | + } |
| 64 | + HostKey hostKey = new HostKey(hostKeyParts[0], Base64.getDecoder().decode(hostKeyParts[1])); |
| 65 | + hostKeys.add(hostKey); |
| 66 | + } |
| 67 | + scanner.close(); |
| 68 | + return hostKeys; |
| 69 | + } |
| 70 | + |
50 | 71 | @Override |
51 | 72 | public boolean verify(EC2Computer computer, HostKey hostKey, TaskListener listener) throws IOException { |
52 | 73 | HostKey existingHostKey = HostKeyHelper.getInstance().getHostKey(computer); |
53 | | - if (null == existingHostKey) { |
54 | | - byte[] key = Base64.getDecoder().decode("AAAAC3NzaC1lZDI1NTE5AAAAIFGNRfg0pVrEdViJgKEdRKqFRG6kS/jOnFQC+wa5cp0v"); |
55 | | - HostKey staticHostKey = new HostKey("ssh-ed25519", key); |
| 74 | + ArrayList<HostKey> staticHostKeys = getStaticHostKeys(computer); |
56 | 75 |
|
57 | | - if (hostKey.equals(staticHostKey)) { |
58 | | - HostKeyHelper.getInstance().saveHostKey(computer, hostKey); |
59 | | - EC2Cloud.log(LOGGER, Level.INFO, computer.getListener(), String.format("The SSH key %s %s has been successfully checked against the instance console for connections to %s", hostKey.getAlgorithm(), hostKey.getFingerprint(), computer.getName())); |
60 | | - return true; |
61 | | - } |
| 76 | + if (staticHostKeys.size() < 1) { |
| 77 | + EC2Cloud.log(LOGGER, Level.WARNING, computer.getListener(), "No static SSH keys found"); |
| 78 | + // To avoid reconnecting continuously |
| 79 | + computer.setTemporarilyOffline(true, OfflineCause.create(Messages._OfflineCause_SSHKeyCheckFailed())); |
| 80 | + return false; |
| 81 | + } |
62 | 82 |
|
63 | | - EC2Cloud.log(LOGGER, Level.WARNING, computer.getListener(), String.format("The SSH key (%s %s) presented by the instance is different from the one printed out on the instance console (%s %s). The connection to %s is closed to prevent a possible man-in-the-middle attack", |
64 | | - hostKey.getAlgorithm(), hostKey.getFingerprint(), staticHostKey.getAlgorithm(), staticHostKey.getFingerprint(), computer.getName())); |
| 83 | + if (null == existingHostKey) { |
| 84 | + for (HostKey staticHostKey : staticHostKeys) { |
| 85 | + if (hostKey.equals(staticHostKey)) { |
| 86 | + HostKeyHelper.getInstance().saveHostKey(computer, hostKey); |
| 87 | + EC2Cloud.log(LOGGER, Level.INFO, computer.getListener(), String.format("The SSH key %s %s has been successfully checked against the instance console for connections to %s", hostKey.getAlgorithm(), hostKey.getFingerprint(), computer.getName())); |
| 88 | + return true; |
| 89 | + } |
| 90 | + } |
65 | 91 | // To avoid reconnecting continuously |
66 | 92 | computer.setTemporarilyOffline(true, OfflineCause.create(Messages._OfflineCause_SSHKeyCheckFailed())); |
67 | 93 | return false; |
|
0 commit comments