Skip to content

Commit 2c3beac

Browse files
committed
Merge pull request #575 from guessi/extend_base64_function_support
Extend Base64() function support
2 parents e2206fd + dc64e72 commit 2c3beac

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

README.markdown

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,36 @@ Converts any object to an array containing that object. Empty argument lists are
146146

147147
#### `base64`
148148

149-
Converts a string to and from base64 encoding. Requires an action ('encode', 'decode') and either a plain or base64-encoded string. *Type*: rvalue.
149+
Converts a string to and from base64 encoding.
150+
Requires an `action` ('encode', 'decode') and either a plain or base64-encoded `string`,
151+
and an optional `method` ('default', 'strict', 'urlsafe')
152+
153+
for backward compatibility, `metohd` will be set as `default` if not specified.
154+
155+
*Examples:*
156+
~~~
157+
base64('encode', 'hello')
158+
base64('encode', 'hello', 'default')
159+
# return: "aGVsbG8=\n"
160+
161+
base64('encode', 'hello', 'strict')
162+
# return: "aGVsbG8="
163+
164+
base64('decode', 'aGVsbG8=')
165+
base64('decode', 'aGVsbG8=\n')
166+
base64('decode', 'aGVsbG8=', 'default')
167+
base64('decode', 'aGVsbG8=\n', 'default')
168+
base64('decode', 'aGVsbG8=', 'strict')
169+
# return: "hello"
170+
171+
base64('encode', 'https://puppetlabs.com', 'urlsafe')
172+
# return: "aHR0cHM6Ly9wdXBwZXRsYWJzLmNvbQ=="
173+
174+
base64('decode', 'aHR0cHM6Ly9wdXBwZXRsYWJzLmNvbQ==', 'urlsafe')
175+
# return: "https://puppetlabs.com"
176+
~~~
177+
178+
*Type*: rvalue.
150179

151180
#### `basename`
152181

lib/puppet/parser/functions/base64.rb

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ module Puppet::Parser::Functions
66
77
Usage:
88
9-
$encodestring = base64('encode','thestring')
10-
$decodestring = base64('decode','dGhlc3RyaW5n')
9+
$encodestring = base64('encode', 'thestring')
10+
$decodestring = base64('decode', 'dGhlc3RyaW5n')
11+
12+
# explicitly define encode/decode method: default, strict, urlsafe
13+
$method = 'default'
14+
$encodestring = base64('encode', 'thestring', $method)
15+
$decodestring = base64('decode', 'dGhlc3RyaW5n', $method)
1116
1217
ENDHEREDOC
1318

1419
require 'base64'
1520

16-
raise Puppet::ParseError, ("base64(): Wrong number of arguments (#{args.length}; must be = 2)") unless args.length == 2
21+
raise Puppet::ParseError, ("base64(): Wrong number of arguments (#{args.length}; must be >= 2)") unless args.length >= 2
1722

1823
actions = ['encode','decode']
1924

@@ -25,11 +30,37 @@ module Puppet::Parser::Functions
2530
raise Puppet::ParseError, ("base64(): the second argument must be a string to base64")
2631
end
2732

33+
method = ['default','strict','urlsafe']
34+
35+
if args.length <= 2
36+
chosenMethod = 'default'
37+
else
38+
chosenMethod = args[2]
39+
end
40+
41+
unless method.include?(chosenMethod)
42+
raise Puppet::ParseError, ("base64(): the third argument must be one of 'default', 'strict', or 'urlsafe'")
43+
end
44+
2845
case args[0]
2946
when 'encode'
30-
result = Base64.encode64(args[1])
47+
case chosenMethod
48+
when 'default'
49+
result = Base64.encode64(args[1])
50+
when 'strict'
51+
result = Base64.strict_encode64(args[1])
52+
when 'urlsafe'
53+
result = Base64.urlsafe_encode64(args[1])
54+
end
3155
when 'decode'
32-
result = Base64.decode64(args[1])
56+
case chosenMethod
57+
when 'default'
58+
result = Base64.decode64(args[1])
59+
when 'strict'
60+
result = Base64.strict_decode64(args[1])
61+
when 'urlsafe'
62+
result = Base64.urlsafe_decode64(args[1])
63+
end
3364
end
3465

3566
return result

spec/functions/base64_spec.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,34 @@
44
it { is_expected.not_to eq(nil) }
55
it { is_expected.to run.with_params().and_raise_error(Puppet::ParseError) }
66
it { is_expected.to run.with_params("one").and_raise_error(Puppet::ParseError) }
7-
it { is_expected.to run.with_params("one", "two", "three").and_raise_error(Puppet::ParseError) }
87
it { is_expected.to run.with_params("one", "two").and_raise_error(Puppet::ParseError, /first argument must be one of/) }
98
it { is_expected.to run.with_params("encode", ["two"]).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
109
it { is_expected.to run.with_params("encode", 2).and_raise_error(Puppet::ParseError, /second argument must be a string/) }
10+
it { is_expected.to run.with_params("encode", "thestring", "three").and_raise_error(Puppet::ParseError, /third argument must be one of/) }
11+
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n", "strict").and_raise_error(ArgumentError) }
1112

1213
it { is_expected.to run.with_params("encode", "thestring").and_return("dGhlc3RyaW5n\n") }
13-
it { is_expected.to run.with_params("encode", "a very long string that will cause the base64 encoder to produce output with multiple lines").and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") }
1414
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n").and_return("thestring") }
1515
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n").and_return("thestring") }
16+
17+
it { is_expected.to run.with_params("encode", "thestring", "default").and_return("dGhlc3RyaW5n\n") }
18+
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n", "default").and_return("thestring") }
19+
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n\n", "default").and_return("thestring") }
20+
21+
it { is_expected.to run.with_params("encode", "thestring", "strict").and_return("dGhlc3RyaW5n") }
22+
it { is_expected.to run.with_params("decode", "dGhlc3RyaW5n", "strict").and_return("thestring") }
23+
24+
it { is_expected.to run.with_params("encode", "a very long string that will cause the base64 encoder to produce output with multiple lines").and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n") }
25+
it { is_expected.to run.with_params("decode", "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0\nIGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5l\ncw==\n").and_return("a very long string that will cause the base64 encoder to produce output with multiple lines") }
26+
it { is_expected.to run.with_params("decode", "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==").and_return("a very long string that will cause the base64 encoder to produce output with multiple lines") }
27+
28+
it { is_expected.to run.with_params("encode", "a very long string that will cause the base64 encoder to produce output with multiple lines", "strict").and_return("YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==") }
29+
it { is_expected.to run.with_params("decode", "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==").and_return("a very long string that will cause the base64 encoder to produce output with multiple lines") }
30+
it { is_expected.to run.with_params("decode", "YSB2ZXJ5IGxvbmcgc3RyaW5nIHRoYXQgd2lsbCBjYXVzZSB0aGUgYmFzZTY0IGVuY29kZXIgdG8gcHJvZHVjZSBvdXRwdXQgd2l0aCBtdWx0aXBsZSBsaW5lcw==", "strict").and_return("a very long string that will cause the base64 encoder to produce output with multiple lines") }
31+
32+
it { is_expected.to run.with_params("encode", "https://www.google.com.tw/?gws_rd=ssl#q=hello+world", "urlsafe").and_return("aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk") }
33+
it { is_expected.to run.with_params("decode", "aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS50dy8_Z3dzX3JkPXNzbCNxPWhlbGxvK3dvcmxk", "urlsafe").and_return("https://www.google.com.tw/?gws_rd=ssl#q=hello+world") }
34+
35+
it { is_expected.to run.with_params("encode", "https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add", "urlsafe").and_return("aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=") }
36+
it { is_expected.to run.with_params("decode", "aHR0cHM6Ly9naXRodWIuY29tL3B1cHBldGxhYnMvcHVwcGV0bGFicy1zdGRsaWIvcHVsbHM_dXRmOD0lRTIlOUMlOTMmcT1pcyUzQXByK2lzJTNBb3BlbitBZGQ=", "urlsafe").and_return("https://github.com/puppetlabs/puppetlabs-stdlib/pulls?utf8=%E2%9C%93&q=is%3Apr+is%3Aopen+Add") }
1637
end

0 commit comments

Comments
 (0)