Skip to content

Commit

Permalink
feat: replace logrus in sim with slog
Browse files Browse the repository at this point in the history
This removes another thirdparty dependecy without sacrificing
any functionality.

Signed-off-by: Marvin Drees <[email protected]>
  • Loading branch information
MDr164 committed Mar 7, 2024
1 parent 064b4f6 commit 4cdbf0e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 90 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/go-logr/stdr v1.2.2
github.com/secure-systems-lab/go-securesystemslib v0.8.0
github.com/sigstore/sigstore v1.8.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
)
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -52,14 +51,10 @@ github.com/secure-systems-lab/go-securesystemslib v0.8.0 h1:mr5An6X45Kb2nddcFlbm
github.com/secure-systems-lab/go-securesystemslib v0.8.0/go.mod h1:UH2VZVuJfCYR8WgMlCU1uFsOUU+KeyrTWcSS73NBOzU=
github.com/sigstore/sigstore v1.8.2 h1:0Ttjcn3V0fVQXlYq7+oHaaHkGFIt3ywm7SF4JTU/l8c=
github.com/sigstore/sigstore v1.8.2/go.mod h1:CHVcSyknCcjI4K2ZhS1SI28r0tcQyBlwtALG536x1DY=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/titanous/rocacheck v0.0.0-20171023193734-afe73141d399 h1:e/5i7d4oYZ+C1wj2THlRK+oAhjeS/TRQwMfkIuet3w0=
Expand All @@ -70,7 +65,6 @@ go.opentelemetry.io/otel/trace v1.15.0 h1:5Fwje4O2ooOxkfyqI/kJwxWotggDLix4BSAvpE
go.opentelemetry.io/otel/trace v1.15.0/go.mod h1:CUsmE2Ht1CRkvE8OsMESvraoZrrcgD1J2W8GV1ev0Y4=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
Expand All @@ -86,6 +80,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/go-jose/go-jose.v2 v2.6.1 h1:qEzJlIDmG9q5VO0M/o8tGS65QMHMS1w01TQJB1VPJ4U=
gopkg.in/go-jose/go-jose.v2 v2.6.1/go.mod h1:zzZDPkNNw/c9IE7Z9jr11mBZQhKQTMzoEEIoEdZlFBI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
112 changes: 56 additions & 56 deletions internal/testutils/simulator/repository_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"crypto/ed25519"
"crypto/sha256"
"fmt"
"log/slog"
"net/url"
"os"
"path/filepath"
Expand All @@ -64,7 +65,6 @@ import (
"time"

"github.com/sigstore/sigstore/pkg/signature"
log "github.com/sirupsen/logrus"
"github.com/theupdateframework/go-tuf/v2/metadata"
"github.com/theupdateframework/go-tuf/v2/metadata/fetcher"
)
Expand Down Expand Up @@ -165,12 +165,12 @@ func (rs *RepositorySimulator) setupMinimalValidRepository() {

mtdkey, err := metadata.KeyFromPublicKey(*publicKey)
if err != nil {
log.Fatalf("repository simulator: key conversion failed while setting repository: %v", err)
slog.Error("Repository simulator: key conversion failed while setting repository", "err", err)
os.Exit(1)
}

err = rs.MDRoot.Signed.AddKey(mtdkey, role)
if err != nil {
log.Debugf("repository simulator: failed to add key: %v", err)
if err = rs.MDRoot.Signed.AddKey(mtdkey, role); err != nil {
slog.Error("Repository simulator: failed to add key", "err", err)
}
rs.AddSigner(role, mtdkey.ID(), *signer)
}
Expand Down Expand Up @@ -215,13 +215,14 @@ func (rs *RepositorySimulator) AllTargets() <-chan metadata.TargetsType {
func CreateKey() (*ed25519.PublicKey, *ed25519.PrivateKey, *signature.Signer) {
public, private, err := ed25519.GenerateKey(nil)
if err != nil {
log.Printf("failed to generate key: %v", err)
slog.Error("Failed to generate key", "err", err)
}

signer, err := signature.LoadSigner(private, crypto.Hash(0))
if err != nil {
log.Printf("failed to load signer: %v", err)
slog.Error("failed to load signer", "err", err)
}

return &public, &private, &signer
}

Expand All @@ -238,16 +239,16 @@ func (rs *RepositorySimulator) RotateKeys(role string) {
for k := range rs.Signers[role] {
delete(rs.Signers[role], k)
}
for i := 0; i < rs.MDRoot.Signed.Roles[role].Threshold; i++ {

for i := 0; i < rs.MDRoot.Signed.Roles[role].Threshold; i++ {
publicKey, _, signer := CreateKey()
mtdkey, err := metadata.KeyFromPublicKey(*publicKey)
if err != nil {
log.Fatalf("repository simulator: key conversion failed while rotating keys: %v", err)
slog.Error("Repository simulator: key conversion failed while rotating keys", "err", err)
os.Exit(1)
}
err = rs.MDRoot.Signed.AddKey(mtdkey, role)
if err != nil {
log.Debugf("repository simulator: failed to add key: %v", err)
if err = rs.MDRoot.Signed.AddKey(mtdkey, role); err != nil {
slog.Error("Repository simulator: failed to add key", "err", err)
}
rs.AddSigner(role, mtdkey.ID(), *signer)
}
Expand All @@ -257,18 +258,18 @@ func (rs *RepositorySimulator) RotateKeys(role string) {
func (rs *RepositorySimulator) PublishRoot() {
rs.MDRoot.ClearSignatures()
for _, signer := range rs.Signers[metadata.ROOT] {
_, err := rs.MDRoot.Sign(*signer)
if err != nil {
log.Debugf("repository simulator: failed to sign root: %v", err)
if _, err := rs.MDRoot.Sign(*signer); err != nil {
slog.Error("Repository simulator: failed to sign root", "err", err)
}
}

mtd, err := rs.MDRoot.MarshalJSON()
if err != nil {
log.Debugf("failed to marshal metadata while publishing root: %v", err)
slog.Error("Failed to marshal metadata while publishing root", "err", err)
}
rs.SignedRoots = append(rs.SignedRoots, mtd)
log.Debugf("published root v%d", rs.MDRoot.Signed.Version)

slog.Info("Published root", "version", rs.MDRoot.Signed.Version)
}

func lastIndex(str string, delimiter string) (string, string, string) {
Expand Down Expand Up @@ -344,7 +345,6 @@ func hasSuffix(path, prefix string) bool {
}

func (rs *RepositorySimulator) fetch(urlPath string) ([]byte, error) {

path, err := trimPrefix(urlPath, rs.LocalDir)
if err != nil {
return nil, err
Expand All @@ -358,7 +358,7 @@ func (rs *RepositorySimulator) fetch(urlPath string) ([]byte, error) {
if role == metadata.ROOT || (rs.MDRoot.Signed.ConsistentSnapshot && verAndName != metadata.TIMESTAMP) {
version, err = strconv.Atoi(versionStr)
if err != nil {
log.Printf("repository simulator: downloading file: failed to convert version: %v", err)
slog.Error("Repository simulator: downloading file: failed to convert version", "err", err)
}
} else {
role = verAndName
Expand All @@ -377,7 +377,7 @@ func (rs *RepositorySimulator) fetch(urlPath string) ([]byte, error) {
targetPath = filepath.Join(dirParts, sep, filename)
target, err := rs.FetchTarget(targetPath, prefix)
if err != nil {
log.Printf("failed to fetch target: %v", err)
slog.Error("Failed to fetch target", "err", err)
}
return target, err
}
Expand All @@ -392,10 +392,12 @@ func (rs *RepositorySimulator) FetchTarget(targetPath string, targetHash string)
if !ok {
return nil, fmt.Errorf("no target %s", targetPath)
}

if targetHash != "" && !contains(repoTarget.TargetFile.Hashes, []byte(targetHash)) {
return nil, fmt.Errorf("hash mismatch for %s", targetPath)
}
log.Printf("fetched target %s", targetPath)

slog.Info("Fetched target", "path", targetPath)
return repoTarget.Data, nil
}

Expand All @@ -417,10 +419,10 @@ func (rs *RepositorySimulator) FetchMetadata(role string, version *int) ([]byte,
if role == metadata.ROOT {
// Return a version previously serialized in PublishRoot()
if version == nil || *version > len(rs.SignedRoots) && *version > 0 {
log.Printf("unknown root version %d", *version)
slog.Error("Unknown root version", "version", *version)
return []byte{}, &metadata.ErrDownloadHTTP{StatusCode: 404}
}
log.Printf("fetched root version %d", version)
slog.Info("Fetched root", "version", version)
return rs.SignedRoots[*version-1], nil
}

Expand All @@ -434,7 +436,7 @@ func (rs *RepositorySimulator) FetchMetadata(role string, version *int) ([]byte,
} else {
md, ok := rs.MDDelegates[role]
if !ok {
log.Printf("unknown role %s", role)
slog.Error("Unknown role", "role", role)
return []byte{}, &metadata.ErrDownloadHTTP{StatusCode: 404}
}
return signMetadata(role, &md, rs)
Expand All @@ -446,16 +448,15 @@ func signMetadata[T metadata.Roles](role string, md *metadata.Metadata[T], rs *R
for _, signer := range rs.Signers[role] {
// TODO: check if a bool argument should be added to Sign as in python-tuf
// Not appending only for a local repo example !!! missing type for signers
_, err := md.Sign(*signer)
if err != nil {
log.Debugf("repository simulator: failed to sign metadata: %v", err)
if _, err := md.Sign(*signer); err != nil {
slog.Error("Repository simulator: failed to sign metadata", "err", err)
}
}
// TODO: test if the version is the correct one
// log.Printf("fetched %s v%d with %d sigs", role, md.GetVersion(), len(rs.Signers[role]))
mtd, err := md.MarshalJSON()
if err != nil {
log.Printf("failed to marshal metadata while signing for role %s: %v", role, err)
slog.Error("Failed to marshal metadata while signing for role", "role", role, "err", err)
}
return mtd, err
}
Expand All @@ -464,7 +465,7 @@ func (rs *RepositorySimulator) computeHashesAndLength(role string) (map[string]m
noVersion := -1
data, err := rs.FetchMetadata(role, &noVersion)
if err != nil {
log.Debugf("failed to fetch metadata: %v", err)
slog.Error("Failed to fetch metadata", "err", err)
}
digest := sha256.Sum256(data)
hashes := map[string]metadata.HexBytes{"sha256": digest[:]}
Expand Down Expand Up @@ -522,7 +523,8 @@ func (rs *RepositorySimulator) AddTarget(role string, data []byte, path string)
targets := rs.getDelegator(role)
target, err := metadata.TargetFile().FromBytes(path, data, "sha256")
if err != nil {
log.Panicf("failed to add target from %s: %v", path, err)
slog.Error("Failed to add target", "path", path, "err", err)
os.Exit(1)
}
targets.Targets[path] = target
rs.TargetFiles[path] = RepositoryTarget{
Expand All @@ -535,7 +537,8 @@ func (rs *RepositorySimulator) AddTarget(role string, data []byte, path string)
func (rs *RepositorySimulator) AddDelegation(delegatorName string, role metadata.DelegatedRole, targets metadata.TargetsType) {
delegator := rs.getDelegator(delegatorName)
if delegator.Delegations != nil && delegator.Delegations.SuccinctRoles != nil {
log.Fatalln("can't add a role when SuccinctRoles is used")
slog.Error("Can't add a role when SuccinctRoles is used")
os.Exit(1)
}
// Create delegation
if delegator.Delegations == nil {
Expand All @@ -551,11 +554,11 @@ func (rs *RepositorySimulator) AddDelegation(delegatorName string, role metadata
publicKey, _, signer := CreateKey()
mdkey, err := metadata.KeyFromPublicKey(*publicKey)
if err != nil {
log.Fatalf("repository simulator: key conversion failed while adding delegation: %v", err)
slog.Error("Repository simulator: key conversion failed while adding delegation", "err", err)
os.Exit(1)
}
err = delegator.AddKey(mdkey, role.Name)
if err != nil {
log.Debugf("repository simulator: failed to add key: %v", err)
if err = delegator.AddKey(mdkey, role.Name); err != nil {
slog.Error("Repository simulator: failed to add key", "err", err)
}
rs.AddSigner(role.Name, mdkey.ID(), *signer)
if _, ok := rs.MDDelegates[role.Name]; !ok {
Expand All @@ -573,12 +576,14 @@ func (rs *RepositorySimulator) AddDelegation(delegatorName string, role metadata
func (rs *RepositorySimulator) AddSuccinctRoles(delegatorName string, bitLength int, namePrefix string) {
delegator := rs.getDelegator(delegatorName)
if delegator.Delegations != nil && delegator.Delegations.Roles != nil {
log.Fatalln("can't add a SuccinctRoles when delegated roles are used")
slog.Error("Can't add a SuccinctRoles when delegated roles are used")
os.Exit(1)
}
publicKey, _, signer := CreateKey()
mdkey, err := metadata.KeyFromPublicKey(*publicKey)
if err != nil {
log.Fatalf("repository simulator: key conversion failed while adding succinct roles: %v", err)
slog.Error("Repository simulator: key conversion failed while adding succinct roles", "err", err)
os.Exit(1)
}
succinctRoles := &metadata.SuccinctRoles{
KeyIDs: []string{},
Expand All @@ -596,9 +601,8 @@ func (rs *RepositorySimulator) AddSuccinctRoles(delegatorName string, bitLength
}
rs.AddSigner(delegatedName, mdkey.ID(), *signer)
}
err = delegator.AddKey(mdkey, metadata.TARGETS)
if err != nil {
log.Debugf("repository simulator: failed to add key: %v", err)
if err = delegator.AddKey(mdkey, metadata.TARGETS); err != nil {
slog.Error("Repository simulator: failed to add key", "err", err)
}
}

Expand All @@ -609,24 +613,22 @@ func (rs *RepositorySimulator) AddSuccinctRoles(delegatorName string, bitLength
func (rs *RepositorySimulator) Write() {
if rs.DumpDir == "" {
rs.DumpDir = os.TempDir()
log.Debugf("Repository Simulator dumps in %s\n", rs.DumpDir)
slog.Info("Repository Simulator dumps into tmp dir", "path", rs.DumpDir)
}
rs.DumpVersion += 1
destDir := filepath.Join(rs.DumpDir, strconv.Itoa(int(rs.DumpVersion)))
err := os.MkdirAll(destDir, os.ModePerm)
if err != nil {
log.Debugf("repository simulator: failed to create dir: %v", err)
if err := os.MkdirAll(destDir, os.ModePerm); err != nil {
slog.Error("Repository simulator: failed to create dir", "err", err)
}
for ver := 1; ver < len(rs.SignedRoots)+1; ver++ {
f, _ := os.Create(filepath.Join(destDir, fmt.Sprintf("%d.root.json", ver)))
defer f.Close()
meta, err := rs.FetchMetadata(metadata.ROOT, &ver)
if err != nil {
log.Debugf("failed to fetch metadata: %v", err)
slog.Error("Failed to fetch metadata", "err", err)
}
_, err = f.Write(meta)
if err != nil {
log.Debugf("repository simulator: failed to write signed roots: %v", err)
if _, err = f.Write(meta); err != nil {
slog.Error("Repository simulator: failed to write signed roots", "err", err)
}
}
noVersion := -1
Expand All @@ -635,11 +637,10 @@ func (rs *RepositorySimulator) Write() {
defer f.Close()
meta, err := rs.FetchMetadata(role, &noVersion)
if err != nil {
log.Debugf("failed to fetch metadata: %v", err)
slog.Error("Failed to fetch metadata", "err", err)
}
_, err = f.Write(meta)
if err != nil {
log.Debugf("repository simulator: failed to write signed roots: %v", err)
if _, err = f.Write(meta); err != nil {
slog.Error("Repository simulator: failed to write signed roots", "err", err)
}
}
for role := range rs.MDDelegates {
Expand All @@ -648,11 +649,10 @@ func (rs *RepositorySimulator) Write() {
defer f.Close()
meta, err := rs.FetchMetadata(role, &noVersion)
if err != nil {
log.Debugf("failed to fetch metadata: %v", err)
slog.Error("Failed to fetch metadata", "err", err)
}
_, err = f.Write(meta)
if err != nil {
log.Debugf("repository simulator: failed to write signed roots: %v", err)
if _, err = f.Write(meta); err != nil {
slog.Error("Repository simulator: failed to write signed roots", "err", err)
}
}
}
Loading

0 comments on commit 4cdbf0e

Please sign in to comment.