From 2f6c41bf3517fb7817fa158833bdb1f58c7dd69d Mon Sep 17 00:00:00 2001 From: Lucas Beier Date: Tue, 8 Aug 2017 14:44:26 -0300 Subject: [PATCH] Uses environment variables as fallback when no config file is used. In our project we rather set the user and password as environment variables instead of using them hardcoded in a file (following the 12 factor app recommendations). This commit is a propostal to use the environment variables as fallback when no config file is used. --- README.md | 5 ++--- config/{icinga2.json => icinga2.json.example} | 0 lib/icinga2.rb | 14 +++++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) rename config/{icinga2.json => icinga2.json.example} (100%) diff --git a/README.md b/README.md index f3aa853..d86fee4 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ In case you want to use client certificates, set the `client_cn` accordingly. ### Dashing Configuration -Edit `config/icinga2.json` and adjust the settings for the Icinga 2 API credentials. +Move `config/icinga2.json.example` to `config/icinga2.json` and adjust the settings for the Icinga 2 API credentials. ``` vim config/icinga2.json @@ -177,8 +177,7 @@ vim config/icinga2.json } ``` -If you don't have any configuration file yet, the default values from the example above -will be used. +If you don't have any configuration file, the environment variables `ICINGA_API_HOST`, `ICINGA_API_PORT`, `ICINGA_API_USER` and `ICINGA_API_PASSWORD` will be used. If these environment variables are not setted, an `ArgumentError` will raise. If you prefer to use client certificates, set `pki_path` accordingly. The Icinga 2 job expects the certificate file names based on the local FQDN e.g. `pki/icinga2-master1.localdomain.crt`. diff --git a/config/icinga2.json b/config/icinga2.json.example similarity index 100% rename from config/icinga2.json rename to config/icinga2.json.example diff --git a/lib/icinga2.rb b/lib/icinga2.rb index ec3b65c..9fef205 100644 --- a/lib/icinga2.rb +++ b/lib/icinga2.rb @@ -93,12 +93,16 @@ def initialize(configFile) @password = @config["icinga2"]["api"]["password"] @pkiPath = @config["icinga2"]["api"]["pki_path"] else - @log.warn(sprintf('Config file %s not found! Using default config.', configFile)) - @host = "localhost" - @port = 5665 - @user = "dashing" - @password = "icinga2ondashingr0xx" + @log.warn(sprintf('Config file %s not found. Using ENV vars.', configFile)) + @host = ENV['ICINGA_API_HOST'] + @port = ENV['ICINGA_API_PORT'] + @user = ENV['ICINGA_API_USER'] + @password = ENV['ICINGA_API_PASSWORD'] @pkiPath = "pki/" + + if [@host, @port, @user, @password].all? {|value| value.nil? or value == ""} + raise ArgumentError.new('No config file or env var found!') + end end @apiVersion = "v1" # TODO: allow user to configure version?