Class: Mdm::Cred

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/mdm/cred.rb

Constant Summary collapse

KEY_ID_REGEX =

CONSTANTS

/([0-9a-fA-F:]{47})/
PTYPES =
{
    'read/write password' => 'password_rw',
    'read-only password' => 'password_ro',
    'SMB hash' => 'smb_hash',
    'SSH private key' => 'ssh_key',
    'SSH public key' => 'ssh_pubkey'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#servceMdm::Service

The service this cred is for

Returns:



22
23
24
# File 'app/models/mdm/cred.rb', line 22

belongs_to :service,
class_name: 'Mdm::Service',
inverse_of: :creds

#task_credsArray<Mdm::TaskCred>

Details about what Tasks touched this cred

Returns:



30
31
32
33
# File 'app/models/mdm/cred.rb', line 30

has_many :task_creds,
class_name: 'Mdm::TaskCred',
dependent: :destroy,
inverse_of: :cred

#tasksArray<Mdm::Task>

Tasks that touched this service

Returns:



39
# File 'app/models/mdm/cred.rb', line 39

has_many :tasks, :through => :task_creds

Instance Method Details

#ptype_humanObject



44
45
46
47
48
49
50
# File 'app/models/mdm/cred.rb', line 44

def ptype_human
  humanized = PTYPES.select do |k, v|
    v == ptype
  end.keys[0]

  humanized ? humanized : ptype
end

#ssh_key_idObject

Returns its key id. If this is not an ssh-type key, returns nil.



53
54
55
56
57
# File 'app/models/mdm/cred.rb', line 53

def ssh_key_id
  return nil unless self.ptype =~ /^ssh_/
  return nil unless self.proof =~ KEY_ID_REGEX
  $1.downcase # Can't run into NilClass problems.
end

#ssh_key_matches?(other_cred) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/mdm/cred.rb', line 59

def ssh_key_matches?(other_cred)
  return false unless other_cred.kind_of? self.class
  return false unless self.ptype == other_cred.ptype
  case self.ptype
    when "ssh_key"
      matches = self.ssh_private_keys
    when "ssh_pubkey"
      matches = self.ssh_public_keys
    else
      return false
  end
  matches.include?(self) and matches.include?(other_cred)
end

#ssh_keysObject

Returns all keys with matching key ids, including itself If this is not an ssh-type key, always returns an empty array.



75
76
77
# File 'app/models/mdm/cred.rb', line 75

def ssh_keys
  (self.ssh_private_keys | self.ssh_public_keys)
end

#ssh_private_keysObject

Returns all private keys with matching key ids, including itself If this is not an ssh-type key, always returns an empty array.



81
82
83
84
85
86
87
# File 'app/models/mdm/cred.rb', line 81

def ssh_private_keys
  return [] unless self.ssh_key_id
  matches = self.class.all(
      :conditions => ["creds.ptype = ? AND creds.proof ILIKE ?", "ssh_key", "%#{self.ssh_key_id}%"]
  )
  matches.select {|c| c.workspace == self.workspace}
end

#ssh_public_keysObject

Returns all public keys with matching key ids, including itself If this is not an ssh-type key, always returns an empty array.



91
92
93
94
95
96
97
# File 'app/models/mdm/cred.rb', line 91

def ssh_public_keys
  return [] unless self.ssh_key_id
  matches = self.class.all(
      :conditions => ["creds.ptype = ? AND creds.proof ILIKE ?", "ssh_pubkey", "%#{self.ssh_key_id}%"]
  )
  matches.select {|c| c.workspace == self.workspace}
end

#workspaceObject

Returns its workspace



100
101
102
# File 'app/models/mdm/cred.rb', line 100

def workspace
  self.service.host.workspace
end