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 1cbe60c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
32 changes: 17 additions & 15 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,17 +69,7 @@ func NewClient(cacheDB *bbolt.DB, cacheKey string, orgID int64) (*Client, error)
return c, nil
}

func (c *Client) rollback(err error) {
c.transactionalStore.rollback()
if err != nil {
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)
}
}

// Update updates the uptane client
// Update updates the uptane client and rollbacks in case of error
func (c *Client) Update(response *pbgo.LatestConfigsResponse) error {
c.Lock()
defer c.Unlock()
Expand All @@ -89,9 +78,23 @@ func (c *Client) Update(response *pbgo.LatestConfigsResponse) 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) }()
defer func() {
c.transactionalStore.rollback()
if err != nil {
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)
}
}()

err = c.update(response)
return 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
}
Expand All @@ -103,7 +106,6 @@ func (c *Client) Update(response *pbgo.LatestConfigsResponse) error {
if err != nil {
return err
}
log.Infof("committing")

err = c.transactionalStore.commit()
return err
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 1cbe60c

Please sign in to comment.