This repository has been archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #567 from gamechanger/js-git-known-hosts
Js git known hosts
- Loading branch information
Showing
7 changed files
with
116 additions
and
22 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import os | ||
import logging | ||
|
||
from ...subprocess import check_output | ||
|
||
def _get_known_hosts_path(): | ||
ssh_dir = os.path.expanduser('~root/.ssh') | ||
if not os.path.isdir(ssh_dir): | ||
os.makedirs(ssh_dir) | ||
return os.path.join(ssh_dir, 'known_hosts') | ||
|
||
def ensure_known_hosts(hosts): | ||
known_hosts_path = _get_known_hosts_path() | ||
modified = False | ||
with open(known_hosts_path, 'r+') as f: | ||
contents = f.read() | ||
if not contents.endswith('\n'): | ||
contents += '\n' | ||
for host in hosts: | ||
if host not in contents: | ||
logging.info('Adding {} ssh key to roots ssh known_hosts file'.format(host)) | ||
command = ['sh', '-c', 'ssh-keyscan -t rsa {}'.format(host)] | ||
result = check_output(command, demote=False) | ||
contents += result | ||
modified = True | ||
if modified: | ||
f.seek(0) | ||
f.write(contents) |
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
Empty file.
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,57 @@ | ||
import os | ||
import tempfile | ||
|
||
from mock import patch | ||
|
||
import dusty.constants | ||
from dusty.systems.known_hosts import ensure_known_hosts | ||
from ....testcases import DustyTestCase | ||
|
||
@patch('dusty.systems.known_hosts._get_known_hosts_path') | ||
@patch('dusty.systems.known_hosts.check_output') | ||
class TestKnownHostsSystem(DustyTestCase): | ||
def setUp(self): | ||
super(TestKnownHostsSystem, self).setUp() | ||
self.temp_hosts_path = tempfile.mkstemp()[1] | ||
|
||
def tearDown(self): | ||
super(TestKnownHostsSystem, self).tearDown() | ||
os.remove(self.temp_hosts_path) | ||
|
||
def test_preserves_existing_content(self, fake_check_output, fake_get_known_hosts): | ||
fake_get_known_hosts.return_value = self.temp_hosts_path | ||
fake_check_output.return_value = 'dusty.host:SOMESHA' | ||
|
||
initial_content = 'prev.known.host.1:SOMESHA\nprev.known.host.2:SOMESHA' | ||
with open(self.temp_hosts_path, 'w') as f: | ||
f.write(initial_content) | ||
expected_result_content = 'prev.known.host.1:SOMESHA\nprev.known.host.2:SOMESHA\ndusty.host:SOMESHA' | ||
|
||
ensure_known_hosts(['dusty.host']) | ||
with open(self.temp_hosts_path, 'r') as f: | ||
self.assertEqual(f.read(), expected_result_content) | ||
|
||
def test_not_modified(self, fake_check_output, fake_get_known_hosts): | ||
fake_get_known_hosts.return_value = self.temp_hosts_path | ||
fake_check_output.return_value = 'prev.known.host.1:SOMESHA' | ||
|
||
initial_content = 'prev.known.host.1:SOMESHA\nprev.known.host.2:SOMESHA' | ||
with open(self.temp_hosts_path, 'w') as f: | ||
f.write(initial_content) | ||
|
||
ensure_known_hosts(['prev.known.host.1']) | ||
with open(self.temp_hosts_path, 'r') as f: | ||
self.assertEqual(f.read(), initial_content) | ||
|
||
def test_redundant_additions(self, fake_check_output, fake_get_known_hosts): | ||
fake_get_known_hosts.return_value = self.temp_hosts_path | ||
fake_check_output.return_value = 'dusty.host:SOMESHA' | ||
|
||
initial_content = 'prev.known.host.1:SOMESHA\nprev.known.host.2:SOMESHA' | ||
with open(self.temp_hosts_path, 'w') as f: | ||
f.write(initial_content) | ||
expected_result_content = 'prev.known.host.1:SOMESHA\nprev.known.host.2:SOMESHA\ndusty.host:SOMESHA' | ||
|
||
ensure_known_hosts(['dusty.host', 'dusty.host', 'dusty.host']) | ||
with open(self.temp_hosts_path, 'r') as f: | ||
self.assertEqual(f.read(), expected_result_content) |