-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #403: Extend Rudder specific inventory with client side data #404
Open
ncharles
wants to merge
1
commit into
fusioninventory:2.4.x
Choose a base branch
from
ncharles:bug_403_extend_inventory_in_rudder
base: 2.4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,9 @@ use warnings; | |
use English qw(-no_match_vars); | ||
|
||
use FusionInventory::Agent::Tools; | ||
use UNIVERSAL::require; | ||
use File::stat; | ||
|
||
|
||
sub isEnabled { | ||
return -r getUuidFile(); | ||
|
@@ -42,19 +45,84 @@ sub doInventory { | |
# Get agent capabilities | ||
my @agentCapabilities = _listAgentCapabilities(); | ||
|
||
my $customProperties = _getCustomProperties(logger => $logger); | ||
|
||
my $rudder = { | ||
HOSTNAME => $hostname, | ||
UUID => $Uuid, | ||
AGENT => \@agents, | ||
SERVER_ROLES => { SERVER_ROLE => \@serverRoles }, | ||
AGENT_CAPABILITIES => { AGENT_CAPABILITY => \@agentCapabilities }, | ||
CUSTOM_PROPERTIES => $customProperties, | ||
}; | ||
|
||
$inventory->addEntry( | ||
section => 'RUDDER', entry => $rudder | ||
); | ||
} | ||
|
||
sub _getCustomProperties { | ||
my (%params) = @_; | ||
my $logger = $params{logger}; | ||
|
||
my $custom_properties_dir = ($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\hooks.d' : '/var/rudder/hooks.d'; | ||
my $custom_properties; | ||
if (-d "$custom_properties_dir") { | ||
my @custom_properties_list = (); | ||
my @ordered_script_list = (); | ||
opendir(DIR, $custom_properties_dir); | ||
# List each file in the custom_properties directory, each files being a script | ||
@ordered_script_list = sort readdir(DIR); | ||
closedir(DIR); | ||
while (my $file = shift @ordered_script_list) { | ||
my $script_file = $custom_properties_dir . "/" . $file; | ||
if (-f $script_file) { | ||
next if ($file =~ m/^\./); | ||
# Ignore non-executable file, or folders | ||
next unless -x $script_file; | ||
|
||
# Check that the file is not world writable | ||
my $permissions = stat($script_file); | ||
my $retMode = $permissions->mode; | ||
$retMode = $retMode & 0777; | ||
if (($retMode & 002) || ($retMode & 020)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this fails on windows - even if file is writable only by admin, it gets 777 :( |
||
$logger->error("Skipping script $script_file as it is world or group writable") if $logger; | ||
next; | ||
} | ||
|
||
$logger->debug2("executiong $script_file") if $logger; | ||
my $properties = qx($script_file); | ||
my $exit_code = $? >> 8; | ||
if ($exit_code > 0) { | ||
$logger->error("Script $script_file failed to run properly, with exit code $exit_code") if $logger; | ||
next; | ||
} | ||
|
||
# check that it is valid JSON | ||
eval { | ||
my $package = "JSON::PP"; | ||
$package->require(); | ||
if ($EVAL_ERROR) { | ||
print STDERR | ||
"Failed to load JSON module: ($EVAL_ERROR)\n"; | ||
next; | ||
} | ||
my $coder = JSON::PP->new; | ||
my $propertiesData = $coder->decode($properties); | ||
push @custom_properties_list, $coder->encode($propertiesData); | ||
}; | ||
if ($@) { | ||
$logger->error("Script $script_file didn't return valid JSON entry, error is:$@") if $logger; | ||
} | ||
} | ||
|
||
} | ||
$custom_properties = "[". join(",", @custom_properties_list) . "]"; | ||
} | ||
return $custom_properties; | ||
} | ||
|
||
|
||
sub _listServerRoles { | ||
my $server_roles_dir = ($OSNAME eq 'MSWin32') ? 'C:\Program Files\Rudder\etc\server-roles.d' : '/opt/rudder/etc/server-roles.d'; | ||
my @server_roles; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here, I failed to check if file was owned by root (or its Windows equivalent) - as there can be several admin
how would you do that ? @peckpeck @g-bougard
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used$currentUser = $ <;
my $stats = stat($script_file);
my $owner = $stats->uid;
my
file must be owned by root or current user
if (($owner != 0) && ($owner != $currentUser)) {
$logger->error("Skipping script $script_file as it is not owned by root nor current user (owner is $owner)") if $logger;
next;
}
but this fails miserably on Windows - it always believe the owner of the file is 0