2013-08-10 18:24:27 +02:00
|
|
|
from base import Task
|
|
|
|
from common import phases
|
|
|
|
from common.tasks.initd import InstallInitScripts
|
2013-12-29 18:11:48 +01:00
|
|
|
from common.tasks import apt
|
2013-08-10 18:24:27 +02:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
class AddSudoPackage(Task):
|
2013-12-29 20:54:36 +01:00
|
|
|
description = 'Adding `sudo\' to the image packages'
|
2013-08-10 18:24:27 +02:00
|
|
|
phase = phases.preparation
|
2013-12-29 18:11:48 +01:00
|
|
|
predecessors = [apt.AddDefaultSources]
|
2013-08-10 18:24:27 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-12-29 16:09:47 +01:00
|
|
|
info.packages.add('sudo')
|
2013-08-10 18:24:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CreateAdminUser(Task):
|
|
|
|
description = 'Creating the admin user'
|
|
|
|
phase = phases.system_modification
|
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-08-10 18:24:27 +02:00
|
|
|
from common.tools import log_check_call
|
2014-02-23 22:16:10 +01:00
|
|
|
log_check_call(['chroot', info.root,
|
|
|
|
'useradd',
|
2013-08-15 22:17:37 +00:00
|
|
|
'--create-home', '--shell', '/bin/bash',
|
2013-08-10 18:24:27 +02:00
|
|
|
info.manifest.plugins['admin_user']['username']])
|
|
|
|
|
|
|
|
|
|
|
|
class PasswordlessSudo(Task):
|
|
|
|
description = 'Allowing the admin user to use sudo without a password'
|
|
|
|
phase = phases.system_modification
|
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-08-10 18:24:27 +02:00
|
|
|
sudo_admin_path = os.path.join(info.root, 'etc/sudoers.d/99_admin')
|
|
|
|
username = info.manifest.plugins['admin_user']['username']
|
|
|
|
with open(sudo_admin_path, 'w') as sudo_admin:
|
|
|
|
sudo_admin.write('{username} ALL=(ALL) NOPASSWD:ALL'.format(username=username))
|
|
|
|
import stat
|
|
|
|
ug_read_only = (stat.S_IRUSR | stat.S_IRGRP)
|
|
|
|
os.chmod(sudo_admin_path, ug_read_only)
|
|
|
|
|
|
|
|
|
|
|
|
class AdminUserCredentials(Task):
|
|
|
|
description = 'Modifying ec2-get-credentials to copy the ssh public key to the admin user'
|
|
|
|
phase = phases.system_modification
|
2013-11-21 15:54:42 +01:00
|
|
|
predecessors = [InstallInitScripts]
|
2013-08-10 18:24:27 +02:00
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-08-10 18:24:27 +02:00
|
|
|
from common.tools import sed_i
|
|
|
|
getcreds_path = os.path.join(info.root, 'etc/init.d/ec2-get-credentials')
|
|
|
|
username = info.manifest.plugins['admin_user']['username']
|
|
|
|
sed_i(getcreds_path, 'username=\'root\'', 'username=\'{username}\''.format(username=username))
|
|
|
|
|
|
|
|
|
|
|
|
class DisableRootLogin(Task):
|
2013-12-28 13:54:15 +01:00
|
|
|
description = 'Disabling SSH login for root'
|
2013-08-10 18:24:27 +02:00
|
|
|
phase = phases.system_modification
|
|
|
|
|
2014-01-05 15:57:11 +01:00
|
|
|
@classmethod
|
|
|
|
def run(cls, info):
|
2013-12-15 19:24:41 +01:00
|
|
|
from subprocess import CalledProcessError
|
|
|
|
from common.tools import log_check_call
|
|
|
|
try:
|
2014-02-23 22:16:10 +01:00
|
|
|
log_check_call(['chroot', info.root,
|
|
|
|
'dpkg-query', '-W', 'openssh-server'])
|
2013-12-15 19:24:41 +01:00
|
|
|
from common.tools import sed_i
|
|
|
|
sshdconfig_path = os.path.join(info.root, 'etc/ssh/sshd_config')
|
|
|
|
sed_i(sshdconfig_path, 'PermitRootLogin yes', 'PermitRootLogin no')
|
|
|
|
except CalledProcessError:
|
|
|
|
import logging
|
|
|
|
logging.getLogger(__name__).warn('The OpenSSH server has not been installed, '
|
|
|
|
'not disabling SSH root login.')
|