2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-09-04 20:17:55 +00:00

refactor: centralize policy string trimming

This commit is contained in:
Vinícius Ferrão
2026-08-25 12:41:24 -03:00
parent 593e349557
commit 751d237c13
5 changed files with 36 additions and 12 deletions
+3 -1
View File
@@ -3,13 +3,15 @@ package xCAT::DHCP::Backend;
use strict;
use warnings;
use xCAT::StringUtils qw(trim);
my %valid_backend = map { $_ => 1 } qw(auto isc kea);
sub normalize {
my ( $class, $backend ) = @_;
$backend = 'auto' unless defined($backend) && $backend ne '';
$backend =~ s/^\s+|\s+$//g;
$backend = trim($backend);
$backend = lc($backend);
return $backend if $valid_backend{$backend};
+5 -3
View File
@@ -3,6 +3,8 @@ package xCAT::DHCP::OmapiPolicy;
use strict;
use warnings;
use xCAT::StringUtils qw(trim);
my %ALGORITHMS = (
'hmac-md5' => 157,
'hmac-sha1' => 161,
@@ -57,7 +59,7 @@ sub normalize_algorithm {
my ( $class, $algorithm ) = @_;
$algorithm = 'hmac-md5' unless defined($algorithm) && $algorithm ne '';
$algorithm =~ s/^\s+|\s+$//g;
$algorithm = trim($algorithm);
$algorithm = lc($algorithm);
return $algorithm if $ALGORITHMS{$algorithm};
@@ -86,7 +88,7 @@ sub normalize_key_name {
my ( $class, $key_name ) = @_;
$key_name = 'xcat_key' unless defined($key_name) && $key_name ne '';
$key_name =~ s/^\s+|\s+$//g;
$key_name = trim($key_name);
return $key_name if $key_name =~ /\A[A-Za-z0-9_][A-Za-z0-9_.-]*\z/;
return;
@@ -96,7 +98,7 @@ sub normalize_omshell_path {
my ( $class, $path ) = @_;
$path = '/usr/bin/omshell' unless defined($path) && $path ne '';
$path =~ s/^\s+|\s+$//g;
$path = trim($path);
return $path if $path =~ m{\A/[A-Za-z0-9_.:/%+=@-]+\z};
return;
+2 -1
View File
@@ -6,6 +6,7 @@ use warnings;
use Math::BigInt;
use Socket;
use xCAT::NetworkUtils qw/getipaddr/;
use xCAT::StringUtils qw(trim);
sub parse_dynamic_ranges {
my ( $class, $ranges ) = @_;
@@ -25,7 +26,7 @@ sub parse {
my ( $class, $range ) = @_;
return unless defined($range);
$range =~ s/^\s+|\s+$//g;
$range = trim($range);
return unless $range ne '';
if ( $range =~ m{/} ) {
+20
View File
@@ -0,0 +1,20 @@
# IBM(c) 2026 EPL license http://www.eclipse.org/legal/epl-v10.html
package xCAT::StringUtils;
use strict;
use warnings;
use Exporter qw(import);
our @EXPORT_OK = qw(trim);
sub trim {
my ($value) = @_;
return $value unless defined($value);
$value =~ s/^\s+|\s+$//g;
return $value;
}
1;
+6 -7
View File
@@ -5,6 +5,7 @@ use strict;
use warnings;
use Exporter qw(import);
use xCAT::StringUtils qw(trim);
our @EXPORT_OK = qw(
MODERN_TLS_VERSION
@@ -23,14 +24,12 @@ sub _site_value {
return '' unless $site && defined $site->{$key};
my $value = $site->{$key};
$value =~ s/^\s+|\s+$//g;
return $value;
return trim($site->{$key});
}
sub _normalize_policy {
my $policy = shift || '';
$policy =~ s/^\s+|\s+$//g;
$policy = trim($policy);
return lc($policy);
}
@@ -71,7 +70,7 @@ sub _enabled_protocols {
my %enabled;
foreach my $token (split /:/, $ssl_version) {
$token =~ s/^\s+|\s+$//g;
$token = trim($token);
next if $token eq '';
next if $token =~ /^!/;
$enabled{lc($token)} = 1;
@@ -84,7 +83,7 @@ sub _explicitly_disables {
my ($ssl_version, $protocol) = @_;
foreach my $token (split /:/, ($ssl_version || '')) {
$token =~ s/^\s+|\s+$//g;
$token = trim($token);
return 1 if lc($token) eq '!' . lc($protocol);
}
@@ -111,7 +110,7 @@ sub _deprecated_cipher_enabled {
my $ciphers = shift || '';
foreach my $token (split /:/, $ciphers) {
$token =~ s/^\s+|\s+$//g;
$token = trim($token);
next if $token eq '' || $token =~ /^!/;
return 1 if $token =~ /(?:^|[+_-])(?:3DES|DES-CBC3|RC4)(?:$|[+_-])/i;
return 1 if $token =~ /^(?:LOW|EXP|EXPORT)$/i;