Class: OsCtl::Lib::KernelKeyring

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/kernel_keyring.rb

Overview

Provides access to usage info of the kernel keyring

Defined Under Namespace

Classes: KeyUser

Instance Method Summary collapse

Constructor Details

#initialize(key_users: '/proc/key-users') ⇒ KernelKeyring

Returns a new instance of KernelKeyring.



31
32
33
# File 'lib/libosctl/kernel_keyring.rb', line 31

def initialize(key_users: '/proc/key-users')
  @key_users = parse_key_users(key_users)
end

Instance Method Details

#each {|| ... } ⇒ Object

Iterate over keyring users

Yield Parameters:



69
70
71
# File 'lib/libosctl/kernel_keyring.rb', line 69

def each(&)
  @key_users.each_value(&)
end

#for_id_map(id_map) ⇒ Array<KeyUser>

Get key users whose IDs are included in a map

Parameters:

Returns:



45
46
47
# File 'lib/libosctl/kernel_keyring.rb', line 45

def for_id_map(id_map)
  @key_users.each_value.select { |ku| id_map.include_host_id?(ku.uid) }
end

#for_uid(uid) ⇒ KeyUser?

Get key user by user ID

Parameters:

  • uid (Integer)

Returns:



38
39
40
# File 'lib/libosctl/kernel_keyring.rb', line 38

def for_uid(uid)
  @key_users[uid]
end

#maxbytesInteger

System-wide maximum number of bytes

Returns:

  • (Integer)


63
64
65
# File 'lib/libosctl/kernel_keyring.rb', line 63

def maxbytes
  @maxbytes ||= File.read('/proc/sys/kernel/keys/maxbytes').strip.to_i
end

#maxkeysInteger

System-wide maximum number of keys

Returns:

  • (Integer)


57
58
59
# File 'lib/libosctl/kernel_keyring.rb', line 57

def maxkeys
  @maxkeys ||= File.read('/proc/sys/kernel/keys/maxkeys').strip.to_i
end

#parse_key_users(path) ⇒ Object (protected)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/libosctl/kernel_keyring.rb', line 75

def parse_key_users(path)
  ret = {}

  # See man keyrings(7) for the file format
  File.open(path) do |f|
    f.each_line do |line|
      uid, usage, total_keys, owned_keys, owned_bytes = line.split

      uid_int = uid[0..-2].to_i
      nkeys, nikeys = total_keys.split('/')
      qnkeys, maxkeys = owned_keys.split('/')
      qnbytes, maxbytes = owned_bytes.split('/')

      ret[uid_int] = KeyUser.new(
        uid_int,
        usage.to_i,
        nkeys.to_i,
        nikeys.to_i,
        qnkeys.to_i,
        maxkeys.to_i,
        qnbytes.to_i,
        maxbytes.to_i
      )
    end
  end

  ret
end

#sortArray<KeyUser>

Get key users sorted by the number of owned keys

Returns:



51
52
53
# File 'lib/libosctl/kernel_keyring.rb', line 51

def sort
  @key_users.values.sort { |a, b| a.qnkeys <=> b.qnkeys }
end