@@ -2,6 +2,7 @@ package storage
22
33import (
44 "context"
5+ "crypto"
56 "encoding/json"
67 "fmt"
78 "io"
@@ -22,19 +23,23 @@ const (
2223)
2324
2425type Signature struct {
25- // SignDesignExtensions is a flag that determines if the signature should
26- // include the extension payloads .
27- signExtensions bool
26+ // Signer if provided, will be used to sign extensions. If not provided,
27+ // no extensions will be signed .
28+ Signer crypto. Signer
2829 Storage
2930}
3031
31- func NewSignatureStorage (signExtensions bool , s Storage ) * Signature {
32+ func NewSignatureStorage (signer crypto. Signer , s Storage ) * Signature {
3233 return & Signature {
33- signExtensions : signExtensions ,
34- Storage : s ,
34+ Signer : signer ,
35+ Storage : s ,
3536 }
3637}
3738
39+ func (s * Signature ) SigningEnabled () bool {
40+ return s .Signer != nil
41+ }
42+
3843// AddExtension includes the signature manifest of the vsix. Signing happens on
3944// demand, so leave the manifest unsigned. This is safe to do even if
4045// 'signExtensions' is disabled, as these files lay dormant until signed.
@@ -61,7 +66,7 @@ func (s *Signature) Manifest(ctx context.Context, publisher, name string, versio
6166 return nil , err
6267 }
6368
64- if s .signExtensions {
69+ if s .SigningEnabled () {
6570 manifest .Assets .Asset = append (manifest .Assets .Asset , VSIXAsset {
6671 Type : VSIXSignatureType ,
6772 Path : sigzipFilename ,
@@ -72,11 +77,11 @@ func (s *Signature) Manifest(ctx context.Context, publisher, name string, versio
7277}
7378
7479func (s * Signature ) Open (ctx context.Context , fp string ) (fs.File , error ) {
75- if s .signExtensions && filepath .Base (fp ) == "p7s.sig" {
80+ if s .SigningEnabled () && filepath .Base (fp ) == "p7s.sig" {
7681 // This file must exist, and it is always empty
7782 return mem .NewFileHandle (mem .CreateFile ("p7s.sig" )), nil
7883 }
79- if s .signExtensions && filepath .Base (fp ) == sigzipFilename {
84+ if s .SigningEnabled () && filepath .Base (fp ) == sigzipFilename {
8085 // hijack this request, sign the sig manifest
8186 manifest , err := s .Storage .Open (ctx , filepath .Join (filepath .Dir (fp ), sigManifestName ))
8287 if err != nil {
@@ -85,13 +90,12 @@ func (s *Signature) Open(ctx context.Context, fp string) (fs.File, error) {
8590 }
8691 defer manifest .Close ()
8792
88- key , _ := extensionsign .GenerateKey ()
8993 manifestData , err := io .ReadAll (manifest )
9094 if err != nil {
9195 return nil , xerrors .Errorf ("read signature manifest: %w" , err )
9296 }
9397
94- signed , err := extensionsign .SignAndZipManifest (key , manifestData )
98+ signed , err := extensionsign .SignAndZipManifest (s . Signer , manifestData )
9599 if err != nil {
96100 return nil , xerrors .Errorf ("sign and zip manifest: %w" , err )
97101 }
0 commit comments