-
Notifications
You must be signed in to change notification settings - Fork 690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add host-key verification strategy check-static #483
Open
cgroschupp
wants to merge
7
commits into
jenkinsci:master
Choose a base branch
from
cgroschupp:feat/ssh-check-static
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d36e99a
add host-key verification strategy check-static
cgroschupp 79fa8ce
remove hard-coded ssh public key from check-static
cgroschupp d2455f8
null check
cgroschupp 474ffad
Add help for check-static
cgroschupp 798b86d
convert EC2SlaveMonitorTest line endings
cgroschupp 06e1dda
add tests
cgroschupp cf08064
update docs
cgroschupp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/hudson/plugins/ec2/ssh/verifiers/CheckStaticStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2020-, M Ramon Leon, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
package hudson.plugins.ec2.ssh.verifiers; | ||
|
||
import hudson.model.TaskListener; | ||
import hudson.plugins.ec2.EC2Cloud; | ||
import hudson.plugins.ec2.EC2Computer; | ||
import hudson.plugins.ec2.SlaveTemplate; | ||
import hudson.slaves.OfflineCause; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.Scanner; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* This strategy checks the key presented by the host with the key in the Static Host Keys` field in the slave template. | ||
* If the key is not found, the connection is not trusted. If it's found, the key is stored in the ssh-host-key.xml file | ||
* of the node directory and checked on every further connection. | ||
* @author Christian Groschupp | ||
* @since TODO | ||
*/ | ||
public class CheckStaticStrategy extends SshHostKeyVerificationStrategy { | ||
private static final Logger LOGGER = Logger.getLogger(CheckStaticStrategy.class.getName()); | ||
|
||
private ArrayList<HostKey> getStaticHostKeys(EC2Computer computer) { | ||
ArrayList<HostKey> hostKeys; | ||
hostKeys = new ArrayList<>(); | ||
|
||
SlaveTemplate computerSlaveTemplate = computer.getSlaveTemplate(); | ||
if (computerSlaveTemplate == null) { | ||
EC2Cloud.log(LOGGER, Level.WARNING, computer.getListener(), "No compute slave template found, return empty hostKeys list"); | ||
return hostKeys; | ||
} | ||
|
||
Scanner scanner = new Scanner(computerSlaveTemplate.getStaticHostKeys()); | ||
|
||
while (scanner.hasNextLine()) { | ||
String hostKeyString = scanner.nextLine(); | ||
String[] hostKeyParts = hostKeyString.split(" "); | ||
if (hostKeyParts.length < 2 || hostKeyParts.length > 3) { | ||
EC2Cloud.log(LOGGER, Level.WARNING, computer.getListener(), "The provided static SSH key is invalid"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you add the reason it would be appreciated, like: the format is |
||
continue; | ||
} | ||
HostKey hostKey = new HostKey(hostKeyParts[0], Base64.getDecoder().decode(hostKeyParts[1])); | ||
hostKeys.add(hostKey); | ||
} | ||
scanner.close(); | ||
return hostKeys; | ||
} | ||
|
||
@Override | ||
public boolean verify(EC2Computer computer, HostKey hostKey, TaskListener listener) throws IOException { | ||
HostKey existingHostKey = HostKeyHelper.getInstance().getHostKey(computer); | ||
ArrayList<HostKey> staticHostKeys = getStaticHostKeys(computer); | ||
|
||
if (staticHostKeys.size() < 1) { | ||
EC2Cloud.log(LOGGER, Level.WARNING, computer.getListener(), "No configured static SSH key or none of the statically configured SSH keys are valid"); | ||
// To avoid reconnecting continuously | ||
computer.setTemporarilyOffline(true, OfflineCause.create(Messages._OfflineCause_SSHKeyCheckFailed())); | ||
return false; | ||
} | ||
|
||
if (null == existingHostKey) { | ||
for (HostKey staticHostKey : staticHostKeys) { | ||
if (hostKey.equals(staticHostKey)) { | ||
HostKeyHelper.getInstance().saveHostKey(computer, hostKey); | ||
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())); | ||
return true; | ||
} | ||
} | ||
// To avoid reconnecting continuously | ||
computer.setTemporarilyOffline(true, OfflineCause.create(Messages._OfflineCause_SSHKeyCheckFailed())); | ||
return false; | ||
|
||
} else if (existingHostKey.equals(hostKey)) { | ||
EC2Cloud.log(LOGGER, Level.INFO, computer.getListener(), "Connection allowed after the host key has been verified"); | ||
return true; | ||
} else { | ||
EC2Cloud.log(LOGGER, Level.WARNING, computer.getListener(), String.format("The SSH key (%s) presented by the instance has changed since first saved (%s). The connection to %s is closed to prevent a possible man-in-the-middle attack", hostKey.getFingerprint(), existingHostKey.getFingerprint(), computer.getName())); | ||
// To avoid reconnecting continuously | ||
computer.setTemporarilyOffline(true, OfflineCause.create(Messages._OfflineCause_SSHKeyCheckFailed())); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/main/resources/hudson/plugins/ec2/SlaveTemplate/help-staticHostKeys.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div> | ||
List of static ssh host keys, seperated by new line. Only used when Host Key Verification Strategy is check-static. | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.