| 1 | package Amazon::API::Provenance; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Amazon::API::Constants qw($TRUE $SUCCESS $FAILURE); | ||
| 7 | use Amazon::API::Utils qw(slurp_json choose slurp); | ||
| 8 | use Cwd qw(getcwd); | ||
| 9 | use Data::Dumper; | ||
| 10 | use English qw(-no_match_vars); | ||
| 11 | use JSON; | ||
| 12 | use MIME::Base64 qw(decode_base64 encode_base64); | ||
| 13 | use Storable qw(retrieve); | ||
| 14 | |||
| 15 | use Role::Tiny::With; | ||
| 16 | with 'Amazon::API::Provenance::Role::KMS'; | ||
| 17 | with 'Amazon::API::Provenance::Role::SSM'; | ||
| 18 | with 'Amazon::API::Provenance::Role::Records'; | ||
| 19 | with 'Amazon::API::Role::Botocore'; | ||
| 20 | |||
| 21 | use Readonly; | ||
| 22 | Readonly::Scalar our $KEY_ALIAS => 'alias/provenance-signing'; | ||
| 23 | Readonly::Scalar our $DEFAULT_KEY_SPEC => 'ECC_NIST_P256'; | ||
| 24 | Readonly::Scalar our $DEFAULT_KEY_USAGE => 'SIGN_VERIFY'; | ||
| 25 | Readonly::Scalar our $SIGNING_ALGORITHM => 'ECDSA_SHA_256'; | ||
| 26 | |||
| 27 | our @ISA = qw(CLI::Simple); | ||
| 28 | |||
| 29 | if ( $ENV{MODULINO_WRAPPER} ) { | ||
| 30 | eval { | ||
| 31 | require CLI::Simple; | ||
| 32 | 1; | ||
| 33 | } or die "CLI::Simple is required to use the Amazon::API provenance CLI\n"; | ||
| 34 | |||
| 35 | __PACKAGE__->use_log4perl( level => 'info', ); | ||
| 36 | } | ||
| 37 | |||
| 38 | caller or exit __PACKAGE__->main; | ||
| 39 | |||
| 40 | ######################################################################## | ||
| 41 | sub cmd_create_provenance { | ||
| 42 | ######################################################################## | ||
| 43 | my ($self) = @_; | ||
| 44 | |||
| 45 | my $service = $self->get_service; | ||
| 46 | |||
| 47 | die | ||
| 48 | "ERROR: usage: --service [--key-spec --key-usage --key-alias --input-path --output-path] --service service create-provenance\n" | ||
| 49 | if !$service; | ||
| 50 | |||
| 51 | my $key_alias = $self->_validate_key_alias; | ||
| 52 | |||
| 53 | my $input_path = $self->get_input_path // getcwd; | ||
| 54 | |||
| 55 | my $botocore_version = slurp_json( sprintf '%s/%s', $input_path, 'botocore-version.json' ); | ||
| 56 | |||
| 57 | my $services = retrieve( sprintf '%s/%s', $input_path, 'botocore-metadata.api' ); | ||
| 58 | |||
| 59 | my $service_meta = $services->{$service}; | ||
| 60 | |||
| 61 | my $module_name = $self->module_name($service); | ||
| 62 | |||
| 63 | my $basename = $module_name; | ||
| 64 | $basename =~ s/::/-/xsmg; | ||
| 65 | my $version = $service_meta->{date}; | ||
| 66 | $version =~ s/[-]/./xsmg; | ||
| 67 | |||
| 68 | $basename = sprintf '%s-%s', $basename, $version; | ||
| 69 | my $tarball = sprintf '%s.tar.gz', $basename; | ||
| 70 | |||
| 71 | my $verified = $self->_create_provenance_records( | ||
| 72 | module_name => $module_name, | ||
| 73 | service_meta => $service_meta, | ||
| 74 | botocore_version => $botocore_version, | ||
| 75 | service => $service, | ||
| 76 | key_alias => $key_alias, | ||
| 77 | tarball => $tarball, | ||
| 78 | output_path => $self->get_output_path // getcwd, | ||
| 79 | ); | ||
| 80 | |||
| 81 | if ( !$verified ) { | ||
| 82 | print {*STDERR} "ERROR: verification failed.\n"; | ||
| 83 | return $FAILURE; | ||
| 84 | } | ||
| 85 | |||
| 86 | my @provenance_files = glob "$basename.*"; | ||
| 87 | print {*STDOUT} sprintf "%s\n", join "\n", @provenance_files; | ||
| 88 | |||
| 89 | return $SUCCESS; | ||
| 90 | } | ||
| 91 | |||
| 92 | ######################################################################## | ||
| 93 | sub cmd_sign { | ||
| 94 | ######################################################################## | ||
| 95 | my ($self) = @_; | ||
| 96 | |||
| 97 | my ($key_id) = $self->get_key_id // $self->get_key_alias; | ||
| 98 | |||
| 99 | my ($message) = $self->get_args; | ||
| 100 | |||
| 101 | if ( $message =~ /^file:\/\/(.*)$/xsm ) { | ||
| 102 | my $file = $1; | ||
| 103 | die "ERROR: $file not found or is not readable\n" | ||
| 104 | if !-f $file || !-r $file; | ||
| 105 | |||
| 106 | $message = slurp($1); | ||
| 107 | |||
| 108 | die sprintf "ERROR: message be <= 4096 character. Length of message: %s\n", length $message | ||
| 109 | if length $message > 4096; | ||
| 110 | } | ||
| 111 | |||
| 112 | die "ERROR: usage sign --key-alias alias [--key-id id --signing-algorith] message\n" | ||
| 113 | if !$key_id || !$message; | ||
| 114 | |||
| 115 | my $rsp = $self->_get_kms->Sign( | ||
| 116 | { Message => $message, | ||
| 117 | SigningAlgorithm => $self->get_signing_algorithm, | ||
| 118 | KeyId => $key_id, | ||
| 119 | MessageType => 'RAW', | ||
| 120 | } | ||
| 121 | ); | ||
| 122 | |||
| 123 | my $fh = choose { | ||
| 124 | return *STDOUT | ||
| 125 | if !$self->get_output_path; | ||
| 126 | |||
| 127 | open my $ofh, '>', $self->get_output_path | ||
| 128 | or die sprintf "ERROR: could not open %s for writing\n%s", $self->get_output_path, $EVAL_ERROR; | ||
| 129 | |||
| 130 | return $ofh; | ||
| 131 | }; | ||
| 132 | |||
| 133 | print {$fh} $rsp->{Signature}; | ||
| 134 | |||
| 135 | close $fh; | ||
| 136 | |||
| 137 | return $SUCCESS; | ||
| 138 | } | ||
| 139 | |||
| 140 | ######################################################################## | ||
| 141 | sub cmd_verify { | ||
| 142 | ######################################################################## | ||
| 143 | my ($self) = @_; | ||
| 144 | |||
| 145 | my ($key_id) = $self->get_key_id // $self->get_key_alias; | ||
| 146 | |||
| 147 | my ($message) = $self->get_args; | ||
| 148 | |||
| 149 | die "ERROR: usage: verify --signature-file signature-file [--signing-algorithm] message\n" | ||
| 150 | if !$self->get_signature_file || !$message; | ||
| 151 | |||
| 152 | my $signature = slurp( $self->get_signature_file ); | ||
| 153 | |||
| 154 | if ( $message =~ /^file:\/\/(.*)$/xsm ) { | ||
| 155 | my $file = $1; | ||
| 156 | die "ERROR: $file not found or is not readable\n" | ||
| 157 | if !-f $file || !-r $file; | ||
| 158 | |||
| 159 | $message = slurp($1); | ||
| 160 | |||
| 161 | die sprintf "ERROR: message be <= 4096 character. Length of message: %s\n", length $message | ||
| 162 | if length $message > 4096; | ||
| 163 | } | ||
| 164 | |||
| 165 | my $rsp = $self->_get_kms->Verify( | ||
| 166 | { KeyId => $key_id, | ||
| 167 | Message => $message, | ||
| 168 | MessageType => 'RAW', | ||
| 169 | SigningAlgorithm => $self->get_signing_algorithm, | ||
| 170 | Signature => $signature, | ||
| 171 | } | ||
| 172 | ); | ||
| 173 | |||
| 174 | print {*STDOUT} JSON->new->pretty->encode($rsp); | ||
| 175 | |||
| 176 | return $SUCCESS; | ||
| 177 | } | ||
| 178 | |||
| 179 | ######################################################################## | ||
| 180 | sub cmd_create_key { | ||
| 181 | ######################################################################## | ||
| 182 | my ($self) = @_; | ||
| 183 | |||
| 184 | my ($key_alias) = $self->get_args; | ||
| 185 | $key_alias //= $self->get_key_alias; | ||
| 186 | |||
| 187 | die "ERROR: usage: create-key [--key-spec --key-usage] key-alias\n" | ||
| 188 | if !$key_alias; | ||
| 189 | |||
| 190 | if ( $key_alias !~ /^alias\//xsm ) { | ||
| 191 | $key_alias = "alias/$key_alias"; | ||
| 192 | warn "WARNING: alias will be set as $key_alias\n"; | ||
| 193 | } | ||
| 194 | |||
| 195 | my $rsp = eval { | ||
| 196 | return $self->_kms_create_key( | ||
| 197 | key_spec => $self->get_key_spec // $DEFAULT_KEY_SPEC, | ||
| 198 | key_usage => $self->get_key_usage // $DEFAULT_KEY_USAGE, | ||
| 199 | description => $self->get_key_description // $key_alias, | ||
| 200 | ); | ||
| 201 | }; | ||
| 202 | |||
| 203 | die "ERROR: could not create $key_alias\n$EVAL_ERROR" | ||
| 204 | if !$rsp || $EVAL_ERROR; | ||
| 205 | |||
| 206 | my $key_id = $rsp->{KeyMetadata}{KeyId}; | ||
| 207 | |||
| 208 | my $alias_rsp = eval { $self->_kms_create_alias( $key_id, $key_alias ); }; | ||
| 209 | |||
| 210 | die "ERROR: could not create key alias: $key_alias for key id: $key_id\n$EVAL_ERROR" | ||
| 211 | if !$alias_rsp || $EVAL_ERROR; | ||
| 212 | |||
| 213 | print {*STDOUT} JSON->new->pretty->encode( | ||
| 214 | [ create_key_response => $rsp, | ||
| 215 | create_alias_response => $alias_rsp, | ||
| 216 | ] | ||
| 217 | ); | ||
| 218 | |||
| 219 | return $SUCCESS; | ||
| 220 | } | ||
| 221 | |||
| 222 | ######################################################################## | ||
| 223 | sub cmd_export_pem { | ||
| 224 | ######################################################################## | ||
| 225 | my ($self) = @_; | ||
| 226 | |||
| 227 | my $kms = $self->_get_kms; | ||
| 228 | |||
| 229 | my $key_id = $self->get_key_id; | ||
| 230 | |||
| 231 | my $fh = choose { | ||
| 232 | return *STDOUT | ||
| 233 | if !$self->get_output_path; | ||
| 234 | |||
| 235 | open my $out_fh, '>', $self->get_output_path | ||
| 236 | or die sprintf "ERROR: could not open %s for writing\n%s", $self->get_output_path, $OS_ERROR; | ||
| 237 | |||
| 238 | return $out_fh | ||
| 239 | }; | ||
| 240 | |||
| 241 | my $rsp = $self->_kms_get_public_key($key_id); | ||
| 242 | |||
| 243 | my $public_key = $rsp->{PublicKey}; | ||
| 244 | my $der_data = decode_base64($public_key); | ||
| 245 | |||
| 246 | print {$fh} "-----BEGIN PUBLIC KEY-----\n"; | ||
| 247 | print {$fh} encode_base64($public_key); # encode_base64 automatically wraps at 76 chars | ||
| 248 | print {$fh} "-----END PUBLIC KEY-----\n"; | ||
| 249 | |||
| 250 | close $fh; | ||
| 251 | |||
| 252 | return $SUCCESS; | ||
| 253 | } | ||
| 254 | |||
| 255 | ######################################################################## | ||
| 256 | sub cmd_describe_key { | ||
| 257 | ######################################################################## | ||
| 258 | my ($self) = @_; | ||
| 259 | |||
| 260 | my ($key_id) = $self->get_args; | ||
| 261 | $key_id //= $self->get_key_id; | ||
| 262 | |||
| 263 | die "ERROR: usage: describe-key key-id\n" | ||
| 264 | if !$key_id; | ||
| 265 | |||
| 266 | my $rsp = $self->_kms_describe_key($key_id); | ||
| 267 | |||
| 268 | print {*STDOUT} JSON->new->pretty->encode($rsp); | ||
| 269 | |||
| 270 | return $SUCCESS; | ||
| 271 | } | ||
| 272 | |||
| 273 | ######################################################################## | ||
| 274 | sub _validate_key_alias { | ||
| 275 | ######################################################################## | ||
| 276 | my ($self) = @_; | ||
| 277 | |||
| 278 | my $kms = $self->_get_kms; | ||
| 279 | |||
| 280 | my $key_spec = $self->get_key_spec // $DEFAULT_KEY_SPEC; | ||
| 281 | my $key_usage = $self->get_key_usage // $DEFAULT_KEY_USAGE; | ||
| 282 | my $key_alias = $self->get_key_alias // $KEY_ALIAS; | ||
| 283 | |||
| 284 | my $parameter_name = $key_alias; | ||
| 285 | $parameter_name =~ s/^alias//xsm; | ||
| 286 | |||
| 287 | my $key_id = eval { $self->_ssm_get_parameter($parameter_name); }; | ||
| 288 | |||
| 289 | die $EVAL_ERROR | ||
| 290 | if !$key_id && $EVAL_ERROR !~ /ParameterNotFound/xsm; | ||
| 291 | |||
| 292 | if ( !$key_id ) { | ||
| 293 | $self->get_logger->info( sprintf 'ssm:%s not found...creating new key', $parameter_name ); | ||
| 294 | # we can't find it in SSM so we'll have to create new key | ||
| 295 | my $rsp = $kms->_kms_create_key( | ||
| 296 | key_spec => $key_spec, | ||
| 297 | key_usage => $key_usage, | ||
| 298 | description => 'Amazon::API provenance signing' | ||
| 299 | ); | ||
| 300 | |||
| 301 | $key_id = $rsp->{KeyMetadata}{KeyId}; | ||
| 302 | |||
| 303 | $self->_ssm_put_parameter( $parameter_name => $key_id ); | ||
| 304 | } | ||
| 305 | |||
| 306 | eval { | ||
| 307 | $self->_kms_describe_key($key_alias); | ||
| 308 | return $TRUE; | ||
| 309 | } or do { | ||
| 310 | $self->_kms_create_alias( $key_id, $key_alias ); | ||
| 311 | }; | ||
| 312 | |||
| 313 | return $key_alias; | ||
| 314 | } | ||
| 315 | |||
| 316 | ######################################################################## | ||
| 317 | sub main { | ||
| 318 | ######################################################################## | ||
| 319 | my @option_specs = qw( | ||
| 320 | help|h | ||
| 321 | input-path|i=s | ||
| 322 | key-alias|k=s | ||
| 323 | key-id|k=s | ||
| 324 | key-spec=s | ||
| 325 | key-usage=s | ||
| 326 | key-description=s | ||
| 327 | log-level|l=s | ||
| 328 | output-path|o=s | ||
| 329 | service|s=s | ||
| 330 | signature-file=s | ||
| 331 | signing-algorithm=s | ||
| 332 | profile=s | ||
| 333 | ); | ||
| 334 | |||
| 335 | my %commands = ( | ||
| 336 | 'create-provenance' => \&cmd_create_provenance, | ||
| 337 | 'export-pem' => \&cmd_export_pem, | ||
| 338 | 'describe-key' => \&cmd_describe_key, | ||
| 339 | 'create-key' => \&cmd_create_key, | ||
| 340 | 'sign' => \&cmd_sign, | ||
| 341 | 'verify' => \&cmd_verify, | ||
| 342 | ); | ||
| 343 | |||
| 344 | my $extra_options = [qw(kms ssm)]; | ||
| 345 | |||
| 346 | my $default_options = { | ||
| 347 | key_id => 'alias/provenance-signing', | ||
| 348 | key_alias => 'alias/provenance-signing', | ||
| 349 | signing_algorithm => $SIGNING_ALGORITHM, | ||
| 350 | }; | ||
| 351 | |||
| 352 | my $cli = __PACKAGE__->new( | ||
| 353 | commands => \%commands, | ||
| 354 | option_specs => \@option_specs, | ||
| 355 | default_options => $default_options, | ||
| 356 | extra_options => $extra_options, | ||
| 357 | ); | ||
| 358 | |||
| 359 | return $cli->run(); | ||
| 360 | } | ||
| 361 | |||
| 362 | 1; | ||
| 363 | |||
| 364 | ## no critic | ||
| 365 | |||
| 366 | __END__ |