provisioning tool for building opinionated architecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.9 KiB

9 months ago
7 months ago
9 months ago
6 months ago
9 months ago
  1. #!/bin/bash
  2. readonly EXPANDED_CONF="$MIAOU_CONFIGDIR/miaou.expanded.yaml"
  3. TARGET=$(yq '.target' "$EXPANDED_CONF")
  4. readonly TARGET
  5. function check() {
  6. container_exists "$CONTAINER" || return 1
  7. container_running "$CONTAINER" || return 2
  8. check_reverseproxy || return 4
  9. check_banner || return 5
  10. check_certbot || return 6
  11. PREFIX="recipe:dmz:check" echo "container <$CONTAINER> approved successfully!"
  12. return 0
  13. }
  14. function check_reverseproxy() {
  15. lxc exec "$CONTAINER" -- bash <<EOF
  16. set -Eeuo pipefail
  17. dpkg -l nginx | grep -q ^ii
  18. systemctl is-active --quiet nginx
  19. nginx -tq
  20. EOF
  21. }
  22. function check_certbot() {
  23. lxc exec "$CONTAINER" -- bash <<EOF
  24. set -Eeuo pipefail
  25. dpkg -l certbot | grep -q ^ii
  26. dpkg -l python3-certbot-nginx | grep -q ^ii
  27. EOF
  28. }
  29. function check_banner() {
  30. if [[ $TARGET != "prod" ]]; then
  31. lxc exec "$CONTAINER" -- bash <<EOF
  32. set -Eeuo pipefail
  33. test -f /etc/nginx/snippets/banner_$TARGET.conf
  34. EOF
  35. fi
  36. }
  37. function install() {
  38. PREFIX="recipe:dmz:install"
  39. : $PREFIX
  40. echowarn "about to deploy new container <$CONTAINER> ..."
  41. if ! container_exists "$CONTAINER"; then
  42. echowarn "about to create new container <$CONTAINER> ..."
  43. lxc-miaou-create "$CONTAINER"
  44. echo OK
  45. fi
  46. if ! container_running "$CONTAINER"; then
  47. echowarn "about to start asleep container <$CONTAINER> ..."
  48. lxc start "$CONTAINER"
  49. echo OK
  50. fi
  51. credential_email=$(load_yaml_from_expanded credential.email)
  52. lxc exec "$CONTAINER" -- bash <<EOF
  53. set -Eeuo pipefail
  54. apt-get update && apt-get dist-upgrade -y
  55. apt-get install -y nftables nginx ssl-cert libnginx-mod-http-subs-filter certbot python3-certbot-nginx
  56. echo "registering with your default credential email <$credential_email>"
  57. certbot register --agree-tos --email $credential_email --no-eff-email || echo "already resgistered!"
  58. rm /etc/nginx/sites-{enabled,available}/default -f
  59. systemctl enable nginx
  60. nginx -tq || rm /etc/nginx/sites-enabled/hosts
  61. systemctl start nginx
  62. EOF
  63. if [[ "$TARGET" != "prod" ]]; then
  64. echo "copying Nginx banner to container <$CONTAINER> ... "
  65. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/nginx/snippets/banner_$TARGET.conf" "$CONTAINER/etc/nginx/snippets/banner_$TARGET.conf"
  66. lxc file push --uid 0 --gid 0 "$MIAOU_BASEDIR/templates/nginx/snippets/banner_exp.conf" "$CONTAINER/etc/nginx/snippets/banner_exp.conf"
  67. echo "copying files over container <$CONTAINER> ... OK"
  68. else
  69. echo "no Nginx banner on PROD!"
  70. fi
  71. echo "populate nftables entries into yaml"
  72. local wan_interface dmz_ip
  73. wan_interface=$(ip route show default | cut -d ' ' -f5)
  74. dmz_ip=$(host "$CONTAINER.lxd" | cut -d ' ' -f4)
  75. yq ".nftables.wan_interface=\"$wan_interface\"" "$EXPANDED_CONF" -i
  76. yq ".nftables.dmz_ip=\"$dmz_ip\"" "$EXPANDED_CONF" -i
  77. local nftables_reloading=false
  78. if [[ "$TARGET" != "dev" ]]; then
  79. mkdir -p "$MIAOU_CONFIGDIR/nftables.rules.d"
  80. echo "nat http/s port to dmz"
  81. tera -t "$MIAOU_BASEDIR/templates/nftables/nat.table.j2" "$EXPANDED_CONF" -o "$MIAOU_CONFIGDIR/nftables.rules.d/nat.table" &>/dev/null
  82. sudo cp "$MIAOU_CONFIGDIR/nftables.rules.d/nat.table" /etc/nftables.rules.d/nat.table
  83. nftables_reloading=true
  84. else
  85. if [[ -f /etc/nftables.rules.d/nat.table ]]; then
  86. sudo_required "remove previous nat.table"
  87. sudo rm -f /etc/nftables.rules.d/nat.table
  88. nftables_reloading=true
  89. fi
  90. fi
  91. if [[ "$nftables_reloading" == true ]]; then
  92. sudo_required "reload nftables"
  93. sudo systemctl reload nftables.service
  94. fi
  95. }
  96. # MAIN
  97. . "$MIAOU_BASEDIR/lib/init.sh"
  98. arg1_required "$@"
  99. readonly CONTAINER="$1"
  100. check || (
  101. install
  102. check
  103. )