Skip to content

Commit fa8506e

Browse files
vin01chr4
authored andcommitted
Support connection limti configuration for roles
1 parent fe1d50a commit fa8506e

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

init.sls

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ createuser-{{ index }}:
134134
{% endif %}
135135
- user: postgres
136136

137+
{% set connection_limit = config.get('connection_limit') %}
138+
{% if connection_limit %}
139+
set_user_{{ config['username'] }}_connection_limit:
140+
cmd.run:
141+
- name: psql -U postgres -c "ALTER ROLE {{ config['username'] }} CONNECTION LIMIT {{ connection_limit }};"
142+
- unless: psql -U postgres -t -c "SELECT rolconnlimit FROM pg_roles WHERE rolname = '{{ config['username'] }}';" | grep -wq {{ connection_limit }}
143+
- runas: postgres
144+
{% endif %}
145+
137146
# The "replication" and "all" keywords are not real databases but special keywords used for permissions in pg_hba.conf
138147
{% if config['database'] != "replication" and config['database'] != 'all' %}
139148

pillar.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ postgresql:
138138
address: 10.1.2.0/24
139139
method: trust
140140
login: false
141+
connection_limit: 100
141142

142143
- username: example_user_with_role
143144
type: host

tests/integration/postgresql/postgresql_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@
138138
its('output') { should match /example_role/ }
139139
end
140140

141+
# Assert that connection limit is reflected correctly
142+
describe sql.query("SELECT rolconnlimit FROM pg_roles WHERE rolname='example_role'") do
143+
its('output') { should eq('100') }
144+
end
145+
146+
# Assert that connection limit for other users is the default
147+
describe sql.query("SELECT rolconnlimit FROM pg_roles WHERE rolname='deploy'") do
148+
its('output') { should eq('-1') }
149+
end
150+
141151
# Assert extensions
142152
describe sql.query("SELECT extname FROM pg_extension;", ['db_with_extension']) do
143153
its('output') { should match /pgcrypto/ }

0 commit comments

Comments
 (0)