Skip to content

Commit 0cdd5a3

Browse files
committed
setup signer opts pass through
1 parent 404d3a7 commit 0cdd5a3

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

cli/server.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"cdr.dev/slog"
1717
"cdr.dev/slog/sloggers/sloghuman"
18+
"github.com/coder/code-marketplace/extensionsign"
1819

1920
"github.com/coder/code-marketplace/api"
2021
"github.com/coder/code-marketplace/database"
@@ -23,12 +24,14 @@ import (
2324

2425
func serverFlags() (addFlags func(cmd *cobra.Command), opts *storage.Options) {
2526
opts = &storage.Options{}
27+
var sign bool
2628
return func(cmd *cobra.Command) {
2729
cmd.Flags().StringVar(&opts.ExtDir, "extensions-dir", "", "The path to extensions.")
2830
cmd.Flags().StringVar(&opts.Artifactory, "artifactory", "", "Artifactory server URL.")
2931
cmd.Flags().StringVar(&opts.Repo, "repo", "", "Artifactory repository.")
3032
cmd.Flags().DurationVar(&opts.ListCacheDuration, "list-cache-duration", time.Minute, "The duration of the extension cache.")
31-
cmd.Flags().BoolVar(&opts.SignExtensions, "sign", false, "Sign extensions.")
33+
cmd.Flags().BoolVar(&sign, "sign", false, "Sign extensions.")
34+
_ = cmd.Flags().MarkHidden("sign") // This flag needs to import a key, not just be a bool
3235

3336
var before func(cmd *cobra.Command, args []string) error
3437
if cmd.PreRunE != nil {
@@ -47,6 +50,9 @@ func serverFlags() (addFlags func(cmd *cobra.Command), opts *storage.Options) {
4750
if before != nil {
4851
return before(cmd, args)
4952
}
53+
if sign { // TODO: Remove this for an actual key import
54+
opts.Signer, _ = extensionsign.GenerateKey()
55+
}
5056
return nil
5157
}
5258
}, opts

storage/signature.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package storage
22

33
import (
44
"context"
5+
"crypto"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -22,19 +23,23 @@ const (
2223
)
2324

2425
type 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

7479
func (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
}

storage/storage.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package storage
22

33
import (
44
"context"
5+
"crypto"
56
"encoding/json"
67
"encoding/xml"
78
"fmt"
@@ -127,7 +128,7 @@ type VSIXAsset struct {
127128
}
128129

129130
type Options struct {
130-
SignExtensions bool
131+
Signer crypto.Signer
131132
Artifactory string
132133
ExtDir string
133134
Repo string
@@ -292,7 +293,7 @@ func NewStorage(ctx context.Context, options *Options) (Storage, error) {
292293
return nil, err
293294
}
294295

295-
return NewSignatureStorage(options.SignExtensions, store), nil
296+
return NewSignatureStorage(options.Signer, store), nil
296297
}
297298

298299
// ReadVSIXManifest reads and parses an extension manifest from a vsix file. If

0 commit comments

Comments
 (0)