Skip to content

Commit

Permalink
Remove logr dep (#83)
Browse files Browse the repository at this point in the history
* Ignore temporary files from emacs (ends qith '~')

Signed-off-by: Fredrik Skogman <[email protected]>

* Remove dep of go-logr/logr.
Provided is an (almost) logr compatible interface.

Signed-off-by: Fredrik Skogman <[email protected]>

* Remove V method from logger.

Signed-off-by: Fredrik Skogman <[email protected]>

* remove unnecessary variable

Signed-off-by: Fredrik Skogman <[email protected]>

* Removed unnecessary code change

Signed-off-by: Fredrik Skogman <[email protected]>

* ran go mod tidy

Signed-off-by: Fredrik Skogman <[email protected]>

---------

Signed-off-by: Fredrik Skogman <[email protected]>
  • Loading branch information
kommendorkapten authored and rdimitrov committed Jan 29, 2024
1 parent 9cd17b5 commit c4fef46
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
dist/
.idea/
.idea/
*~
15 changes: 9 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# notices and license terms. Your use of these subcomponents is subject to
# the terms and conditions of the subcomponent's license, as noted in the
# LICENSE file.
#
#
# SPDX-License-Identifier: BSD-2-Clause

# We want to use bash
Expand Down Expand Up @@ -39,17 +39,21 @@ build-%:

# Test target
.PHONY: test
test:
test:
go test -race -covermode atomic ./...

#####################
# lint section
#####################

.PHONY: lint
lint:
lint:
golangci-lint run

.PHONY: fmt
fmt:
go fmt ./...

#####################
# examples section
#####################
Expand All @@ -60,13 +64,13 @@ example-all: example-client example-repository example-multirepo example-tuf-cli

# Target for demoing the examples/client/client_example.go
.PHONY: example-client
example-client:
example-client:
@echo "Executing the following example - client/client_example.go"
@cd examples/client/ && go run .

# Target for demoing the examples/repository/basic_repository.go
.PHONY: example-repository
example-repository:
example-repository:
@echo "Executing the following example - repository/basic_repository.go"
@cd examples/repository/ && go run .

Expand Down Expand Up @@ -114,4 +118,3 @@ clean:
@rm -rf tuf_metadata
@rm -f tuf-client
@rm -f root.json

4 changes: 2 additions & 2 deletions examples/client/client_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func DownloadTarget(localMetadataDir, target string) error {
return fmt.Errorf("failed while finding a cached target: %w", err)
}
if path != "" {
log.V(4).Info("Target is already present", "target", target, "path", path)
log.Info("Target is already present", "target", target, "path", path)
}

// target is not present locally, so let's try to download it
Expand All @@ -182,7 +182,7 @@ func DownloadTarget(localMetadataDir, target string) error {
return fmt.Errorf("failed to download target file %s - %w", target, err)
}

log.V(4).Info("Successfully downloaded target", "target", target, "path", path)
log.Info("Successfully downloaded target", "target", target, "path", path)

return nil
}
4 changes: 2 additions & 2 deletions examples/multirepo/client/client_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func BootstrapTUF() ([]byte, map[string][]byte, error) {
return nil, nil, fmt.Errorf("failed while finding a cached target: %w", err)
}
if path != "" {
log.V(4).Info("Target is already present", "target", name, "path", path)
log.Info("Target is already present", "target", name, "path", path)
}

// target is not present locally, so let's try to download it
Expand All @@ -152,7 +152,7 @@ func BootstrapTUF() ([]byte, map[string][]byte, error) {
repositoryName := strings.Split(name, string(os.PathSeparator))
trustedRoots[repositoryName[0]] = bytes
}
log.V(4).Info("Successfully downloaded target", "target", name, "path", path)
log.Info("Successfully downloaded target", "target", name, "path", path)
}

return mapBytes, trustedRoots, nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/rdimitrov/go-tuf-metadata
go 1.21

require (
github.com/go-logr/logr v1.3.0
github.com/go-logr/stdr v1.2.2
github.com/secure-systems-lab/go-securesystemslib v0.7.0
github.com/sigstore/sigstore v1.7.5
Expand All @@ -17,6 +16,7 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-containerregistry v0.16.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
23 changes: 19 additions & 4 deletions metadata/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@

package metadata

import "github.com/go-logr/logr"
var log Logger = DiscardLogger{}

var log logr.Logger = logr.Discard()
// Logger partially implements the go-log/logr's interface:
// https://github.com/go-logr/logr/blob/master/logr.go
type Logger interface {
// Info logs a non-error message with key/value pairs
Info(msg string, kv ...any)
// Error logs an error with a given message and key/value pairs.
Error(err error, msg string, kv ...any)
}

type DiscardLogger struct{}

func (d DiscardLogger) Info(msg string, kv ...any) {
}

func (d DiscardLogger) Error(err error, msg string, kv ...any) {
}

func SetLogger(logger logr.Logger) {
func SetLogger(logger Logger) {
log = logger
}

func GetLogger() logr.Logger {
func GetLogger() Logger {
return log
}
40 changes: 20 additions & 20 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Root(expires ...time.Time) *Metadata[RootType] {
Threshold: 1,
}
}
log.V(5).Info("Created metadata", "type", ROOT)
log.Info("Created metadata", "type", ROOT)
return &Metadata[RootType]{
Signed: RootType{
Type: ROOT,
Expand All @@ -71,7 +71,7 @@ func Snapshot(expires ...time.Time) *Metadata[SnapshotType] {
if len(expires) == 0 {
expires = []time.Time{time.Now().UTC()}
}
log.V(5).Info("Created metadata", "type", SNAPSHOT)
log.Info("Created metadata", "type", SNAPSHOT)
return &Metadata[SnapshotType]{
Signed: SnapshotType{
Type: SNAPSHOT,
Expand All @@ -94,7 +94,7 @@ func Timestamp(expires ...time.Time) *Metadata[TimestampType] {
if len(expires) == 0 {
expires = []time.Time{time.Now().UTC()}
}
log.V(5).Info("Created metadata", "type", TIMESTAMP)
log.Info("Created metadata", "type", TIMESTAMP)
return &Metadata[TimestampType]{
Signed: TimestampType{
Type: TIMESTAMP,
Expand All @@ -117,7 +117,7 @@ func Targets(expires ...time.Time) *Metadata[TargetsType] {
if len(expires) == 0 {
expires = []time.Time{time.Now().UTC()}
}
log.V(5).Info("Created metadata", "type", TARGETS)
log.Info("Created metadata", "type", TARGETS)
return &Metadata[TargetsType]{
Signed: TargetsType{
Type: TARGETS,
Expand All @@ -142,7 +142,7 @@ func TargetFile() *TargetFiles {
func MetaFile(version int64) *MetaFiles {
if version < 1 {
// attempting to set incorrect version
log.V(5).Info("Attempting to set incorrect version for MetaFile", "version", version)
log.Info("Attempting to set incorrect version for MetaFile", "version", version)
version = 1
}
return &MetaFiles{
Expand All @@ -168,7 +168,7 @@ func (meta *Metadata[T]) FromFile(name string) (*Metadata[T], error) {
return nil, err
}
*meta = *m
log.V(5).Info("Loaded metadata from file", "name", name)
log.Info("Loaded metadata from file", "name", name)
return meta, nil
}

Expand All @@ -179,13 +179,13 @@ func (meta *Metadata[T]) FromBytes(data []byte) (*Metadata[T], error) {
return nil, err
}
*meta = *m
log.V(5).Info("Loaded metadata from bytes")
log.Info("Loaded metadata from bytes")
return meta, nil
}

// ToBytes serialize metadata to bytes
func (meta *Metadata[T]) ToBytes(pretty bool) ([]byte, error) {
log.V(5).Info("Writing metadata to bytes")
log.Info("Writing metadata to bytes")
if pretty {
return json.MarshalIndent(*meta, "", "\t")
}
Expand All @@ -194,7 +194,7 @@ func (meta *Metadata[T]) ToBytes(pretty bool) ([]byte, error) {

// ToFile save metadata to file
func (meta *Metadata[T]) ToFile(name string, pretty bool) error {
log.V(5).Info("Writing metadata to file", "name", name)
log.Info("Writing metadata to file", "name", name)
data, err := meta.ToBytes(pretty)
if err != nil {
return err
Expand Down Expand Up @@ -232,7 +232,7 @@ func (meta *Metadata[T]) Sign(signer signature.Signer) (*Signature, error) {
// update the Signatures part
meta.Signatures = append(meta.Signatures, *sig)
// return the new signature
log.V(4).Info("Signed metadata with key", "ID", key.ID())
log.Info("Signed metadata with key", "ID", key.ID())
return sig, nil
}

Expand All @@ -245,7 +245,7 @@ func (meta *Metadata[T]) VerifyDelegate(delegatedRole string, delegatedMetadata
var roleKeyIDs []string
var roleThreshold int

log.V(5).Info("Verifying", "role", delegatedRole)
log.Info("Verifying", "role", delegatedRole)

// collect keys, keyIDs and threshold based on delegator type
switch i := i.(type) {
Expand Down Expand Up @@ -362,19 +362,19 @@ func (meta *Metadata[T]) VerifyDelegate(delegatedRole string, delegatedMetadata
// verify if the signature for that payload corresponds to the given key
if err := verifier.VerifySignature(bytes.NewReader(sign.Signature), bytes.NewReader(payload)); err != nil {
// failed to verify the metadata with that key ID
log.V(5).Info("Failed to verify %s with key ID %s", delegatedRole, keyID)
log.Info("Failed to verify %s with key ID %s", delegatedRole, keyID)
} else {
// save the verified keyID only if verification passed
signingKeys[keyID] = true
log.V(5).Info("Verified with key", "role", delegatedRole, "ID", keyID)
log.Info("Verified with key", "role", delegatedRole, "ID", keyID)
}
}
// check if the amount of valid signatures is enough
if len(signingKeys) < roleThreshold {
log.V(4).Info("Verifying failed, not enough signatures", "role", delegatedRole, "got", len(signingKeys), "want", roleThreshold)
log.Info("Verifying failed, not enough signatures", "role", delegatedRole, "got", len(signingKeys), "want", roleThreshold)
return ErrUnsignedMetadata{Msg: fmt.Sprintf("Verifying %s failed, not enough signatures, got %d, want %d", delegatedRole, len(signingKeys), roleThreshold)}
}
log.V(4).Info("Verified successfully", "role", delegatedRole)
log.Info("Verified successfully", "role", delegatedRole)
return nil
}

Expand Down Expand Up @@ -445,7 +445,7 @@ func (source *TargetFiles) Equal(expected TargetFiles) bool {

// FromFile generate TargetFiles from file
func (t *TargetFiles) FromFile(localPath string, hashes ...string) (*TargetFiles, error) {
log.V(5).Info("Generating target file from file", "path", localPath)
log.Info("Generating target file from file", "path", localPath)
// open file
in, err := os.Open(localPath)
if err != nil {
Expand All @@ -462,7 +462,7 @@ func (t *TargetFiles) FromFile(localPath string, hashes ...string) (*TargetFiles

// FromBytes generate TargetFiles from bytes
func (t *TargetFiles) FromBytes(localPath string, data []byte, hashes ...string) (*TargetFiles, error) {
log.V(5).Info("Generating target file from bytes", "path", localPath)
log.Info("Generating target file from bytes", "path", localPath)
var hasher hash.Hash
targetFile := &TargetFiles{
Hashes: map[string]HexBytes{},
Expand Down Expand Up @@ -498,7 +498,7 @@ func (t *TargetFiles) FromBytes(localPath string, data []byte, hashes ...string)

// ClearSignatures clears Signatures
func (meta *Metadata[T]) ClearSignatures() {
log.V(5).Info("Cleared signatures")
log.Info("Cleared signatures")
meta.Signatures = []Signature{}
}

Expand Down Expand Up @@ -713,7 +713,7 @@ func (signed *TargetsType) AddKey(key *Key, role string) error {
signed.Delegations.Keys[key.ID()] = key // TODO: should we check if we don't accidentally override an existing keyID with another key value?
return nil
}
log.V(5).Info("Delegated role already has keyID", "role", role, "ID", key.ID())
log.Info("Delegated role already has keyID", "role", role, "ID", key.ID())
}
}
if !isDelegatedRole {
Expand All @@ -726,7 +726,7 @@ func (signed *TargetsType) AddKey(key *Key, role string) error {
signed.Delegations.Keys[key.ID()] = key // TODO: should we check if we don't accidentally override an existing keyID with another key value?
return nil
}
log.V(5).Info("SuccinctRoles role already has keyID", "ID", key.ID())
log.Info("SuccinctRoles role already has keyID", "ID", key.ID())

}
signed.Delegations.Keys[key.ID()] = key // TODO: should we check if we don't accidentally override an existing keyID with another key value?
Expand Down
10 changes: 5 additions & 5 deletions metadata/multirepo/multirepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (client *MultiRepoClient) initTUFClients() error {

// loop through each repository listed in the map file and initialize it
for repoName, repoURL := range client.Config.RepoMap.Repositories {
log.V(4).Info("Initializing", "name", repoName, "url", repoURL[0])
log.Info("Initializing", "name", repoName, "url", repoURL[0])

// get the trusted root file from the location specified in the map file relevant to its path
// NOTE: the root.json file is expected to be in a folder named after the repository it corresponds to placed in the same folder as the map file
Expand Down Expand Up @@ -149,7 +149,7 @@ func (client *MultiRepoClient) initTUFClients() error {

// save the client
client.TUFClients[repoName] = repoTUFClient
log.V(5).Info("Successfully initialized", "name", repoName, "url", repoURL)
log.Info("Successfully initialized", "name", repoName, "url", repoURL)
}
return nil
}
Expand All @@ -160,7 +160,7 @@ func (client *MultiRepoClient) Refresh() error {

// loop through each initialized TUF client and refresh it
for name, repoTUFClient := range client.TUFClients {
log.V(4).Info("Refreshing", "name", name)
log.Info("Refreshing", "name", name)
err := repoTUFClient.Refresh()
if err != nil {
return err
Expand Down Expand Up @@ -312,7 +312,7 @@ func (client *MultiRepoClient) DownloadTarget(repos []string, targetFile *metada
}
if len(targetPath) != 0 && len(targetBytes) != 0 {
// we already got the target for this target info cached locally, so return it
log.V(4).Info("Target already present locally from repo", "target", targetFile.Path, "repo", repoName)
log.Info("Target already present locally from repo", "target", targetFile.Path, "repo", repoName)
return targetPath, targetBytes, nil
}
// not present locally, so let's try to download it
Expand All @@ -323,7 +323,7 @@ func (client *MultiRepoClient) DownloadTarget(repos []string, targetFile *metada
continue
}
// we got the target for this target info, so return it
log.V(4).Info("Downloaded target from repo", "target", targetFile.Path, "repo", repoName)
log.Info("Downloaded target from repo", "target", targetFile.Path, "repo", repoName)
return targetPath, targetBytes, nil
}
// error out as we haven't succeeded downloading the target file
Expand Down
Loading

0 comments on commit c4fef46

Please sign in to comment.