File Coverage

blib/lib/WebService/HashiCorp/Vault/Secret/Kvv2.pm
Criterion Covered Total %
statement 6 34 17.6
branch 0 16 0.0
condition 0 3 0.0
subroutine 2 8 25.0
pod 2 3 66.6
total 10 64 15.6


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::Kvv2;
10              
11 2     2   13 use Moo;
  2         4  
  2         12  
12             our $VERSION = '0.01'; # VERSION
13 2     2   698 use namespace::clean;
  2         4  
  2         12  
14              
15             extends 'WebService::HashiCorp::Vault::Base';
16              
17             has '+mount' => ( is => 'ro', default => 'secret' );
18             has 'path' => ( is => 'ro' );
19             has 'auth' => ( is => 'ro' );
20             has '_metadata' => ( is => 'ro' );
21             has 'data' => ( is => 'rw',
22             trigger => sub {
23             my $self = shift;
24             $self->_save(@_);
25             $self->BUILD()
26             });
27             has 'lease_duration' => ( is => 'ro' );
28             has 'lease_id' => ( is => 'ro' );
29             has 'renewable' => ( is => 'ro' );
30              
31             sub BUILD {
32 0     0 0   my $self = shift;
33 0 0         return unless $self->path;
34 0           $self->_clear_self();
35 0 0         if (my $resp = $self->get( $self->_mkuri($self->path) )) {
36             $self->{auth} = $resp->{auth}
37 0 0         if $resp->{auth};
38 0           $self->{data} = $resp->{data}->{data}; # avoid triggering the trigger
39 0           $self->{_metadata} = $resp->{data}->{_metadata}; # avoid triggering the trigger
40             $self->{lease_duration} = $resp->{lease_duration}
41 0 0         if $resp->{lease_duration};
42             $self->{lease_id} = $resp->{lease_id}
43 0 0         if $resp->{lease_id};
44             $self->{renewable} = 0 + $resp->{renewable} # convert from JSON::Boolean
45 0 0         if $resp->{renewable};
46             }
47             }
48              
49             sub _mkuri {
50 0     0     my $self = shift;
51 0           my @paths = @_;
52 0           return join '/',
53             $self->base_url,
54             $self->version,
55             $self->mount, 'data',
56             @paths
57             }
58              
59             sub _clear_self {
60 0     0     my $self = shift;
61 0           for my $k (qw/ auth data lease_duration lease_id renewable /) {
62 0 0         delete $self->{$k} if $self->{$k}
63             }
64 0           return 1
65             }
66              
67              
68             sub delete {
69 0     0 1   my $self = shift;
70 0           my $result = $self->SUPER::delete( $self->_mkuri($self->path) );
71 0           $self->_clear_self();
72 0           return $result
73             }
74              
75             sub _save {
76 0     0     my $self = shift;
77 0           my $data = shift;
78 0 0         die sprintf( "Secret data must be hashref, not a %s\n", ref $data )
79             if ref $data ne 'HASH';
80 0           return $self->post( $self->_mkuri($self->path), { data => $data } );
81             }
82              
83              
84             sub list {
85 0     0 1   my $self = shift;
86 0   0       my $result = $self->SUPER::list( $self->_mkuri($self->path || ()) );
87             return $result->{data}
88 0           }
89              
90              
91             1;
92              
93             __END__