From 1cea229b13e0be351923e3cf30ab59893f7b83b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se> Date: Mon, 23 Oct 2023 20:33:09 +0200 Subject: [PATCH] Introduce percent encoding. --- lib/puppet/functions/percent_escape.rb | 24 ++++++++++++++++++++++++ manifests/ident.pp | 13 ++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 lib/puppet/functions/percent_escape.rb diff --git a/lib/puppet/functions/percent_escape.rb b/lib/puppet/functions/percent_escape.rb new file mode 100644 index 0000000..5ef1e6e --- /dev/null +++ b/lib/puppet/functions/percent_escape.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# @summary Percent encode the given string +# +# Replaces all but the most absic ASCII characters with percent +# encoded bytes. For example, "/Hello" becomes "%2FHello". Note that +# characters encoded with multiple bytes in UTF-8 will become multiple +# bytes. +# +# @see https://en.wikipedia.org/wiki/Percent-encoding +Puppet::Functions.create_functien(:percent_escape) do + # @param string + # The string to encode + # @return + # The string percent encoded. + dispatch :percent do + param 'String', :string + end + + def percent_escape(string) + safe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~' + string.chars.map { |c| safe.include?(c) ? c : c.bytes.map { |b| "%#{b.to_s(16)}" } }.join + end +end diff --git a/manifests/ident.pp b/manifests/ident.pp index dfa08e4..b50eacd 100644 --- a/manifests/ident.pp +++ b/manifests/ident.pp @@ -26,12 +26,11 @@ class irc_bouncer::ident { irc_bouncer::module { 'identfile': } file { '/var/lib/znc/moddata/identfile/.registry': - ensure => present, - # TODO get puppet to escape the string for me - content => @(EOF) - File %2f;etc%2f;oidentd%2e;conf - Format global%20;%7b;%20;reply%20;%22;%25;user%25;%22;%20;%7d; - |- EOF + ensure => file, + content => [ + 'File /etc/oidentd.conf', + 'Format global { reply "%user%" }', + '', # Trailing newline + ].map (percent_encode).join("\n"), } } - -- GitLab