Skip to content

Commit

Permalink
use lazyregexp in a few places
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux committed Jan 17, 2025
1 parent a4e0acd commit 497d0b0
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 13 deletions.
4 changes: 3 additions & 1 deletion pkg/security/secl/compiler/eval/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"regexp"
"slices"
"strings"

"golang.org/x/mod/internal/lazyregexp"
)

// StringCmpOpts defines options to apply during string comparison
Expand Down Expand Up @@ -123,7 +125,7 @@ type RegexpStringMatcher struct {
re *regexp.Regexp
}

var stringBigOrRe = regexp.MustCompile(`^(?:\.\*)?\(([a-zA-Z_|]+)\)(?:\.\*)?$`)
var stringBigOrRe = lazyregexp.New(`^(?:\.\*)?\(([a-zA-Z_|]+)\)(?:\.\*)?$`)

// Compile a regular expression based pattern
func (r *RegexpStringMatcher) Compile(pattern string, caseInsensitive bool) error {
Expand Down
7 changes: 4 additions & 3 deletions pkg/security/secl/containerutils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
package containerutils

import (
"regexp"
"strings"

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

// ContainerIDPatternStr defines the regexp used to match container IDs
// ([0-9a-fA-F]{64}) is standard container id used pretty much everywhere, length: 64
// ([0-9a-fA-F]{32}-\d+) is container id used by AWS ECS, length: 43
// ([0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){4}) is container id used by Garden, length: 28
var ContainerIDPatternStr = ""
var containerIDPattern *regexp.Regexp
var containerIDPattern *lazyregexp.LazyRegexp

var containerIDCoreChars = "0123456789abcdefABCDEF"

Expand All @@ -26,7 +27,7 @@ func init() {
prefixes = append(prefixes, runtimePrefix.prefix)
}
ContainerIDPatternStr = "(?:" + strings.Join(prefixes[:], "|") + ")?([0-9a-fA-F]{64})|([0-9a-fA-F]{32}-\\d+)|([0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){4})"
containerIDPattern = regexp.MustCompile(ContainerIDPatternStr)
containerIDPattern = lazyregexp.New(ContainerIDPatternStr)
}

func isSystemdScope(cgroup CGroupID) bool {
Expand Down
5 changes: 4 additions & 1 deletion pkg/security/secl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/DataDog/datadog-agent/pkg/security/secl
go 1.23.0

require (
github.com/DataDog/datadog-agent/pkg/util/lazyregexp v0.0.0-00010101000000-000000000000
github.com/Masterminds/semver/v3 v3.3.1
github.com/Masterminds/sprig/v3 v3.3.0
github.com/alecthomas/participle v0.7.1
Expand All @@ -17,6 +18,7 @@ require (
github.com/spf13/cast v1.7.1
github.com/stretchr/testify v1.10.0
github.com/xeipuuv/gojsonschema v1.2.0
golang.org/x/mod v0.22.0
golang.org/x/sys v0.29.0
golang.org/x/text v0.21.0
golang.org/x/tools v0.29.0
Expand All @@ -40,7 +42,8 @@ require (
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/crypto v0.32.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

replace github.com/DataDog/datadog-agent/pkg/util/lazyregexp => ../../util/lazyregexp
6 changes: 2 additions & 4 deletions pkg/security/secl/validators/ruleid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
// Package validators holds validators related files
package validators

import (
"regexp"
)
import "github.com/DataDog/datadog-agent/pkg/util/lazyregexp"

// RuleIDPattern represents the regex that `RuleID`s must match
var RuleIDPattern = regexp.MustCompile(`^[a-zA-Z0-9_]*$`)
var RuleIDPattern = lazyregexp.New(`^[a-zA-Z0-9_]*$`)

// CheckRuleID validates a ruleID
func CheckRuleID(ruleID string) bool {
Expand Down
4 changes: 2 additions & 2 deletions pkg/security/seclog/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ package seclog

import (
"fmt"
"regexp"
"runtime"
"strings"
"sync"

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

Expand All @@ -22,7 +22,7 @@ const (
)

// used to extract package.struct.func from the caller
var re = regexp.MustCompile(`[^\.]*\/([^\.]*)\.\(?\*?([^\.\)]*)\)?\.(.*)$`)
var re = lazyregexp.New(`[^\.]*\/([^\.]*)\.\(?\*?([^\.\)]*)\)?\.(.*)$`)

// TagStringer implements fmt.Stringer
type TagStringer struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/security/seclwin/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/security/seclwin/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/security/utils/proc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"

"github.com/DataDog/datadog-agent/pkg/security/secl/model/sharedconsts"
"github.com/DataDog/datadog-agent/pkg/util/kernel"
"github.com/DataDog/datadog-agent/pkg/util/lazyregexp"
"github.com/shirou/gopsutil/v4/process"
)

Expand All @@ -35,7 +35,7 @@ func Getpid() uint32 {
return uint32(os.Getpid())
}

var networkNamespacePattern = regexp.MustCompile(`net:\[(\d+)\]`)
var networkNamespacePattern = lazyregexp.New(`net:\[(\d+)\]`)

// NetNSPath represents a network namespace path
type NetNSPath struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/lazyregexp/re.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func (lr *LazyRegexp) re() *regexp.Regexp {
return lr.compiled
}

// MatchString see regexp.MatchString
func (lr *LazyRegexp) MatchString(s string) bool {
return lr.re().MatchString(s)
}

// FindStringSubmatch see regexp.FindStringSubmatch
func (lr *LazyRegexp) FindStringSubmatch(s string) []string {
return lr.re().FindStringSubmatch(s)
Expand Down

0 comments on commit 497d0b0

Please sign in to comment.