Permalink
Browse files

Hard-code passwordless access for postgres user and add helpful comme…

…ntary
  • Loading branch information...
1 parent 14ac07a commit 561f91d97e0aa6940d6cc2c35ab6f8d5a137fda1 @davidc-donorschoose davidc-donorschoose committed with jtimberman Jan 7, 2013
Showing with 194 additions and 8 deletions.
  1. +17 −2 README.md
  2. +177 −6 templates/default/pg_hba.conf.erb
View
19 README.md
@@ -118,13 +118,28 @@ the array must be symbols. Each hash will be written as a line in
`pg_hba.conf`. For example, this entry from
`node['postgresql']['pg_hba']`:
- {:type => 'local', :db => 'all', :user => 'postgres', :addr => nil, :method => 'ident'}
+ {:comment => '# Optional comment',
+ :type => 'local', :db => 'all', :user => 'postgres', :addr => nil, :method => 'md5'}
Will result in the following line in `pg_hba.conf`:
- local all postgres ident
+ # Optional comment
+ local all postgres md5
Use `nil` if the CIDR-ADDRESS should be empty (as above).
+Don't provide a comment if none is desired in the `pg_hba.conf` file.
+
+Note that the following authorization rule is supplied automatically by
+the cookbook template. The cookbook needs this to execute SQL in the
+PostgreSQL server without supplying the clear-text password (which isn't
+known by the cookbook). Therefore, your `node['postgresql']['pg_hba']`
+attributes don't need to specify this authorization rule:
+
+ # "local" is for Unix domain socket connections only
+ local all all ident
+
+(By the way, the template uses `peer` instead of `ident` for PostgreSQL-9.1
+and above, which has the same effect.)
Recipes
=======
View
183 templates/default/pg_hba.conf.erb
@@ -1,10 +1,181 @@
-# PostgreSQL Client Authentication Configuration File
# This file was automatically generated and dropped off by Chef!
-# Please refer to the PostgreSQL documentation for details on
-# configuration settings.
-# TYPE DATABASE USER CIDR-ADDRESS METHOD
-<% node['postgresql']['pg_hba'].each do |auth| -%>
-<%= auth[:type] %> <%= auth[:db] %> <%= auth[:user] %> <%= auth[:addr] %> <%= auth[:method] %>
+# PostgreSQL Client Authentication Configuration File
+# ===================================================
+#
+# Refer to the "Client Authentication" section in the PostgreSQL
+# documentation for a complete description of this file. A short
+# synopsis follows.
+#
+# This file controls: which hosts are allowed to connect, how clients
+# are authenticated, which PostgreSQL user names they can use, which
+# databases they can access. Records take one of these forms:
+#
+# local DATABASE USER METHOD [OPTIONS]
+<% if node['postgresql']['version'].to_f < 9.1 -%>
+# host DATABASE USER CIDR-ADDRESS METHOD [OPTIONS]
+# hostssl DATABASE USER CIDR-ADDRESS METHOD [OPTIONS]
+# hostnossl DATABASE USER CIDR-ADDRESS METHOD [OPTIONS]
+<% elsif node['postgresql']['version'].to_f >= 9.1 -%>
+# host DATABASE USER ADDRESS METHOD [OPTIONS]
+# hostssl DATABASE USER ADDRESS METHOD [OPTIONS]
+# hostnossl DATABASE USER ADDRESS METHOD [OPTIONS]
+<% end -%>
+#
+# (The uppercase items must be replaced by actual values.)
+#
+# The first field is the connection type: "local" is a Unix-domain
+# socket, "host" is either a plain or SSL-encrypted TCP/IP socket,
+# "hostssl" is an SSL-encrypted TCP/IP socket, and "hostnossl" is a
+# plain TCP/IP socket.
+#
+<% if node['postgresql']['version'].to_f < 9.0 -%>
+# DATABASE can be "all", "sameuser", "samerole", a
+<% elsif node['postgresql']['version'].to_f >= 9.0 -%>
+# DATABASE can be "all", "sameuser", "samerole", "replication", a
+<% end -%>
+<% if node['postgresql']['version'].to_f < 9.1 -%>
+# database name, or a comma-separated list thereof.
+<% elsif node['postgresql']['version'].to_f >= 9.1 -%>
+# database name, or a comma-separated list thereof. The "all"
+# keyword does not match "replication". Access to replication
+# must be enabled in a separate record (see example below).
+<% end -%>
+#
+# USER can be "all", a user name, a group name prefixed with "+", or a
+# comma-separated list thereof. In both the DATABASE and USER fields
+# you can also write a file name prefixed with "@" to include names
+# from a separate file.
+#
+<% if node['postgresql']['version'].to_f < 9.1 -%>
+# CIDR-ADDRESS specifies the set of hosts the record matches.
+# It is made up of an IP address and a CIDR mask that is
+<% elsif node['postgresql']['version'].to_f >= 9.1 -%>
+# ADDRESS specifies the set of hosts the record matches. It can be a
+# host name, or it is made up of an IP address and a CIDR mask that is
<% end -%>
+# an integer (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that
+<% if node['postgresql']['version'].to_f < 9.1 -%>
+# specifies the number of significant bits in the mask.
+<% elsif node['postgresql']['version'].to_f >= 9.1 -%>
+# specifies the number of significant bits in the mask. A host name
+# that starts with a dot (.) matches a suffix of the actual host name.
+<% end -%>
+# Alternatively, you can write an IP address and netmask in separate
+<% if node['postgresql']['version'].to_f < 9.0 -%>
+# columns to specify the set of hosts.
+<% elsif node['postgresql']['version'].to_f >= 9.0 -%>
+# columns to specify the set of hosts. Instead of a CIDR-address, you
+# can write "samehost" to match any of the server's own IP addresses,
+# or "samenet" to match any address in any subnet that the server is
+# directly connected to.
+<% end -%>
+#
+# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
+<% if node['postgresql']['version'].to_f < 9.0 -%>
+# "krb5", "ident", "pam", "ldap" or "cert". Note that
+<% elsif node['postgresql']['version'].to_f < 9.1 -%>
+# "krb5", "ident", "pam", "ldap", "radius" or "cert". Note that
+<% elsif node['postgresql']['version'].to_f >= 9.1 -%>
+# "krb5", "ident", "peer", "pam", "ldap", "radius" or "cert". Note that
+<% end -%>
+# "password" sends passwords in clear text; "md5" is preferred since
+# it sends encrypted passwords.
+#
+# OPTIONS are a set of options for the authentication in the format
+# NAME=VALUE. The available options depend on the different
+# authentication methods -- refer to the "Client Authentication"
+# section in the documentation for a list of which options are
+# available for which authentication methods.
+#
+# Database and user names containing spaces, commas, quotes and other
+# special characters must be quoted. Quoting one of the keywords
+<% if node['postgresql']['version'].to_f < 9.0 -%>
+# "all", "sameuser" or "samerole" makes the name lose
+<% elsif node['postgresql']['version'].to_f >= 9.0 -%>
+# "all", "sameuser", "samerole" or "replication" makes the name lose
+<% end -%>
+# its special character, and just match a database or username with
+# that name.
+#
+# This file is read on server startup and when the postmaster receives
+# a SIGHUP signal. If you edit the file on a running system, you have
+# to SIGHUP the postmaster for the changes to take effect. You can
+# use "pg_ctl reload" to do that.
+
+# Put your actual configuration here
+# ----------------------------------
+#
+# If you want to allow non-local connections, you need to add more
+# "host" records. In that case you will also need to make PostgreSQL
+# listen on a non-local interface via the listen_addresses
+# configuration parameter, or via the -i or -h command line switches.
+
+
+
+<% if node['postgresql']['version'].to_f < 9.1 -%>
+# TYPE DATABASE USER CIDR-ADDRESS METHOD
+<% elsif node['postgresql']['version'].to_f >= 9.1 -%>
+# TYPE DATABASE USER ADDRESS METHOD
+<% end -%>
+
+# "local" is for Unix domain socket connections only
+<% if node['postgresql']['version'].to_f < 9.1 -%>
+local all all ident
+<% elsif node['postgresql']['version'].to_f >= 9.1 -%>
+local all all peer
+<% end -%>
+# IPv4 local connections:
+#host all all 127.0.0.1/32 ident
+# IPv6 local connections:
+#host all all ::1/128 ident
+<% if node['postgresql']['version'].to_f >= 9.1 -%>
+# Allow replication connections from localhost, by a user with the
+# replication privilege.
+#local replication postgres peer
+#host replication postgres 127.0.0.1/32 ident
+#host replication postgres ::1/128 ident
+<% end -%>
+
+###########
+# The preceeding rule for local login by Unix-domain socket connections
+# was in the pg_hba.conf released by the Postgres Development Group.
+# It was left here as a hard-coded authentication configuration since
+# it is needed by the chef postgresql cookbook recipes.
+#
+# Effectively, it lets postgresql rely on OS account authentication.
+# It allows an OS user to login as the same-named postgresql database
+# user without a password challenge via the Unix-domain socket, but only
+# for same host/no network connections.
+#
+# Specifically, this allows the 'postgres' user to run the psql command
+# on the local host with no -h hostname, with no password. This allows
+# chef recipes to build resources such as the following:
+#
+# bash "some-database-administration-task" do
+# user 'postgres'
+# code <<-EOH
+# echo "-- put sql statement here" | psql
+# EOH
+# action :run
+# end
+#
+# Note that the chef cookbook cannot use the md5 method (password),
+# since chef only has the encrypted password through a node attribute
+# and the plain text password cannot be derived.
+###########
+
+###########
+# Other authentication configurations taken from chef node defaults:
+###########
+<% node['postgresql']['pg_hba'].each do |auth| -%>
+<% if auth[:comment] %>
+<%= auth[:comment] %>
+<% end %>
+<% if auth[:addr] %>
+<%= auth[:type].ljust(7) %> <%= auth[:db].ljust(15) %> <%= auth[:user].ljust(15) %> <%= auth[:addr].ljust(23) %> <%= auth[:method] %>
+<% else %>
+<%= auth[:type].ljust(7) %> <%= auth[:db].ljust(15) %> <%= auth[:user].ljust(15) %> <%= auth[:method] %>
+<% end %>
+<% end %>

0 comments on commit 561f91d

Please sign in to comment.