Skip to content

docker_run_flags: Shellescape any provided values #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions lib/puppet/parser/functions/docker_run_flags.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
# frozen_string_literal: true

require 'shellwords'
#
# docker_run_flags.rb
#
module Puppet::Parser::Functions
newfunction(:'docker::escape', type: :rvalue) do |args|
subject = args[0]

escape_function = if self['facts'] && self['facts']['os']['family'] == 'windows'
'powershell_escape'
else
'shell_escape'
end

call_function(escape_function, subject)
end

# Transforms a hash into a string of docker flags
newfunction(:docker_run_flags, type: :rvalue) do |args|
opts = args[0] || {}
flags = []

if opts['username']
flags << "-u '#{opts['username'].shellescape}'"
flags << "-u #{call_function('docker::escape', [opts['username']])}"
end

if opts['hostname']
flags << "-h '#{opts['hostname'].shellescape}'"
flags << "-h #{call_function('docker::escape', [opts['hostname']])}"
end

if opts['restart']
Expand All @@ -24,9 +35,9 @@ module Puppet::Parser::Functions

if opts['net']
if opts['net'].is_a? String
flags << "--net #{opts['net'].shellescape}"
flags << "--net #{call_function('docker::escape', [opts['net']])}"
elsif opts['net'].is_a? Array
flags << "--net #{opts['net'].join(' --net ').shellescape}"
flags += opts['net'].map { |item| ["--net #{call_function('docker::escape', [item])}"] }
end
end

Expand Down Expand Up @@ -72,17 +83,17 @@ module Puppet::Parser::Functions

multi_flags = ->(values, fmt) {
filtered = [values].flatten.compact
filtered.map { |val| (fmt + params_join_char) % val }
filtered.map { |val| (fmt + params_join_char) % call_function('docker::escape', [val]) }
}

[
['--dns %s', 'dns'],
['--dns-search %s', 'dns_search'],
['--expose=%s', 'expose'],
['--link %s', 'links'],
['--lxc-conf="%s"', 'lxc_conf'],
['--lxc-conf=%s', 'lxc_conf'],
['--volumes-from %s', 'volumes_from'],
['-e "%s"', 'env'],
['-e %s', 'env'],
['--env-file %s', 'env_file'],
['-p %s', 'ports'],
['-l %s', 'labels'],
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": [
{
"name": "puppetlabs/stdlib",
"version_requirement": ">= 4.24.0 < 9.0.0"
"version_requirement": ">= 8.2.0 < 9.0.0"
},
{
"name": "puppetlabs/apt",
Expand Down
14 changes: 7 additions & 7 deletions spec/acceptance/docker_params_changed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
else
docker_args = ''
docker_network = 'bridge'
volume_location = '/opt'
volume_location = '/opt/'
docker_image = 'hello-world:linux'
end

Expand All @@ -33,8 +33,8 @@
install_pp = "class { 'docker': #{docker_args}}"
apply_manifest(install_pp)
end
run_shell("mkdir #{volume_location}/volume_1")
run_shell("mkdir #{volume_location}/volume_2")
run_shell("mkdir #{volume_location}volume_1")
run_shell("mkdir #{volume_location}volume_2")
end

context 'when image is changed' do
Expand Down Expand Up @@ -78,8 +78,8 @@ class {'docker': #{docker_args}}
volumes1 = "volumes => ['volume-1:C:\\volume_1']"
volumes2 = "volumes => ['volume-1:C:\\volume_1', 'volume-2:C:\\volume_2']"
else
volumes1 = "volumes => ['volume-1:#{volume_location}/volume_1']"
volumes2 = "volumes => ['volume-1:#{volume_location}/volume_1', 'volume-2:#{volume_location}/volume_2']"
volumes1 = "volumes => ['volume-1:#{volume_location}volume_1']"
volumes2 = "volumes => ['volume-1:#{volume_location}volume_1', 'volume-2:#{volume_location}volume_2']"
end

let(:pp1) do
Expand Down Expand Up @@ -143,7 +143,7 @@ class {'docker': #{docker_args}}
end

after(:all) do
run_shell("rm -r #{volume_location}/volume_1")
run_shell("rm -r #{volume_location}/volume_2")
run_shell("rm -r #{volume_location}volume_1")
run_shell("rm -r #{volume_location}volume_2")
end
end
32 changes: 32 additions & 0 deletions spec/unit/lib/puppet/parser/functions/docker_run_flags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'spec_helper'

describe 'the "docker_run_flags" parser function' do
before :each do
Puppet[:modulepath] = 'spec/fixtures/modules'
end

let :scope do
node = Puppet::Node.new('localhost')
compiler = Puppet::Parser::Compiler.new(node)
scope = Puppet::Parser::Scope.new(compiler)
allow(scope).to receive(:environment).and_return(nil)
allow(scope).to receive(:[]).with('facts').and_return({ 'os' => { 'family' => os_family } })
scope
end

context 'on POSIX system' do
let(:os_family) { 'Linux' }

it 'escapes special chars' do
expect(scope.function_docker_run_flags([{ 'env' => [%.MYSQL_PASSWORD='"$()[]{}<>.], 'extra_params' => [] }])).to eq(%(-e MYSQL_PASSWORD\\=\\'\\"\\$\\(\\)\\[\\]\\{\\}\\<\\> \\\n))
end
end

context 'on windows' do
let(:os_family) { 'windows' }

it 'escapes special chars' do
expect(scope.function_docker_run_flags([{ 'env' => [%.MYSQL_PASSWORD='"$()[]{}<>.], 'extra_params' => [] }])).to eq(%^-e MYSQL_PASSWORD=`'\\`"`$()[]{}<> \\\n^)
end
end
end