|
3 | 3 | require_relative 'spec_helper'
|
4 | 4 | require_relative 'helpers/auth_query_helper'
|
5 | 5 |
|
6 |
| -describe "Auth Query" do |
7 |
| - let(:configured_instances) {[5432, 10432]} |
| 6 | +describe 'Auth Query' do |
| 7 | + let(:configured_instances) { [5432, 10_432] } |
8 | 8 | let(:config_user) { { 'username' => 'sharding_user', 'password' => 'sharding_user' } }
|
9 | 9 | let(:pg_user) { { 'username' => 'sharding_user', 'password' => 'sharding_user' } }
|
10 |
| - let(:processes) { Helpers::AuthQuery.single_shard_auth_query(pool_name: "sharded_db", pg_user: pg_user, config_user: config_user, extra_conf: config, wait_until_ready: wait_until_ready ) } |
| 10 | + let(:processes) do |
| 11 | + Helpers::AuthQuery.single_shard_auth_query(pool_name: 'sharded_db', pg_user: pg_user, config_user: config_user, |
| 12 | + extra_conf: config, wait_until_ready: wait_until_ready) |
| 13 | + end |
11 | 14 | let(:config) { {} }
|
12 | 15 | let(:wait_until_ready) { true }
|
13 | 16 |
|
|
19 | 22 | @failing_process = false
|
20 | 23 | end
|
21 | 24 |
|
22 |
| - context "when auth_query is not configured" do |
| 25 | + context 'when auth_query is not configured' do |
23 | 26 | context 'and cleartext passwords are set' do
|
24 |
| - it "uses local passwords" do |
25 |
| - conn = PG.connect(processes.pgcat.connection_string("sharded_db", config_user['username'], config_user['password'])) |
| 27 | + it 'uses local passwords' do |
| 28 | + conn = PG.connect(processes.pgcat.connection_string('sharded_db', config_user['username'], |
| 29 | + config_user['password'])) |
26 | 30 |
|
27 |
| - expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil |
| 31 | + expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil |
28 | 32 | end
|
29 | 33 | end
|
30 | 34 |
|
31 | 35 | context 'and cleartext passwords are not set' do
|
32 | 36 | let(:config_user) { { 'username' => 'sharding_user' } }
|
33 | 37 |
|
34 |
| - it "does not start because it is not possible to authenticate" do |
| 38 | + it 'does not start because it is not possible to authenticate' do |
35 | 39 | @failing_process = true
|
36 |
| - expect { processes.pgcat }.to raise_error(StandardError, /You have to specify a user password for every pool if auth_query is not specified/) |
| 40 | + expect do |
| 41 | + processes.pgcat |
| 42 | + end.to raise_error(StandardError, |
| 43 | + /You have to specify a user password for every pool if auth_query is not specified/) |
37 | 44 | end
|
38 | 45 | end
|
39 | 46 | end
|
40 | 47 |
|
41 | 48 | context 'when auth_query is configured' do
|
42 | 49 | context 'with global configuration' do
|
43 | 50 | around(:example) do |example|
|
44 |
| - |
45 | 51 | # Set up auth query
|
46 | 52 | Helpers::AuthQuery.set_up_auth_query_for_user(
|
47 | 53 | user: 'md5_auth_user',
|
48 | 54 | password: 'secret'
|
49 |
| - ); |
| 55 | + ) |
50 | 56 |
|
51 | 57 | example.run
|
52 | 58 |
|
53 | 59 | # Drop auth query support
|
54 | 60 | Helpers::AuthQuery.tear_down_auth_query_for_user(
|
55 | 61 | user: 'md5_auth_user',
|
56 | 62 | password: 'secret'
|
57 |
| - ); |
| 63 | + ) |
| 64 | + end |
| 65 | + |
| 66 | + context 'with different lookup database' do |
| 67 | + let(:config) do |
| 68 | + { 'general' => { |
| 69 | + 'auth_query' => "SELECT * FROM public.user_lookup2('$1');", |
| 70 | + 'auth_query_user' => 'md5_auth_use2r', |
| 71 | + 'auth_query_password' => 'secret', |
| 72 | + 'auth_query_database' => 'lookup_db' ## doesn't exist yet |
| 73 | + } } |
| 74 | + end |
| 75 | + |
| 76 | + it 'it uses obtained passwords' do ## should fail |
| 77 | + connection_string = processes.pgcat.connection_string('sharded_db', pg_user['username']) |
| 78 | + conn = PG.connect(connection_string) |
| 79 | + |
| 80 | + expect(conn.exec('SELECT 1 + 2')).not_to be_nil |
| 81 | + end |
58 | 82 | end
|
59 | 83 |
|
60 | 84 | context 'with correct global parameters' do
|
61 |
| - let(:config) { { 'general' => { 'auth_query' => "SELECT * FROM public.user_lookup('$1');", 'auth_query_user' => 'md5_auth_user', 'auth_query_password' => 'secret' } } } |
| 85 | + let(:config) do |
| 86 | + { 'general' => { 'auth_query' => "SELECT * FROM public.user_lookup('$1');", 'auth_query_user' => 'md5_auth_user', |
| 87 | + 'auth_query_password' => 'secret' } } |
| 88 | + end |
62 | 89 | context 'and with cleartext passwords set' do
|
63 | 90 | it 'it uses local passwords' do
|
64 |
| - conn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password'])) |
65 |
| - expect(conn.exec("SELECT 1 + 2")).not_to be_nil |
| 91 | + conn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'], pg_user['password'])) |
| 92 | + expect(conn.exec('SELECT 1 + 2')).not_to be_nil |
66 | 93 | end
|
67 | 94 | end
|
68 | 95 |
|
69 | 96 | context 'and with cleartext passwords not set' do
|
70 | 97 | let(:config_user) { { 'username' => 'sharding_user', 'password' => 'sharding_user' } }
|
71 | 98 |
|
72 | 99 | it 'it uses obtained passwords' do
|
73 |
| - connection_string = processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password']) |
| 100 | + connection_string = processes.pgcat.connection_string('sharded_db', pg_user['username'], |
| 101 | + pg_user['password']) |
74 | 102 | conn = PG.connect(connection_string)
|
75 |
| - expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil |
| 103 | + expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil |
76 | 104 | end
|
77 | 105 |
|
78 | 106 | it 'allows passwords to be changed without closing existing connections' do
|
79 |
| - pgconn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'])) |
80 |
| - expect(pgconn.exec("SELECT 1 + 2")).not_to be_nil |
| 107 | + pgconn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'])) |
| 108 | + expect(pgconn.exec('SELECT 1 + 2')).not_to be_nil |
81 | 109 | Helpers::AuthQuery.exec_in_instances(query: "ALTER USER #{pg_user['username']} WITH ENCRYPTED PASSWORD 'secret2';")
|
82 |
| - expect(pgconn.exec("SELECT 1 + 4")).not_to be_nil |
| 110 | + expect(pgconn.exec('SELECT 1 + 4')).not_to be_nil |
83 | 111 | Helpers::AuthQuery.exec_in_instances(query: "ALTER USER #{pg_user['username']} WITH ENCRYPTED PASSWORD '#{pg_user['password']}';")
|
84 | 112 | end
|
85 | 113 |
|
86 | 114 | it 'allows passwords to be changed and that new password is needed when reconnecting' do
|
87 |
| - pgconn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'])) |
88 |
| - expect(pgconn.exec("SELECT 1 + 2")).not_to be_nil |
| 115 | + pgconn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'])) |
| 116 | + expect(pgconn.exec('SELECT 1 + 2')).not_to be_nil |
89 | 117 | Helpers::AuthQuery.exec_in_instances(query: "ALTER USER #{pg_user['username']} WITH ENCRYPTED PASSWORD 'secret2';")
|
90 |
| - newconn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'], 'secret2')) |
91 |
| - expect(newconn.exec("SELECT 1 + 2")).not_to be_nil |
| 118 | + newconn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'], 'secret2')) |
| 119 | + expect(newconn.exec('SELECT 1 + 2')).not_to be_nil |
92 | 120 | Helpers::AuthQuery.exec_in_instances(query: "ALTER USER #{pg_user['username']} WITH ENCRYPTED PASSWORD '#{pg_user['password']}';")
|
93 | 121 | end
|
94 | 122 | end
|
95 | 123 | end
|
96 | 124 |
|
97 | 125 | context 'with wrong parameters' do
|
98 |
| - let(:config) { { 'general' => { 'auth_query' => 'SELECT 1', 'auth_query_user' => 'wrong_user', 'auth_query_password' => 'wrong' } } } |
| 126 | + let(:config) do |
| 127 | + { 'general' => { 'auth_query' => 'SELECT 1', 'auth_query_user' => 'wrong_user', |
| 128 | + 'auth_query_password' => 'wrong' } } |
| 129 | + end |
99 | 130 |
|
100 | 131 | context 'and with clear text passwords set' do
|
101 |
| - it "it uses local passwords" do |
102 |
| - conn = PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password'])) |
| 132 | + it 'it uses local passwords' do |
| 133 | + conn = PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'], pg_user['password'])) |
103 | 134 |
|
104 |
| - expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil |
| 135 | + expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil |
105 | 136 | end
|
106 | 137 | end
|
107 | 138 |
|
108 | 139 | context 'and with cleartext passwords not set' do
|
109 | 140 | let(:config_user) { { 'username' => 'sharding_user' } }
|
110 |
| - it "it fails to start as it cannot authenticate against servers" do |
| 141 | + it 'it fails to start as it cannot authenticate against servers' do |
111 | 142 | @failing_process = true
|
112 |
| - expect { PG.connect(processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password'])) }.to raise_error(StandardError, /Error trying to obtain password from auth_query/ ) |
| 143 | + expect do |
| 144 | + PG.connect(processes.pgcat.connection_string('sharded_db', pg_user['username'], |
| 145 | + pg_user['password'])) |
| 146 | + end.to raise_error(StandardError, /Error trying to obtain password from auth_query/) |
113 | 147 | end
|
114 | 148 |
|
115 | 149 | context 'and we fix the issue and reload' do
|
116 | 150 | let(:wait_until_ready) { false }
|
117 | 151 |
|
118 | 152 | it 'fails in the beginning but starts working after reloading config' do
|
119 |
| - connection_string = processes.pgcat.connection_string("sharded_db", pg_user['username'], pg_user['password']) |
120 |
| - while !(processes.pgcat.logs =~ /Waiting for clients/) do |
121 |
| - sleep 0.5 |
122 |
| - end |
| 153 | + connection_string = processes.pgcat.connection_string('sharded_db', pg_user['username'], |
| 154 | + pg_user['password']) |
| 155 | + sleep 0.5 until processes.pgcat.logs =~ /Waiting for clients/ |
123 | 156 |
|
124 |
| - expect { PG.connect(connection_string)}.to raise_error(PG::ConnectionBad) |
| 157 | + expect { PG.connect(connection_string) }.to raise_error(PG::ConnectionBad) |
125 | 158 | expect(processes.pgcat.logs).to match(/Error trying to obtain password from auth_query/)
|
126 | 159 |
|
127 | 160 | current_config = processes.pgcat.current_config
|
128 |
| - config = { 'general' => { 'auth_query' => "SELECT * FROM public.user_lookup('$1');", 'auth_query_user' => 'md5_auth_user', 'auth_query_password' => 'secret' } } |
| 161 | + config = { 'general' => { 'auth_query' => "SELECT * FROM public.user_lookup('$1');", |
| 162 | + 'auth_query_user' => 'md5_auth_user', 'auth_query_password' => 'secret' } } |
129 | 163 | processes.pgcat.update_config(current_config.deep_merge(config))
|
130 | 164 | processes.pgcat.reload_config
|
131 | 165 |
|
132 | 166 | conn = nil
|
133 |
| - expect { conn = PG.connect(connection_string)}.not_to raise_error |
134 |
| - expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil |
| 167 | + expect { conn = PG.connect(connection_string) }.not_to raise_error |
| 168 | + expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil |
135 | 169 | end
|
136 | 170 | end
|
137 | 171 | end
|
|
140 | 174 |
|
141 | 175 | context 'with per pool configuration' do
|
142 | 176 | around(:example) do |example|
|
143 |
| - |
144 | 177 | # Set up auth query
|
145 | 178 | Helpers::AuthQuery.set_up_auth_query_for_user(
|
146 | 179 | user: 'md5_auth_user',
|
147 | 180 | password: 'secret'
|
148 |
| - ); |
| 181 | + ) |
149 | 182 |
|
150 | 183 | Helpers::AuthQuery.set_up_auth_query_for_user(
|
151 | 184 | user: 'md5_auth_user1',
|
152 | 185 | password: 'secret',
|
153 | 186 | database: 'shard1'
|
154 |
| - ); |
| 187 | + ) |
155 | 188 |
|
156 | 189 | example.run
|
157 | 190 |
|
158 | 191 | # Tear down auth query
|
159 | 192 | Helpers::AuthQuery.tear_down_auth_query_for_user(
|
160 | 193 | user: 'md5_auth_user',
|
161 | 194 | password: 'secret'
|
162 |
| - ); |
| 195 | + ) |
163 | 196 |
|
164 | 197 | Helpers::AuthQuery.tear_down_auth_query_for_user(
|
165 | 198 | user: 'md5_auth_user1',
|
166 | 199 | password: 'secret',
|
167 | 200 | database: 'shard1'
|
168 |
| - ); |
| 201 | + ) |
169 | 202 | end
|
170 | 203 |
|
171 | 204 | context 'with correct parameters' do
|
172 |
| - let(:processes) { Helpers::AuthQuery.two_pools_auth_query(pool_names: ["sharded_db0", "sharded_db1"], pg_user: pg_user, config_user: config_user, extra_conf: config ) } |
173 |
| - let(:config) { |
174 |
| - { 'pools' => |
175 |
| - { |
176 |
| - 'sharded_db0' => { |
177 |
| - 'auth_query' => "SELECT * FROM public.user_lookup('$1');", |
178 |
| - 'auth_query_user' => 'md5_auth_user', |
179 |
| - 'auth_query_password' => 'secret' |
180 |
| - }, |
181 |
| - 'sharded_db1' => { |
182 |
| - 'auth_query' => "SELECT * FROM public.user_lookup('$1');", |
183 |
| - 'auth_query_user' => 'md5_auth_user1', |
184 |
| - 'auth_query_password' => 'secret' |
185 |
| - }, |
| 205 | + let(:processes) do |
| 206 | + Helpers::AuthQuery.two_pools_auth_query(pool_names: %w[sharded_db0 sharded_db1], pg_user: pg_user, |
| 207 | + config_user: config_user, extra_conf: config) |
| 208 | + end |
| 209 | + let(:config) do |
| 210 | + { 'pools' => { |
| 211 | + 'sharded_db0' => { |
| 212 | + 'auth_query' => "SELECT * FROM public.user_lookup('$1');", |
| 213 | + 'auth_query_user' => 'md5_auth_user', |
| 214 | + 'auth_query_password' => 'secret' |
| 215 | + }, |
| 216 | + 'sharded_db1' => { |
| 217 | + 'auth_query' => "SELECT * FROM public.user_lookup('$1');", |
| 218 | + 'auth_query_user' => 'md5_auth_user1', |
| 219 | + 'auth_query_password' => 'secret' |
186 | 220 | }
|
187 |
| - } |
188 |
| - } |
| 221 | + } } |
| 222 | + end |
189 | 223 |
|
190 | 224 | context 'and with cleartext passwords set' do
|
191 | 225 | it 'it uses local passwords' do
|
192 |
| - conn = PG.connect(processes.pgcat.connection_string("sharded_db0", pg_user['username'], pg_user['password'])) |
193 |
| - expect(conn.exec("SELECT 1 + 2")).not_to be_nil |
194 |
| - conn = PG.connect(processes.pgcat.connection_string("sharded_db1", pg_user['username'], pg_user['password'])) |
195 |
| - expect(conn.exec("SELECT 1 + 2")).not_to be_nil |
| 226 | + conn = PG.connect(processes.pgcat.connection_string('sharded_db0', pg_user['username'], |
| 227 | + pg_user['password'])) |
| 228 | + expect(conn.exec('SELECT 1 + 2')).not_to be_nil |
| 229 | + conn = PG.connect(processes.pgcat.connection_string('sharded_db1', pg_user['username'], |
| 230 | + pg_user['password'])) |
| 231 | + expect(conn.exec('SELECT 1 + 2')).not_to be_nil |
196 | 232 | end
|
197 | 233 | end
|
198 | 234 |
|
199 | 235 | context 'and with cleartext passwords not set' do
|
200 | 236 | let(:config_user) { { 'username' => 'sharding_user' } }
|
201 | 237 |
|
202 | 238 | it 'it uses obtained passwords' do
|
203 |
| - connection_string = processes.pgcat.connection_string("sharded_db0", pg_user['username'], pg_user['password']) |
| 239 | + connection_string = processes.pgcat.connection_string('sharded_db0', pg_user['username'], |
| 240 | + pg_user['password']) |
204 | 241 | conn = PG.connect(connection_string)
|
205 |
| - expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil |
206 |
| - connection_string = processes.pgcat.connection_string("sharded_db1", pg_user['username'], pg_user['password']) |
| 242 | + expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil |
| 243 | + connection_string = processes.pgcat.connection_string('sharded_db1', pg_user['username'], |
| 244 | + pg_user['password']) |
207 | 245 | conn = PG.connect(connection_string)
|
208 |
| - expect(conn.async_exec("SELECT 1 + 2")).not_to be_nil |
| 246 | + expect(conn.async_exec('SELECT 1 + 2')).not_to be_nil |
209 | 247 | end
|
210 | 248 | end
|
211 |
| - |
212 | 249 | end
|
213 | 250 | end
|
214 | 251 | end
|
|
0 commit comments