Skip to content

Commit

Permalink
Merge pull request #472 from DannyBen/add/env-var-whitelist
Browse files Browse the repository at this point in the history
Add `allowed` option to `environment_variable`
  • Loading branch information
DannyBen authored Dec 22, 2023
2 parents a05f7d0 + e205064 commit 6efc553
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/bashly/config_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ def assert_env_var(key, value)
assert_optional_string "#{key}.default", value['default']
assert_boolean "#{key}.required", value['required']
assert_boolean "#{key}.private", value['private']
assert_array "#{key}.allowed", value['allowed'], of: :string

refute value['required'] && value['default'], "#{key} cannot have both nub`required` and nub`default`"
end

def assert_command(key, value)
Expand Down
1 change: 1 addition & 0 deletions lib/bashly/libraries/strings/strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ missing_required_environment_variable: "missing required environment variable: %
missing_dependency: "missing dependency: %{dependency}"
disallowed_flag: "%{name} must be one of: %{allowed}"
disallowed_argument: "%{name} must be one of: %{allowed}"
disallowed_environment_variable: "%{name} environment variable must be one of: %{allowed}"
unsupported_bash_version: "bash version 4 or higher is required"
validation_error: "validation error in %s:\\n%s"
5 changes: 5 additions & 0 deletions lib/bashly/script/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@ def whitelisted_args
args.select(&:allowed)
end

# Returns an array of all the environemnt_variables with a whitelist arg
def whitelisted_environment_variables
environment_variables.select(&:allowed)
end

# Returns an array of all the flags with a whitelist arg
def whitelisted_flags
flags.select(&:allowed)
Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/script/environment_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Script
class EnvironmentVariable < Base
class << self
def option_keys
@option_keys ||= %i[default help name required private]
@option_keys ||= %i[allowed default help name required private]
end
end

Expand Down
13 changes: 12 additions & 1 deletion lib/bashly/views/command/environment_variables_filter.gtx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
if default_environment_variables.any? or required_environment_variables.any?
if default_environment_variables.any? || required_environment_variables.any? ||
whitelisted_environment_variables.any?

= view_marker
= render(:environment_variables_default)

Expand All @@ -10,4 +12,13 @@ if default_environment_variables.any? or required_environment_variables.any?
> fi
end
end

if whitelisted_environment_variables.any?
whitelisted_environment_variables.each do |env_var|
> if [[ -n "${<%= env_var.name.upcase %>:-}" ]] && [[ ! ${<%= env_var.name.upcase %>:-} =~ ^({{ env_var.allowed.join '|' }})$ ]]; then
> printf "%s\n" "{{ strings[:disallowed_environment_variable] % { name: env_var.name.upcase, allowed: env_var.allowed.join(', ') } }}" >&2
> exit 1
> fi
end
end
end
7 changes: 7 additions & 0 deletions schemas/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@
"minLength": 1,
"default": "%{name} must be one of: %{allowed}"
},
"disallowed_environment_variable": {
"title": "disallowed_environment_variable",
"description": "A forbidden environment variable error of the current script\nhttps://bashly.dannyb.co/advanced/strings/#custom-strings",
"type": "string",
"minLength": 1,
"default": "%{name} environment variable must be one of: %{allowed}"
},
"unsupported_bash_version": {
"title": "unsupported bash version",
"description": "An unsupported Bash version error of the current script\nhttps://bashly.dannyb.co/advanced/strings/#custom-strings",
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/validations/env_var_required_and_default
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#<Bashly::ConfigurationError: root.environment_variables[0] cannot have both nub`required` and nub`default`>
8 changes: 8 additions & 0 deletions spec/fixtures/script/validations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@
- name: api_key
required: 1

:env_var_required_and_default:
name: invalid
help: env_var cannot have both required and default
environment_variables:
- name: app_env
required: true
default: dev

:env_var_private:
name: invalid
help: env_var.private should be a boolean
Expand Down

0 comments on commit 6efc553

Please sign in to comment.