Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow underscore override per RR #146

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions etc/Default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@ IGNORE_IPV6_LINK_LOCAL => 0,
# to 1 will allow underscores anywhere in a device name.
ALLOW_UNDERSCORES_IN_DEVICE_NAMES => 0,

# Allow resource records (RR) to contain underscores by
# giving the web user a checkbox to override the underscore
# validation. This allows the default and common case of
# not allowing underscores to exist while still permitting the
# local user to override and allow a record with an underscore.
# This is useful for text (TXT) or service (SRV) records.
UNDERSCORES_IN_DEVICE_NAMES_OVERRIDE_ALLOWED => 0,

#####################################################################
#
# IP Address Space Management
Expand Down
6 changes: 6 additions & 0 deletions htdocs/management/host.html
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@
push( @field_headers, $tmp{label} );
push( @cell_data, $tmp{value} );

my $tmp_underscore_override = {};
if ($ui->underscore_override_form_field($tmp_underscore_override)) {
push(@field_headers, $tmp_underscore_override->{label});
push(@cell_data, $tmp_underscore_override->{value});
}

$m->comp('/generic/attribute_table.mhtml', %comphash);
(@field_headers, @cell_data) = ((),());

Expand Down
152 changes: 89 additions & 63 deletions lib/Netdot/Model/RR.pm
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,11 @@ sub insert {
expiration => $argv->{expiration},
info => $argv->{info},
);
my $extra_arguments = {
allow_underscore_override => $argv->{allow_underscore_override},
};

$class->_validate_args(\%state);
$class->_validate_args(\%state, $extra_arguments);

$rr = $class->SUPER::insert(\%state);

Expand Down Expand Up @@ -718,76 +721,99 @@ sub add_alias {
############################################################################
# _validate_args - Validate arguments to insert and update
#
# Args:
# hashref
# Returns:
# Args:
# argv - hashref containing database field and values
# extra_argv - (optional) hashref containing extra parameters
# Returns:
# True, or throws exception if validation fails
# Examples:
# $class->_validate_args($argv);
#
sub _validate_args {
my ($self, $argv) = @_;

my ($self, $argv, $extra_argv) = @_;

if (! defined $extra_argv) {
$extra_argv = {};
}

my $zone;
if (ref($self)){
$zone = $self->zone;
if (ref($self)) {
$zone = $self->zone;
}
if ( defined $argv->{zone} ){
if ( ref($argv->{zone}) ){
# We're being passed an object
$zone = $argv->{zone}
}else{
if ( $argv->{zone} =~ /\D+/ ){
$zone = Zone->search(name=>$argv->{zone})->first;
}else{
$zone = Zone->retrieve($argv->{zone});
}
}
if (defined $argv->{zone}) {
if (ref($argv->{zone})) {
# We're being passed an object
$zone = $argv->{zone};
}
else {
if ($argv->{zone} =~ /\D+/){
$zone = Zone->search(name=>$argv->{zone})->first;
}
else {
$zone = Zone->retrieve($argv->{zone});
}
}
}
if ( defined $argv->{name} ){
# Convert to lowercase
my $name = lc($argv->{name});

# Remove whitespace
$name =~ s/\s+//g;

# Remove trailing dots, if any
$name =~ s/\.$//;

# Remove commas
$name =~ s/,//;

# Valid characters
if ( $name =~ /[^A-Za-z0-9\.\-_@\*]/ ){
$self->throw_user("Invalid name: $name. Contains invalid characters");
}

if ( $self->config->get('ALLOW_UNDERSCORES_IN_DEVICE_NAMES') eq '0' ){
# Underscore only allowed at beginning of string or dotted section
if ( $name =~ /[^^.]_/ || $name =~ /_$/ ){
$self->throw_user("Invalid name: $name. Invalid underscores");
}
}

# Name must not start or end with a dash
if ( $name =~ /^\-/ || $name =~ /\-$/ ){
$self->throw_user("Invalid name: $name. Name must not start or end with a dash");
}

# Length restrictions (RFC 1035)
my $fqdn = $name.".".$zone->name;
if ( length($fqdn) > 255 ){
$self->throw_user("Invalid FQDN: $fqdn. Length exceeds 255 characters");
}
# labels (sections between dots) must not exceed 63 chars
foreach my $label ( split(/\./, $fqdn) ){
unless ( length($label) >= 1 && length($label) < 64 ){
$self->throw_user(sprintf("RR::validate_args(): '%s' has Invalid label: '%s'. ".
"Each label must be between 1 and 63 characters long",
$fqdn, $label));
}
}
$argv->{name} = $name;
if (defined $argv->{name}) {
# Convert to lowercase
my $name = lc($argv->{name});

# Remove whitespace
$name =~ s/\s+//g;

# Remove trailing dots, if any
$name =~ s/\.$//;

# Remove commas
$name =~ s/,//;

# Valid characters
if ($name =~ /[^A-Za-z0-9\.\-_@\*]/) {
$self->throw_user("Invalid name: $name. Contains invalid characters");
}

if (
$self->config->get('ALLOW_UNDERSCORES_IN_DEVICE_NAMES') eq '1'
||
(
$self->config->get('UNDERSCORES_IN_DEVICE_NAMES_OVERRIDE_ALLOWED') eq '1'
&&
$extra_argv->{allow_underscore_override}
)
) {
$logger->debug('Allow underscores in device name. No underscore validation check.');
}
else {
# Underscore only allowed at beginning of string or dotted section
if ($name =~ /[^^.]_/ || $name =~ /_$/){
$self->throw_user("Invalid name: $name. Invalid underscores");
}
}

# Name must not start or end with a dash
if ($name =~ /^\-/ || $name =~ /\-$/) {
$self->throw_user("Invalid name: $name. Name must not start or end with a dash");
}

# Length restrictions (RFC 1035)
my $fqdn = $name.".".$zone->name;
if (length($fqdn) > 255) {
$self->throw_user("Invalid FQDN: $fqdn. Length exceeds 255 characters");
}
# labels (sections between dots) must not exceed 63 chars
for my $label (split(/\./, $fqdn)){
unless (length($label) >= 1 && length($label) < 64) {
$self->throw_user(
sprintf(
"RR::validate_args(): '%s' has Invalid label: '%s'. ".
"Each label must be between 1 and 63 characters long",
$fqdn,
$label,
)
);
}
}
$argv->{name} = $name;
}
1;
}
Expand Down
47 changes: 47 additions & 0 deletions lib/Netdot/UI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,53 @@ sub form_field {
}
}


############################################################################

=head2 underscore_override_form_field - Return if Underscore Override checkbox if enabled.

This method will populate a hashref with a label and checkbox to allow the
user to override the underscore validation for names.
If ALLOW_UNDERSCORES_IN_DEVICE_NAMES is set to true, then there is no
need to return the checkbox since underscores are allowed at the global
level.
If UNDERSCORES_IN_DEVICE_NAMES_OVERRIDE_ALLOWED is set to true then
return the checkbox to allow the user to override the underscore check
at a per device name.

Arguments:
hashref to populate with label and value
Returns:
Boolean true or false if the calling code should add label and value
to the UI.

Examples:

my $tmp = {};
if ($ui->underscore_override_form_field($tmp)) {
# do something with $tmp
}

=cut

sub underscore_override_form_field {
my $self = shift;
my $hash = shift;

if (
! $self->config->get('ALLOW_UNDERSCORES_IN_DEVICE_NAMES')
&&
$self->config->get('UNDERSCORES_IN_DEVICE_NAMES_OVERRIDE_ALLOWED')
) {
$hash->{label} = 'Allow underscores:';
$hash->{value} = '<input type="checkbox" name="RR__NEW__allow_underscore_override" value="1" />';
return 1;
}

return 0;
}


############################################################################

=head2 table_descr_link - Generate link to display a table\'s description
Expand Down