Skip to content

Commit b821bc9

Browse files
authored
usm: move is tls static tags (#38108)
1 parent 5224e9d commit b821bc9

18 files changed

+225
-208
lines changed

pkg/network/encoding/encoding_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func getExpectedConnections(encodedWithQueryType bool, httpOutBlob []byte) *mode
144144
NpmEnabled: false,
145145
UsmEnabled: false,
146146
},
147-
Tags: network.GetStaticTags(tagOpenSSL | tagTLS),
147+
Tags: tls.GetStaticTags(tagOpenSSL | tagTLS),
148148
}
149149
// fixup Protocol stack as on windows or macos
150150
// we don't have tags mechanism inserting TLS protocol on protocol stack

pkg/network/encoding/marshal/format.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/twmb/murmur3"
1414

1515
"github.com/DataDog/datadog-agent/pkg/network"
16+
"github.com/DataDog/datadog-agent/pkg/network/protocols/tls"
1617
"github.com/DataDog/datadog-agent/pkg/process/util"
1718
)
1819

@@ -268,7 +269,7 @@ func formatRouteIdx(v *network.Via, routes map[network.Via]RouteIdx) int32 {
268269
func formatTags(c network.ConnectionStats, tagsSet *network.TagsSet, connDynamicTags map[string]struct{}) ([]uint32, uint32) {
269270
var checksum uint32
270271

271-
staticTags := network.GetStaticTags(c.StaticTags)
272+
staticTags := tls.GetStaticTags(c.StaticTags)
272273
tagsIdx := make([]uint32, 0, len(staticTags)+len(connDynamicTags)+len(c.Tags))
273274

274275
for _, tag := range staticTags {

pkg/network/encoding/marshal/format_usm_linux_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
model "github.com/DataDog/agent-payload/v5/process"
1616

1717
"github.com/DataDog/datadog-agent/pkg/network/protocols"
18-
"github.com/DataDog/datadog-agent/pkg/network/protocols/http"
18+
"github.com/DataDog/datadog-agent/pkg/network/protocols/tls"
1919
)
2020

2121
func TestFormatTLSProtocols(t *testing.T) {
@@ -28,7 +28,7 @@ func TestFormatTLSProtocols(t *testing.T) {
2828
{
2929
name: "GnuTLS - unknown protocol",
3030
protocol: protocols.Stack{Application: protocols.Unknown},
31-
staticTags: http.TLS | http.GnuTLS,
31+
staticTags: tls.TLS | tls.GnuTLS,
3232
want: &model.ProtocolStack{
3333
Stack: []model.ProtocolType{
3434
model.ProtocolType_protocolTLS,
@@ -38,7 +38,7 @@ func TestFormatTLSProtocols(t *testing.T) {
3838
{
3939
name: "OpenSSL - HTTP protocol",
4040
protocol: protocols.Stack{Application: protocols.HTTP},
41-
staticTags: http.TLS | http.OpenSSL,
41+
staticTags: tls.TLS | tls.OpenSSL,
4242
want: &model.ProtocolStack{
4343
Stack: []model.ProtocolType{
4444
model.ProtocolType_protocolTLS,
@@ -49,7 +49,7 @@ func TestFormatTLSProtocols(t *testing.T) {
4949
{
5050
name: "GoTLS - MySQL protocol",
5151
protocol: protocols.Stack{Application: protocols.MySQL},
52-
staticTags: http.TLS | http.Go,
52+
staticTags: tls.TLS | tls.Go,
5353
want: &model.ProtocolStack{
5454
Stack: []model.ProtocolType{
5555
model.ProtocolType_protocolTLS,

pkg/network/encoding/marshal/usm_protocols.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ package marshal
88
import (
99
model "github.com/DataDog/agent-payload/v5/process"
1010

11-
"github.com/DataDog/datadog-agent/pkg/network"
1211
"github.com/DataDog/datadog-agent/pkg/network/protocols"
12+
"github.com/DataDog/datadog-agent/pkg/network/protocols/tls"
1313
"github.com/DataDog/datadog-agent/pkg/util/log"
1414
)
1515

@@ -34,7 +34,7 @@ import (
3434
func FormatProtocolStack(originalStack protocols.Stack, staticTags uint64) *model.ProtocolStack {
3535
var stack []model.ProtocolType
3636

37-
if network.IsTLSTag(staticTags) || originalStack.Encryption == protocols.TLS {
37+
if tls.IsTLSTag(staticTags) || originalStack.Encryption == protocols.TLS {
3838
stack = addProtocol(stack, protocols.TLS)
3939
}
4040
if originalStack.Application != protocols.Unknown {

pkg/network/protocols/http/tls_counter_linux.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77

88
package http
99

10+
import "github.com/DataDog/datadog-agent/pkg/network/protocols/tls"
11+
1012
// Add increments the TLS-aware counter based on the specified transaction's static tags
1113
func (t *TLSCounter) Add(tx Transaction) {
1214
switch tx.StaticTags() {
13-
case GnuTLS:
15+
case tls.GnuTLS:
1416
t.counterGnuTLS.Add(1)
15-
case OpenSSL:
17+
case tls.OpenSSL:
1618
t.counterOpenSSL.Add(1)
17-
case Go:
19+
case tls.Go:
1820
t.counterGoTLS.Add(1)
19-
case Istio:
21+
case tls.Istio:
2022
t.counterIstioTLS.Add(1)
21-
case NodeJS:
23+
case tls.NodeJS:
2224
t.counterNodeJSTLS.Add(1)
2325
default:
2426
t.counterPlain.Add(1)

pkg/network/protocols/http/types.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package http
99

1010
/*
11-
#include "../../ebpf/c/protocols/tls/tags-types.h"
1211
#include "../../ebpf/c/protocols/http/types.h"
1312
#include "../../ebpf/c/protocols/classification/defs.h"
1413
*/
@@ -27,25 +26,3 @@ type EbpfTx C.http_transaction_t
2726
const (
2827
BufferSize = C.HTTP_BUFFER_SIZE
2928
)
30-
31-
type ConnTag = uint64
32-
33-
const (
34-
GnuTLS ConnTag = C.LIBGNUTLS
35-
OpenSSL ConnTag = C.LIBSSL
36-
Go ConnTag = C.GO
37-
TLS ConnTag = C.CONN_TLS
38-
Istio ConnTag = C.ISTIO
39-
NodeJS ConnTag = C.NODEJS
40-
)
41-
42-
var (
43-
StaticTags = map[ConnTag]string{
44-
GnuTLS: "tls.library:gnutls",
45-
OpenSSL: "tls.library:openssl",
46-
Go: "tls.library:go",
47-
TLS: "tls.connection:encrypted",
48-
Istio: "tls.library:istio",
49-
NodeJS: "tls.library:nodejs",
50-
}
51-
)

pkg/network/protocols/http/types_linux.go

Lines changed: 0 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/network/protocols/tls/tags.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2024-present Datadog, Inc.
5+
6+
// Package tls contains definitions and methods related to tags parsed from the TLS handshake
7+
package tls
8+
9+
import (
10+
"crypto/tls"
11+
"fmt"
12+
)
13+
14+
// Constants for tag keys
15+
const (
16+
TagTLSVersion = "tls.version:"
17+
TagTLSCipherSuiteID = "tls.cipher_suite_id:"
18+
TagTLSClientVersion = "tls.client_version:"
19+
version10 = "tls_1.0"
20+
version11 = "tls_1.1"
21+
version12 = "tls_1.2"
22+
version13 = "tls_1.3"
23+
)
24+
25+
// Bitmask constants for Offered_versions matching kernelspace definitions
26+
const (
27+
OfferedTLSVersion10 uint8 = 0x01
28+
OfferedTLSVersion11 uint8 = 0x02
29+
OfferedTLSVersion12 uint8 = 0x04
30+
OfferedTLSVersion13 uint8 = 0x08
31+
)
32+
33+
// VersionTags maps TLS versions to tag names for server chosen version (exported for testing)
34+
var VersionTags = map[uint16]string{
35+
tls.VersionTLS10: TagTLSVersion + version10,
36+
tls.VersionTLS11: TagTLSVersion + version11,
37+
tls.VersionTLS12: TagTLSVersion + version12,
38+
tls.VersionTLS13: TagTLSVersion + version13,
39+
}
40+
41+
// ClientVersionTags maps TLS versions to tag names for client offered versions (exported for testing)
42+
var ClientVersionTags = map[uint16]string{
43+
tls.VersionTLS10: TagTLSClientVersion + version10,
44+
tls.VersionTLS11: TagTLSClientVersion + version11,
45+
tls.VersionTLS12: TagTLSClientVersion + version12,
46+
tls.VersionTLS13: TagTLSClientVersion + version13,
47+
}
48+
49+
// Mapping of offered version bitmasks to version constants
50+
var offeredVersionBitmask = []struct {
51+
bitMask uint8
52+
version uint16
53+
}{
54+
{OfferedTLSVersion10, tls.VersionTLS10},
55+
{OfferedTLSVersion11, tls.VersionTLS11},
56+
{OfferedTLSVersion12, tls.VersionTLS12},
57+
{OfferedTLSVersion13, tls.VersionTLS13},
58+
}
59+
60+
// Tags holds the TLS tags. It is used to store the TLS version, cipher suite and offered versions.
61+
// We can't use the struct from eBPF as the definition is shared with windows.
62+
type Tags struct {
63+
ChosenVersion uint16
64+
CipherSuite uint16
65+
OfferedVersions uint8
66+
}
67+
68+
// MergeWith merges the tags from another Tags struct into this one
69+
func (t *Tags) MergeWith(that Tags) {
70+
if t.ChosenVersion == 0 {
71+
t.ChosenVersion = that.ChosenVersion
72+
}
73+
if t.CipherSuite == 0 {
74+
t.CipherSuite = that.CipherSuite
75+
}
76+
if t.OfferedVersions == 0 {
77+
t.OfferedVersions = that.OfferedVersions
78+
}
79+
80+
}
81+
82+
// IsEmpty returns true if all fields are zero
83+
func (t *Tags) IsEmpty() bool {
84+
if t == nil {
85+
return true
86+
}
87+
return t.ChosenVersion == 0 && t.CipherSuite == 0 && t.OfferedVersions == 0
88+
}
89+
90+
// String returns a string representation of the Tags struct
91+
func (t *Tags) String() string {
92+
return fmt.Sprintf("ChosenVersion: %d, CipherSuite: %d, OfferedVersions: %d", t.ChosenVersion, t.CipherSuite, t.OfferedVersions)
93+
}
94+
95+
// parseOfferedVersions parses the Offered_versions bitmask into a slice of version strings
96+
func parseOfferedVersions(offeredVersions uint8) []string {
97+
versions := make([]string, 0, len(offeredVersionBitmask))
98+
for _, ov := range offeredVersionBitmask {
99+
if (offeredVersions & ov.bitMask) != 0 {
100+
if name := ClientVersionTags[ov.version]; name != "" {
101+
versions = append(versions, name)
102+
}
103+
}
104+
}
105+
return versions
106+
}
107+
108+
func hexCipherSuiteTag(cipherSuite uint16) string {
109+
return fmt.Sprintf("%s0x%04X", TagTLSCipherSuiteID, cipherSuite)
110+
}
111+
112+
// GetDynamicTags generates dynamic tags based on TLS information
113+
func (t *Tags) GetDynamicTags() map[string]struct{} {
114+
if t.IsEmpty() {
115+
return nil
116+
}
117+
tags := make(map[string]struct{})
118+
119+
// Server chosen version
120+
if tag, ok := VersionTags[t.ChosenVersion]; ok {
121+
tags[tag] = struct{}{}
122+
}
123+
124+
// Client offered versions
125+
for _, versionName := range parseOfferedVersions(t.OfferedVersions) {
126+
tags[versionName] = struct{}{}
127+
}
128+
129+
// Cipher suite ID as hex string
130+
if t.CipherSuite != 0 {
131+
tags[hexCipherSuiteTag(t.CipherSuite)] = struct{}{}
132+
}
133+
134+
return tags
135+
}

pkg/network/tags_linux.go renamed to pkg/network/protocols/tls/tags_linux.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,26 @@
55

66
//go:build linux
77

8-
package network
9-
10-
import (
11-
"github.com/DataDog/datadog-agent/pkg/network/protocols/http"
12-
)
8+
package tls
139

1410
const (
1511
// ConnTagGnuTLS is the tag for GnuTLS connections
16-
ConnTagGnuTLS = http.GnuTLS
12+
ConnTagGnuTLS = GnuTLS
1713
// ConnTagOpenSSL is the tag for OpenSSL connections
18-
ConnTagOpenSSL = http.OpenSSL
14+
ConnTagOpenSSL = OpenSSL
1915
// ConnTagGo is the tag for GO TLS connections
20-
ConnTagGo = http.Go
16+
ConnTagGo = Go
2117
// ConnTagTLS is the tag for TLS connections in general
22-
ConnTagTLS = http.TLS
18+
ConnTagTLS = TLS
2319
// ConnTagIstio is the tag for Istio TLS connections
24-
ConnTagIstio = http.Istio
20+
ConnTagIstio = Istio
2521
// ConnTagNodeJS is the tag for NodeJS TLS connections
26-
ConnTagNodeJS = http.NodeJS
22+
ConnTagNodeJS = NodeJS
2723
)
2824

2925
// GetStaticTags return the string list of static tags from network.ConnectionStats.Tags
3026
func GetStaticTags(staticTags uint64) (tags []string) {
31-
for tag, str := range http.StaticTags {
27+
for tag, str := range StaticTags {
3228
if (staticTags & tag) > 0 {
3329
tags = append(tags, str)
3430
}

pkg/network/tags_nolinux.go renamed to pkg/network/protocols/tls/tags_nolinux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
//go:build !linux
77

8-
package network
8+
package tls
99

1010
// GetStaticTags return the string list of static tags from network.ConnectionStats.Tags
11-
func GetStaticTags(_ uint64) (tags []string) {
11+
func GetStaticTags(uint64) (tags []string) {
1212
return tags
1313
}
1414

1515
// IsTLSTag return if the tag is a TLS tag
16-
func IsTLSTag(_ uint64) bool {
16+
func IsTLSTag(uint64) bool {
1717
return false
1818
}

0 commit comments

Comments
 (0)