| 1 | package Amazon::API::Provenance::Role::Records; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Amazon::API::BuildInfo; | ||
| 7 | use English qw(-no_match_vars); | ||
| 8 | use Amazon::API::Utils qw(slurp slurp_json); | ||
| 9 | use Cwd qw(getcwd); | ||
| 10 | use Data::Dumper; | ||
| 11 | use Digest::SHA qw(sha256_hex); | ||
| 12 | use File::Basename qw(basename); | ||
| 13 | use JSON; | ||
| 14 | use Role::Tiny; | ||
| 15 | |||
| 16 | use Readonly; | ||
| 17 | Readonly::Scalar our $KEY_ALIAS => 'alias/provenance-signing'; | ||
| 18 | Readonly::Scalar our $SIGNING_ALGORITHM => 'ECDSA_SHA_256'; | ||
| 19 | |||
| 20 | ######################################################################## | ||
| 21 | sub _key_alias { return $KEY_ALIAS } | ||
| 22 | ######################################################################## | ||
| 23 | |||
| 24 | ######################################################################## | ||
| 25 | sub _create_provenance_records { | ||
| 26 | ######################################################################## | ||
| 27 | my ( $self, %args ) = @_; | ||
| 28 | |||
| 29 | my ( $module_name, $service, $service_meta, $key_alias, $botocore_version, $tarball ) | ||
| 30 | = @args{qw(module_name service service_meta key_alias botocore_version tarball)}; | ||
| 31 | |||
| 32 | my %provenance_record = ( | ||
| 33 | 'provenance_version' => 1, | ||
| 34 | 'hash_algo' => 'sha256', | ||
| 35 | 'botocore' => $botocore_version, | ||
| 36 | 'service-2.json' => $service_meta->{service_2_digest}, | ||
| 37 | 'paginators-1.json' => $service_meta->{paginators_1_digest}, | ||
| 38 | 'endpoint-rule-set-1.json' => $service_meta->{endpoint_rule_set_1_digest}, | ||
| 39 | 'Amazon::API::BuildInfo::GIT_SHA' => $Amazon::API::BuildInfo::GIT_SHA, | ||
| 40 | 'Amazon::API::BuildInfo::VERSION' => $Amazon::API::BuildInfo::VERSION, | ||
| 41 | 'module_name' => $module_name, | ||
| 42 | 'service' => sprintf( '%s/%s', $service, $service_meta->{date} ), | ||
| 43 | ); | ||
| 44 | |||
| 45 | my $digest_meta = $self->_create_tarball_digest( $tarball, $args{output_path} ); | ||
| 46 | %provenance_record = ( %provenance_record, %{$digest_meta} ); | ||
| 47 | |||
| 48 | my $provenance_record_to_sign = JSON->new->pretty->canonical->encode( \%provenance_record ); | ||
| 49 | |||
| 50 | my $basename = basename( $provenance_record{tarball}, '.tar.gz' ); | ||
| 51 | |||
| 52 | $self->_write_provenance_record( $provenance_record_to_sign, $basename, $args{output_path} ); | ||
| 53 | |||
| 54 | $self->_sign_provenance_record( $provenance_record_to_sign, $basename, $args{output_path} ); | ||
| 55 | |||
| 56 | my $verify_rsp = eval { return $self->_verify_provenance( $key_alias, $basename, $args{output_path} ); }; | ||
| 57 | |||
| 58 | die "ERROR: unable to verify provenance files\n$EVAL_ERROR" | ||
| 59 | if !$verify_rsp || $EVAL_ERROR; | ||
| 60 | |||
| 61 | return $verify_rsp->{SignatureValid}; | ||
| 62 | } | ||
| 63 | |||
| 64 | ######################################################################## | ||
| 65 | sub _verify_provenance { | ||
| 66 | ######################################################################## | ||
| 67 | my ( $self, $key_alias, $basename, $output_path ) = @_; | ||
| 68 | $output_path //= getcwd; | ||
| 69 | |||
| 70 | return $self->_get_kms->Verify( | ||
| 71 | { KeyId => $key_alias, | ||
| 72 | Message => slurp("$output_path/$basename.json"), | ||
| 73 | MessageType => 'RAW', | ||
| 74 | SigningAlgorithm => $SIGNING_ALGORITHM, | ||
| 75 | Signature => slurp("$output_path/$basename.sig"), | ||
| 76 | } | ||
| 77 | ); | ||
| 78 | } | ||
| 79 | |||
| 80 | ######################################################################## | ||
| 81 | sub _write_provenance_record { | ||
| 82 | ######################################################################## | ||
| 83 | my ( $self, $provenance_record, $basename, $output_path ) = @_; | ||
| 84 | $output_path //= getcwd; | ||
| 85 | |||
| 86 | open my $fh, '>', "$output_path/$basename.json" | ||
| 87 | or die "ERROR: could not open provenance.json for writing\n$OS_ERROR"; | ||
| 88 | |||
| 89 | print {$fh} $provenance_record; | ||
| 90 | |||
| 91 | close $fh; | ||
| 92 | |||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | ######################################################################## | ||
| 97 | sub _sign_provenance_record { | ||
| 98 | ######################################################################## | ||
| 99 | my ( $self, $provenance_record, $basename, $output_path ) = @_; | ||
| 100 | $output_path //= getcwd; | ||
| 101 | |||
| 102 | my $sign_rsp = $self->_kms_sign( $KEY_ALIAS, $provenance_record ); | ||
| 103 | |||
| 104 | $self->get_logger->debug( Dumper( [ sign_rsp => $sign_rsp ] ) ); | ||
| 105 | |||
| 106 | open my $fh, '>', "$output_path/$basename.sig" | ||
| 107 | or die "ERROR: could not open provenance.json.sig for writing\n$OS_ERROR"; | ||
| 108 | |||
| 109 | binmode $fh; | ||
| 110 | |||
| 111 | print {$fh} $sign_rsp->{Signature}; | ||
| 112 | |||
| 113 | close $fh; | ||
| 114 | |||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | ######################################################################## | ||
| 119 | sub _create_tarball_digest { | ||
| 120 | ######################################################################## | ||
| 121 | my ( $self, $tarball, $output_path ) = @_; | ||
| 122 | |||
| 123 | my $fullpath = "$output_path/$tarball"; | ||
| 124 | |||
| 125 | die "ERROR: $fullpath not found\n" | ||
| 126 | if !-e $fullpath; | ||
| 127 | |||
| 128 | open my $fh, '<', $fullpath | ||
| 129 | or die "ERROR: $fullpath: $!\n"; | ||
| 130 | |||
| 131 | binmode $fh; | ||
| 132 | my $digest = Digest::SHA->new(256)->addfile($fh)->hexdigest; | ||
| 133 | close $fh; | ||
| 134 | |||
| 135 | return { tarball => $tarball, digest => $digest }; | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; |