From f95f2e57d513a99dfa58a230b5fb0b7116aca608 Mon Sep 17 00:00:00 2001
From: Fabrice Gangler <fabrice.gangler@adullact.org>
Date: Mon, 27 Feb 2023 15:47:05 +0100
Subject: [PATCH] feat: allow to configure CFSSL port and IP

---
 CHANGELOG.md |  2 ++
 README.md    | 26 ++++++++++++++++++++++++++
 Vagrantfile  | 17 +++++++++++++----
 3 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f773396..488fa77 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Added
 
+- allow to configure CFSSL port and IP
+
 ### Changed
 
 - use Ubuntu 22.04 instead of Ubuntu 20.04
diff --git a/README.md b/README.md
index f56c39a..ac338bd 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,9 @@ cd  vagrant-cfssl
 ### Step 2 - Download all required Puppet modules used to configure the virtual machine
 
 ```bash
+# To be executed in the directory containing the Vagrantfile
+
+# Download all required Puppet modules used to configure the virtual machine
 ./BUILD.sh
 ```
 
@@ -43,7 +46,30 @@ You can modify this file to change value of any parameter documented by **Puppet
 [REFERENCE.md](https://gitlab.adullact.net/adullact/puppet-cfssl/-/blob/main/REFERENCE.md)
 
 ```bash
+# To be executed in the directory containing the Vagrantfile
+
+# Creates and starts the VM (Virtual Machine) according to the Vagrantfile
+vagrant destroy -f  # stops the running machine Vagrant and destroys all resources
 vagrant up
+
+      # Creates and starts the VM (Virtual Machine)
+      # with some customizations (ports, ip, ...)
+      # - customize CFSSL port   ---> 8888      (default, port allowed above 1000)
+      # - customize CFSSL port   ---> 127.0.0.1 (default)
+      vagrant destroy -f  # stops the running machine Vagrant and destroys all resources
+      VAGRANT_HOST_CFSSL_PORT=8888  \
+      VAGRANT_HOST_CFSSL_IP=0.0.0.0 \
+      vagrant up
+
+# Stops gracefully the VM
+vagrant halt
+
+# Restart the VM
+vagrant up
+
+# Stops the VM and destroys all resources
+# that were created during the machine creation process.
+vagrant destroy -f
 ```
 
 Then you wait few minutes (depends on your network access and power of your computer).
diff --git a/Vagrantfile b/Vagrantfile
index b440b57..02f890a 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -8,14 +8,23 @@ fi
 SCRIPT
 
 Vagrant.configure("2") do |config|
+
+  # Default values
+  default_host_cfssl_port = 8888
+  default_host_cfssl_ip    = "127.0.0.1"
+
+  # Environment variable customizations
+  host_cfssl_port = ENV['VAGRANT_HOST_CFSSL_PORT'] ? ENV['VAGRANT_HOST_CFSSL_PORT'] : default_host_cfssl_port
+  host_cfssl_ip   = ENV['VAGRANT_HOST_CFSSL_IP']   ? ENV['VAGRANT_HOST_CFSSL_IP']   : default_host_cfssl_ip
+
   config.vm.box = "ubuntu/jammy64" # Ubuntu 22.04
   config.vm.hostname = "pki.example.org"
-  config.vm.network "forwarded_port", id: 'CfsslApi',  guest: 8888,  host: 8888, auto_correct: true, host_ip: "127.0.0.1"
-  # config.vm.network "forwarded_port", id: 'PostgreSQL', guest: 5432, host: 5432, auto_correct: true, host_ip: "127.0.0.1"
+  config.vm.network "forwarded_port", id: 'CfsslApi',  guest: 8888,  host: host_cfssl_port, auto_correct: true, host_ip: host_cfssl_ip
+# config.vm.network "forwarded_port", id: 'PostgreSQL', guest: 5432, host: 5432, auto_correct: true, host_ip: "127.0.0.1"
   config.vm.provider "virtualbox" do |vb|
     vb.name = "DEMO_CFSSL"
-    # vb.memory = "4096"
-    # vb.cpus = "4"
+  # vb.memory = "4096"
+  # vb.cpus = "4"
   end
 
   config.vm.synced_folder "puppet/hieradata/", "/tmp/vagrant-puppet/hieradata"
-- 
GitLab