2929# See COPYRIGHT and LICENSE files for more details.
3030module EnvData
3131 class LdapSeeder < Seeder
32+ KNOWN_CONNECTION_KEYS = %w[
33+ host port security tls_verify tls_certificate sync_users
34+ filter basedn binduser bindpassword
35+ login_mapping firstname_mapping lastname_mapping mail_mapping admin_mapping
36+ groupfilter
37+ ] . freeze
38+
39+ KNOWN_FILTER_KEYS = %w[ base filter sync_users group_attribute ] . freeze
40+
3241 def seed_data!
3342 print_status " ↳ Creating LDAP connection" do
3443 Setting . seed_ldap . each do |name , options |
44+ validate_options! ( name , options )
45+
3546 ldap = LdapAuthSource . find_or_initialize_by ( name :)
3647
3748 print_ldap_status ( ldap )
@@ -51,6 +62,34 @@ def applicable?
5162
5263 private
5364
65+ def validate_options! ( name , options )
66+ check_unknown_keys! ( options , KNOWN_CONNECTION_KEYS ,
67+ scope : "LDAP connection '#{ name } '" )
68+
69+ filters = options [ "groupfilter" ]
70+ return if filters . blank?
71+
72+ filters . each do |filter_name , filter_options |
73+ check_unknown_keys! ( filter_options , KNOWN_FILTER_KEYS ,
74+ scope : "LDAP group filter '#{ filter_name } ' (connection '#{ name } ')" )
75+ end
76+ end
77+
78+ def check_unknown_keys! ( options , known_keys , scope :)
79+ unknown = options . keys - known_keys
80+ return if unknown . empty?
81+
82+ raise <<~MSG . strip
83+ #{ scope } : unknown configuration key(s): #{ unknown . map { |k | env_form ( k ) } . join ( ', ' ) } .
84+ Accepted keys: #{ known_keys . map { |k | env_form ( k ) } . join ( ', ' ) } .
85+ Note: in environment variable names, single underscores split path segments and double underscores encode a literal underscore (e.g. LOGIN__MAPPING, not LOGIN_MAPPING).
86+ MSG
87+ end
88+
89+ def env_form ( key )
90+ key . gsub ( "_" , "__" ) . upcase
91+ end
92+
5493 # rubocop:disable Metrics/AbcSize
5594 def upsert_settings ( ldap , options )
5695 ldap . host = options [ "host" ]
0 commit comments