Class: Entitlement::Entitlement

Inherits:
Object
  • Object
show all
Defined in:
lib/entitlement/entitlement.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntitlement

Returns a new instance of Entitlement.



176
177
178
# File 'lib/entitlement/entitlement.rb', line 176

def initialize
  @entitlements          = []
end

Class Method Details

.decrypt(file_name) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/entitlement/entitlement.rb', line 90

def self.decrypt(file_name)
  content = File.read(file_name)
  if !content != ''
    Struct.new('Entitlements', :data, :iv, :key) unless defined?(Struct::Entitlements)
    entitlements = Marshal.load(content)
    public_key = OpenSSL::PKey::RSA.new(@public_key)
    cipher = OpenSSL::Cipher.new('aes-256-cbc')
    cipher.decrypt
    cipher.key = public_key.public_decrypt(entitlements.key)
    cipher.iv = public_key.public_decrypt(entitlements.iv)
    cipher.update(entitlements.data) + cipher.final
  else
    ''
  end
end

.define(&definition) ⇒ Object



77
78
79
80
81
# File 'lib/entitlement/entitlement.rb', line 77

def self.define(&definition)
  @instance ||= new
  @definition = definition
  read_entitlement_definition
end

.entitled?(full_action, resources = nil) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/entitlement/entitlement.rb', line 139

def self.entitled?(full_action, resources = nil)
  module_name, resource_type, action = full_action.split('#')
  return true if no_license_required?(full_action)
  load unless @instance
  @instance.instance_eval do
    Puppet.debug "Checking #{@name} entitlement for action #{action} in type #{resource_type} from module #{module_name}"
    if list_contains_valid_entitlement?(full_action, resources)
      Puppet.debug 'Entitlement ok'
      check_expiring_entitlements(full_action)
    else
      message = "#{action} in #{module_name}-#{resource_type} is not entitled on this node or domain at this point in time. Check entitlements list or purchase right entitlements."
      if action == 'index'
        Puppet.log_exception Puppet::DevError.new(message)
      else
        puts ''
        display_entitlements
        raise message
      end
    end
  end
end

.inspectObject



106
107
108
109
110
111
# File 'lib/entitlement/entitlement.rb', line 106

def self.inspect
  load unless @instance
  @instance.instance_eval do
    display_entitlements
  end
end

.loadObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/entitlement/entitlement.rb', line 46

def self.load
  paths = Puppet.settings[:basemodulepath].split(':')
  paths << Puppet.settings[:modulepath].split(':')
  paths << "#{Puppet.settings[:environmentpath]}/#{Puppet.settings[:environment]}/modules"
  paths << Puppet.settings[:confdir]
  paths << '/etc/puppetlabs/puppet'   # Always use a default path too
  Puppet.debug "Looking for entitlement files in #{paths.join(',')}"
  entitlement_files = paths.flatten.collect { |p| Dir.glob("#{p}/**/*.entitlements") }
  if entitlement_files.flatten.empty?
    # rubocop: disable Style/EvalWithLocation
    Puppet.debug 'No entitlement files found'
    instance_eval("::Entitlement::Entitlement.define { company 'Unknown'}")
  else
    entitlement_files.flatten.each do |file|
      Puppet.debug "found entitlements file #{file}"
      next unless File.exist?(file)
      read_filename = File.basename(file)
      # rubocop: disable Style/RescueStandardError
      content = "$entitlements_file = '#{file}'\n" + decrypt(file)

      begin
        instance_eval("::Entitlement::Entitlement.define {#{content} }")
      rescue => e
        raise "Error in license. Please contact Enterprise Modules.\n #{e}"
      end
      # rubocop: enable Style/EvalWithLocation
      # rubocop: enable Style/RescueStandardError
    end
  end
end

.no_license_required?(full_action) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
# File 'lib/entitlement/entitlement.rb', line 129

def self.no_license_required?( full_action)
  module_name, resource_type, action = full_action.split('#')
  return true if virtual?(module_name)
  #
  # We want all in easy_type to be free except the validation type
  #
  return false if module_name == 'easy_type' && resource_type == 'validation'
  return true if FREE_MODULES.include?(module_name)
end

.purgeableObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/entitlement/entitlement.rb', line 113

def self.purgeable
  load unless @instance
  @instance.instance_eval do
    files_to_purge = []
    files_to_keep = []
    @entitlements.each do |e|
      if e.purgable?
        files_to_purge << e.file
      else
        files_to_keep << e.file
      end
    end
    (files_to_purge.flatten - files_to_keep.flatten)
  end
end

.read_entitlement_definitionObject



83
84
85
86
87
88
# File 'lib/entitlement/entitlement.rb', line 83

def self.read_entitlement_definition
  @instance.instance_eval(&@definition)
  @instance.instance_eval do
    raise 'Entitlements file must contain a company name' unless @name
  end
end

.virtual?(module_name) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/entitlement/entitlement.rb', line 161

def self.virtual?(module_name)
  if ENV['NO_VIRTUAL_ENTITLEMENT']
    false
  elsif Facter.value('is_virtual') && (Facter.value('virtual') == 'virtualbox' || (dmi = Facter.value('dmi')) && dmi.dig('product','name') == 'VirtualBox')
    @entitlement_displayed ||= {}
    unless @entitlement_displayed[module_name]
      @entitlement_displayed[module_name] = true
      Puppet.notice "Running on VM. You are free to use #{module_name} on a WorkStation VM. Check the license requirements on http://www.enterprisemodules.com/ for details."
    end
    true
  else
    false
  end
end

Instance Method Details

#check_expiring_entitlements(module_name) ⇒ Object



248
249
250
# File 'lib/entitlement/entitlement.rb', line 248

def check_expiring_entitlements(module_name)
  @entitlements.each { |e| e.notify_if_expiring(module_name) }
end

#company(name) ⇒ Object



180
181
182
# File 'lib/entitlement/entitlement.rb', line 180

def company(name)
  @name = name
end

#display_entitlementsObject



237
238
239
240
241
242
# File 'lib/entitlement/entitlement.rb', line 237

def display_entitlements
  @entitlements.each do |e|
    e.inspect
    puts ''
  end
end

#entitlement(entitlement, &validator) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/entitlement/entitlement.rb', line 188

def entitlement(entitlement, &validator)
  verify_options(entitlement)
  puppet_server   = entitlement[:puppet_server]
  node_group      = entitlement[:node_group]
  number          = entitlement[:number]
  warning_number  = entitlement[:warning_number]
  count           = entitlement[:count]
  modules         = entitlement[:modules]
  from            = entitlement[:from]
  to              = entitlement[:to]
  nodes           = entitlement[:nodes]
  domains         = entitlement[:domains]
  opensource      = entitlement[:opensource]
  license_id      = entitlement[:license_id] || 'none'
  usage_only      = entitlement[:usage_only]
  pay_per_use     = entitlement[:pay_per_use]
  public_key      = entitlement[:public_key]
  api_url         = entitlement[:api_url]
  filename_string = entitlement[:filename_string]
  entry = if count
            SizedEntry.new(modules, from, to, filename_string, count, &validator)
          elsif node_group
            NodeGroupEntry.new(modules, from, to, node_group, filename_string, number, puppet_server, license_id, warning_number, &validator)
          elsif number
            NumberedEntry.new(modules, from, to, filename_string, number, puppet_server, license_id, warning_number, &validator)
          elsif nodes
            NodesEntry.new(modules, from, to, filename_string, nodes, &validator)
          elsif domains
            DomainsEntry.new(modules, from, to, filename_string, domains, &validator)
          elsif opensource
            OpenSourceEntry.new(modules, &validator)
          elsif usage_only
            UsageOnlyEntry.new(modules, &validator)
          elsif pay_per_use
            PayPerUse.new(modules, public_key, api_url, @name, node_name, &validator)
          elsif validator
            CustomEntry.new(modules, from, to, filename_string, &validator)
          else
            raise 'invalid entitlement line in file'
          end
  @entitlements << entry
end

#list_contains_valid_entitlement?(module_name, resources) ⇒ Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/entitlement/entitlement.rb', line 244

def list_contains_valid_entitlement?(module_name, resources)
  @entitlements.any? { |e| e.valid?(module_name, resources) }
end

#node_nameObject



184
185
186
# File 'lib/entitlement/entitlement.rb', line 184

def node_name
  Facter.value('fqdn').gsub(/\..*/, '')
end

#verify_options(entitlement) ⇒ Object



231
232
233
234
235
# File 'lib/entitlement/entitlement.rb', line 231

def verify_options(entitlement)
  entitlement.each_key do |o|
    raise "invalid entitlement option '#{o}'" unless VALID_OPTIONS.include?(o)
  end
end