| 1 |
#!/bin/sh |
| 2 |
#? |
| 3 |
#? NAME |
| 4 |
#? $0 - wrapper script to handle O-Saft in a Docker image |
| 5 |
#? |
| 6 |
#? SYNOPSIS |
| 7 |
#? $0 |
| 8 |
#? $0 build |
| 9 |
#? $0 usage |
| 10 |
#? $0 shell |
| 11 |
#? $0 root |
| 12 |
#? $0 status |
| 13 |
#? $0 inspect |
| 14 |
#? $0 rmi |
| 15 |
#? $0 apk package |
| 16 |
#? $0 apt package |
| 17 |
#? $0 gui |
| 18 |
#? $0 sshx |
| 19 |
#? $0 call /path/to/program [arguments] |
| 20 |
#? $0 cp file-to-be-copied |
| 21 |
#? $0 [options and commands of o-saft.pl] |
| 22 |
#? |
| 23 |
#? OPTIONS |
| 24 |
#? -h got it |
| 25 |
# -help got it |
| 26 |
#? -n do not execute, just show what would be done |
| 27 |
#? -v be a bit verbose |
| 28 |
#? -id=ID use Docker image ID (registry:tag); default: owasp/o-saft |
| 29 |
#? -tag=23 use Docker image ID with tag; default: (empty) |
| 30 |
#? -OSAFT_VERSION=21.11.12 same as OSAFT_VERSION environment variable |
| 31 |
#? -- pass all remaining parameters to o-saft.pl in Docker image |
| 32 |
#? |
| 33 |
#? QUICKSTART |
| 34 |
#? The main purpose of this wrapper is to provide a very simple call of |
| 35 |
#? o-saft.pl in a running O-Saft Docker image. It is the default mode. |
| 36 |
#? Example: |
| 37 |
#? $0 +info your.tld |
| 38 |
#? |
| 39 |
#? DESCRIPTION |
| 40 |
#? This is a multipurpose wrapper to handle O-Saft in a Docker image. |
| 41 |
#? It can operate in following modes: |
| 42 |
#? build - build the Docker image for O-Saft from Dockerfile |
| 43 |
#? usage - print the initial usage message for O-Saft in Docker |
| 44 |
#? status - show various information about O-Saft Docker image |
| 45 |
#? rmi - remove O-Saft Docker image(s) |
| 46 |
#? apk - add and commit packages in O-Saft Docker image |
| 47 |
#? shell - run a shell (/bin/sh) in Docker image |
| 48 |
#? root - run a shell (/bin/sh) as user root in Docker image |
| 49 |
#? call - run specified program with arguments in Docker image |
| 50 |
#? cp - copy file to /O-Saft directory in the Docker image |
| 51 |
# hacker - show more information |
| 52 |
#? gui - start o-saft.tcl in the Docker image (display to host) |
| 53 |
#? sshx - start o-saft.tcl in the Docker image (tunnel X via ssh) |
| 54 |
#? * - anything else (default): run o-saft.pl in Docker image |
| 55 |
#? |
| 56 |
#? This wrapper expects, that the O-Saft Docker image has the name: |
| 57 |
#? owasp/o-saft |
| 58 |
#? If there are more Docker images with the same (registry) name, a tag |
| 59 |
#? can be used to identify the image, see -tag=TAG option, i.e. |
| 60 |
#? $0 -tag=17.07.17 +info your.tld |
| 61 |
#? will use the name: owasp/o-saft:17.07.17 . The -id=ID option can |
| 62 |
#? be used to redefine the (registry:tag) name to be used completely. |
| 63 |
#? For example: |
| 64 |
#? owasp/o-saft:4711 |
| 65 |
#? |
| 66 |
#? If any other mode than the default is used, please see WARNING and |
| 67 |
#? SECURITY below. |
| 68 |
#? |
| 69 |
#? TERMINOLOGY |
| 70 |
#? |
| 71 |
#? Docker vs. docker |
| 72 |
#? The term "Docker" is used when the Docker system in general is meant. |
| 73 |
#? The term "docker" is used when the Docker client program is meant. |
| 74 |
#? |
| 75 |
#? mode vs. command |
| 76 |
#? The term "mode" is used for our script, which is similar to the term |
| 77 |
#? "command" of Docker. This is to distinguish the commands herein from |
| 78 |
#? those of Docker. |
| 79 |
#? |
| 80 |
#? "o-saft.pl in Docker image" |
| 81 |
#? Means that the program defined by O-Saft Docker's ENTRYPOINT will be |
| 82 |
#? executed. This can be o-saft.pl or o-saft . |
| 83 |
#? |
| 84 |
#? MODES |
| 85 |
#? Modes are simply an alias to call docker with the proper commands |
| 86 |
#? and options for the O-Saft Docker image. Most modes use the command |
| 87 |
#? "docker run --rm ...". |
| 88 |
#? This wrapper ensures that the proper O-Saft image within Docker will |
| 89 |
#? be used by passing the correct "repository:tag" image id (default: |
| 90 |
#? owasp/o-saft). This script will exit, if the image owasp/o-saft does |
| 91 |
#? exist (to avoid automatic pull from Docker's repository). |
| 92 |
#? |
| 93 |
#? The modes are: |
| 94 |
#? |
| 95 |
#? default |
| 96 |
#? In default mode, all parameters given to this wrapper are passed to |
| 97 |
#? o-saft.pl in the Docker image. This means that for all the existing |
| 98 |
#? calls, the name of the script (usually o-saft.pl) needs simply to be |
| 99 |
#? replaced by $0 . Example: |
| 100 |
#? o-saft.pl +info your.tld |
| 101 |
#? becomes |
| 102 |
#? $0 +info your.tld |
| 103 |
#? Please see LIMITATIONS below also. |
| 104 |
#? |
| 105 |
#? gui |
| 106 |
#? Start o-saft.tcl in the Docker image with display to the host where |
| 107 |
#? started. |
| 108 |
#? |
| 109 |
#? sshx |
| 110 |
#? Start o-saft.tcl in the Docker image using ssh to tunnel X to the |
| 111 |
#? hosts display. |
| 112 |
#? |
| 113 |
#? build |
| 114 |
#? This mode builds the Docker image for O-Saft from the Dockerfile. If |
| 115 |
#? ./Dockerfile is missing following will be used: |
| 116 |
#? https://github.com/OWASP/O-Saft/raw/master/Dockerfile |
| 117 |
#? See ENVIONMENT VARIABLES below for supported environment variables. |
| 118 |
#? |
| 119 |
#? apk |
| 120 |
#? apt |
| 121 |
#? Add and commit the specified packages in O-Saft Docker image. |
| 122 |
#? Note that this creates a new layer for the O-Saft Docker image. |
| 123 |
#? For more advanced mode, please refer to o-saft-docker-dev. |
| 124 |
#? Uses apk or apt to add/install the package, depending on the mode. |
| 125 |
#? |
| 126 |
#? rmi |
| 127 |
#? Remove all O-Saft images, including tagged images. |
| 128 |
#? |
| 129 |
#? usage |
| 130 |
#? Print a brief purpose and usage of this script. |
| 131 |
#? It is intended to be called from within the Docker image once, right |
| 132 |
#? after the initial build (see Dockerfile). |
| 133 |
#? |
| 134 |
#? shell |
| 135 |
#? Start a shell in O-Saft Docker image. |
| 136 |
#? This is useful because the ENTRYPOINT in the image is o-saft or |
| 137 |
#? o-saft.pl , which otherwise will always be executed. |
| 138 |
#? |
| 139 |
#? root |
| 140 |
#? Same as mode shell, but login as user root. |
| 141 |
#? |
| 142 |
#? cp |
| 143 |
#? Copy file to /O-Saft directory in the Docker image. |
| 144 |
#? |
| 145 |
#? call |
| 146 |
#? Execute specified program with given arguments. The program must be |
| 147 |
#? found using $PATH or a full path. |
| 148 |
#? This might be usefull to retrieve other information from the image, |
| 149 |
#? i.e. O-Saft itself uses it to get the environment variables set in |
| 150 |
#? the image: |
| 151 |
#? $0 call /usr/bin/env |
| 152 |
#? |
| 153 |
#? inspect |
| 154 |
#? Print information about O-Saft Docker image using inspect command. |
| 155 |
#? |
| 156 |
#? status |
| 157 |
#? Show various information about O-Saft Docker image. |
| 158 |
#? |
| 159 |
#? WARNING |
| 160 |
#? It is highly recommended to *not* use the created image as base for |
| 161 |
#? other Docker images. Also, do *not* use openssl from the image for |
| 162 |
#? other purpose than O-Saft intends. |
| 163 |
#? |
| 164 |
#? SECURITY |
| 165 |
#? When working with Docker images, at least two types of security have |
| 166 |
#? to be observed. |
| 167 |
#? |
| 168 |
#? It is rather difficult to create a secure Docker image, at least the |
| 169 |
#? pulled files (Dockerfile, image, other sources) must be trusted. |
| 170 |
#? |
| 171 |
#? The known security types are: |
| 172 |
#? |
| 173 |
#? Security of the created images |
| 174 |
#? When creating Docker images, wether using build, load or pull, you |
| 175 |
#? must trust the sources: the binary (load or pull) or the Dockerfile |
| 176 |
#? (which may be fetched at runtime too) used to build the image. |
| 177 |
#? |
| 178 |
#? Update June/2018: Crypto-Miner in Docker images detected: |
| 179 |
#? https://www.fortinet.com/blog/threat-research/yet-another-crypto-mining-botnet.html |
| 180 |
#? https://kromtech.com/blog/security-center/cryptojacking-invades-cloud-how-modern-containerization-trend-is-exploited-by-attackers |
| 181 |
#? https://sysdig.com/blog/detecting-cryptojacking/ |
| 182 |
#? That's why we prefer a transparent build of the Docker image. We re- |
| 183 |
#? comment to build the Docker image for O-Saft using the Dockerfile. |
| 184 |
#? |
| 185 |
#? Security of the tools executed from within the image (docker run) |
| 186 |
#? If the image is trustworthy (see before), the tools executed in the |
| 187 |
#? image may have security impacts. |
| 188 |
#? This script creates a Docker image for O-Saft. O-Saft is supposed to |
| 189 |
#? checks a target for SSL/TLS related issues. I. g. it doesn not harm |
| 190 |
#? the target using penetration or attacking techniques (except check |
| 191 |
#? for Heartbleed vulnerability). So o-saft.pl itself should not be any |
| 192 |
#? security issue. However, to perform the checks, O-Saft recommends to |
| 193 |
#? install a special version of OpenSSL. This OpenSSL has functionality |
| 194 |
#? enabled, which is known to be insecure (i. e. SSLv2, SSLv3). |
| 195 |
#? |
| 196 |
#? That's why this script uses a transparent method to create an image. |
| 197 |
#? |
| 198 |
#? HACKER's INFO |
| 199 |
#? Options for this script are with a single - (dash) only, because the |
| 200 |
#? same options with double -- exist for o-saft.pl. See LIMITATIONS |
| 201 |
#? below also. |
| 202 |
# |
| 203 |
# We do not use shortcut commands for docker like run, rmi, save, etc. |
| 204 |
# because they are depricated according docker documentation. |
| 205 |
# |
| 206 |
# Keep in mind that docker is picky about the sequence of commands and |
| 207 |
# options. In particular all options must preced the IMAGE argument. |
| 208 |
# |
| 209 |
# "docker commit ..." is used without the --message option because the |
| 210 |
# documentation about --message and/or -m is ambigious. Also passed |
| 211 |
# comment text is mainly the same as Docker already reports with the |
| 212 |
# "docker history IMAGE" command in the "CREATED BY" column. |
| 213 |
# |
| 214 |
# KNOWN PROBLEMS |
| 215 |
# * Error when building docker image |
| 216 |
# fetch http://dl-cdn.alpinelinux.org/alpine/edge/main/x86_64/APKINDEX.tar.gz |
| 217 |
# WARNING: Ignoring http://dl-cdn.alpinelinux.org/alpine/edge/main/x86_64/APKINDEX.tar.gz: temporary error (try again later) |
| 218 |
# fetch http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz |
| 219 |
# WARNING: Ignoring http://dl-cdn.alpinelinux.org/alpine/edge/community/x86_64/APKINDEX.tar.gz: temporary error (try again later) |
| 220 |
# ERROR: unsatisfiable constraints: |
| 221 |
# |
| 222 |
# Reason: backend mirror dl-cdn.alpinelinux.org down or not responding |
| 223 |
# Most likely an update of the host's docker package will fix it. |
| 224 |
#? |
| 225 |
#? LIMITATIONS |
| 226 |
#? The options -n -v -id=ID and -h must be specified leftmost if |
| 227 |
#? they should apply for this script. |
| 228 |
#? These options can only be passed to o-saft.pl in the Docker image, |
| 229 |
#? if they are preceeded by the -- parameter. |
| 230 |
#? The option +VERSION is not supported for this tool, it's passed to |
| 231 |
#? the Docker image. |
| 232 |
#? Unknown parameters to this script will be treated as arguments to be |
| 233 |
#? passed to o-saft.pl and will terminate internal argument scanning in |
| 234 |
#? this script. |
| 235 |
#? The script calls the program defined by the ENTRYPOINT in the Docker |
| 236 |
#? image (which is most likely o-saft). |
| 237 |
#? This script is not intended to call o-saft.cgi in the Docker image. |
| 238 |
#? However, it is possible to do so: |
| 239 |
#? $0 call o-saft.cgi --cgi [options and commands of o-saft.pl] |
| 240 |
#? |
| 241 |
#? ENVIONMENT VARIABLES |
| 242 |
#? Following environment variables can be used to pass settings to the |
| 243 |
#? script: |
| 244 |
#? |
| 245 |
#? OSAFT_VERSION - version to be passed to Dockerfile |
| 246 |
#? OSAFT_VM_SRC_OSAFT - URL to o-saft.tgz |
| 247 |
#? OSAFT_VM_SRC_SSLEAY - URL to Net-SSLeay.tgz |
| 248 |
#? OSAFT_VM_SRC_SOCKET - URL to IO-Socket.tgz |
| 249 |
#? OSAFT_VM_SRC_OPENSSL - URL to openssl.tgz |
| 250 |
#? OSAFT_VM_SHA_OSAFT - checksum of o-saft.tgz |
| 251 |
#? o_saft_docker_name - contains an image registry name |
| 252 |
#? o_saft_docker_tag - contains an image tag |
| 253 |
#? |
| 254 |
#? To get the build-in defaults use: |
| 255 |
#? $0 status |
| 256 |
#? |
| 257 |
#? EXAMPLES |
| 258 |
#? See SYNOPSIS. |
| 259 |
#? |
| 260 |
#? SEE ALSO |
| 261 |
#? docker(1), o-saft-docker-dev, o-saft.pl, o-saft.tcl |
| 262 |
#? |
| 263 |
#? VERSION |
| 264 |
#? @(#) o-saft-docker 1.44 22/04/22 22:22:10 |
| 265 |
#? |
| 266 |
#? AUTHOR |
| 267 |
#? 17-jul-17 Achim Hoffmann |
| 268 |
#? |
| 269 |
# ----------------------------------------------------------------------------- |
| 270 |
|
| 271 |
VERSION="${OSAFT_VERSION:-19.01.19}" |
| 272 |
tag=${o_saft_docker_tag}; # tag=${tag:-17.09.17} |
| 273 |
registry=${o_saft_docker_name}; registry=${registry:-owasp/o-saft} |
| 274 |
image_name="${registry}" |
| 275 |
entrypoint_pl='ENTRYPOINT ["perl", "/O-Saft/o-saft.pl"]'; # default ENTRYPOINT |
| 276 |
entrypoint_sh='ENTRYPOINT ["/O-Saft/o-saft"]'; # default ENTRYPOINT |
| 277 |
entry__cmd='CMD ["--norc", "--help=docker"]': # default CMD |
| 278 |
[ -n "$tag" ] && image_name="${registry}:${tag}" |
| 279 |
try= |
| 280 |
exe=o-saft.pl |
| 281 |
dockerfile="https://github.com/OWASP/O-Saft/raw/master/Dockerfile" |
| 282 |
|
| 283 |
check_image_name () { |
| 284 |
#? check if image with configured registry:tag exists; exit otherwise |
| 285 |
# This function should be called before most docker commands. |
| 286 |
# This tool does not allow automatic pull by default. |
| 287 |
# Most (all?) Docker commands automatically try to pull the specified |
| 288 |
# image if it does not exist. More worse, while some Docker commands |
| 289 |
# use any matching registry name (run without --entrypoint=), others |
| 290 |
# require that the registry:tag name exists (run with --entrypoint=). |
| 291 |
# Unfortunately Docker does not provide any option or configuration |
| 292 |
# to inhibit such automatic pulls. |
| 293 |
# This function tries to find existing images matching the configured |
| 294 |
# image (name). The function exits the script if the found image name |
| 295 |
# does not equal the configured image name. |
| 296 |
# Lists all images matching the required one. As "docker image ls -q" |
| 297 |
# reports the image IDs only, duplicate image IDs may be returned. |
| 298 |
# Images which are just tags, return the same image ID. Hence we can |
| 299 |
# simply filter them using "sort -u". |
| 300 |
#dbx# \docker image ls ${image_name} -q | \sort -u | \wc -w |
| 301 |
is_id=`\docker image ls ${image_name} -q | \sort -u | \wc -w` |
| 302 |
[ "${is_id}" -eq 1 ] && return; # found image, return |
| 303 |
|
| 304 |
[ "${is_id}" -ne 1 ] && \ |
| 305 |
echo "**ERROR: given image '${image_name}' does not match found image(s)" # '${is_id}'" |
| 306 |
if [ "${is_id}" -gt 1 ]; then |
| 307 |
for tag in `\docker image ls ${image_name} | \awk '/^REPOSITORY/{next}{print $2}'` ; do |
| 308 |
# varaiable tag is the same as the global one! |
| 309 |
echo "# consider using option -id=${image_name}:$tag or -tag=$tag" |
| 310 |
done |
| 311 |
fi |
| 312 |
exit 3; # no image found, exit script |
| 313 |
} |
| 314 |
|
| 315 |
docker_usage () { |
| 316 |
_me=${0##*/} |
| 317 |
# TODO: env|grep osaft_vm_build |
| 318 |
_openssl="/openssl/bin/openssl"; # TODO: still hardcoded for docker |
| 319 |
if [ -n "$osaft_vm_build" ]; then |
| 320 |
_openssl="openssl" # we're inside docker |
| 321 |
_me=${0##*/} |
| 322 |
else |
| 323 |
_openssl="$0 call $_openssl" # we're outside docker |
| 324 |
_me=$0 |
| 325 |
fi |
| 326 |
_ciphers=`$_openssl ciphers -V ALL:COMPLEMENTOFALL:aNULL | \wc -l` |
| 327 |
cat << EoDescription |
| 328 |
|
| 329 |
# openssl: |
| 330 |
$_openssl supports $_ciphers ciphers |
| 331 |
|
| 332 |
# o-saft.pl: |
| 333 |
O-Saft docker image is ready to run o-saft.pl, use: |
| 334 |
|
| 335 |
docker run --rm -it ${image_name} +info your.tld |
| 336 |
or: |
| 337 |
$_me +info your.tld |
| 338 |
or: |
| 339 |
o-saft -docker +info your.tld |
| 340 |
|
| 341 |
# o-saft.tcl: |
| 342 |
The GUI o-saft.tcl will only work inside Docker if the packages tcl |
| 343 |
tk and xvfb are installed. However o-saft.tcl may be started on the |
| 344 |
host and then use o-saft.pl from the Docker image. |
| 345 |
|
| 346 |
# run on host: |
| 347 |
|
| 348 |
o-saft.tcl --docker |
| 349 |
|
| 350 |
# run in Docker image |
| 351 |
|
| 352 |
$0 apk tcl tk xvfb |
| 353 |
$0 gui |
| 354 |
$0 gui your.tld |
| 355 |
|
| 356 |
EoDescription |
| 357 |
} |
| 358 |
# TODO: alpine:3.8 misses 'tablelist' there seems to be no package 'tklib' |
| 359 |
|
| 360 |
docker_hacker () { |
| 361 |
cat << EoHacker |
| 362 |
|
| 363 |
To use the GUI o-saft.tcl inside Docker, additionl packages need to |
| 364 |
be installed. For Docker image based on alpine, following works: |
| 365 |
|
| 366 |
$0 apk add tcl tk xvfb |
| 367 |
|
| 368 |
EoHacker |
| 369 |
} |
| 370 |
|
| 371 |
docker_inspect () { |
| 372 |
$try \docker image inspect ${image_name} |
| 373 |
} |
| 374 |
|
| 375 |
docker_status () { |
| 376 |
image_id=`\docker image ls -q ${image_name}` |
| 377 |
cat << EoStatus |
| 378 |
# using environment ... |
| 379 |
# OSAFT_VERSION = $OSAFT_VERSION |
| 380 |
# o_saft_docker_name = $o_saft_docker_name |
| 381 |
# o_saft_docker_tag = $o_saft_docker_tag |
| 382 |
|
| 383 |
# using ... |
| 384 |
# registry name = ${registry} |
| 385 |
# tag = ${tag} |
| 386 |
#? registry:tag = ${image_name} |
| 387 |
|
| 388 |
# docker image id for ${image_name} is $image_id |
| 389 |
|
| 390 |
# docker images for ${registry} ... |
| 391 |
EoStatus |
| 392 |
|
| 393 |
# list matching images |
| 394 |
$try \docker image ls ${registry}; # will be listed below again |
| 395 |
# list related (tagged) images |
| 396 |
for _id in `\docker image ls -q ${registry}` ; do |
| 397 |
$try \docker image ls | \grep "$_id" |
| 398 |
done; |
| 399 |
echo |
| 400 |
echo "# docker processes ..." |
| 401 |
$try \docker container ls -a |
| 402 |
|
| 403 |
echo "" |
| 404 |
echo "# O-Saft docker image ..." |
| 405 |
_id=`$0 call env | grep osaft_vm_build` |
| 406 |
if [ -n "$_id" ]; then |
| 407 |
echo "$_id" |
| 408 |
else |
| 409 |
echo "<<no build variable 'osaft_vm_build' found>>" |
| 410 |
fi |
| 411 |
} |
| 412 |
|
| 413 |
docker_build () { |
| 414 |
# build O-Saft Docker image from Dockerfile, mainly using defaults |
| 415 |
# from Dockerfile; cleanup the containers |
| 416 |
_arg1= |
| 417 |
_arg2= |
| 418 |
_arg3= |
| 419 |
_arg4= |
| 420 |
_arg5= |
| 421 |
_tar=./o-saft_docker.tar |
| 422 |
_cfg=./Dockerfile |
| 423 |
[ ! -e $_cfg ] && $try \wget ${dockerfile} -O $_cfg |
| 424 |
# pass environment variables (known by Dockerfile) to docker build |
| 425 |
[ -z "$OSAFT_VERSION" ] && OSAFT_VERSION="$VERSION" # use hardcoded VERSION if environment missing |
| 426 |
[ -n "$OSAFT_VERSION" ] && _arg0="--build-arg OSAFT_VERSION=$OSAFT_VERSION" |
| 427 |
[ -n "$OSAFT_VM_SRC_OSAFT" ] && _arg1="--build-arg OSAFT_VM_SRC_OSAFT=$OSAFT_VM_SRC_OSAFT" |
| 428 |
[ -n "$OSAFT_VM_SRC_SSLEAY" ] && _arg2="--build-arg OSAFT_VM_SRC_SSLEAY=$OSAFT_VM_SRC_SSLEAY" |
| 429 |
[ -n "$OSAFT_VM_SRC_SOCKET" ] && _arg3="--build-arg OSAFT_VM_SRC_SOCKET=$OSAFT_VM_SRC_SOCKET" |
| 430 |
[ -n "$OSAFT_VM_SRC_OPENSSL" ] && _arg4="--build-arg OSAFT_VM_SRC_OPENSSL=$OSAFT_VM_SRC_OPENSSL" |
| 431 |
[ -n "$OSAFT_VM_SHA_OSAFT" ] && _arg5="--build-arg OSAFT_VM_SHA_OSAFT=$OSAFT_VM_SHA_OSAFT" |
| 432 |
# TODO: do we need more of these variables? |
| 433 |
$try \docker image build -f ./Dockerfile --force-rm --rm -t ${image_name} \ |
| 434 |
$_arg0 $_arg1 $_arg2 $_arg3 $_arg4 $_arg5 . && \ |
| 435 |
$try \docker image save -o $_tar ${image_name} && \ |
| 436 |
$try \docker image rm `$try docker image ls -q ${image_name}` && \ |
| 437 |
$try \docker image load -i $_tar && \ |
| 438 |
$try \rm $_tar && \ |
| 439 |
$try \docker image tag ${image_name} o-saft && \ |
| 440 |
$try \docker image tag ${image_name} ${image_name}:$OSAFT_VERSION |
| 441 |
} |
| 442 |
|
| 443 |
docker_osaft () { |
| 444 |
# this is the command for the default mode, passing all parameters |
| 445 |
# expects a proper ENTRYPOINT like /O-Saft/o-saft.pl in the image |
| 446 |
$try \docker container run --rm -i ${image_name} $@ |
| 447 |
} |
| 448 |
|
| 449 |
docker_gui () { |
| 450 |
# start GUI in docker with display to host |
| 451 |
# --entrypoint= not really necessary if ENTRYPOINT=/O-Saft/o-saft |
| 452 |
$try \docker container run --rm -i --entrypoint=/O-Saft/o-saft.tcl \ |
| 453 |
-e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix ${image_name} $@ |
| 454 |
} |
| 455 |
|
| 456 |
docker_sshx () { |
| 457 |
_ssh=`\docker port ${image_name} 22` |
| 458 |
if [ -z "${_ssh}" ]; then |
| 459 |
echo "**ERROR: no ssh port available for '${image_name}'" |
| 460 |
else |
| 461 |
# use "ip addr" to get docker's ip, should return something like: |
| 462 |
# 6: docker0 inet 172.17.0.1/16 brd 172.17.255.255 scope global ... |
| 463 |
# TODO: returns IP of last interface if more than on exists |
| 464 |
_ip=`\ip -4 -o addr | \awk '($2~/docker/){sub("/..","",$4);print $4}'` |
| 465 |
$try \ssh user@${_id} -p ${_ssh} /O-Saft/o-saft.tcl |
| 466 |
fi |
| 467 |
} |
| 468 |
|
| 469 |
docker_call () { |
| 470 |
cmd="$1" |
| 471 |
shift |
| 472 |
$try \docker container run --rm -it --entrypoint=$cmd ${image_name} $@ |
| 473 |
} |
| 474 |
|
| 475 |
docker_shell () { |
| 476 |
$try \docker container run --rm -it --entrypoint=/bin/sh ${image_name} |
| 477 |
} |
| 478 |
|
| 479 |
docker_root () { |
| 480 |
$try \docker container run --rm -it --entrypoint=/bin/sh --user root ${image_name} |
| 481 |
} |
| 482 |
|
| 483 |
docker_cp () { |
| 484 |
_id="o-saft-cp" |
| 485 |
# create a new container with a dummy command; exit if exists or fails |
| 486 |
$try \docker container run --name ${_id} ${image_name} +VERSION || exit 4 |
| 487 |
$try \docker container cp $@ ${_id}:/O-Saft/ |
| 488 |
$try \docker container commit ${_id} ${image_name} |
| 489 |
$try \docker container rm ${_id} |
| 490 |
# |
| 491 |
# if we need to reset the entrypoint: |
| 492 |
# $try \docker container commit \ |
| 493 |
# --change='ENTRYPOINT ["perl","/O-Saft/o-saft.pl"]' \ |
| 494 |
# or: |
| 495 |
# $try \docker container commit \ |
| 496 |
# --change='ENTRYPOINT ["/O-Saft/o-saft"]' \ |
| 497 |
# |
| 498 |
} |
| 499 |
|
| 500 |
docker_apk () { |
| 501 |
_id="o-saft-apk" |
| 502 |
# after commit the entrypoint is changed, hence the original one is |
| 503 |
# set again with commit's --change option; unfortunately hardcoded |
| 504 |
$try \docker container run -t --user root --entrypoint=/sbin/apk \ |
| 505 |
--name ${_id} ${image_name} add --no-cache $@ \ |
| 506 |
|| exit 4 |
| 507 |
$try \docker container commit \ |
| 508 |
--change="${entrypoint_sh}" --change="${entry__cmd}" \ |
| 509 |
${_id} ${image_name} |
| 510 |
$try \docker container rm ${_id} |
| 511 |
return |
| 512 |
|
| 513 |
# TODO: following more cleaner method does not work (reason yet unknown) |
| 514 |
$try \docker container run -d --name ${_id} ${image_name} +VERSION || exit 4 |
| 515 |
$try \docker container exec -t --user root ${_id} /sbin/apk add --no-cache $@ |
| 516 |
$try \docker container commit ${_id} ${image_name} |
| 517 |
$try \docker container rm ${_id} |
| 518 |
} |
| 519 |
|
| 520 |
docker_apt () { |
| 521 |
_id="o-saft-apt" |
| 522 |
$try \docker container run -t --user root --entrypoint=/usr/bin/apk \ |
| 523 |
--name ${_id} ${image_name} install --no-cache $@ \ |
| 524 |
|| exit 4 |
| 525 |
$try \docker container commit \ |
| 526 |
--change="${entrypoint_sh}" --change="${entry__cmd}" \ |
| 527 |
${_id} ${image_name} |
| 528 |
$try \docker container rm ${_id} |
| 529 |
} |
| 530 |
|
| 531 |
docker_rmi () { |
| 532 |
# parameters can only be the options for Docker's rmi command |
| 533 |
#dbx# echo "# registry: ${registry} # image_name: ${image_name}" |
| 534 |
_ids=`\docker image ls -q ${registry}` # ${image_name} |
| 535 |
echo "# image IDs to be deleted: $_ids " |
| 536 |
# need to find all images, including referenced images, this is done |
| 537 |
# with "images" command, then extract all lines with matching IDs, |
| 538 |
# only these IDs are deleted |
| 539 |
# Note: "docker images" should return newest image on top, hence all |
| 540 |
# images which just represent a tag (alias) come first and will |
| 541 |
# be deleted first |
| 542 |
\docker image ls | while read _any ; do |
| 543 |
for _id in $_ids ; do |
| 544 |
_img=`echo "$_any" | \awk "/$_id/"'{print $3}'` |
| 545 |
#dbx# echo "# ID $_any : $_id : $_img" |
| 546 |
[ -z "$_img" ] && continue # other image |
| 547 |
$try \docker image rm -f $@ $_img |
| 548 |
done |
| 549 |
done |
| 550 |
$try \docker image rm ${registry} |
| 551 |
} |
| 552 |
|
| 553 |
my_help () { |
| 554 |
ich=${0##*/} |
| 555 |
\sed -ne "s/\$0/$ich/g" -e '/^#?/s/#?//p' $0 |
| 556 |
} |
| 557 |
|
| 558 |
# first check for options |
| 559 |
while [ $# -gt 0 ]; do |
| 560 |
case "$1" in |
| 561 |
'-h' | '-?' | '-help') my_help; exit 0; ;; |
| 562 |
'-n') try=echo; ;; |
| 563 |
'-v') set -x ; ;; |
| 564 |
-tag=*) tag="`expr "$1" ':' '-tag=\(.*\)'`" |
| 565 |
image_name="${registry}:$tag" |
| 566 |
;; |
| 567 |
-id=*) image_name="`expr "$1" ':' '-id=\(.*\)'`"; ;; |
| 568 |
-OSAFT_VERSION=*) OSAFT_VERSION="`expr "$1" ':' '-OSAFT_VERSION=\(.*\)'`"; ;; |
| 569 |
*) break; ;; |
| 570 |
esac |
| 571 |
shift |
| 572 |
done |
| 573 |
|
| 574 |
# don't run inside docker |
| 575 |
case "$1" in |
| 576 |
'hacker') ;; |
| 577 |
'usage') ;; |
| 578 |
*) if [ -n "$osaft_vm_build" ]; then |
| 579 |
echo "**ERROR: cannot run inside docker image; exit" |
| 580 |
exit 1 |
| 581 |
fi |
| 582 |
;; |
| 583 |
esac |
| 584 |
|
| 585 |
# some modes do not need to check existing images |
| 586 |
case "$1" in |
| 587 |
'build') ;; |
| 588 |
'usage') ;; |
| 589 |
'hacker') ;; |
| 590 |
*) check_image_name; # may exit, see description there |
| 591 |
esac |
| 592 |
|
| 593 |
# NOTE: All functions may use ${registry} and ${image_name}, which are global |
| 594 |
# variables. We do not pass these values as parameters to the functions |
| 595 |
# because we want $@ (all parameters of this script) in the functions |
| 596 |
while [ $# -gt 0 ]; do |
| 597 |
mode="$1" |
| 598 |
shift |
| 599 |
case "$mode" in |
| 600 |
'build') docker_build; exit 0; ;; |
| 601 |
'usage') docker_usage; exit 0; ;; |
| 602 |
'call') docker_call $@; exit 0; ;; |
| 603 |
'sshx') docker_sshx $@; exit 0; ;; |
| 604 |
'gui') docker_gui $@; exit 0; ;; |
| 605 |
'cp') docker_cp $@; exit 0; ;; |
| 606 |
'apk') docker_apk $@; exit 0; ;; |
| 607 |
'rmi') docker_rmi $@; exit 0; ;; |
| 608 |
'root') docker_root; exit 0; ;; |
| 609 |
'shell') docker_shell; exit 0; ;; |
| 610 |
'inspect')docker_inspect; exit 0; ;; |
| 611 |
'status') docker_status; exit 0; ;; |
| 612 |
'hacker') docker_hacker; exit 0; ;; |
| 613 |
# -registry=*) registry= # not supported, as this is for O-Saft only |
| 614 |
'--') shift; break; ;; |
| 615 |
*) break; ;; |
| 616 |
esac |
| 617 |
done |
| 618 |
set -- "$mode" $@ ; # 1st argument has no special meaning, pass to o-saft.pl |
| 619 |
|
| 620 |
if [ $# -lt 1 ]; then |
| 621 |
# did not get any argument, print own usage |
| 622 |
my_help |
| 623 |
echo |
| 624 |
echo "**WARNING: $0 called without parameters; nothing done." |
| 625 |
echo |
| 626 |
exit 2 |
| 627 |
fi |
| 628 |
|
| 629 |
docker_osaft $@ |
| 630 |
|
| 631 |
exit |