Changeset 482

Show
Ignore:
Timestamp:
04/02/06 07:19:58
Author:
miyagawa
Message:

Auto-rewrite config if encrypted fields are found. Fixes #91

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plagger/lib/Plagger.pm

    r475 r482  
    66use Carp; 
    77use Data::Dumper; 
     8use File::Copy; 
    89use File::Basename; 
    910use File::Find::Rule; 
     
    1213 
    1314use base qw( Class::Accessor::Fast ); 
    14 __PACKAGE__->mk_accessors( qw(conf update subscription plugins_path cache) ); 
     15__PACKAGE__->mk_accessors( qw(conf update subscription plugins_path cache ) ); 
    1516 
    1617use Plagger::Cache; 
     
    4041        plugins_path => {}, 
    4142        plugins => [], 
     43        rewrite_tasks => [] 
    4244    }, $class; 
    4345 
     
    4850        $self->{conf} = $config->{global}; 
    4951        $self->{conf}->{log}   ||= { level => 'debug' }; 
     52        $self->{config_path} = $opt{config}; 
    5053    } else { 
    5154        croak "Plagger->bootstrap: $opt{config}: $!"; 
     
    5760    $self->load_cache($opt{config}); 
    5861    $self->load_plugins(@{ $config->{plugins} || [] }); 
     62    $self->rewrite_config if @{ $self->{rewrite_tasks} }; 
    5963    $self->run(); 
     64} 
     65 
     66sub add_rewrite_task { 
     67    my($self, @stuff) = @_; 
     68    push @{ $self->{rewrite_tasks} }, \@stuff; 
     69} 
     70 
     71sub rewrite_config { 
     72    my $self = shift; 
     73 
     74    open my $fh, $self->{config_path} or $self->error("$self->{config_path}: $!"); 
     75    my $data = join '', <$fh>; 
     76    close $fh; 
     77 
     78    my $old = $data; 
     79    my $count; 
     80 
     81    # xxx this is a quick hack: It should be a YAML roundtrip maybe 
     82    for my $task (@{ $self->{rewrite_tasks} }) { 
     83        my($key, $old_value, $new_value ) = @$task; 
     84        if ($data =~ s/^(\s+$key:\s+)$old_value\s*$/$1$new_value/m) { 
     85            $count++; 
     86        } else { 
     87            $self->log(error => "$key: $old_value not found in $self->{config_path}"); 
     88        } 
     89    } 
     90 
     91    if ($count) { 
     92        File::Copy::copy( $self->{config_path}, $self->{config_path} . ".bak" ); 
     93        open my $fh, ">", $self->{config_path} or $self->error("$self->{config_path}: $!"); 
     94        print $fh $data; 
     95        close $fh; 
     96 
     97        $self->log(info => "rewrote $self->{config_path} with encrypting $count config values"); 
     98    } 
    6099} 
    61100 
  • trunk/plagger/lib/Plagger/Plugin.pm

    r447 r482  
    55__PACKAGE__->mk_accessors( qw(conf rule rule_hook cache) ); 
    66 
     7use Plagger::Crypt; 
    78use Plagger::Rule; 
    89use Plagger::Rules; 
     
    2627sub init { 
    2728    my $self = shift; 
     29 
    2830    if (my $rule = $self->{rule}) { 
    2931        $rule = [ $rule ] if ref($rule) eq 'HASH'; 
     
    3335        $self->{rule} = Plagger::Rule->new({ module => 'Always' }); 
    3436    } 
     37 
     38    if (my $params = $self->encrypt_config) { 
     39        $params = [ $params ] unless ref $params; 
     40 
     41        for my $key (@$params) { 
     42            my $config = $self->conf; 
     43            # support foo/bar/baz 
     44            while ($key =~ s!^(\w+)/!!) { 
     45                $config = $config->{$1}; 
     46            } 
     47            my $decrypted = Plagger::Crypt->decrypt($config->{$key}); 
     48            if ($decrypted eq $config->{$key}) { 
     49                Plagger->context->add_rewrite_task($key, $decrypted, Plagger::Crypt->encrypt($decrypted, 'base64')); 
     50            } else { 
     51                $config->{$key} = $decrypted; 
     52            } 
     53        } 
     54    } 
    3555} 
     56 
     57sub encrypt_config { } 
    3658 
    3759sub conf { $_[0]->{conf} } 
  • trunk/plagger/lib/Plagger/Plugin/Subscription/Bloglines.pm

    r467 r482  
    1010    $self->class_id . '-' . $self->conf->{username}; 
    1111} 
     12 
     13sub encrypt_config { 'password' } 
    1214 
    1315sub register {