Changeset 1129

Show
Ignore:
Timestamp:
01/25/08 17:30:47 (7 months ago)
Author:
kindlund
Message:

Updated Manager and Database code to reflect changes to Ruby webservice API.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • honeyclient/trunk/lib/HoneyClient/Manager.pm

    r1128 r1129  
    590590 
    591591        # Mark the VM as suspended within the database. 
    592         #my $num_urls_inserted = HoneyClient::Manager::Database::set_client_suspended($clientDbId); 
     592        HoneyClient::Manager::Database::set_client_suspended($clientDbId); 
    593593    } 
    594594 
     
    728728    # Note: We assume our VM has a single MAC address 
    729729    # and a single IP address. 
    730 #    $vmStateTable->{$vmName}->{sources}->{$vmMAC}->{$vmIP} = { 
    731730    $vmStateTable->{$vm->name}->{sources}->{$vm->mac_address}->{$vm->ip_address} = { 
    732731        # XXX: We assume we can't pinpoint what source TCP ports the 
     
    752751    # Recreate the client stub; handle faults. 
    753752    $stubAgent = getClientHandle(namespace     => "HoneyClient::Agent", 
    754 #                                 address       => $vmIP, 
    755753                                 address       => $vm->ip_address, 
    756754                                 fault_handler => \&_handleFaultAndCleanup); 
     
    764762    # Recreate the client stub; ignore faults. 
    765763    $stubAgent = getClientHandle(namespace     => "HoneyClient::Agent", 
    766 #                                 address       => $vmIP, 
    767764                                 address       => $vm->ip_address, 
    768765                                 fault_handler => \&_agentHandleFault); 
     
    953950    $LOG->info("agent_state = " . Data::Dumper::Dumper($agent_state)); 
    954951 
     952    # XXX: We should delete the URLs from agent_state after successfully committing them into the database. 
     953 
    955954    my $num_urls_inserted = HoneyClient::Manager::Database::insert_history_urls($agent_state->{$agent_driver}); 
    956955    $LOG->info($num_urls_inserted . " URL(s) Inserted."); 
  • honeyclient/trunk/lib/HoneyClient/Manager/Database.pm

    r1128 r1129  
     1####################################################################### 
     2# Created on:  Jan 25, 2008 
     3# Package:     HoneyClient::Manager::Database 
     4# File:        Database.pm 
     5# Description: XML-RPC client interface to access HoneyClient data within 
     6#              the Hive web service. 
     7# 
     8# CVS: $Id: Database.pm 994 2007-11-08 20:30:12Z kindlund $ 
     9# 
     10# @author mbriggs, kindlund 
     11# 
     12# Copyright (C) 2008 The MITRE Corporation.  All rights reserved. 
     13# 
     14# This program is free software; you can redistribute it and/or 
     15# modify it under the terms of the GNU General Public License 
     16# as published by the Free Software Foundation, using version 2 
     17# of the License. 
     18#  
     19# This program is distributed in the hope that it will be useful, 
     20# but WITHOUT ANY WARRANTY; without even the implied warranty of 
     21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     22# GNU General Public License for more details. 
     23#  
     24# You should have received a copy of the GNU General Public License 
     25# along with this program; if not, write to the Free Software 
     26# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
     27# 02110-1301, USA. 
     28# 
     29####################################################################### 
     30 
     31=pod 
     32 
     33=head1 NAME 
     34 
     35HoneyClient::Manager::Database - Perl extension to provide an abstract 
     36interface for storing/accessing HoneyClient within the Hive web service. 
     37 
     38=head1 VERSION 
     39 
     40This documentation refers to HoneyClient::Manager::Database version 1.00. 
     41 
     42=head1 SYNOPSIS 
     43 
     44  # XXX: Need to fill this in. 
     45 
     46=head1 DESCRIPTION 
     47 
     48This library is an abstract interface to access and store HoneyClient 
     49data within the Hive web service. 
     50 
     51# XXX: Need further descriptions. 
     52 
     53=cut 
     54 
     55package HoneyClient::Manager::Database; 
     56 
    157use strict; 
    2  
     58use warnings; 
     59use Carp (); 
     60 
     61####################################################################### 
     62# Module Initialization                                               # 
     63####################################################################### 
     64 
     65BEGIN { 
     66    # Defines which functions can be called externally. 
     67    require Exporter; 
     68    our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS, $VERSION); 
     69 
     70    # Set our package version. 
     71    $VERSION = 1.00; 
     72 
     73    @ISA = qw(Exporter); 
     74 
     75    # Symbols to export automatically  
     76    # Note: Since this module is object-oriented, we do *NOT* export 
     77    # any functions other than "new" to call statically.  Each function 
     78    # for this module *must* be called as a method from a unique 
     79    # object instance. 
     80    @EXPORT = qw(); 
     81 
     82    # Items to export into callers namespace by default. Note: do not export 
     83    # names by default without a very good reason. Use EXPORT_OK instead. 
     84    # Do not simply export all your public functions/methods/constants. 
     85 
     86    # This allows declaration use HoneyClient::Manager::Database ':all'; 
     87    # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK 
     88    # will save memory. 
     89 
     90    # Note: Since this module is object-oriented, we do *NOT* export 
     91    # any functions other than "new" to call statically.  Each function 
     92    # for this module *must* be called as a method from a unique 
     93    # object instance. 
     94    %EXPORT_TAGS = ( 
     95        'all' => [ qw() ], 
     96    ); 
     97 
     98    # Symbols to autoexport (when qw(:all) tag is used) 
     99    @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); 
     100 
     101    $SIG{PIPE} = 'IGNORE'; # Do not exit on broken pipes. 
     102
     103our (@EXPORT_OK, $VERSION); 
     104 
     105=pod 
     106 
     107=begin testing 
     108 
     109# Make sure Log::Log4perl loads 
     110BEGIN { use_ok('Log::Log4perl', qw(:nowarn)) 
     111        or diag("Can't load Log::Log4perl package. Check to make sure the package library is correctly listed within the path."); 
     112        
     113        # Suppress all logging messages, since we need clean output for unit testing. 
     114        Log::Log4perl->init({ 
     115            "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     116            "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     117            "log4perl.appender.Buffer.min_level"                => "fatal", 
     118            "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     119            "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     120        }); 
     121
     122require_ok('Log::Log4perl'); 
     123use Log::Log4perl qw(:easy); 
     124 
     125# Make sure HoneyClient::Util::Config loads. 
     126BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar)) 
     127        or diag("Can't load HoneyClient::Util::Config package.  Check to make sure the package library is correctly listed within the path.");  
     128 
     129        # Suppress all logging messages, since we need clean output for unit testing. 
     130        Log::Log4perl->init({ 
     131            "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     132            "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     133            "log4perl.appender.Buffer.min_level"                => "fatal", 
     134            "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     135            "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     136        }); 
     137         
     138
     139require_ok('HoneyClient::Util::Config'); 
     140can_ok('HoneyClient::Util::Config', 'getVar'); 
     141use HoneyClient::Util::Config qw(getVar); 
     142 
     143# Make sure the module loads properly, with the exportable 
     144# functions shared. 
     145BEGIN { use_ok('HoneyClient::Manager::Database') or diag("Can't load HoneyClient::Manager::Database package.  Check to make sure the package library is correctly listed within the path."); } 
     146require_ok('HoneyClient::Manager::Database'); 
     147use HoneyClient::Manager::Database; 
     148 
     149# Suppress all logging messages, since we need clean output for unit testing. 
     150Log::Log4perl->init({ 
     151    "log4perl.rootLogger"                               => "DEBUG, Buffer", 
     152    "log4perl.appender.Buffer"                          => "Log::Log4perl::Appender::TestBuffer", 
     153    "log4perl.appender.Buffer.min_level"                => "fatal", 
     154    "log4perl.appender.Buffer.layout"                   => "Log::Log4perl::Layout::PatternLayout", 
     155    "log4perl.appender.Buffer.layout.ConversionPattern" => "%d{yyyy-MM-dd HH:mm:ss} %5p [%M] (%F:%L) - %m%n", 
     156}); 
     157 
     158# Make sure we use the exception testing library. 
     159require_ok('Test::Exception'); 
     160can_ok('Test::Exception', 'dies_ok'); 
     161use Test::Exception; 
     162 
     163# Make sure YAML loads. 
     164BEGIN { use_ok('YAML') or diag("Can't load YAML package.  Check to make sure the package library is correctly listed within the path."); } 
     165require_ok('YAML'); 
    3166use YAML; 
     167 
     168# Make sure XML::RPC loads. 
     169BEGIN { use_ok('XML::RPC') or diag("Can't load XML::RPC package.  Check to make sure the package library is correctly listed within the path."); } 
     170require_ok('XML::RPC'); 
    4171use XML::RPC; 
    5 use Carp qw(); 
     172 
     173# Make sure Data::Dumper loads. 
     174BEGIN { use_ok('Data::Dumper') or diag("Can't load Data::Dumper package.  Check to make sure the package library is correctly listed within the path."); } 
     175require_ok('Data::Dumper'); 
    6176use Data::Dumper; 
     177 
     178# Make sure Data::Structure::Util loads. 
     179BEGIN { use_ok('Data::Structure::Util', qw(unbless)) or diag("Can't load Data::Structure::Util package.  Check to make sure the package library is correctly listed within the path."); } 
     180require_ok('Data::Structure::Util'); 
     181can_ok('Data::Structure::Util', 'unbless'); 
    7182use Data::Structure::Util; 
    8183 
    9 package HoneyClient::Manager::Database; 
    10  
     184=end testing 
     185 
     186=cut 
     187 
     188####################################################################### 
     189 
     190# Include Global Configuration Processing Library 
     191use HoneyClient::Util::Config qw(getVar); 
     192 
     193# Include Parsing Library 
     194use YAML; 
     195 
     196# Include Communications Library 
     197use XML::RPC; 
     198 
     199# Include Dumper Library 
     200use Data::Dumper; 
     201 
     202# Include Utility Library 
     203use Data::Structure::Util; 
     204 
     205# Package Global Variable 
    11206our $AUTOLOAD; 
    12207 
     208# Include Logging Library 
     209use Log::Log4perl qw(:easy); 
     210 
     211# The global logging object. 
     212our $LOG = get_logger(); 
     213 
     214####################################################################### 
     215# Public Methods Implemented                                          # 
     216####################################################################### 
     217 
     218# Helper function designed to programmatically call the Hive web service, 
     219# based upon arbitrary inputs given. 
     220# 
     221# For example, this call: 
     222# my $output = HoneyClient::Manager::Database::foo($bar); 
     223# 
     224# ... will contact the Hive web service using the "foo" function name 
     225# and supply the hashtable reference, specified by $bar. 
     226# 
     227# Note: Errors generated by the web service call will be propagated back 
     228# in the form of croaked exceptions. 
     229#  
     230# Inputs: the hashtable or object to send  
     231# Outputs: the returned data from the web service 
    13232sub AUTOLOAD { 
    14233    my $obj = shift; 
     
    16235    my $name = $AUTOLOAD; 
    17236    $name =~  s/.*://; 
    18      
     237 
     238    # Perform the RPC call. 
     239    # XXX: Externalize this URL. 
    19240    my $xmlrpc = XML::RPC->new('http://172.16.164.103:3000/hc_database/api'); 
    20      
    21241    my $ret = $xmlrpc->call($name,$obj_yaml); 
    22242 
    23     # XXX: Make this more robust. 
    24     # Need to check if arguments are like "faultCode" or "errString", I believe. 
    25243    # Error checking. 
    26     if (ref($ret) eq "HASH") { 
    27         Carp::croak("Error: " . Data::Dumper::Dumper($ret)); 
     244    if ((ref($ret) eq "HASH") && (exists($ret->{faultCode}))) { 
     245        # XXX: Log this error. 
     246        Carp::croak("Error: " . $ret->{faultString}); 
    28247    } 
    29248 
     
    31250} 
    32251 
    33 sub insert { 
    34     my ($class,$obj) = @_; 
    35     my $obj_yaml = YAML::freeze(Data::Structure::Util::unbless($obj)); 
    36  
    37     # Execute insert via XML-RPC and return id 
    38     eval { 
    39         my $xmlrpc = XML::RPC->new('http://172.16.164.103:3000/hc_database/api'); 
    40         return $xmlrpc->call("insert",$class,$obj_yaml); 
    41     }; 
    42     if ($@) { 
    43         return 0; 
    44     } 
    45 
     252####################################################################### 
    46253 
    472541; 
     255 
     256####################################################################### 
     257# Additional Module Documentation                                     # 
     258####################################################################### 
     259 
     260__END__ 
     261 
     262=head1 BUGS & ASSUMPTIONS 
     263 
     264# XXX: Fill this in. 
     265 
     266=head1 SEE ALSO 
     267 
     268L<perltoot/"Autoloaded Data Methods"> 
     269 
     270L<http://www.honeyclient.org/trac> 
     271 
     272=head1 REPORTING BUGS 
     273 
     274L<http://www.honeyclient.org/trac/newticket> 
     275 
     276=head1 AUTHORS 
     277 
     278Matt Briggs, E<lt>mbriggs@mitre.orgE<gt> 
     279 
     280Darien Kindlund, E<lt>kindlund@mitre.orgE<gt> 
     281 
     282=head1 COPYRIGHT & LICENSE 
     283 
     284Copyright (C) 2008 The MITRE Corporation.  All rights reserved. 
     285 
     286This program is free software; you can redistribute it and/or 
     287modify it under the terms of the GNU General Public License 
     288as published by the Free Software Foundation, using version 2 
     289of the License. 
     290  
     291This program is distributed in the hope that it will be useful, 
     292but WITHOUT ANY WARRANTY; without even the implied warranty of 
     293MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     294GNU General Public License for more details. 
     295  
     296You should have received a copy of the GNU General Public License 
     297along with this program; if not, write to the Free Software 
     298Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
     29902110-1301, USA. 
     300 
     301 
     302=cut