Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass logger into repo and client #385

Merged
merged 5 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions internal/fsutil/perm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package fsutil

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -59,7 +58,6 @@ func TestEnsureMaxPermissions(t *testing.T) {
assert.NoError(t, err)
err = EnsureMaxPermissions(fi, os.FileMode(0222))
assert.Error(t, err)
fmt.Println(err)

// Check matching due to more restrictive perms on file
err = os.Chmod(p, 0444)
Expand Down
31 changes: 29 additions & 2 deletions local_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -197,18 +198,44 @@ type persistedKeys struct {
Data json.RawMessage `json:"data"`
}

type StoreOpts struct {
Logger *log.Logger
PassFunc util.PassphraseFunc
}

func FileSystemStore(dir string, p util.PassphraseFunc) LocalStore {
return &fileSystemStore{
dir: dir,
passphraseFunc: p,
logger: log.New(io.Discard, "", 0),
signerForKeyID: make(map[string]keys.Signer),
keyIDsForRole: make(map[string][]string),
}
}

func FileSystemStoreWithOpts(dir string, opts ...StoreOpts) LocalStore {
store := &fileSystemStore{
dir: dir,
passphraseFunc: nil,
logger: log.New(io.Discard, "", 0),
signerForKeyID: make(map[string]keys.Signer),
keyIDsForRole: make(map[string][]string),
}
for _, opt := range opts {
if opt.Logger != nil {
store.logger = opt.Logger
}
if opt.PassFunc != nil {
store.passphraseFunc = opt.PassFunc
}
}
return store
}

type fileSystemStore struct {
dir string
passphraseFunc util.PassphraseFunc
logger *log.Logger

signerForKeyID map[string]keys.Signer
keyIDsForRole map[string][]string
Expand Down Expand Up @@ -526,7 +553,7 @@ func (f *fileSystemStore) ChangePassphrase(role string) error {
keys, _, err := f.loadPrivateKeys(role)
if err != nil {
if os.IsNotExist(err) {
fmt.Printf("Failed to change passphrase. Missing keys file for %s role. \n", role)
f.logger.Printf("Failed to change passphrase. Missing keys file for %s role. \n", role)
}
return err
}
Expand All @@ -548,7 +575,7 @@ func (f *fileSystemStore) ChangePassphrase(role string) error {
if err := util.AtomicallyWriteFile(f.keysPath(role), append(data, '\n'), 0600); err != nil {
return err
}
fmt.Printf("Successfully changed passphrase for %s keys file\n", role)
f.logger.Printf("Successfully changed passphrase for %s keys file\n", role)
return nil
}

Expand Down
2 changes: 0 additions & 2 deletions pkg/keys/deprecated_ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"io"
"os"

"github.com/theupdateframework/go-tuf/data"
)
Expand Down Expand Up @@ -98,6 +97,5 @@ func (p *deprecatedP256Verifier) UnmarshalPublicKey(key *data.PublicKey) error {
}

p.key = key
fmt.Fprintln(os.Stderr, "tuf: warning using deprecated ecdsa hex-encoded keys")
return nil
}
69 changes: 55 additions & 14 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
"log"
"path"
"sort"
"strings"
Expand Down Expand Up @@ -48,18 +49,47 @@ type Repo struct {
meta map[string]json.RawMessage
prefix string
indent string
logger *log.Logger
}

type RepoOpts func(r *Repo)

func WithLogger(logger *log.Logger) RepoOpts {
return func(r *Repo) {
r.logger = logger
}
}

func WithHashAlgorithms(hashAlgorithms ...string) RepoOpts {
return func(r *Repo) {
r.hashAlgorithms = hashAlgorithms
}
}

func WithPrefix(prefix string) RepoOpts {
return func(r *Repo) {
r.prefix = prefix
}
}

func WithIndex(indent string) RepoOpts {
return func(r *Repo) {
r.indent = indent
}
}

func NewRepo(local LocalStore, hashAlgorithms ...string) (*Repo, error) {
return NewRepoIndent(local, "", "", hashAlgorithms...)
}

func NewRepoIndent(local LocalStore, prefix string, indent string, hashAlgorithms ...string) (*Repo, error) {
func NewRepoIndent(local LocalStore, prefix string, indent string,
hashAlgorithms ...string) (*Repo, error) {
r := &Repo{
local: local,
hashAlgorithms: hashAlgorithms,
prefix: prefix,
indent: indent,
logger: log.New(io.Discard, "", 0),
}

var err error
Expand All @@ -70,6 +100,17 @@ func NewRepoIndent(local LocalStore, prefix string, indent string, hashAlgorithm
return r, nil
}

func NewRepoWithOpts(local LocalStore, opts ...RepoOpts) (*Repo, error) {
r, err := NewRepo(local)
if err != nil {
return nil, err
}
for _, opt := range opts {
opt(r)
}
return r, nil
}

func (r *Repo) Init(consistentSnapshot bool) error {
t, err := r.topLevelTargets()
if err != nil {
Expand All @@ -91,7 +132,7 @@ func (r *Repo) Init(consistentSnapshot bool) error {
return err
}

fmt.Println("Repository initialized")
r.logger.Println("Repository initialized")
return nil
}

Expand Down Expand Up @@ -533,7 +574,7 @@ func (r *Repo) RevokeKeyWithExpires(keyRole, id string, expires time.Time) error

err = r.setMeta("root.json", root)
if err == nil {
fmt.Println("Revoked", keyRole, "key with ID", id, "in root metadata")
r.logger.Println("Revoked", keyRole, "key with ID", id, "in root metadata")
}
return err
}
Expand Down Expand Up @@ -783,7 +824,7 @@ func (r *Repo) Sign(roleFilename string) error {
r.meta[roleFilename] = b
err = r.local.SetMeta(roleFilename, b)
if err == nil {
fmt.Println("Signed", roleFilename, "with", numKeys, "key(s)")
r.logger.Println("Signed", roleFilename, "with", numKeys, "key(s)")
}
return err
}
Expand Down Expand Up @@ -1223,7 +1264,7 @@ func (r *Repo) removeTargetsWithExpiresFromMeta(metaName string, paths []string,
for _, path := range paths {
path = util.NormalizeTarget(path)
if _, ok := t.Targets[path]; !ok {
fmt.Printf("[%v] The following target is not present: %v\n", metaName, path)
r.logger.Printf("[%v] The following target is not present: %v\n", metaName, path)
continue
}
removed = true
Expand All @@ -1243,17 +1284,17 @@ func (r *Repo) removeTargetsWithExpiresFromMeta(metaName string, paths []string,

err = r.setMeta(metaName, t)
if err == nil {
fmt.Printf("[%v] Removed targets:\n", metaName)
r.logger.Printf("[%v] Removed targets:\n", metaName)
for _, v := range removed_targets {
fmt.Println("*", v)
r.logger.Println("*", v)
}
if len(t.Targets) != 0 {
fmt.Printf("[%v] Added/staged targets:\n", metaName)
r.logger.Printf("[%v] Added/staged targets:\n", metaName)
for k := range t.Targets {
fmt.Println("*", k)
r.logger.Println("*", k)
}
} else {
fmt.Printf("[%v] There are no added/staged targets\n", metaName)
r.logger.Printf("[%v] There are no added/staged targets\n", metaName)
}
}
return err
Expand Down Expand Up @@ -1307,7 +1348,7 @@ func (r *Repo) SnapshotWithExpires(expires time.Time) error {
}
err = r.setMeta("snapshot.json", snapshot)
if err == nil {
fmt.Println("Staged snapshot.json metadata with expiration date:", snapshot.Expires)
r.logger.Println("Staged snapshot.json metadata with expiration date:", snapshot.Expires)
}
return err
}
Expand Down Expand Up @@ -1339,7 +1380,7 @@ func (r *Repo) TimestampWithExpires(expires time.Time) error {

err = r.setMeta("timestamp.json", timestamp)
if err == nil {
fmt.Println("Staged timestamp.json metadata with expiration date:", timestamp.Expires)
r.logger.Println("Staged timestamp.json metadata with expiration date:", timestamp.Expires)
}
return err
}
Expand Down Expand Up @@ -1505,15 +1546,15 @@ func (r *Repo) Commit() error {

err = r.local.Commit(root.ConsistentSnapshot, versions, hashes)
if err == nil {
fmt.Println("Committed successfully")
r.logger.Println("Committed successfully")
}
return err
}

func (r *Repo) Clean() error {
err := r.local.Clean()
if err == nil {
fmt.Println("Removed all staged metadata and target files")
r.logger.Println("Removed all staged metadata and target files")
}
return err
}
Expand Down
9 changes: 8 additions & 1 deletion repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -1422,7 +1423,12 @@ func (rs *RepoSuite) TestKeyPersistence(c *C) {
// Test changing the passphrase
// 1. Create a secure store with a passphrase (create new object and temp folder so we discard any previous state)
tmp = newTmpDir(c)
store = FileSystemStore(tmp.path, testPassphraseFunc)
var logBytes bytes.Buffer
storeOpts := StoreOpts{
Logger: log.New(&logBytes, "", 0),
PassFunc: testPassphraseFunc,
}
store = FileSystemStoreWithOpts(tmp.path, storeOpts)

// 1.5. Changing passphrase works for top-level and delegated roles.
r, err := NewRepo(store)
Expand All @@ -1433,6 +1439,7 @@ func (rs *RepoSuite) TestKeyPersistence(c *C) {

// 2. Test changing the passphrase when the keys file does not exist - should FAIL
c.Assert(store.(PassphraseChanger).ChangePassphrase("root"), NotNil)
c.Assert(strings.Contains(logBytes.String(), "Missing keys file"), Equals, true)

// 3. Generate a new key
signer, err = keys.GenerateEd25519Key()
Expand Down