Skip to content

Commit

Permalink
feat(freebsd): Add geli drive encryption support
Browse files Browse the repository at this point in the history
  • Loading branch information
g-bougard committed Feb 8, 2019
1 parent e054385 commit 2ed34b8
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
103 changes: 102 additions & 1 deletion lib/FusionInventory/Agent/Task/Inventory/BSD/Drives.pm
Expand Up @@ -20,6 +20,14 @@ sub doInventory {
my $inventory = $params{inventory};
my $logger = $params{logger};

my $zpool = canRun('zpool');

# Check we can run geli command to lookup encrypted fs
my ($geli);
if (canRun('geom')) {
$geli = _getGeliList(%params);
}

# get filesystem types
my @types =
grep { ! /^(?:fdesc|devfs|procfs|linprocfs|linsysfs|tmpfs|fdescfs)$/ }
Expand All @@ -28,11 +36,35 @@ sub doInventory {
# get filesystem for each type
my @filesystems;
foreach my $type (@types) {
push @filesystems, getFilesystemsFromDf(
my $foundfs = getFilesystemsFromDf(
logger => $logger,
command => "df -P -k -t $type",
type => $type
);

# Check for geli encryption
if ($geli) {
foreach my $fs (@{$foundfs}) {
my $encrypted;
if ($type eq 'zfs' && $zpool) {
my $status = _getZpoolStatus(
volumn => $fs->{VOLUMN},
%params
);
($encrypted) = grep { /.eli$/ } keys(%{$status->{config}});
} else {
($encrypted) = $fs->{VOLUMN} =~ m|/([^/]+.eli)$|;
}
if ($encrypted && $geli->{$encrypted}) {
$fs->{ENCRYPT_NAME} = "geli";
$fs->{ENCRYPT_STATUS} = $geli->{$encrypted}->{state} =~ /^ACTIVE$/i ? 'Yes' : 'No';
$fs->{ENCRYPT_ALGO} = $geli->{$encrypted}->{algo};
$fs->{ENCRYPT_TYPE} = $geli->{$encrypted}->{type};
}
}
}

push @filesystems, @{$foundfs};
}

# add filesystems to the inventory
Expand All @@ -44,4 +76,73 @@ sub doInventory {
}
}

sub _getZpoolStatus {
my (%params) = @_;

my $volumn = $params{volumn}
or return;

my @lines = getAllLines(
command => "zpool status $volumn",
%params
);

my $status = {};
foreach my $line (@lines) {
next unless $line;
if ($line =~ /^\s*(\w+)\s*:\s*(\w.*)$/) {
$status->{$1} = $2;
} elsif ($line =~ /^\s*config\s*:/) {
$status->{config} = {};
} elsif ($status->{config} && $line =~ /^\s*([\w.]+)\s+(\w+)\s+\w+\s+\w+\s+\w+/) {
next if $1 eq "NAME";
$status->{config}->{$1} = $2;
}
}

return $status;
}

sub _getGeliList {
my (%params) = @_;

my $geli;

my @status = getAllLines(
command => "geom eli status -s",
%params
);

foreach my $status (@status) {
next unless $status =~ /^(\S+)\s/;

my $volumn = $1;

my @lines = getAllLines(
command => "geom eli list $volumn",
%params
);
foreach my $line (@lines) {
next unless $line;
if ($line =~ /^State:\s*(\S+)$/) {
$geli->{$volumn}->{state} = $1;
} elsif ($line =~ /^EncryptionAlgorithm:\s*(\S+)$/) {
$geli->{$volumn}->{algo} = $1;
} elsif ($line =~ /^KeyLength:\s*(\S+)$/) {
$geli->{$volumn}->{keysize} = $1;
} elsif ($line =~ /^Crypto:\s*(\S+)$/) {
$geli->{$volumn}->{type} = $1;
}
}

# Fix algo with keysize
if ($geli->{$volumn}->{algo} && $geli->{$volumn}->{keysize}) {
$geli->{$volumn}->{algo} = $geli->{$volumn}->{algo}."-".$geli->{$volumn}->{keysize};
delete $geli->{$volumn}->{keysize};
}
}

return $geli;
}

1;
2 changes: 1 addition & 1 deletion lib/FusionInventory/Agent/Tools/Unix.pm
Expand Up @@ -198,7 +198,7 @@ sub getFilesystemsFromDf {

close $handle;

return @filesystems;
return wantarray ? @filesystems : \@filesystems ;
}

sub getFilesystemsTypesFromMount {
Expand Down

0 comments on commit 2ed34b8

Please sign in to comment.