Skip to content

Commit 203e757

Browse files
Merge pull request rails#43068 from mbayucot/42994-add-support-for-postgresql-certs-on-db-tasks
Add ssl support for postgresql database tasks
2 parents c0e17d4 + 99bc69a commit 203e757

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

activerecord/CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
* Add ssl support for postgresql database tasks
2+
3+
Add `PGSSLMODE`, `PGSSLCERT`, `PGSSLKEY` and `PGSSLROOTCERT` to pg_env from database config
4+
when running postgresql database tasks.
5+
6+
```yaml
7+
# config/database.yml
8+
9+
production:
10+
sslmode: verify-full
11+
sslcert: client.crt
12+
sslkey: client.key
13+
sslrootcert: ca.crt
14+
```
15+
16+
Environment variables
17+
18+
```
19+
PGSSLMODE=verify-full
20+
PGSSLCERT=client.crt
21+
PGSSLKEY=client.key
22+
PGSSLROOTCERT=ca.crt
23+
```
24+
25+
Fixes #42994
26+
27+
*Michael Bayucot*
28+
129
* Avoid scoping update callbacks in `ActiveRecord::Relation#update!`.
230

331
Making it consistent with how scoping is applied only to the query in `ActiveRecord::Relation#update`

activerecord/lib/active_record/tasks/postgresql_database_tasks.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ def establish_master_connection
9999

100100
def psql_env
101101
{}.tap do |env|
102-
env["PGHOST"] = db_config.host if db_config.host
103-
env["PGPORT"] = configuration_hash[:port].to_s if configuration_hash[:port]
104-
env["PGPASSWORD"] = configuration_hash[:password].to_s if configuration_hash[:password]
105-
env["PGUSER"] = configuration_hash[:username].to_s if configuration_hash[:username]
102+
env["PGHOST"] = db_config.host if db_config.host
103+
env["PGPORT"] = configuration_hash[:port].to_s if configuration_hash[:port]
104+
env["PGPASSWORD"] = configuration_hash[:password].to_s if configuration_hash[:password]
105+
env["PGUSER"] = configuration_hash[:username].to_s if configuration_hash[:username]
106+
env["PGSSLMODE"] = configuration_hash[:sslmode].to_s if configuration_hash[:sslmode]
107+
env["PGSSLCERT"] = configuration_hash[:sslcert].to_s if configuration_hash[:sslcert]
108+
env["PGSSLKEY"] = configuration_hash[:sslkey].to_s if configuration_hash[:sslkey]
109+
env["PGSSLROOTCERT"] = configuration_hash[:sslrootcert].to_s if configuration_hash[:sslrootcert]
106110
end
107111
end
108112

activerecord/test/cases/tasks/postgresql_rake_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,18 @@ def test_structure_dump_with_env
383383
end
384384
end
385385

386+
def test_structure_dump_with_ssl_env
387+
expected_env = { "PGSSLMODE" => "verify-full", "PGSSLCERT" => "client.crt", "PGSSLKEY" => "client.key", "PGSSLROOTCERT" => "root.crt" }
388+
expected_command = [expected_env, "pg_dump", "--schema-only", "--no-privileges", "--no-owner", "--file", @filename, "my-app-db"]
389+
390+
assert_called_with(Kernel, :system, expected_command, returns: true) do
391+
ActiveRecord::Tasks::DatabaseTasks.structure_dump(
392+
@configuration.merge(sslmode: "verify-full", sslcert: "client.crt", sslkey: "client.key", sslrootcert: "root.crt"),
393+
@filename
394+
)
395+
end
396+
end
397+
386398
def test_structure_dump_with_extra_flags
387399
expected_command = [{}, "pg_dump", "--schema-only", "--no-privileges", "--no-owner", "--file", @filename, "--noop", "my-app-db"]
388400

@@ -550,6 +562,21 @@ def test_structure_load_with_env
550562
end
551563
end
552564

565+
def test_structure_load_with_ssl_env
566+
filename = "awesome-file.sql"
567+
expected_env = { "PGSSLMODE" => "verify-full", "PGSSLCERT" => "client.crt", "PGSSLKEY" => "client.key", "PGSSLROOTCERT" => "root.crt" }
568+
expected_command = [expected_env, "psql", "--set", "ON_ERROR_STOP=1", "--quiet", "--no-psqlrc", "--file", filename, "--noop", @configuration["database"]]
569+
570+
assert_called_with(Kernel, :system, expected_command, returns: true) do
571+
with_structure_load_flags(["--noop"]) do
572+
ActiveRecord::Tasks::DatabaseTasks.structure_load(
573+
@configuration.merge(sslmode: "verify-full", sslcert: "client.crt", sslkey: "client.key", sslrootcert: "root.crt"),
574+
filename
575+
)
576+
end
577+
end
578+
end
579+
553580
def test_structure_load_with_hash_extra_flags_for_a_different_driver
554581
filename = "awesome-file.sql"
555582
expected_command = [{}, "psql", "--set", "ON_ERROR_STOP=1", "--quiet", "--no-psqlrc", "--file", filename, @configuration["database"]]

0 commit comments

Comments
 (0)