From 231244fde4ee1505a1c1f6be7d7c92d1d01a76f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20Andr=C3=A8=20Vikestrand=20Skogum?= Date: Tue, 15 Oct 2024 08:13:24 +0200 Subject: [PATCH 1/4] fix: Sorts group mappings in ldap.toml by org_role --- CHANGELOG.md | 2 ++ resources/config_ldap_group_mapping.rb | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18fd15cd..8e09b5db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This file is used to list changes made in each version of grafana. ## Unreleased +- Fix: Sorts group mappings in ldap.toml by org_role + ## 10.8.0 - *2024-09-27* - Added org_ids to ldap_mapping functions diff --git a/resources/config_ldap_group_mapping.rb b/resources/config_ldap_group_mapping.rb index b7b605dc..7eccb149 100644 --- a/resources/config_ldap_group_mapping.rb +++ b/resources/config_ldap_group_mapping.rb @@ -93,15 +93,23 @@ def remove_group_mapping next if nil_or_empty?(new_resource.send(rp)) [rp.to_s, new_resource.send(rp)] - end.compact.sort.to_h + end.compact.to_h + # Fjern eksisterende gruppemapping om den finnes remove_group_mapping if group_mapping_exist? + # Hent eksisterende group_mappings eller opprett en tom liste hvis ingen finnes ldap_server_config(new_resource.host)['group_mappings'] ||= [] + + # Legg til den nye gruppemappingen ldap_server_config(new_resource.host)['group_mappings'].push(mapping) + + # Sorter group_mappings etter org_role + ldap_server_config(new_resource.host)['group_mappings'].sort_by! { |gm| gm['org_role'] } end end + action :delete do converge_by("Remove LDAP server #{new_resource.host} group mapping for #{new_resource.group_dn} from OrgID #{new_resource.org_id}") { remove_group_mapping } if group_mapping_exist? end From ff8c6c8494e41203e968145d3eb74064c92d798e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20Andr=C3=A8=20Vikestrand=20Skogum?= Date: Tue, 15 Oct 2024 08:22:02 +0200 Subject: [PATCH 2/4] fix: blank line --- resources/config_ldap_group_mapping.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/resources/config_ldap_group_mapping.rb b/resources/config_ldap_group_mapping.rb index 7eccb149..0322e2fd 100644 --- a/resources/config_ldap_group_mapping.rb +++ b/resources/config_ldap_group_mapping.rb @@ -91,25 +91,16 @@ def remove_group_mapping converge_if_changed do mapping = resource_properties.map do |rp| next if nil_or_empty?(new_resource.send(rp)) - [rp.to_s, new_resource.send(rp)] end.compact.to_h - # Fjern eksisterende gruppemapping om den finnes remove_group_mapping if group_mapping_exist? - - # Hent eksisterende group_mappings eller opprett en tom liste hvis ingen finnes ldap_server_config(new_resource.host)['group_mappings'] ||= [] - - # Legg til den nye gruppemappingen ldap_server_config(new_resource.host)['group_mappings'].push(mapping) - - # Sorter group_mappings etter org_role ldap_server_config(new_resource.host)['group_mappings'].sort_by! { |gm| gm['org_role'] } end end - action :delete do converge_by("Remove LDAP server #{new_resource.host} group mapping for #{new_resource.group_dn} from OrgID #{new_resource.org_id}") { remove_group_mapping } if group_mapping_exist? end From e4723c8281c92597a72226940d75ff6edfe2c313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20Andr=C3=A8=20Vikestrand=20Skogum?= Date: Wed, 16 Oct 2024 12:39:06 +0200 Subject: [PATCH 3/4] fix: testing fix --- libraries/ldap_config_file.rb | 4 ++-- resources/config_ldap_group_mapping.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ldap_config_file.rb b/libraries/ldap_config_file.rb index 13f56cf7..c3dbb45f 100644 --- a/libraries/ldap_config_file.rb +++ b/libraries/ldap_config_file.rb @@ -77,12 +77,12 @@ def load_file_ldap_config_host_attributes(config_file, host) # @param group_dn [String] The group DN to return configuration for # @return [Hash] Host attribute configuration # - def load_file_ldap_config_host_group_mapping(config_file, host, group_dn) + def load_file_ldap_config_host_group_mapping(config_file, host, org_role) host_config = load_file_ldap_config_host(config_file, host) return if nil_or_empty?(host_config) - group_mapping = host_config.fetch('group_mappings', []).select { |gm| gm['group_dn'].eql?(group_dn) }.first + group_mapping = host_config.fetch('group_mappings', []).select { |gm| gm['org_role'].eql?(org_role) }.first Chef::Log.debug("load_file_ldap_config_host_group_mapping: #{config_file} host #{host} group #{group_dn} - [#{group_mapping.class}] #{group_mapping}") group_mapping diff --git a/resources/config_ldap_group_mapping.rb b/resources/config_ldap_group_mapping.rb index 0322e2fd..89abf533 100644 --- a/resources/config_ldap_group_mapping.rb +++ b/resources/config_ldap_group_mapping.rb @@ -39,7 +39,7 @@ property :org_id, Integer load_current_value do |new_resource| - current_config = load_file_ldap_config_host_group_mapping(new_resource.config_file, new_resource.host, new_resource.group_dn) + current_config = load_file_ldap_config_host_group_mapping(new_resource.config_file, new_resource.host, new_resource.org_role) current_value_does_not_exist! unless current_config From 035b252e981ad7eb763f6617c3783a700cc9599a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simen=20Andr=C3=A8=20Vikestrand=20Skogum?= Date: Wed, 16 Oct 2024 12:48:18 +0200 Subject: [PATCH 4/4] fix: testing fix --- libraries/ldap_config_file.rb | 6 +++--- resources/config_ldap_group_mapping.rb | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/ldap_config_file.rb b/libraries/ldap_config_file.rb index c3dbb45f..ac9b60b1 100644 --- a/libraries/ldap_config_file.rb +++ b/libraries/ldap_config_file.rb @@ -77,13 +77,13 @@ def load_file_ldap_config_host_attributes(config_file, host) # @param group_dn [String] The group DN to return configuration for # @return [Hash] Host attribute configuration # - def load_file_ldap_config_host_group_mapping(config_file, host, org_role) + def load_file_ldap_config_host_group_mapping(config_file, host, org_id) host_config = load_file_ldap_config_host(config_file, host) return if nil_or_empty?(host_config) - group_mapping = host_config.fetch('group_mappings', []).select { |gm| gm['org_role'].eql?(org_role) }.first - Chef::Log.debug("load_file_ldap_config_host_group_mapping: #{config_file} host #{host} group #{group_dn} - [#{group_mapping.class}] #{group_mapping}") + group_mapping = host_config.fetch('group_mappings', []).select { |gm| gm['org_id'].eql?(org_id) }.first + Chef::Log.debug("load_file_ldap_config_host_group_mapping: #{config_file} host #{host} group #{org_id} - [#{group_mapping.class}] #{group_mapping}") group_mapping end diff --git a/resources/config_ldap_group_mapping.rb b/resources/config_ldap_group_mapping.rb index 89abf533..930ca819 100644 --- a/resources/config_ldap_group_mapping.rb +++ b/resources/config_ldap_group_mapping.rb @@ -39,7 +39,7 @@ property :org_id, Integer load_current_value do |new_resource| - current_config = load_file_ldap_config_host_group_mapping(new_resource.config_file, new_resource.host, new_resource.org_role) + current_config = load_file_ldap_config_host_group_mapping(new_resource.config_file, new_resource.host, new_resource.group_dn) current_value_does_not_exist! unless current_config @@ -97,7 +97,7 @@ def remove_group_mapping remove_group_mapping if group_mapping_exist? ldap_server_config(new_resource.host)['group_mappings'] ||= [] ldap_server_config(new_resource.host)['group_mappings'].push(mapping) - ldap_server_config(new_resource.host)['group_mappings'].sort_by! { |gm| gm['org_role'] } + ldap_server_config(new_resource.host)['group_mappings'].sort_by! { |gm| gm['org_id'] } end end