Class: IPA::Client
- Inherits:
-
Object
- Object
- IPA::Client
- Defined in:
- lib/ipa/client.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #api_post(method: nil, item: [], params: {}) ⇒ Object
- #host_add(hostname: nil, all: false, force: false, random: nil, userpassword: nil, params: {}) ⇒ Object
- #host_del(hostname: nil, params: {}) ⇒ Object
- #host_exists?(hostname) ⇒ Boolean
- #host_find(hostname: nil, all: false, params: {}) ⇒ Object
- #host_show(hostname: nil, all: false, params: {}) ⇒ Object
- #hostgroup_add(hostgroup: nil, description: nil, all: false, params: {}) ⇒ Object
- #hostgroup_add_member(hostgroup: nil, hostnames: nil, params: {}) ⇒ Object
- #hostgroup_show(hostgroup: nil, all: false, params: {}) ⇒ Object
-
#initialize(host: nil, ca_cert: '/etc/ipa/ca.crt') ⇒ Client
constructor
A new instance of Client.
- #login(host) ⇒ Object
Constructor Details
#initialize(host: nil, ca_cert: '/etc/ipa/ca.crt') ⇒ Client
Returns a new instance of Client.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ipa/client.rb', line 17 def initialize(host: nil, ca_cert: '/etc/ipa/ca.crt') raise ArgumentError, 'Missing FreeIPA host' unless host @uri = URI.parse("https://#{host}/ipa/session/json") @http = HTTPClient.new @http.ssl_config.set_trust_ca(ca_cert) @headers = {'referer' => "https://#{uri.host}/ipa/json", 'Content-Type' => 'application/json', 'Accept' => 'application/json'} self.login(host) end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
15 16 17 |
# File 'lib/ipa/client.rb', line 15 def headers @headers end |
#http ⇒ Object (readonly)
Returns the value of attribute http.
15 16 17 |
# File 'lib/ipa/client.rb', line 15 def http @http end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
15 16 17 |
# File 'lib/ipa/client.rb', line 15 def uri @uri end |
Instance Method Details
#api_post(method: nil, item: [], params: {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ipa/client.rb', line 44 def api_post(method: nil, item: [], params: {}) raise ArgumentError, 'Missing method in API request' unless method if Time.new.to_i > @session_timeout then self.login end request = {} request[:method] = method request[:params] = [[item || []], params] resp = self.http.post(self.uri, request.to_json, self.headers) JSON.parse(resp.body) end |
#host_add(hostname: nil, all: false, force: false, random: nil, userpassword: nil, params: {}) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ipa/client.rb', line 91 def host_add(hostname: nil, all: false, force: false, random: nil, userpassword: nil, params: {}) raise ArgumentError, 'Hostname is required' unless hostname params[:all] = all params[:force] = force params[:random] = random unless random.nil? params[:userpassword] = userpassword unless userpassword.nil? self.api_post(method: 'host_add', item: hostname, params: params) end |
#host_del(hostname: nil, params: {}) ⇒ Object
102 103 104 105 106 |
# File 'lib/ipa/client.rb', line 102 def host_del(hostname: nil, params: {}) raise ArgumentError, 'Hostname is required' unless hostname self.api_post(method: 'host_del', item: hostname, params: params) end |
#host_exists?(hostname) ⇒ Boolean
122 123 124 125 126 127 128 129 |
# File 'lib/ipa/client.rb', line 122 def host_exists?(hostname) resp = self.host_show(hostname: hostname) if resp['error'] false else true end end |
#host_find(hostname: nil, all: false, params: {}) ⇒ Object
108 109 110 111 112 |
# File 'lib/ipa/client.rb', line 108 def host_find(hostname: nil, all: false, params: {}) params[:all] = all self.api_post(method: 'host_find', item: hostname, params: params) end |
#host_show(hostname: nil, all: false, params: {}) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/ipa/client.rb', line 114 def host_show(hostname: nil, all: false, params: {}) raise ArgumentError, 'Hostname is required' unless hostname params[:all] = all self.api_post(method: 'host_show', item: hostname, params: params) end |
#hostgroup_add(hostgroup: nil, description: nil, all: false, params: {}) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/ipa/client.rb', line 66 def hostgroup_add(hostgroup: nil, description: nil, all: false, params: {}) raise ArgumentError, 'Hostgroup is required' unless hostgroup raise ArgumentError, 'description is required' unless description params[:all] = all params[:description] = description self.api_post(method: 'hostgroup_add', item: hostgroup, params: params) end |
#hostgroup_add_member(hostgroup: nil, hostnames: nil, params: {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ipa/client.rb', line 76 def hostgroup_add_member(hostgroup: nil, hostnames: nil, params: {}) raise ArgumentError, 'Hostgroup is required' unless hostgroup raise ArgumentError, 'Hostnames is required' unless hostnames params[:all] = true if hostnames.kind_of?(Array) params[:host] = hostnames end if hostnames.kind_of?(String) params[:host] = [hostnames] end self.api_post(method: 'hostgroup_add_member', item: hostgroup, params: params) end |
#hostgroup_show(hostgroup: nil, all: false, params: {}) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/ipa/client.rb', line 58 def hostgroup_show(hostgroup: nil,all: false, params: {}) raise ArgumentError, 'Hostgroup is required' unless hostgroup params[:all] = all self.api_post(method: 'hostgroup_show', item: hostgroup, params: params) end |
#login(host) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ipa/client.rb', line 29 def login(host) # Set the timeout to 15 minutes @session_timeout = (Time.new.to_i + 900) gssapi = GSSAPI::Simple.new(@uri.host, 'HTTP') # Initiate the security context token = gssapi.init_context login_uri = URI.parse("https://#{host}/ipa/session/login_kerberos") login_request = {:method => "ping", :params => [[], {}]} login_headers = {'referer' => "https://#{uri.host}/ipa/ui/index.html", 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => "Negotiate #{Base64.strict_encode64(token)}"} self.http.post(login_uri, login_request.to_json, login_headers) end |