Docker/Role/RegistryAuth.pm

filter: Statementstmt 97.8% 45 / 46 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 95.7% 68 / 71 CC 9 SCAR 12.5
CC
9
Cov
95%
_registry_auth_config
6.0
_registry_b64url
4.0
BEGIN
1.0
CRAP 3.5
« Docker/API/System.pm ↑ Summary Docker/API/Configs.pm »
1 package API::Docker::Role::RegistryAuth;
2 # ABSTRACT: AuthConfig encoding shared by the registry-facing endpoints
3 52 our $VERSION = '0.006';
4 52 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 52 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 52 ▶use JSON::MaybeXS qw( decode_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 52 ▶use MIME::Base64 qw( decode_base64 encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 52 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 29 ▶sub _registry_auth_header {
Subroutine _registry_auth_header: called CC=1 SCAR=0.0
12 29 my ($self, $auth) = @_;
13 29 return $self->_registry_b64url($auth);
14 }
15  
16 2 ▶sub _registry_config_header {
Subroutine _registry_config_header: called CC=1 SCAR=0.0
17 2 my ($self, $config) = @_;
18 2 return $self->_registry_b64url($config);
19 }
20  
21 # The single encoding both header shapes share: X-Registry-Auth carries one
22 # AuthConfig, X-Registry-Config a map of registry hostname to AuthConfig, but
23 # each is nothing more than a JSON value in padded base64url. Keeping it in one
24 # place is the whole reason this role exists -- two copies of a base64 routine
25 # are what drifted apart before it.
26 31 ▶sub _registry_b64url {
Subroutine _registry_b64url: called CC=4 SCAR=13.9
27 31 my ($self, $value) = @_;
28  
29 31 my $payload;
30 31 ▶ if (!defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 9 22 31
elsif (ref $value eq 'HASH') { } 19 3 22
31 9 $payload = '{}';
32 }
33 elsif (ref $value eq 'HASH') {
34 19 $payload = encode_json($value);
35 }
36 else {
37 # Already pre-built JSON or a pre-encoded string. A base64-looking one
38 # (no braces) passes through -- but respelled into the URL-safe alphabet
39 # first, because the engine decodes with Go's base64.URLEncoding and a
40 # '+' or '/' left over from standard base64 makes it fail. This is the
41 # inverse of the tr{-_}{+/} in _registry_auth_config below.
42 3 ▶ if ($value =~ /^[A-Za-z0-9+\/=_\-]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[^[A-Za-z0-9+/=_\-]+$]) 3 0 3
43 3 $value =~ tr{+/}{-_};
44 3 return $value;
45 }
46 0 $payload = $value;
47 }
48  
49 # Padded base64url, and the padding is not optional -- see above.
50 28 my $b64 = encode_base64($payload, '');
51 28 $b64 =~ tr{+/}{-_};
52 28 return $b64;
53 }
54  
55 11 ▶sub _registry_auth_config {
Subroutine _registry_auth_config: called CC=6 SCAR=17.9
56 11 my ($self, $auth) = @_;
57  
58 11 ▶ return undef unless defined $auth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $auth 1 10 11
59 10 ▶ return { %$auth } if ref $auth eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $auth eq "HASH" 4 6 10
60 6 ▶ croak __PACKAGE__ . '->_registry_auth_config auth must be a HashRef, a '
Branch: 1 branch
BranchTrueFalseTotal
if ref $auth 0 6 6
61 . 'JSON object or a base64url-encoded one' if ref $auth;
62  
63 # The inverse of _registry_auth_header, so a caller can hand POST /auth
64 # exactly what it was going to push with -- including a header value it
65 # built earlier. The same "looks base64-like" test decides, and in the same
66 # order, or the two would disagree about a given string.
67 6 my $json = $auth;
68 6 ▶ unless ($auth =~ /^\s*\{/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth =~ /^\s*\{/) 4 2 6
69 4 my $b64 = $auth;
70 4 $b64 =~ tr{-_}{+/};
71 # decode_base64 tolerates missing padding, so a value that lost its '='
72 # somewhere still decodes here; the header side is where the pad matters.
73 4 $json = decode_base64($b64);
74 }
75  
76 6 my $config = eval { decode_json($json) };
77 6 ▶ croak __PACKAGE__ . '->_registry_auth_config could not read auth as an '
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 1 5 6
78 . 'AuthConfig: ' . $@ unless ref $config eq 'HASH';
79 5 return $config;
80 }
81  
82  
83 52 1;
84  
85 __END__
« Docker/API/System.pm ↑ Summary Docker/API/Configs.pm »