summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormulhern <mulhern@yoctoproject.org>2013-08-26 08:01:48 -0400
committermulhern <mulhern@yoctoproject.org>2013-08-30 15:39:58 -0400
commitec1c761ad87b1dec899e9d48403ad03398a7f9ed (patch)
treeb1af11bde8dc682fdd2c5dbb2c1996f0e94065b3
parent6e23502b380d845382b914ca6b3afe96c4b80c8d (diff)
downloadmeta-security-ec1c761ad87b1dec899e9d48403ad03398a7f9ed.tar.gz
Bastille: set Yocto specific questions via config file.
The python script, set_required_questions.py reads the list of questions and answers from the config file and edits the REQUIRE_DISTRO field for those questions in the questions file so that it includes "Yocto" if it is not already present. This has the effect of causing Bastille, when loading questions for the Yocto distribution, to load only those that are answered in the existing config file. Under the assumption that the exisitng config file contains question/answer pairs that are relevant to the Yocto project, this will cause the interactive question screen to have answers that are relevant to the Yocto project. Signed-off-by: mulhern <mulhern@yoctoproject.org>
-rw-r--r--recipes-security/bastille/bastille_3.2.1.bb3
-rwxr-xr-xrecipes-security/bastille/files/set_required_questions.py137
2 files changed, 140 insertions, 0 deletions
diff --git a/recipes-security/bastille/bastille_3.2.1.bb b/recipes-security/bastille/bastille_3.2.1.bb
index 8969f6b..1c924e7 100644
--- a/recipes-security/bastille/bastille_3.2.1.bb
+++ b/recipes-security/bastille/bastille_3.2.1.bb
@@ -139,6 +139,9 @@ do_install () {
139 install -m 0644 OSMap/HP-UX.service ${D}${datadir}/Bastille/OSMap 139 install -m 0644 OSMap/HP-UX.service ${D}${datadir}/Bastille/OSMap
140 install -m 0644 OSMap/OSX.bastille ${D}${datadir}/Bastille/OSMap 140 install -m 0644 OSMap/OSX.bastille ${D}${datadir}/Bastille/OSMap
141 install -m 0644 OSMap/OSX.system ${D}${datadir}/Bastille/OSMap 141 install -m 0644 OSMap/OSX.system ${D}${datadir}/Bastille/OSMap
142
143 ${THISDIR}/files/set_required_questions.py ${WORKDIR}/config Questions
144
142 install -m 0777 ${WORKDIR}/config ${D}${sysconfdir}/Bastille/config 145 install -m 0777 ${WORKDIR}/config ${D}${sysconfdir}/Bastille/config
143 146
144 for file in `cat Modules.txt` ; do 147 for file in `cat Modules.txt` ; do
diff --git a/recipes-security/bastille/files/set_required_questions.py b/recipes-security/bastille/files/set_required_questions.py
new file mode 100755
index 0000000..ed299e5
--- /dev/null
+++ b/recipes-security/bastille/files/set_required_questions.py
@@ -0,0 +1,137 @@
1#!/usr/bin/env python
2
3#Signed-off-by: Anne Mulhern <mulhern@yoctoproject.org>
4
5import argparse, os, shutil, sys, tempfile
6from os import path
7
8
9
10def get_config(lines):
11 """
12 From a sequence of lines retrieve the question file name, question identifier
13 pairs.
14 """
15 for l in lines:
16 if not l.startswith("#"):
17 try:
18 (coord, value) = l.split("=")
19 try:
20 (fname, ident) = coord.split(".")
21 yield fname, ident
22 except ValueError as e:
23 raise ValueError("Badly formatted coordinates %s in line %s." % (coord, l.strip()))
24 except ValueError as e:
25 raise ValueError("Skipping badly formatted line %s, %s" % (l.strip(), e))
26
27
28
29def check_contains(line, name):
30 """
31 Check if the value field for REQUIRE_DISTRO contains the given name.
32 @param name line The REQUIRE_DISTRO line
33 @param name name The name to look for in the value field of the line.
34 """
35 try:
36 (label, distros) = line.split(":")
37 return name in distros.split()
38 except ValueError as e:
39 raise ValueError("Error splitting REQUIRE_DISTRO line: %s" % e)
40
41
42
43def add_requires(the_ident, distro, lines):
44
45 """
46 Yield a sequence of lines the same as lines except that where
47 the_ident matches a question identifier change the REQUIRE_DISTRO so that
48 it includes the specified distro.
49
50 @param name the_ident The question identifier to be matched.
51 @param name distro The distribution to added to the questions REQUIRE_DISTRO
52 field.
53 @param lines The sequence to be processed.
54 """
55 for l in lines:
56 yield l
57 if l.startswith("LABEL:"):
58 try:
59 (label, ident) = l.split(":")
60 if ident.strip() == the_ident:
61 break
62 except ValueError as e:
63 raise ValueError("Unexpected line %s in questions file." % l.strip())
64 for l in lines:
65 if l.startswith("REQUIRE_DISTRO"):
66 if not check_contains(l, distro):
67 yield l.rstrip() + " " + distro + "\n"
68 else:
69 yield l
70 break;
71 else:
72 yield l
73 for l in lines:
74 yield l
75
76
77
78def xform_file(qfile, distro, qlabel):
79 questions_in = open(qfile)
80 questions_out = tempfile.NamedTemporaryFile(delete=False)
81 for l in add_requires(qlabel, distro, questions_in):
82 questions_out.write(l)
83 questions_out.close()
84 questions_in.close()
85 shutil.move(questions_out.name, qfile)
86
87
88
89def handle_args(parser):
90 parser.add_argument('config_file',
91 help = "Configuration file path.")
92 parser.add_argument('questions_dir',
93 help = "Directory containing Questions files.")
94 parser.add_argument('--distro', '-d',
95 help = "The distribution, the default is Yocto.",
96 default = "Yocto")
97 return parser.parse_args()
98
99
100
101def check_args(args):
102 args.config_file = os.path.abspath(args.config_file)
103 args.questions_dir = os.path.abspath(args.questions_dir)
104
105 if not os.path.isdir(args.questions_dir):
106 raise ValueError("Specified Questions directory %s does not exist or is not a directory." % args.questions_dir)
107
108 if not os.path.isfile(args.config_file):
109 raise ValueError("Specified configuration file %s not found." % args.config_file)
110
111
112
113def main():
114 opts = handle_args(argparse.ArgumentParser(description="A simple script that sets required questions based on the question/answer pairs in a configuration file."))
115
116 try:
117 check_args(opts)
118 except ValueError as e:
119 sys.exit("Fatal error: %s" % e)
120
121
122 try:
123 config_in = open(opts.config_file)
124 for qfile, qlabel in get_config(config_in):
125 questions_file = os.path.join(opts.questions_dir, qfile + ".txt")
126 xform_file(questions_file, opts.distro, qlabel)
127 config_in.close()
128
129 except IOError as e:
130 sys.exit("Fatal error reading config file: %s" % e)
131 except ValueError as e:
132 sys.exit("Fatal error: %s" % e)
133
134
135
136if __name__ == "__main__":
137 main()