File Coverage

blib/lib/WebService/HashiCorp/Vault/Secret/Cubbyhole.pm
Criterion Covered Total %
statement 6 30 20.0
branch 0 16 0.0
condition 0 3 0.0
subroutine 2 7 28.5
pod 2 3 66.6
total 10 59 16.9


line stmt bran cond sub pod time code
1             #!perl
2             # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab
3             # ABSTRACT: Perl API for HashiCorp's Vault (Secret)
4              
5             # See also https://github.com/hashicorp/vault-ruby
6             # And https://github.com/ianunruh/hvac
7             # And https://www.vaultproject.io/api/index.html
8              
9             package WebService::HashiCorp::Vault::Secret::Cubbyhole;
10              
11 2     2   14 use Moo;
  2         16  
  2         16  
12             our $VERSION = '0.03'; # VERSION
13 2     2   710 use namespace::clean;
  2         23  
  2         37  
14              
15             extends 'WebService::HashiCorp::Vault::Base';
16              
17             has '+mount' => ( is => 'ro', default => 'cubbyhole' );
18             has 'path' => ( is => 'ro' );
19             has 'auth' => ( is => 'ro' );
20             has 'data' => ( is => 'rw',
21             trigger => sub {
22             my $self = shift;
23             $self->_save(@_);
24             $self->BUILD()
25             });
26             has 'lease_duration' => ( is => 'ro' );
27             has 'lease_id' => ( is => 'ro' );
28             has 'renewable' => ( is => 'ro' );
29              
30             sub BUILD {
31 0     0 0   my $self = shift;
32 0 0         return unless $self->path;
33 0           $self->_clear_self();
34 0 0         if (my $resp = $self->get( $self->_mkuri($self->path) )) {
35             $self->{auth} = $resp->{auth}
36 0 0         if $resp->{auth};
37 0           $self->{data} = $resp->{data}; # avoid tiggerng the trigger
38             $self->{lease_duration} = $resp->{lease_duration}
39 0 0         if $resp->{lease_duration};
40             $self->{lease_id} = $resp->{lease_id}
41 0 0         if $resp->{lease_id};
42             $self->{renewable} = 0 + $resp->{renewable} # convert from JSON::Boolean
43 0 0         if $resp->{renewable};
44             }
45             }
46              
47              
48             sub _clear_self {
49 0     0     my $self = shift;
50 0           for my $k (qw/ auth data lease_duration lease_id renewable /) {
51 0 0         delete $self->{$k} if $self->{$k}
52             }
53 0           return 1
54             }
55              
56              
57             sub delete {
58 0     0 1   my $self = shift;
59 0           my $result = $self->SUPER::delete( $self->_mkuri($self->path) );
60 0           $self->_clear_self();
61 0           return $result
62             }
63              
64             sub _save {
65 0     0     my $self = shift;
66 0           my $data = shift;
67 0 0         die sprintf( "Secret data must be hashref, not a %s\n", ref $data )
68             if ref $data ne 'HASH';
69 0           return $self->post( $self->_mkuri($self->path), $data );
70             }
71              
72              
73             sub list {
74 0     0 1   my $self = shift;
75 0   0       my $result = $self->SUPER::list( $self->_mkuri($self->path || ()) );
76             return $result->{data}
77 0           }
78              
79              
80             1;
81              
82             __END__