Class: SmartProxies::PuppetCA

Inherits:
Object
  • Object
show all
Defined in:
app/models/smart_proxies/puppet_ca.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ PuppetCA


7
8
9
10
11
# File 'app/models/smart_proxies/puppet_ca.rb', line 7

def initialize opts
  @name, @state, @fingerprint, @valid_from, @expires_at, @smart_proxy_id = opts.flatten
  @valid_from = Time.parse(@valid_from) unless @valid_from.blank?
  @expires_at = Time.parse(@expires_at) unless @expires_at.blank?
end

Instance Attribute Details

#expires_atObject (readonly)

Returns the value of attribute expires_at


5
6
7
# File 'app/models/smart_proxies/puppet_ca.rb', line 5

def expires_at
  @expires_at
end

#fingerprintObject (readonly)

Returns the value of attribute fingerprint


5
6
7
# File 'app/models/smart_proxies/puppet_ca.rb', line 5

def fingerprint
  @fingerprint
end

#nameObject (readonly)

Returns the value of attribute name


5
6
7
# File 'app/models/smart_proxies/puppet_ca.rb', line 5

def name
  @name
end

#smart_proxy_idObject (readonly)

Returns the value of attribute smart_proxy_id


5
6
7
# File 'app/models/smart_proxies/puppet_ca.rb', line 5

def smart_proxy_id
  @smart_proxy_id
end

#stateObject (readonly)

Returns the value of attribute state


5
6
7
# File 'app/models/smart_proxies/puppet_ca.rb', line 5

def state
  @state
end

#valid_fromObject (readonly)

Returns the value of attribute valid_from


5
6
7
# File 'app/models/smart_proxies/puppet_ca.rb', line 5

def valid_from
  @valid_from
end

Class Method Details

.all(proxy) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/smart_proxies/puppet_ca.rb', line 15

def all(proxy)
  raise ::Foreman::Exception.new(N_("Must specify a Smart Proxy to use")) if proxy.nil?

  unless (certs = Rails.cache.read("ca_#{proxy.id}"))
    api = ProxyAPI::Puppetca.new({:url => proxy.url})

    certs = api.all.map do |name, properties|
      new([name.strip, properties['state'], properties['fingerprint'], properties["not_before"], properties["not_after"], proxy.id])
    end.compact

    # save our CA details for 5 seconds
    Rails.cache.write("ca_#{proxy.id}", certs, {:expires_in  => 1.minute }) if Rails.env.production?
  end
  certs
end

.find(proxy, name) ⇒ Object


31
32
33
34
35
# File 'app/models/smart_proxies/puppet_ca.rb', line 31

def find(proxy, name)
  all(proxy).select{|c| c.name == name}.first
rescue
  raise ActiveRecord::RecordNotFound
end

.find_by_state(proxy, state) ⇒ Object


37
38
39
40
41
# File 'app/models/smart_proxies/puppet_ca.rb', line 37

def find_by_state(proxy, state)
   all(proxy).select{|c| c.state == state}
rescue
  raise ActiveRecord::RecordNotFound
end

Instance Method Details

#<=>(other) ⇒ Object


61
62
63
# File 'app/models/smart_proxies/puppet_ca.rb', line 61

def <=> other
  self.name <=> other.name
end

#destroyObject


51
52
53
54
55
# File 'app/models/smart_proxies/puppet_ca.rb', line 51

def destroy
  proxy = SmartProxy.find(smart_proxy_id)
  Rails.cache.delete("ca_#{proxy.id}") if Rails.env.production?
  ProxyAPI::Puppetca.new({:url => proxy.url}).del_certificate name
end

#signObject


44
45
46
47
48
49
# File 'app/models/smart_proxies/puppet_ca.rb', line 44

def sign
  raise ::Foreman::Exception.new(N_("unable to sign a non pending certificate")) unless state == "pending"
  proxy = SmartProxy.find(smart_proxy_id)
  Rails.cache.delete("ca_#{proxy.id}") if Rails.env.production?
  ProxyAPI::Puppetca.new({:url => proxy.url}).sign_certificate name
end

#to_paramObject


57
# File 'app/models/smart_proxies/puppet_ca.rb', line 57

def to_param; name end

#to_sObject


59
# File 'app/models/smart_proxies/puppet_ca.rb', line 59

def to_s; name end