| 1 | + | #!/bin/bash |
| 2 | + | |
| 3 | + | ################################################################################ |
| 4 | + | # Help # |
| 5 | + | ################################################################################ |
| 6 | + | Help() { |
| 7 | + | # Display Help |
| 8 | + | echo "A versatile script to create a docker image for testing. Call this script with no arguments to simply create a local image that you can use to test your changes. For more complex use see the below help section" |
| 9 | + | echo |
| 10 | + | echo "Syntax: docker-create.sh [-h (help)|-t (test)|-p (publish)|-e (herokud)|-f (herokup)|-g (fly)|-n (notag) [tag={tag}|message={message}|buildarg={buildarg}|springProfile={springProfile}]" |
| 11 | + | echo "options: (All optional)" |
| 12 | + | echo "tag= Write a custom tag that will be added to the container when it is build locally." |
| 13 | + | echo "message= Write a message used for the actual tag-message in git" |
| 14 | + | echo "buildarg= Write a build argument here that will be used as the answer to challenge 4." |
| 15 | + | echo "springProfile= Specify a certain build. Options: without-vault, local-vault, kubernetes-vault" |
| 16 | + | echo |
| 17 | + | } |
| 18 | + | |
| 19 | + | ################################################################################ |
| 20 | + | # Heroku helpers # |
| 21 | + | ################################################################################ |
| 22 | + | |
| 23 | + | break_on_tag(){ |
| 24 | + | if test -n "${tag+x}"; then |
| 25 | + | echo "tag is set" |
| 26 | + | else |
| 27 | + | echo "tag ${tag} was not set properly, aborting" |
| 28 | + | exit |
| 29 | + | fi |
| 30 | + | } |
| 31 | + | heroku_check_container() { |
| 32 | + | break_on_tag |
| 33 | + | echo "validating dockerfile to contain tag "${tag}" (should be part of '$(head -n 1 ../../Dockerfile.web)')" |
| 34 | + | if [[ "$(head -n 1 ../../Dockerfile.web)" != *"${tag}"* ]]; then |
| 35 | + | echo "tag ${tag} in dockerfile FROM was not set properly, aborting" |
| 36 | + | exit |
| 37 | + | fi |
| 38 | + | echo "Check if all required binaries are installed" |
| 39 | + | source ../../scripts/check-available-commands.sh |
| 40 | + | checkCommandsAvailable heroku |
| 41 | + | } |
| 42 | + | |
| 43 | + | Heroku_publish_demo() { |
| 44 | + | echo "preparing heroku deployment to demo" |
| 45 | + | heroku_check_container |
| 46 | + | heroku container:login |
| 47 | + | echo "heroku deployment to demo" |
| 48 | + | cd ../.. |
| 49 | + | heroku container:push --recursive --arg argBasedVersion=${tag}heroku --app arcane-scrubland-42646 |
| 50 | + | heroku container:release web --app arcane-scrubland-42646 |
| 51 | + | heroku container:push --recursive --arg argBasedVersion=${tag}heroku,CTF_ENABLED=true,HINTS_ENABLED=false --app wrongsecrets-ctf |
| 52 | + | heroku container:release web --app wrongsecrets-ctf |
| 53 | + | exit |
| 54 | + | } |
| 55 | + | |
| 56 | + | Heroku_publish_prod(){ |
| 57 | + | echo "preparing heroku deployment to prod" |
| 58 | + | heroku_check_container |
| 59 | + | heroku container:login |
| 60 | + | echo "heroku deployment to prod" |
| 61 | + | cd ../.. |
| 62 | + | heroku container:push --recursive --arg argBasedVersion=${tag}heroku,CANARY_URLS=http://canarytokens.com/feedback/images/traffic/tgy3epux7jm59n0ejb4xv4zg3/submit.aspx,http://canarytokens.com/traffic/cjldn0fsgkz97ufsr92qelimv/post.jsp --app=wrongsecrets |
| 63 | + | heroku container:release web --app=wrongsecrets |
| 64 | + | exit |
| 65 | + | } |
| 66 | + | |
| 67 | + | Fly_publish(){ |
| 68 | + | echo "Publishing to Fly.io (wrongsecrets.fly.dev)" |
| 69 | + | echo "Check if all required binaries are installed" |
| 70 | + | source ../../scripts/check-available-commands.sh |
| 71 | + | checkCommandsAvailable fly |
| 72 | + | break_on_tag |
| 73 | + | echo "validating fly.toml to contain tag "${tag}" (should be part of '$(cat ../../fly.toml | grep argBasedVersion)')" |
| 74 | + | if [[ "$(cat ../../fly.toml | grep argBasedVersion)" != *"${tag}"* ]]; then |
| 75 | + | echo "tag ${tag} in fly.toml not properly set, aborting" |
| 76 | + | exit |
| 77 | + | fi |
| 78 | + | cd ../.. && fly deploy |
| 79 | + | exit |
| 80 | + | } |
| 81 | + | |
| 82 | + | ################################################################################ |
| 83 | + | ################################################################################ |
| 84 | + | # Main program # |
| 85 | + | ################################################################################ |
| 86 | + | ################################################################################ |
| 87 | + | |
| 88 | + | # Set options |
| 89 | + | ############################# |
| 90 | + | # Set option to local if no option provided |
| 91 | + | script_mode="local" |
| 92 | + | # Parse provided options |
| 93 | + | while getopts ":htpefgn*" option; do |
| 94 | + | case $option in |
| 95 | + | h) # display Help |
| 96 | + | Help |
| 97 | + | exit |
| 98 | + | ;; |
| 99 | + | t) # set script to test mode |
| 100 | + | script_mode="test" |
| 101 | + | ;; |
| 102 | + | p) # set script to publish mode |
| 103 | + | script_mode="publish" |
| 104 | + | ;; |
| 105 | + | e) # Helper |
| 106 | + | script_mode="heroku_d" |
| 107 | + | ;; |
| 108 | + | f) # Helper |
| 109 | + | script_mode="heroku_p" |
| 110 | + | ;; |
| 111 | + | g) #Helper |
| 112 | + | script_mode="fly_p" |
| 113 | + | ;; |
| 114 | + | n) #notags |
| 115 | + | disable_tagging_in_git="true" |
| 116 | + | ;; |
| 117 | + | \?|\*) # Invalid option |
| 118 | + | echo "Error: Invalid option" |
| 119 | + | echo |
| 120 | + | Help |
| 121 | + | exit |
| 122 | + | ;; |
| 123 | + | esac |
| 124 | + | done |
| 125 | + | |
| 126 | + | # Check all arguments added to the command |
| 127 | + | ################################################ |
| 128 | + | for ARGUMENT in "$@"; |
| 129 | + | do |
| 130 | + | if [[ $ARGUMENT != "-h" && $ARGUMENT != "-t" && $ARGUMENT != "-p" && $ARGUMENT != "-e" && $ARGUMENT != "-f" && $ARGUMENT != "-g" ]] |
| 131 | + | then |
| 132 | + | KEY=$(echo "$ARGUMENT" | cut -f1 -d=) |
| 133 | + | KEY_LENGTH=${#KEY} |
| 134 | + | VALUE="${ARGUMENT:$KEY_LENGTH+1}" |
| 135 | + | export "$KEY"="$VALUE" |
| 136 | + | fi |
| 137 | + | done |
| 138 | + | |
| 139 | + | if test -n "${tag+x}"; then |
| 140 | + | echo "tag is set" |
| 141 | + | else |
| 142 | + | SCRIPT_PATH=$(dirname $(dirname $(dirname $(readlink -f "$0")))) |
| 143 | + | tag="local-test" |
| 144 | + | echo "Setting default tag: ${tag}" |
| 145 | + | fi |
| 146 | + | |
| 147 | + | if test -n "${message+x}"; then |
| 148 | + | echo "message is set" |
| 149 | + | else |
| 150 | + | SCRIPT_PATH=$(dirname $(dirname $(dirname $(readlink -f "$0")))) |
| 151 | + | message="local testcontainer build" |
| 152 | + | echo "Setting default message: ${message}" |
| 153 | + | fi |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | if test -n "${buildarg+x}"; then |
| 158 | + | echo "buildarg is set" |
| 159 | + | else |
| 160 | + | buildarg="argBasedPassword='this is on your command line'" |
| 161 | + | echo "Setting buildarg to ${buildarg}" |
| 162 | + | fi |
| 163 | + | |
| 164 | + | if test -n "${springProfile+x}"; then |
| 165 | + | if [[ $springProfile == 'local-vault' ]] || [[ $springProfile == 'without-vault' ]] || [[ $springProfile == 'kubernetes-vault' ]]; then |
| 166 | + | echo "Setting springProfile to $springProfile" |
| 167 | + | else |
| 168 | + | echo "Please specify a springProfile of without-vault, local-vault or kubernetes-vault as a springProfile" |
| 169 | + | exit 1 |
| 170 | + | fi |
| 171 | + | else |
| 172 | + | springProfile="All" |
| 173 | + | fi |
| 174 | + | |
| 175 | + | echo "Spring profile: $springProfile" |
| 176 | + | echo "Version tag: $tag" |
| 177 | + | echo "buildarg supplied: $buildarg" |
| 178 | + | |
| 179 | + | if test -n "${disable_tagging_in_git+x}"; then |
| 180 | + | echo "tagging is disabled" |
| 181 | + | else |
| 182 | + | disable_tagging_in_git="false" |
| 183 | + | fi |
| 184 | + | |
| 185 | + | if [[ $script_mode == "heroku_d" ]] ; then |
| 186 | + | Heroku_publish_demo |
| 187 | + | elif [[ $script_mode == "heroku_p" ]]; then |
| 188 | + | Heroku_publish_prod |
| 189 | + | elif [[ $script_mode == "fly_p" ]]; then |
| 190 | + | Fly_publish |
| 191 | + | fi |
| 192 | + | |
| 193 | + | |
| 194 | + | local_extra_info() { |
| 195 | + | if [[ $script_mode == "local" ]] ; then |
| 196 | + | echo "" |
| 197 | + | echo "⚠️⚠️ This script is running in local mode, with no arguments this script will build your current code and package into a docker container for easy local testing" |
| 198 | + | echo "If the container gets built correctly you can run the container with the command: docker run -p 8080:8080 jeroenwillemsen/wrongsecrets:local-test, if there are errors the script should tell you what to do ⚠️⚠️" |
| 199 | + | echo "" |
| 200 | + | fi |
| 201 | + | } |
| 202 | + | |
| 203 | + | check_required_install() { |
| 204 | + | echo "Check if all required binaries are installed" |
| 205 | + | source ../../scripts/check-available-commands.sh |
| 206 | + | checkCommandsAvailable java docker mvn git curl |
| 207 | + | echo "Checking if gsed or sed is installed" |
| 208 | + | if [ -x "$(command -v "gsed")" ] ; then |
| 209 | + | echo "gsed is installed" |
| 210 | + | findAndReplace="gsed" |
| 211 | + | elif [ -x "$(command -v "sed")" ] ; then |
| 212 | + | echo "sed is installed" |
| 213 | + | findAndReplace="sed" |
| 214 | + | else |
| 215 | + | echo "Error: sed or gsed is not installed, please install one of these" |
| 216 | + | exit 1 |
| 217 | + | fi |
| 218 | + | } |
| 219 | + | |
| 220 | + | check_os() { |
| 221 | + | echo "Checking for compatible operating system" |
| 222 | + | unameOut="$(uname -s)" |
| 223 | + | case "${unameOut}" in |
| 224 | + | Darwin*) |
| 225 | + | echo "OSX detected 🍎" |
| 226 | + | ;; |
| 227 | + | Linux*) |
| 228 | + | echo "Linux detected 🐧" |
| 229 | + | ;; |
| 230 | + | MINGW64*|CYGWIN) |
| 231 | + | echo "Windows detected 🗔" |
| 232 | + | ;; |
| 233 | + | *) |
| 234 | + | echo "🛑🛑 Unknown operating system, this script has only been tests on Windows, Mac OS and Ubuntu. Please be aware there may be some issues 🛑🛑" |
| 235 | + | ;; |
| 236 | + | esac |
| 237 | + | } |
| 238 | + | |
| 239 | + | check_correct_launch_location() { |
| 240 | + | if [[ "$(pwd)" != *"scripts"* ]]; then |
| 241 | + | echo "🛑🛑 Please run the script from the scripts folder as it causes issues with the steps that cannot be expected 🛑🛑" |
| 242 | + | echo "🛑🛑 You are currently running it from $(pwd) 🛑🛑" |
| 243 | + | exit 1 |
| 244 | + | fi |
| 245 | + | } |
| 246 | + | |
| 247 | + | generate_test_data() { |
| 248 | + | echo "Generating challenge 12-data" |
| 249 | + | openssl rand -base64 32 | tr -d '\n' > yourkey.txt |
| 250 | + | echo "Generating challenge 16-data" |
| 251 | + | SECENDKEYPART1=$(openssl rand -base64 5 | tr -d '\n') |
| 252 | + | SECENDKEYPART2=$(openssl rand -base64 3 | tr -d '\n') |
| 253 | + | SECENDKEYPART3=$(openssl rand -base64 2 | tr -d '\n') |
| 254 | + | SECENDKEYPART4=$(openssl rand -base64 3 | tr -d '\n') |
| 255 | + | echo -n "${SECENDKEYPART1}9${SECENDKEYPART2}6${SECENDKEYPART3}2${SECENDKEYPART4}7" > secondkey.txt |
| 256 | + | printf "function secret() { \n var password = \"$SECENDKEYPART1\" + 9 + \"$SECENDKEYPART2\" + 6 + \"$SECENDKEYPART3\" + 2 + \"$SECENDKEYPART4\" + 7;\n return password;\n }\n" > ../../js/index.js |
| 257 | + | echo "Generating challenge 17" |
| 258 | + | rm thirdkey.txt |
| 259 | + | openssl rand -base64 32 | tr -d '\n' > thirdkey.txt |
| 260 | + | answer=$(<thirdkey.txt) |
| 261 | + | answerRegexSafe="$(printf '%s' "$answer" | $findAndReplace -e 's/[]\/$*.^|[]/\\&/g' | $findAndReplace ':a;N;$!ba;s,\n,\\n,g')" |
| 262 | + | $findAndReplace -i "s/Placeholder Password, find the real one in the history of the container/$answerRegexSafe/g" ../../src/main/resources/.bash_history |
| 263 | + | } |
| 264 | + | |
| 265 | + | build_update_pom() { |
| 266 | + | echo "Building and updating pom.xml file so we can use it in our docker" |
| 267 | + | cd ../.. && mvn clean && mvn --batch-mode release:update-versions -DdevelopmentVersion=${tag}-SNAPSHOT && mvn install -DskipTests |
| 268 | + | cd .github/scripts |
| 269 | + | docker buildx create --name mybuilder |
| 270 | + | docker buildx use mybuilder |
| 271 | + | } |
| 272 | + | |
| 273 | + | create_containers() { |
| 274 | + | echo "Creating containers" |
| 275 | + | if [[ "$script_mode" == "publish" ]]; then |
| 276 | + | docker buildx build --platform linux/amd64,linux/arm64 -t jeroenwillemsen/addo-example:$tag-no-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=without-vault" --push ./../../. |
| 277 | + | docker buildx build --platform linux/amd64,linux/arm64 -t jeroenwillemsen/addo-example:$tag-local-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=local-vault" --push ./../../. |
| 278 | + | docker buildx build --platform linux/amd64,linux/arm64 -t jeroenwillemsen/addo-example:$tag-k8s-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=kubernetes-vault" --push ./../../. |
| 279 | + | docker buildx build --platform linux/amd64,linux/arm64 -t jeroenwillemsen/wrongsecrets:$tag-no-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=without-vault" --push ./../../. |
| 280 | + | docker buildx build --platform linux/amd64,linux/arm64 -t jeroenwillemsen/wrongsecrets:$tag-local-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=local-vault" --push ./../../. |
| 281 | + | docker buildx build --platform linux/amd64,linux/arm64 -t jeroenwillemsen/wrongsecrets:$tag-k8s-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=kubernetes-vault" --push ./../../. |
| 282 | + | cd ../.. |
| 283 | + | docker buildx build --platform linux/amd64,linux/arm64 -t jeroenwillemsen/wrongsecrets-desktop:$tag -f Dockerfile.webdesktop --push . |
| 284 | + | elif [[ "$script_mode" == "test" ]]; then |
| 285 | + | docker buildx build -t jeroenwillemsen/wrongsecrets:$tag --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=without-vault" --load ./../../. |
| 286 | + | else |
| 287 | + | if [[ "$springProfile" != "All" ]]; then |
| 288 | + | docker buildx build -t jeroenwillemsen/wrongsecrets:$tag-$springProfile --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=$springProfile" --load ./../../. |
| 289 | + | else |
| 290 | + | docker buildx build -t jeroenwillemsen/wrongsecrets:$tag-no-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=without-vault" --load ./../../. |
| 291 | + | docker buildx build -t jeroenwillemsen/wrongsecrets:$tag-local-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=local-vault" --load ./../../. |
| 292 | + | docker buildx build -t jeroenwillemsen/wrongsecrets:$tag-k8s-vault --build-arg "$buildarg" --build-arg "PORT=8081" --build-arg "argBasedVersion=$tag" --build-arg "spring_profile=kubernetes-vault" --load ./../../. |
| 293 | + | fi |
| 294 | + | fi |
| 295 | + | } |
| 296 | + | |
| 297 | + | restore_temp_change() { |
| 298 | + | echo "Restoring temporal change" |
| 299 | + | git restore ../../js/index.js |
| 300 | + | git restore ../../pom.xml |
| 301 | + | git restore ../../src/main/resources/.bash_history |
| 302 | + | } |
| 303 | + | |
| 304 | + | commit_and_tag() { |
| 305 | + | if [[ "$script_mode" == "publish" ]]; then |
| 306 | + | echo "committing changes and new pom file with version ${tag}" |
| 307 | + | git commit -am "Update POM file with new version: ${tag}" |
| 308 | + | git push |
| 309 | + | if [[ "$disable_tagging_in_git" == "true" ]]; then |
| 310 | + | echo "Skip git tagging" |
| 311 | + | else |
| 312 | + | echo "tagging version with tag '${tag}' and message '${message}'" |
| 313 | + | git tag -a $tag -m "${message}" |
| 314 | + | git push --tags |
| 315 | + | fi |
| 316 | + | else |
| 317 | + | return |
| 318 | + | fi |
| 319 | + | } |
| 320 | + | |
| 321 | + | echo_next_steps() { |
| 322 | + | if [[ "$script_mode" == "publish" ]]; then |
| 323 | + | echo "Don't forget to update experiment-bed" |
| 324 | + | echo "git checkout experiment-bed && git merge master --no-edit" |
| 325 | + | echo "git push" |
| 326 | + | |
| 327 | + | #staging (https://arcane-scrubland-42646.herokuapp.com/) |
| 328 | + | echo "Completed docker upload for X86, now taking care of heroku, do yourself: update Dockerfile.web, then run 'heroku container:login'" |
| 329 | + | echo "then for the test container: 'heroku container:push --recursive --arg argBasedVersion=${tag}heroku --app arcane-scrubland-42646' and 'heroku container:release web --app arcane-scrubland-42646'" |
| 330 | + | echo "then for the prd container:'heroku container:push --recursive --arg argBasedVersion=${tag}heroku --arg CANARY_URLS=http://canarytokens.com/feedback/images/traffic/tgy3epux7jm59n0ejb4xv4zg3/submit.aspx,http://canarytokens.com/traffic/cjldn0fsgkz97ufsr92qelimv/post.jsp --app=wrongsecrets' and release 'heroku container:release web --app=wrongsecrets'" |
| 331 | + | #want to release? do heroku container:release web --app=wrongsecrets |
| 332 | + | fi |
| 333 | + | } |
| 334 | + | |
| 335 | + | test() { |
| 336 | + | source ../../scripts/assert.sh |
| 337 | + | if [[ "$script_mode" == "test" ]]; then |
| 338 | + | echo "Running the tests" |
| 339 | + | echo "Starting the docker container" |
| 340 | + | docker run -d -p 8080:8080 jeroenwillemsen/wrongsecrets:local-test |
| 341 | + | until $(curl --output /dev/null --silent --head --fail http://localhost:8080); do |
| 342 | + | printf '.' |
| 343 | + | sleep 5 |
| 344 | + | done |
| 345 | + | response=$(curl localhost:8080) |
| 346 | + | assert_contain "$response" "Wondering what a secret is?" |
| 347 | + | if [ "$?" == 0 ]; then |
| 348 | + | log_success "The container test completed successfully" |
| 349 | + | else |
| 350 | + | log_failure "The container test has failed, this means that when we built your changes and ran a basic sanity test on the homepage it failed. Please build the container locally and double check the container is running correctly." |
| 351 | + | fi |
| 352 | + | echo "Testing complete" |
| 353 | + | else |
| 354 | + | return |
| 355 | + | fi |
| 356 | + | } |
| 357 | + | |
| 358 | + | local_extra_info |
| 359 | + | check_correct_launch_location |
| 360 | + | check_os |
| 361 | + | check_required_install |
| 362 | + | generate_test_data |
| 363 | + | build_update_pom |
| 364 | + | create_containers |
| 365 | + | restore_temp_change |
| 366 | + | commit_and_tag |
| 367 | + | echo_next_steps |
| 368 | + | test |
| 369 | + | |