From d52d09e57d56d8128b783b72ec389c07e3993d99 Mon Sep 17 00:00:00 2001 From: Areg Vrtanesyan Date: Thu, 24 Jul 2025 10:26:30 +0100 Subject: [PATCH 1/4] Fixing conflicts and refreshing FreeBSD build for cloudflared daemon --- Makefile | 7 +++++-- diagnostic/network/collector_unix.go | 2 +- diagnostic/network/collector_unix_test.go | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index e9b411dcf74..8dfef42add6 100644 --- a/Makefile +++ b/Makefile @@ -54,8 +54,11 @@ endif IMPORT_PATH := github.com/cloudflare/cloudflared PACKAGE_DIR := $(CURDIR)/packaging PREFIX := /usr -INSTALL_BINDIR := $(PREFIX)/bin/ -INSTALL_MANDIR := $(PREFIX)/share/man/man1/ +ifeq ($(LOCAL_OS),freebsd) + PREFIX := /usr/local +endif +INSTALL_BINDIR := $(PREFIX)/bin +INSTALL_MANDIR := $(PREFIX)/share/man/man1 LOCAL_ARCH ?= $(shell uname -m) ifneq ($(GOARCH),) diff --git a/diagnostic/network/collector_unix.go b/diagnostic/network/collector_unix.go index 2db2d2629f3..94bfaa15628 100644 --- a/diagnostic/network/collector_unix.go +++ b/diagnostic/network/collector_unix.go @@ -1,4 +1,4 @@ -//go:build darwin || linux +//go:build darwin || linux || freebsd package diagnostic diff --git a/diagnostic/network/collector_unix_test.go b/diagnostic/network/collector_unix_test.go index 5ec231a3f83..5d52c591149 100644 --- a/diagnostic/network/collector_unix_test.go +++ b/diagnostic/network/collector_unix_test.go @@ -1,4 +1,4 @@ -//go:build darwin || linux +//go:build darwin || linux || freebsd package diagnostic_test From c58b267df86f6537f57b3c12e274933a5ffd604e Mon Sep 17 00:00:00 2001 From: Areg Vrtanesyan Date: Thu, 24 Jul 2025 10:30:30 +0100 Subject: [PATCH 2/4] Fixing conflicts and refreshing FreeBSD build for cloudflared daemon - added missing file --- diagnostic/system_collector_freebsd.go | 172 +++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 diagnostic/system_collector_freebsd.go diff --git a/diagnostic/system_collector_freebsd.go b/diagnostic/system_collector_freebsd.go new file mode 100644 index 00000000000..3f81cfb9478 --- /dev/null +++ b/diagnostic/system_collector_freebsd.go @@ -0,0 +1,172 @@ +//go:build freebsd + +package diagnostic + +import ( + "context" + "fmt" + "os/exec" + "runtime" + "strconv" +) + +type SystemCollectorImpl struct { + version string +} + +func NewSystemCollectorImpl( + version string, +) *SystemCollectorImpl { + return &SystemCollectorImpl{ + version, + } +} + +func (collector *SystemCollectorImpl) Collect(ctx context.Context) (*SystemInformation, error) { + memoryInfo, memoryInfoRaw, memoryInfoErr := collectMemoryInformation(ctx) + fdInfo, fdInfoRaw, fdInfoErr := collectFileDescriptorInformation(ctx) + disks, disksRaw, diskErr := collectDiskVolumeInformationUnix(ctx) + osInfo, osInfoRaw, osInfoErr := collectOSInformationUnix(ctx) + + var memoryMaximum, memoryCurrent, fileDescriptorMaximum, fileDescriptorCurrent uint64 + var osSystem, name, osVersion, osRelease, architecture string + + err := SystemInformationGeneralError{ + OperatingSystemInformationError: nil, + MemoryInformationError: nil, + FileDescriptorsInformationError: nil, + DiskVolumeInformationError: nil, + } + + if memoryInfoErr != nil { + err.MemoryInformationError = SystemInformationError{ + Err: memoryInfoErr, + RawInfo: memoryInfoRaw, + } + } else { + memoryMaximum = memoryInfo.MemoryMaximum + memoryCurrent = memoryInfo.MemoryCurrent + } + + if fdInfoErr != nil { + err.FileDescriptorsInformationError = SystemInformationError{ + Err: fdInfoErr, + RawInfo: fdInfoRaw, + } + } else { + fileDescriptorMaximum = fdInfo.FileDescriptorMaximum + fileDescriptorCurrent = fdInfo.FileDescriptorCurrent + } + + if diskErr != nil { + err.DiskVolumeInformationError = SystemInformationError{ + Err: diskErr, + RawInfo: disksRaw, + } + } + + if osInfoErr != nil { + err.OperatingSystemInformationError = SystemInformationError{ + Err: osInfoErr, + RawInfo: osInfoRaw, + } + } else { + osSystem = osInfo.OsSystem + name = osInfo.Name + osVersion = osInfo.OsVersion + osRelease = osInfo.OsRelease + architecture = osInfo.Architecture + } + + cloudflaredVersion := collector.version + info := NewSystemInformation( + memoryMaximum, + memoryCurrent, + fileDescriptorMaximum, + fileDescriptorCurrent, + osSystem, + name, + osVersion, + osRelease, + architecture, + cloudflaredVersion, + runtime.Version(), + runtime.GOARCH, + disks, + ) + + return info, err +} + +func collectFileDescriptorInformation(ctx context.Context) ( + *FileDescriptorInformation, + string, + error, +) { + const ( + fileDescriptorMaximumKey = "kern.maxfiles" + fileDescriptorCurrentKey = "kern.num_files" + ) + + command := exec.CommandContext(ctx, "sysctl", fileDescriptorMaximumKey, fileDescriptorCurrentKey) + + stdout, err := command.Output() + if err != nil { + return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err) + } + + output := string(stdout) + + fileDescriptorInfo, err := ParseFileDescriptorInformationFromKV( + output, + fileDescriptorMaximumKey, + fileDescriptorCurrentKey, + ) + if err != nil { + return nil, output, err + } + + // returning raw output in case other collected information + // resulted in errors + return fileDescriptorInfo, output, nil +} + +func collectMemoryInformation(ctx context.Context) ( + *MemoryInformation, + string, + error, +) { + const ( + memoryMaximumKey = "hw.memsize" + memoryAvailableKey = "hw.memsize_usable" + ) + + command := exec.CommandContext( + ctx, + "sysctl", + memoryMaximumKey, + memoryAvailableKey, + ) + + stdout, err := command.Output() + if err != nil { + return nil, "", fmt.Errorf("error retrieving output from command '%s': %w", command.String(), err) + } + + output := string(stdout) + + mapper := func(field string) (uint64, error) { + const kiloBytes = 1024 + value, err := strconv.ParseUint(field, 10, 64) + return value / kiloBytes, err + } + + memoryInfo, err := ParseMemoryInformationFromKV(output, memoryMaximumKey, memoryAvailableKey, mapper) + if err != nil { + return nil, output, err + } + + // returning raw output in case other collected information + // resulted in errors + return memoryInfo, output, nil +} From a211d4f78dcf75131faebd204505509c15df187c Mon Sep 17 00:00:00 2001 From: Areg Vrtanesyan Date: Thu, 24 Jul 2025 11:46:36 +0100 Subject: [PATCH 3/4] Workaround for LOCAL_OS in PREFIX selection as it is not defined it yet. Line 86 for "LOCAL_OS ?= $(shell go env GOOS)" could be moved ahead for cleaner code. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8dfef42add6..b4e336b26a9 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ endif IMPORT_PATH := github.com/cloudflare/cloudflared PACKAGE_DIR := $(CURDIR)/packaging PREFIX := /usr -ifeq ($(LOCAL_OS),freebsd) +ifeq ($(shell go env GOOS),freebsd) PREFIX := /usr/local endif INSTALL_BINDIR := $(PREFIX)/bin From 098a12eb9b630f6f0b41c6716f5dfb0c5ed7ff35 Mon Sep 17 00:00:00 2001 From: Areg Vrtanesyan Date: Wed, 17 Sep 2025 16:03:24 +0100 Subject: [PATCH 4/4] Adjusting with latest changes --- Makefile | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index b4e336b26a9..cef500d52e4 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,13 @@ else DEB_PACKAGE_NAME := $(BINARY_NAME) endif -DATE := $(shell date -u -r RELEASE_NOTES '+%Y-%m-%d-%H%M UTC') +# Use git in windows since we don't have access to the `date` tool +ifeq ($(TARGET_OS), windows) + DATE := $(shell git log -1 --format="%ad" --date=format-local:'%Y-%m-%dT%H:%M UTC' -- RELEASE_NOTES) +else + DATE := $(shell date -u -r RELEASE_NOTES '+%Y-%m-%d-%H:%M UTC') +endif + VERSION_FLAGS := -X "main.Version=$(VERSION)" -X "main.BuildTime=$(DATE)" ifdef PACKAGE_MANAGER VERSION_FLAGS := $(VERSION_FLAGS) -X "github.com/cloudflare/cloudflared/cmd/cloudflared/updater.BuiltForPackageManager=$(PACKAGE_MANAGER)" @@ -67,6 +73,8 @@ else ifeq ($(LOCAL_ARCH),x86_64) TARGET_ARCH ?= amd64 else ifeq ($(LOCAL_ARCH),amd64) TARGET_ARCH ?= amd64 +else ifeq ($(LOCAL_ARCH),386) + TARGET_ARCH ?= 386 else ifeq ($(LOCAL_ARCH),i686) TARGET_ARCH ?= amd64 else ifeq ($(shell echo $(LOCAL_ARCH) | head -c 5),armv8) @@ -123,6 +131,8 @@ endif #for FIPS compliance, FPM defaults to MD5. RPM_DIGEST := --rpm-digest sha256 +GO_TEST_LOG_OUTPUT = /tmp/gotest.log + .PHONY: all all: cloudflared test @@ -132,7 +142,7 @@ clean: .PHONY: vulncheck vulncheck: - @govulncheck ./... + @go run -mod=readonly golang.org/x/vuln/cmd/govulncheck@latest ./... .PHONY: cloudflared cloudflared: @@ -155,11 +165,9 @@ generate-docker-version: .PHONY: test test: vet -ifndef CI - go test -v -mod=vendor -race $(LDFLAGS) ./... -else - @mkdir -p .cover - go test -v -mod=vendor -race $(LDFLAGS) -coverprofile=".cover/c.out" ./... + $Q go test -json -v -mod=vendor -race $(LDFLAGS) ./... 2>&1 | tee $(GO_TEST_LOG_OUTPUT) +ifneq ($(FIPS), true) + @go run -mod=readonly github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest -input $(GO_TEST_LOG_OUTPUT) endif .PHONY: cover @@ -233,8 +241,8 @@ github-release: python3 github_release.py --path $(PWD)/built_artifacts --release-version $(VERSION) python3 github_message.py --release-version $(VERSION) -.PHONY: macos-release -macos-release: +.PHONY: gitlab-release +gitlab-release: python3 github_release.py --path $(PWD)/artifacts/ --release-version $(VERSION) .PHONY: r2-linux-release @@ -249,7 +257,7 @@ capnp: .PHONY: vet vet: - go vet -mod=vendor github.com/cloudflare/cloudflared/... + $Q go vet -mod=vendor github.com/cloudflare/cloudflared/... .PHONY: fmt fmt: @@ -258,7 +266,7 @@ fmt: .PHONY: fmt-check fmt-check: - @./fmt-check.sh + @./.ci/scripts/fmt-check.sh .PHONY: lint lint: @@ -267,3 +275,23 @@ lint: .PHONY: mocks mocks: go generate mocks/mockgen.go + +.PHONY: ci-build +ci-build: + @GOOS=linux GOARCH=amd64 $(MAKE) cloudflared + @mkdir -p artifacts + @mv cloudflared artifacts/cloudflared + +.PHONY: ci-fips-build +ci-fips-build: + @FIPS=true GOOS=linux GOARCH=amd64 $(MAKE) cloudflared + @mkdir -p artifacts + @mv cloudflared artifacts/cloudflared + +.PHONY: ci-test +ci-test: fmt-check lint test + @go run -mod=readonly github.com/jstemmer/go-junit-report/v2@latest -in $(GO_TEST_LOG_OUTPUT) -parser gojson -out report.xml -set-exit-code + +.PHONY: ci-fips-test +ci-fips-test: + @FIPS=true $(MAKE) ci-test