| 1 | package WWW::OpenBao; | ||||||||||||||||||||
| 2 | # ABSTRACT: HTTP client for OpenBao / HashiCorp Vault API | ||||||||||||||||||||
| 3 | 4 | our $VERSION = '0.002'; | |||||||||||||||||||
| 4 | 4 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 4 | ▶ | use namespace::clean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 4 | has endpoint => (is => 'ro', required => 1); | |||||||||||||||||||
| 11 | 1 | ▶ | has token => (is => 'rw', default => sub { '' }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 8 | ▶ | has kv_mount => (is => 'ro', default => sub { 'secret' }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 4 | has _http => (is => 'lazy'); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 0 | ▶ | sub _build__http { HTTP::Tiny->new(timeout => 10) } | ||||||||||||||||||
|
Subroutine
_build__http: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | # KV v2 paths | ||||||||||||||||||||
| 18 | 6 | ▶ | sub _kv_path { my ($self, $p) = @_; "v1/" . $self->kv_mount . "/data/$p" } | ||||||||||||||||||
|
Subroutine
_kv_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 5 | ▶ | sub _kv_metadata_path { my ($self, $p) = @_; "v1/" . $self->kv_mount . "/metadata/$p" } | ||||||||||||||||||
|
Subroutine
_kv_metadata_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # Core HTTP | ||||||||||||||||||||
| 22 | sub _request { | ||||||||||||||||||||
| 23 | 8 | ▶ | my ($self, $method, $path, $body) = @_; | ||||||||||||||||||
|
Subroutine
_request: called CC=5 SCAR=16.6
| |||||||||||||||||||||
| 24 | 8 | my $url = $self->endpoint . '/' . $path; | |||||||||||||||||||
| 25 | 8 | my %opts = (headers => { | |||||||||||||||||||
| 26 | 'X-Vault-Token' => $self->token, | ||||||||||||||||||||
| 27 | }); | ||||||||||||||||||||
| 28 | 8 | ▶ | if ($body) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 29 | 0 | $opts{content} = encode_json($body); | |||||||||||||||||||
| 30 | 0 | $opts{headers}{'Content-Type'} = 'application/json'; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | 8 | my $resp = $self->_http->request($method, $url, \%opts); | |||||||||||||||||||
| 33 | 8 | ▶ | return undef if $resp->{status} == 404; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | croak "OpenBao $method $path: $resp->{status} $resp->{content}" | ||||||||||||||||||||
| 35 | 6 | ▶ | unless $resp->{success}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 4 | ▶ | return $resp->{content} ? decode_json($resp->{content}) : {}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # KV v2: read secret data | ||||||||||||||||||||
| 40 | sub read_secret { | ||||||||||||||||||||
| 41 | 1 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||
|
Subroutine
read_secret: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 42 | 1 | my $resp = $self->_request('GET', $self->_kv_path($path)); | |||||||||||||||||||
| 43 | 1 | ▶ | return undef unless $resp; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 1 | return $resp->{data}{data}; | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # KV v2: read the metadata (version / created_time / destroyed / ...) that | ||||||||||||||||||||
| 48 | # rides along the same data/ read. Separate entry point on purpose so | ||||||||||||||||||||
| 49 | # read_secret keeps returning the bare data.data hashref consumers depend on. | ||||||||||||||||||||
| 50 | sub read_secret_metadata { | ||||||||||||||||||||
| 51 | 3 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||
|
Subroutine
read_secret_metadata: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 52 | 3 | my $resp = $self->_request('GET', $self->_kv_path($path)); | |||||||||||||||||||
| 53 | 3 | ▶ | return undef unless $resp; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 2 | return $resp->{data}{metadata}; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # KV v2: write secret data | ||||||||||||||||||||
| 58 | sub write_secret { | ||||||||||||||||||||
| 59 | 0 | ▶ | my ($self, $path, $data) = @_; | ||||||||||||||||||
|
Subroutine
write_secret: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 60 | 0 | return $self->_request('POST', $self->_kv_path($path), { data => $data }); | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # KV v2: delete secret (all versions + metadata) | ||||||||||||||||||||
| 64 | sub delete_secret { | ||||||||||||||||||||
| 65 | 0 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||
|
Subroutine
delete_secret: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 66 | 0 | return $self->_request('DELETE', $self->_kv_metadata_path($path)); | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | # KV v2: list secrets at path | ||||||||||||||||||||
| 70 | sub list_secrets { | ||||||||||||||||||||
| 71 | 0 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||
|
Subroutine
list_secrets: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 72 | 0 | my $resp = $self->_request('LIST', $self->_kv_metadata_path($path)); | |||||||||||||||||||
| 73 | 0 | ▶ | return [] unless $resp; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | 0 | ▶ | return $resp->{data}{keys} // []; | ||||||||||||||||||
|
Condition0%0/2
$resp->{"data"}{"keys"} // []
MC/DC0%0/1
$resp->{"data"}{"keys"} // []
$resp->{"data"}{"keys"}
Truth table0%0/2
A
A= $resp->{"data"}{"keys"}
| |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # KV v2: check if secret exists without fetching data. Only a 404 (absent | ||||||||||||||||||||
| 78 | # path) is a soft "no"; a 403 (policy forbids a path that may well exist) and | ||||||||||||||||||||
| 79 | # any other non-2xx propagate via _request's croak, so callers can tell | ||||||||||||||||||||
| 80 | # "not allowed to see" apart from "not there". | ||||||||||||||||||||
| 81 | sub secret_exists { | ||||||||||||||||||||
| 82 | 4 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||
|
Subroutine
secret_exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 83 | 4 | return defined $self->_request('GET', $self->_kv_metadata_path($path)); | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | # Auth: Kubernetes ServiceAccount login | ||||||||||||||||||||
| 87 | sub login_k8s { | ||||||||||||||||||||
| 88 | 0 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||
|
Subroutine
login_k8s: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 89 | 0 | ▶ | my $role = $args{role} // croak "login_k8s requires 'role'"; | ||||||||||||||||||
|
Condition0%0/3
$args{"role"} // croak("login_k8s requires 'role'")
MC/DC0%0/2
$args{"role"} // croak("login_k8s requires 'role'")
$args{"role"}
croak("login_k8s requires 'role'")
Truth table0%0/3
A // B
A= $args{"role"}B= croak("login_k8s requires 'role'")
| |||||||||||||||||||||
| 90 | 0 | ▶ | my $jwt = $args{jwt} // _read_sa_token(); | ||||||||||||||||||
|
Condition0%0/3
$args{"jwt"} // _read_sa_token()
MC/DC0%0/2
$args{"jwt"} // _read_sa_token()
$args{"jwt"}
_read_sa_token()
Truth table0%0/3
A // B
A= $args{"jwt"}B= _read_sa_token()
| |||||||||||||||||||||
| 91 | 0 | my $resp = $self->_request('POST', 'v1/auth/kubernetes/login', { | |||||||||||||||||||
| 92 | role => $role, jwt => $jwt, | ||||||||||||||||||||
| 93 | }); | ||||||||||||||||||||
| 94 | 0 | $self->token($resp->{auth}{client_token}); | |||||||||||||||||||
| 95 | 0 | return $resp->{auth}; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | sub _read_sa_token { | ||||||||||||||||||||
| 99 | 0 | ▶ | my $path = '/var/run/secrets/kubernetes.io/serviceaccount/token'; | ||||||||||||||||||
|
Subroutine
_read_sa_token: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 100 | 0 | ▶ | open my $fh, '<', $path or croak "Cannot read SA token: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 0 | local $/; | |||||||||||||||||||
| 102 | 0 | return <$fh>; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | # Sys: health check | ||||||||||||||||||||
| 106 | sub health { | ||||||||||||||||||||
| 107 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||
|
Subroutine
health: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 108 | 0 | return eval { $self->_request('GET', 'v1/sys/health') }; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | # Sys: initialize vault (first time) | ||||||||||||||||||||
| 112 | sub init { | ||||||||||||||||||||
| 113 | 0 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||
|
Subroutine
init: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 114 | 0 | ▶ | my $shares = $args{secret_shares} // 1; | ||||||||||||||||||
|
Condition0%0/2
$args{"secret_shares"} // 1
MC/DC0%0/1
$args{"secret_shares"} // 1
$args{"secret_shares"}
Truth table0%0/2
A
A= $args{"secret_shares"}
| |||||||||||||||||||||
| 115 | 0 | ▶ | my $threshold = $args{secret_threshold} // 1; | ||||||||||||||||||
|
Condition0%0/2
$args{"secret_threshold"} // 1
MC/DC0%0/1
$args{"secret_threshold"} // 1
$args{"secret_threshold"}
Truth table0%0/2
A
A= $args{"secret_threshold"}
| |||||||||||||||||||||
| 116 | 0 | return $self->_request('POST', 'v1/sys/init', { | |||||||||||||||||||
| 117 | secret_shares => $shares, secret_threshold => $threshold, | ||||||||||||||||||||
| 118 | }); | ||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | # Sys: unseal | ||||||||||||||||||||
| 122 | sub unseal { | ||||||||||||||||||||
| 123 | 0 | ▶ | my ($self, $key) = @_; | ||||||||||||||||||
|
Subroutine
unseal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 124 | 0 | return $self->_request('POST', 'v1/sys/unseal', { key => $key }); | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | # Sys: enable secrets engine | ||||||||||||||||||||
| 128 | sub enable_engine { | ||||||||||||||||||||
| 129 | 0 | ▶ | my ($self, $path, $type) = @_; | ||||||||||||||||||
|
Subroutine
enable_engine: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 130 | 0 | return $self->_request('POST', "v1/sys/mounts/$path", { type => $type }); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 4 | 1; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||