Module: Spectre::Http::Keystone

Defined in:
lib/spectre/http/keystone.rb

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Class Method Details

.authenticate(keystone_url, username, password, project, domain, cert) ⇒ Object



45
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/spectre/http/keystone.rb', line 45

def self.authenticate keystone_url, username, password, project, domain, cert
  auth_data = {
    auth: {
      identity: {
        methods: ['password'],
        password: {
          user: {
            name: username,
            password: password,
            domain: {
              name: domain,
            },
          },
        },
      },
      scope: {
        project: {
          name: project,
          domain: {
            name: domain,
          },
        },
      },
    },
  }

  keystone_url = keystone_url + '/' unless keystone_url.end_with? '/'

  base_uri = URI(keystone_url)
  uri = URI.join(base_uri, 'auth/tokens?nocatalog=true')

  http = Net::HTTP.new(base_uri.host, base_uri.port)

  if cert
    http.use_ssl = true
    http.ca_file = cert
  end

  req = Net::HTTP::Post.new(uri)
  req.body = JSON.pretty_generate(auth_data)
  req.content_type = 'application/json'

  res = http.request(req)

  raise "error while authenticating: #{res.code} #{res.message}\n#{res.body}" if res.code != '201'

  [
    res['X-Subject-Token'],
    JSON.parse(res.body),
  ]
end

.on_req(_http, net_req, req) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spectre/http/keystone.rb', line 22

def self.on_req _http, net_req, req
  return unless req.key? 'keystone' and req['auth'] == 'keystone'

  keystone_cfg = req['keystone']

  if @@cache.key? keystone_cfg
    token = @@cache[keystone_cfg]
  else
    token, _ = authenticate(
      keystone_cfg['url'],
      keystone_cfg['username'],
      keystone_cfg['password'],
      keystone_cfg['project'],
      keystone_cfg['domain'],
      keystone_cfg['cert'],
    )

    @@cache[keystone_cfg] = token
  end

  net_req['X-Auth-Token'] = token
end