1 | module Puppet::Parser::Functions
|
---|
2 |
|
---|
3 | newfunction(:base64, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
|
---|
4 |
|
---|
5 | Base64 encode or decode a string based on the command and the string submitted
|
---|
6 |
|
---|
7 | Usage:
|
---|
8 |
|
---|
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)
|
---|
16 |
|
---|
17 | ENDHEREDOC
|
---|
18 |
|
---|
19 | require 'base64'
|
---|
20 |
|
---|
21 | raise Puppet::ParseError, ("base64(): Wrong number of arguments (#{args.length}; must be >= 2)") unless args.length >= 2
|
---|
22 |
|
---|
23 | actions = ['encode','decode']
|
---|
24 |
|
---|
25 | unless actions.include?(args[0])
|
---|
26 | raise Puppet::ParseError, ("base64(): the first argument must be one of 'encode' or 'decode'")
|
---|
27 | end
|
---|
28 |
|
---|
29 | unless args[1].is_a?(String)
|
---|
30 | raise Puppet::ParseError, ("base64(): the second argument must be a string to base64")
|
---|
31 | end
|
---|
32 |
|
---|
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 |
|
---|
45 | case args[0]
|
---|
46 | when 'encode'
|
---|
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
|
---|
55 | when 'decode'
|
---|
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
|
---|
64 | end
|
---|
65 |
|
---|
66 | return result
|
---|
67 | end
|
---|
68 | end
|
---|