Skip to content

Commit c4fef46

Browse files
kommendorkaptenrdimitrov
authored andcommitted
Remove logr dep (#83)
* 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]>
1 parent 9cd17b5 commit c4fef46

File tree

10 files changed

+88
-69
lines changed

10 files changed

+88
-69
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vscode
22
dist/
3-
.idea/
3+
.idea/
4+
*~

Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# notices and license terms. Your use of these subcomponents is subject to
77
# the terms and conditions of the subcomponent's license, as noted in the
88
# LICENSE file.
9-
#
9+
#
1010
# SPDX-License-Identifier: BSD-2-Clause
1111

1212
# We want to use bash
@@ -39,17 +39,21 @@ build-%:
3939

4040
# Test target
4141
.PHONY: test
42-
test:
42+
test:
4343
go test -race -covermode atomic ./...
4444

4545
#####################
4646
# lint section
4747
#####################
4848

4949
.PHONY: lint
50-
lint:
50+
lint:
5151
golangci-lint run
5252

53+
.PHONY: fmt
54+
fmt:
55+
go fmt ./...
56+
5357
#####################
5458
# examples section
5559
#####################
@@ -60,13 +64,13 @@ example-all: example-client example-repository example-multirepo example-tuf-cli
6064

6165
# Target for demoing the examples/client/client_example.go
6266
.PHONY: example-client
63-
example-client:
67+
example-client:
6468
@echo "Executing the following example - client/client_example.go"
6569
@cd examples/client/ && go run .
6670

6771
# Target for demoing the examples/repository/basic_repository.go
6872
.PHONY: example-repository
69-
example-repository:
73+
example-repository:
7074
@echo "Executing the following example - repository/basic_repository.go"
7175
@cd examples/repository/ && go run .
7276

@@ -114,4 +118,3 @@ clean:
114118
@rm -rf tuf_metadata
115119
@rm -f tuf-client
116120
@rm -f root.json
117-

examples/client/client_example.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func DownloadTarget(localMetadataDir, target string) error {
173173
return fmt.Errorf("failed while finding a cached target: %w", err)
174174
}
175175
if path != "" {
176-
log.V(4).Info("Target is already present", "target", target, "path", path)
176+
log.Info("Target is already present", "target", target, "path", path)
177177
}
178178

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

185-
log.V(4).Info("Successfully downloaded target", "target", target, "path", path)
185+
log.Info("Successfully downloaded target", "target", target, "path", path)
186186

187187
return nil
188188
}

examples/multirepo/client/client_example.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func BootstrapTUF() ([]byte, map[string][]byte, error) {
127127
return nil, nil, fmt.Errorf("failed while finding a cached target: %w", err)
128128
}
129129
if path != "" {
130-
log.V(4).Info("Target is already present", "target", name, "path", path)
130+
log.Info("Target is already present", "target", name, "path", path)
131131
}
132132

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

158158
return mapBytes, trustedRoots, nil

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/rdimitrov/go-tuf-metadata
33
go 1.21
44

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

1817
require (
1918
github.com/davecgh/go-spew v1.1.1 // indirect
19+
github.com/go-logr/logr v1.3.0 // indirect
2020
github.com/golang/protobuf v1.5.3 // indirect
2121
github.com/google/go-containerregistry v0.16.1 // indirect
2222
github.com/inconshreveable/mousetrap v1.1.0 // indirect

metadata/logger.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@
1111

1212
package metadata
1313

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

16-
var log logr.Logger = logr.Discard()
16+
// Logger partially implements the go-log/logr's interface:
17+
// https://github.com/go-logr/logr/blob/master/logr.go
18+
type Logger interface {
19+
// Info logs a non-error message with key/value pairs
20+
Info(msg string, kv ...any)
21+
// Error logs an error with a given message and key/value pairs.
22+
Error(err error, msg string, kv ...any)
23+
}
24+
25+
type DiscardLogger struct{}
26+
27+
func (d DiscardLogger) Info(msg string, kv ...any) {
28+
}
29+
30+
func (d DiscardLogger) Error(err error, msg string, kv ...any) {
31+
}
1732

18-
func SetLogger(logger logr.Logger) {
33+
func SetLogger(logger Logger) {
1934
log = logger
2035
}
2136

22-
func GetLogger() logr.Logger {
37+
func GetLogger() Logger {
2338
return log
2439
}

metadata/metadata.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func Root(expires ...time.Time) *Metadata[RootType] {
5050
Threshold: 1,
5151
}
5252
}
53-
log.V(5).Info("Created metadata", "type", ROOT)
53+
log.Info("Created metadata", "type", ROOT)
5454
return &Metadata[RootType]{
5555
Signed: RootType{
5656
Type: ROOT,
@@ -71,7 +71,7 @@ func Snapshot(expires ...time.Time) *Metadata[SnapshotType] {
7171
if len(expires) == 0 {
7272
expires = []time.Time{time.Now().UTC()}
7373
}
74-
log.V(5).Info("Created metadata", "type", SNAPSHOT)
74+
log.Info("Created metadata", "type", SNAPSHOT)
7575
return &Metadata[SnapshotType]{
7676
Signed: SnapshotType{
7777
Type: SNAPSHOT,
@@ -94,7 +94,7 @@ func Timestamp(expires ...time.Time) *Metadata[TimestampType] {
9494
if len(expires) == 0 {
9595
expires = []time.Time{time.Now().UTC()}
9696
}
97-
log.V(5).Info("Created metadata", "type", TIMESTAMP)
97+
log.Info("Created metadata", "type", TIMESTAMP)
9898
return &Metadata[TimestampType]{
9999
Signed: TimestampType{
100100
Type: TIMESTAMP,
@@ -117,7 +117,7 @@ func Targets(expires ...time.Time) *Metadata[TargetsType] {
117117
if len(expires) == 0 {
118118
expires = []time.Time{time.Now().UTC()}
119119
}
120-
log.V(5).Info("Created metadata", "type", TARGETS)
120+
log.Info("Created metadata", "type", TARGETS)
121121
return &Metadata[TargetsType]{
122122
Signed: TargetsType{
123123
Type: TARGETS,
@@ -142,7 +142,7 @@ func TargetFile() *TargetFiles {
142142
func MetaFile(version int64) *MetaFiles {
143143
if version < 1 {
144144
// attempting to set incorrect version
145-
log.V(5).Info("Attempting to set incorrect version for MetaFile", "version", version)
145+
log.Info("Attempting to set incorrect version for MetaFile", "version", version)
146146
version = 1
147147
}
148148
return &MetaFiles{
@@ -168,7 +168,7 @@ func (meta *Metadata[T]) FromFile(name string) (*Metadata[T], error) {
168168
return nil, err
169169
}
170170
*meta = *m
171-
log.V(5).Info("Loaded metadata from file", "name", name)
171+
log.Info("Loaded metadata from file", "name", name)
172172
return meta, nil
173173
}
174174

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

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

195195
// ToFile save metadata to file
196196
func (meta *Metadata[T]) ToFile(name string, pretty bool) error {
197-
log.V(5).Info("Writing metadata to file", "name", name)
197+
log.Info("Writing metadata to file", "name", name)
198198
data, err := meta.ToBytes(pretty)
199199
if err != nil {
200200
return err
@@ -232,7 +232,7 @@ func (meta *Metadata[T]) Sign(signer signature.Signer) (*Signature, error) {
232232
// update the Signatures part
233233
meta.Signatures = append(meta.Signatures, *sig)
234234
// return the new signature
235-
log.V(4).Info("Signed metadata with key", "ID", key.ID())
235+
log.Info("Signed metadata with key", "ID", key.ID())
236236
return sig, nil
237237
}
238238

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

248-
log.V(5).Info("Verifying", "role", delegatedRole)
248+
log.Info("Verifying", "role", delegatedRole)
249249

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

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

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

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

499499
// ClearSignatures clears Signatures
500500
func (meta *Metadata[T]) ClearSignatures() {
501-
log.V(5).Info("Cleared signatures")
501+
log.Info("Cleared signatures")
502502
meta.Signatures = []Signature{}
503503
}
504504

@@ -713,7 +713,7 @@ func (signed *TargetsType) AddKey(key *Key, role string) error {
713713
signed.Delegations.Keys[key.ID()] = key // TODO: should we check if we don't accidentally override an existing keyID with another key value?
714714
return nil
715715
}
716-
log.V(5).Info("Delegated role already has keyID", "role", role, "ID", key.ID())
716+
log.Info("Delegated role already has keyID", "role", role, "ID", key.ID())
717717
}
718718
}
719719
if !isDelegatedRole {
@@ -726,7 +726,7 @@ func (signed *TargetsType) AddKey(key *Key, role string) error {
726726
signed.Delegations.Keys[key.ID()] = key // TODO: should we check if we don't accidentally override an existing keyID with another key value?
727727
return nil
728728
}
729-
log.V(5).Info("SuccinctRoles role already has keyID", "ID", key.ID())
729+
log.Info("SuccinctRoles role already has keyID", "ID", key.ID())
730730

731731
}
732732
signed.Delegations.Keys[key.ID()] = key // TODO: should we check if we don't accidentally override an existing keyID with another key value?

metadata/multirepo/multirepo.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (client *MultiRepoClient) initTUFClients() error {
105105

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

110110
// get the trusted root file from the location specified in the map file relevant to its path
111111
// 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
@@ -149,7 +149,7 @@ func (client *MultiRepoClient) initTUFClients() error {
149149

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

161161
// loop through each initialized TUF client and refresh it
162162
for name, repoTUFClient := range client.TUFClients {
163-
log.V(4).Info("Refreshing", "name", name)
163+
log.Info("Refreshing", "name", name)
164164
err := repoTUFClient.Refresh()
165165
if err != nil {
166166
return err
@@ -312,7 +312,7 @@ func (client *MultiRepoClient) DownloadTarget(repos []string, targetFile *metada
312312
}
313313
if len(targetPath) != 0 && len(targetBytes) != 0 {
314314
// we already got the target for this target info cached locally, so return it
315-
log.V(4).Info("Target already present locally from repo", "target", targetFile.Path, "repo", repoName)
315+
log.Info("Target already present locally from repo", "target", targetFile.Path, "repo", repoName)
316316
return targetPath, targetBytes, nil
317317
}
318318
// not present locally, so let's try to download it
@@ -323,7 +323,7 @@ func (client *MultiRepoClient) DownloadTarget(repos []string, targetFile *metada
323323
continue
324324
}
325325
// we got the target for this target info, so return it
326-
log.V(4).Info("Downloaded target from repo", "target", targetFile.Path, "repo", repoName)
326+
log.Info("Downloaded target from repo", "target", targetFile.Path, "repo", repoName)
327327
return targetPath, targetBytes, nil
328328
}
329329
// error out as we haven't succeeded downloading the target file

0 commit comments

Comments
 (0)