Skip to content
Open
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
19 changes: 16 additions & 3 deletions src/chef-server-ctl/plugins/psql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# limitations under the License.

require "json"
require "shellwords"

known_dbs = {
"opscode_chef" => { "dbname" => "opscode_chef", "config_key" => "opscode-erchef", "hashseed" => "private_chef" },
Expand All @@ -34,8 +35,12 @@
service_name = ARGV[1]
write_arg = "--write"
options_arg = "--options"
psql_options = []
if ARGV.include?(options_arg)
psql_options = " #{ARGV[ARGV.index(options_arg) + 1]}"
# Split the user-supplied options string into individual tokens
# (honoring shell-style quoting) so each becomes a literal argv
# element passed to psql. This avoids handing the string to a shell.
psql_options = Shellwords.split(ARGV[ARGV.index(options_arg) + 1].to_s)
end

known_db_names = known_dbs.keys.sort.join(", ")
Expand Down Expand Up @@ -96,6 +101,14 @@
ENV["PGPASSWORD"] = db_password
ENV["PAGER"] = "less"
ENV["LESS"] = "-iMSx4 -FX"
cmd = "/opt/opscode/embedded/bin/psql --host #{db_host} --username #{db_username} --port #{db_port} --dbname #{db_name}#{psql_options}"
exec cmd
# Exec psql with an explicit argument vector (no shell). Passing the
# command as separate arguments means user-supplied --options tokens are
# delivered to psql verbatim and are never interpreted as shell syntax.
cmd = ["/opt/opscode/embedded/bin/psql",
"--host", db_host.to_s,
"--username", db_username.to_s,
"--port", db_port.to_s,
"--dbname", db_name.to_s,
*psql_options]
exec(*cmd)
end
Loading