| 1 | package Manta::Client 0.6; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp 'croak'; | ||
| 6 | use Crypt::OpenSSL::RSA; | ||
| 7 | use JSON::Parse 'parse_json'; | ||
| 8 | use LWP::UserAgent; | ||
| 9 | use MIME::Base64 'encode_base64'; | ||
| 10 | use Net::SSH::Perl::Key; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | croak "Illegal parameter list has odd number of values" if @_ % 2; | ||
| 15 | my %params = @_; | ||
| 16 | my $self = {}; | ||
| 17 | bless $self, $class; | ||
| 18 | for my $required (qw{ user url key_file }) { | ||
| 19 | croak "Required parameter '$required' not passed to '$class' constructor" unless defined $params{$required}; | ||
| 20 | $self->{$required} = $params{$required}; | ||
| 21 | } | ||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub _request { | ||
| 26 | my $self = shift; | ||
| 27 | croak "Illegal parameter list has odd number of values" if @_ % 2; | ||
| 28 | my %params = @_; | ||
| 29 | for my $required (qw{ method path }) { | ||
| 30 | croak "Required parameter '$required' not passed to _request method" unless defined $params{$required}; | ||
| 31 | } | ||
| 32 | my $date = scalar gmtime; | ||
| 33 | my $date_header = "date: $date"; | ||
| 34 | open F, "<$self->{key_file}"; | ||
| 35 | my $key = join '', <F>; | ||
| 36 | close F; | ||
| 37 | my $fingerprint = Net::SSH::Perl::Key->read_private("RSA", $self->{key_file})->fingerprint("md5"); | ||
| 38 | my $privatekey = Crypt::OpenSSL::RSA->new_private_key($key); | ||
| 39 | $privatekey->use_sha256_hash(); | ||
| 40 | my $signature = encode_base64($privatekey->sign($date_header), ""); | ||
| 41 | my $h = HTTP::Headers->new(%{$params{headers}}); | ||
| 42 | $h->header(date => $date); | ||
| 43 | $h->header('Authorization' => "Signature keyId=\"/$self->{user}/keys/$fingerprint\",algorithm=\"rsa-sha256\",signature=\"$signature\""); | ||
| 44 | my $ua = LWP::UserAgent->new(default_headers => $h); | ||
| 45 | my $response; | ||
| 46 | if ($params{method} eq "GET") { | ||
| 47 | $response = $ua->get("$self->{url}/$params{path}"); | ||
| 48 | if ($response->is_success) { | ||
| 49 | return $response->decoded_content; | ||
| 50 | } else { | ||
| 51 | return undef; | ||
| 52 | } | ||
| 53 | } elsif ($params{method} eq "PUT") { | ||
| 54 | $response = $ua->put("$self->{url}/$params{path}", Content => $params{content}, %$h); | ||
| 55 | return !!$response->is_success; | ||
| 56 | } elsif ($params{method} eq "DELETE") { | ||
| 57 | $response = $ua->delete("$self->{url}/$params{path}"); | ||
| 58 | return !!$response->is_success; | ||
| 59 | } else { | ||
| 60 | croak ("bad method"); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | sub get { | ||
| 65 | my ($self, $path) = @_; | ||
| 66 | my $response = $self->_request(path => $path, method => "GET"); | ||
| 67 | return $response; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub put { | ||
| 71 | my $self = shift; | ||
| 72 | croak "Illegal parameter list has odd number of values" if @_ % 2; | ||
| 73 | my %params = @_; | ||
| 74 | croak "Required parameter 'path' not passed to put method put" unless defined $params{path}; | ||
| 75 | return $self->_request(path => $params{path}, method => "PUT", content => $params{content}, headers => {"content-type" => $params{"content-type"}}); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub rm { | ||
| 79 | my ($self, $path) = @_; | ||
| 80 | my $response = $self->_request(path => $path, method => "DELETE"); | ||
| 81 | return $response; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub mkdir { | ||
| 85 | my ($self, $path) = @_; | ||
| 86 | return $self->put(path => $path, "content-type" => "application/json; type=directory"); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub ls { | ||
| 90 | # FIXME - limited to 256 objects | ||
| 91 | my ($self, $path) = @_; | ||
| 92 | my $response = $self->_request(path => $path, method => "GET"); | ||
| 93 | if ($response) { | ||
| 94 | my %results; | ||
| 95 | foreach(split '\n', $response) { | ||
| 96 | my $json = parse_json($_); | ||
| 97 | $results{$json->{name}} = { type => $json->{type}, mtime => $json->{mtime}, size => $json->{size}, etag => $json->{etag} }; | ||
| 98 | } | ||
| 99 | return \%results; | ||
| 100 | } | ||
| 101 | return undef; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub ln { | ||
| 105 | my ($self, $src, $dst) = @_; | ||
| 106 | return $self->_request(path => $dst, method => "PUT", headers => {Location => $src, "content-type" => "application/json; type=link"}); | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ |