~harlowja/cloud-init/local-before-net

« back to all changes in this revision

Viewing changes to cloudinit/config/cc_seed_random.py

  • Committer: Scott Moser
  • Author(s): Dustin Kirkland
  • Date: 2014-03-03 21:44:31 UTC
  • Revision ID: smoser@ubuntu.com-20140303214431-494bqb0xlhp3xai1
seed_random: support a 'command' to seed /dev/random

This extends 'random_seed' top level entry to include a 'command'
entry, that has the opportunity to then seed the random number generator.

Example config:
 #cloud-config
 random_seed:
   command: ['dd', 'if=/dev/zero', 'of=/dev/random', 'bs=1M', 'count=10']

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# vi: ts=4 expandtab
2
2
#
3
3
#    Copyright (C) 2013 Yahoo! Inc.
 
4
#    Copyright (C) 2014 Canonical, Ltd
4
5
#
5
6
#    Author: Joshua Harlow <harlowja@yahoo-inc.com>
 
7
#    Author: Dustin Kirkland <kirkland@ubuntu.com>
 
8
#    Author: Scott Moser <scott.moser@canonical.com>
6
9
#
7
10
#    This program is free software: you can redistribute it and/or modify
8
11
#    it under the terms of the GNU General Public License version 3, as
20
23
from StringIO import StringIO
21
24
 
22
25
from cloudinit.settings import PER_INSTANCE
 
26
from cloudinit import log as logging
23
27
from cloudinit import util
24
28
 
25
29
frequency = PER_INSTANCE
 
30
LOG = logging.getLogger(__name__)
26
31
 
27
32
 
28
33
def _decode(data, encoding=None):
38
43
        raise IOError("Unknown random_seed encoding: %s" % (encoding))
39
44
 
40
45
 
 
46
def handle_random_seed_command(command, required):
 
47
    if not command and required:
 
48
        raise ValueError("no command found but required=true")
 
49
    elif not command:
 
50
        LOG.debug("no command provided")
 
51
        return
 
52
 
 
53
    cmd = command[0]
 
54
    if not util.which(cmd):
 
55
        if required:
 
56
            raise ValueError("command '%s' not found but required=true", cmd)
 
57
        else:
 
58
            LOG.debug("command '%s' not found for seed_command", cmd)
 
59
            return
 
60
    util.subp(command)
 
61
 
 
62
 
41
63
def handle(name, cfg, cloud, log, _args):
42
 
    if not cfg or "random_seed" not in cfg:
43
 
        log.debug(("Skipping module named %s, "
44
 
                   "no 'random_seed' configuration found"), name)
45
 
        return
 
64
    mycfg = cfg.get('random_seed', {})
 
65
    seed_path = mycfg.get('file', '/dev/urandom')
 
66
    seed_data = mycfg.get('data', '')
46
67
 
47
 
    my_cfg = cfg['random_seed']
48
 
    seed_path = my_cfg.get('file', '/dev/urandom')
49
68
    seed_buf = StringIO()
50
 
    seed_buf.write(_decode(my_cfg.get('data', ''),
51
 
                           encoding=my_cfg.get('encoding')))
 
69
    if seed_data:
 
70
        seed_buf.write(_decode(seed_data, encoding=mycfg.get('encoding')))
52
71
 
 
72
    # 'random_seed' is set up by Azure datasource, and comes already in
 
73
    # openstack meta_data.json
53
74
    metadata = cloud.datasource.metadata
54
75
    if metadata and 'random_seed' in metadata:
55
76
        seed_buf.write(metadata['random_seed'])
56
77
 
57
78
    seed_data = seed_buf.getvalue()
58
79
    if len(seed_data):
59
 
        log.debug("%s: adding %s bytes of random seed entrophy to %s", name,
 
80
        log.debug("%s: adding %s bytes of random seed entropy to %s", name,
60
81
                  len(seed_data), seed_path)
61
82
        util.append_file(seed_path, seed_data)
 
83
 
 
84
    command = mycfg.get('command', ['pollinate', '-q'])
 
85
    req = mycfg.get('command_required', False)
 
86
    try:
 
87
        handle_random_seed_command(command=command, required=req)
 
88
    except ValueError as e:
 
89
        log.warn("handling random command [%s] failed: %s", command, e)
 
90
        raise e