|
|
@ -1,10 +1,9 @@
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
import os.path
|
|
|
|
import os.path
|
|
|
|
import uuid
|
|
|
|
import uuid
|
|
|
|
import paramiko
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from tornado.options import define, options
|
|
|
|
from tornado.options import define
|
|
|
|
from policy import get_host_keys, get_policy_class
|
|
|
|
from policy import get_host_keys, get_policy_class, check_policy_setting
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
define('address', default='127.0.0.1', help='listen address')
|
|
|
|
define('address', default='127.0.0.1', help='listen address')
|
|
|
@ -12,32 +11,47 @@ define('port', default=8888, help='listen port', type=int)
|
|
|
|
define('debug', default=False, help='debug mode', type=bool)
|
|
|
|
define('debug', default=False, help='debug mode', type=bool)
|
|
|
|
define('policy', default='warning',
|
|
|
|
define('policy', default='warning',
|
|
|
|
help='missing host key policy, reject|autoadd|warning')
|
|
|
|
help='missing host key policy, reject|autoadd|warning')
|
|
|
|
|
|
|
|
define('hostFile', default='', help='User-defined host keys file')
|
|
|
|
|
|
|
|
define('sysHostFile', default='', help='System-wide host keys File')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_application_settings():
|
|
|
|
base_dir = os.path.dirname(__file__)
|
|
|
|
base_dir = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
filename = os.path.join(base_dir, 'known_hosts')
|
|
|
|
|
|
|
|
host_keys = get_host_keys(filename)
|
|
|
|
|
|
|
|
system_host_keys = get_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
|
|
|
|
|
|
|
|
policy_class = get_policy_class(options.policy)
|
|
|
|
|
|
|
|
logging.info(policy_class.__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if policy_class is paramiko.client.AutoAddPolicy:
|
|
|
|
|
|
|
|
host_keys.save(filename) # for permission test
|
|
|
|
|
|
|
|
elif policy_class is paramiko.client.RejectPolicy:
|
|
|
|
|
|
|
|
if not host_keys and not system_host_keys:
|
|
|
|
|
|
|
|
raise ValueError('Empty known_hosts with reject policy?')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_app_settings(options):
|
|
|
|
settings = dict(
|
|
|
|
settings = dict(
|
|
|
|
template_path=os.path.join(base_dir, 'templates'),
|
|
|
|
template_path=os.path.join(base_dir, 'templates'),
|
|
|
|
static_path=os.path.join(base_dir, 'static'),
|
|
|
|
static_path=os.path.join(base_dir, 'static'),
|
|
|
|
cookie_secret=uuid.uuid4().hex,
|
|
|
|
cookie_secret=uuid.uuid4().hex,
|
|
|
|
xsrf_cookies=True,
|
|
|
|
xsrf_cookies=True,
|
|
|
|
host_keys=host_keys,
|
|
|
|
|
|
|
|
host_keys_filename=filename,
|
|
|
|
|
|
|
|
system_host_keys=system_host_keys,
|
|
|
|
|
|
|
|
policy=policy_class(),
|
|
|
|
|
|
|
|
debug=options.debug
|
|
|
|
debug=options.debug
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_host_keys_settings(options):
|
|
|
|
|
|
|
|
if not options.hostFile:
|
|
|
|
|
|
|
|
host_keys_filename = os.path.join(base_dir, 'known_hosts')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
host_keys_filename = options.hostFile
|
|
|
|
|
|
|
|
host_keys = get_host_keys(host_keys_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not options.sysHostFile:
|
|
|
|
|
|
|
|
filename = os.path.expanduser('~/.ssh/known_hosts')
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
filename = options.sysHostFile
|
|
|
|
|
|
|
|
system_host_keys = get_host_keys(filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
settings = dict(
|
|
|
|
|
|
|
|
host_keys=host_keys,
|
|
|
|
|
|
|
|
system_host_keys=system_host_keys,
|
|
|
|
|
|
|
|
host_keys_filename=host_keys_filename
|
|
|
|
|
|
|
|
)
|
|
|
|
return settings
|
|
|
|
return settings
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_policy_setting(options, host_keys_settings):
|
|
|
|
|
|
|
|
policy_class = get_policy_class(options.policy)
|
|
|
|
|
|
|
|
logging.info(policy_class.__name__)
|
|
|
|
|
|
|
|
check_policy_setting(policy_class, host_keys_settings)
|
|
|
|
|
|
|
|
return policy_class()
|
|
|
|