Class: Hiera::Mwcache

Inherits:
Filecache
  • Object
show all
Defined in:
puppet/modules/wmflib/lib/hiera/mwcache.rb

Instance Method Summary collapse

Constructor Details

#initializeMwcache

Returns a new instance of Mwcache



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'puppet/modules/wmflib/lib/hiera/mwcache.rb', line 6

def initialize
  super
  require 'httpclient'
  require 'yaml'
  require 'json'
  config = Config[:mwyaml]
  @httphost = config[:host] || 'https://wikitech.wikimedia.org'
  @endpoint = config[:endpoint] || '/w/api.php'
  @http = HTTPClient.new(:agent_name => 'HieraMwCache/0.1')

  # Use the operating system's certificate store, not ruby-httpclient's cacert.p7s which doesn't
  # even have the root used to sign Let's Encrypt CAs (DST Root CA X3)
  @http.ssl_config.clear_cert_store
  @http.ssl_config.set_default_paths

  @stat_ttl = config[:cache_ttl] || 60
  if defined? @http.ssl_config.ssl_version
    @http.ssl_config.ssl_version = 'TLSv1'
  else
    # Note: this seem to work in later versions of the library,
    # but has no effect. How cute, I <3 ruby.
    @http.ssl_config.options = OpenSSL::SSL::OP_NO_SSLv3
  end
end

Instance Method Details

#read(path, expected_type, default = nil, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'puppet/modules/wmflib/lib/hiera/mwcache.rb', line 31

def read(path, expected_type, default = nil, &block)
  read_file(path, expected_type, &block)
rescue Hiera::MediawikiPageNotFoundError => detail
  # Any errors other than this will cause hiera to raise an error and puppet to fail.
  Hiera.debug("Page #{detail} is non-existent, setting defaults #{default}")
  @cache[path][:data] = default
rescue => detail
  # When failing to read data, we raise an exception, see https://phabricator.wikimedia.org/T78408
  error = "Reading data from #{path} failed: #{detail.class}: #{detail}"
  raise error
end

#read_file(path, expected_type = Object, &block) ⇒ Object

rubocop: disable Lint/UnusedMethodArgument



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'puppet/modules/wmflib/lib/hiera/mwcache.rb', line 44

def read_file(path, expected_type = Object, &block)
  if stale?(path)
    resp = get_from_mediawiki(path, true)
    data = resp["*"]
    @cache[path][:data] = block_given? ? yield(data) : data

    if !@cache[path][:data].nil? && !@cache[path][:data].is_a?(expected_type)
      raise TypeError, "Data retrieved from #{path} is #{@cache[path][:data].class}, not #{expected_type} or nil"
    end
  end

  @cache[path][:data]
end