From eb265a4edb6020d3439eb6f3c7a5d041141d4428 Mon Sep 17 00:00:00 2001 From: Nazar Holubovskyy Date: Fri, 4 Sep 2020 20:19:20 +0100 Subject: [PATCH] Add virtual machine IP to the Yii2 "allowedIPs" (#229) --- Vagrantfile | 2 +- vagrant/provision/once-as-root.sh | 4 +++ vagrant/provision/provision.awk | 50 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 vagrant/provision/provision.awk diff --git a/Vagrantfile b/Vagrantfile index 44fd76c..99c2a78 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -72,7 +72,7 @@ Vagrant.configure(2) do |config| # config.vbguest.auto_update = false # provisioners - config.vm.provision 'shell', path: './vagrant/provision/once-as-root.sh', args: [options['timezone']] + config.vm.provision 'shell', path: './vagrant/provision/once-as-root.sh', args: [options['timezone'], options['ip']] config.vm.provision 'shell', path: './vagrant/provision/once-as-vagrant.sh', args: [options['github_token']], privileged: false config.vm.provision 'shell', path: './vagrant/provision/always-as-root.sh', run: 'always' diff --git a/vagrant/provision/once-as-root.sh b/vagrant/provision/once-as-root.sh index ad59603..689a7af 100644 --- a/vagrant/provision/once-as-root.sh +++ b/vagrant/provision/once-as-root.sh @@ -3,6 +3,7 @@ #== Import script args == timezone=$(echo "$1") +readonly IP=$2 #== Bash helpers == @@ -21,6 +22,9 @@ export DEBIAN_FRONTEND=noninteractive info "Configure timezone" timedatectl set-timezone ${timezone} --no-ask-password +info "Add the VM IP to the list of allowed IPs" +awk -v ip=$IP -f /app/vagrant/provision/provision.awk /app/config/web.php + info "Prepare root password for MySQL" debconf-set-selections <<< 'mariadb-server mysql-server/root_password password' debconf-set-selections <<< 'mariadb-server mysql-server/root_password_again password' diff --git a/vagrant/provision/provision.awk b/vagrant/provision/provision.awk new file mode 100644 index 0000000..bcf44d3 --- /dev/null +++ b/vagrant/provision/provision.awk @@ -0,0 +1,50 @@ +### +# Modifying Yii2's files for Vagrant VM +# +# @author HA3IK +# @version 1.0.0 + +BEGIN { + print "AWK BEGINs its work:" + IGNORECASE = 1 + + # Correct IP - wildcard last octet + match(ip, /(([0-9]+\.)+)/, arr) + ip = arr[1] "*" +} +# BODY +{ + # Check if it's the same file + if (FILENAME != isFile["same"]){ + msg = "- Work with: " FILENAME + # Close a previous file + close(isFile["same"]) + # Delete previous data + delete isFile + # Save current file + isFile["same"] = FILENAME + # Define array index for the file + switch (FILENAME){ + case /config\/web\.php$/: + isFile["IsConfWeb"] = 1 + msg = msg " - add allowed IP: " ip + break + } + # Print the concatenated message for the file + print msg + } + + # IF config/web.php + if (isFile["IsConfWeb"]){ + # IF line has "allowedIPs" and doesn't has our IP + if (match($0, "allowedIPs") && !match($0, ip)){ + match($0, /([^\]]+)(.+)/, arr) + $0 = sprintf("%s, '%s'%s", arr[1], ip, arr[2]) + } + # Rewrite the file + print $0 > FILENAME + } +} +END { + print "AWK ENDs its work." +}