update the test and finish off the helper script

This commit is contained in:
Zane C. B-H 2020-12-02 04:51:14 -06:00
parent 7fe95d5194
commit 2f428a3878
2 changed files with 144 additions and 56 deletions

View File

@ -12,6 +12,14 @@ sub version{
sub help{
print '
--verb <HTTP verb> The HTTP verb in question. Either GET or PUT.
--path <API path> The API resource path. Such as /device/devices
--data <data file> If doing a put, this is a path to a file containing the data that the put contains.
-n Print a newline character at the end.
-c Print "Authorizion: " with the string for curl.
-q Print it quoted.
--version Show the version info.
--help Print this message.
';
}
@ -29,7 +37,6 @@ foreach my $key ( keys %{$required_args} ) {
# initiate the helper
my $lmsig_helper=LogicMonitor::REST::Signature->new({
company=>$ENV{Logicmonitor_company},
accessKey=>$ENV{Logicmonitor_accessKey},
accessID=>$ENV{Logicmonitor_accessID},
});
@ -42,15 +49,17 @@ my $path;
my $version;
my $help;
my $newline;
my $quoted;
my $curl;
GetOptions(
'verb=s' => \$verb,
'data=s' => \$file,
'path=s' => \$path,
'v' => \$version,
'version' => \$version,
'h' => \$help,
'help' => \$help,
'n'=>\$newline,
'n' => \$newline,
'q' => \$quoted,
'c' => \$curl,
);
# print the version or help info if requested
@ -74,6 +83,15 @@ if ($file) {
close( $fh );
}
# add the quotes if '-q' is given
if ($quoted) {
print "'";
}
if ($curl) {
print "Authorization: ";
}
# create the auth header data with the sig included
print $lmsig_helper->auth_header({
HTTPverb=>$verb,
@ -81,6 +99,67 @@ print $lmsig_helper->auth_header({
path=>$path,
});
# add the quotes if '-q' is given
if ($quoted) {
print "'";
}
if ( $newline ){
print "\n";
}
=head1 NAME
lm-auth-helper - Builds LMv1 token header info for Logicmonitor.
=head1 SYNOPSIS
lm-auth-helper B<--path> <path> [B<--verb> <HTTP verb>] [B<--data> <file>] [B<-c>] [B<-q>]
=head1 DESCRIPTION
This puts together the auth token header for Logicmonitor.
This requires the following enviromentalvalues to be set.
Logicmonitor_accessKey
Logicmonitor_accessID
Those are respectively the access key and access ID for the REST API.
=head1 SWITCHES
=head2 --verb <HTTP verb>
The HTTP verb in question. Either GET or PUT.
=head2 --path <API path>
The API resource path. Such as /device/devices
=head2 --data <data file>
If doing a put, this is a path to a file containing the data that the put contains.
=head2 -n
Print a newline character at the end.
=head2 -c
Print "Authorizion: " with the string for curl.
=head2 -q
Print it quoted.
=head2 --version
Show the version info.
=head2 --help
Print this message.
=cut

113
t/sig.t
View File

@ -5,78 +5,87 @@ use warnings;
use Test::More;
BEGIN {
use_ok( 'LogicMonitor::REST::Signature' );
use_ok('LogicMonitor::REST::Signature');
}
my $company='foo';
my $accessKey='some key';
my $accessID='some ID';
my $company = 'foo';
my $accessKey = 'some key';
my $accessID = 'some ID';
# make sure it errors when undef or missing values
my $worked=0;
my $worked = 0;
my $lmsig_helper;
eval{
$lmsig_helper=LogicMonitor::REST::Signature->new({
});
$worked=1
eval {
$lmsig_helper = LogicMonitor::REST::Signature->new( {} );
$worked = 1;
};
ok( $worked eq '0', 'init') or diag("Iinitated with missing values");
ok( $worked eq '0', 'init' ) or diag("Iinitated with missing values");
# make sure we init it
$worked=0;
eval{
$lmsig_helper=LogicMonitor::REST::Signature->new({
accessKey=>$accessKey,
accessID=>$accessID,
});
$worked=1
$worked = 0;
eval {
$lmsig_helper = LogicMonitor::REST::Signature->new(
{
accessKey => $accessKey,
accessID => $accessID,
}
);
$worked = 1;
};
ok( $worked eq '1', 'init') or diag("Failed to init the object... ".$@);
ok( $worked eq '1', 'init' ) or diag( "Failed to init the object... " . $@ );
# make sure it can generate a known one, which requires a time stamp
$worked=0;
eval{
my $sig=$lmsig_helper->signature({
HTTPverb=>'GET',
path=>'/foo',
data=>'',
timestamp=>'1',
});
if ( $sig ne 'N2I0NmRiZTRlMTEyMGRlNDFkMzJmYjQ5Y2I1MzZiZThkOGVlZWNkNzdkNmIxNTU3MWQ0ODRjNDIzOGMwZGRmZQ==' ){
die 'Got "'.$sig.'" but was expecting "N2I0NmRiZTRlMTEyMGRlNDFkMzJmYjQ5Y2I1MzZiZThkOGVlZWNkNzdkNmIxNTU3MWQ0ODRjNDIzOGMwZGRmZQ=="';
$worked = 0;
eval {
my $sig = $lmsig_helper->signature(
{
HTTPverb => 'GET',
path => '/foo',
data => '',
timestamp => '1',
}
);
if ( $sig ne 'N2I0NmRiZTRlMTEyMGRlNDFkMzJmYjQ5Y2I1MzZiZThkOGVlZWNkNzdkNmIxNTU3MWQ0ODRjNDIzOGMwZGRmZQ==' ) {
die 'Got "' . $sig
. '" but was expecting "N2I0NmRiZTRlMTEyMGRlNDFkMzJmYjQ5Y2I1MzZiZThkOGVlZWNkNzdkNmIxNTU3MWQ0ODRjNDIzOGMwZGRmZQ=="';
}
$worked=1
$worked = 1;
};
ok( $worked eq '1', 'signature 0') or diag("Failed to create the expected signature... ".$@);
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 'N2I0NmRiZTRlMTEyMGRlNDFkMzJmYjQ5Y2I1MzZiZThkOGVlZWNkNzdkNmIxNTU3MWQ0ODRjNDIzOGMwZGRmZQ==' ){
die 'Got "'.$sig.'" but was expecting "N2I0NmRiZTRlMTEyMGRlNDFkMzJmYjQ5Y2I1MzZiZThkOGVlZWNkNzdkNmIxNTU3MWQ0ODRjNDIzOGMwZGRmZQ=="';
$worked = 0;
eval {
my $sig = $lmsig_helper->signature(
{
HTTPverb => 'GET',
path => '/foo',
timestamp => '1',
}
);
if ( $sig ne 'N2I0NmRiZTRlMTEyMGRlNDFkMzJmYjQ5Y2I1MzZiZThkOGVlZWNkNzdkNmIxNTU3MWQ0ODRjNDIzOGMwZGRmZQ==' ) {
die 'Got "' . $sig
. '" but was expecting "N2I0NmRiZTRlMTEyMGRlNDFkMzJmYjQ5Y2I1MzZiZThkOGVlZWNkNzdkNmIxNTU3MWQ0ODRjNDIzOGMwZGRmZQ=="';
}
$worked=1
$worked = 1;
};
ok( $worked eq '1', 'signature 1') or diag("Failed to create the expected signature... ".$@);
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->auth_header({
HTTPverb=>'GET',
path=>'/foo',
data=>'',
});
if ( $auth_header !~ /^LMv1\ .*\:[a-zA-Z0-9\+\/\=]*:[0-9]+$/ ){
die 'Got "'.$auth_header.'" but was expecting "e0bb5OESDeQdMvtJy1Nr6Nju7Nd9axVXHUhMQjjA3f4="';
$worked = 0;
eval {
my $auth_header = $lmsig_helper->auth_header(
{
HTTPverb => 'GET',
path => '/foo',
data => '',
}
);
if ( $auth_header !~ /^LMv1\ .*\:[a-zA-Z0-9\+\/\=]*:[0-9]+$/ ) {
die 'Got "' . $auth_header . '" but was expecting "e0bb5OESDeQdMvtJy1Nr6Nju7Nd9axVXHUhMQjjA3f4="';
}
$worked=1
$worked = 1;
};
ok( $worked eq '1', 'auth_header 0') or diag("Failed to create a auth_header... ".$@);
ok( $worked eq '1', 'auth_header 0' ) or diag( "Failed to create a auth_header... " . $@ );
done_testing(6);