@@ -34,6 +34,40 @@ import (
3434 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
3535)
3636
37+ func TestGeneratePostgresHBA (t * testing.T ) {
38+ reconciler := & Reconciler {}
39+
40+ assert .Assert (t , reconciler .generatePostgresHBA (nil ) == nil ,
41+ "expected nil to return nil" )
42+
43+ for _ , tt := range []struct {
44+ rule , expected string
45+ }{
46+ {
47+ rule : `{ connection: host, method: scram }` ,
48+ expected : `"host" all all all "scram"` ,
49+ },
50+ {
51+ rule : `{ connection: local, method: peer, databases: [one, two] }` ,
52+ expected : `"local" "one","two" all all "peer"` ,
53+ },
54+ {
55+ rule : `{ connection: local, method: peer, users: [alice, bob] }` ,
56+ expected : `"local" all "alice","bob" all "peer"` ,
57+ },
58+ {
59+ rule : `{ connection: hostssl, method: md5, options: { clientcert: verify-ca } }` ,
60+ expected : `"hostssl" all all all "md5" "clientcert"="verify-ca"` ,
61+ },
62+ } {
63+ var rule * v1beta1.PostgresHBARule
64+ require .UnmarshalInto (t , & rule , tt .rule )
65+
66+ hba := reconciler .generatePostgresHBA (rule )
67+ assert .Equal (t , hba .String (), tt .expected , "\n %#v" , rule )
68+ }
69+ }
70+
3771func TestGeneratePostgresHBAs (t * testing.T ) {
3872 ctx := context .Background ()
3973 reconciler := & Reconciler {}
@@ -50,12 +84,35 @@ func TestGeneratePostgresHBAs(t *testing.T) {
5084 assert .Assert (t , len (required ) > 0 ,
5185 "expected at least one mandatory rule" )
5286
87+ t .Run ("Authentication" , func (t * testing.T ) {
88+ cluster := v1beta1 .NewPostgresCluster ()
89+ require .UnmarshalInto (t , & cluster .Spec .Authentication , `{
90+ rules: [
91+ { connection: host, method: scram },
92+ { connection: local, method: peer, users: [alice, bob] },
93+ ],
94+ }` )
95+
96+ result := reconciler .generatePostgresHBAs (ctx , cluster ).AsStrings ()
97+ assert .Assert (t , cmp .Len (result , len (required )+ 2 ),
98+ "expected two rules from the Authentication section and no defaults" )
99+
100+ // mandatory rules should be first
101+ assert .DeepEqual (t , result [:len (required )], required )
102+
103+ // specified rules should be last and in their original order
104+ assert .DeepEqual (t , result [len (required ):], []string {
105+ `"host" all all all "scram"` ,
106+ `"local" all "alice","bob" all "peer"` ,
107+ })
108+ })
109+
53110 t .Run ("Patroni" , func (t * testing.T ) {
54111 cluster := v1beta1 .NewPostgresCluster ()
55112 require .UnmarshalInto (t , & cluster .Spec .Patroni , `{
56- dynamicConfiguration: {
57- postgresql: { pg_hba: [ "first custom", "another" ] },
58- },
113+ dynamicConfiguration: {
114+ postgresql: { pg_hba: [ "first custom", "another" ] },
115+ },
59116 }` )
60117
61118 result := reconciler .generatePostgresHBAs (ctx , cluster ).AsStrings ()
@@ -68,6 +125,36 @@ func TestGeneratePostgresHBAs(t *testing.T) {
68125 // specified rules should be last and in their original order
69126 assert .DeepEqual (t , result [len (required ):], []string {`first custom` , `another` })
70127 })
128+
129+ t .Run ("Precedence" , func (t * testing.T ) {
130+ cluster := v1beta1 .NewPostgresCluster ()
131+ require .UnmarshalInto (t , & cluster .Spec .Authentication , `{
132+ rules: [
133+ { connection: host, method: scram },
134+ { connection: local, method: peer, users: [alice, bob] },
135+ ],
136+ }` )
137+ require .UnmarshalInto (t , & cluster .Spec .Patroni , `{
138+ dynamicConfiguration: {
139+ postgresql: { pg_hba: [ "another" ] },
140+ },
141+ }` )
142+
143+ result := reconciler .generatePostgresHBAs (ctx , cluster ).AsStrings ()
144+ assert .Assert (t , cmp .Len (result , len (required )+ 2 + 1 ),
145+ "expected two rules from the Authentication section" +
146+ " plus one from the Patroni section" )
147+
148+ // mandatory rules should be first
149+ assert .DeepEqual (t , result [:len (required )], required )
150+
151+ // specified rules are next, no defaults
152+ assert .DeepEqual (t , result [len (required ):], []string {
153+ `"host" all all all "scram"` , // Authentication
154+ `"local" all "alice","bob" all "peer"` , // Authentication
155+ `another` , // Patroni
156+ })
157+ })
71158}
72159
73160func TestGeneratePostgresParameters (t * testing.T ) {
0 commit comments