File tree Expand file tree Collapse file tree 3 files changed +75
-9
lines changed
Expand file tree Collapse file tree 3 files changed +75
-9
lines changed Original file line number Diff line number Diff line change 1+ package collectionsign
2+
3+ import (
4+ "errors"
5+ "fmt"
6+
7+ "OpenSPMRegistry/config"
8+ )
9+
10+ // LoadSignerForPackageCollections returns a Signer when package collections and signing are enabled in config.
11+ // It returns (nil, nil) when signing is disabled. It returns an error if signing is enabled but misconfigured
12+ // or if loading keys/certs fails.
13+ func LoadSignerForPackageCollections (pc config.PackageCollectionsConfig ) (* Signer , error ) {
14+ if ! pc .Signing .Enabled {
15+ return nil , nil
16+ }
17+ if ! pc .Enabled {
18+ return nil , errors .New ("packageCollections.signing.enabled requires packageCollections.enabled" )
19+ }
20+ if len (pc .Signing .CertChain ) == 0 || pc .Signing .PrivateKey == "" {
21+ return nil , errors .New ("packageCollections.signing.enabled requires certChain (non-empty) and privateKey" )
22+ }
23+ s , err := NewSignerFromFiles (pc .Signing .CertChain , pc .Signing .PrivateKey )
24+ if err != nil {
25+ return nil , fmt .Errorf ("package collection signing: %w" , err )
26+ }
27+ return s , nil
28+ }
Original file line number Diff line number Diff line change 1+ package collectionsign
2+
3+ import (
4+ "testing"
5+
6+ "OpenSPMRegistry/config"
7+ )
8+
9+ func TestLoadSignerForPackageCollections_Disabled (t * testing.T ) {
10+ s , err := LoadSignerForPackageCollections (config.PackageCollectionsConfig {
11+ Enabled : true ,
12+ Signing : config.PackageCollectionsSigningConfig {Enabled : false },
13+ })
14+ if err != nil {
15+ t .Fatal (err )
16+ }
17+ if s != nil {
18+ t .Fatal ("expected nil signer" )
19+ }
20+ }
21+
22+ func TestLoadSignerForPackageCollections_SigningWithoutCollections (t * testing.T ) {
23+ _ , err := LoadSignerForPackageCollections (config.PackageCollectionsConfig {
24+ Enabled : false ,
25+ Signing : config.PackageCollectionsSigningConfig {Enabled : true },
26+ })
27+ if err == nil {
28+ t .Fatal ("expected error" )
29+ }
30+ }
31+
32+ func TestLoadSignerForPackageCollections_MissingPaths (t * testing.T ) {
33+ _ , err := LoadSignerForPackageCollections (config.PackageCollectionsConfig {
34+ Enabled : true ,
35+ Signing : config.PackageCollectionsSigningConfig {
36+ Enabled : true ,
37+ },
38+ })
39+ if err == nil {
40+ t .Fatal ("expected error" )
41+ }
42+ }
Original file line number Diff line number Diff line change @@ -102,16 +102,12 @@ func main() {
102102 }
103103 a := middleware .NewAuthentication (authenticator .CreateAuthenticator (serverConfig .Server ), registryMux )
104104
105+ signer , err := collectionsign .LoadSignerForPackageCollections (serverConfig .Server .PackageCollections )
106+ if err != nil {
107+ log .Fatal (err )
108+ }
105109 var collOpts []controller.ControllerOption
106- pc := serverConfig .Server .PackageCollections
107- if pc .Enabled && pc .Signing .Enabled {
108- if len (pc .Signing .CertChain ) == 0 || pc .Signing .PrivateKey == "" {
109- log .Fatal ("packageCollections.signing.enabled requires certChain (non-empty) and privateKey" )
110- }
111- signer , err := collectionsign .NewSignerFromFiles (pc .Signing .CertChain , pc .Signing .PrivateKey )
112- if err != nil {
113- log .Fatalf ("package collection signing: %v" , err )
114- }
110+ if signer != nil {
115111 collOpts = append (collOpts , controller .WithCollectionSigner (signer ))
116112 slog .Info ("Package collection JWS signing enabled" )
117113 }
You can’t perform that action at this time.
0 commit comments