Config-Model-Backend-Augeas-0.118000755001750001750 012402644254 15564 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/META.yml000444001750001750 152612402644254 17176 0ustar00domidomi000000000000--- abstract: 'Read and write configuration data through Augeas' author: - 'Dominique Dumont (ddumont at cpan dot org)' build_requires: Test::Differences: '0' Test::Exception: '0' Test::More: '0' Test::Warn: '0.11' version: '0.77' configure_requires: Module::Build: '0.36' dynamic_config: 1 generated_by: 'Module::Build version 0.4208, CPAN::Meta::Converter version 2.142060' license: lgpl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Config-Model-Backend-Augeas provides: Config::Model::Backend::Augeas: file: lib/Config/Model/Backend/Augeas.pm version: '0.118' requires: Config::Augeas: '0.303' Config::Model: '2.034' Exception::Class: '0' Log::Log4perl: '1.11' Mouse: '0' resources: license: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt version: '0.118' Config-Model-Backend-Augeas-0.118/MANIFEST000444001750001750 46512402644254 17037 0ustar00domidomi000000000000augeas-box/etc/hosts augeas-box/etc/ssh/sshd_config Build.PL ChangeLog lib/Config/Model/Backend/Augeas.pm lib/Config/Model/models/Itself/ConfigWR.d/augeas-backend.pl MANIFEST This list of files META.json META.yml README t/augeas_backend.t t/augeas_from_scratch.t t/model_extension.t t/pod.t t/test_model.pl Config-Model-Backend-Augeas-0.118/Build.PL000444001750001750 364212402644254 17222 0ustar00domidomi000000000000# Copyright (c) 2008-2011 Dominique Dumont. # # This file is part of Config-Model-Backend-Augead. # # Config-Model is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser Public License as # published by the Free Software Foundation; either version 2.1 of # the License, or (at your option) any later version. # # Config-Model is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser Public License for more details. # # You should have received a copy of the GNU Lesser Public License # along with Config-Model; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA # 02110-1301 USA use Module::Build; use warnings FATAL => qw(all) ; use strict ; require 5.008; print "Note: Augeas integration tests require Augeas library > 0.3.3\n\n"; my $build = Module::Build->new ( module_name => 'Config::Model::Backend::Augeas', license => 'LGPL_2_1', dist_abstract => 'Read and write configuration data through Augeas', dist_author => 'Dominique Dumont (ddumont at cpan dot org)', configure_requires => { 'Module::Build' => '0.36' }, requires => { 'Log::Log4perl' => '1.11', # for TRACE log level 'Exception::Class' => 0, 'Config::Augeas' => '0.303', 'Config::Model' => '2.034', 'Mouse' => 0, }, build_requires => { 'Test::More' => 0, 'Test::Exception' => 0, 'Test::Differences' => 0, 'Test::Warn' => '0.11', # tests fail with 0.08 'version' => '0.77' }, # cleanup required by t/augeas_backend.t add_to_cleanup => [qw/wr_root r_root/], ); $build->add_build_element('pl'); $build->create_build_script; Config-Model-Backend-Augeas-0.118/META.json000444001750001750 251412402644254 17344 0ustar00domidomi000000000000{ "abstract" : "Read and write configuration data through Augeas", "author" : [ "Dominique Dumont (ddumont at cpan dot org)" ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4208", "license" : [ "lgpl_2_1" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Config-Model-Backend-Augeas", "prereqs" : { "build" : { "requires" : { "Test::Differences" : "0", "Test::Exception" : "0", "Test::More" : "0", "Test::Warn" : "0.11", "version" : "0.77" } }, "configure" : { "requires" : { "Module::Build" : "0.36" } }, "runtime" : { "requires" : { "Config::Augeas" : "0.303", "Config::Model" : "2.034", "Exception::Class" : "0", "Log::Log4perl" : "1.11", "Mouse" : "0" } } }, "provides" : { "Config::Model::Backend::Augeas" : { "file" : "lib/Config/Model/Backend/Augeas.pm", "version" : "0.118" } }, "release_status" : "stable", "resources" : { "license" : [ "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt" ] }, "version" : "0.118" } Config-Model-Backend-Augeas-0.118/ChangeLog000444001750001750 665312402644254 17505 0ustar00domidomi0000000000002014-09-06 Dominique Dumont v0.118 * use empty step arg when call set on a checklist (Closes Debian #759965) 2014-06-19 Dominique Dumont v0.117 * experience config attribute is deprecated (Closes Debian #750339) 2014-04-03 Dominique Dumont v0.116 * Added missing build require on Test::Differences 2014-04-03 Dominique Dumont v0.115 * tweak test to be compatible with Config::Model < and >= 2.052 2013-05-05 Dominique Dumont v0.114 * removed from test unused 'use' statement that tripped smoke tests 2013-04-20 Dominique Dumont v0.113 * removed Any::Moose in favor of Mouse (depends on Mouse now) * Depends on Config::Model 2.034 (HEAD, master) * added meta-model snippet so config-model-edit can handle Augeas backend 2012-07-09 Dominique Dumont v0.112 * tests: Fixe broken test by changing the way the tests retrieve node's backend manager (from $node->{bmgr} to $node->backend_mgr) Closes Debian #680821 2012-05-24 Dominique Dumont v0.111 * In t/*.t: Change the way Augeas version is tested to cope with 0.10.0 (Closes Debian #674390) + build-depends on version 0.77 2011-09-21 Dominique Dumont v0.110 * Moosified C::M::Backend::Augeas (fixes Debian #642156) * Build.PL: + require Any::Moose * require Config::Model 1.255 2010-12-13 Dominique Dumont v0.109 * updated tests to work with Config::Model 1.236 (fix Ubuntu bug #749108) 2010-12-13 Dominique Dumont v0.108 * updated tests to work with Config::Model 1.207 * C::M::B::Augeas: synopsis is now a model snippet 2009-05-29 Dominique Dumont v0.107 * lib/Config/Model/Backend/Augeas.pm (all): Use Log::Log4perl for trace with Data::Read and Data::Write categories 2009-05-28 Dominique Dumont * lib/Config/Model/Backend/Augeas.pm: replaced obsolete config_file parameter with file (Config::Model 0.636) (all): ignore Augeas #comment nodes 2009-03-31 Dominique Dumont v0.106 * t/*.t: fixed tests that broke with Config::Model 0.634 (Fixes Debian bug #521968) 2009-01-28 Dominique Dumont v0.105 * t/augeas*.t: Sometimes the Augeas locale bug workaround also needs LC_ALL=C. 2009-01-27 Dominique Dumont v0.104 * lib/Config/Model/Backend/Augeas.pm (write): Do no create directory to store config files. Creating dir for file is Augeas problem in this backend. 2009-01-25 Dominique Dumont v0.103 * t/augeas_backend.t: Added workaround Augeas locale bug so that tests can be run on amd64 arch. The workaround re-run tests with LANG=C environment 2009-01-19 Dominique Dumont v0.102 * lib/Config/Model/Backend/Augeas.pm: Lots of improvement to deal with sshd_config lenses 2008-11-16 Domi * lib/Config/Model/Backend/Augeas.pm (write): Fixed a bug that prevented to write through Augeas even if read through Augeas was not done. 2008-11-13 Dominique Dumont v0.101 * lib/Config/Model/Backend/Augeas.pm: First version Config-Model-Backend-Augeas-0.118/README000444001750001750 623412402644254 16606 0ustar00domidomi000000000000With Config::Model::Backend::Augeas, Config::Model now provides a way to load and save data through RedHat's Augeas library. Config::Model and Augeas are both configuration editing tool. Both parse configuration files in their native formats and transforms them into a tree. Configuration changes are made by manipulating this tree and saving it back into native config files. Both Augeas and Config::Model rely on a tree to represent configuration data. On one side, Augeas is able to load and write data while respecting the structure and comments of the original configuration file. But Augeas purpose is not to validate the semantic content of the configuration file. On the other side, Config::Model is able to validate the semantic content of the configuration file, but it may discard all comments and will write back configuration data using a canonical order. Thus the structure of the original configuration file is not preserved. You can now have the best of both worlds by using the Augeas library to load and write data from Config::Model configuration tree. You can now instruct Config::Model to use Augeas backend in the autoread and autowrite feature. See Config::Model::BackendMgr for details. Unfortunately, you must ensure that the tree representation specified in Config::Model and Augeas lens are quite close together: - Config::Model element names and Augeas lens names must be identical - Both structures must be very close. Each Config::Model node must have a corresponsing lens. See Config::Model::Backend::Augeas documentation to use Augeas with your configuration model. For more details on Config::Model see: http://config-model.wiki.sourceforge.net/ For more details on Augeas, see: http://augeas.net ---------------------------------------------------------------------- BUGS Augeas 0.3.5 does not handle properly locale on 64 bits architecture. This is currently being fixed. In the meantime, you must set either environment variables to "C" locale: LANG=C LC_COLLATE=C ---------------------------------------------------------------------- Copyright (c) 2008-2013 Dominique Dumont. This file is part of Config-Model-Backend-Augeas. Config-Model-Backend-Augeas is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. Config-Model is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details. You should have received a copy of the GNU Lesser Public License along with Config-Model; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ---------------------------------------------------------------------- INSTALLATION tar zxvf .tar.gz cd perl Build.PL ./Build test If you want to install this software without packaging, type also: ./Build install Config-Model-Backend-Augeas-0.118/lib000755001750001750 012402644254 16332 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/lib/Config000755001750001750 012402644254 17537 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/lib/Config/Model000755001750001750 012402644254 20577 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/lib/Config/Model/models000755001750001750 012402644254 22062 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/lib/Config/Model/models/Itself000755001750001750 012402644254 23310 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/lib/Config/Model/models/Itself/ConfigWR.d000755001750001750 012402644254 25210 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/lib/Config/Model/models/Itself/ConfigWR.d/augeas-backend.pl000444001750001750 440512402644254 30537 0ustar00domidomi000000000000[ [ name => "Itself::ConfigWR", 'element' => [ 'backend' => { choice => [qw/Augeas/], help => { augeas => "Experimental backend with RedHat's Augeas library. See http://augeas.net for details", } }, 'save' => { type => 'leaf', value_type => 'enum', choice => [qw/backup newfile/], level => 'hidden', description => 'Specify how to save the configuration file. Either create a newfile (with extension .augnew, and do not overwrite the original file) or move the original file into a backup file (.augsave extension). Configuration files are overwritten by default', warp => { follow => '- backend', rules => [ Augeas => { level => 'normal', } ], } }, 'set_in' => { type => 'leaf', value_type => 'reference', refer_to => '- - element', level => 'hidden', description => 'Sometimes, the structure of a file loaded by Augeas starts directly with a list of items. For instance, /etc/hosts structure starts with a list of lines that specify hosts and IP addresses. This parameter specifies an element name in Config::Model root class that will hold the configuration data retrieved by Augeas', warp => { follow => '- backend', rules => [ Augeas => { level => 'normal', } ], } }, 'sequential_lens' => { type => 'list', level => 'hidden', cargo => { type => 'leaf', value_type => 'uniline', }, warp => { follow => { b => '- backend' }, rules => [ '$b eq "Augeas"' => { level => 'normal', } ], }, description => 'List of hash or list Augeas lenses where value are stored in sequential Augeas nodes. See Config::Model::Backend::Augeas for details.', }, ], ], ]; Config-Model-Backend-Augeas-0.118/lib/Config/Model/Backend000755001750001750 012402644254 22126 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/lib/Config/Model/Backend/Augeas.pm000444001750001750 6322112402644254 24052 0ustar00domidomi000000000000# Copyright (c) 2008-2011 Dominique Dumont. # # This file is part of Config-Model-Backend-Augeas. # # Config-Model is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser Public License as # published by the Free Software Foundation; either version 2.1 of # the License, or (at your option) any later version. # # Config-Model is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser Public License for more details. # # You should have received a copy of the GNU Lesser Public License # along with Config-Model; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA package Config::Model::Backend::Augeas; use Mouse; use Carp; use Log::Log4perl qw(get_logger :levels); extends 'Config::Model::Backend::Any'; use Config::Model::Exception; use File::Path; use Log::Log4perl qw(get_logger :levels); my $has_augeas = 1; eval { require Config::Augeas; }; $has_augeas = 0 if $@; our $VERSION = '0.118'; my $logger = get_logger('Backend::Augeas'); sub suffix { return ''; } =head1 NAME Config::Model::Backend::Augeas - Read and write config data through Augeas =head1 SYNOPSIS # model specification with augeas backend { config_class_name => 'OpenSsh::Sshd', # try Augeas and fall-back with custom method read_config => [ { backend => 'augeas' , file => '/etc/ssh/sshd_config', # declare "seq" Augeas elements sequential_lens => [/AcceptEnv AllowGroups [etc]/], }, { backend => 'custom' , # dir hardcoded in custom class class => 'Config::Model::Sshd' } ], # write_config will be written using read_config specifications element => ... } =head1 DESCRIPTION This class provides a way to load or store configuration data through L. This way, the structure and commments of the original configuration file will preserved. To use Augeas as a backend, you must specify the following C parameters: =over =item backend Use C (or C)in this case. =item save Either C or C. See L for details. =item file Name of the configuration file. =item sequential_lens This one is tricky. Set to one when new Augeas list or hash node must be created for each new list or hash element. See L for details. =back For instance: read_config => [ { backend => 'augeas' , save => 'backup', file => '/etc/ssh/sshd_config', # declare "seq" Augeas elements sequential_lens => [/AcceptEnv AllowGroups/], }, ], =head2 Sequential lens Some configuration files feature data that must be written as list or as hash. Depending on the syntax, Augeas list or hash lenses can be written so that new "container" nodes are required for each new element. For instance, C lines can be repeated several times in C. Since Augeas must keep track of these several lines, Augeas tree will be written like: /files/etc/ssh/sshd_config/HostKey[1] /files/etc/ssh/sshd_config/HostKey[2] /files/etc/ssh/sshd_config/HostKey[3] and not: /files/etc/ssh/sshd_config/HostKey/1 /files/etc/ssh/sshd_config/HostKey/2 /files/etc/ssh/sshd_config/HostKey/3 The C node is created several times. A new hostkey must be added with the following syntax: /files/etc/ssh/sshd_config/HostKey[4] and not: /files/etc/ssh/sshd_config/HostKey/4 So the C lens is sequential. The situation is more complex when syntax allow repeated values on several lines. Like: AcceptEnv LC_PAPER LC_NAME LC_ADDRESS AcceptEnv LC_IDENTIFICATION LC_ALL Augeas will have this tree: /files/etc/ssh/sshd_config/AcceptEnv[1]/1 /files/etc/ssh/sshd_config/AcceptEnv[1]/2 /files/etc/ssh/sshd_config/AcceptEnv[1]/3 /files/etc/ssh/sshd_config/AcceptEnv[2]/4 /files/etc/ssh/sshd_config/AcceptEnv[2]/5 Note that the first index between squarekeeps track of how are grouped the C data, but the I list index is after the slash. Augeas does not require new elements to create C. A new element can be added as : /files/etc/ssh/sshd_config/AcceptEnv[2]/6 So this lens is not sequential. The same kind of trouble occurs with hash elements. Some hashes tree are like: /files/etc/foo/my_hash/my_key1 /files/etc/foo/my_hash/my_key2 Others are like: /files/etc/foo/my_hash[1]/my_key1 /files/etc/foo/my_hash[2]/my_key2 Note that a list-like index is used with the hash key. This also depends on the syntax of the configuration file. For instance, C in C can be : Subsystem sftp /usr/lib/openssh/sftp-server Subsystem fooftp /usr/lib/openssh/fooftp-server Subsystem barftp /usr/lib/openssh/barftp-server This (unvalid) sshd configuration is represented by: /files/etc/ssh/sshd_config/Subsystem[1]/sftp /files/etc/ssh/sshd_config/Subsystem[2]/fooftp /files/etc/ssh/sshd_config/Subsystem[3]/barftp Any new Subsystem must be added with: /files/etc/ssh/sshd_config/Subsystem[4]/bazftp In this case, the hash is also sequential. For these examples, the augeas backend declaration must feature: sequential_lens => [qw/HostKey Subsystem/], =head2 Augeas backend limitation The structure and element names of the Config::Model tree must match the structure defined in Augeas lenses. I.e. the order of the element declared in Config::Model must match the order required by Augeas lenses. Sometimes, the structure of a file loaded by Augeas starts directly with a list of items. For instance C structure starts with a list of lines that specify hosts and IP addresses. The C parameter specifies an element name in Config::Model root class that will hold the configuration data retrieved by Augeas. =cut # for tests only sub _augeas_object { return shift->{augeas_obj}; } sub read { my $self = shift; my %args = @_; # contains root and config_dir return 0 unless $has_augeas; $self->{augeas_obj} ||= Config::Augeas->new( root => $args{root}, save => $args{save} ); if ( defined $args{config_file} ) { warn "Augeas::read : deprecated config_file parameter, use file instead\n"; $args{file} ||= delete $args{config_file}; } foreach my $param (qw/config_dir file/) { if ( not defined $args{$param} ) { Config::Model::Exception::Model->throw( error => "read_augeas error: model " . "does not specify '$param' for Augeas ", object => $self->{node} ); } } my $cdir = $args{root} . $args{config_dir}; $logger->info( "Read config data through Augeas in directory '$cdir' " . "file $args{file}" ); my $mainpath = '/files' . $args{config_dir} . $args{file}; my @result = $self->augeas_deep_match($mainpath); my @cm_path = @result; # cleanup resulting path to remove Augeas '/files', remove the # file path and plug the remaining path where it is consistent in # the model. I.e if the file "root" matches a list element (like # for /etc/hosts), get this element name from "set_in" parameter my $set_in = $args{set_in} || ''; map { s!$mainpath!!; $_ = "/$set_in/$_" if $set_in; s!/+!/!g; } @cm_path; # Create a hash of sequential lenses my %is_seq_lens = map { ( $_ => 1 ); } @{ $args{sequential_lens} || [] }; my $augeas_obj = $self->{augeas_obj}; # this may break as data will be written in the tree in an order # decided by Augeas. This may break complex model with warping as # the best writing order is indicated by the model stored in # Config::Model and not by Augeas. while (@result) { my $aug_p = shift @result; my $cm_p = shift @cm_path; # Config::Model path my $v = $augeas_obj->get($aug_p); next unless defined $v; $logger->debug("read-augeas $aug_p, will set C::M path '$cm_p' with '$v'"); $cm_p =~ s!^/!!; # With some list, we can get # /files/etc/ssh/sshd_config/AcceptEnv[1]/1/ = LC_PAPER # /files/etc/ssh/sshd_config/AcceptEnv[1]/2/ = LC_NAME # /files/etc/ssh/sshd_config/AcceptEnv[2]/3/ = LC_ADDRESS # /files/etc/ssh/sshd_config/AcceptEnv[2]/4/ = LC_TELEPHONE # Depending on the syntax, list can be in the form: # /files/etc/ssh/sshd_config/AcceptEnv[2]/3/ non-seq ditch idx # /files/etc/hosts/4/ non-seq # /files/etc/ssh/sshd_config/HostKey[2] is-seq keep idx # Likewise, hashes can be in the form # /files/etc/ssh/sshd_config/Subsystem[2]/foo/ is-seq ditch idx # /files/etc/ssh/sshd_config/Bar/foo/ non-seq my @cm_steps = split m!/+!, $cm_p; my $obj = $self->{node}; $obj = $obj->fetch_element( shift @cm_steps ) if $set_in; while ( my $step = shift @cm_steps ) { my ( $label, $idx ) = ( $step =~ /(\w+)(?:\[(\d+)\])?/ ); my $is_seq = $is_seq_lens{$label} || 0; $logger->debug( "read-augeas: step label $label " . ( defined $idx ? "idx $idx " : '' ) . "(is_seq $is_seq)" ); # idx will be treated next iteration if needed if ( $obj->get_type eq 'node' and $obj->element_type($label) eq 'list' and $is_seq ) { $idx = 1 unless defined $idx; $logger->debug("read-augeas: keep seq lens idx $idx"); unshift @cm_steps, $idx; } if ( $label =~ /\[/ ) { Config::Model::Exception::Model->throw( error => "read_augeas error: can't use $label with " . "Augeas index in Config::Model. $label should " . "probably be listed as 'sequential_lens'", object => $self->{node} ); } # augeas list begin at 1 not 0 my $type = $obj->get_type ; $label -= 1 if $obj->get_type eq 'list'; if ( scalar @cm_steps > 0 ) { $logger->debug("read-augeas: get $label"); $obj = $obj->get($label); } elsif ($type eq 'check_list') { $logger->debug("read-augeas: set check_list $label $v"); $obj->check( $v ); } elsif ($type eq 'leaf') { $logger->debug("read-augeas: set leaf $label $v"); $obj->store( $v ); } else { # last step $logger->debug("read-augeas: set $type $label $v"); $obj->set( $label , $v ); } if ( not defined $obj ) { Config::Model::Exception::Model->throw( error => "read_augeas error: '$cm_p' led to undef object. " . "Check for errors in 'sequential_lens' specification", object => $self->{node} ); } } } return 1; } sub augeas_deep_match { my ( $self, $mainpath ) = @_; # work around Augeas feature where '*' matches only one hierarchy # level # See https://www.redhat.com/archives/augeas-devel/2008-July/msg00016.html my @worklist = ($mainpath); my $logger = get_logger('Data::Read'); $logger->debug("read-augeas on @worklist"); my $augeas_obj = $self->{augeas_obj}; my @result; while (@worklist) { my $p = pop @worklist; # filter out comments # see http://augeas.net/page/Path_expressions my @newpath = $augeas_obj->match( $p . "/*[label() != '#comment']" ); $logger->debug("read-augeas $p/* matches paths: @newpath"); push @worklist, @newpath; push @result, @newpath; } return @result; } # this is a bit of a hack. This function is called by Autoread to # check whether the configuration file should be opened before calling # write or not. If the config file is opened before augeas writes in # it, all comments and structure is lost. sub skip_open { 1; } sub write { my $self = shift; my %args = @_; # contains root and config_dir return 0 unless $has_augeas; if ( defined $args{config_file} ) { warn "Augeas::write : deprecated config_file parameter, use file instead\n"; $args{file} ||= delete $args{config_file}; } foreach my $param (qw/config_dir file/) { if ( not defined $args{$param} ) { Config::Model::Exception::Model->throw( error => "write_augeas error: model " . "does not specify '$param' for Augeas ", object => $self->{node} ); } } my $cdir = $args{root} . $args{config_dir}; get_logger("Data::Write") ->info( "Write config data through Augeas in directory '$cdir' " . "file $args{file}" ); my $set_in = $args{set_in} || ''; my $mainpath = '/files' . $args{config_dir} . $args{file}; my $augeas_obj = $self->{augeas_obj} ||= Config::Augeas->new( root => $args{root}, save => $args{save} ); my %to_set = $self->copy_in_augeas( $augeas_obj, $mainpath, $set_in, $args{sequential_lens} ); $self->save($mainpath); } sub save { my $self = shift; my $mainpath = shift; # can't use augeas print directly in logging system... $self->{augeas_obj}->print() if get_logger('Data::Write')->is_debug; $self->{augeas_obj}->save || die "Augeas save failed" . $self->{augeas_obj}->print("/augeas/$mainpath/*"); } sub copy_in_augeas { my $self = shift; my $augeas_obj = shift; my $mainpath = shift; my $set_in = shift; my $seq_list = shift || []; my %is_seq_lens = map { ( $_ => 1 ); } @$seq_list; # The callback are kludgy and may be improved when the # following bugs are fixed: # https://fedorahosted.org/augeas/ticket/23 # https://fedorahosted.org/augeas/ticket/24 my @scan_args = ( fallback => 'all', auto_vivify => 0, list_element_cb => \&list_element_cb, check_list_element_cb => \&std_cb, hash_element_cb => \&hash_element_cb, leaf_cb => \&std_cb, node_content_cb => \&node_content_cb, ); # perform the scan my $view_scanner = Config::Model::ObjTreeScanner->new(@scan_args); $view_scanner->scan_node( [ $mainpath, $augeas_obj, $set_in, \%is_seq_lens ], $self->{node} ); } sub std_cb { my ( $scanner, $data_ref, $obj, $element, $index, $value_obj ) = @_; my ( $p, $augeas_obj, $set_in, $is_seq_lens ) = @$data_ref; my $v = $value_obj->fetch(); if ( defined $v and $v ne '' ) { get_logger("Data::Write")->info("copy_in_augeas: set $p = '$v'"); $augeas_obj->set( $p, $v ); #$self->save($mainpath) if $::debug ; } else { $augeas_obj->remove($p); } } sub list_element_cb { my ( $scanner, $data_ref, $node, $element_name, @idx ) = @_; my ( $p, $augeas_obj, $set_in, $is_seq_lens ) = @$data_ref; my $is_seq = $is_seq_lens->{$element_name} || 0; # the idea is to compare list indexes from Config::Model with the # corresponding hash-like keys in Augeas tree # find Augeas nodes matching this path my @matches = $augeas_obj->match( $p . "[label() != '#comment']" ); # need to find 2nd levels of sub-nodes for non-seq list lenses @matches = sort map { $augeas_obj->match( $_ . "/*[label() != '#comment']" ); } @matches unless $is_seq; # Depending on the syntax, list can be in the form: # /files/etc/ssh/sshd_config/AcceptEnv[2]/3/ non-seq use [last()]/idx/ # /files/etc/hosts/4/ non-seq use [last()]/idx/ # /files/etc/ssh/sshd_config/HostKey[2] is-seq use [idx] if ($is_seq) { # sequential lens need a list index to store list element. # I.e foo[1]/1 foo[1]/2 foo[2]/3 is ok. foo/1 foo/2 will # fail. But Augeas does return foo/1 if only one element is # present in the tree :-/ my $replace = $element_name . '[1]'; map { s/$element_name(?!\[)/$replace/ } @matches; } my $logger = get_logger("Data::Write"); $logger->debug( "copy_in_augeas: List (@idx) path $p matches (seq $is_seq):\n\t" . join( "\n\t", @matches ) ); # store list indexes found in Augeas and their corresponding path my %match = map { my ($k) = m!/([\w\[\]\-]+)$!; # need to keep only index in %match key $k =~ s/\w+\[(\d+)\]$/$1/ if $is_seq; ( $k => $_ ); } @matches; # Handle indexes found in Config::Model, but not in Augeas # tree. Create a new Augeas path for sequential list lenses. This # path will be used by scan_list if ($is_seq) { my $count = $augeas_obj->count_match( $p . "[label() != '#comment']" ); foreach my $idx (@idx) { # augeas index starts at 1 not 0 my $i = $idx + 1; next if defined $match{$i}; $match{$i} = $p . '[' . ++$count . "]" . ( $is_seq ? '' : "/$i" ); $logger->debug( "copy_in_augeas: New list path $match{$i} " . "for index $i" ); } } # now scan the elements stored by Config::Model hash keys to # store the hash values foreach my $i (@idx) { # use Augeas path (given by match command) or the path # created for unknown non-seq list elements my $scan_path = delete $match{ $i + 1 } || $p . '/' . ( $i + 1 ); $logger->debug("copy_in_augeas: scan list called on $scan_path index $i"); $scanner->scan_list( [ $scan_path, $augeas_obj, $set_in, $is_seq_lens ], $node, $element_name, $i ); } # cleanup indexes found in Augeas but not in Config::Model foreach ( keys %match ) { my $rm_path = $match{$_}; $logger->debug("copy_in_augeas: List rm $_ ->$rm_path"); $augeas_obj->remove($rm_path) || die "remove $rm_path failed"; # check if removing parent node in Augeas is needed $rm_path =~ s!/([\w\[\]\-]+)$!!; # chomp last "step" of the path if ( $augeas_obj->count_match( $rm_path . "[label() != '#comment']" ) == 1 and $set_in ne $element_name and $rm_path =~ /$element_name$/ ) { $logger->debug("copy_in_augeas: List rm parent node $_ ->$rm_path"); $augeas_obj->remove($rm_path) || die "remove $rm_path failed"; } } } # this callback is similar but not identical to the list callback. sub hash_element_cb { my ( $scanner, $data_ref, $node, $element_name, @keys ) = @_; my ( $p, $augeas_obj, $set_in, $is_seq_lens ) = @$data_ref; my $is_seq = $is_seq_lens->{$element_name} || 0; # the idea is to compare hash keys from Config::Model with the # corresponding hash-like keys in Augeas tree # find Augeas nodes matching this path my @matches = $augeas_obj->match( $p . "[label() != '#comment']" ); # need to find 2nd levels of sub-nodes @matches = sort map { $augeas_obj->match( $_ . "/*[label() != '#comment']" ); } @matches; # sequential lens need a list index to store element. I.e # foo[1]/key1 foo[2]/key2 is ok. foo/key1 foo/key2 will fail But # Augeas does return foo/key1 if only one element is present in # the tree :-/ if ($is_seq) { my $replace = $element_name . '[1]'; map { s/$element_name(?!\[)/$replace/ } @matches; } my $logger = get_logger('Data::Write'); $logger->debug( "copy_in_augeas: Hash path $p matches (seq $is_seq):\n\t" . join( "\n\t", @matches ) ); # store indexes found in Augeas and their corresponding path my %match = map { my ($k) = m!/([\w\[\]\-]+)$!; # need to keep only index in %match key $k =~ s/\w+\[(\d+)\]$/$1/ if $is_seq; ( $k => $_ ); } @matches; # Handle keys found in Config::Model, but not in Augeas # tree. Create a new Augeas path for sequential hash lenses. This # path will be used by scan_list. This insertion cannot be done if # no elements are already present in Augeas tree. if ( $is_seq and @matches ) { my $count = $augeas_obj->count_match( $p . "[label() != '#comment']" ); foreach (@keys) { next if defined $match{$_}; my $ip = $p . '[last()]'; $logger->debug("inserting $element_name after $ip\n"); $augeas_obj->insert( $element_name, after => $ip ) || die "augeas insert $element_name after $ip failed"; my $np = $match{$_} = $p . '[' . ++$count . "]/$_"; $logger->debug("copy_in_augeas: New hash path $np for key $_"); } } # now scan the elements stored by Config::Model hash keys to # store the hash values foreach (@keys) { # use Augeas path (given by match command) or a new path for # new elements my $scan_path = delete $match{$_} || $p . "/$_"; $scanner->scan_hash( [ $scan_path, $augeas_obj, $set_in, $is_seq_lens ], $node, $element_name, $_ ); } # cleanup keys found in Augeas but not in Config::Model foreach ( keys %match ) { my $rm_path = $match{$_}; $logger->debug("copy_in_augeas: Hash rm $_ ->$rm_path"); $augeas_obj->remove($rm_path) || die "remove $rm_path failed"; # check if removing parent node in Augeas is needed $rm_path =~ s!/([\w\[\]\-]+)$!!; if ( $augeas_obj->count_match( $rm_path . "[label() != '#comment']" ) == 1 and $set_in ne $element_name and $is_seq ) { $logger->debug("copy_in_augeas: Hash rm parent $_ ->$rm_path"); $augeas_obj->remove($rm_path) || die "remove $rm_path failed"; } } } sub node_content_cb { my ( $scanner, $data_ref, $node, @element ) = @_; my ( $p, $augeas_obj, $set_in, $is_seq_lens ) = @$data_ref; my $logger = get_logger('Data::Write'); # See set_in parameter (who said kludge ?) if ( scalar @element == 1 and $element[0] eq $set_in ) { # Augeas tree is stored below element[0] $logger->debug("copy_in_augeas: Augeas tree set in node path $p"); $scanner->scan_element( [ $p, $augeas_obj, $set_in, $is_seq_lens ], $node, $element[0] ); } else { my @matches = $augeas_obj->match( $p . "/*[label() != '#comment']" ); # cleanup indexes are we don't handle them now with element # (later in lists and hashes) map { s/\[\d+\]+$//; } @matches; $logger->debug( "copy_in_augeas: Node path $p matches:\n\t" . join( "\n\t", @matches ), ); # store elements found in Augeas and their corresponding path my %match = map { my ($elt) = m!/([\w\-]+)$!; ( $elt => $_ ) } @matches; # Handle element found in Config::Model, but not in Augeas # tree. Create a new Augeas path for new elements respecting # the order of the elements declared in Config::Model. This # path will be used by scan_element. This insertion cannot # be done if no elements are already present in Augeas tree. if (@matches) { my $previous_match = ''; foreach (@element) { if ( defined $match{$_} ) { $previous_match = $_; } elsif ( $node->fetch_element($_)->dump_as_data ) { # insert in Augeas only if the element contains # something interesting my ( $direction, $ip ) = $previous_match ? ( after => $p . '/' . $previous_match . '[last()]' ) : ( before => $matches[0] ); $logger->debug("inserting $_ $direction $ip"); $augeas_obj->insert( $_, $direction => $ip ) || die "augeas insert $_ $direction $ip failed"; my $np = $match{$_} = "$p/$_"; $logger->debug("copy_in_augeas: New hash path $np for element $_"); } } } # now scan the elements stored by Config::Model elements to # store the children nodes foreach (@element) { # use Augeas path (given by match command) or the path # created for new elements my $scan_path = delete $match{$_} || $p . '/' . $_; $logger->debug("copy_in_augeas: Node scan $scan_path for element $_"); $scanner->scan_element( [ $scan_path, $augeas_obj, $set_in, $is_seq_lens ], $node, $_ ); } # cleanup keys found in Augeas but not in Config::Model foreach ( keys %match ) { my $rm_path = $match{$_}; $logger->debug("copy_in_augeas: Node rm $_ ->$rm_path"); $augeas_obj->remove($rm_path) || die "remove $rm_path failed"; } } } 1; =head1 Log and trace This module use L to log debug and info trace with C and C categories. =head1 CAVEATS =over =item * Augeas C<#comment> nodes are ignored =back =head1 SEE ALSO =over =item * http://augeas.net/ : Augeas project page =item * L =item * Augeas mailing list: http://augeas.net/developers.html =item * Config::Model mailing list : http://sourceforge.net/mail/?group_id=155650 =back =head1 AUTHOR Dominique Dumont, Eddumont at cpan dot org@ =head1 COPYRIGHT Copyright (C) 2008-2010 by Dominique Dumont =head1 LICENSE This library is free software; you can redistribute it and/or modify it under the LGPL terms. =cut Config-Model-Backend-Augeas-0.118/t000755001750001750 012402644254 16027 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/t/pod.t000444001750001750 32212402644254 17110 0ustar00domidomi000000000000# -*- cperl -*- use strict; use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; my @poddirs = qw( lib ); all_pod_files_ok( all_pod_files( @poddirs ) ); Config-Model-Backend-Augeas-0.118/t/augeas_backend.t000444001750001750 1760512402644254 21316 0ustar00domidomi000000000000# -*- cperl -*- # test augeas backend # workaround Augeas locale bug if (not defined $ENV{LC_ALL} or $ENV{LC_ALL} ne 'C' or $ENV{LANG} ne 'C') { $ENV{LC_ALL} = $ENV{LANG} = 'C'; exec("perl $0 @ARGV"); } use ExtUtils::testlib; use Test::More ; use Test::Differences; use Config::Model; use File::Path; use File::Copy ; use version 0.77 ; use Log::Log4perl qw(:easy :levels); use warnings; no warnings qw(once); use strict; use vars qw/$model/; my $arg = shift || ''; my ( $trace, $log, $show ) = (0) x 3; $log = 1 if $arg =~ /l/; $show = 1 if $arg =~ /s/; $trace = 1 if $arg =~ /t/ ; Config::Model::Exception::Any->Trace(1) if $arg =~ /e/; my $home = $ENV{HOME} || ""; my $log4perl_user_conf_file = "$home/.log4config-model"; if ( $log and -e $log4perl_user_conf_file ) { Log::Log4perl::init($log4perl_user_conf_file); } else { Log::Log4perl->easy_init( $log ? $WARN : $ERROR ); } $model = Config::Model -> new (legacy => 'ignore',) ; eval { require Config::Augeas ;} ; if ( $@ ) { plan skip_all => 'Config::Augeas is not installed'; } else { plan tests => 18; } ok(1,"compiled"); # pseudo root were input config file are read my $r_root = 'augeas-box/'; # pseudo root where config files are written by config-model my $wr_root = 'wr_root/'; # cleanup before tests rmtree($wr_root); mkpath($wr_root.'etc/ssh/', { mode => 0755 }) ; copy($r_root.'etc/hosts',$wr_root.'etc/') ; copy($r_root.'etc/ssh/sshd_config',$wr_root.'etc/ssh/') ; # set_up data do "t/test_model.pl" ; my $i_hosts = $model->instance(instance_name => 'hosts_inst', root_class_name => 'Hosts', root_dir => $wr_root , ); ok( $i_hosts, "Created instance for /etc/hosts" ); my $i_root = $i_hosts->config_root ; my $expect = "record:0 ipaddr=127.0.0.1 canonical=localhost alias=localhost - record:1 ipaddr=192.168.0.1 canonical=bilbo - - " ; my $dump = $i_root->dump_tree ; print $dump if $trace ; is( $dump , $expect,"check dump of augeas data"); # change data content, '~' is like a splice, 'record~0' like a "shift" $i_root->load("record~0 record:0 canonical=buildbot - record:1 canonical=komarr ipaddr=192.168.0.10 - record:2 canonical=repoman ipaddr=192.168.0.11 - record:3 canonical=goner ipaddr=192.168.0.111") ; $dump = $i_root->dump_tree ; print $dump if $trace ; $i_hosts->write_back ; ok(1,"/etc/hosts write back done") ; my $aug_file = $wr_root.'etc/hosts'; my $aug_save_file = $aug_file.'.augsave' ; ok(-e $aug_save_file, "check that backup config file $aug_save_file was written"); my @expect = ("192.168.0.1 buildbot\n", "192.168.0.10\tkomarr\n", "192.168.0.11\trepoman\n", "192.168.0.111\tgoner\n" ); open(AUG,$aug_file) || die "Can't open $aug_file:$!"; is_deeply([],\@expect,"check content of $aug_file") ; close AUG; # check directly the content of augeas my $augeas_obj = $i_root->backend_mgr->get_backend('augeas')->_augeas_object ; my $nb = $augeas_obj -> count_match("/files/etc/hosts/*") ; is($nb,4,"Check nb of hosts in Augeas") ; # delete last entry $i_root->load("record~3"); $i_hosts->write_back ; ok(1,"/etc/hosts write back after deletion of record~3 (goner) done") ; $nb = $augeas_obj -> count_match("/files/etc/hosts/*") ; is($nb,3,"Check nb of hosts in Augeas after deletion") ; pop @expect; # remove goner entry open(AUG,$aug_file) || die "Can't open $aug_file:$!"; is_deeply([],\@expect,"check content of $aug_file after deletion of goner") ; close AUG; $augeas_obj->print('/') if $trace; my $have_pkg_config = `pkg-config --version` || ''; chomp $have_pkg_config ; my $aug_version = $have_pkg_config ? `pkg-config --modversion augeas` : '' ; chomp $aug_version ; my $skip = (not $have_pkg_config) ? 'pkgconfig is not installed' : version->parse($aug_version) le version->parse('0.3.1') ? 'Need Augeas library > 0.3.1' : ''; SKIP: { skip $skip , 8 if $skip ; my $i_sshd = $model->instance(instance_name => 'sshd_inst', root_class_name => 'Sshd', root_dir => $wr_root , ); ok( $i_sshd, "Created instance for sshd" ); ok( $i_sshd, "Created instance for /etc/ssh/sshd_config" ); open(SSHD,"$wr_root/etc/ssh/sshd_config") || die "can't open file: $!"; my @sshd_orig = ; close SSHD ; my $sshd_root = $i_sshd->config_root ; $sshd_root->init; # required by Config::Model 1.236 my $ssh_augeas_obj = $sshd_root->backend_mgr->get_backend('augeas')->_augeas_object ; $ssh_augeas_obj->print('/files/etc/ssh/sshd_config/*') if $trace; #my @aug_content = $ssh_augeas_obj->match("/files/etc/ssh/sshd_config/*") ; #print join("\n",@aug_content) ; my $assign = $Config::Model::VERSION >= 2.052 ? ':=' : ':' ; $expect = qq(AcceptEnv${assign}LC_PAPER,LC_NAME,LC_ADDRESS,LC_TELEPHONE,LC_MEASUREMENT,LC_IDENTIFICATION,LC_ALL AllowUsers${assign}foo,"bar\@192.168.0.*" HostbasedAuthentication=no HostKey${assign}/etc/ssh/ssh_host_key,/etc/ssh/ssh_host_rsa_key,/etc/ssh/ssh_host_dsa_key Subsystem:rftp=/usr/lib/openssh/rftp-server Subsystem:sftp=/usr/lib/openssh/sftp-server Subsystem:tftp=/usr/lib/openssh/tftp-server Match:0 Condition User=domi - Settings AllowTcpForwarding=yes - - Match:1 Condition User=Chirac Group="pres.*" - Settings Banner=/etc/bienvenue1.txt - - Match:2 Condition User=bush Group="pres.*" Host="white.house.*" - Settings Banner=/etc/welcome.txt - - Ciphers=arcfour256,aes192-cbc,aes192-ctr,aes256-cbc,aes256-ctr - ); $dump = $sshd_root->dump_tree ; print $dump if $trace ; eq_or_diff( [ split /\n/, $dump ] , [ split /\n/, $expect ] ,"check dump of augeas data"); # change data content, '~' is like a splice, 'record~0' like a "shift" $sshd_root->load("HostbasedAuthentication=yes Subsystem:ddftp=/home/dd/bin/ddftp Subsystem~rftp ") ; # augeas is broken somehow when reloading CIphers, let's delete this field $sshd_root->fetch_element("Ciphers")->clear ; $dump = $sshd_root->dump_tree ; print $dump if $trace ; $i_sshd->write_back ; my $aug_sshd_file = $wr_root.'etc/ssh/sshd_config'; my $aug_save_sshd_file = $aug_sshd_file.'.augsave' ; ok(-e $aug_save_sshd_file, "check that backup config file $aug_save_sshd_file was written"); my @mod = @sshd_orig; $mod[2] = "HostbasedAuthentication yes\n"; splice @mod, 8,0,"Protocol 1,2\n"; $mod[15] = "Subsystem ddftp /home/dd/bin/ddftp\n"; splice @mod,24,1 ; # remove Ciphers check because Augeas looks broken open(AUG,$aug_sshd_file) || die "Can't open $aug_sshd_file:$!"; eq_or_diff([],\@mod,"check content of $aug_sshd_file") ; close AUG; $sshd_root->load("Match~1") ; $dump = $sshd_root->dump_tree ; print $dump if $trace ; $i_sshd->write_back ; my $i=0; print "mod--\n",map { $i++ . ': '. $_} @mod,"---\n" if $trace ; my @lines = splice @mod,36,2 ; splice @mod, 32,2, @lines ; pop @mod ; open(AUG,$aug_sshd_file) || die "Can't open $aug_sshd_file:$!"; is_deeply([],\@mod,"check content of $aug_sshd_file after Match~1") ; close AUG; $sshd_root->load("Match:2 Condition User=sarko Group=pres.* - Settings Banner=/etc/bienvenue2.txt") ; $i_sshd->write_back ; push @mod,"Match User sarko Group pres.*\n","Banner /etc/bienvenue2.txt\n"; open(AUG,$aug_sshd_file) || die "Can't open $aug_sshd_file:$!"; my @got = ; map {s/^[\t ]+//;} @got; eq_or_diff(\@got,\@mod,"check content of $aug_sshd_file after Match:2 ...") ; close AUG; $sshd_root->load("Match:2 Condition User=sarko Group=pres.* - Settings AllowTcpForwarding=yes") ; $i_sshd->write_back ; $i=0; print "mod--\n",map { $i++ . ': '. $_} @mod,"---\n" if $trace ; splice @mod,37,0,"AllowTcpForwarding yes\n"; open(AUG,$aug_sshd_file) || die "Can't open $aug_sshd_file:$!"; @got = ; map {s/^[\t ]+//;} @got; eq_or_diff( \@got,\@mod,"check content of $aug_sshd_file after Match:2 AllowTcpForwarding=yes") ; close AUG; } # end SKIP section Config-Model-Backend-Augeas-0.118/t/test_model.pl000444001750001750 705512402644254 20667 0ustar00domidomi000000000000# test model used by t/*.t $model->create_config_class ( name => 'Host', element => [ [qw/ipaddr canonical alias/] => { type => 'leaf', value_type => 'uniline', } ] ); $model->create_config_class ( name => 'Hosts', read_config => [ { backend => 'augeas', config_dir => '/etc/', file => 'hosts', set_in => 'record', save => 'backup', #sequential_lens => ['record'], }, ], element => [ record => { type => 'list', cargo => { type => 'node', config_class_name => 'Host', } , }, ] ); $model->create_config_class ( name => 'Sshd', 'read_config' => [ { backend => 'augeas', config_dir => '/etc/ssh/', file => 'sshd_config', save => 'backup', sequential_lens => [qw/HostKey Subsystem Match/], }, ], element => [ 'AcceptEnv', { 'cargo' => { 'value_type' => 'uniline', 'type' => 'leaf' }, 'type' => 'list', }, 'AllowUsers', { 'cargo' => { 'value_type' => 'uniline', 'type' => 'leaf' }, 'type' => 'list', }, 'ForceCommand', { 'value_type' => 'uniline', 'type' => 'leaf', }, 'HostbasedAuthentication', { 'value_type' => 'enum', choice => [qw/no yes/], 'type' => 'leaf', }, 'HostKey', { 'cargo' => { 'value_type' => 'uniline', 'type' => 'leaf' }, 'type' => 'list', }, 'DenyUSers', { 'cargo' => { 'value_type' => 'uniline', 'type' => 'leaf' }, 'type' => 'list', }, 'Protocol', { 'default_list' => ['1', '2'], 'type' => 'check_list', 'choice' => ['1', '2'] }, 'Subsystem', { 'cargo' => { 'value_type' => 'uniline', 'mandatory' => '1', 'type' => 'leaf' }, 'type' => 'hash', 'index_type' => 'string' }, 'Match', { 'cargo' => { 'type' => 'node', 'config_class_name' => 'Sshd::MatchBlock' }, 'type' => 'list', }, 'Ciphers', { 'upstream_default_list' => [ 'aes256-cbc', 'aes256-ctr', 'arcfour256' ], ordered => 1, 'type' => 'check_list', 'description' => 'Specifies the ciphers allowed for protocol version 2. By default, all ciphers are allowed.', 'choice' => [ 'arcfour256', 'aes192-cbc', 'aes192-ctr', 'aes256-cbc', 'aes256-ctr' ] }, ] ); $model->create_config_class ( 'name' => 'Sshd::MatchBlock', 'element' => [ 'Condition', { 'type' => 'node', 'config_class_name' => 'Sshd::MatchCondition' }, 'Settings', { 'type' => 'node', 'config_class_name' => 'Sshd::MatchElement' } ] ); $model->create_config_class ( 'name' => 'Sshd::MatchCondition', 'element' => [ 'User', { 'value_type' => 'uniline', 'type' => 'leaf', }, 'Group', { 'value_type' => 'uniline', 'type' => 'leaf', }, 'Host', { 'value_type' => 'uniline', 'type' => 'leaf', }, 'Address', { 'value_type' => 'uniline', 'type' => 'leaf', } ] ); $model->create_config_class ( 'name' => 'Sshd::MatchElement', 'element' => [ 'AllowTcpForwarding', { 'value_type' => 'enum', 'type' => 'leaf', 'choice' => ['no', 'yes'] }, 'Banner', { 'value_type' => 'uniline', 'type' => 'leaf', }, ] ); Config-Model-Backend-Augeas-0.118/t/augeas_from_scratch.t000444001750001750 523712402644254 22357 0ustar00domidomi000000000000# -*- cperl -*- # test augeas backend use ExtUtils::testlib; use Test::More ; use Config::Model; use File::Path; use File::Copy ; use version 0.77 ; use warnings; no warnings qw(once); use strict; use vars qw/$model/; # workaround Augeas locale bug if (not defined $ENV{LC_ALL} or $ENV{LC_ALL} ne 'C' or $ENV{LANG} ne 'C') { $ENV{LC_ALL} = $ENV{LANG} = 'C'; exec("perl $0 @ARGV"); } my $arg = shift || ''; my $trace = $arg =~ /t/ ? 1 : 0 ; $::verbose = 1 if $arg =~ /v/; $::debug = 1 if $arg =~ /d/; Config::Model::Exception::Any->Trace(1) if $arg =~ /e/; use Log::Log4perl qw(:easy) ; Log::Log4perl->easy_init($arg =~ /l/ ? $TRACE: $WARN); $model = Config::Model -> new (legacy => 'ignore',) ; eval { require Config::Augeas ;} ; if ( $@ ) { plan skip_all => 'Config::Augeas is not installed'; } else { plan tests => 4; } ok(1,"compiled"); # pseudo root where config files are written by config-model my $wr_root = 'wr_root/'; # cleanup before tests rmtree($wr_root); mkpath($wr_root.'etc/ssh/', { mode => 0755 }) ; # set_up data do "t/test_model.pl" ; my $have_pkg_config = `pkg-config --version` || ''; chomp $have_pkg_config ; my $aug_version = $have_pkg_config ? `pkg-config --modversion augeas` : '' ; chomp $aug_version ; my $skip = (not $have_pkg_config) ? 'pkgconfig is not installed' : version->parse($aug_version) le version->parse('0.3.1') ? 'Need Augeas library > 0.3.1' : ''; SKIP: { skip $skip , 3 if $skip ; my $i_sshd = $model->instance(instance_name => 'sshd_inst', root_class_name => 'Sshd', root_dir => $wr_root , ); ok( $i_sshd, "Created instance for sshd" ); ok( $i_sshd, "Created instance for /etc/ssh/sshd_config" ); my $sshd_root = $i_sshd->config_root ; $sshd_root->init ; my $ssh_augeas_obj = $sshd_root->backend_mgr->get_backend('augeas')->_augeas_object ; $ssh_augeas_obj->print('/files/etc/ssh/sshd_config/*') if $trace; #my @aug_content = $ssh_augeas_obj->match("/files/etc/ssh/sshd_config/*") ; #print join("\n",@aug_content) ; # change data content, '~' is like a splice, 'record~0' like a "shift" $sshd_root->load("HostbasedAuthentication=yes Subsystem:ddftp=/home/dd/bin/ddftp ") ; my $dump = $sshd_root->dump_tree ; print $dump if $trace ; $i_sshd->write_back ; my @mod = ("HostbasedAuthentication yes\n", "Protocol 1,2\n", "Subsystem ddftp /home/dd/bin/ddftp\n" ); my $aug_sshd_file = $wr_root.'etc/ssh/sshd_config'; open(AUG,$aug_sshd_file) || die "Can't open $aug_sshd_file:$!"; is_deeply([],\@mod,"check content of $aug_sshd_file") ; close AUG; } # end SKIP section Config-Model-Backend-Augeas-0.118/t/model_extension.t000444001750001750 631712402644254 21554 0ustar00domidomi000000000000#!/usr/bin/perl use 5.010; use strict; use warnings; use Test::More ; # this test checks that the model extension # (e.g. lib/Config/Model/models/Itself/Class.d/augeas-backend.pl) # containing the "meta" model for Augeas backend can be loaded by # Config::Model::Itself and used # I.e. # Load Config::Model::Itself # check that extension located in dir mentioned above is installed # load a Sshd model that use augeas backend use ExtUtils::testlib; use Log::Log4perl qw(:easy :levels) ; use Config::Model ; no warnings qw(once); eval { require Config::Model::Itself ;} ; if ( $@ ) { plan skip_all => 'Config::Model::Itself is not installed'; } else { plan tests => 3; } my $arg = shift || ''; my ($log,$show) = (0) x 2 ; my $trace = $arg =~ /t/ ? 1 : 0 ; $log = 1 if $arg =~ /l/; $show = 1 if $arg =~ /s/; my $home = $ENV{HOME} || ""; my $log4perl_user_conf_file = "$home/.log4config-model"; if ($log and -e $log4perl_user_conf_file ) { Log::Log4perl::init($log4perl_user_conf_file); } else { Log::Log4perl->easy_init($log ? $WARN: $ERROR); } Config::Model::Exception::Any->Trace(1) if $arg =~ /e/; my $meta_model = Config::Model -> new ( ) ;# model_dir => '.' ); ok(1,"compiled"); my $meta_inst = $meta_model->instance( root_class_name => 'Itself::Model', instance_name => 'meta', ); ok($meta_inst,"Loaded Itself::Model") ; my $meta_root = $meta_inst->config_root ; my %ssh_model; $ssh_model{class}{'MasterModel::SshdWithAugeas'} = { 'read_config' => [ { backend => 'Augeas', config_dir => '/etc/ssh', file => 'sshd_config', sequential_lens => [qw/HostKey Subsystem Match/], }, { backend => 'perl_file', config_dir => '/etc/ssh', auto_create => 1, }, ], element => [ 'AcceptEnv', { 'cargo' => { 'value_type' => 'uniline', 'type' => 'leaf' }, 'type' => 'list', }, 'HostbasedAuthentication', { 'value_type' => 'boolean', 'type' => 'leaf', }, 'HostKey', { 'cargo' => { 'value_type' => 'uniline', 'type' => 'leaf' }, 'type' => 'list', }, 'Subsystem', { 'cargo' => { 'value_type' => 'uniline', 'mandatory' => '1', 'type' => 'leaf' }, 'type' => 'hash', 'index_type' => 'string' }, ] } ; $meta_root->load_data(\%ssh_model); print $meta_root->dump_tree if $trace; # kind of not necessary since load_data aboce will fail if the model extension is # not loaded my $backend = $meta_root->grab("class:MasterModel::SshdWithAugeas read_config:0 backend") ; like( join( ',', $backend->get_choice), qr/Augeas/, "test that augeas backend is part of backend choice" ) ; Config-Model-Backend-Augeas-0.118/augeas-box000755001750001750 012402644254 17617 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/augeas-box/etc000755001750001750 012402644254 20372 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/augeas-box/etc/hosts000444001750001750 6012402644254 21546 0ustar00domidomi000000000000127.0.0.1 localhost localhost 192.168.0.1 bilbo Config-Model-Backend-Augeas-0.118/augeas-box/etc/ssh000755001750001750 012402644254 21167 5ustar00domidomi000000000000Config-Model-Backend-Augeas-0.118/augeas-box/etc/ssh/sshd_config000444001750001750 211112402644254 23530 0ustar00domidomi000000000000# only a few parameters for augeas tests in core module # leaf, list and hash elements HostbasedAuthentication no # some comment before Hostkey HostKey /etc/ssh/ssh_host_key HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key # comment before Subsystem Subsystem sftp /usr/lib/openssh/sftp-server # illegal for sshd but handy for tests Subsystem tftp /usr/lib/openssh/tftp-server # will be removed in tests Subsystem rftp /usr/lib/openssh/rftp-server # comment before Accept env AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT AcceptEnv LC_IDENTIFICATION LC_ALL AllowUsers foo bar@192.168.0.* # test checklist Ciphers arcfour256,aes192-cbc,aes192-ctr,aes256-cbc,aes256-ctr # Match parsing block is broken and depend on resolution of # https://fedorahosted.org/augeas/ticket/7 Match User domi AllowTcpForwarding yes # un commentaire Match User Chirac Group pres.* Banner /etc/bienvenue1.txt # some comment Match User bush Group pres.* Host white.house.* Banner /etc/welcome.txt