-
Notifications
You must be signed in to change notification settings - Fork 1.3k
usm: move is tls static tags #38108
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
Merged
Merged
usm: move is tls static tags #38108
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// 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 tls contains definitions and methods related to tags parsed from the TLS handshake | ||
package tls | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
) | ||
|
||
// Constants for tag keys | ||
const ( | ||
TagTLSVersion = "tls.version:" | ||
TagTLSCipherSuiteID = "tls.cipher_suite_id:" | ||
TagTLSClientVersion = "tls.client_version:" | ||
version10 = "tls_1.0" | ||
version11 = "tls_1.1" | ||
version12 = "tls_1.2" | ||
version13 = "tls_1.3" | ||
) | ||
|
||
// Bitmask constants for Offered_versions matching kernelspace definitions | ||
const ( | ||
OfferedTLSVersion10 uint8 = 0x01 | ||
OfferedTLSVersion11 uint8 = 0x02 | ||
OfferedTLSVersion12 uint8 = 0x04 | ||
OfferedTLSVersion13 uint8 = 0x08 | ||
) | ||
|
||
// VersionTags maps TLS versions to tag names for server chosen version (exported for testing) | ||
var VersionTags = map[uint16]string{ | ||
tls.VersionTLS10: TagTLSVersion + version10, | ||
tls.VersionTLS11: TagTLSVersion + version11, | ||
tls.VersionTLS12: TagTLSVersion + version12, | ||
tls.VersionTLS13: TagTLSVersion + version13, | ||
} | ||
|
||
// ClientVersionTags maps TLS versions to tag names for client offered versions (exported for testing) | ||
var ClientVersionTags = map[uint16]string{ | ||
tls.VersionTLS10: TagTLSClientVersion + version10, | ||
tls.VersionTLS11: TagTLSClientVersion + version11, | ||
tls.VersionTLS12: TagTLSClientVersion + version12, | ||
tls.VersionTLS13: TagTLSClientVersion + version13, | ||
} | ||
|
||
// Mapping of offered version bitmasks to version constants | ||
var offeredVersionBitmask = []struct { | ||
bitMask uint8 | ||
version uint16 | ||
}{ | ||
{OfferedTLSVersion10, tls.VersionTLS10}, | ||
{OfferedTLSVersion11, tls.VersionTLS11}, | ||
{OfferedTLSVersion12, tls.VersionTLS12}, | ||
{OfferedTLSVersion13, tls.VersionTLS13}, | ||
} | ||
|
||
// Tags holds the TLS tags. It is used to store the TLS version, cipher suite and offered versions. | ||
// We can't use the struct from eBPF as the definition is shared with windows. | ||
type Tags struct { | ||
ChosenVersion uint16 | ||
CipherSuite uint16 | ||
OfferedVersions uint8 | ||
} | ||
|
||
// MergeWith merges the tags from another Tags struct into this one | ||
func (t *Tags) MergeWith(that Tags) { | ||
if t.ChosenVersion == 0 { | ||
t.ChosenVersion = that.ChosenVersion | ||
} | ||
if t.CipherSuite == 0 { | ||
t.CipherSuite = that.CipherSuite | ||
} | ||
if t.OfferedVersions == 0 { | ||
t.OfferedVersions = that.OfferedVersions | ||
} | ||
|
||
} | ||
|
||
// IsEmpty returns true if all fields are zero | ||
func (t *Tags) IsEmpty() bool { | ||
if t == nil { | ||
return true | ||
} | ||
return t.ChosenVersion == 0 && t.CipherSuite == 0 && t.OfferedVersions == 0 | ||
} | ||
|
||
// String returns a string representation of the Tags struct | ||
func (t *Tags) String() string { | ||
return fmt.Sprintf("ChosenVersion: %d, CipherSuite: %d, OfferedVersions: %d", t.ChosenVersion, t.CipherSuite, t.OfferedVersions) | ||
} | ||
|
||
// parseOfferedVersions parses the Offered_versions bitmask into a slice of version strings | ||
func parseOfferedVersions(offeredVersions uint8) []string { | ||
versions := make([]string, 0, len(offeredVersionBitmask)) | ||
for _, ov := range offeredVersionBitmask { | ||
if (offeredVersions & ov.bitMask) != 0 { | ||
if name := ClientVersionTags[ov.version]; name != "" { | ||
versions = append(versions, name) | ||
} | ||
} | ||
} | ||
return versions | ||
} | ||
|
||
func hexCipherSuiteTag(cipherSuite uint16) string { | ||
return fmt.Sprintf("%s0x%04X", TagTLSCipherSuiteID, cipherSuite) | ||
} | ||
|
||
// GetDynamicTags generates dynamic tags based on TLS information | ||
func (t *Tags) GetDynamicTags() map[string]struct{} { | ||
if t.IsEmpty() { | ||
return nil | ||
} | ||
tags := make(map[string]struct{}) | ||
|
||
// Server chosen version | ||
if tag, ok := VersionTags[t.ChosenVersion]; ok { | ||
tags[tag] = struct{}{} | ||
} | ||
|
||
// Client offered versions | ||
for _, versionName := range parseOfferedVersions(t.OfferedVersions) { | ||
tags[versionName] = struct{}{} | ||
} | ||
|
||
// Cipher suite ID as hex string | ||
if t.CipherSuite != 0 { | ||
tags[hexCipherSuiteTag(t.CipherSuite)] = struct{}{} | ||
} | ||
|
||
return tags | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.