diff options
Diffstat (limited to 'lib/puppet/parser')
-rw-r--r-- | lib/puppet/parser/functions.rb | 20 | ||||
-rw-r--r-- | lib/puppet/parser/scope.rb | 41 |
2 files changed, 40 insertions, 21 deletions
diff --git a/lib/puppet/parser/functions.rb b/lib/puppet/parser/functions.rb index cec7c02c5..38ba0da9f 100644 --- a/lib/puppet/parser/functions.rb +++ b/lib/puppet/parser/functions.rb @@ -125,22 +125,16 @@ module Functions require 'erb' vals.collect do |file| - unless file =~ /^#{File::SEPARATOR}/ - file = File.join(Puppet[:templatedir], file) - end - - unless File.exists?(file) - raise Puppet::ParseError, - "Could not find template %s" % file - end - - template = ERB.new(File.read(file)) + # Use a wrapper, so the template can't get access to the full + # Scope object. + wrapper = Puppet::Parser::Scope::TemplateWrapper.new(self, file) begin - template.result(binding) + wrapper.result() rescue => detail - raise Puppet::ParseError, "Could not interpret template %s: %s" % - [file, detail] + raise Puppet::ParseError, + "Failed to parse template %s: %s" % + [file, detail] end end.join("") end diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb index 9165cdb7e..307545143 100644 --- a/lib/puppet/parser/scope.rb +++ b/lib/puppet/parser/scope.rb @@ -10,6 +10,39 @@ module Puppet::Parser attr_accessor :file, :line, :type, :name end + # A simple wrapper for templates, so they don't have full access to + # the scope objects. + class TemplateWrapper + attr_accessor :scope, :file + + def initialize(scope, file) + @scope = scope + if file =~ /^#{File::SEPARATOR}/ + @file = file + else + @file = File.join(Puppet[:templatedir], file) + end + + unless FileTest.exists?(@file) + raise Puppet::ParseError + "Could not find template %s" % file + end + end + + def method_missing(name, *args) + if value = @scope.lookupvar(name.to_s) and value != :undefined and value != "" + return value + else + super + end + end + + def result + template = ERB.new(File.read(@file)) + template.result(binding) + end + end + # This doesn't actually work right now. Puppet.config.setdefaults(:puppet, :lexical => [false, "Whether to use lexical scoping (vs. dynamic)."], @@ -43,14 +76,6 @@ module Puppet::Parser @@declarative = val end - def method_missing(name, *args) - if value = lookupvar(name.to_s) and value != :undefined and value != "" - return value - else - super - end - end - # Add all of the defaults for a given object to that object. def adddefaults(obj) defaults = lookupdefaults(obj.type) |