Skip to content

Commit

Permalink
Merge pull request #14233 from mlschroe/master
Browse files Browse the repository at this point in the history
[backend] also use new BSConsign functions for cosign
  • Loading branch information
mlschroe committed Apr 26, 2023
2 parents 71d515a + dd0f266 commit 9c3e4cd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 61 deletions.
12 changes: 9 additions & 3 deletions src/backend/BSContar.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ sub blobid {
return 'sha256:'.Digest::SHA::sha256_hex($_[0]);
}

sub make_blob_entry {
my ($name, $blob, %extra) = @_;
my $blobid = blobid($blob);
my $ent = { %extra, 'name' => $name, 'size' => length($blob), 'data' => $blob, 'blobid' => $blobid };
return ($ent, $blobid);
}

sub checksum_entry {
my ($ent, $ctx) = @_;
my $offset = 0;
Expand Down Expand Up @@ -262,8 +269,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 Expand Up @@ -423,8 +430,7 @@ sub container_from_helm {
push @layercomp, '';
}
# create ent for the config
my $config_ent = { 'name' => 'config.json', 'size' => length($config_json), 'data' => $config_json, 'mtime' => $mtime };
$config_ent->{'mimetype'} = $mt_helm_config;
my ($config_ent) = make_blob_entry('config.json', $config_json, 'mtime' => $mtime, 'mimetype' => $mt_helm_config);
# create ent for the manifest
my $manifest = {
'Layers' => [ $chartbasename ],
Expand Down
55 changes: 20 additions & 35 deletions src/backend/BSPublisher/Registry.pm
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,24 @@ 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.$$");
#BSPublisher::Blobstore::blobstore_lnk($blobid, "$dir/$blobid");
return $blobid;
}

sub push_blob_content {
my ($repodir, $content) = @_;
my $blob_id = BSContar::blobid($content);
my $dir = "$repodir/:blobs";
return $blob_id if -e "$dir/$blob_id";
mkdir_p($dir) unless -d $dir;
unlink("$dir/.$blob_id.$$");
writestr("$dir/.$blob_id.$$", "$dir/$blob_id", $content);
return $blob_id;
}

sub push_manifest {
my ($repodir, $mani_json) = @_;
my $mani_id = BSContar::blobid($mani_json);
Expand Down Expand Up @@ -429,27 +417,20 @@ sub update_sigs {
sub create_cosign_manifest {
my ($repodir, $oci, $knownmanifests, $knownblobs, $config, @payload_layers) = @_;

my $config_blobid = push_blob_content($repodir, $config);
my ($config_ent, $config_blobid) = BSContar::make_blob_entry('config.json', $config);
push_blob($repodir, $config_ent);
$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_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);
my ($payload_layer_data, $payload) = splice(@payload_layers, 0, 2);
my ($payload_ent, $payload_blobid) = BSContar::make_blob_entry($payload_layer_data->{'digest'}, $payload);
die unless $payload_blobid eq $payload_layer_data->{'digest'};
push_blob($repodir, $payload_ent);
$knownblobs->{$payload_blobid} = 1;
die unless $payload_blobid eq $payload_layer->{'digest'};
push @{$mani->{'layers'}}, $payload_layer;
push @layer_data, $payload_layer_data;
}
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 +627,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 +661,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 +683,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
35 changes: 12 additions & 23 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,21 @@ 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_ent, $config_blobid) = BSContar::make_blob_entry('config.json', $config);
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);
die unless $payload_blobid eq $payload_layer->{'digest'};
push @{$mani->{'layers'}}, $payload_layer;
my ($payload_layer_data, $payload) = splice(@layers, 0, 2);
my ($payload_ent, $payload_blobid) = BSContar::make_blob_entry($payload_layer_data->{'digest'}, $payload);
die unless $payload_blobid eq $payload_layer_data->{'digest'};
blob_upload($payload_blobid, $payload_ent);
push @layer_data, $payload_layer_data;
}
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 9c3e4cd

Please sign in to comment.