Store metadata in raw format (#104)

This commit is contained in:
Clément Oudot 2010-06-25 13:51:09 +00:00
parent edaec866d0
commit d114827e70
5 changed files with 125 additions and 88 deletions

View File

@ -14,8 +14,7 @@ use XML::Simple;
use Safe; use Safe;
use Encode; use Encode;
our $VERSION = '0.1'; our $VERSION = '0.2';
our $DEBUG = 0;
## @cmethod Lemonldap::NG::Common::Conf::SAML::Metadata new(hashRef args) ## @cmethod Lemonldap::NG::Common::Conf::SAML::Metadata new(hashRef args)
# Class constructor. # Class constructor.
@ -33,19 +32,6 @@ sub new {
return $self; return $self;
} }
## @method void setDebug(boolean debug)
# Set debug flag
# @param boolean debug value
# @return nothing
sub setDebug {
my $self = shift;
my $debug = shift || 1;
$DEBUG = $debug;
return;
}
## @method public boolean initiliazeFromConf(string s) ## @method public boolean initiliazeFromConf(string s)
# Initialize this object from configuration string. # Initialize this object from configuration string.
# @param $s Configuration string. # @param $s Configuration string.

View File

@ -3,50 +3,98 @@ package Lemonldap::NG::Common::Conf::Serializer;
use Data::Dumper; use Data::Dumper;
BEGIN { BEGIN {
*Lemonldap::NG::Common::Conf::normalize = \&normalize;
*Lemonldap::NG::Common::Conf::unnormalize = \&unnormalize;
*Lemonldap::NG::Common::Conf::serialize = \&serialize; *Lemonldap::NG::Common::Conf::serialize = \&serialize;
*Lemonldap::NG::Common::Conf::unserialize = \&unserialize; *Lemonldap::NG::Common::Conf::unserialize = \&unserialize;
} }
## @method string normalize(string value)
# Change quotes, spaces and line breaks
# @param value Input value
# @return normalized string
sub normalize {
my ( $self, $value ) = splice @_;
# trim white spaces
$value =~ s/^\s*(.*?)\s*$/$1/;
# Convert carriage returns (\r) and line feeds (\n)
$value =~ s/\r/%0D/g;
$value =~ s/\n/%0A/g;
# Convert simple quotes
$value =~ s/'/'/g;
# Surround with simple quotes
$value = "'$value'" unless ( $self->{noQuotes} );
return $value;
}
## @method string unnormalize(string value)
# Revert quotes, spaces and line breaks
# @param value Input value
# @return unnormalized string
sub unnormalize {
my ( $self, $value ) = splice @_;
# Convert simple quotes
$value =~ s/&#?39;/'/g;
# Convert carriage returns (\r) and line feeds (\n)
$value =~ s/%0D/\r/g;
$value =~ s/%0A/\n/g;
return $value;
}
## @method hashref serialize(hashref conf)
# Parse configuration and convert it into fields
# @param conf Configuration
# @return fields
sub serialize { sub serialize {
my ( $self, $conf ) = @_; my ( $self, $conf ) = splice @_;
my $fields; my $fields;
# Data::Dumper options
local $Data::Dumper::Indent = 0; local $Data::Dumper::Indent = 0;
local $Data::Dumper::Varname = "data"; local $Data::Dumper::Varname = "data";
# Parse configuration
while ( my ( $k, $v ) = each(%$conf) ) { while ( my ( $k, $v ) = each(%$conf) ) {
# Ignore reVhost and cipher
next if ( $k =~ /^(?:reVHosts|cipher)$/ ); next if ( $k =~ /^(?:reVHosts|cipher)$/ );
# 1.Hash ref
if ( ref($v) ) { if ( ref($v) ) {
$fields->{$k} = Dumper($v); $fields->{$k} = $self->normalize( Dumper($v) );
$fields->{$k} =~ s/'/'/g;
$fields->{$k} = "'$fields->{$k}'" unless ( $self->{noQuotes} );
} }
# 2. Numeric values
elsif ( $v =~ /^\d+$/ ) { elsif ( $v =~ /^\d+$/ ) {
$fields->{$k} = "$v"; $fields->{$k} = "$v";
} }
# 3. Standard values
else { else {
$fields->{$k} = $self->normalize($v);
# trim white spaces
$v =~ s/^\s*(.*?)\s*$/$1/;
# Convert carriage returns (\r) and line feeds (\n)
$v =~ s/\r/%0D/g;
$v =~ s/\n/%0A/g;
# Convert simple quotes
$v =~ s/'/'/g;
# Surround with simple quotes
$v = "'$v'" unless ( $self->{noQuotes} );
# Store value in field
$fields->{$k} = $v;
} }
} }
return $fields; return $fields;
} }
## @method hashref unserialize(hashref fields)
# Convert fields into configuration
# @param fields Fields
# @return configuration
sub unserialize { sub unserialize {
my ( $self, $fields ) = @_; my ( $self, $fields ) = splice @_;
my $conf; my $conf;
# Parse fields
while ( my ( $k, $v ) = each(%$fields) ) { while ( my ( $k, $v ) = each(%$fields) ) {
# Remove surrounding quotes # Remove surrounding quotes
@ -59,34 +107,45 @@ sub unserialize {
and not ref($v) ) and not ref($v) )
{ {
$conf->{$k} = {}; $conf->{$k} = {};
# Value should be a Data::Dumper, else this is an old format
if ( defined($v) and $v !~ /^\$/ ) { if ( defined($v) and $v !~ /^\$/ ) {
print STDERR
"Lemonldap::NG : Warning: configuration is in old format, you've to migrate !\n"; $msg .=
" Warning: configuration is in old format, you've to migrate!";
eval { require Storable; require MIME::Base64; }; eval { require Storable; require MIME::Base64; };
if ($@) { if ($@) {
$msg = "Error : $@"; $msg .= " Error: $@";
return 0; return 0;
} }
$conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) ); $conf->{$k} = Storable::thaw( MIME::Base64::decode_base64($v) );
} }
# Convert Data::Dumper
else { else {
my $data; my $data;
$v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/; $v =~ s/^\$([_a-zA-Z][_a-zA-Z0-9]*) *=/\$data =/;
$v =~ s/&#?39;/'/g; $v = $self->unnormalize($v);
# Evaluate expression
eval $v; eval $v;
print STDERR
"Lemonldap::NG : Error while reading configuration with $k key: $@\n" if ($@) {
if ($@); $msg .= " Error: cannot read configuration key $k: $@";
}
# Store value in configuration object
$conf->{$k} = $data; $conf->{$k} = $data;
} }
} }
# Other fields type
else { else {
$v =~ s/&#?39;/'/g; $conf->{$k} = $self->unnormalize($v);
$v =~ s/%0D/\r/g;
$v =~ s/%0A/\n/g;
$conf->{$k} = $v;
} }
} }
return $conf; return $conf;
} }

View File

@ -189,15 +189,25 @@ sub confNode {
my $h = $self->keyToH( $target, $self->conf ); my $h = $self->keyToH( $target, $self->conf );
$h = $h->{samlIDPMetaDataXML} if ( $h->{samlIDPMetaDataXML} ); $h = $h->{samlIDPMetaDataXML} if ( $h->{samlIDPMetaDataXML} );
$h = $h->{samlSPMetaDataXML} if ( $h->{samlSPMetaDataXML} ); $h = $h->{samlSPMetaDataXML} if ( $h->{samlSPMetaDataXML} );
my $data;
# Manage old metadata format
if ( ref($h) eq "HASH" ) {
$self->lmLog( "Convert metadata from old format", 'debug' );
my $metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new(); my $metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new();
$metadata->initializeFromConfHash($h); $metadata->initializeFromConfHash($h);
$data = $metadata->toXML();
}
else {
$data = $h;
}
my $text = $target; my $text = $target;
$text =~ s/^\/([^\/]+)\/.*$/$1/; $text =~ s/^\/([^\/]+)\/.*$/$1/;
$res .= $self->li("$target/") $res .= $self->li("$target/")
. $self->span( . $self->span(
id => "$target/", id => "$target/",
text => $text, text => $text,
data => $metadata->toXML(), data => $data,
js => $js, js => $js,
help => $help, help => $help,
target => "samlmetadata", target => "samlmetadata",

View File

@ -506,18 +506,9 @@ sub findAllConfKeys {
# @return A formated value. # @return A formated value.
sub formatValue { sub formatValue {
my ( $self, $key, $value ) = @_; my ( $self, $key, $value ) = @_;
my $newvalue = $value;
if ( $key =~ /^(samlIDPMetaDataXML|samlSPMetaDataXML)/ ) { # Not used now
my $metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new(); return $value;
if ( ref($value) ) {
$metadata->initializeFromConfHash($value);
}
else {
$metadata->initializeFromXML($value);
}
$newvalue = $metadata->toHash();
}
return $newvalue;
} }
## @method protected void setKeyToH(hashref h,string key,string k2,string value) ## @method protected void setKeyToH(hashref h,string key,string k2,string value)

View File

@ -165,22 +165,18 @@ sub loadIDPs {
$self->lmLog( "Get Metadata for IDP $_", 'debug' ); $self->lmLog( "Get Metadata for IDP $_", 'debug' );
# Get metadata from configuration my $idp_metadata =
my $idp_metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new(); $self->{samlIDPMetaDataXML}->{$_}->{samlIDPMetaDataXML};
unless (
$idp_metadata->initializeFromConfHash( # Check metadata format
$self->{samlIDPMetaDataXML}->{$_}->{samlIDPMetaDataXML} if ( ref $idp_metadata eq "HASH" ) {
) $self->abort(
) "Metadata for IDP $_ is in old format. Please reload them from Manager"
{ );
$self->lmLog( "Fail to read IDP $_ Metadata from configuration",
'error' );
return 0;
} }
# Add this IDP to Lasso::Server # Add this IDP to Lasso::Server
my $result = my $result = $self->addIDP( $self->{_lassoServer}, $idp_metadata );
$self->addIDP( $self->{_lassoServer}, $idp_metadata->toXML() );
unless ($result) { unless ($result) {
$self->lmLog( "Fail to use IDP $_ Metadata", 'error' ); $self->lmLog( "Fail to use IDP $_ Metadata", 'error' );
@ -188,7 +184,7 @@ sub loadIDPs {
} }
# Store IDP entityID and Organization Name # Store IDP entityID and Organization Name
my $entityID = $idp_metadata->{entityID}; my ($entityID) = ( $idp_metadata =~ /entityID="(.+?)"/i );
my $name = my $name =
$self->getOrganizationName( $self->{_lassoServer}, $entityID ) $self->getOrganizationName( $self->{_lassoServer}, $entityID )
|| ucfirst($_); || ucfirst($_);
@ -248,22 +244,17 @@ sub loadSPs {
$self->lmLog( "Get Metadata for SP $_", 'debug' ); $self->lmLog( "Get Metadata for SP $_", 'debug' );
# Get metadata from configuration my $sp_metadata = $self->{samlSPMetaDataXML}->{$_}->{samlSPMetaDataXML};
my $sp_metadata = Lemonldap::NG::Common::Conf::SAML::Metadata->new();
unless ( # Check metadata format
$sp_metadata->initializeFromConfHash( if ( ref $sp_metadata eq "HASH" ) {
$self->{samlSPMetaDataXML}->{$_}->{samlSPMetaDataXML} $self->abort(
) "Metadata for SP $_ is in old format. Please reload them from Manager"
) );
{
$self->lmLog( "Fail to read SP $_ Metadata from configuration",
'error' );
return 0;
} }
# Add this SP to Lasso::Server # Add this SP to Lasso::Server
my $result = my $result = $self->addSP( $self->{_lassoServer}, $sp_metadata );
$self->addSP( $self->{_lassoServer}, $sp_metadata->toXML() );
unless ($result) { unless ($result) {
$self->lmLog( "Fail to use SP $_ Metadata", 'error' ); $self->lmLog( "Fail to use SP $_ Metadata", 'error' );
@ -271,7 +262,7 @@ sub loadSPs {
} }
# Store SP entityID and Organization Name # Store SP entityID and Organization Name
my $entityID = $sp_metadata->{entityID}; my ($entityID) = ( $sp_metadata =~ /entityID="(.+?)"/i );
my $name = my $name =
$self->getOrganizationName( $self->{_lassoServer}, $entityID ) $self->getOrganizationName( $self->{_lassoServer}, $entityID )
|| ucfirst($_); || ucfirst($_);