App/TenableSC/Utils.pm

filter: Statementstmt 27.1% 22 / 81 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 6 / 14 Podpod 0.0% 0 / 8 totaltotal 21.7% 28 / 129 CC 15 SCAR 32.8
CC
15
Cov
21%
config_parser
72.0
config_parse_line
20.0
cli_version
12.0
CRAP 26.5
« Net/SecurityCenter/Utils.pm ↑ Summary Net/SecurityCenter/REST.pm »
1 package App::TenableSC::Utils;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.310';
13  
14 1 our @EXPORT_OK = qw(
15  
16 config_parse_line
17 config_parser
18  
19 file_slurp
20 trim
21 dumper
22  
23 cli_version
24 cli_error
25 cli_readkey
26  
27 );
28  
29 1 our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK );
30  
31 sub cli_error {
32  
33 0 ▶ my ($error) = @_;
Subroutine cli_error: not called CC=1 SCAR=6.9
Pod undocumented
34 0 $error =~ s/ at .* line \d+.*//;
35  
36 0 my $script_name = basename($0);
37  
38 0 print "$script_name ERROR: $error\n";
39  
40 0 exit(255);
41  
42 }
43  
44 sub cli_readkey {
45  
46 0 ▶ my ($message) = @_;
Subroutine cli_readkey: not called CC=1 SCAR=6.9
Pod undocumented
47  
48 0 my $value = undef;
49  
50 0 print $message;
51 0 ReadMode 'noecho';
52  
53 0 $value = ReadLine 0;
54 0 chomp $value;
55  
56 0 ReadMode 'normal';
57 0 print "\n";
58  
59 0 return $value;
60  
61 }
62  
63 sub cli_version {
64  
65 0 ▶ require IO::Socket::SSL;
Subroutine cli_version: not called CC=3 SCAR=24.8
Pod undocumented
66 0 require LWP::UserAgent;
67 0 require Net::SecurityCenter;
68 0 require App::TenableSC;
69  
70 0 ▶ my $io_socket_ssl = ($IO::Socket::SSL::VERSION) ? $IO::Socket::SSL::VERSION : 'n/a';
Branch: 1 branch
BranchTrueFalseTotal
$IO::Socket::SSL::VERSION ? : 0 0 0
71 0 ▶ my $lwp_useragent = ($LWP::UserAgent::VERSION) ? $LWP::UserAgent::VERSION : 'n/a';
Branch: 1 branch
BranchTrueFalseTotal
$LWP::UserAgent::VERSION ? : 0 0 0
72 0 my $script_name = basename($0);
73  
74 0 print <<"EOF";
75 $script_name v$VERSION
76  
77 CORE
78 Perl ($^V, $^O)
79 Net::SecurityCenter ($Net::SecurityCenter::VERSION)
80 App::TenableSC ($App::TenableSC::VERSION)
81  
82 MODULES
83 LWP::UserAgent ($lwp_useragent)
84 IO::Socket::SSL ($io_socket_ssl)
85  
86 EOF
87  
88 0 exit 0;
89  
90 }
91  
92 sub trim {
93 0 ▶ Net::SecurityCenter::Utils::trim(@_);
Subroutine trim: not called CC=1 SCAR=6.9
Pod undocumented
94 }
95  
96 sub dumper {
97 0 ▶ Net::SecurityCenter::Utils::dumper(@_);
Subroutine dumper: not called CC=1 SCAR=6.9
Pod undocumented
98 }
99  
100 sub file_slurp {
101  
102 0 ▶ my ($file) = @_;
Subroutine file_slurp: not called CC=3 SCAR=24.8
Pod undocumented
103  
104 0 ▶ open my $fh, '<', $file or croak("Can't open $file file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Can't open $file file: $!") 0 0 0
105  
106 0 my $string = do {
107 0 local $/ = undef;
108 0 <$fh>;
109 };
110  
111 0 ▶ close $fh or croak("Failed to close $file file: $!");
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Failed to close $file file: $!") 0 0 0
112  
113 0 return $string;
114  
115 }
116  
117 #-------------------------------------------------------------------------------
118  
119 sub config_parse_line {
120  
121 0 ▶ my ($value) = @_;
Subroutine config_parse_line: not called CC=4 SCAR=30.0
Pod undocumented
122  
123 0 ▶ return 1 if ( $value =~ m/^(yes|true)$/s );
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^(yes|true)$/s 0 0 0
124 0 ▶ return 0 if ( $value =~ m/^(no|false)$/s );
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^(no|false)$/s 0 0 0
125  
126 0 ▶ if ( $value =~ /\,/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\,/) 0 0 0
127 0 return map { trim($_) } split /,/, $value;
128 }
129  
130 0 return $value;
131  
132 }
133  
134 #-------------------------------------------------------------------------------
135  
136 sub config_parser {
137  
138 0 ▶ my ($config_string) = @_;
Subroutine config_parser: not called CC=8 SCAR=42.8
Pod undocumented
139  
140 0 my $section = '_'; # Root section
141 0 my $config_data = {};
142  
143 0 foreach my $line ( split /\n/, $config_string ) {
144  
145 0 chomp($line);
146  
147 # skip comments and empty lines
148 0 ▶ next if ( $line =~ m/^\s*([#;])/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*([#;])/ 0 0 0
149 0 ▶ next if ( $line =~ m/^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
150  
151 0 ▶ if ( $line =~ m/^\[(.*)\]\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\[(.*)\]\s*$/) 0 0 0
152 0 $section = trim($1);
153 0 next;
154 }
155  
156 0 ▶ if ( $line =~ m/^([^=]+?)\s*=\s*(.*?)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^=]+?)\s*=\s*(.*?)\s*$/) 0 0 0
157  
158 0 my ( $field, $raw_value ) = ( $1, $2 );
159 0 my $parsed_value = [ config_parse_line($raw_value) ];
160  
161 0 ▶ my $value = ( ( @{$parsed_value} == 1 ) ? $parsed_value->[0] : $parsed_value );
Branch: 1 branch
BranchTrueFalseTotal
@{$parsed_value;} == 1 ? : 0 0 0
162  
163 0 ▶ if ( not defined $section ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $section) 0 0 0
164 0 $config_data->{$field} = $value;
165 0 next;
166 }
167  
168 0 $config_data->{$section}->{$field} = $value;
169  
170 }
171  
172 }
173  
174 0 return $config_data;
175  
176 }
177  
178 1 1;
179  
180 =pod
181  
182 =encoding UTF-8
183  
184  
185 =head1 NAME
186  
187 App::TenableSC::Utils - Utilities for App::TenableSC
188  
189  
190 =head1 SYNOPSIS
191  
192 use App::TenableSC::Utils qw(:all);
193  
194 print dumper( { 'foo' => 'bar' } );
195  
196  
197 =head1 DESCRIPTION
198  
199 This module provides Perl scripts easy way to interface the REST API of Tenable.sc
200 (SecurityCenter).
201  
202 For more information about the Tenable.sc (SecurityCenter) REST API follow the online documentation:
203  
204 L<https://docs.tenable.com/sccv/api/index.html>
205  
206  
207 =head1 METHODS
208  
209  
210 =head1 SUPPORT
211  
212 =head2 Bugs / Feature Requests
213  
214 Please report any bugs or feature requests through the issue tracker
215 at L<https://github.com/giterlizzi/perl-Net-SecurityCenter/issues>.
216 You will be notified automatically of any progress on your issue.
217  
218 =head2 Source Code
219  
220 This is open source software. The code repository is available for
221 public review and contribution under the terms of the license.
222  
223 L<https://github.com/giterlizzi/perl-Net-SecurityCenter>
224  
225 git clone https://github.com/giterlizzi/perl-Net-SecurityCenter.git
226  
227  
228 =head1 AUTHOR
229  
230 =over 4
231  
232 =item * Giuseppe Di Terlizzi <gdt@cpan.org>
233  
234 =back
235  
236  
237 =head1 LICENSE AND COPYRIGHT
238  
239 This software is copyright (c) 2018-2021 by Giuseppe Di Terlizzi.
240  
241 This is free software; you can redistribute it and/or modify it under
242 the same terms as the Perl 5 programming language system itself.
243  
244 =cut
« Net/SecurityCenter/Utils.pm ↑ Summary Net/SecurityCenter/REST.pm »