From 8162bcfbfbcee5327bbe7b7487e60060abe2082e Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Mon, 18 Oct 2010 02:15:15 -0400 Subject: [PATCH] use modularized logging for msva-query-agent --- Crypt/Monkeysphere/MSVA/Client.pm | 74 +++++++++++++++++-------------- msva-query-agent | 14 +++--- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/Crypt/Monkeysphere/MSVA/Client.pm b/Crypt/Monkeysphere/MSVA/Client.pm index 8b6e514..e242836 100644 --- a/Crypt/Monkeysphere/MSVA/Client.pm +++ b/Crypt/Monkeysphere/MSVA/Client.pm @@ -25,40 +25,28 @@ use strict; use warnings; + use JSON; + use Crypt::Monkeysphere::MSVA::Logger; + use LWP::UserAgent; + use HTTP::Request; + require Crypt::X509; - BEGIN { - use Exporter (); - our (@EXPORT_OK,@ISA); - @ISA = qw(Exporter); - @EXPORT_OK = qw( &create_apd ); + sub log { + my $self = shift; + $self->{logger}->log(@_); } - our @EXPORT_OK; - - use JSON; - use Crypt::Monkeysphere::MSVA qw( msvalog ); sub query_agent { - use LWP::UserAgent; - use HTTP::Request; - my $self = shift; my $context = shift; my $peer = shift; my $pkctype = shift; my $pkcdata = shift; - my $msvasocket = shift; - if (! defined $msvasocket or $msvasocket eq '') { - $msvasocket = 'http://localhost:8901'; - } - - my $apd = create_apd($context, $peer, $pkctype, $pkcdata); + my $apd = $self->create_apd($context, $peer, $pkctype, $pkcdata); my $apdjson = to_json($apd); - # create the user agent - my $ua = LWP::UserAgent->new; - my $headers = HTTP::Headers->new( 'Content-Type' => 'application/json', 'Content-Length' => length($apdjson), @@ -66,7 +54,7 @@ 'Accept' => 'application/json', ); - my $requesturl = $msvasocket . '/reviewcert'; + my $requesturl = $self->{socket} . '/reviewcert'; my $request = HTTP::Request->new( 'POST', @@ -75,8 +63,8 @@ $apdjson, ); - msvalog('debug', "Contacting MSVA at %s\n", $requesturl); - my $response = $ua->request($request); + $self->log('debug', "Contacting MSVA at %s\n", $requesturl); + my $response = $self->{ua}->request($request); my $status = $response->status_line; my $ret; @@ -88,28 +76,28 @@ } sub create_apd { + my $self = shift; my $context = shift; my $peer = shift; my $pkctype = shift; my $pkcdata = shift; - msvalog('debug', "context: %s\n", $context); - msvalog('debug', "peer: %s\n", $peer); - msvalog('debug', "pkctype: %s\n", $pkctype); - + $self->log('debug', "context: %s\n", $context); + $self->log('debug', "peer: %s\n", $peer); + $self->log('debug', "pkctype: %s\n", $pkctype); if ($pkctype eq 'x509der') { my $cert = Crypt::X509->new(cert => $pkcdata); if ($cert->error) { die; }; - msvalog('info', "x509der certificate loaded.\n"); - msvalog('verbose', "cert subject: %s\n", $cert->subject_cn()); - msvalog('verbose', "cert issuer: %s\n", $cert->issuer_cn()); - msvalog('verbose', "cert pubkey algo: %s\n", $cert->PubKeyAlg()); - msvalog('verbose', "cert pubkey: %s\n", unpack('H*', $cert->pubkey())); + $self->log('info', "x509der certificate loaded.\n"); + $self->log('verbose', "cert subject: %s\n", $cert->subject_cn()); + $self->log('verbose', "cert issuer: %s\n", $cert->issuer_cn()); + $self->log('verbose', "cert pubkey algo: %s\n", $cert->PubKeyAlg()); + $self->log('verbose', "cert pubkey: %s\n", unpack('H*', $cert->pubkey())); } else { - msvalog('error', "unknown pkc type '%s'.\n", $pkctype); + $self->log('error', "unknown pkc type '%s'.\n", $pkctype); }; return { @@ -121,6 +109,24 @@ data => [map(ord, split(//,$pkcdata))], }, }; + }; + + + sub new { + my $class = shift; + my %args = @_; + my $self = {}; + + $self->{logger} = Crypt::Monkeysphere::MSVA::Logger->new($args{log_level}); + $self->{socket} = $args{socket}; + $self->{socket} = 'http://localhost:8901' + if (! defined $self->{socket} or $self->{socket} eq ''); + + # create the user agent + $self->{ua} = LWP::UserAgent->new; + + bless ($self,$class); + return $self; } 1; diff --git a/msva-query-agent b/msva-query-agent index 1c77282..fbe692b 100755 --- a/msva-query-agent +++ b/msva-query-agent @@ -19,7 +19,6 @@ use warnings; use strict; -use Crypt::Monkeysphere::MSVA qw( msvalog ); use Crypt::Monkeysphere::MSVA::Client; my $context = shift; @@ -32,12 +31,17 @@ my $pkcdata = do { ; }; -my ($status,$ret) = Crypt::Monkeysphere::MSVA::Client->query_agent($context,$peer,$pkctype, $pkcdata, $ENV{MONKEYSPHERE_VALIDATION_AGENT_SOCKET}); +my $client = Crypt::Monkeysphere::MSVA::Client->new( + socket => $ENV{MONKEYSPHERE_VALIDATION_AGENT_SOCKET}, + log_level => $ENV{MSVA_LOG_LEVEL}, + ); -msvalog('info', "status: %s\n", $status); +my ($status,$ret) = $client->query_agent($context,$peer,$pkctype,$pkcdata); + +$client->log('info', "status: %s\n", $status); if (defined $ret) { - msvalog('info', "valid: %s\n", $ret->{valid}); - msvalog('fatal', "message: %s\n", $ret->{message}); + $client->log('info', "valid: %s\n", $ret->{valid}); + $client->log('fatal', "message: %s\n", $ret->{message}); exit 0 if ($ret->{valid}); } -- 2.26.2