Skip to content

Commit 1b9f242

Browse files
authored
Land rapid7#19159, improve error handling for postgres platform/arch detection
2 parents 82ce0a9 + 3d044c4 commit 1b9f242

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

lib/postgres/postgres-pr/connection.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,18 @@ def map_compile_arch_to_architecture(compile_arch)
213213
def detect_platform_and_arch
214214
result = {}
215215

216-
query_result = query('select version()').rows.join.match(/on (?<architecture>\w+)-\w+-(?<platform>\w+)/)
217-
server_vars = {
218-
'version_compile_machine' => query_result[:architecture],
219-
'version_compile_os' => query_result[:platform]
220-
}
221-
222-
result[:arch] = map_compile_arch_to_architecture(server_vars['version_compile_machine'])
223-
result[:platform] = map_compile_os_to_platform(server_vars['version_compile_os'])
216+
query_result = query('select version()').rows[0][0]
217+
match_platform_and_arch = query_result.match(/on (?<architecture>\w+)-\w+-(?<platform>\w+)/)
218+
219+
if match_platform_and_arch.nil?
220+
arch = platform = query_result
221+
else
222+
arch = match_platform_and_arch[:architecture]
223+
platform = match_platform_and_arch[:platform]
224+
end
225+
226+
result[:arch] = map_compile_arch_to_architecture(arch)
227+
result[:platform] = map_compile_os_to_platform(platform)
224228

225229
result
226230
end

spec/lib/rex/proto/postgresql/client_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@
6868
[
6969
{ version: 'PostgreSQL 9.4.26 on x86_64-pc-linux-gnu (Debian 9.4.26-1.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit', expected: { arch: 'x86_64', platform: 'Linux' } },
7070
{ version: 'PostgreSQL 14.11 (Debian 14.11-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit', expected: { arch: 'x86_64', platform: 'Linux' } },
71-
{ version: 'PostgreSQL 14.11 (Homebrew) on x86_64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit', expected: { arch: 'x86_64', platform: 'OSX' } }
71+
{ version: 'PostgreSQL 14.11 (Homebrew) on x86_64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit', expected: { arch: 'x86_64', platform: 'OSX' } },
72+
{
73+
version: 'PostgreSQL 14.11 (Homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>',
74+
expected: {
75+
arch: 'postgresql 14.11 (homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>',
76+
platform: 'postgresql 14.11 (homebrew) <arch>-<platform>, compiled by <platform> clang version 15.0.0 (clang-1500.1.0.2.5), <arch>'
77+
}
78+
}
7279
].each do |test|
7380
context "when the database is version #{test[:version]}" do
7481
it "returns #{test[:expected]}" do

0 commit comments

Comments
 (0)