Skip to content

Commit

Permalink
[RCM-632] Add UUID in request (#15088)
Browse files Browse the repository at this point in the history
* Add org uuid field

* Add org uuid in request

* Remove generate file

* Comment exported method
  • Loading branch information
coignetp authored Jan 17, 2023
1 parent 552a7d7 commit 24caf73
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 183 deletions.
8 changes: 7 additions & 1 deletion pkg/config/remote/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type uptaneClient interface {
Update(response *pbgo.LatestConfigsResponse) error
State() (uptane.State, error)
DirectorRoot(version uint64) ([]byte, error)
StoredOrgUUID() (string, error)
Targets() (data.TargetFiles, error)
TargetFile(path string) ([]byte, error)
TargetsMeta() ([]byte, error)
Expand Down Expand Up @@ -267,7 +268,12 @@ func (s *Service) refresh() error {
if err != nil {
log.Warnf("could not get previous backend client state: %v", err)
}
request := buildLatestConfigsRequest(s.hostname, s.traceAgentEnv, previousState, activeClients, s.products, s.newProducts, s.lastUpdateErr, clientState)
orgUUID, err := s.uptane.StoredOrgUUID()
if err != nil {
return err
}

request := buildLatestConfigsRequest(s.hostname, s.traceAgentEnv, orgUUID, previousState, activeClients, s.products, s.newProducts, s.lastUpdateErr, clientState)
s.Unlock()
ctx := context.Background()
response, err := s.api.Fetch(ctx, request)
Expand Down
29 changes: 29 additions & 0 deletions pkg/config/remote/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (m *mockUptane) DirectorRoot(version uint64) ([]byte, error) {
return args.Get(0).([]byte), args.Error(1)
}

func (m *mockUptane) StoredOrgUUID() (string, error) {
args := m.Called()
return args.Get(0).(string), args.Error(1)
}

func (m *mockUptane) Targets() (data.TargetFiles, error) {
args := m.Called()
return args.Get(0).(data.TargetFiles), args.Error(1)
Expand Down Expand Up @@ -133,7 +138,9 @@ func TestServiceBackoffFailure(t *testing.T) {
CurrentDirectorRootVersion: 0,
Products: []string{},
NewProducts: []string{},
OrgUuid: "abcdef",
}).Return(lastConfigResponse, errors.New("simulated HTTP error"))
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return([]byte{}, nil)
Expand All @@ -158,7 +165,9 @@ func TestServiceBackoffFailure(t *testing.T) {
NewProducts: []string{},
HasError: true,
Error: httpError,
OrgUuid: "abcdef",
}).Return(lastConfigResponse, errors.New("simulated HTTP error"))
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return([]byte{}, nil)
Expand Down Expand Up @@ -208,7 +217,9 @@ func TestServiceBackoffFailureRecovery(t *testing.T) {
CurrentDirectorRootVersion: 0,
Products: []string{},
NewProducts: []string{},
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return([]byte{}, nil)
Expand Down Expand Up @@ -334,7 +345,9 @@ func TestService(t *testing.T) {
CurrentDirectorRootVersion: 0,
Products: []string{},
NewProducts: []string{},
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return([]byte{}, nil)
Expand Down Expand Up @@ -368,6 +381,7 @@ func TestService(t *testing.T) {
}
fileAPM1 := []byte(`testapm1`)
fileAPM2 := []byte(`testapm2`)
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TargetsMeta").Return(targets, nil)
uptaneClient.On("TargetsCustom").Return(testTargetsCustom, nil)

Expand Down Expand Up @@ -417,6 +431,7 @@ func TestService(t *testing.T) {
BackendClientState: []byte(`test_state`),
HasError: false,
Error: "",
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)

service.clients.seen(client) // Avoid blocking on channel sending when nothing is at the other end
Expand Down Expand Up @@ -476,6 +491,7 @@ func TestServiceClientPredicates(t *testing.T) {
AppVersion: "1",
},
}
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TargetsMeta").Return([]byte(`{"signed": "testtargets"}`), nil)
uptaneClient.On("TargetsCustom").Return([]byte(`{"opaque_backend_state":"dGVzdF9zdGF0ZQ=="}`), nil)

Expand Down Expand Up @@ -523,6 +539,7 @@ func TestServiceClientPredicates(t *testing.T) {
BackendClientState: []byte(`test_state`),
HasError: false,
Error: "",
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)

service.clients.seen(client) // Avoid blocking on channel sending when nothing is at the other end
Expand Down Expand Up @@ -561,10 +578,12 @@ func TestServiceGetRefreshIntervalNone(t *testing.T) {
Products: []string{},
NewProducts: []string{},
BackendClientState: []byte("test_state"),
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)

// No explicit refresh interval is provided by the backend
testTargetsCustomNoOverride := []byte(`{"opaque_backend_state":"dGVzdF9zdGF0ZQ=="}`)
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return(testTargetsCustomNoOverride, nil)
Expand Down Expand Up @@ -596,10 +615,12 @@ func TestServiceGetRefreshIntervalValid(t *testing.T) {
Products: []string{},
NewProducts: []string{},
BackendClientState: []byte("test_state"),
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)

// An acceptable refresh interval is provided by the backend
testTargetsCustomOk := []byte(`{"opaque_backend_state":"dGVzdF9zdGF0ZQ==", "agent_refresh_interval": 42}`)
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return(testTargetsCustomOk, nil)
Expand Down Expand Up @@ -631,10 +652,12 @@ func TestServiceGetRefreshIntervalTooSmall(t *testing.T) {
Products: []string{},
NewProducts: []string{},
BackendClientState: []byte("test_state"),
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)

// A too small refresh interval is provided by the backend (the refresh interval should not change)
testTargetsCustomOverrideOutOfRangeSmall := []byte(`{"opaque_backend_state":"dGVzdF9zdGF0ZQ==", "agent_refresh_interval": -1}`)
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return(testTargetsCustomOverrideOutOfRangeSmall, nil)
Expand Down Expand Up @@ -666,10 +689,12 @@ func TestServiceGetRefreshIntervalTooBig(t *testing.T) {
Products: []string{},
NewProducts: []string{},
BackendClientState: []byte("test_state"),
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)

// A too large refresh interval is provided by the backend (the refresh interval should not change)
testTargetsCustomOverrideOutOfRangeBig := []byte(`{"opaque_backend_state":"dGVzdF9zdGF0ZQ==", "agent_refresh_interval": 500}`)
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return(testTargetsCustomOverrideOutOfRangeBig, nil)
Expand Down Expand Up @@ -704,10 +729,12 @@ func TestServiceGetRefreshIntervalNoOverrideAllowed(t *testing.T) {
Products: []string{},
NewProducts: []string{},
BackendClientState: []byte("test_state"),
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)

// An interval is provided, but it should not be applied
testTargetsCustomOk := []byte(`{"opaque_backend_state":"dGVzdF9zdGF0ZQ==", "agent_refresh_interval": 42}`)
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TUFVersionState").Return(uptane.TUFVersions{}, nil)
uptaneClient.On("Update", lastConfigResponse).Return(nil)
uptaneClient.On("TargetsCustom").Return(testTargetsCustomOk, nil)
Expand Down Expand Up @@ -752,6 +779,7 @@ func TestConfigExpiration(t *testing.T) {
AppVersion: "1",
},
}
uptaneClient.On("StoredOrgUUID").Return("abcdef", nil)
uptaneClient.On("TargetsMeta").Return([]byte(`{"signed": "testtargets"}`), nil)
uptaneClient.On("TargetsCustom").Return([]byte(`{"opaque_backend_state":"dGVzdF9zdGF0ZQ=="}`), nil)
uptaneClient.On("Targets").Return(data.TargetFiles{
Expand Down Expand Up @@ -783,6 +811,7 @@ func TestConfigExpiration(t *testing.T) {
BackendClientState: []byte(`test_state`),
HasError: false,
Error: "",
OrgUuid: "abcdef",
}).Return(lastConfigResponse, nil)

service.clients.seen(client) // Avoid blocking on channel sending when nothing is at the other end
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/remote/service/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func getRemoteConfigAuthKeys(apiKey string, rcKey string) (remoteConfigAuthKeys,
}, nil
}

func buildLatestConfigsRequest(hostname string, traceAgentEnv string, state uptane.TUFVersions, activeClients []*pbgo.Client, products map[data.Product]struct{}, newProducts map[data.Product]struct{}, lastUpdateErr error, clientState []byte) *pbgo.LatestConfigsRequest {
func buildLatestConfigsRequest(hostname string, traceAgentEnv string, orgUUID string, state uptane.TUFVersions, activeClients []*pbgo.Client, products map[data.Product]struct{}, newProducts map[data.Product]struct{}, lastUpdateErr error, clientState []byte) *pbgo.LatestConfigsRequest {
productsList := make([]data.Product, len(products))
i := 0
for k := range products {
Expand Down Expand Up @@ -185,6 +185,7 @@ func buildLatestConfigsRequest(hostname string, traceAgentEnv string, state upta
HasError: lastUpdateErr != nil,
Error: lastUpdateErrString,
TraceAgentEnv: traceAgentEnv,
OrgUuid: orgUUID,
}
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/config/remote/uptane/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ func (c *Client) verify() error {
return nil
}

func (c *Client) storedOrgUUID() (string, error) {
// StoredOrgUUID returns the org UUID given by the backend
func (c *Client) StoredOrgUUID() (string, error) {
// This is an important block of code : to avoid being locked out
// of the agent in case of a wrong uuid being stored, we link an
// org UUID storage to a root version. What this means in practice
Expand Down Expand Up @@ -289,7 +290,7 @@ func (c *Client) verifyOrg() error {
// we can remove the orgUUID from the snapshot and they'll work
// again. This being said, this is last resort.
if custom.OrgUUID != nil {
orgUUID, err := c.storedOrgUUID()
orgUUID, err := c.StoredOrgUUID()
if err != nil {
return fmt.Errorf("could not obtain stored/remote orgUUID: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/proto/datadog/remoteconfig/remoteconfig.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ message LatestConfigsRequest {
bool has_error = 11;
string error = 12;
string trace_agent_env = 13;
string org_uuid = 14;
}

message LatestConfigsResponse {
Expand Down
Loading

0 comments on commit 24caf73

Please sign in to comment.