-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
Signed-off-by: Eliott Bouhana <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 2016-present Datadog, Inc. | ||
|
||
//go:build darwin && (amd64 || arm64) && !go1.24 && !datadog.no_waf && (cgo || appsec) | ||
|
||
package lib | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
_ "embed" | ||
) | ||
|
||
//go:embed .version | ||
var EmbeddedWAFVersion string | ||
|
||
func DumpEmbeddedWAF() (file *os.File, closer func() error, err error) { | ||
file, err := os.CreateTemp("", embedNamePattern) | ||
Check failure on line 24 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.21
Check failure on line 24 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 24 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-13 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 24 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 24 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.21
|
||
if err != nil { | ||
return nil, nil, fmt.Errorf("error creating temp file: %w", err) | ||
} | ||
path = file.Name() | ||
Check failure on line 28 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.21
Check failure on line 28 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 28 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-13 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 28 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 28 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.21
|
||
|
||
defer func() { | ||
if err != nil { | ||
if closeErr := file.Close(); closeErr != nil { | ||
err = errors.Join(err, fmt.Errorf("error closing file: %w", closeErr)) | ||
} | ||
if rmErr := os.Remove(path); rmErr != nil { | ||
Check failure on line 35 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.21
Check failure on line 35 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 35 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-13 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 35 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 35 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.21
|
||
err = errors.Join(err, fmt.Errorf("error removing file: %w", rmErr)) | ||
} | ||
} | ||
}() | ||
|
||
gr, err := gzip.NewReader(bytes.NewReader(libddwaf)) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error creating gzip reader: %w", err) | ||
} | ||
|
||
if _, err := io.Copy(file, gr); err != nil { | ||
Check failure on line 46 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.21
Check failure on line 46 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 46 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-13 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 46 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 46 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.21
|
||
return nil, nil, fmt.Errorf("error copying gzip content to file: %w", err) | ||
} | ||
|
||
if err := gr.Close(); err != nil { | ||
return nil, nil, fmt.Errorf("error closing gzip reader: %w", err) | ||
} | ||
|
||
return file, func() { | ||
Check failure on line 54 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.21
Check failure on line 54 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 54 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-13 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 54 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 54 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.21
|
||
return errors.Join(file.Close(), os.Remove(path)) | ||
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.21
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.21
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-13 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-14 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-13 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.22 (DD_APPSEC_WAF_LOG_LEVEL=TRACE)
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.21
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-12 go1.21
Check failure on line 55 in internal/lib/dump_waf_darwin.go GitHub Actions / GitHub Runner / macos-13 go1.21
|
||
}, nil | ||
} |
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 2016-present Datadog, Inc. | ||
|
||
//go:build linux && (amd64 || arm64) && !go1.24 && !datadog.no_waf && (cgo || appsec) | ||
|
||
package lib | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"errors" | ||
"fmt" | ||
"golang.org/x/sys/unix" | ||
"io" | ||
"os" | ||
|
||
_ "embed" | ||
) | ||
|
||
//go:embed .version | ||
var EmbeddedWAFVersion string | ||
|
||
func DumpEmbeddedWAF() (file *os.File, closer func() error, err error) { | ||
|
||
fd, err := unix.MemfdCreate("libddwaf", 0) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error creating memfd: %w", err) | ||
} | ||
|
||
file = os.NewFile(uintptr(fd), fmt.Sprintf("/proc/self/fd/%d", fd)) | ||
if file == nil { | ||
return nil, nil, errors.New("error creating file from fd") | ||
} | ||
|
||
defer func() { | ||
if file != nil && err != nil { | ||
if closeErr := file.Close(); closeErr != nil { | ||
err = errors.Join(err, fmt.Errorf("error closing file: %w", closeErr)) | ||
} | ||
} | ||
}() | ||
|
||
gr, err := gzip.NewReader(bytes.NewReader(libddwaf)) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("error creating gzip reader: %w", err) | ||
} | ||
|
||
if _, err := io.Copy(file, gr); err != nil { | ||
return nil, nil, fmt.Errorf("error copying gzip content to memfd: %w", err) | ||
} | ||
|
||
if err := gr.Close(); err != nil { | ||
return nil, nil, fmt.Errorf("error closing gzip reader: %w", err) | ||
} | ||
|
||
return file, file.Close, nil | ||
} |
This file was deleted.