Class: IPA::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ipa/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, ca_cert: '/etc/ipa/ca.crt') ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
# 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/json")

  gssapi = GSSAPI::Simple.new(uri.host, 'HTTP')
  # Initiate the security context
  token = gssapi.init_context

  @http = HTTPClient.new
  @http.ssl_config.set_trust_ca(ca_cert)
  @headers = {'referer' => "https://#{uri.host}/ipa/ui/index.html", 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => "Negotiate #{Base64.strict_encode64(token)}"}
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



15
16
17
# File 'lib/ipa/client.rb', line 15

def headers
  @headers
end

#httpObject (readonly)

Returns the value of attribute http.



15
16
17
# File 'lib/ipa/client.rb', line 15

def http
  @http
end

#uriObject (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

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
# File 'lib/ipa/client.rb', line 31

def api_post(method: nil, item: [], params: {})
  raise ArgumentError, 'Missing method in API request' unless method
  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

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
# File 'lib/ipa/client.rb', line 40

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

Raises:

  • (ArgumentError)


51
52
53
54
55
# File 'lib/ipa/client.rb', line 51

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

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'lib/ipa/client.rb', line 71

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



57
58
59
60
61
# File 'lib/ipa/client.rb', line 57

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

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
# File 'lib/ipa/client.rb', line 63

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