Module: Fog::OpenStack
- Extended by:
- Provider
- Defined in:
- lib/fog/openstack/core.rb
Defined Under Namespace
Modules: Errors
Class Method Summary collapse
- .authenticate(options, connection_options = {}) ⇒ Object
-
.authenticate_v1(options, connection_options = {}) ⇒ Object
legacy v1.0 style auth.
-
.authenticate_v2(options, connection_options = {}) ⇒ Object
Keystone Style Auth.
-
.authenticate_v3(options, connection_options = {}) ⇒ Object
Keystone Style Auth.
-
.escape(str, extra_exclude_chars = '') ⇒ Object
CGI.escape, but without special treatment on spaces.
- .get_service(body, service_type = [], service_name = nil) ⇒ Object
- .get_supported_version(supported_versions, uri, auth_token, connection_options = {}) ⇒ Object
- .map_endpoint_type(type) ⇒ Object
- .retrieve_tokens_v2(options, connection_options = {}) ⇒ Object
- .retrieve_tokens_v3(options, connection_options = {}) ⇒ Object
Class Method Details
.authenticate(options, connection_options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/fog/openstack/core.rb', line 56 def self.authenticate(, = {}) case [:openstack_auth_uri].path when /v1(\.\d+)?/ authenticate_v1(, ) when /v2(\.\d+)?/ authenticate_v2(, ) when /v3(\.\d+)?/ authenticate_v3(, ) else authenticate_v2(, ) end end |
.authenticate_v1(options, connection_options = {}) ⇒ Object
legacy v1.0 style auth
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fog/openstack/core.rb', line 70 def self.authenticate_v1(, = {}) uri = [:openstack_auth_uri] connection = Fog::Core::Connection.new(uri.to_s, false, ) @openstack_api_key = [:openstack_api_key] @openstack_username = [:openstack_username] response = connection.request({ :expects => [200, 204], :headers => { 'X-Auth-Key' => @openstack_api_key, 'X-Auth-User' => @openstack_username }, :method => 'GET', :path => (uri.path and not uri.path.empty?) ? uri.path : 'v1.0' }) return { :token => response.headers['X-Auth-Token'], :server_management_url => response.headers['X-Server-Management-Url'] || response.headers['X-Storage-Url'], :identity_public_endpoint => response.headers['X-Keystone'] } end |
.authenticate_v2(options, connection_options = {}) ⇒ Object
Keystone Style Auth
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/fog/openstack/core.rb', line 94 def self.authenticate_v2(, = {}) uri = [:openstack_auth_uri] tenant_name = [:openstack_tenant] service_type = [:openstack_service_type] service_name = [:openstack_service_name] identity_service_type = [:openstack_identity_service_type] endpoint_type = ([:openstack_endpoint_type] || 'publicURL').to_s openstack_region = [:openstack_region] body = retrieve_tokens_v2(, ) service = get_service(body, service_type, service_name) [:unscoped_token] = body['access']['token']['id'] unless service unless tenant_name response = Fog::Core::Connection.new( "#{uri.scheme}://#{uri.host}:#{uri.port}/v2.0/tenants", false, ).request({ :expects => [200, 204], :headers => {'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Auth-Token' => body['access']['token']['id']}, :method => 'GET' }) body = Fog::JSON.decode(response.body) if body['tenants'].empty? raise Fog::Errors::NotFound.new('No Tenant Found') else [:openstack_tenant] = body['tenants'].first['name'] end end body = retrieve_tokens_v2(, ) service = get_service(body, service_type, service_name) end service['endpoints'] = service['endpoints'].select do |endpoint| endpoint['region'] == openstack_region end if openstack_region if service['endpoints'].empty? raise Fog::Errors::NotFound.new("No endpoints available for region '#{openstack_region}'") end if openstack_region unless service available = body['access']['serviceCatalog'].map { |endpoint| endpoint['type'] }.sort.join ', ' missing = service_type.join ', ' = "Could not find service #{missing}. Have #{available}" raise Fog::Errors::NotFound, end if service['endpoints'].count > 1 regions = service["endpoints"].map{ |e| e['region'] }.uniq.join(',') raise Fog::Errors::NotFound.new("Multiple regions available choose one of these '#{regions}'") end identity_service = get_service(body, identity_service_type) if identity_service_type tenant = body['access']['token']['tenant'] user = body['access']['user'] management_url = service['endpoints'].find{|s| s[endpoint_type]}[endpoint_type] identity_url = identity_service['endpoints'].find{|s| s['publicURL']}['publicURL'] if identity_service { :user => user, :tenant => tenant, :identity_public_endpoint => identity_url, :server_management_url => management_url, :token => body['access']['token']['id'], :expires => body['access']['token']['expires'], :current_user_id => body['access']['user']['id'], :unscoped_token => [:unscoped_token] } end |
.authenticate_v3(options, connection_options = {}) ⇒ Object
Keystone Style Auth
177 178 179 180 181 182 183 184 185 186 187 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/fog/openstack/core.rb', line 177 def self.authenticate_v3(, = {}) uri = [:openstack_auth_uri] tenant_name = [:openstack_tenant] project_name = [:openstack_project_name] service_type = [:openstack_service_type] service_name = [:openstack_service_name] identity_service_type = [:openstack_identity_service_type] endpoint_type = map_endpoint_type([:openstack_endpoint_type] || 'publicURL') openstack_region = [:openstack_region] body, token_headers = retrieve_tokens_v3(, ) service = get_service(body, service_type, service_name) [:unscoped_token] = token_headers['X-Subject-Token'] unless service unless project_name request_body = { :expects => [200], :headers => {'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Auth-Token' => token_headers['X-Subject-Token']}, :method => 'GET' } user_id = body['token']['user']['id'] response = Fog::Core::Connection.new( "#{uri.scheme}://#{uri.host}:#{uri.port}/v3/users/#{user_id}/projects", false, ).request(request_body) projects_body = Fog::JSON.decode(response.body) if projects_body['projects'].empty? raise Fog::Errors::NotFound.new('No Project Found') else [:openstack_project_name] = projects_body['projects'].first['name'] [:openstack_domain_name] = body['token']['user']['domain']['name'] end end tenant_name = [:openstack_project_name] unless tenant_name response = Fog::Core::Connection.new( "#{uri.scheme}://#{uri.host}:#{uri.port}/v3/projects", false, ).request({ :expects => [200, 204], :headers => {'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Auth-Token' => token_headers['X-Subject-Token']}, :method => 'GET' }) body = Fog::JSON.decode(response.body) if body['projects'].empty? raise Fog::Errors::NotFound.new('No Tenant Found') else [:openstack_tenant] = body['projects'].first['name'] end end body, token_headers = retrieve_tokens_v3(, ) service = get_service(body, service_type, service_name) end service['endpoints'] = service['endpoints'].select do |endpoint| endpoint['region'] == openstack_region && endpoint['interface'] == endpoint_type end if openstack_region if service['endpoints'].empty? raise Fog::Errors::NotFound.new("No endpoints available for region '#{openstack_region}'") end if openstack_region unless service available = body['token']['catalog'].map { |endpoint| endpoint['type'] }.sort.join ', ' missing = service_type.join ', ' = "Could not find service #{missing}. Have #{available}" raise Fog::Errors::NotFound, end admin = false body['token']['roles'].each do |r| admin = true if r["name"] == "admin" end regions = service["endpoints"].map{ |e| e['region'] }.uniq if regions.count > 1 and not admin raise Fog::Errors::NotFound.new("Multiple regions available choose one of these '#{regions.join(',')}'") end identity_service = get_service(body, identity_service_type) if identity_service_type tenant = body['token']['project']['name'] if body['token']['project'] tenant = body['token']['user']['project']['name'] unless body['token']['project'] user = body['token']['user']['name'] management_url = service['endpoints'].find{|s| s["interface"][endpoint_type]}["url"] identity_url = identity_service['endpoints'].find{|s| s["interface"]["public"]}["url"] if identity_service { :user => user, :tenant => tenant, :identity_public_endpoint => identity_url, :server_management_url => management_url, :token => token_headers['X-Subject-Token'], :expires => body['token']['expires_at'], :current_user_id => body['token']['user']['id'], :unscoped_token => [:unscoped_token] } end |
.escape(str, extra_exclude_chars = '') ⇒ Object
CGI.escape, but without special treatment on spaces
436 437 438 439 440 |
# File 'lib/fog/openstack/core.rb', line 436 def self.escape(str,extra_exclude_chars = '') str.gsub(/([^a-zA-Z0-9_.-#{extra_exclude_chars}]+)/) do '%' + $1.unpack('H2' * $1.bytesize).join('%').upcase end end |
.get_service(body, service_type = [], service_name = nil) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/fog/openstack/core.rb', line 288 def self.get_service(body, service_type=[], service_name=nil) if not body['access'].nil? body['access']['serviceCatalog'].find do |s| if service_name.nil? or service_name.empty? service_type.include?(s['type']) else service_type.include?(s['type']) and s['name'] == service_name end end elsif not body['token']['catalog'].nil? body['token']['catalog'].find do |s| if service_name.nil? or service_name.empty? service_type.include?(s['type']) else service_type.include?(s['type']) and s['name'] == service_name end end end end |
.get_supported_version(supported_versions, uri, auth_token, connection_options = {}) ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/fog/openstack/core.rb', line 408 def self.get_supported_version(supported_versions, uri, auth_token, = {}) connection = Fog::Core::Connection.new("#{uri.scheme}://#{uri.host}:#{uri.port}", false, ) response = connection.request({ :expects => [200, 204, 300], :headers => {'Content-Type' => 'application/json', 'Accept' => 'application/json', 'X-Auth-Token' => auth_token}, :method => 'GET' }) body = Fog::JSON.decode(response.body) version = nil unless body['versions'].empty? supported_version = body['versions'].find do |x| x["id"].match(supported_versions) && (x["status"] == "CURRENT" || x["status"] == "SUPPORTED") end version = supported_version["id"] if supported_version end if version.nil? raise Fog::OpenStack::Errors::ServiceUnavailable.new( "OpenStack service only supports API versions #{supported_versions.inspect}") end version end |
.map_endpoint_type(type) ⇒ Object
442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/fog/openstack/core.rb', line 442 def self.map_endpoint_type type case type when "publicURL" "public" when "internalURL" "internal" when "adminURL" "admin" end end |
.retrieve_tokens_v2(options, connection_options = {}) ⇒ Object
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/fog/openstack/core.rb', line 309 def self.retrieve_tokens_v2(, = {}) api_key = [:openstack_api_key].to_s username = [:openstack_username].to_s tenant_name = [:openstack_tenant].to_s auth_token = [:openstack_auth_token] || [:unscoped_token] uri = [:openstack_auth_uri] connection = Fog::Core::Connection.new(uri.to_s, false, ) request_body = {:auth => Hash.new} if auth_token request_body[:auth][:token] = { :id => auth_token } else request_body[:auth][:passwordCredentials] = { :username => username, :password => api_key } end request_body[:auth][:tenantName] = tenant_name if tenant_name response = connection.request({ :expects => [200, 204], :headers => {'Content-Type' => 'application/json'}, :body => Fog::JSON.encode(request_body), :method => 'POST', :path => (uri.path and not uri.path.empty?) ? uri.path : 'v2.0' }) Fog::JSON.decode(response.body) end |
.retrieve_tokens_v3(options, connection_options = {}) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/fog/openstack/core.rb', line 342 def self.retrieve_tokens_v3(, = {}) api_key = [:openstack_api_key].to_s username = [:openstack_username].to_s if [:openstack_tenant_id] tenant_id = [:openstack_tenant_id].to_s else tenant_name = [:openstack_tenant].to_s end auth_token = [:openstack_auth_token] || [:unscoped_token] uri = [:openstack_auth_uri] userdomain = [:openstack_user_domain] || [:openstack_domain] || 'Default' project_domain = [:openstack_project_domain] || [:openstack_domain] || 'Default' connection = Fog::Core::Connection.new(uri.to_s, false, ) request_body = {:auth => Hash.new} if auth_token request_body[:auth][:token] = { :id => auth_token } else request_body[:auth][:identity] = { :methods => ["password"], :password => { :user => { :name => username, :password => api_key } } } unless tenant_id.nil? request_body[:auth][:scope] = { :project => { :id => tenant_id } } else request_body[:auth][:scope] = { :project => { :name => tenant_name } } end unless userdomain.to_s.empty? request_body[:auth][:identity][:password][:user][:domain] = { :name => userdomain } end unless project_domain.to_s.empty? request_body[:auth][:scope][:project][:domain] = { :name => project_domain } end end response = connection.request({ :expects => [201], :headers => {'Content-Type' => 'application/json'}, :body => Fog::JSON.encode(request_body), :method => 'POST', :path => (uri.path and not uri.path.empty?) ? uri.path : 'v3' }) return Fog::JSON.decode(response.body), response.headers end |