-
Notifications
You must be signed in to change notification settings - Fork 0
/
oscap-lxd
executable file
·216 lines (196 loc) · 7.9 KB
/
oscap-lxd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/bin/bash
# Copyright 2024 Denis Manente <denis.manente@automatic-server.com>
# Copyright 2015 Martin Preisler <martin@preisler.me>
#
# Based on oscap-ssh from Martin Preisler <martin@preisler.me>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
function die()
{
echo "$*" >&2
exit 1
}
which lxc > /dev/null || die "Cannot find lxc, please install the lxd-client."
which mktemp > /dev/null || die "Cannot find mktemp, please install coreutils."
function usage()
{
echo "oscap-lxd"
echo
echo "$ oscap-lxd instancename info INPUT_CONTENT"
echo
echo "$ oscap-lxd instancename xccdf eval [options] INPUT_CONTENT"
echo
echo "Only source datastreams are supported as INPUT_CONTENT!"
echo
echo "supported oscap xccdf eval options are:"
echo " --profile"
echo " --tailoring-file"
echo " --tailoring-id"
echo " --cpe (external OVAL dependencies are not supported yet!)"
# --oval-results, --sce-results and --check-engine-results are not supported
# use --results-arf instead
echo " --results"
echo " --results-arf"
echo " --report"
echo " --skip-valid"
echo " --fetch-remote-resources"
echo " --progress"
echo " --datastream-id"
echo " --xccdf-id"
echo " --benchmark-id"
echo " --remediate"
echo
echo "$ oscap-lxd instancename oval eval [options] INPUT_CONTENT"
echo
echo "supported oscap oval eval options are:"
echo " --id"
echo " --variables"
echo " --directives"
echo " --results"
echo " --report"
echo " --skip-valid"
echo " --datastream-id"
echo " --oval-id"
echo " --probe-root (has to be remote probe root!)"
echo
echo "$ oscap-lxd instancename oval collect [options] INPUT_CONTENT"
echo
echo "supported oscap oval collect options are:"
echo " --id"
echo " --syschar"
echo " --variables"
echo " --skip-valid"
}
if [ $# -lt 1 ]; then
echo "Missing instance name."
usage
die
fi
INSTANCE="$1"
if [ "$2" == "--v" ]; then
true
elif [ "$2" == "info" ]; then
true
elif [ "$2 $3" == "xccdf eval" ]; then
true
elif [ "$2 $3" == "oval eval" ]; then
true
elif [ "$2 $3" == "oval collect" ]; then
true
else
die "This script only support 'info', 'xccdf eval', 'oval eval', 'oval collect'."
fi
shift 1
REMOTE_TEMP_DIR=$(lxc exec "$INSTANCE" -- mktemp -d) || die "Failed to create instance temporary directory!"
args=("$@")
LOCAL_CONTENT_PATH=""
LOCAL_TAILORING_PATH=""
LOCAL_CPE_PATH=""
LOCAL_VARIABLES_PATH=""
LOCAL_DIRECTIVES_PATH=""
TARGET_RESULTS=""
TARGET_RESULTS_ARF=""
TARGET_REPORT=""
TARGET_SYSCHAR=""
# We have to rewrite various paths to a instance temp dir
for i in $(seq 0 `expr $# - 1`); do
let j=i+1
case "${args[i]}" in
("--tailoring-file")
LOCAL_TAILORING_PATH=${args[j]}
args[j]="$REMOTE_TEMP_DIR/tailoring.xml"
;;
("--cpe")
LOCAL_CPE_PATH=${args[j]}
args[j]="$REMOTE_TEMP_DIR/cpe.xml"
;;
("--variables")
LOCAL_VARIABLES_PATH=${args[j]}
args[j]="$REMOTE_TEMP_DIR/variables.xml"
;;
("--directives")
LOCAL_DIRECTIVES_PATH=${args[j]}
args[j]="$REMOTE_TEMP_DIR/directives.xml"
;;
("--results")
TARGET_RESULTS=${args[j]}
args[j]="$REMOTE_TEMP_DIR/results.xml"
;;
("--results-arf")
TARGET_RESULTS_ARF=${args[j]}
args[j]="$REMOTE_TEMP_DIR/results-arf.xml"
;;
("--report")
TARGET_REPORT=${args[j]}
args[j]="$REMOTE_TEMP_DIR/report.html"
;;
("--syschar")
TARGET_SYSCHAR=${args[j]}
args[j]="$REMOTE_TEMP_DIR/syschar.xml"
;;
*)
;;
esac
done
if [ "$1" != "--v" ]; then
# Last argument should be the content path
LOCAL_CONTENT_PATH="${args[`expr $# - 1`]}"
args[`expr $# - 1`]="$REMOTE_TEMP_DIR/input.xml"
fi
[ "$LOCAL_CONTENT_PATH" == "" ] || [ -f "$LOCAL_CONTENT_PATH" ] || die "Expected the last argument to be an input file, '$LOCAL_CONTENT_PATH' isn't a valid file path or the file doesn't exist!"
[ "$LOCAL_TAILORING_PATH" == "" ] || [ -f "$LOCAL_TAILORING_PATH" ] || die "Tailoring file path '$LOCAL_TAILORING_PATH' isn't a valid file path or the file doesn't exist!"
[ "$LOCAL_CPE_PATH" == "" ] || [ -f "$LOCAL_CPE_PATH" ] || die "CPE file path '$LOCAL_CPE_PATH' isn't a valid file path or the file doesn't exist!"
[ "$LOCAL_VARIABLES_PATH" == "" ] || [ -f "$LOCAL_VARIABLES_PATH" ] || die "OVAL variables file path '$LOCAL_VARIABLES_PATH' isn't a valid file path or the file doesn't exist!"
[ "$LOCAL_DIRECTIVES_PATH" == "" ] || [ -f "$LOCAL_DIRECTIVES_PATH" ] || die "OVAL directives file path '$LOCAL_DIRECTIVES_PATH' isn't a valid file path or the file doesn't exist!"
if [ "$LOCAL_CONTENT_PATH" != "" ]; then
echo "Copying input file '$LOCAL_CONTENT_PATH' to instance working directory '$REMOTE_TEMP_DIR'..."
lxc file push "$LOCAL_CONTENT_PATH" "$INSTANCE/$REMOTE_TEMP_DIR/input.xml" || die "Failed to copy input file to instance temporary directory!"
fi
if [ "$LOCAL_TAILORING_PATH" != "" ]; then
echo "Copying tailoring file '$LOCAL_TAILORING_PATH' to instance working directory '$REMOTE_TEMP_DIR'..."
lxc file push "$LOCAL_TAILORING_PATH" "$INSTANCE/$REMOTE_TEMP_DIR/tailoring.xml" || die "Failed to copy tailoring file to instance temporary directory!"
fi
if [ "$LOCAL_CPE_PATH" != "" ]; then
echo "Copying CPE file '$LOCAL_CPE_PATH' to instance working directory '$REMOTE_TEMP_DIR'..."
lxc file push "$LOCAL_CPE_PATH" "$INSTANCE/$REMOTE_TEMP_DIR/cpe.xml" || die "Failed to copy CPE file to instance temporary directory!"
fi
if [ "$LOCAL_VARIABLES_PATH" != "" ]; then
echo "Copying OVAL variables file '$LOCAL_VARIABLES_PATH' to instance working directory '$REMOTE_TEMP_DIR'..."
lxc file push "$LOCAL_VARIABLES_PATH" "$INSTANCE/$REMOTE_TEMP_DIR/variables.xml" || die "Failed to copy OVAL variables file to instance temporary directory!"
fi
if [ "$LOCAL_DIRECTIVES_PATH" != "" ]; then
echo "Copying OVAL directives file '$LOCAL_DIRECTIVES_PATH' to instance working directory '$REMOTE_TEMP_DIR'..."
lxc file push "$LOCAL_DIRECTIVES_PATH" "$INSTANCE/$REMOTE_TEMP_DIR/directives.xml" || die "Failed to copy OVAL directives file to instance temporary directory!"
fi
echo "Starting the evaluation..."
lxc exec "$INSTANCE" -- oscap "${args[@]}"
OSCAP_EXIT_CODE=$?
echo "oscap exit code: $OSCAP_EXIT_CODE"
echo "Copying back requested files..."
if [ "$TARGET_RESULTS" != "" ]; then
lxc file pull "$INSTANCE/$REMOTE_TEMP_DIR/results.xml" "$TARGET_RESULTS" || die "Failed to copy the results file back to local machine!"
fi
if [ "$TARGET_RESULTS_ARF" != "" ]; then
lxc file pull "$INSTANCE/$REMOTE_TEMP_DIR/results-arf.xml" "$TARGET_RESULTS_ARF" || die "Failed to copy the ARF file back to local machine!"
fi
if [ "$TARGET_REPORT" != "" ]; then
lxc file pull "$INSTANCE/$REMOTE_TEMP_DIR/report.html" "$TARGET_REPORT" || die "Failed to copy the HTML report back to local machine!"
fi
if [ "$TARGET_SYSCHAR" != "" ]; then
lxc file pull "$INSTANCE/$REMOTE_TEMP_DIR/syschar.xml" "$TARGET_SYSCHAR" || die "Failed to copy the OVAL syschar file back to local machine!"
fi
echo "Removing instance temporary directory..."
lxc exec "$INSTANCE" -- rm -r $REMOTE_TEMP_DIR || die "Failed to remove instance temporary directory!"
exit $OSCAP_EXIT_CODE