forked from chainguard-dev/malcontent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
186 lines (143 loc) · 5.3 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Copyright 2024 Chainguard, Inc.
# SPDX-License-Identifier: Apache-2.0
SAMPLES_REPO ?= chainguard-dev/malcontent-samples
SAMPLES_COMMIT ?= 2bd3bff19c0253821b3886db65a5059587cac893
# BEGIN: lint-install ../malcontent
# http://github.com/tinkerbell/lint-install
.PHONY: lint
lint: _lint
LINT_ARCH := $(shell uname -m)
LINT_OS := $(shell uname)
LINT_OS_LOWER := $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]')
LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
# shellcheck and hadolint lack arm64 native binaries: rely on x86-64 emulation
ifeq ($(LINT_OS),Darwin)
ifeq ($(LINT_ARCH),arm64)
LINT_ARCH=x86_64
endif
endif
# yara-x adds an additional string for the platform (apple, unknown)
LINT_PLATFORM :=
ifeq ($(LINT_OS),Darwin)
LINT_PLATFORM=apple
else
LINT_PLATFORM=unknown
endif
LINT_PLATFOM_SUFFIX :=
ifeq ($(LINT_OS),Linux)
LINT_PLATFORM_SUFFIX=-gnu
endif
LINTERS :=
FIXERS :=
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
GOLANGCI_LINT_VERSION ?= v1.62.0
GOLANGCI_LINT_BIN := $(LINT_ROOT)/out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
$(GOLANGCI_LINT_BIN):
mkdir -p $(LINT_ROOT)/out/linters
rm -rf $(LINT_ROOT)/out/linters/golangci-lint-*
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(LINT_ROOT)/out/linters $(GOLANGCI_LINT_VERSION)
mv $(LINT_ROOT)/out/linters/golangci-lint $@
YARA_X_VERSION ?= v0.10.0
YARA_X_BIN := $(LINT_ROOT)/out/linters/yr-$(YARA_X_VERSION)-$(LINT_ARCH)
$(YARA_X_BIN):
mkdir -p $(LINT_ROOT)/out/linters
rm -rf $(LINT_ROOT)/out/linters/yr
curl -sSfL https://github.com/VirusTotal/yara-x/releases/download/$(YARA_X_VERSION)/yara-x-$(YARA_X_VERSION)-$(LINT_ARCH)-$(LINT_PLATFORM)-$(LINT_OS_LOWER)$(LINT_PLATFORM_SUFFIX).gzip -o yara-x.gzip
tar -xzvf yara-x.gzip && mv yr $(LINT_ROOT)/out/linters && rm yara-x.gzip
mv $(LINT_ROOT)/out/linters/yr $@
LINTERS += golangci-lint-lint
golangci-lint-lint: $(GOLANGCI_LINT_BIN)
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" \;
FIXERS += golangci-lint-fix
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLANGCI_LINT_CONFIG)" --fix \;
LINTERS += yara-x-fmt
yara-x-fmt: $(YARA_X_BIN)
find rules -type f -name "*.yara" -execdir "$(YARA_X_BIN)" fmt {} \;
yara-x-compile: $(YARA_X_BIN)
"$(YARA_X_BIN)" compile ./rules/
.PHONY: _lint $(LINTERS)
_lint: $(LINTERS)
.PHONY: fix $(FIXERS)
fix: $(FIXERS)
# END: lint-install ../malcontent
# sample checkouts have 3 stages:
# out/samples/.git - I'm a blank git repo
# out/samples/.git/commit-<hash> - I'm checked out to this particular commit
# out/samples/.decompressed-<hash> - I've decompressed to this particular commit
out/${SAMPLES_REPO}/.git/commit-$(SAMPLES_COMMIT):
mkdir -p out/$(SAMPLES_REPO)
test -d out/$(SAMPLES_REPO)/.git || git clone --depth 4 https://github.com/$(SAMPLES_REPO).git out/$(SAMPLES_REPO)
rm out/$(SAMPLES_REPO)/.git/commit-* 2>/dev/null || true
git -C out/$(SAMPLES_REPO) switch - || true
git -C out/$(SAMPLES_REPO) pull
git -C out/$(SAMPLES_REPO) checkout $(SAMPLES_COMMIT)
touch out/$(SAMPLES_REPO)/.git/commit-$(SAMPLES_COMMIT)
out/$(SAMPLES_REPO)/.decompressed-$(SAMPLES_COMMIT): out/${SAMPLES_REPO}/.git/commit-$(SAMPLES_COMMIT)
find out/$(SAMPLES_REPO)/ -name "*.xz" -type f -exec xz -dk {} \;
touch out/$(SAMPLES_REPO)/.decompressed-$(SAMPLES_COMMIT)
# unit tests only
.PHONY: test
test:
go test ./pkg/...
# integration tests only
.PHONY: integration
integration: out/$(SAMPLES_REPO)/.decompressed-$(SAMPLES_COMMIT)
go test -timeout 0 ./tests/...
.PHONY: bench
bench: out/$(SAMPLES_REPO)/.decompressed-$(SAMPLES_COMMIT)
go test -run=^\$$ -bench=. ./... -benchmem
BENCH_CMD := go test -benchmem -run=^\$$ -bench ^BenchmarkRun\$$ github.com/chainguard-dev/malcontent/tests -args
.PHONY: bench-malcontent
bench-malcontent:
$(BENCH_CMD) -path="macOS/clean/malcontent"
.PHONY: bench-all-samples
bench-all-samples:
$(BENCH_CMD) -path=""
.PHONY: bench-does-nothing
bench-does-nothing:
$(BENCH_CMD) -path="does-nothing"
.PHONY: bench-javascript
bench-javascript:
$(BENCH_CMD) -path="Javascript"
.PHONY: bench-linux
bench-linux:
$(BENCH_CMD) -path="Linux"
.PHONY: bench-macos
bench-macos:
$(BENCH_CMD) -path="macOS"
.PHONY: bench-npm
bench-npm:
$(BENCH_CMD) -path="NPM"
.PHONY: bench-php
bench-php:
$(BENCH_CMD) -path="PHP"
.PHONY: bench-python
bench-python:
$(BENCH_CMD) -path="Python"
.PHONY: bench-typescript
bench-typescript:
$(BENCH_CMD) -path="TypeScript"
.PHONY: bench-windows
bench-windows:
$(BENCH_CMD) -path="Windows"
.PHONY: out/mal
out/mal:
mkdir -p out
go build -o out/mal ./cmd/mal
.PHONY: update-third-party
update-third-party:
./third_party/yara/update.sh
.PHONY: refresh-sample-testdata
refresh-sample-testdata: out/$(SAMPLES_REPO)/.decompressed-$(SAMPLES_COMMIT) out/mal
./out/mal refresh
ARCH ?= $(shell uname -m)
CRANE_VERSION=v0.20.2
out/crane-$(ARCH)-$(CRANE_VERSION):
mkdir -p out
GOBIN=$(CURDIR)/out go install github.com/google/go-containerregistry/cmd/crane@$(CRANE_VERSION)
mv out/crane out/crane-$(ARCH)-$(CRANE_VERSION)
export-image: out/crane-$(ARCH)-$(CRANE_VERSION)
./out/crane-$(ARCH)-$(CRANE_VERSION) \
export \
cgr.dev/chainguard/static:latest@sha256:bde549df44d5158013856a778b34d8972cf52bb2038ec886475d857ec7c365ed - | xz > pkg/action/testdata/static.tar.xz