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
|