Skip to content

Commit

Permalink
feat: add windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMuller committed Nov 6, 2023
1 parent d90451d commit f462bb8
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 17 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,26 @@ jobs:
platforms: ${{ matrix.arch }}
- run: docker run --platform=linux/${{ matrix.arch }} -v${HOME}/go/pkg/mod:/root/go/pkg/mod -v $PWD:$PWD -w $PWD -eCGO_ENABLED=${{ matrix.cgo_enabled }} -eDD_APPSEC_WAF_TIMEOUT=$DD_APPSEC_WAF_TIMEOUT golang go test -v -count=10 -shuffle=on ./...

windows-other:
runs-on: windows-latest
strategy:
matrix:
arch: ["386"]
cgo_enabled: [ "0", "1" ] # test it compiles with and without the cgo
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
cache: true
- name: go test
shell: bash
run: |
# Install gotestsum
env GOBIN=$PWD go install gotest.tools/gotestsum@latest
# Run the tests with gotestsum
env GOARCH=${{matrix.arch}} CGO_ENABLED=${{ matrix.cgo_enabled }} ./gotestsum -- -v -count=10 -shuffle=on ./...
# A simple join target to simplify setting up branch protection settings in GH.
done:
name: Done
Expand All @@ -105,6 +125,7 @@ jobs:
- native
- golang-linux-container
- linux-other
- windows-other
steps:
- name: Done
run: echo "Done!"
42 changes: 32 additions & 10 deletions _tools/libddwaf-updater/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,20 @@ func main() {
}

func createEmbedSource(tgt target) {
ext := "so"
if tgt.os == "darwin" {
var base string
var ext string
switch tgt.os {
case "darwin":
base = "libddwaf"
ext = "dylib"
case "linux":
base = "libddwaf"
ext = "so"
case "windows":
base = "ddwaf"
ext = "dll"
default:
panic(fmt.Errorf("unsupported os: %s", tgt.os))
}

gosource := strings.Join(
Expand All @@ -113,10 +124,10 @@ func createEmbedSource(tgt target) {
"",
`import _ "embed" // Needed for go:embed`,
"",
fmt.Sprintf("//go:embed %s-%s/libddwaf.%s", tgt.os, tgt.arch, ext),
fmt.Sprintf("//go:embed %s-%s/%s.%s", tgt.os, tgt.arch, base, ext),
"var libddwaf []byte",
"",
fmt.Sprintf(`const embedNamePattern = "libddwaf-*.%s"`, ext),
fmt.Sprintf(`const embedNamePattern = "%s-*.%s"`, base, ext),
"", // Trailing new line...
},
"\n",
Expand Down Expand Up @@ -157,12 +168,11 @@ func handleTarget(wg *sync.WaitGroup, version string, tgt target, embedDir strin
if err != nil {
panic(err)
}
sha = sha[:64] // Only keep the hex-encoded SHA256
sum, err := script.File(path.Join(tmpdir, tarName)).SHA256Sum()
if err != nil {
panic(err)
}
// To match the shasum format...
sum = fmt.Sprintf("%s %s\n", sum, tarName)
if sum != sha {
panic(fmt.Errorf("checksum mismatch on %s:\nExpected %s\nActual %s", tarUrl, sha, sum))
}
Expand All @@ -189,7 +199,7 @@ func handleTarget(wg *sync.WaitGroup, version string, tgt target, embedDir strin

var destPath string
switch name := header.FileInfo().Name(); name {
case "libddwaf.so", "libddwaf.dylib":
case "libddwaf.so", "libddwaf.dylib", "ddwaf.dll":
destPath = path.Join(embedDir, name)
foundLib = true
case "ddwaf.h":
Expand All @@ -207,9 +217,11 @@ func handleTarget(wg *sync.WaitGroup, version string, tgt target, embedDir strin
if _, err := script.NewPipe().WithReader(arch).WriteFile(destPath); err != nil {
panic(err)
}
// Make the libraries executable, as this can be useful to link directly to those objects to perform troubleshooting.
if err := os.Chmod(destPath, 0755); err != nil {
panic(err)
if path.Ext(destPath) != ".h" {
// Make the libraries executable, as this can be useful to link directly to those objects to perform troubleshooting.
if err := os.Chmod(destPath, 0755); err != nil {
panic(err)
}
}

if foundLib && (foundHdr || !tgt.primary) {
Expand Down Expand Up @@ -254,6 +266,16 @@ var targets = []target{
arch: "arm64",
assetLabel: "aarch64-linux-musl",
},
{
os: "windows",
arch: "amd64",
assetLabel: "windows-x64",
},
{
os: "windows",
arch: "386",
assetLabel: "windows-win32",
},
// These are currentlu not supported by ebitengine/purego:
// {os: "linux", arch: "armv7", assetLabel: "armv7-linux-musl"},
// {os: "linux", arch: "i386", assetLabel: "i386-linux-musl"},
Expand Down
2 changes: 1 addition & 1 deletion internal/vendor/vendor.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 ((darwin && (amd64 || arm64)) || (linux && (amd64 || arm64))) && !go1.22
//go:build ((darwin && (amd64 || arm64)) || (linux && (amd64 || arm64)) || (windows && (amd64 || 386))) && !go1.22

package vendor

Expand Down
18 changes: 18 additions & 0 deletions internal/vendor/vendor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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)) || (linux && (amd64 || arm64)) || (windows && (amd64 || 386))) && !go1.22

package vendor

import (
"testing"

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

func TestVersionHasCorrectFormat(t *testing.T) {
assert.Regexp(t, `^\d+\.\d+\.\d+$`, EmbeddedWAFVersion)
}
14 changes: 14 additions & 0 deletions internal/vendor/vendor_windows_386.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 2016-present Datadog, Inc.

//go:build windows && 386 && !go1.22
package vendor

import _ "embed" // Needed for go:embed

//go:embed windows-386/ddwaf.dll
var libddwaf []byte

const embedNamePattern = "ddwaf-*.dll"
14 changes: 14 additions & 0 deletions internal/vendor/vendor_windows_amd64.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 2016-present Datadog, Inc.

//go:build windows && amd64 && !go1.22
package vendor

import _ "embed" // Needed for go:embed

//go:embed windows-amd64/ddwaf.dll
var libddwaf []byte

const embedNamePattern = "ddwaf-*.dll"
Binary file added internal/vendor/windows-386/ddwaf.dll
Binary file not shown.
Binary file added internal/vendor/windows-amd64/ddwaf.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion waf_dl.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 (linux || darwin) && (amd64 || arm64) && !go1.22
//go:build ((darwin && (amd64 || arm64)) || (linux && (amd64 || arm64)) || (windows && (amd64 || 386))) && !go1.22

package waf

Expand Down
2 changes: 1 addition & 1 deletion waf_dl_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 (linux || darwin) && (amd64 || arm64) && !go1.22
//go:build ((darwin && (amd64 || arm64)) || (linux && (amd64 || arm64)) || (windows && (amd64 || 386))) && !go1.22

package waf

Expand Down
2 changes: 1 addition & 1 deletion waf_dl_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Copyright 2016-present Datadog, Inc.

// Build when the target OS or architecture are not supported
//go:build (!linux && !darwin) || (!amd64 && !arm64) || go1.22
//go:build !((darwin && (amd64 || arm64)) || (linux && (amd64 || arm64)) || (windows && (amd64 || 386))) || go1.22

package waf

Expand Down
2 changes: 1 addition & 1 deletion waf_unsupported_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// Supported OS/Arch but unsupported Go version
// Supported OS Supported Arch Bad Go Version
//go:build (linux || darwin || windows) && (amd64 || arm64) && go1.22
//go:build ((darwin && (amd64 || arm64)) || (linux && (amd64 || arm64)) || (windows && (amd64 || 386))) && go1.22

package waf

Expand Down
2 changes: 1 addition & 1 deletion waf_unsupported_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

// Unsupported target OS or architecture on a supported Go version
// Unsupported OS Unsupported Arch Good Go Version
//go:build ((!linux && !darwin) || (!amd64 && !arm64)) && !go1.22
//go:build !((darwin && (amd64 || arm64)) || (linux && (amd64 || arm64)) || (windows && (amd64 || 386))) && !go1.22

package waf

Expand Down
2 changes: 1 addition & 1 deletion waf_unsupported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Copyright 2016-present Datadog, Inc.

// Build when the target OS or Arch are not supported
//go:build (!linux && !darwin) || (!amd64 && !arm64) || go1.22
//go:build !((darwin && (amd64 || arm64)) || (linux && (amd64 || arm64)) || (windows && (amd64 || 386))) || go1.22

package waf_test

Expand Down

0 comments on commit f462bb8

Please sign in to comment.