From b14535811fb05f526b878e3655c07ed463600f7a Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Thu, 31 Dec 2020 13:14:59 +0100 Subject: [PATCH 1/3] Fix deprecation warning for URI.escape Ruby 2.7 deprecated URI.escape. --- lib/puppet/functions/foreman/foreman.rb | 3 ++- lib/puppet/functions/foreman/smartvar.rb | 3 ++- lib/puppet/parser/functions/foreman.rb | 3 ++- lib/puppet/parser/functions/smartvar.rb | 3 ++- lib/puppet/provider/foreman_resource/rest_v3.rb | 3 ++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/puppet/functions/foreman/foreman.rb b/lib/puppet/functions/foreman/foreman.rb index 2b8e57df9..d42880df4 100644 --- a/lib/puppet/functions/foreman/foreman.rb +++ b/lib/puppet/functions/foreman/foreman.rb @@ -37,6 +37,7 @@ # # Happy Foreman API-ing! +require "cgi" require "yaml" require "net/http" require "net/https" @@ -61,7 +62,7 @@ def foreman(item, search, per_page = "20", foreman_url = "https://localhost", fo raise Puppet::ParseError, "Foreman: Invalid filter_result: #{filter_result}, must not be boolean true" if filter_result == true begin - path = URI.escape("/api/#{item}?search=#{search}&per_page=#{per_page}") + path = "/api/#{CGI.escape(item)}?search=#{CGI.escape(search)}&per_page=#{CGI.escape(per_page)}" req = Net::HTTP::Get.new(path) req['Content-Type'] = 'application/json' diff --git a/lib/puppet/functions/foreman/smartvar.rb b/lib/puppet/functions/foreman/smartvar.rb index f83b8179c..684e4bb9d 100644 --- a/lib/puppet/functions/foreman/smartvar.rb +++ b/lib/puppet/functions/foreman/smartvar.rb @@ -2,6 +2,7 @@ # Foreman holds all the value names and their possible values, # this function simply ask foreman for the right value for this host. +require "cgi" require "net/http" require "net/https" require "uri" @@ -23,7 +24,7 @@ def smartvar(var, foreman_url = "http://foreman", foreman_user = "admin", forema http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' - path = URI.escape("/hosts/#{fqdn}/lookup_keys/#{var}") + path = "/hosts/#{CGI.escape(fqdn)}/lookup_keys/#{CGI.escape(var)}" req = Net::HTTP::Get.new(path) req.basic_auth(foreman_user, foreman_pass) req['Content-Type'] = 'application/json' diff --git a/lib/puppet/parser/functions/foreman.rb b/lib/puppet/parser/functions/foreman.rb index f1b217d18..ae10468f0 100644 --- a/lib/puppet/parser/functions/foreman.rb +++ b/lib/puppet/parser/functions/foreman.rb @@ -38,6 +38,7 @@ # # Happy Foreman API-ing! +require "cgi" require "yaml" require "net/http" require "net/https" @@ -65,7 +66,7 @@ module Puppet::Parser::Functions raise Puppet::ParseError, "Foreman: Invalid filter_result: #{filter_result}, must be a String or an Array" unless filter_result.is_a? String or filter_result.is_a? Array or filter_result.is_a? Hash or filter_result == false begin - path = URI.escape("/api/#{item}?search=#{search}&per_page=#{per_page}") + path = "/api/#{CGI.escape(item)}?search=#{CGI.escape(search)}&per_page=#{CGI.escape(per_page)}" req = Net::HTTP::Get.new(path) req['Content-Type'] = 'application/json' diff --git a/lib/puppet/parser/functions/smartvar.rb b/lib/puppet/parser/functions/smartvar.rb index 488100140..07915dde9 100644 --- a/lib/puppet/parser/functions/smartvar.rb +++ b/lib/puppet/parser/functions/smartvar.rb @@ -3,6 +3,7 @@ # this function simply ask foreman for the right value for this host. +require "cgi" require "net/http" require "net/https" require "uri" @@ -24,7 +25,7 @@ module Puppet::Parser::Functions http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' - path = URI.escape("/hosts/#{fqdn}/lookup_keys/#{var}") + path = "/hosts/#{CGI.escape(fqdn)}/lookup_keys/#{CGI.escape(var)}" req = Net::HTTP::Get.new(path) req.basic_auth(foreman_user, foreman_pass) req['Content-Type'] = 'application/json' diff --git a/lib/puppet/provider/foreman_resource/rest_v3.rb b/lib/puppet/provider/foreman_resource/rest_v3.rb index cf6b85de2..f920c263f 100644 --- a/lib/puppet/provider/foreman_resource/rest_v3.rb +++ b/lib/puppet/provider/foreman_resource/rest_v3.rb @@ -3,6 +3,7 @@ # This provider uses Net::HTTP from Ruby stdlib, JSON (stdlib on 1.9+ or the # gem on 1.8) and the oauth gem for auth, so requiring minimal dependencies. +require 'cgi' require 'uri' Puppet::Type.type(:foreman_resource).provide(:rest_v3) do @@ -59,7 +60,7 @@ def request(method, path, params = {}, data = nil, headers = {}) base_url += '/' unless base_url.end_with?('/') uri = URI.join(base_url, path) - uri.query = params.map { |p,v| "#{URI.escape(p.to_s)}=#{URI.escape(v.to_s)}" }.join('&') unless params.empty? + uri.query = params.map { |p,v| "#{CGI.escape(p.to_s)}=#{CGI.escape(v.to_s)}" }.join('&') unless params.empty? headers = { 'Accept' => 'application/json', From d70ceadb5fe9982fd74d2f8d869c92bbd8d49645 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Thu, 31 Dec 2020 13:19:54 +0100 Subject: [PATCH 2/3] Correct indenting --- lib/puppet/functions/foreman/smartvar.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/puppet/functions/foreman/smartvar.rb b/lib/puppet/functions/foreman/smartvar.rb index 684e4bb9d..24722cc6f 100644 --- a/lib/puppet/functions/foreman/smartvar.rb +++ b/lib/puppet/functions/foreman/smartvar.rb @@ -16,7 +16,7 @@ optional_param 'String', :foreman_pass end - def smartvar(var, foreman_url = "http://foreman", foreman_user = "admin", foreman_pass = "changeme") + def smartvar(var, foreman_url = "http://foreman", foreman_user = "admin", foreman_pass = "changeme") scope = closure_scope fqdn = scope['facts']['fqdn'] From 7d808c962b834ff3a096b9cc79a28c418b932da8 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Thu, 31 Dec 2020 13:20:25 +0100 Subject: [PATCH 3/3] Remove mention of Ruby 1.8 in a comment Ruby 1.8 is long dead and nobody needs to be reminded of those days. --- lib/puppet/provider/foreman_resource/rest_v3.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/puppet/provider/foreman_resource/rest_v3.rb b/lib/puppet/provider/foreman_resource/rest_v3.rb index f920c263f..fb03bf698 100644 --- a/lib/puppet/provider/foreman_resource/rest_v3.rb +++ b/lib/puppet/provider/foreman_resource/rest_v3.rb @@ -1,7 +1,7 @@ # Base provider for other Puppet types managing Foreman resources # -# This provider uses Net::HTTP from Ruby stdlib, JSON (stdlib on 1.9+ or the -# gem on 1.8) and the oauth gem for auth, so requiring minimal dependencies. +# This provider uses Net::HTTP from Ruby stdlib, JSON and the oauth gem for +# auth, so requiring minimal dependencies. require 'cgi' require 'uri'