diff --git a/pkg/config/remote/uptane/client.go b/pkg/config/remote/uptane/client.go index a1bf554e37f921..19120f4fc445c4 100644 --- a/pkg/config/remote/uptane/client.go +++ b/pkg/config/remote/uptane/client.go @@ -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 @@ -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() @@ -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.updateRepos(response) + err = c.update(response) + return err +} + +// update updates the uptane client +func (c *Client) update(response *pbgo.LatestConfigsResponse) error { + err := c.updateRepos(response) if err != nil { return err } @@ -103,10 +106,8 @@ func (c *Client) Update(response *pbgo.LatestConfigsResponse) error { if err != nil { return err } - log.Infof("committing") - err = c.transactionalStore.commit() - return err + return c.transactionalStore.commit() } // TargetsCustom returns the current targets custom of this uptane client diff --git a/pkg/config/remote/uptane/remote_store.go b/pkg/config/remote/uptane/remote_store.go index 07889c1b2bc1eb..ff5f95f89869ab 100644 --- a/pkg/config/remote/uptane/remote_store.go +++ b/pkg/config/remote/uptane/remote_store.go @@ -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)