idmclient 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/idmclient.rb +92 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: be3c0b7fd8b9c6f301a26511ecf06e26e065a38b
|
|
4
|
+
data.tar.gz: b2d82c374665c64c3ed8ebe3667e973816c16d22
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4bda7d3e5d421b06e0852c0ba34f05f4dea27d42387454e201e5feab3082b73d25e847878494c36a4003423ad9701991acf6caa2ec903bd91cf782c0636221d4
|
|
7
|
+
data.tar.gz: cbc039ff834600351b47415ba160ff508d8c67b7ff98f88496874663c4c86c40c8baedf5aa8bff6c964a8c6731b25895cd86cde3c772c1a9e6406a1374f6f2cb
|
data/lib/idmclient.rb
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Needed for HTTP requests
|
|
2
|
+
require 'uri'
|
|
3
|
+
require 'net/http'
|
|
4
|
+
# Needed for JSON-RPC cookie management
|
|
5
|
+
require 'http-cookie'
|
|
6
|
+
# Needed for '{...}'.to_json
|
|
7
|
+
require 'json'
|
|
8
|
+
# Needed for request Id (UUID)
|
|
9
|
+
require 'securerandom'
|
|
10
|
+
|
|
11
|
+
# Test if this is executed in a ManageIQ / Cloudforms environment
|
|
12
|
+
begin
|
|
13
|
+
# Needed for MiqPassword.decrypt
|
|
14
|
+
require 'miq-password'
|
|
15
|
+
MIQ_METHOD = true
|
|
16
|
+
rescue LoadError
|
|
17
|
+
MIQ_METHOD = false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class IDMClient
|
|
21
|
+
attr_reader :http, :cookiejar, :uri_base, :uri_auth, :uri_data, :api_version
|
|
22
|
+
|
|
23
|
+
def initialize(uri, options=Hash.new)
|
|
24
|
+
# Set defaults
|
|
25
|
+
options.reverse_merge!({
|
|
26
|
+
:ca_file => '/etc/ipa/ca.crt',
|
|
27
|
+
:api_version => '2.228',
|
|
28
|
+
:debug => false
|
|
29
|
+
})
|
|
30
|
+
@uri_base = uri
|
|
31
|
+
@uri_auth = URI("#{uri_base}/session/login_password")
|
|
32
|
+
@uri_data = URI("#{uri_base}/session/json")
|
|
33
|
+
@api_version = options[:api_version]
|
|
34
|
+
# Prepare the connection
|
|
35
|
+
@http = Net::HTTP.new(uri_auth.host, uri_auth.port)
|
|
36
|
+
http.use_ssl = uri_auth.scheme == 'https'
|
|
37
|
+
http.ca_file = options[:ca_file]
|
|
38
|
+
# Configure debug output
|
|
39
|
+
if options[:debug]
|
|
40
|
+
http.set_debug_output($stdout)
|
|
41
|
+
end
|
|
42
|
+
# Prepare cookie storage
|
|
43
|
+
@cookiejar = HTTP::CookieJar.new
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def authenticate(username, password)
|
|
47
|
+
http.start {
|
|
48
|
+
# Prepare the authentication request
|
|
49
|
+
req = Net::HTTP::Post.new(uri_auth, 'Referer' => uri_base)
|
|
50
|
+
req.form_data = {
|
|
51
|
+
:user => username,
|
|
52
|
+
# Cloudforms / ManageIQ - Decrypt password if necessary
|
|
53
|
+
:password => (MIQ_METHOD && password.match(/^v\d\:\{.*\}$/)) ? MiqPassword.decrypt(password) : password
|
|
54
|
+
}
|
|
55
|
+
# Make the authentication request
|
|
56
|
+
res = http.request req
|
|
57
|
+
# Save the returned cookies
|
|
58
|
+
res.get_fields('Set-Cookie').each { |value| cookiejar.parse(value, req.uri) }
|
|
59
|
+
# Expecting an HTTP 200 response
|
|
60
|
+
return res.code == '200'
|
|
61
|
+
}
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def call(method, args=Array.new, options=Hash.new)
|
|
65
|
+
# Update options
|
|
66
|
+
options['version'] = options['version'] || api_version
|
|
67
|
+
# Start a connection
|
|
68
|
+
http.start {
|
|
69
|
+
# Prepare the data request
|
|
70
|
+
req_id = SecureRandom.uuid
|
|
71
|
+
req = Net::HTTP::Post.new(
|
|
72
|
+
uri_data,
|
|
73
|
+
'Content-Type' => 'application/json',
|
|
74
|
+
'Referer' => uri_base,
|
|
75
|
+
'Cookie' => HTTP::Cookie.cookie_value(cookiejar.cookies(uri_data)))
|
|
76
|
+
req.body = {
|
|
77
|
+
"method": method,
|
|
78
|
+
"params": [args, options],
|
|
79
|
+
"id": req_id,
|
|
80
|
+
}.to_json
|
|
81
|
+
# Make the data request and parse response
|
|
82
|
+
res = http.request req
|
|
83
|
+
data = JSON.parse(res.body)
|
|
84
|
+
# Check for error
|
|
85
|
+
if data['error']
|
|
86
|
+
raise data['error']['message']
|
|
87
|
+
end
|
|
88
|
+
# Validate request Id and return results
|
|
89
|
+
return (data['id'] == req_id) ? data['result']['result'] : nil
|
|
90
|
+
}
|
|
91
|
+
end
|
|
92
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: idmclient
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Joshua Cornutt
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-09-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: FreeIPA & Red Hat Identity Management (IdM) JSON-RPC API wrapper
|
|
14
|
+
email: joshua@joscor.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/idmclient.rb
|
|
20
|
+
homepage: https://github.com/01000101/idmclient-gem
|
|
21
|
+
licenses:
|
|
22
|
+
- MIT
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubyforge_project:
|
|
40
|
+
rubygems_version: 2.6.13
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: Wrapper for FreeIPA & Red Hat Identity Management (IdM)
|
|
44
|
+
test_files: []
|