Skip to content

Commit

Permalink
Add basic validation for exec source data
Browse files Browse the repository at this point in the history
This commit adds enough validation of data returned by exec source
commands to at least fail if the data isn't a hash. Small steps.
  • Loading branch information
reidmv committed Apr 21, 2020
1 parent 5153dd9 commit 2e5a62c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/r10k/source/exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def initialize(name, basedir, options = {})
end
end

unless R10K::Source::Hash.valid_environment_hash?(environments)
raise R10K::Error, _("Environment source ${name} command %{cmd} did not return valid environment data.\n" \
'Returned: %{dat}') % {name: name, cmd: command, dat: environments}
end

# Set the environments key for the parent class to consume
options[:environments] = environments

Expand Down
8 changes: 8 additions & 0 deletions lib/r10k/source/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ class R10K::Source::Hash < R10K::Source::Base

include R10K::Logging

# @param hash [Hash] A hash to validate.
# @return [Boolean] False if the hash is obviously invalid. A true return
# means _maybe_ it's valid.
def self.valid_environment_hash?(hash)
# TODO: more robust schema valiation
hash.is_a?(Hash)
end

# @param name [String] The identifier for this source.
# @param basedir [String] The base directory where the generated environments will be created.
# @param options [Hash] An additional set of options for this source. The
Expand Down
9 changes: 8 additions & 1 deletion spec/unit/source/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@
end

context 'that produces invalid output' do
it "rejects schema invalid data"
it "rejects strings as schema invalid data" do
allow_any_instance_of(R10K::Util::Subprocess)
.to receive(:execute)
.and_return(double('result', stdout: "200 OK"))

expect { described_class.new('execsource', '/some/nonexistent/dir', command: '/path/to/command') }
.to raise_error(/did not return valid environment data/)
end

it 'raises an error for non-json, non-yaml data' do
allow_any_instance_of(R10K::Util::Subprocess)
Expand Down

0 comments on commit 2e5a62c

Please sign in to comment.