Skip to content

Commit

Permalink
Merge pull request #86 from nathanlcarlson/user_fact
Browse files Browse the repository at this point in the history
Addition of 'users' facts
  • Loading branch information
jhoblitt authored Sep 24, 2024
2 parents 5bf52f8 + bfc1ce6 commit a77598b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ ipmi => {
ipaddress_source => Static Address,
macaddress => 00:30:48:c9:64:2a,
subnet_mask => 255.255.255.0,
users => {
1 => {
id => 1,
name => '',
privilege => 'NO ACCESS',
},
2 => {
id => 2,
name => 'admin',
privilege => 'ADMINISTRATOR',
}
},
},
1 => {
channel => 1,
Expand All @@ -176,6 +188,18 @@ ipmi => {
ipaddress_source => Static Address,
macaddress => 00:30:48:c9:64:2a,
subnet_mask => 255.255.255.0,
users => {
1 => {
id => 1,
name => '',
privilege => 'NO ACCESS',
},
2 => {
id => 2,
name => 'admin',
privilege => 'ADMINISTRATOR',
}
},
},
}
```
Expand Down
18 changes: 18 additions & 0 deletions lib/facter/ipmi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ def add_ipmi_fact(name, value)
next if lan_channel.empty?

lan_channel['channel'] = channel_nr

# Get Users
ipmitool_user_output = Facter::Util::Resolution.exec("ipmitool user list #{channel_nr} 2>&1")
lan_channel['users'] = {}
ipmitool_user_output.each_line do |line|
case line.strip
when %r{^(\d+)\s+(.+)(\s+(true|false)\s+){3}(USER|ADMINISTRATOR|CALLBACK|OPERATOR|NO ACCESS)$}
id = Regexp.last_match(1).strip.to_i
name = Regexp.last_match(2).strip
privilege = Regexp.last_match(5).strip
lan_channel['users'][id] = {
id: id,
name: name,
privilege: privilege,
}
end
end

ipmi['default'] = lan_channel unless ipmi.key?('default')
ipmi[channel_nr] = lan_channel
end
Expand Down
52 changes: 51 additions & 1 deletion spec/unit/facter/ipmi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,50 @@
: O=OEM
Bad Password Threshold : Not Available
OUTPUT
ipmitool_user_output = <<-USEROUTPUT
ID Name Callin Link Auth IPMI Msg Channel Priv Limit
1 true false true ADMINISTRATOR
2 admin true false true ADMINISTRATOR
3 foo_123_bar true false true ADMINISTRATOR
4 (Empty User) true false false NO ACCESS
5 (Empty User) true false false NO ACCESS
6 (Empty User) true false false NO ACCESS
7 (Empty User) true false false NO ACCESS
8 (Empty User) true false false NO ACCESS
9 (Empty User) true false false NO ACCESS
10 (Empty User) true false false NO ACCESS
11 (Empty User) true false false NO ACCESS
12 foobar true false true USER
USEROUTPUT
Facter::Util::Resolution.expects(:which).at_least(1).with('ipmitool').returns('/usr/bin/ipmitool')
Facter::Util::Resolution.expects(:exec).at_least(1).with('ipmitool lan print 1 2>&1').returns(ipmitool_output)
(2..11).to_a.each do |mocked_channel|
Facter::Util::Resolution.expects(:exec).at_least(1).with("ipmitool lan print #{mocked_channel} 2>&1").returns("Invalid channel: #{mocked_channel}")
end
Facter::Util::Resolution.expects(:exec).at_least(1).with('ipmitool user list 1 2>&1').returns(ipmitool_user_output)
Facter.fact(:kernel).stubs(:value).returns('Linux')
end

let(:facts) { { kernel: 'Linux' } }

it do
expect(Facter.value('ipmi.1.users.1.id')).to eq(1)
expect(Facter.value('ipmi.1.users.1.name')).to eq('')
expect(Facter.value('ipmi.1.users.1.privilege')).to eq('ADMINISTRATOR')
end

it do
expect(Facter.value('ipmi.1.users.12.id')).to eq(12)
expect(Facter.value('ipmi.1.users.12.name')).to eq('foobar')
expect(Facter.value('ipmi.1.users.12.privilege')).to eq('USER')
end

it do
expect(Facter.value('ipmi.1.users.4.id')).to eq(4)
expect(Facter.value('ipmi.1.users.4.name')).to eq('(Empty User)')
expect(Facter.value('ipmi.1.users.4.privilege')).to eq('NO ACCESS')
end

it do
expect(Facter.value(:ipmi_ipaddress)).to eq('192.168.0.37')
expect(Facter.value('ipmi.default.ipaddress')).to eq('192.168.0.37')
Expand Down Expand Up @@ -138,11 +172,27 @@
: O=OEM
Bad Password Threshold : Not Available
OUTPUT

ipmitool_user_output = <<-USEROUTPUT
ID Name Callin Link Auth IPMI Msg Channel Priv Limit
1 true false true ADMINISTRATOR
2 admin true false true ADMINISTRATOR
3 foo_123_bar true false true ADMINISTRATOR
4 (Empty User) true false false NO ACCESS
5 (Empty User) true false false NO ACCESS
6 (Empty User) true false false NO ACCESS
7 (Empty User) true false false NO ACCESS
8 (Empty User) true false false NO ACCESS
9 (Empty User) true false false NO ACCESS
10 (Empty User) true false false NO ACCESS
11 (Empty User) true false false NO ACCESS
12 foobar true false true USER
USEROUTPUT
Facter::Util::Resolution.expects(:which).at_least(1).with('ipmitool').returns('/usr/bin/ipmitool')
Facter::Util::Resolution.expects(:exec).at_least(1).with('ipmitool lan print 2 2>&1').returns(ipmitool_2_output)
Facter::Util::Resolution.expects(:exec).at_least(1).with('ipmitool lan print 3 2>&1').returns(ipmitool_3_output)
Facter::Util::Resolution.expects(:exec).at_least(1).with('ipmitool lan print 1 2>&1').returns('Invalid channel: 1')
Facter::Util::Resolution.expects(:exec).at_least(1).with('ipmitool user list 2 2>&1').returns(ipmitool_user_output)
Facter::Util::Resolution.expects(:exec).at_least(1).with('ipmitool user list 3 2>&1').returns(ipmitool_user_output)
(4..11).to_a.each do |mocked_channel|
Facter::Util::Resolution.expects(:exec).at_least(1).with("ipmitool lan print #{mocked_channel} 2>&1").returns("Invalid channel: #{mocked_channel}")
end
Expand Down

0 comments on commit a77598b

Please sign in to comment.