auth helper program looks to be done

This commit is contained in:
Zane C. B-H 2020-12-01 03:23:22 -06:00
parent d729946e6a
commit a3119dce55
4 changed files with 106 additions and 3 deletions

View File

@ -16,6 +16,7 @@ my %WriteMakefileArgs = (
TEST_REQUIRES => {
'Test::More' => '0',
},
INST_SCRIPT => 'bin',
PREREQ_PM => {
'Crypt::Mac::HMAC'=>'0.069',
'Time::HiRes'=>'1.9764',

87
bin/lm-auth-helper Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/env perl
use strict;
use warnings;
use LogicMonitor::REST::Signature;
use Getopt::Long;
sub version{
print "lm-auth-helper v. 0.0.1\n";
}
sub help{
print '
';
}
my $required_args = {
'Logicmonitor_Company' => 1,
'Logicmonitor_accessKey' => 1,
'Logicmonitor_accessID' => 1,
};
foreach my $key ( keys %{$required_args} ) {
if (!defined( $ENV{$key} )) {
die( 'The enviromental variable "'.$key.'" is not set' );
}
}
# initiate the helper
my $lmsig_helper=LogicMonitor::REST::Signature->new({
company=>$ENV{Logicmonitor_Company},
accessKey=>$ENV{Logicmonitor_accessKey},
accessID=>$ENV{Logicmonitor_accessID},
});
# read the options in questions
my $verb = 'GET';
my $file;;
my $data='';
my $path;
my $version;
my $help;
my $newline;
GetOptions(
'verb=s' => \$verb,
'data=s' => \$file,
'path=s' => \$path,
'v' => \$version,
'version' => \$version,
'h' => \$help,
'help' => \$help,
'n'=>\$newline,
);
# print the version or help info if requested
if ( $version ){
&version;
exit;
}
if ( $help ){
&version;
&help;
exit;
}
# read the file contaning the data if specified
if ($file) {
my $fh;
open( $fh,'<', $file ) or die $!;
while (readline($fh)) {
$data=$data.$_;
}
close( $fh );
}
# create the auth header data with the sig included
print $lmsig_helper->auth_header({
HTTPverb=>$verb,
data=>$data,
path=>$path,
});
if ( $newline ){
print "\n";
}

View File

@ -8,7 +8,7 @@ use Crypt::Mac::HMAC qw( hmac_b64 );
=head1 NAME
LogicMonitor::REST::Signature - The great new LogicMonitor::REST::Signature!
LogicMonitor::REST::Signature - Generate signatures and authheader info for the Logicmonitor REST API.
=head1 VERSION

19
t/sig.t
View File

@ -50,10 +50,25 @@ eval{
};
ok( $worked eq '1', 'signature 0') or diag("Failed to create the expected signature... ".$@);
# make sure it can generate a known one with no data, which requires a time stamp
$worked=0;
eval{
my $sig=$lmsig_helper->signature({
HTTPverb=>'GET',
path=>'/foo',
timestamp=>'1',
});
if ( $sig ne 'e0bb5OESDeQdMvtJy1Nr6Nju7Nd9axVXHUhMQjjA3f4=' ){
die 'Got "'.$sig.'" but was expecting "e0bb5OESDeQdMvtJy1Nr6Nju7Nd9axVXHUhMQjjA3f4="';
}
$worked=1
};
ok( $worked eq '1', 'signature 1') or diag("Failed to create the expected signature... ".$@);
# tests if it can call auth_header and generate a valid signature
$worked=0;
eval{
my $auth_header=$lmsig_helper->signature({
my $auth_header=$lmsig_helper->auth_header({
HTTPverb=>'GET',
path=>'/foo',
data=>'',
@ -65,4 +80,4 @@ eval{
};
ok( $worked eq '1', 'auth_header 0') or diag("Failed to create a auth_header... ".$@);
done_testing(5);
done_testing(6);