Module: Bowtie::Middleware::PolicyCheck::Loader

Extended by:
Loader
Included in:
Loader
Defined in:
lib/bowtie/middleware/policy_check.rb

Constant Summary collapse

CONFIG_FILE_NAME =
'.bowtie.yml'
CONFIG_BLOCK_KEY =
'permits'
CONFIG_METHODS_KEY =
'method'
CONFIG_PLANS_KEY =
'plans'
CONFIG_PATH_KEY =
'path'
CONFIG_PROFILE_KEY =
'profile'
CONFIG_STATUS_KEY =
'status'

Instance Method Summary collapse

Instance Method Details

#default_permit_allObject



63
64
65
66
67
68
69
# File 'lib/bowtie/middleware/policy_check.rb', line 63

def default_permit_all
  Policy.new('',
             nil,
             nil,
             0,
             nil)
end

#methods_from_permitted_section_config(config) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/bowtie/middleware/policy_check.rb', line 121

def methods_from_permitted_section_config(config)
  method_config = config[CONFIG_METHODS_KEY]

  if method_config.nil? || method_config == '*'
    [nil]
  else
    if method_config.is_a? Array
      method_config.map(&:upcase)
    elsif method_config.is_a? String
      [method_config].map(&:upcase)
    else
      []
    end
  end
end

#plans_from_permitted_section_config(config) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/bowtie/middleware/policy_check.rb', line 137

def plans_from_permitted_section_config(config)
  plan_config = config[CONFIG_PLANS_KEY]

  if plan_config.nil? || plan_config == '*'
    [nil]
  else
    if plan_config.is_a? Array
      plan_config
    else
      [plan_config]
    end
  end
end

#policy_records(source) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/bowtie/middleware/policy_check.rb', line 53

def policy_records(source)
  records = Dir["#{source}/**/.bowtie.yml"].collect { |path|
    policy_records_for_path(path.gsub(source, ''), File.read(path))
  }.compact.flatten

  records << default_permit_all if records.length == 0

  records
end

#policy_records_for_path(path, content) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/bowtie/middleware/policy_check.rb', line 71

def policy_records_for_path(path, content)
  base_path = path.gsub(CONFIG_FILE_NAME, '')
  base_path = "/#{base_path}" unless base_path.start_with? '/'
  base_path = base_path[0..-2] if base_path.end_with? '/'

  environment_config = YAML.load(content) || {}

  permitted_section_configs = environment_config[CONFIG_BLOCK_KEY] || []
  permitted_section_configs = [permitted_section_configs] unless permitted_section_configs.is_a? Array

  policy_records_from_permitted_section_configs(base_path, permitted_section_configs)
end

#policy_records_from_permitted_section_config(base_path, config) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bowtie/middleware/policy_check.rb', line 94

def policy_records_from_permitted_section_config(base_path, config)
  path_extension = config[CONFIG_PATH_KEY]
  path_extension = path_extension[1..-1] if path_extension && path_extension.start_with?('/')

  policy_path = [base_path, path_extension].compact.join('/')

  methods = methods_from_permitted_section_config(config)
  plans   = plans_from_permitted_section_config(config)
  profile_restrictions = profile_restrictions_from_permitted_section_config(config)
  status = status_from_permitted_section_config(config)

  records = []

  methods.each do |method|
    plans.each do |plan|
      records << Policy.new(policy_path,
                            method,
                            plan,
                            policy_path.length,
                            profile_restrictions,
                            status)
    end
  end

  return records
end

#policy_records_from_permitted_section_configs(base_path, configs) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/bowtie/middleware/policy_check.rb', line 84

def policy_records_from_permitted_section_configs(base_path, configs)
  records = []

  configs.each do |config|
    records += policy_records_from_permitted_section_config(base_path, config)
  end

  return records
end

#profile_restrictions_from_permitted_section_config(config) ⇒ Object



151
152
153
# File 'lib/bowtie/middleware/policy_check.rb', line 151

def profile_restrictions_from_permitted_section_config(config)
  _profile_restrictions_config = config[CONFIG_PROFILE_KEY]
end

#status_from_permitted_section_config(config) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/bowtie/middleware/policy_check.rb', line 155

def status_from_permitted_section_config(config)
  status_config = config[CONFIG_STATUS_KEY]

  if status_config.nil? || status_config == '*'
    nil
  else
    status_config
  end
end