From cabd312b7fc580186a7347b5f899c6520e6dbb84 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Mon, 15 Jul 2024 12:06:19 +0200 Subject: [PATCH] Refs #37604 - Normalize DNS forwarders to an array Previously the documentation showed an example that abused an internal implementation detail that's no longer allowed with the stricter input validation. --- .../240715095211-normalize-dns-forwarders.rb | 8 +++++ ...20240715095211_normalize_dns_forwarders.rb | 8 +++++ .../240715095211-normalize-dns-forwarders.rb | 8 +++++ ...715095211_normalize_dns_forwarders_spec.rb | 33 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 config/foreman-proxy-content.migrations/240715095211-normalize-dns-forwarders.rb create mode 100644 config/foreman.migrations/20240715095211_normalize_dns_forwarders.rb create mode 100644 config/katello.migrations/240715095211-normalize-dns-forwarders.rb create mode 100644 spec/migrations/20240715095211_normalize_dns_forwarders_spec.rb diff --git a/config/foreman-proxy-content.migrations/240715095211-normalize-dns-forwarders.rb b/config/foreman-proxy-content.migrations/240715095211-normalize-dns-forwarders.rb new file mode 100644 index 00000000..a014fa42 --- /dev/null +++ b/config/foreman-proxy-content.migrations/240715095211-normalize-dns-forwarders.rb @@ -0,0 +1,8 @@ +# forwarders was always an array, but previously it was documented as +# --foreman-proxy-dns-forwarders "192.0.2.1; 192.0.2.2" +fp_mod = answers['foreman_proxy'] +if fp_mod.is_a?(Hash) && fp_mod['forwarders'] + fp_mod['forwarders'] = fp_mod['forwarders'].flat_map do |forwarder| + forwarder.split(';').map(&:strip) + end +end diff --git a/config/foreman.migrations/20240715095211_normalize_dns_forwarders.rb b/config/foreman.migrations/20240715095211_normalize_dns_forwarders.rb new file mode 100644 index 00000000..a014fa42 --- /dev/null +++ b/config/foreman.migrations/20240715095211_normalize_dns_forwarders.rb @@ -0,0 +1,8 @@ +# forwarders was always an array, but previously it was documented as +# --foreman-proxy-dns-forwarders "192.0.2.1; 192.0.2.2" +fp_mod = answers['foreman_proxy'] +if fp_mod.is_a?(Hash) && fp_mod['forwarders'] + fp_mod['forwarders'] = fp_mod['forwarders'].flat_map do |forwarder| + forwarder.split(';').map(&:strip) + end +end diff --git a/config/katello.migrations/240715095211-normalize-dns-forwarders.rb b/config/katello.migrations/240715095211-normalize-dns-forwarders.rb new file mode 100644 index 00000000..a014fa42 --- /dev/null +++ b/config/katello.migrations/240715095211-normalize-dns-forwarders.rb @@ -0,0 +1,8 @@ +# forwarders was always an array, but previously it was documented as +# --foreman-proxy-dns-forwarders "192.0.2.1; 192.0.2.2" +fp_mod = answers['foreman_proxy'] +if fp_mod.is_a?(Hash) && fp_mod['forwarders'] + fp_mod['forwarders'] = fp_mod['forwarders'].flat_map do |forwarder| + forwarder.split(';').map(&:strip) + end +end diff --git a/spec/migrations/20240715095211_normalize_dns_forwarders_spec.rb b/spec/migrations/20240715095211_normalize_dns_forwarders_spec.rb new file mode 100644 index 00000000..53d268d6 --- /dev/null +++ b/spec/migrations/20240715095211_normalize_dns_forwarders_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +migration '20240715095211_normalize_dns_forwarders' do + scenarios %w[foreman katello foreman-proxy-content] do + context 'valid array' do + let(:answers) do + { + 'foreman_proxy' => { + 'forwarders' => ['192.0.2.1', '192.0.2.2'], + }, + } + end + + it 'leaves the answers untouched' do + expect(migrated_answers['foreman_proxy']['forwarders']).to eq(['192.0.2.1', '192.0.2.2']) + end + end + + context 'semicolon separated value' do + let(:answers) do + { + 'foreman_proxy' => { + 'forwarders' => ['192.0.2.1; 192.0.2.2'], + }, + } + end + + it 'leaves the answers untouched' do + expect(migrated_answers['foreman_proxy']['forwarders']).to eq(['192.0.2.1', '192.0.2.2']) + end + end + end +end