Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure required LDAP HBA options are present #4140

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,25 @@ spec:
x-kubernetes-map-type: atomic
x-kubernetes-validations:
- message: '"hba" cannot be combined with other fields'
rule: 'has(self.hba) ? !has(self.connection) && !has(self.databases)
&& !has(self.method) && !has(self.options) && !has(self.users)
: true'
rule: '[has(self.hba), has(self.connection) || has(self.databases)
|| has(self.method) || has(self.options) || has(self.users)].exists_one(b,b)'
- message: '"connection" and "method" are required'
rule: 'has(self.hba) ? true : has(self.connection) && has(self.method)'
rule: has(self.hba) || (has(self.connection) && has(self.method))
- message: the "ldap" method requires an "ldapbasedn", "ldapprefix",
or "ldapsuffix" option
rule: has(self.hba) || self.method != "ldap" || (has(self.options)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand this to say that either we have hba or we have ldap with ldap_ options? Is this another way to say that?

Suggested change
rule: has(self.hba) || self.method != "ldap" || (has(self.options)
rule: has(self.hba) || self.method == "ldap" && (has(self.options)

The "A or not B or C (required by B)" feels a little harder to read for me, but am I missing something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every rule must evaluate to true. self.method == "ldap" rejects all other methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read these as "valid when (1) has hba or (2) isn't ldap or (3) [ldap stuff]"

Copy link
Member Author

@cbandy cbandy Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have hba or we have ldap with ldap options

Nope. This is saying:

when [we don't have hba] and [the method is ldap],
then [options must be] and [options must blah blah];
otherwise [ok]

But that ☝🏻 "otherwise" is not intended to be "otherwise [the whole thing]." So, it's spelled:

when [we have hba] or [the method isn't ldap], then [ok];
otherwise, [options must be] and [options must blah blah].

&& ["ldapbasedn","ldapprefix","ldapsuffix"].exists(k, k
in self.options))
- message: cannot use "ldapbasedn", "ldapbinddn", "ldapbindpasswd",
"ldapsearchattribute", or "ldapsearchfilter" options with
"ldapprefix" or "ldapsuffix" options
rule: has(self.hba) || self.method != "ldap" || !has(self.options)
|| [["ldapprefix","ldapsuffix"], ["ldapbasedn","ldapbinddn","ldapbindpasswd","ldapsearchattribute","ldapsearchfilter"]].exists_one(a,
a.exists(k, k in self.options))
- message: the "radius" method requires "radiusservers" and
"radiussecrets" options
rule: has(self.hba) || self.method != "radius" || (has(self.options)
&& ["radiusservers","radiussecrets"].all(k, k in self.options))
maxItems: 10
type: array
x-kubernetes-list-type: atomic
Expand Down
115 changes: 115 additions & 0 deletions internal/testing/validation/postgrescluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,121 @@ func TestPostgresAuthenticationRules(t *testing.T) {
assert.Assert(t, cmp.Contains(cause.Message, "unsafe"))
}
})

t.Run("LDAP", func(t *testing.T) {
t.Run("Required", func(t *testing.T) {
cluster := base.DeepCopy()
require.UnmarshalInto(t, &cluster.Spec.Authentication, `{
rules: [
{ connection: hostssl, method: ldap },
{ connection: hostssl, method: ldap, options: {} },
{ connection: hostssl, method: ldap, options: { ldapbinddn: any } },
],
}`)

err := cc.Create(ctx, cluster, client.DryRunAll)
assert.Assert(t, apierrors.IsInvalid(err))

status := require.StatusError(t, err)
assert.Assert(t, status.Details != nil)
assert.Assert(t, cmp.Len(status.Details.Causes, 3))

for i, cause := range status.Details.Causes {
assert.Equal(t, cause.Field, fmt.Sprintf("spec.authentication.rules[%d]", i), "%#v", cause)
assert.Assert(t, cmp.Contains(cause.Message, `"ldap" method requires`))
}

// These are valid.

cluster.Spec.Authentication = nil
require.UnmarshalInto(t, &cluster.Spec.Authentication, `{
rules: [
{ connection: hostssl, method: ldap, options: { ldapbasedn: any } },
{ connection: hostssl, method: ldap, options: { ldapprefix: any } },
{ connection: hostssl, method: ldap, options: { ldapsuffix: any } },
],
}`)
assert.NilError(t, cc.Create(ctx, cluster, client.DryRunAll))
})

t.Run("Mixed", func(t *testing.T) {
// Some options cannot be combined with others.

cluster := base.DeepCopy()
require.UnmarshalInto(t, &cluster.Spec.Authentication, `{
rules: [
{ connection: hostssl, method: ldap, options: { ldapbinddn: any, ldapprefix: other } },
{ connection: hostssl, method: ldap, options: { ldapbasedn: any, ldapsuffix: other } },
],
}`)

err := cc.Create(ctx, cluster, client.DryRunAll)
assert.Assert(t, apierrors.IsInvalid(err))

status := require.StatusError(t, err)
assert.Assert(t, status.Details != nil)
assert.Assert(t, cmp.Len(status.Details.Causes, 2))

for i, cause := range status.Details.Causes {
assert.Equal(t, cause.Field, fmt.Sprintf("spec.authentication.rules[%d]", i), "%#v", cause)
assert.Assert(t, cmp.Regexp(`cannot use .+? options with .+? options`, cause.Message))
}

// These combinations are allowed.

cluster.Spec.Authentication = nil
require.UnmarshalInto(t, &cluster.Spec.Authentication, `{
rules: [
{ connection: hostssl, method: ldap, options: { ldapprefix: one, ldapsuffix: two } },
{ connection: hostssl, method: ldap, options: { ldapbasedn: one, ldapbinddn: two } },
{ connection: hostssl, method: ldap, options: {
ldapbasedn: one, ldapsearchattribute: two, ldapsearchfilter: three,
} },
],
}`)
assert.NilError(t, cc.Create(ctx, cluster, client.DryRunAll))
})
})

t.Run("RADIUS", func(t *testing.T) {
t.Run("Required", func(t *testing.T) {
cluster := base.DeepCopy()
require.UnmarshalInto(t, &cluster.Spec.Authentication, `{
rules: [
{ connection: hostssl, method: radius },
{ connection: hostssl, method: radius, options: {} },
{ connection: hostssl, method: radius, options: { radiusidentifiers: any } },
{ connection: hostssl, method: radius, options: { radiusservers: any } },
{ connection: hostssl, method: radius, options: { radiussecrets: any } },
],
}`)

err := cc.Create(ctx, cluster, client.DryRunAll)
assert.Assert(t, apierrors.IsInvalid(err))

status := require.StatusError(t, err)
assert.Assert(t, status.Details != nil)
assert.Assert(t, cmp.Len(status.Details.Causes, 5))

for i, cause := range status.Details.Causes {
assert.Equal(t, cause.Field, fmt.Sprintf("spec.authentication.rules[%d]", i), "%#v", cause)
assert.Assert(t, cmp.Contains(cause.Message, `"radius" method requires`))
}

// These are valid.

cluster.Spec.Authentication = nil
require.UnmarshalInto(t, &cluster.Spec.Authentication, `{
rules: [
{ connection: hostssl, method: radius, options: { radiusservers: one, radiussecrets: two } },
{ connection: hostssl, method: radius, options: {
radiusservers: one, radiussecrets: two, radiusports: three,
} },
],
}`)
assert.NilError(t, cc.Create(ctx, cluster, client.DryRunAll))
})
})
}

func TestPostgresConfigParameters(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,19 @@ type PostgresHBARule struct {

// ---
// Emulate OpenAPI "anyOf" aka Kubernetes union.
// +kubebuilder:validation:XValidation:rule=`has(self.hba) ? !has(self.connection) && !has(self.databases) && !has(self.method) && !has(self.options) && !has(self.users) : true`,message=`"hba" cannot be combined with other fields`
// +kubebuilder:validation:XValidation:rule=`has(self.hba) ? true : has(self.connection) && has(self.method)`,message=`"connection" and "method" are required`
// +kubebuilder:validation:XValidation:rule=`[has(self.hba), has(self.connection) || has(self.databases) || has(self.method) || has(self.options) || has(self.users)].exists_one(b,b)`,message=`"hba" cannot be combined with other fields`
// +kubebuilder:validation:XValidation:rule=`has(self.hba) || (has(self.connection) && has(self.method))`,message=`"connection" and "method" are required`
//
// Some authentication methods *must* be further configured via options.
//
// https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_10_0;f=src/backend/libpq/hba.c#l1501
// https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_17_0;f=src/backend/libpq/hba.c#l1886
// +kubebuilder:validation:XValidation:rule=`has(self.hba) || self.method != "ldap" || (has(self.options) && ["ldapbasedn","ldapprefix","ldapsuffix"].exists(k, k in self.options))`,message=`the "ldap" method requires an "ldapbasedn", "ldapprefix", or "ldapsuffix" option`
// +kubebuilder:validation:XValidation:rule=`has(self.hba) || self.method != "ldap" || !has(self.options) || [["ldapprefix","ldapsuffix"], ["ldapbasedn","ldapbinddn","ldapbindpasswd","ldapsearchattribute","ldapsearchfilter"]].exists_one(a, a.exists(k, k in self.options))`,message=`cannot use "ldapbasedn", "ldapbinddn", "ldapbindpasswd", "ldapsearchattribute", or "ldapsearchfilter" options with "ldapprefix" or "ldapsuffix" options`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 I'm not liking these all-on-one-long-line markers these days.

//
// https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_10_0;f=src/backend/libpq/hba.c#l1539
// https://git.postgresql.org/gitweb/?p=postgresql.git;hb=refs/tags/REL_17_0;f=src/backend/libpq/hba.c#l1945
// +kubebuilder:validation:XValidation:rule=`has(self.hba) || self.method != "radius" || (has(self.options) && ["radiusservers","radiussecrets"].all(k, k in self.options))`,message=`the "radius" method requires "radiusservers" and "radiussecrets" options`
//
// +structType=atomic
type PostgresHBARuleSpec struct {
Expand Down