Skip to content

Commit

Permalink
[backend] also use new BSConsign functions for cosign
Browse files Browse the repository at this point in the history
  • Loading branch information
mlschroe committed Apr 25, 2023
1 parent 5664974 commit 96ffc08
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/backend/BSContar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ sub get_config {
my $config_ent = $tar->{$config_file};
die("File $config_file not included in tar\n") unless $config_ent;
my $config_json = BSTar::extract($config_ent->{'file'}, $config_ent);
$config_ent->{'blobid'} ||= blobid($config_json); # convenience
my $config = JSON::XS::decode_json($config_json);
$config_ent->{'blobid'} = blobid($config_json); # convenience
return ($config_ent, $config);
}

Expand Down
35 changes: 15 additions & 20 deletions src/backend/BSPublisher/Registry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,17 @@ sub disownrepo {
}

sub push_blob {
my ($repodir, $containerinfo, $ent) = @_;
my ($repodir, $ent) = @_;

my $blobid = $ent->{'blobid'} || BSContar::blobid_entry($ent);
my $dir = "$repodir/:blobs";
return $blobid if -e "$dir/$blobid";
mkdir_p($dir) unless -d $dir;
unlink("$dir/.$blobid.$$");
if ($containerinfo->{'uploadfile'}) {
BSContar::write_entry($ent, "$dir/.$blobid.$$");
if ($ent->{'blobfile'}) {
link($ent->{'blobfile'}, "$dir/.$blobid.$$") || die("link $ent->{'blobfile'} $dir/.$blobid.$$: $!\n");
} else {
my $blobdir = $containerinfo->{'blobdir'};
link("$blobdir/_blob.$blobid", "$dir/.$blobid.$$") || die("link $blobdir/_blob.$blobid $dir/.$blobid.$$: $!\n");
BSContar::write_entry($ent, "$dir/.$blobid.$$");
}
rename("$dir/.$blobid.$$", "$dir/$blobid") || die("rename $dir/.$blobid.$$ $dir/$blobid: $!\n");
unlink("$dir/.$blobid.$$");
Expand Down Expand Up @@ -431,25 +430,17 @@ sub create_cosign_manifest {

my $config_blobid = push_blob_content($repodir, $config);
$knownblobs->{$config_blobid} = 1;
my $config_data = {
'mediaType' => $oci ? $BSContar::mt_oci_config : $BSContar::mt_docker_config,
'size' => length($config),
'digest' => $config_blobid,
};
my $mediaType = $oci ? $BSContar::mt_oci_manifest : $BSContar::mt_docker_manifest;
my $mani = {
'schemaVersion' => 2,
'mediaType' => $mediaType,
'config' => $config_data,
'layers' => [],
};
my $config_ent = { 'name' => 'config.json', 'size' => length($config), 'data' => $config, 'blobid' => $config_blobid };
my $config_data = BSContar::create_config_data($config_ent, $oci);
my @layer_data;
while (@payload_layers >= 2) {
my ($payload_layer, $payload) = splice(@payload_layers, 0, 2);
my $payload_blobid = push_blob_content($repodir, $payload);
$knownblobs->{$payload_blobid} = 1;
die unless $payload_blobid eq $payload_layer->{'digest'};
push @{$mani->{'layers'}}, $payload_layer;
push @layer_data, $payload_layer;
}
my $mani = BSContar::create_dist_manifest_data($config_data, \@layer_data, $oci);
my $mani_json = BSContar::create_dist_manifest($mani);
my $mani_id = push_manifest($repodir, $mani_json);
$knownmanifests->{$mani_id} = 1;
Expand Down Expand Up @@ -646,6 +637,10 @@ sub push_containers {
}
} else {
($tar, $mtime, $layer_compression) = BSPublisher::Containerinfo::construct_container_tar($containerinfo, 1);
# set blobfile in entries so we can create a link in push_blob
for (@$tar) {
$_->{'blobfile'} = "$containerinfo->{'blobdir'}/_blob.$_->{'blobid'}" if $_->{'blobid'};
}
}
my %tar = map {$_->{'name'} => $_} @$tar;

Expand Down Expand Up @@ -676,7 +671,7 @@ sub push_containers {
# put config blob into repo
my $config_data = BSContar::create_config_data($config_ent, $oci);
my $config_blobid = $config_ent->{'blobid'} = $config_data->{'digest'};
push_blob($repodir, $containerinfo, $config_ent);
push_blob($repodir, $config_ent);
$knownblobs{$config_blobid} = 1;

# put layer blobs into repo
Expand All @@ -698,7 +693,7 @@ sub push_containers {
$layer_datas{$layer_file} = $layer_data;

my $layer_blobid = $layer_ent->{'blobid'} = $layer_data->{'digest'};
push_blob($repodir, $containerinfo, $layer_ent);
push_blob($repodir, $layer_ent);
$knownblobs{$layer_blobid} = 1;
}
close $tarfd if $tarfd;
Expand Down
33 changes: 12 additions & 21 deletions src/backend/bs_regpush
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ sub blob_upload {
return $blobid;
}

sub blob_upload_content {
my ($blobid, $content) = @_;
return blob_upload($blobid, { 'data' => $content, 'size' => length($content) });
}

sub blob_download {
my ($blobid, $filename) = @_;
my $stdout_receiver = sub {
Expand Down Expand Up @@ -264,27 +259,23 @@ sub manifest_upload_tags {

sub cosign_upload {
my ($tag, $config, @layers) = @_;
my $config_blobid = blob_upload_content(BSContar::blobid($config), $config);
my $config_data = {
'mediaType' => $BSContar::mt_oci_config,
'size' => length($config),
'digest' => $config_blobid,
};
my $mediaType = $BSContar::mt_oci_manifest;
my $mani = {
'schemaVersion' => 2,
'mediaType' => $mediaType,
'config' => $config_data,
'layers' => [],
};
my $oci = 1;
my $config_blobid = BSContar::blobid($config);
my $config_ent = { 'name' => 'config.json', 'size' => length($config), 'data' => $config, 'blobid' => $config_blobid };
blob_upload($config_blobid, $config_ent);
my $config_data = BSContar::create_config_data($config_ent, $oci);
my @layer_data;
while (@layers >= 2) {
my ($payload_layer, $payload) = splice(@layers, 0, 2);
my $payload_blobid = blob_upload_content(BSContar::blobid($payload), $payload);
my $payload_blobid = BSContar::blobid($payload);
die unless $payload_blobid eq $payload_layer->{'digest'};
push @{$mani->{'layers'}}, $payload_layer;
my $payload_ent = { 'name' => $payload_blobid, 'size' => length($payload), 'data' => $payload, 'blobid' => $payload_blobid };
blob_upload($payload_blobid, $payload_ent);
push @layer_data, $payload_layer;
}
my $mani = BSContar::create_dist_manifest_data($config_data, \@layer_data, $oci);
my $mani_json = BSContar::create_dist_manifest_list($mani);
return manifest_upload($mani_json, $tag, $mediaType);
return manifest_upload($mani_json, $tag, $mani->{'mediaType'});
}

sub get_all_tags {
Expand Down

0 comments on commit 96ffc08

Please sign in to comment.