| 1 | #!perl | ||||||||||||||||||||||||||||
| 2 | # vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab | ||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Perl API for HashiCorp's Vault (Base) | ||||||||||||||||||||||||||||
| 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::Base; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 16 | our $VERSION = '0.04'; # VERSION | |||||||||||||||||||||||||||
| 13 | 16 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 16 | with 'WebService::Client'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 16 | has '+base_url' => ( default => 'http://127.0.0.1:8200' ); | |||||||||||||||||||||||||||
| 18 | 16 | has token => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 19 | 16 | has _token_expires => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 20 | 16 | has approle => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 21 | 16 | has version => ( is => 'ro', default => 'v1' ); | |||||||||||||||||||||||||||
| 22 | 16 | has mount => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | before 'get' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 25 | 0 | $_[0]->_check_token(); | |||||||||||||||||||||||||||
| 26 | 0 | $_[0]->_set_headers(); | |||||||||||||||||||||||||||
| 27 | 16 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | before 'post' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 30 | # Skip checking the token on a token request | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | if ($_[1] !~ m#auth/approle/login$#) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | $_[0]->_check_token(); | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 0 | $_[0]->_set_headers(); | |||||||||||||||||||||||||||
| 35 | 16 | }; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | before 'put' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 38 | 0 | $_[0]->_check_token(); | |||||||||||||||||||||||||||
| 39 | 0 | $_[0]->_set_headers(); | |||||||||||||||||||||||||||
| 40 | 16 | }; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | before 'delete' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 43 | 0 | $_[0]->_check_token(); | |||||||||||||||||||||||||||
| 44 | 0 | $_[0]->_set_headers(); | |||||||||||||||||||||||||||
| 45 | 16 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub _check_token { | ||||||||||||||||||||||||||
|
Subroutine
_check_token: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 48 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $self->_request_token() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | unless defined $self->token; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ## Check the token and get a new one if required | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if (defined $self->_token_expires && (time > $self->_token_expires)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->_token_expires and time > $self->_token_expires
MC/DC0%0/2
defined $self->_token_expires and time > $self->_token_expires
defined $self->_token_expires
time > $self->_token_expires
Truth table0%0/3
A and B
A= defined $self->_token_expiresB= time > $self->_token_expires
| |||||||||||||||||||||||||||||
| 55 | 0 | $self->_request_token() | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub _set_headers { | ||||||||||||||||||||||||||
|
Subroutine
_set_headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 60 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 61 | 0 | $self->ua->default_header( | |||||||||||||||||||||||||||
| 62 | 'X-Vault-Token' => $self->token, | ||||||||||||||||||||||||||||
| 63 | 'User_Agent' => sprintf( | ||||||||||||||||||||||||||||
| 64 | 'WebService::HashiCorp::Vault %s (perl %s; %s)', | ||||||||||||||||||||||||||||
| 65 | __PACKAGE__->VERSION, | ||||||||||||||||||||||||||||
| 66 | $^V, $^O), | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub _mkuri { | ||||||||||||||||||||||||||
|
Subroutine
_mkuri: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 71 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 0 | my @paths = @_; | |||||||||||||||||||||||||||
| 73 | 0 | return join '/', | |||||||||||||||||||||||||||
| 74 | $self->base_url, | ||||||||||||||||||||||||||||
| 75 | $self->version, | ||||||||||||||||||||||||||||
| 76 | $self->mount, | ||||||||||||||||||||||||||||
| 77 | @paths | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | sub _request_token { | ||||||||||||||||||||||||||
|
Subroutine
_request_token: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 82 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | die 'Must provide either token or approle' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | unless defined $self->approle; | ||||||||||||||||||||||||||||
| 86 | die 'role_id missing in approle' | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | unless defined $self->approle->{role_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | die 'secret_id missing in approle' | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | unless defined $self->approle->{secret_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $url = join('/', $self->base_url, $self->version, 'auth/approle/login'); | |||||||||||||||||||||||||||
| 92 | 0 | my $resp = $self->post( $url , $self->approle ); | |||||||||||||||||||||||||||
| 93 | 0 | $self->{token} = $resp->{auth}->{client_token}; | |||||||||||||||||||||||||||
| 94 | ## Set the expiry to 1 second before acutal expiry | ||||||||||||||||||||||||||||
| 95 | 0 | $self->_token_expires(time + $resp->{auth}->{lease_duration} - 1); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub list { | ||||||||||||||||||||||||||
|
Subroutine
list: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 0 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | $self->_check_token; | |||||||||||||||||||||||||||
| 103 | 0 | $self->_set_headers; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $headers = $self->_headers(); | |||||||||||||||||||||||||||
| 106 | 0 | my $url = $self->_url($path); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # HashiCorp have decided that 'LIST' is a http verb, so we must hack it in | ||||||||||||||||||||||||||||
| 109 | 0 | my $req = HTTP::Request->new( | |||||||||||||||||||||||||||
| 110 | 'LIST' => $url, | ||||||||||||||||||||||||||||
| 111 | HTTP::Headers->new(%$headers) | ||||||||||||||||||||||||||||
| 112 | ); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # this is a WebService::Client internal function. I said hack! | ||||||||||||||||||||||||||||
| 115 | 0 | return $self->req( $req ); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 16 | 1; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||