From 1a18c65d60d2a8a74a872f7d8f5235fc67a83985 Mon Sep 17 00:00:00 2001 From: Jevgenij Sevostjanov Date: Wed, 31 Jan 2018 19:21:22 +0000 Subject: [PATCH] [CC-1227] Capability to provide environment variables from dashboard --- cookbooks/env_vars/libraries/helpers.rb | 11 ++++++++++- cookbooks/env_vars/templates/default/env.cloud.erb | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/cookbooks/env_vars/libraries/helpers.rb b/cookbooks/env_vars/libraries/helpers.rb index 78b73997..e4f437ef 100644 --- a/cookbooks/env_vars/libraries/helpers.rb +++ b/cookbooks/env_vars/libraries/helpers.rb @@ -16,10 +16,19 @@ def fetch_environment_variables(app_data) return [] unless metadata && metadata['environment_variables'] variables = metadata['environment_variables'].map do |var_hash| - { :name => var_hash['name'], :value => ::Base64.strict_decode64(var_hash['value']) } + if variable_validator(var_hash[:name]) + { :name => var_hash['name'], :value => escape_variable_value(::Base64.strict_decode64(var_hash['value'])).delete!("\n") } + else + Chef::Log.error('[COOKBOOK ENV_VARS] Variable name is not valid') + end end end + # Function for variable name validation + def variable_validator(name) + !!name.match(/\A[a-zA-Z]\w*\z/) + end + # Escapes the value of variable to be correctly enclosed in double quotes. Enclosing characters # in double quotes (") preserves the literal value of all characters within the quotes, with the # exception of $, `, \, and, when history expansion is enabled, !. diff --git a/cookbooks/env_vars/templates/default/env.cloud.erb b/cookbooks/env_vars/templates/default/env.cloud.erb index ff00ed70..caf383de 100644 --- a/cookbooks/env_vars/templates/default/env.cloud.erb +++ b/cookbooks/env_vars/templates/default/env.cloud.erb @@ -1,3 +1,3 @@ -<% @environment_variables.each do |variable| %> -export <%= variable[:name] %>="<%= escape_variable_value(variable[:value]) %>" -<% end %> +<% @environment_variables.each do |variable| -%> +export <%=variable[:name]%>="<%= variable[:value] %>" +<% end -%>