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

[pull] main from DataDog:main #45

Merged
merged 11 commits into from
Nov 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
/cmd/system-probe/windows_resources/ @DataDog/windows-kernel-integrations
/cmd/system-probe/main_windows*.go @DataDog/windows-kernel-integrations
/cmd/system-probe/api/client/client_windows.go @DataDog/windows-kernel-integrations
/cmd/system-probe/api/server/listener_windows.go @DataDog/windows-kernel-integrations
/cmd/systray/ @DataDog/windows-agent
/cmd/security-agent/ @DataDog/agent-security
/cmd/installer/ @DataDog/fleet @DataDog/windows-agent
Expand Down Expand Up @@ -487,7 +488,6 @@
/pkg/process/monitor/ @DataDog/universal-service-monitoring
/pkg/process/net/ @DataDog/universal-service-monitoring @DataDog/Networks
/pkg/process/net/common_windows.go @DataDog/windows-agent
/pkg/process/net/windows_pipe.go @DataDog/windows-kernel-integrations
/pkg/proto/datadog/remoteconfig/ @DataDog/remote-config
/pkg/proto/pbgo/ # do not notify anyone
/pkg/proto/pbgo/trace @DataDog/agent-apm
Expand Down
44 changes: 16 additions & 28 deletions .gitlab/package_build/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,23 @@
.agent_build_x86:
image: registry.ddbuild.io/ci/datadog-agent-buildimages/linux-glibc-2-17-x64$DATADOG_AGENT_BUILDIMAGES_SUFFIX:$DATADOG_AGENT_BUILDIMAGES
tags: ["arch:amd64"]
needs:
[
"go_mod_tidy_check",
"build_system-probe-x64",
"go_deps",
"generate_minimized_btfs_x64",
]
needs: ["build_system-probe-x64", "go_deps", "generate_minimized_btfs_x64"]
variables:
PACKAGE_ARCH: amd64
DD_CC: 'x86_64-unknown-linux-gnu-gcc'
DD_CXX: 'x86_64-unknown-linux-gnu-g++'
DD_CMAKE_TOOLCHAIN: '/opt/cmake/x86_64-unknown-linux-gnu.toolchain.cmake'
DD_CC: "x86_64-unknown-linux-gnu-gcc"
DD_CXX: "x86_64-unknown-linux-gnu-g++"
DD_CMAKE_TOOLCHAIN: "/opt/cmake/x86_64-unknown-linux-gnu.toolchain.cmake"

.agent_build_arm64:
image: registry.ddbuild.io/ci/datadog-agent-buildimages/linux-glibc-2-23-arm64$DATADOG_AGENT_BUILDIMAGES_SUFFIX:$DATADOG_AGENT_BUILDIMAGES
tags: ["arch:arm64"]
needs:
[
"go_mod_tidy_check",
"build_system-probe-arm64",
"go_deps",
"generate_minimized_btfs_arm64",
]
["build_system-probe-arm64", "go_deps", "generate_minimized_btfs_arm64"]
variables:
PACKAGE_ARCH: arm64
DD_CC: 'aarch64-unknown-linux-gnu-gcc'
DD_CXX: 'aarch64-unknown-linux-gnu-g++'
DD_CMAKE_TOOLCHAIN: '/opt/cmake/aarch64-unknown-linux-gnu.toolchain.cmake'
DD_CC: "aarch64-unknown-linux-gnu-gcc"
DD_CXX: "aarch64-unknown-linux-gnu-g++"
DD_CMAKE_TOOLCHAIN: "/opt/cmake/aarch64-unknown-linux-gnu.toolchain.cmake"

.agent_7_build:
variables:
Expand Down Expand Up @@ -116,16 +105,16 @@ iot-agent-x64:
tags: ["arch:amd64"]
image: registry.ddbuild.io/ci/datadog-agent-buildimages/linux-glibc-2-17-x64$DATADOG_AGENT_BUILDIMAGES_SUFFIX:$DATADOG_AGENT_BUILDIMAGES
variables:
DD_CC: 'x86_64-unknown-linux-gnu-gcc'
DD_CXX: 'x86_64-unknown-linux-gnu-g++'
DD_CC: "x86_64-unknown-linux-gnu-gcc"
DD_CXX: "x86_64-unknown-linux-gnu-g++"

iot-agent-arm64:
extends: .iot-agent-common
tags: ["arch:arm64"]
image: registry.ddbuild.io/ci/datadog-agent-buildimages/linux-glibc-2-23-arm64$DATADOG_AGENT_BUILDIMAGES_SUFFIX:$DATADOG_AGENT_BUILDIMAGES
variables:
DD_CC: 'aarch64-unknown-linux-gnu-gcc'
DD_CXX: 'aarch64-unknown-linux-gnu-g++'
DD_CC: "aarch64-unknown-linux-gnu-gcc"
DD_CXX: "aarch64-unknown-linux-gnu-g++"

iot-agent-armhf:
extends: .iot-agent-common
Expand Down Expand Up @@ -169,15 +158,14 @@ dogstatsd-x64:
image: registry.ddbuild.io/ci/datadog-agent-buildimages/linux-glibc-2-17-x64$DATADOG_AGENT_BUILDIMAGES_SUFFIX:$DATADOG_AGENT_BUILDIMAGES
tags: ["arch:amd64"]
variables:
DD_CC: 'x86_64-unknown-linux-gnu-gcc'
DD_CXX: 'x86_64-unknown-linux-gnu-g++'
DD_CC: "x86_64-unknown-linux-gnu-gcc"
DD_CXX: "x86_64-unknown-linux-gnu-g++"

dogstatsd-arm64:
extends: .dogstatsd_build_common
image: registry.ddbuild.io/ci/datadog-agent-buildimages/linux-glibc-2-23-arm64$DATADOG_AGENT_BUILDIMAGES_SUFFIX:$DATADOG_AGENT_BUILDIMAGES
tags: ["arch:arm64"]
needs: ["go_mod_tidy_check", "build_dogstatsd-binary_arm64", "go_deps"]
variables:
DD_CC: 'aarch64-unknown-linux-gnu-gcc'
DD_CXX: 'aarch64-unknown-linux-gnu-g++'

DD_CC: "aarch64-unknown-linux-gnu-gcc"
DD_CXX: "aarch64-unknown-linux-gnu-g++"
6 changes: 3 additions & 3 deletions cmd/agent/subcommands/flare/command_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

"github.com/stretchr/testify/require"

sysprobeserver "github.com/DataDog/datadog-agent/cmd/system-probe/api/server"
"github.com/DataDog/datadog-agent/pkg/config/model"
processNet "github.com/DataDog/datadog-agent/pkg/process/net"
)

const (
Expand All @@ -32,12 +32,12 @@ func sysprobeSocketPath(_ *testing.T) string {
func NewSystemProbeTestServer(handler http.Handler) (*httptest.Server, error) {
server := httptest.NewUnstartedServer(handler)

conn, err := processNet.NewSystemProbeListener(systemProbeTestPipeName)
conn, err := sysprobeserver.NewListener(systemProbeTestPipeName)
if err != nil {
return nil, err
}

server.Listener = conn.GetListener()
server.Listener = conn
return server, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/cluster-agent/subcommands/start/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.

//go:build !windows && kubeapiserver
//go:build !darwin && !windows && kubeapiserver

package start

Expand Down
50 changes: 50 additions & 0 deletions cmd/system-probe/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
package client

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/pkg/util/funcs"
)

Expand All @@ -35,3 +41,47 @@ func get(socketPath string) *http.Client {
},
}
}

// GetCheck returns data unmarshalled from JSON to T, from the specified module at the /<module>/check endpoint.
func GetCheck[T any](client *http.Client, module types.ModuleName) (T, error) {
var data T
req, err := http.NewRequest("GET", ModuleURL(module, "/check"), nil)
if err != nil {
return data, err
}

resp, err := client.Do(req)
if err != nil {
return data, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return data, err
}
if resp.StatusCode != http.StatusOK {
return data, fmt.Errorf("non-ok status code: url %s, status_code: %d, response: `%s`", req.URL, resp.StatusCode, string(body))
}

err = json.Unmarshal(body, &data)
return data, err
}

func constructURL(module string, endpoint string) string {
u, _ := url.Parse("http://sysprobe")
if module != "" {
u = u.JoinPath(module)
}
path, query, found := strings.Cut(endpoint, "?")
u = u.JoinPath(path)
if found {
u.RawQuery = query
}
return u.String()
}

// ModuleURL constructs a system-probe ModuleURL given the specified module and endpoint.
func ModuleURL(module types.ModuleName, endpoint string) string {
return constructURL(string(module), endpoint)
}
53 changes: 53 additions & 0 deletions cmd/system-probe/api/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

package client

import (
"context"
"net"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConstructURL(t *testing.T) {
u := constructURL("", "/asdf?a=b")
assert.Equal(t, "http://sysprobe/asdf?a=b", u)

u = constructURL("zzzz", "/asdf?a=b")
assert.Equal(t, "http://sysprobe/zzzz/asdf?a=b", u)

u = constructURL("zzzz", "asdf")
assert.Equal(t, "http://sysprobe/zzzz/asdf", u)
}

func TestGetCheck(t *testing.T) {
type testData struct {
Str string
Num int
}

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/test/check" {
_, _ = w.Write([]byte(`{"Str": "asdf", "Num": 42}`))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(server.Close)

client := &http.Client{Transport: &http.Transport{DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("tcp", server.Listener.Addr().String())
}}}

resp, err := GetCheck[testData](client, "test")
require.NoError(t, err)
assert.Equal(t, "asdf", resp.Str)
assert.Equal(t, 42, resp.Num)
}
6 changes: 3 additions & 3 deletions cmd/system-probe/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ import (
gorilla "github.com/gorilla/mux"

"github.com/DataDog/datadog-agent/cmd/system-probe/api/module"
"github.com/DataDog/datadog-agent/cmd/system-probe/api/server"
sysconfigtypes "github.com/DataDog/datadog-agent/cmd/system-probe/config/types"
"github.com/DataDog/datadog-agent/cmd/system-probe/modules"
"github.com/DataDog/datadog-agent/cmd/system-probe/utils"
"github.com/DataDog/datadog-agent/comp/core/settings"
"github.com/DataDog/datadog-agent/comp/core/telemetry"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/ebpf"
"github.com/DataDog/datadog-agent/pkg/process/net"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// StartServer starts the HTTP and gRPC servers for the system-probe, which registers endpoints from all enabled modules.
func StartServer(cfg *sysconfigtypes.Config, telemetry telemetry.Component, wmeta workloadmeta.Component, settings settings.Component) error {
conn, err := net.NewSystemProbeListener(cfg.SocketAddress)
conn, err := server.NewListener(cfg.SocketAddress)
if err != nil {
return err
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func StartServer(cfg *sysconfigtypes.Config, telemetry telemetry.Component, wmet
}

go func() {
err = http.Serve(conn.GetListener(), mux)
err = http.Serve(conn, mux)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Errorf("error creating HTTP server: %s", err)
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/system-probe/api/server/listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

// Package server contains system-probe API server functionality
package server

import "errors"

var (
// ErrNotImplemented is an error used when system-probe is attempted to be accessed on an unsupported OS
ErrNotImplemented = errors.New("system-probe unsupported")
)
15 changes: 15 additions & 0 deletions cmd/system-probe/api/server/listener_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

//go:build !unix && !windows

package server

import "net"

// NewListener is not supported
func NewListener(_ string) (net.Listener, error) {
return nil, ErrNotImplemented
}
59 changes: 59 additions & 0 deletions cmd/system-probe/api/server/listener_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024-present Datadog, Inc.

//go:build unix

package server

import (
"errors"
"fmt"
"net"
"os"

"github.com/DataDog/datadog-agent/pkg/util/filesystem"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

// NewListener creates a Unix Domain Socket Listener
func NewListener(socketAddr string) (net.Listener, error) {
if len(socketAddr) == 0 {
return nil, errors.New("uds: empty socket path provided")
}

// Check to see if there's a pre-existing system probe socket.
fileInfo, err := os.Stat(socketAddr)
if err == nil { // No error means the socket file already exists
// If it's not a UNIX socket, then this is a problem.
if fileInfo.Mode()&os.ModeSocket == 0 {
return nil, fmt.Errorf("uds: reuse %s socket path: path already exists and it is not a UNIX socket", socketAddr)
}
// Attempt to remove the pre-existing socket
if err = os.Remove(socketAddr); err != nil {
return nil, fmt.Errorf("uds: remove stale UNIX socket: %v", err)
}
}

conn, err := net.Listen("unix", socketAddr)
if err != nil {
return nil, fmt.Errorf("listen: %s", err)
}

if err := os.Chmod(socketAddr, 0720); err != nil {
return nil, fmt.Errorf("socket chmod write-only: %s", err)
}

perms, err := filesystem.NewPermission()
if err != nil {
return nil, err
}

if err := perms.RestrictAccessToUser(socketAddr); err != nil {
return nil, err
}

log.Debugf("uds: %s successfully initialized", conn.Addr())
return conn, nil
}
Loading
Loading