Skip to content

Commit

Permalink
(rcm-40) improve key format (#10278)
Browse files Browse the repository at this point in the history
* (rcm-40) improve key format

* remove proto key

* add msgpkey

* migrate to msgp

* fix linting issues

* python lint
  • Loading branch information
arbll authored Jan 7, 2022
1 parent 479de41 commit abff3fa
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 28 deletions.
8 changes: 3 additions & 5 deletions pkg/config/remote/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type Service struct {
firstUpdate bool

refreshInterval time.Duration
remoteConfigKey remoteConfigKey

ctx context.Context
clock clock.Clock
Expand Down Expand Up @@ -86,15 +85,15 @@ func NewService() (*Service, error) {
return nil, err
}
backendURL := config.Datadog.GetString("remote_configuration.endpoint")
http := api.NewHTTPClient(backendURL, apiKey, remoteConfigKey.appKey)
http := api.NewHTTPClient(backendURL, apiKey, remoteConfigKey.AppKey)

dbPath := path.Join(config.Datadog.GetString("run_path"), "remote-config.db")
db, err := openCacheDB(dbPath)
if err != nil {
return nil, err
}
cacheKey := fmt.Sprintf("%s/%d/", remoteConfigKey.datacenter, remoteConfigKey.orgID)
uptaneClient, err := uptane.NewClient(db, cacheKey, remoteConfigKey.orgID)
cacheKey := fmt.Sprintf("%s/%d/", remoteConfigKey.Datacenter, remoteConfigKey.OrgID)
uptaneClient, err := uptane.NewClient(db, cacheKey, remoteConfigKey.OrgID)
if err != nil {
return nil, err
}
Expand All @@ -109,7 +108,6 @@ func NewService() (*Service, error) {
ctx: context.Background(),
firstUpdate: true,
refreshInterval: refreshInterval,
remoteConfigKey: remoteConfigKey,
products: make(map[rdata.Product]struct{}),
newProducts: make(map[rdata.Product]struct{}),
hostname: hostname,
Expand Down
13 changes: 10 additions & 3 deletions pkg/config/remote/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package service

import (
"context"
"encoding/base32"
"os"
"testing"

"github.com/DataDog/datadog-agent/pkg/config"
rdata "github.com/DataDog/datadog-agent/pkg/config/remote/data"
"github.com/DataDog/datadog-agent/pkg/config/remote/uptane"
"github.com/DataDog/datadog-agent/pkg/proto/msgpgo"
"github.com/DataDog/datadog-agent/pkg/proto/pbgo"
"github.com/DataDog/datadog-agent/pkg/version"
"github.com/benbjohnson/clock"
Expand Down Expand Up @@ -59,15 +61,20 @@ func (m *mockUptane) TargetsMeta() ([]byte, error) {
return args.Get(0).([]byte), args.Error(1)
}

const (
testRCKey = "dd.com/2/fake_key"
var (
testRCKey = msgpgo.RemoteConfigKey{
AppKey: "fake_key",
OrgID: 2,
Datacenter: "dd.com",
}
)

func newTestService(t *testing.T, api *mockAPI, uptane *mockUptane, clock clock.Clock) *Service {
dir, err := os.MkdirTemp("", "testdbdir")
assert.NoError(t, err)
config.Datadog.Set("run_path", dir)
config.Datadog.Set("remote_configuration.key", testRCKey)
serializedKey, _ := testRCKey.MarshalMsg(nil)
config.Datadog.Set("remote_configuration.key", base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(serializedKey))
service, err := NewService()
assert.NoError(t, err)
service.api = api
Expand Down
33 changes: 14 additions & 19 deletions pkg/config/remote/service/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
package service

import (
"encoding/base32"
"fmt"
"os"
"strconv"
"strings"

"github.com/DataDog/datadog-agent/pkg/config/remote/data"
"github.com/DataDog/datadog-agent/pkg/config/remote/uptane"
"github.com/DataDog/datadog-agent/pkg/proto/msgpgo"
"github.com/DataDog/datadog-agent/pkg/proto/pbgo"
"github.com/DataDog/datadog-agent/pkg/version"
"go.etcd.io/bbolt"
Expand All @@ -31,26 +31,21 @@ func openCacheDB(path string) (*bbolt.DB, error) {
return db, nil
}

type remoteConfigKey struct {
orgID int64
appKey string
datacenter string
}

func parseRemoteConfigKey(rawKey string) (remoteConfigKey, error) {
split := strings.SplitN(rawKey, "/", 3)
if len(split) < 3 {
return remoteConfigKey{}, fmt.Errorf("invalid remote configuration key format, should be datacenter/org_id/app_key")
func parseRemoteConfigKey(serializedKey string) (*msgpgo.RemoteConfigKey, error) {
encoding := base32.StdEncoding.WithPadding(base32.NoPadding)
rawKey, err := encoding.DecodeString(serializedKey)
if err != nil {
return nil, err
}
orgID, err := strconv.ParseInt(split[1], 10, 64)
var key msgpgo.RemoteConfigKey
_, err = key.UnmarshalMsg(rawKey)
if err != nil {
return remoteConfigKey{}, err
return nil, err
}
if key.AppKey == "" || key.Datacenter == "" || key.OrgID == 0 {
return nil, fmt.Errorf("invalid remote config key")
}
return remoteConfigKey{
orgID: orgID,
appKey: split[2],
datacenter: split[0],
}, nil
return &key, nil
}

func buildLatestConfigsRequest(hostname string, state uptane.State, activeClients []*pbgo.Client, products map[data.Product]struct{}, newProducts map[data.Product]struct{}) *pbgo.LatestConfigsRequest {
Expand Down
46 changes: 46 additions & 0 deletions pkg/config/remote/service/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package service

import (
"encoding/base32"
"testing"

"github.com/DataDog/datadog-agent/pkg/proto/msgpgo"
"github.com/stretchr/testify/assert"
)

func TestRemoteConfigKey(t *testing.T) {
tests := []struct {
input string
err bool
output *msgpgo.RemoteConfigKey
}{
{input: generateKey(t, 2, "datadoghq.com", "58d58c60b8ac337293ce2ca6b28b19eb"), output: &msgpgo.RemoteConfigKey{AppKey: "58d58c60b8ac337293ce2ca6b28b19eb", OrgID: 2, Datacenter: "datadoghq.com"}},
{input: generateKey(t, 2, "datadoghq.com", ""), err: true},
{input: generateKey(t, 2, "", "app_Key"), err: true},
{input: generateKey(t, 0, "datadoghq.com", "app_Key"), err: true},
}
for _, test := range tests {
t.Run(test.input, func(tt *testing.T) {
output, err := parseRemoteConfigKey(test.input)
if test.err {
assert.Error(tt, err)
} else {
assert.Equal(tt, test.output, output)
assert.NoError(tt, err)
}
})
}
}

func generateKey(t *testing.T, orgID int64, datacenter string, appKey string) string {
key := msgpgo.RemoteConfigKey{
AppKey: appKey,
OrgID: orgID,
Datacenter: datacenter,
}
rawKey, err := key.MarshalMsg(nil)
if err != nil {
t.Fatal(err)
}
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rawKey)
}
8 changes: 8 additions & 0 deletions pkg/proto/msgpgo/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package msgpgo

// RemoteConfigKey is a RemoteConfigKey
type RemoteConfigKey struct {
AppKey string `msgpack:"key"`
OrgID int64 `msgpack:"org"`
Datacenter string `msgpack:"dc"`
}
160 changes: 160 additions & 0 deletions pkg/proto/msgpgo/key_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit abff3fa

Please sign in to comment.