Skip to content

Commit

Permalink
CodeReview: rename, add a method to update intervals, add info to the…
Browse files Browse the repository at this point in the history
… payload, rephrase comments
  • Loading branch information
chouetz committed Nov 22, 2023
1 parent e09ad81 commit 09cb9bc
Show file tree
Hide file tree
Showing 17 changed files with 334 additions and 292 deletions.
4 changes: 2 additions & 2 deletions comp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ Package inventoryagent implements a component to generate the 'datadog_agent' me

Package inventoryhost exposes the interface for the component to generate the 'host_metadata' metadata payload for inventory.

### [comp/metadata/inventorysigning](https://pkg.go.dev/github.com/DataDog/dd-agent-comp-experiments/comp/metadata/inventorysigning)
### [comp/metadata/packagesigning](https://pkg.go.dev/github.com/DataDog/dd-agent-comp-experiments/comp/metadata/packagesigning)

*Datadog Team*: agent-platform

Package inventorysigning implements a component to generate the 'signing' metadata payload for DD inventory (REDAPL).
Package packagesigning implements a component to generate the 'signing' metadata payload for DD inventory (REDAPL).

### [comp/metadata/resources](https://pkg.go.dev/github.com/DataDog/dd-agent-comp-experiments/comp/metadata/resources)

Expand Down
4 changes: 2 additions & 2 deletions comp/metadata/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/DataDog/datadog-agent/comp/metadata/host"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryagent"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryhost/inventoryhostimpl"
"github.com/DataDog/datadog-agent/comp/metadata/inventorysigning/inventorysigningimpl"
"github.com/DataDog/datadog-agent/comp/metadata/packagesigning/packagesigningimpl"
"github.com/DataDog/datadog-agent/comp/metadata/resources/resourcesimpl"
"github.com/DataDog/datadog-agent/comp/metadata/runner/runnerimpl"
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
Expand All @@ -26,7 +26,7 @@ var Bundle = fxutil.Bundle(
host.Module,
inventoryagent.Module,
inventoryhostimpl.Module,
inventorysigningimpl.Module,
packagesigningimpl.Module,
)

// MockBundle defines the mock fx options for this bundle.
Expand Down
16 changes: 9 additions & 7 deletions comp/metadata/internal/util/inventory_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,14 @@ type InventoryPayload struct {

// CreateInventoryPayload returns an initialized InventoryPayload. 'getPayload' will be called each time a new payload
// needs to be generated.
func CreateInventoryPayload(conf config.Component, l log.Component, s serializer.MetricSerializer, getPayload PayloadGetter, flareFileName string, customInterval time.Duration) InventoryPayload {
func CreateInventoryPayload(conf config.Component, l log.Component, s serializer.MetricSerializer, getPayload PayloadGetter, flareFileName string) InventoryPayload {
minInterval := time.Duration(conf.GetInt("inventories_min_interval")) * time.Second
if customInterval > 0 {
minInterval = customInterval
} else if minInterval <= 0 {
if minInterval <= 0 {
minInterval = defaultMinInterval
}

maxInterval := time.Duration(conf.GetInt("inventories_max_interval")) * time.Second
if customInterval > 0 {
maxInterval = customInterval
} else if maxInterval <= 0 {
if maxInterval <= 0 {
maxInterval = defaultMaxInterval
}

Expand Down Expand Up @@ -204,3 +200,9 @@ func (i *InventoryPayload) fillFlare(fb flaretypes.FlareBuilder) error {
fb.AddFileFromFunc(path, i.GetAsJSON)
return nil
}

// SetIntervals update the default intervals between two payloads.
func (i *InventoryPayload) SetIntervals(minInterval, maxInterval time.Duration) {
defaultMinInterval = minInterval
defaultMaxInterval = maxInterval
}
1 change: 0 additions & 1 deletion comp/metadata/internal/util/inventory_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func getTestInventoryPayload(t *testing.T, confOverrides map[string]any) *Invent
&serializer.MockSerializer{},
func() marshaler.JSONMarshaler { return &testPayload{} },
"test.json",
time.Duration(0),
)
return &i
}
Expand Down
2 changes: 1 addition & 1 deletion comp/metadata/inventoryagent/inventoryagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func newInventoryAgentProvider(deps dependencies) provides {
hostname: hname,
data: make(agentMetadata),
}
ia.InventoryPayload = util.CreateInventoryPayload(deps.Config, deps.Log, deps.Serializer, ia.getPayload, "agent.json", time.Duration(0))
ia.InventoryPayload = util.CreateInventoryPayload(deps.Config, deps.Log, deps.Serializer, ia.getPayload, "agent.json")

if ia.Enabled {
ia.initData()
Expand Down
11 changes: 4 additions & 7 deletions comp/metadata/inventoryhost/inventoryhostimpl/inventoryhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"context"
"encoding/json"
"fmt"
"runtime"
"time"

"github.com/DataDog/datadog-agent/comp/core/config"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/DataDog/datadog-agent/comp/metadata/host/utils"
"github.com/DataDog/datadog-agent/comp/metadata/internal/util"
"github.com/DataDog/datadog-agent/comp/metadata/inventoryhost"
"github.com/DataDog/datadog-agent/comp/metadata/packagesigning/packagesigningimpl"
"github.com/DataDog/datadog-agent/comp/metadata/runner/runnerimpl"
"github.com/DataDog/datadog-agent/pkg/gohai/cpu"
"github.com/DataDog/datadog-agent/pkg/gohai/memory"
Expand Down Expand Up @@ -92,7 +92,7 @@ type hostMetadata struct {
DmiBoardVendor string `json:"dmi_board_vendor"`

// from package repositories
GPGSigningEnabled bool `json:"gpg_signing_enabled"`
LinuxPackageSigningEnabled bool `json:"linux_package_signing_enabled"`
}

// Payload handles the JSON unmarshalling of the metadata payload
Expand Down Expand Up @@ -148,7 +148,7 @@ func newInventoryHostProvider(deps dependencies) provides {
hostname: hname,
data: &hostMetadata{},
}
ih.InventoryPayload = util.CreateInventoryPayload(deps.Config, deps.Log, deps.Serializer, ih.getPayload, "host.json", time.Duration(0))
ih.InventoryPayload = util.CreateInventoryPayload(deps.Config, deps.Log, deps.Serializer, ih.getPayload, "host.json")

return provides{
Comp: ih,
Expand Down Expand Up @@ -235,10 +235,7 @@ func (ih *invHost) fillData() {
ih.data.CloudProviderHostID = cloudproviders.GetHostID(context.Background(), cloudProvider)
ih.data.OsVersion = osVersionGet()

ih.data.GPGSigningEnabled = false
if runtime.GOOS == "linux" {
ih.data.GPGSigningEnabled = true // TODO implement gpgCheck parsing debian/redhat
}
ih.data.LinuxPackageSigningEnabled = packagesigningimpl.GetLinuxPackageSigningPolicy()
}

func (ih *invHost) getPayload() marshaler.JSONMarshaler {
Expand Down
74 changes: 37 additions & 37 deletions comp/metadata/inventoryhost/inventoryhostimpl/inventoryhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,36 @@ func TestGetPayload(t *testing.T) {
setupHostMetadataMock(t)

expectedMetadata := &hostMetadata{
CPUCores: 6,
CPULogicalProcessors: 6,
CPUVendor: "GenuineIntel",
CPUModel: "Intel_i7-8750H",
CPUModelID: "158",
CPUFamily: "6",
CPUStepping: "10",
CPUFrequency: 2208.006,
CPUCacheSize: 9437184,
KernelName: "Linux",
KernelRelease: "5.17.0-1-amd64",
KernelVersion: "Debian_5.17.3-1",
OS: "GNU/Linux",
CPUArchitecture: "unknown",
MemoryTotalKb: 1205632,
MemorySwapTotalKb: 1205632,
IPAddress: "192.168.24.138",
IPv6Address: "fe80::20c:29ff:feb6:d232",
MacAddress: "00:0c:29:b6:d2:32",
AgentVersion: version.AgentVersion,
CloudProvider: "some_cloud_provider",
CloudProviderAccountID: "some_host_id",
CloudProviderSource: "test_source",
CloudProviderHostID: "test_id_1234",
OsVersion: "testOS",
HypervisorGuestUUID: "hypervisorUUID",
DmiProductUUID: "dmiUUID",
DmiBoardAssetTag: "boardTag",
DmiBoardVendor: "boardVendor",
GPGSigningEnabled: true,
CPUCores: 6,
CPULogicalProcessors: 6,
CPUVendor: "GenuineIntel",
CPUModel: "Intel_i7-8750H",
CPUModelID: "158",
CPUFamily: "6",
CPUStepping: "10",
CPUFrequency: 2208.006,
CPUCacheSize: 9437184,
KernelName: "Linux",
KernelRelease: "5.17.0-1-amd64",
KernelVersion: "Debian_5.17.3-1",
OS: "GNU/Linux",
CPUArchitecture: "unknown",
MemoryTotalKb: 1205632,
MemorySwapTotalKb: 1205632,
IPAddress: "192.168.24.138",
IPv6Address: "fe80::20c:29ff:feb6:d232",
MacAddress: "00:0c:29:b6:d2:32",
AgentVersion: version.AgentVersion,
CloudProvider: "some_cloud_provider",
CloudProviderAccountID: "some_host_id",
CloudProviderSource: "test_source",
CloudProviderHostID: "test_id_1234",
OsVersion: "testOS",
HypervisorGuestUUID: "hypervisorUUID",
DmiProductUUID: "dmiUUID",
DmiBoardAssetTag: "boardTag",
DmiBoardVendor: "boardVendor",
LinuxPackageSigningEnabled: true,
}

ih := getTestInventoryHost(t)
Expand All @@ -172,13 +172,13 @@ func TestGetPayloadError(t *testing.T) {

p := ih.getPayload().(*Payload)
expected := &hostMetadata{
AgentVersion: version.AgentVersion,
CloudProvider: "some_cloud_provider",
CloudProviderAccountID: "some_host_id",
CloudProviderSource: "test_source",
CloudProviderHostID: "test_id_1234",
OsVersion: "testOS",
GPGSigningEnabled: true,
AgentVersion: version.AgentVersion,
CloudProvider: "some_cloud_provider",
CloudProviderAccountID: "some_host_id",
CloudProviderSource: "test_source",
CloudProviderHostID: "test_id_1234",
OsVersion: "testOS",
LinuxPackageSigningEnabled: true,
}
assert.Equal(t, expected, p.Metadata)
}
42 changes: 0 additions & 42 deletions comp/metadata/inventorysigning/README.md

This file was deleted.

This file was deleted.

44 changes: 44 additions & 0 deletions comp/metadata/packagesigning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Package Signing Payload

This component populates the Linux Packages signing information in the `inventories` product in DataDog. They fill the `host_signing` table.

This is enabled by default but can be turned off using `inventories_enabled` config.

The payload is sent every 12h (see `inventories_max_interval` in the config). It has a different collection timeframe than the other inventory payloads.

# Format

The payload is a JSON dict with a list of keys, each having the following fields

- `hostname` - **string**: the hostname of the agent as shown on the status page.
- `timestamp` - **int**: the timestamp when the payload was created.
- `agent_version` - **string**: the version of the Agent.
- `signing_keys` - **list of dict of string to JSON type**
- `fingerprint` - **string**: the 8-char long key fingerprint.
- `expiration_date` - **string**: the expiration date of the key.
- `key_type` - **string**: the type of key. which represents how it is referenced in the host. Possible values are "signed-by", "trusted" or "debsig" for debianoids, "repo" or "rpm" for redhat-like (including SUSE)


## Example Payload

Here an example of an signing inventory payload:

```
{
"hostname": "totoro",
"timestamp": 1631281754507358895,
"agent_version: "7.50.0",
"signing_keys": [
{
"fingerprint": "12345ABC",
"expiration_date": "2023-02-12",
"key_type": "trusted",
},
{
"fingerprint": "DEF90874",
"expiration_date": "9999-12-24",
"key_type": "debsig",
}
]
}
```
Loading

0 comments on commit 09cb9bc

Please sign in to comment.