Skip to content

Commit 59027a4

Browse files
authored
Merge pull request #18395 from mlschroe/master
[backend] BSPublisher/Container: put consign_pubkey into the manifestinfo
2 parents c41e0f0 + c647a5d commit 59027a4

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

src/backend/BSPublisher/Container.pm

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ sub query_repostate {
497497
}
498498

499499
sub create_manifestinfo {
500-
my ($registry, $prp, $repository, $containerinfo, $imginfo) = @_;
500+
my ($registry, $prp, $repository, $containerinfo, $imginfo, $cosign) = @_;
501501

502502
my $dir = $registry->{'manifestinfos'};
503503
my $mani_id = $imginfo->{'distmanifest'};
@@ -519,6 +519,10 @@ sub create_manifestinfo {
519519
$_->{'base'} && ($_->{'base'} = \1) for @{$bins || []}; # turn flag to True
520520
$imginfo->{'packages'} = $bins if $bins;
521521
mkdir_p("$dir/$repository");
522+
if ($cosign) {
523+
$imginfo->{'cosign_cookie'} = $cosign->{'cookie'} if $cosign->{'cookie'};
524+
$imginfo->{'cosign_pubkey'} = $cosign->{'pubkey_fp'} if $cosign->{'pubkey_fp'};
525+
}
522526
my $imginfo_json = JSON::XS->new->utf8->canonical->encode($imginfo);
523527
unlink("$dir/$repository/.$mani_id.$$");
524528
writestr("$dir/$repository/.$mani_id.$$", "$dir/$repository/$mani_id", $imginfo_json);
@@ -615,7 +619,6 @@ sub upload_to_registry {
615619

616620
return '' unless @{$containerinfos || []} && @{$tags || []};
617621

618-
my ($pubkey, $signargs) = ($data->{'pubkey'}, $data->{'signargs'});
619622
my $registryserver = $registry->{pushserver} || $registry->{server};
620623
my $pullserver = $registry->{server};
621624
$pullserver =~ s/https?:\/\///;
@@ -632,10 +635,6 @@ sub upload_to_registry {
632635
$oci = 1 if grep {$_ && $_ ne 'gzip'} @{$containerinfo->{'layer_compression'} || []};
633636
}
634637

635-
my $gun = $registry->{'notary_gunprefix'} || $registry->{'server'};
636-
$gun =~ s/^https?:\/\///;
637-
$gun .= "/$repository";
638-
639638
# check if the registry is up-to-date
640639
if ($repostate && !($cosign && $cosign->{'force_rekor_upload'})) {
641640
my $taginfo = $data->{'regdata_cb'} ? {} : undef;
@@ -729,19 +728,19 @@ sub upload_to_registry {
729728
push @opts, '-m' if $multiarch;
730729
push @opts, '--oci' if $oci;
731730
push @opts, '-B', $blobdir if $blobdir;
732-
if ($cosign && $cosign->{'cookie'}) {
731+
if ($cosign) {
733732
my @signargs;
734733
push @signargs, '--project', $projid if $BSConfig::sign_project;
735-
push @signargs, @{$signargs || []};
734+
push @signargs, @{$cosign->{'signargs'} || []};
736735
my $pubkeyfile = "$uploaddir/publisher.$$.pubkey";
737736
push @tempfiles, $pubkeyfile;
738737
mkdir_p($uploaddir);
739738
unlink($pubkeyfile);
740-
writestr($pubkeyfile, undef, $pubkey);
739+
writestr($pubkeyfile, undef, $cosign->{'pubkey'});
741740
push @opts, '--cosign', '--cosigncookie', $cosign->{'cookie'};
742-
push @opts, '-p', $pubkeyfile, '-G', $gun, @signargs;
743-
push @opts, '--rekor', $registry->{'rekorserver'} if $registry->{'rekorserver'};
744-
push @opts, '--force-rekor-upload' if $registry->{'rekorserver'} && $cosign->{'force_rekor_upload'};
741+
push @opts, '-p', $pubkeyfile, '-G', $cosign->{'gun'}, @signargs;
742+
push @opts, '--rekor', $cosign->{'rekorserver'} if $cosign->{'rekorserver'};
743+
push @opts, '--force-rekor-upload' if $cosign->{'rekorserver'} && $cosign->{'force_rekor_upload'};
745744
push @opts, '--slsaprovenance' if $do_slsaprovenance;
746745
push @opts, '--sbom' if $do_sbom;
747746
}
@@ -756,6 +755,9 @@ sub upload_to_registry {
756755
process_regpush_error('uploading to registry', $result) if $result;
757756

758757
if ($data->{'notify'}) {
758+
my $gun = $registry->{'notary_gunprefix'} || $registry->{'server'};
759+
$gun =~ s/^https?:\/\///;
760+
$gun .= "/$repository";
759761
$data->{'notify'}->("$gun:$_") for @$tags;
760762
}
761763

@@ -770,7 +772,7 @@ sub upload_to_registry {
770772
next unless $imginfo->{'distmanifest'};
771773
my $containerinfo = $uploadfiles{delete $imginfo->{'file'}};
772774
$imginfo->{'containerinfo'} = $containerinfo;
773-
create_manifestinfo($registry, "$projid/$repoid", $repository, $containerinfo, $imginfo) if $registry->{'manifestinfos'};
775+
create_manifestinfo($registry, "$projid/$repoid", $repository, $containerinfo, $imginfo, $cosign) if $registry->{'manifestinfos'};
774776
}
775777
if ($data->{'regdata_cb'}) {
776778
for my $tag (@{$uploadinfo->{'tags'} || []}) {
@@ -821,8 +823,9 @@ sub add_notary_upload {
821823
return unless $registry->{'notary'};
822824
my $gun = $registry->{'notary_gunprefix'} || $registry->{'server'};
823825
$gun =~ s/^https?:\/\///;
824-
$notary_uploads->{"$gun/$repository"} ||= {'registry' => $registry, 'digests' => '', 'gun' => "$gun/$repository"};
825-
$notary_uploads->{"$gun/$repository"}->{'digests'} .= $digest if $digest;
826+
$gun .= "/$repository";
827+
$notary_uploads->{$gun} ||= {'registry' => $registry, 'digests' => '', 'gun' => $gun};
828+
$notary_uploads->{$gun}->{'digests'} .= $digest if $digest;
826829
}
827830

828831
=head2 upload_to_notary - do all the collected notary uploads
@@ -954,9 +957,7 @@ sub do_local_uploads {
954957
my $containerinfo = { 'type' => 'artifacthub', 'artifacthubdata' => $data->{'artifacthubdata'}->{"$gun/$repository"} };
955958
push @{$todo{'artifacthub.io'}}, $containerinfo;
956959
}
957-
eval {
958-
BSPublisher::Registry::push_containers($registry, $projid, $repoid, $repository, \%todo, $data);
959-
};
960+
eval { BSPublisher::Registry::push_containers($registry, $projid, $repoid, $repository, \%todo, $data) };
960961
unlink($_) for @tempfiles;
961962
die($@) if $@;
962963
printf "local updating of %s took %d seconds\n", $repository, time() - $now;
@@ -1047,8 +1048,14 @@ sub do_remote_uploads {
10471048
$cosign = $cosign ? {} : undef;
10481049
}
10491050
if ($cosign) {
1050-
my $creator = 'OBS';
1051-
$cosign->{'cookie'} = BSConSign::create_cosign_cookie($data->{'pubkey'}, $gun, $creator);
1051+
$cosign->{'creator'} = 'OBS';
1052+
$cosign->{'gun'} = $gun;
1053+
$cosign->{'pubkey'} = $data->{'pubkey'};
1054+
$cosign->{'signargs'} = $data->{'signargs'};
1055+
$cosign->{'pubkey_fp'} = BSPGP::pk2fingerprint(BSPGP::unarmor($data->{'pubkey'}));
1056+
$cosign->{'cookie'} = BSConSign::create_cosign_cookie($data->{'pubkey'}, $gun, $cosign->{'creator'});
1057+
$cosign->{'rekorserver'} = $registry->{'rekorserver'};
1058+
print "cosign cookie: $cosign->{'cookie'}\n";
10521059
my $cosign_attestation = defined($registry->{'cosign_attestation'}) ? $registry->{'cosign_attestation'} : 1;
10531060
$cosign_attestation = $cosign_attestation->($repository, $projid) if $cosign_attestation && ref($cosign_attestation) eq 'CODE';
10541061
$cosign->{'attestation'} = 1 if $cosign_attestation;

src/backend/BSPublisher/Registry.pm

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -475,19 +475,21 @@ sub create_cosign_attestation_ent {
475475
}
476476

477477
sub update_cosign {
478-
my ($prp, $repo, $gun, $digests_to_cosign, $pubkey, $signargs, $rekorserver, $knownmanifests, $knownblobs) = @_;
478+
my ($prp, $repo, $cosign, $digests_to_cosign, $knownmanifests, $knownblobs) = @_;
479479

480-
my $creator = 'OBS';
481480
my ($projid, $repoid) = split('/', $prp, 2);
481+
my $gun = $cosign->{'gun'};
482+
my $creator = $cosign->{'creator'};
483+
my $rekorserver = $cosign->{'rekorserver'};
482484
my @signcmd;
483485
push @signcmd, $BSConfig::sign;
484486
push @signcmd, '--project', $projid if $BSConfig::sign_project;
485-
push @signcmd, @{$signargs || []};
487+
push @signcmd, @{$cosign->{'signargs'} || []};
486488
my $signfunc = sub { BSUtil::xsystem($_[0], @signcmd, '-O', '-h', 'sha256') };
487489
my $repodir = "$registrydir/$repo";
488490
my $oldsigs = BSUtil::retrieve("$repodir/:cosign", 1) || {};
489491
return if !%$oldsigs && !%$digests_to_cosign;
490-
my $gpgpubkey = BSPGP::unarmor($pubkey);
492+
my $gpgpubkey = BSPGP::unarmor($cosign->{'pubkey'});
491493
my $pubkey_fp = BSPGP::pk2fingerprint($gpgpubkey);
492494
if (($oldsigs->{'pubkey'} || '') ne $pubkey_fp || ($oldsigs->{'gun'} || '') ne $gun || ($oldsigs->{'creator'} || '') ne ($creator || '')) {
493495
$oldsigs = {}; # fingerprint/gun/creator mismatch, do not use old signatures
@@ -585,7 +587,6 @@ sub push_containers {
585587

586588
my ($pubkey, $signargs) = ($data->{'pubkey'}, $data->{'signargs'});
587589

588-
my $rekorserver = $registry->{'rekorserver'};
589590
my $gun = $registry->{'notary_gunprefix'} || $registry->{'server'};
590591
undef $gun if $gun && $gun eq 'local:';
591592
if ($gun) {
@@ -784,7 +785,8 @@ sub push_containers {
784785

785786
# write signatures file (need to do this early as it adds manifests/blobs)
786787
if ($gun && defined($pubkey) && %digests_to_cosign) {
787-
update_cosign($prp, $repo, $gun, \%digests_to_cosign, $pubkey, $signargs, $rekorserver, \%knownmanifests, \%knownblobs);
788+
my $cosign = { 'creator' => 'OBS', 'gun' => $gun, 'pubkey' => $pubkey, 'signargs' => $signargs, 'rekorserver' => $registry->{'rekorserver'} };
789+
update_cosign($prp, $repo, $cosign, \%digests_to_cosign, \%knownmanifests, \%knownblobs);
788790
} elsif (-e "$repodir/:cosign") {
789791
unlink("$repodir/:cosign");
790792
}

0 commit comments

Comments
 (0)