Class: Puppet::Network::AuthConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/network/auth_config_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ AuthConfigParser

Returns a new instance of AuthConfigParser.



10
11
12
# File 'lib/puppet/network/auth_config_parser.rb', line 10

def initialize(string)
  @string = string
end

Class Method Details

.new_from_file(file) ⇒ Object



6
7
8
# File 'lib/puppet/network/auth_config_parser.rb', line 6

def self.new_from_file(file)
  self.new(Puppet::FileSystem.read(file, :encoding => 'utf-8'))
end

Instance Method Details

#modify_right(right, method, value, msg, count) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puppet/network/auth_config_parser.rb', line 73

def modify_right(right, method, value, msg, count)
  value.split(/\s*,\s*/).each do |val|
    begin
      val.strip!
      right.info msg % val
      right.send(method, val)
    rescue Puppet::AuthStoreError => detail
      raise Puppet::ConfigurationError, _("%{detail} at line %{count} of %{file}") % { detail: detail, count: count, file: @file }, detail.backtrace
    end
  end
end

#parseObject



14
15
16
# File 'lib/puppet/network/auth_config_parser.rb', line 14

def parse
  Puppet::Network::AuthConfig.new(parse_rights)
end

#parse_right_directive(right, var, value, count) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet/network/auth_config_parser.rb', line 50

def parse_right_directive(right, var, value, count)
  value.strip!
  case var
  when "allow"
    modify_right(right, :allow, value, _("allowing %s access"), count)
  when "deny"
    modify_right(right, :deny, value, _("denying %s access"), count)
  when "allow_ip"
    modify_right(right, :allow_ip, value, _("allowing IP %s access"), count)
  when "deny_ip"
    modify_right(right, :deny_ip, value, _("denying IP %s access"), count)
  when "method"
    modify_right(right, :restrict_method, value, _("allowing 'method' %s"), count)
  when "environment"
    modify_right(right, :restrict_environment, value, _("adding environment %s"), count)
  when /auth(?:enticated)?/
    modify_right(right, :restrict_authenticated, value, _("adding authentication %s"), count)
  else
    raise Puppet::ConfigurationError,
      _("Invalid argument '%{var}' at line %{count}") % { var: var, count: count }
  end
end

#parse_rightsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet/network/auth_config_parser.rb', line 18

def parse_rights
  rights = Puppet::Network::Rights.new
  right = nil
  count = 1
  @string.each_line { |line|
    case line.chomp
    when /^\s*#/, /^\s*$/
      # skip comments and blank lines
    when /^path\s+((?:~\s+)?[^ ]+)\s*$/ # "path /path" or "path ~ regex"
      name = $1.chomp
      right = rights.newright(name, count, @file)
    when /^\s*(allow(?:_ip)?|deny(?:_ip)?|method|environment|auth(?:enticated)?)\s+(.+?)(\s*#.*)?$/
      if right.nil?
        #TRANSLATORS "path" is a configuration file entry and should not be translated
        raise Puppet::ConfigurationError, _("Missing or invalid 'path' before right directive at line %{count} of %{file}") % { count: count, file: @file }
      end
      parse_right_directive(right, $1, $2, count)
    else
      raise Puppet::ConfigurationError, _("Invalid line %{count}: %{line}") % { count: count, line: line }
    end
    count += 1
  }

  # Verify each of the rights are valid.
  # We let the check raise an error, so that it can raise an error
  # pointing to the specific problem.
  rights.each { |name, r|
    r.valid?
  }
  rights
end