« blib/lib/Langertha/Skeid/CapacityProbe/Registry.pm
↑ Summary
blib/lib/Langertha/Skeid/UsageStore/DBI.pm »
| 1 | package Langertha::Skeid::Registry; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 87 | our $VERSION = '0.004'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Signed Skeid-to-Skeid capacity snapshots: encoding, signing, verification, mapping | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 87 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 87 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 87 | ▶ | use Digest::SHA qw(hmac_sha256_hex); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 87 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 87 | ▶ | use Langertha::Skeid::Secret; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 87 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 45 | ▶ | sub SIGNATURE_HEADER { 'X-Skeid-Registry-Signature' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SIGNATURE_HEADER: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 22 | ▶ | sub SCHEMA_VERSION { 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SCHEMA_VERSION: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 40 | ▶ | sub MIN_SECRET_BYTES { 32 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
MIN_SECRET_BYTES: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 87 | my $JSON = JSON::MaybeXS->new(canonical => 1, utf8 => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 32 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 32 | my ($class, $snapshot) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 32 | return $JSON->encode($snapshot); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 60 | ▶ | sub sign { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sign: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 60 | my ($class, $body, $secret) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 60 | return 'sha256=' . hmac_sha256_hex($body, $secret); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 20 | ▶ | sub signed_snapshot { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
signed_snapshot: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 20 | my ($class, $skeid) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 20 | ▶ | my $secret = $skeid->registry_secret // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$skeid->registry_secret // ""
MC/DC0%0/1
$skeid->registry_secret // ""
$skeid->registry_secret
Truth table50%1/2
A
A= $skeid->registry_secret
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 20 | ▶ | die "registry secret is not set; a snapshot is never published unsigned\n" unless length $secret; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 20 | my $body = $class->encode($skeid->registry_snapshot); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 19 | return ($body, $class->sign($body, $secret)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 29 | ▶ | sub verify { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
verify: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 29 | my ($class, $body, $header, $secret) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 29 | ▶ | return 0 unless defined($body) && defined($header) && defined($secret) && length($secret); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%6/9
defined $body and defined $header and defined $secret and length $secret
defined $body and defined $header and defined $secret
defined $body and defined $header
MC/DC25%1/4
defined $body and defined $header and defined $secret and length $secret
defined $body
defined $header
defined $secret
length $secret
Truth table40%2/5
A and B and C and D
A= defined $bodyB= defined $headerC= defined $secretD= length $secret
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 28 | my $want = $class->sign($body, $secret); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 28 | $header =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 28 | return Langertha::Skeid::Secret->equal($header, $want); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 27 | ▶ | sub reading_from_snapshot { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reading_from_snapshot: called CC=26 SCAR=33.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 27 | my ($class, $snapshot, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 27 | ▶ | my @want = map { lc } grep { defined && length } @{ $args{tags} || [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
defined $_ && length $_
$args{'tags'} || []
MC/DC0%0/2
defined $_ && length $_
defined $_
length $_
MC/DC100%1/1
$args{'tags'} || []
$args{'tags'}
Truth table33%1/3
A && B
A= defined $_B= length $_
Truth table100%2/2
A
A= $args{'tags'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 27 | my $now = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 27 | my ($limit, $free, $selected) = (0, 0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 27 | ▶ | for my $node (@{ $snapshot->{nodes} || [] }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$snapshot->{'nodes'} || []
MC/DC0%0/1
$snapshot->{'nodes'} || []
$snapshot->{'nodes'}
Truth table50%1/2
A
A= $snapshot->{'nodes'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 44 | ▶ | next unless ref($node) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 44 | ▶ | next unless $node->{healthy}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 42 | ▶ | if (@want) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | my %have = map { lc($_) => 1 } grep { defined } @{ ref($node->{tags}) eq 'ARRAY' ? $node->{tags} : [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | next if grep { !$have{$_} } @want; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 41 | $selected++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 41 | my $max = _num($node->{max_conns}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 41 | my $inflight = _num($node->{inflight}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 41 | ▶ | my $cap = ref($node->{capacity}) eq 'HASH' ? $node->{capacity} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 41 | ▶ | my $cap_limit = $cap ? _num($cap->{limit}) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 41 | ▶ | my $backoff = ($cap && $cap->{retry_after} && _num($cap->{retry_after}) > $now) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$cap and $cap->{"retry_after"} and _num($cap->{"retry_after"}) > $now
$cap and $cap->{"retry_after"}
MC/DC66%2/3
$cap and $cap->{"retry_after"} and _num($cap->{"retry_after"}) > $now
$cap
$cap->{"retry_after"}
_num($cap->{"retry_after"}) > $now
Truth table75%3/4
A and B and C
A= $capB= $cap->{"retry_after"}C= _num($cap->{"retry_after"}) > $now
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 41 | ▶ | my $node_limit = $max > 0 ? $max : $cap_limit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 41 | ▶ | if ($node_limit <= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # Nothing bounds this node, so nothing bounds the downstream -- unless it is backing off, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # in which case it simply has no slots to offer. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | next if $backoff; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | return { used => undef, limit => undef }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 40 | ▶ | my $node_free = $max > 0 ? $max - $inflight : $node_limit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 40 | ▶ | if ($cap_limit > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | my $cap_free = $cap_limit - _num($cap->{used}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | $node_free = $cap_free if $cap_free < $node_free; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 40 | ▶ | $node_free = 0 if $backoff; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 40 | ▶ | $node_free = 0 if $node_free < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 40 | ▶ | $node_free = $node_limit if $node_free > $node_limit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 40 | $limit += $node_limit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 40 | $free += $node_free; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 26 | ▶ | return { used => 1, limit => 1 } if !$selected || $limit <= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $selected or $limit <= 0
MC/DC50%1/2
not $selected or $limit <= 0
not $selected
$limit <= 0
Truth table66%2/3
not(A) or B
A= not $selectedB= $limit <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 25 | return { used => $limit - $free, limit => $limit }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 86 | ▶ | sub _num { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_num: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 86 | my ($value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 86 | ▶ | return 0 unless defined $value && !ref $value && $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $value and not ref $value and $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
defined $value and not ref $value
MC/DC33%1/3
defined $value and not ref $value and $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
defined $value
not ref $value
$value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 85 | return 0 + $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 87 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||