Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset RC DB in case an agent changes the RC url #34832

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/config/remote/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func NewService(cfg model.Reader, rcType, baseRawURL, hostname string, tagsGette
databaseFilePath = options.databaseFilePath
}
dbPath := path.Join(databaseFilePath, options.databaseFileName)
db, err := openCacheDB(dbPath, agentVersion, authKeys.apiKey)
db, err := openCacheDB(dbPath, agentVersion, authKeys.apiKey, baseURL.String())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1053,7 +1053,7 @@ type HTTPClient struct {
// An HTTPClient must be closed via HTTPClient.Close() before creating a new one.
func NewHTTPClient(runPath, site, apiKey, agentVersion string) (*HTTPClient, error) {
dbPath := path.Join(runPath, "remote-config-cdn.db")
db, err := openCacheDB(dbPath, agentVersion, apiKey)
db, err := openCacheDB(dbPath, agentVersion, apiKey, site)
if err != nil {
return nil, err
}
Expand Down
24 changes: 13 additions & 11 deletions pkg/config/remote/service/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ type AgentMetadata struct {
Version string `json:"version"`
APIKeyHash string `json:"api-key-hash"`
CreationTime time.Time `json:"creation-time"`
URL string `json:"url"`
}

// hashAPIKey hashes the API key to avoid storing it in plain text using SHA256
func hashAPIKey(apiKey string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(apiKey)))
}

func recreate(path string, agentVersion string, apiKeyHash string) (*bbolt.DB, error) {
func recreate(path string, agentVersion string, apiKeyHash string, url string) (*bbolt.DB, error) {
log.Infof("Clear remote configuration database")
_, err := os.Stat(path)
if err != nil && !os.IsNotExist(err) {
Expand All @@ -69,10 +70,10 @@ func recreate(path string, agentVersion string, apiKeyHash string) (*bbolt.DB, e
}
return nil, err
}
return db, addMetadata(db, agentVersion, apiKeyHash)
return db, addMetadata(db, agentVersion, apiKeyHash, url)
}

func addMetadata(db *bbolt.DB, agentVersion string, apiKeyHash string) error {
func addMetadata(db *bbolt.DB, agentVersion string, apiKeyHash string, url string) error {
return db.Update(func(tx *bbolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(metaBucket))
if err != nil {
Expand All @@ -82,6 +83,7 @@ func addMetadata(db *bbolt.DB, agentVersion string, apiKeyHash string) error {
Version: agentVersion,
APIKeyHash: apiKeyHash,
CreationTime: time.Now(),
URL: url,
})
if err != nil {
return err
Expand All @@ -90,7 +92,7 @@ func addMetadata(db *bbolt.DB, agentVersion string, apiKeyHash string) error {
})
}

func openCacheDB(path string, agentVersion string, apiKey string) (*bbolt.DB, error) {
func openCacheDB(path string, agentVersion string, apiKey string, url string) (*bbolt.DB, error) {
apiKeyHash := hashAPIKey(apiKey)

db, err := bbolt.Open(path, 0600, &bbolt.Options{
Expand All @@ -101,10 +103,10 @@ func openCacheDB(path string, agentVersion string, apiKey string) (*bbolt.DB, er
return nil, fmt.Errorf("rc db is locked. Please check if another instance of the agent is running and using the same `run_path` parameter")
}
log.Infof("Failed to open remote configuration database %s", err)
return recreate(path, agentVersion, apiKeyHash)
return recreate(path, agentVersion, apiKeyHash, url)
}

metadata := new(AgentMetadata)
var metadata AgentMetadata
err = db.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(metaBucket))
if bucket == nil {
Expand All @@ -116,7 +118,7 @@ func openCacheDB(path string, agentVersion string, apiKey string) (*bbolt.DB, er
log.Infof("Missing meta file in meta bucket")
return err
}
err = json.Unmarshal(metadataBytes, metadata)
err = json.Unmarshal(metadataBytes, &metadata)
if err != nil {
log.Infof("Invalid metadata")
return err
Expand All @@ -126,13 +128,13 @@ func openCacheDB(path string, agentVersion string, apiKey string) (*bbolt.DB, er
if err != nil {
_ = db.Close()
log.Infof("Failed to validate remote configuration database %s", err)
return recreate(path, agentVersion, apiKeyHash)
return recreate(path, agentVersion, apiKeyHash, url)
}

if metadata.Version != agentVersion || metadata.APIKeyHash != apiKeyHash {
log.Infof("Different agent version or API Key detected")
if metadata.Version != agentVersion || metadata.APIKeyHash != apiKeyHash || metadata.URL != url {
log.Infof("Different agent version, API Key or URL detected")
_ = db.Close()
return recreate(path, agentVersion, apiKeyHash)
return recreate(path, agentVersion, apiKeyHash, url)
}

return db, nil
Expand Down
38 changes: 31 additions & 7 deletions pkg/config/remote/service/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

const apiKey = "37d58c60b8ac337293ce2ca6b28b19eb"
const rcURL = "dd-rc-url"

func TestAuthKeys(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -128,7 +129,7 @@ func TestRemoteConfigNewDB(t *testing.T) {
defer os.RemoveAll(dir)

// should add the version to newly created databases
db, err := openCacheDB(filepath.Join(dir, "remote-config.db"), "9.9.9", apiKey)
db, err := openCacheDB(filepath.Join(dir, "remote-config.db"), "9.9.9", apiKey, rcURL)
require.NoError(t, err)
defer db.Close()

Expand All @@ -144,14 +145,14 @@ func TestRemoteConfigChangedAPIKey(t *testing.T) {
defer os.RemoveAll(dir)

// should add the version to newly created databases
db0, err := openCacheDB(filepath.Join(dir, "remote-config.db"), "9.9.9", apiKey)
db0, err := openCacheDB(filepath.Join(dir, "remote-config.db"), "9.9.9", apiKey, rcURL)
require.NoError(t, err)
defer db0.Close()
metadata0, err := getMetadata(db0)
require.NoError(t, err)
db0.Close()

db1, err := openCacheDB(filepath.Join(dir, "remote-config.db"), "9.9.9", apiKey+"-new")
db1, err := openCacheDB(filepath.Join(dir, "remote-config.db"), "9.9.9", apiKey+"-new", rcURL)
require.NoError(t, err)
defer db1.Close()
metadata1, err := getMetadata(db1)
Expand All @@ -167,7 +168,7 @@ func TestRemoteConfigReopenNoVersionChange(t *testing.T) {
defer os.RemoveAll(dir)

// should add the version to newly created databases
db, err := openCacheDB(filepath.Join(dir, "remote-config.db"), agentVersion, apiKey)
db, err := openCacheDB(filepath.Join(dir, "remote-config.db"), agentVersion, apiKey, rcURL)
require.NoError(t, err)

metadata, err := getMetadata(db)
Expand All @@ -177,7 +178,7 @@ func TestRemoteConfigReopenNoVersionChange(t *testing.T) {
require.NoError(t, addData(db))
require.NoError(t, db.Close())

db, err = openCacheDB(filepath.Join(dir, "remote-config.db"), agentVersion, apiKey)
db, err = openCacheDB(filepath.Join(dir, "remote-config.db"), agentVersion, apiKey, rcURL)
require.NoError(t, err)
defer db.Close()
require.NoError(t, checkData(db))
Expand All @@ -191,7 +192,7 @@ func TestRemoteConfigOldDB(t *testing.T) {
dbPath := filepath.Join(dir, "remote-config.db")

// create database with current version
db, err := openCacheDB(dbPath, agentVersion, apiKey)
db, err := openCacheDB(dbPath, agentVersion, apiKey, rcURL)
require.NoError(t, err)

require.NoError(t, addData(db))
Expand All @@ -207,7 +208,7 @@ func TestRemoteConfigOldDB(t *testing.T) {
require.NoError(t, db.Close())

// reopen database
db, err = openCacheDB(dbPath, agentVersion, apiKey)
db, err = openCacheDB(dbPath, agentVersion, apiKey, rcURL)
require.NoError(t, err)

// check version after the database opens
Expand All @@ -217,3 +218,26 @@ func TestRemoteConfigOldDB(t *testing.T) {
assert.Equal(t, agentVersion, parsedMeta.Version)
assert.Error(t, checkData(db))
}

func TestRemoteConfigChangedURL(t *testing.T) {
dir, err := os.MkdirTemp("", "remote-config-test")
require.NoError(t, err)
defer os.RemoveAll(dir)

// should add the version to newly created databases
db0, err := openCacheDB(filepath.Join(dir, "remote-config.db"), "9.9.9", apiKey, rcURL)
require.NoError(t, err)
defer db0.Close()
metadata0, err := getMetadata(db0)
require.NoError(t, err)
db0.Close()

db1, err := openCacheDB(filepath.Join(dir, "remote-config.db"), "9.9.9", apiKey, rcURL+"-new")
require.NoError(t, err)
defer db1.Close()
metadata1, err := getMetadata(db1)
require.NoError(t, err)

require.NotEqual(t, metadata0.URL, metadata1.URL)
require.NotEqual(t, metadata0.CreationTime, metadata1.CreationTime)
}