Skip to content
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
7 changes: 7 additions & 0 deletions build/rubygem-simp-cli.spec
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ EOM
options, e.g. `LDAP: {}`) instead of the deprecated `sssd::domains` Array,
and omits the key when there are no domains. Requires simp/sssd 11.0.0 or
later; the item class `SssdDomains` is now `SssdDomainsEntries`
- `simp config` validates LDAP DNs as whole DNs with case-insensitive
attribute types. `simp_options::ldap::base_dn` accepts any valid DN (e.g.
`DC=example,DC=com`, the form `simplib::ldap::domain_to_dn()` produces),
except that the SIMP LDAP server's base DN must still start with `dc=`.
The bind, sync and root DNs accept `CN=` as well as `cn=`. Malformed DNs,
such as an empty value, a `;` or missing comma between RDNs, or stray
whitespace or line breaks, are rejected

* Fri May 20 2022 Trevor Vaughan <tvaughan@onyxpoint.com> - 7.0.1
- Always set the hostname to the one specified during `simp config` in the apply
Expand Down
20 changes: 18 additions & 2 deletions lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../item'
require_relative '../../utils'
require_relative 'cli_is_simp_ldap_server'
require_relative 'cli_network_hostname'

Expand All @@ -27,12 +28,27 @@ def get_recommended_value
end
end

# simp_openldap creates the suffix entry as a `domain` object named by
# its first RDN, so when this host is the SIMP LDAP server, the base DN
# must start with `dc=`.
def validate(x)
(x.to_s =~ %r{^dc=}) ? true : false
return false unless Simp::Cli::Config::Utils.validate_ldap_dn(x)
return true unless simp_ldap_server?

x.match?(%r{\Adc=}i)
end

def not_valid_message
"Valid LDAP Base Distinguished Name must begin with 'dc='"
msg = "Invalid LDAP Base Distinguished Name. Use a DN such as 'dc=example,dc=com'"
msg += "; the SIMP LDAP server's base DN must start with 'dc='" if simp_ldap_server?
msg
end

private

def simp_ldap_server?
@config_items.key?('cli::is_simp_ldap_server') &&
@config_items.fetch('cli::is_simp_ldap_server').value
end
end
end
5 changes: 3 additions & 2 deletions lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../item'
require_relative '../../utils'
require_relative 'cli_is_simp_ldap_server'

module Simp; end
Expand All @@ -15,11 +16,11 @@ def initialize(puppet_env_info = DEFAULT_PUPPET_ENV_INFO)
end

def validate(x)
(x.to_s =~ %r{^cn=}) ? true : false
Simp::Cli::Config::Utils.validate_ldap_cn_dn(x)
end

def not_valid_message
"Valid LDAP Bind Distinguished Name must begin with 'cn='"
"Valid LDAP Bind Distinguished Name must be a DN that begins with a 'cn=' RDN"
end

def get_recommended_value
Expand Down
5 changes: 3 additions & 2 deletions lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../item'
require_relative '../../utils'
require_relative 'cli_is_simp_ldap_server'

module Simp; end
Expand All @@ -25,11 +26,11 @@ def get_recommended_value
end

def not_valid_message
"Valid LDAP Root Distinguished Name must begin with 'cn='"
"Valid LDAP Root Distinguished Name must be a DN that begins with a 'cn=' RDN"
end

def validate(x)
(x.to_s =~ %r{^cn=}) ? true : false
Simp::Cli::Config::Utils.validate_ldap_cn_dn(x)
end
end
end
5 changes: 3 additions & 2 deletions lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative '../item'
require_relative '../../utils'
require_relative 'cli_is_simp_ldap_server'

module Simp; end
Expand All @@ -15,11 +16,11 @@ def initialize(puppet_env_info = DEFAULT_PUPPET_ENV_INFO)
end

def validate(x)
(x.to_s =~ %r{^cn=}) ? true : false
Simp::Cli::Config::Utils.validate_ldap_cn_dn(x)
end

def not_valid_message
"Valid LDAP Sync Distinguished Name must begin with 'cn='"
"Valid LDAP Sync Distinguished Name must be a DN that begins with a 'cn=' RDN"
end

def get_recommended_value
Expand Down
38 changes: 38 additions & 0 deletions lib/simp/cli/config/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ class Simp::Cli::Config::Utils

SHA512_CRYPT_UNSUPPORTED_MSG = "This system's crypt(3) does not support SHA-512 password hashes"

# LDAP Distinguished Name grammar (RFC 4514), used by validate_ldap_dn.
#
# * Attribute types are names (case-insensitive) or numeric OIDs.
# * A value is non-empty. `"`, `+`, `,`, `;`, `<`, `=`, `>` and `\` must be
# escaped with a backslash (or written as `\XX` hex pairs), as must a
# leading `#` or space and a trailing space. `=` isn't special in RFC 4514,
# but an unescaped one almost always means a missing comma.
# * Control characters (including CR, LF and tab) are never allowed.
# * Multi-valued RDNs are joined with `+`; spaces around the `,` between
# RDNs are accepted.
LDAP_DN_ATTR = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)}.freeze
LDAP_DN_ESCAPE = %r{\\(?:[ "\#+,;<=>\\]|\h\h)}.freeze
LDAP_DN_PLAIN = %r{[^\p{Cntrl} "+,;<=>\\]}.freeze
LDAP_DN_VALUE = %r{(?:#{LDAP_DN_ESCAPE}|(?!\#)#{LDAP_DN_PLAIN})(?:(?:#{LDAP_DN_ESCAPE}|#{LDAP_DN_PLAIN}| )*(?:#{LDAP_DN_ESCAPE}|#{LDAP_DN_PLAIN}))?}.freeze
LDAP_DN_RDN = %r{#{LDAP_DN_ATTR}=#{LDAP_DN_VALUE}(?:\+#{LDAP_DN_ATTR}=#{LDAP_DN_VALUE})*}.freeze
LDAP_DN_REGEX = %r{\A#{LDAP_DN_RDN}(?: *, *#{LDAP_DN_RDN})*\z}.freeze

# A Hiera interpolation such as %{hiera('simp_options::ldap::base_dn')}
HIERA_INTERPOLATION_REGEX = %r{%\{[^{}\p{Cntrl}]+\}}.freeze

class << self
def validate_domain(domain)
# From Simplib::Domain custom type from pupmod-simp-simplib
Expand Down Expand Up @@ -41,6 +61,24 @@ def validate_fqdn(fqdn)
((fqdn =~ regex) ? true : false)
end

# Validate an LDAP Distinguished Name against LDAP_DN_REGEX.
def validate_ldap_dn(dn)
dn.is_a?(String) && LDAP_DN_REGEX.match?(dn)
end

# Validate an LDAP DN whose first RDN is `cn=...` (e.g. a bind DN).
#
# A whole RDN may be a Hiera interpolation, as in SIMP's recommended
# values (`cn=hostAuth,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}`).
# Each interpolation is replaced with a placeholder RDN before the DN
# is validated, so it can't hide a malformed DN around it.
def validate_ldap_cn_dn(dn)
return false unless dn.is_a?(String)

resolved = dn.gsub(HIERA_INTERPOLATION_REGEX, 'dc=interpolated')
resolved.match?(%r{\Acn=}i) && validate_ldap_dn(resolved)
end

def validate_ip(ip)
return false unless ip.respond_to?(:=~)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,32 @@
end

describe '#validate' do
context 'when this host is the SIMP LDAP server' do
before :each do
item = Simp::Cli::Config::Item::CliIsSimpLdapServer.new
item.value = true
@ci.config_items[item.key] = item
end

it 'requires a leading dc= RDN' do
expect(@ci.validate('DC=tasty,DC=bacon')).to be true
expect(@ci.validate('o=Bacon')).to be false
expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be false
expect(@ci.not_valid_message).to include("must start with 'dc='")
end
end

it 'validates ldap_base_dns' do
expect(@ci.validate('dc=tasty,dc=bacon')).to be true
expect(@ci.validate('DC=tasty,DC=bacon')).to be true
expect(@ci.validate('o=Bacon')).to be true
expect(@ci.validate('ou=Hosts,dc=tasty,dc=bacon')).to be true
end

it "doesn't validate bad ldap_base_dns" do
expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be false
expect(@ci.validate('dc=')).to be false
expect(@ci.validate("x\ndc=tasty,dc=bacon")).to be false
expect(@ci.validate('tasty.bacon')).to be false
expect(@ci.validate(nil)).to be false
expect(@ci.validate(false)).to be false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,19 @@
expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be true
end

it 'validates an uppercase CN' do
expect(@ci.validate('CN=hostAuth,OU=Hosts,DC=tasty,DC=bacon')).to be true
end

it 'validates the hiera-interpolated recommended value' do
expect(@ci.validate("cn=hostAuth,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}")).to be true
end

it "doesn't validate bad ldap_bind_dns" do
expect(@ci.validate('dc=tasty,dc=bacon')).to be false
expect(@ci.validate('cn=')).to be false
expect(@ci.validate('cn=a,garbage')).to be false
expect(@ci.validate("x\ncn=a,dc=tasty")).to be false
expect(@ci.validate(nil)).to be false
expect(@ci.validate(false)).to be false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,19 @@
expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be true
end

it 'validates an uppercase CN' do
expect(@ci.validate('CN=LDAPAdmin,OU=Hosts,DC=tasty,DC=bacon')).to be true
end

it 'validates the hiera-interpolated recommended value' do
expect(@ci.validate("cn=LDAPAdmin,ou=People,%{hiera('simp_options::ldap::base_dn')}")).to be true
end

it "doesn't validate bad ldap_root_dns" do
expect(@ci.validate('dc=tasty,dc=bacon')).to be false
expect(@ci.validate('cn=')).to be false
expect(@ci.validate('cn=a,garbage')).to be false
expect(@ci.validate("x\ncn=a,dc=tasty")).to be false
expect(@ci.validate(nil)).to be false
expect(@ci.validate(false)).to be false
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@
expect(@ci.validate('cn=LDAPSync,ou=Hosts,dc=tasty,dc=bacon')).to be true
end

it 'validates an uppercase CN' do
expect(@ci.validate('CN=LDAPSync,OU=Hosts,DC=tasty,DC=bacon')).to be true
end

it 'validates the hiera-interpolated recommended value' do
expect(@ci.validate("cn=LDAPSync,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}")).to be true
end

it "doesn't validate bad ldap_sync_dns" do
expect(@ci.validate('cn=')).to be false
expect(@ci.validate('cn=a,garbage')).to be false
expect(@ci.validate("x\ncn=a,dc=tasty")).to be false
expect(@ci.validate(nil)).to be false
expect(@ci.validate(false)).to be false
end
Expand Down
68 changes: 68 additions & 0 deletions spec/lib/simp/cli/config/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,74 @@
end
end

describe '.validate_ldap_dn' do
it 'validates DNs regardless of attribute case or type' do
[
'dc=example,dc=com',
'DC=example,DC=com',
'o=Example',
'ou=People, dc=example,dc=com',
'cn=Doe\\, John,ou=People,dc=example,dc=com',
'cn=a+uid=b,dc=example,dc=com',
'2.5.4.3=a,dc=example',
'cn=Some Name,dc=example',
'cn=\\#1,dc=example',
'cn=\\3Cx\\3E,dc=example',
].each do |dn|
expect(described_class.validate_ldap_dn(dn)).to be(true), dn
end
end

it "doesn't validate malformed DNs" do
[
'dc=',
'=example',
'dc=example,',
'dc=example,,dc=com',
'example.com',
"x\ndc=example",
"dc=example\n",
"dc=example\r",
'dc=example,dc=com ',
'dc= example',
'dc=example;dc=com',
'dc=example dc=com',
'dc=#zz,dc=com',
'cn=a<b,dc=example',
nil,
false,
].each do |dn|
expect(described_class.validate_ldap_dn(dn)).to be(false), dn.inspect
end
end
end

describe '.validate_ldap_cn_dn' do
it 'validates DNs that start with a cn= RDN' do
expect(described_class.validate_ldap_cn_dn('cn=hostAuth,ou=Hosts,dc=example,dc=com')).to be true
expect(described_class.validate_ldap_cn_dn('CN=hostAuth,OU=Hosts,DC=example,DC=com')).to be true
end

it 'validates a Hiera interpolation in an RDN position' do
expect(described_class.validate_ldap_cn_dn("cn=hostAuth,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}")).to be true
end

it "doesn't validate other or malformed DNs" do
[
'dc=example,dc=com',
'cn=a,garbage',
'cn=a,,',
'cn=a, ',
'cn= ,dc=example',
"cn=%{hiera('x')}",
"%{hiera('simp_options::ldap::base_dn')}",
nil,
].each do |dn|
expect(described_class.validate_ldap_cn_dn(dn)).to be(false), dn.inspect
end
end
end

describe '.validate_fqdn' do
it 'validates good FQDNs' do
expect(described_class.validate_fqdn('simp.dev')).to be true
Expand Down
Loading