Registry/Auth/GCEServiceAccount.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 116 CC 3 SCAR 12.0
CC
3
Cov
0%
handle_success
6.0
handle_error
6.0
authorize
2.0
CRAP 3.3
1 package Docker::Registry::Auth::GCEServiceAccount;
2 use Moo;
3 use Types::Standard qw/HashRef Str ArrayRef CodeRef Int/;
4 with 'Docker::Registry::Auth';
5  
6 use Crypt::JWT qw/encode_jwt/;
7 use JSON::MaybeXS;
8 use Path::Class;
9 use URI;
10 use HTTP::Tiny;
11  
12 has service_account_file => (is => 'ro', isa => Str, default => sub {
13 "$ENV{HOME}/.gcloud/sd.json"
14 });
15  
16 has service_account => (is => 'ro', isa => HashRef, lazy => 1, default => sub {
17 my $self = shift;
18 my $f = Path::Class::File->new($self->service_account_file);
19 my $json = JSON::MaybeXS->new;
20 return $json->decode(join '', $f->slurp);
21 });
22  
23 has client_email => (is => 'ro', isa => Str, lazy => 1, default => sub {
24 my $self = shift;
25 my $value = $self->service_account->{ client_email };
26 Docker::Registry::Auth::Exception->throw({
27 message => "client_email entry not found in service_account information",
28 }) if (not defined $value);
29 return $value;
30 });
31 has private_key => (is => 'ro', isa => Str, lazy => 1, default => sub {
32 my $self = shift;
33 my $value = $self->service_account->{ private_key };
34 Docker::Registry::Auth::Exception->throw({
35 message => "private_key entry not found in service_account information",
36 }) if (not defined $value);
37 return $value;
38 });
39  
40 has scopes => (is => 'ro', isa => ArrayRef[Str], default => sub {
41 [ 'https://www.googleapis.com/auth/devstorage.read_only' ];
42 });
43  
44 has time_source => (is => 'ro', isa => CodeRef, default => sub {
45 return sub { time };
46 });
47  
48 has expiry => (is => 'ro', isa => Int, default => 300);
49  
50 has signed_jwt => (is => 'ro', isa => Str, lazy => 1, default => sub {
51 my $self = shift;
52 my $scope = join ' ', @{ $self->scopes };
53  
54 my $time = $self->time_source->();
55 my $key = $self->private_key;
56  
57 return encode_jwt(
58 payload => {
59 iss => $self->client_email,
60 scope => $scope,
61 aud => $self->auth_url,
62 iat => $time,
63 exp => $time + $self->expiry,
64 },
65 alg => 'RS256',
66 key => \$key,
67 );
68 });
69  
70 has auth_url => (is => 'ro', isa => Str, default => sub {
71 'https://www.googleapis.com/oauth2/v4/token';
72 });
73  
74 has accesstoken => (is => 'ro', isa => Str, lazy => 1, default => sub {
75 my $self = shift;
76  
77 my $url = URI->new($self->auth_url);
78 $url->query_form({
79 grant_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
80 assertion => $self->signed_jwt,
81 });
82 my $ua = HTTP::Tiny->new;
83 my $result = $ua->request(
84 'POST',
85 $self->auth_url,
86 { headers => {
87 'Content-Type' => 'application/x-www-form-urlencoded'
88 },
89 content => $url->query
90 }
91 );
92 if (not $result->{ success }) {
93 $self->handle_error($result);
94 } else {
95 my $result = $self->handle_success($result);
96 return $result->{ access_token };
97 }
98 });
99  
100 sub handle_success {
101 my ($self, $result) = @_;
102 my $json = eval { decode_json($result->{ content }) };
103 if (not $json) {
104 Docker::Registry::Auth::Exception->throw({
105 message => "Couldn't json-parse $result->{ content }"
106 });
107 } else {
108 return $json;
109 }
110 }
111 sub handle_error {
112 my ($self, $result) = @_;
113  
114 my $json = eval { decode_json($result->{ content }) };
115 if (not $json) {
116 Docker::Registry::Auth::Exception::HTTP->throw({
117 status => $result->{ status },
118 message => $result->{ content }
119 });
120 } else {
121 Docker::Registry::Auth::Exception::FromRemote->throw({
122 status => $result->{ status },
123 message => $json->{ error_description } // 'no_error_description',
124 code => $json->{ error } // 'no_error_code',
125 });
126 }
127 }
128  
129 sub authorize {
130 my ($self, $request) = @_;
131  
132 $request->header('Authorization', 'Bearer ' . $self->accesstoken);
133 return $request;
134 }
135  
136 1;