Skip to content

Commit

Permalink
refactor(uptane): Refactor the Update methods
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptisteFoy committed Sep 15, 2022
1 parent bce4297 commit 9436cca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
37 changes: 14 additions & 23 deletions pkg/config/remote/uptane/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

rdata "github.com/DataDog/datadog-agent/pkg/config/remote/data"
"github.com/DataDog/datadog-agent/pkg/proto/pbgo"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// Client is an uptane client
Expand Down Expand Up @@ -70,43 +69,35 @@ func NewClient(cacheDB *bbolt.DB, cacheKey string, orgID int64) (*Client, error)
return c, nil
}

func (c *Client) rollback(err error) {
c.transactionalStore.rollback()
// Update updates the uptane client and rollbacks in case of error
func (c *Client) Update(response *pbgo.LatestConfigsResponse) error {
c.Lock()
defer c.Unlock()
c.cachedVerify = false

err := c.update(response)
if err != nil {
c.transactionalStore.rollback()
c.configRemoteStore = newRemoteStoreConfig(c.targetStore)
c.directorRemoteStore = newRemoteStoreDirector(c.targetStore)
c.configTUFClient = client.NewClient(c.configLocalStore, c.configRemoteStore)
c.directorTUFClient = client.NewClient(c.directorLocalStore, c.directorRemoteStore)
return err
}
return c.transactionalStore.commit()
}

// Update updates the uptane client
func (c *Client) Update(response *pbgo.LatestConfigsResponse) error {
c.Lock()
defer c.Unlock()
c.cachedVerify = false
var err error

// in case the commit is successful it is a no-op.
// the defer is present to be sure a transaction is never left behind.
defer func() { c.rollback(err) }()

err = c.updateRepos(response)
// update updates the uptane client
func (c *Client) update(response *pbgo.LatestConfigsResponse) error {
err := c.updateRepos(response)
if err != nil {
return err
}
err = c.pruneTargetFiles()
if err != nil {
return err
}
err = c.verify()
if err != nil {
return err
}
log.Infof("committing")

err = c.transactionalStore.commit()
return err
return c.verify()
}

// TargetsCustom returns the current targets custom of this uptane client
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/remote/uptane/remote_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *remoteStore) latestVersion(r role) uint64 {
return latestVersion
}

// GetMeta implements go-tuf's RemoteStore.<
// GetMeta implements go-tuf's RemoteStore.GetMeta
// See https://pkg.go.dev/github.com/theupdateframework/go-tuf/client#RemoteStore
func (s *remoteStore) GetMeta(path string) (io.ReadCloser, int64, error) {
metaPath, err := parseMetaPath(path)
Expand Down

0 comments on commit 9436cca

Please sign in to comment.