Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASCII-1615] Replace github.com/mholt/archiver/v3 third party library with custom package pkg/util/archive #25498

Merged
merged 7 commits into from May 15, 2024

Conversation

GustavoCaso
Copy link
Member

@GustavoCaso GustavoCaso commented May 10, 2024

What does this PR do?

Remove the third part library github.com/mholt/archiver/v3 from the flare package

Motivation

The library includes the vulnerability CVE-2024-0406. Technically speaking we are not affected by this in production as we do not uncompress tar files. Still, it is a good opportunity to evaluate if we need a third party library for zip opertaions.

We decided to remove the third party dependency by vendoring some of its code.

That has the benefit that we are no longer affected by the CVE-2024-0406 and that we not longer depend on a third party dependency. Which removes a few packages from the main agent binary 🪄

Additional Notes

Possible Drawbacks / Trade-offs

Describe how to test/QA your changes

  • Create a flare
  • Get the flare ZIP file
  • Uncompress the ZIP
  • Validate the file structure is correct and all the files are present

…package pkg/util/archive

The libray includes the vulnerability CVE-2024-0406. Technically speaking we are not affected by this in production as we do not uncompress tar files. Still, it is a good opportunity to evaluate if we need a third party library for zip opertaions.

We decided to remove the third party dependency by vendoring some of its code.

That has the benefit that we are no longer affected by the CVE-2024-0406 and that we not longer depend on a third party dependency.
@GustavoCaso GustavoCaso force-pushed the replace-archive-package-to-create-zip-file branch from c714578 to 23014bf Compare May 10, 2024 08:40
@GustavoCaso GustavoCaso changed the title Replace github.com/mholt/archiver/v3 third party library with custom package pkg/util/archive Replace github.com/mholt/archiver/v3 third party library with custom package pkg/util/archive May 10, 2024
Copy link

cit-pr-commenter bot commented May 10, 2024

Go Package Import Differences

Baseline: 4faaee8
Comparison: 14c6666

binaryosarchchange
agentlinuxamd64
+1, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
agentlinuxarm64
+1, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
agentwindowsamd64
+1, -24
+github.com/DataDog/datadog-agent/pkg/util/archive
-github.com/andybalholm/brotli
-github.com/dsnet/compress
-github.com/dsnet/compress/bzip2
-github.com/dsnet/compress/bzip2/internal/sais
-github.com/dsnet/compress/internal
-github.com/dsnet/compress/internal/errors
-github.com/dsnet/compress/internal/prefix
-github.com/klauspost/compress/flate
-github.com/klauspost/compress/gzip
-github.com/klauspost/compress/zip
-github.com/klauspost/pgzip
-github.com/mholt/archiver/v3
-github.com/nwaples/rardecode
-github.com/pierrec/lz4/v4
-github.com/pierrec/lz4/v4/internal/lz4block
-github.com/pierrec/lz4/v4/internal/lz4errors
-github.com/pierrec/lz4/v4/internal/lz4stream
-github.com/pierrec/lz4/v4/internal/xxh32
-github.com/ulikunitz/xz
-github.com/ulikunitz/xz/internal/hash
-github.com/ulikunitz/xz/internal/xlog
-github.com/ulikunitz/xz/lzma
-github.com/xi2/xz
-hash/crc64
agentwindows386
+1, -24
+github.com/DataDog/datadog-agent/pkg/util/archive
-github.com/andybalholm/brotli
-github.com/dsnet/compress
-github.com/dsnet/compress/bzip2
-github.com/dsnet/compress/bzip2/internal/sais
-github.com/dsnet/compress/internal
-github.com/dsnet/compress/internal/errors
-github.com/dsnet/compress/internal/prefix
-github.com/klauspost/compress/flate
-github.com/klauspost/compress/gzip
-github.com/klauspost/compress/zip
-github.com/klauspost/pgzip
-github.com/mholt/archiver/v3
-github.com/nwaples/rardecode
-github.com/pierrec/lz4/v4
-github.com/pierrec/lz4/v4/internal/lz4block
-github.com/pierrec/lz4/v4/internal/lz4errors
-github.com/pierrec/lz4/v4/internal/lz4stream
-github.com/pierrec/lz4/v4/internal/xxh32
-github.com/ulikunitz/xz
-github.com/ulikunitz/xz/internal/hash
-github.com/ulikunitz/xz/internal/xlog
-github.com/ulikunitz/xz/lzma
-github.com/xi2/xz
-hash/crc64
agentdarwinamd64
+1, -25
-archive/tar
+github.com/DataDog/datadog-agent/pkg/util/archive
-github.com/andybalholm/brotli
-github.com/dsnet/compress
-github.com/dsnet/compress/bzip2
-github.com/dsnet/compress/bzip2/internal/sais
-github.com/dsnet/compress/internal
-github.com/dsnet/compress/internal/errors
-github.com/dsnet/compress/internal/prefix
-github.com/klauspost/compress/flate
-github.com/klauspost/compress/gzip
-github.com/klauspost/compress/zip
-github.com/klauspost/pgzip
-github.com/mholt/archiver/v3
-github.com/nwaples/rardecode
-github.com/pierrec/lz4/v4
-github.com/pierrec/lz4/v4/internal/lz4block
-github.com/pierrec/lz4/v4/internal/lz4errors
-github.com/pierrec/lz4/v4/internal/lz4stream
-github.com/pierrec/lz4/v4/internal/xxh32
-github.com/ulikunitz/xz
-github.com/ulikunitz/xz/internal/hash
-github.com/ulikunitz/xz/internal/xlog
-github.com/ulikunitz/xz/lzma
-github.com/xi2/xz
-hash/crc64
agentdarwinarm64
+1, -25
-archive/tar
+github.com/DataDog/datadog-agent/pkg/util/archive
-github.com/andybalholm/brotli
-github.com/dsnet/compress
-github.com/dsnet/compress/bzip2
-github.com/dsnet/compress/bzip2/internal/sais
-github.com/dsnet/compress/internal
-github.com/dsnet/compress/internal/errors
-github.com/dsnet/compress/internal/prefix
-github.com/klauspost/compress/flate
-github.com/klauspost/compress/gzip
-github.com/klauspost/compress/zip
-github.com/klauspost/pgzip
-github.com/mholt/archiver/v3
-github.com/nwaples/rardecode
-github.com/pierrec/lz4/v4
-github.com/pierrec/lz4/v4/internal/lz4block
-github.com/pierrec/lz4/v4/internal/lz4errors
-github.com/pierrec/lz4/v4/internal/lz4stream
-github.com/pierrec/lz4/v4/internal/xxh32
-github.com/ulikunitz/xz
-github.com/ulikunitz/xz/internal/hash
-github.com/ulikunitz/xz/internal/xlog
-github.com/ulikunitz/xz/lzma
-github.com/xi2/xz
-hash/crc64
iot-agentlinuxamd64
+1, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
iot-agentlinuxarm64
+1, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
heroku-agentlinuxamd64
+1, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
cluster-agentlinuxamd64
+2, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
+github.com/cyphar/filepath-securejoin
cluster-agentlinuxarm64
+2, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
+github.com/cyphar/filepath-securejoin
cluster-agent-cloudfoundrylinuxamd64
+2, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
+github.com/cyphar/filepath-securejoin
cluster-agent-cloudfoundrylinuxarm64
+2, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
+github.com/cyphar/filepath-securejoin
security-agentlinuxamd64
+2, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
+github.com/cyphar/filepath-securejoin
security-agentlinuxarm64
+2, -0
+github.com/DataDog/datadog-agent/pkg/util/archive
+github.com/cyphar/filepath-securejoin

@pr-commenter
Copy link

pr-commenter bot commented May 10, 2024

Test changes on VM

Use this command from test-infra-definitions to manually test this PR changes on a VM:

inv create-vm --pipeline-id=34338108 --os-family=ubuntu

@pr-commenter
Copy link

pr-commenter bot commented May 10, 2024

Regression Detector

Regression Detector Results

Run ID: f16f125c-1af9-43b8-bf26-7d1982d41983
Baseline: 1f10c8a
Comparison: 9f4f978

Performance changes are noted in the perf column of each table:

  • ✅ = significantly better comparison variant performance
  • ❌ = significantly worse comparison variant performance
  • ➖ = no significant change in performance

No significant changes in experiment optimization goals

Confidence level: 90.00%
Effect size tolerance: |Δ mean %| ≥ 5.00%

There were no significant changes in experiment optimization goals at this confidence level and effect size tolerance.

Experiments ignored for regressions

Regressions in experiments with settings containing erratic: true are ignored.

perf experiment goal Δ mean % Δ mean % CI
file_to_blackhole % cpu utilization -43.87 [-48.81, -38.92]

Fine details of change detection per experiment

perf experiment goal Δ mean % Δ mean % CI
basic_py_check % cpu utilization +1.61 [-0.91, +4.13]
pycheck_1000_100byte_tags % cpu utilization +1.14 [-3.63, +5.91]
idle memory utilization +0.10 [+0.07, +0.13]
uds_dogstatsd_to_api ingress throughput +0.00 [-0.20, +0.20]
trace_agent_msgpack ingress throughput -0.00 [-0.00, +0.00]
trace_agent_json ingress throughput -0.01 [-0.02, +0.01]
file_tree memory utilization -0.01 [-0.10, +0.08]
tcp_dd_logs_filter_exclude ingress throughput -0.01 [-0.05, +0.02]
otel_to_otel_logs ingress throughput -0.15 [-0.53, +0.23]
uds_dogstatsd_to_api_cpu % cpu utilization -0.56 [-3.39, +2.27]
tcp_syslog_to_blackhole ingress throughput -5.98 [-26.28, +14.31]
file_to_blackhole % cpu utilization -43.87 [-48.81, -38.92]

Explanation

A regression test is an A/B test of target performance in a repeatable rig, where "performance" is measured as "comparison variant minus baseline variant" for an optimization goal (e.g., ingress throughput). Due to intrinsic variability in measuring that goal, we can only estimate its mean value for each experiment; we report uncertainty in that value as a 90.00% confidence interval denoted "Δ mean % CI".

For each experiment, we decide whether a change in performance is a "regression" -- a change worth investigating further -- if all of the following criteria are true:

  1. Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.

  2. Its 90.00% confidence interval "Δ mean % CI" does not contain zero, indicating that if our statistical model is accurate, there is at least a 90.00% chance there is a difference in performance between baseline and comparison variants.

  3. Its configuration does not mark it "erratic".

Copy link
Contributor

@pgimalac pgimalac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, can we add (a lot of) tests though ?

pkg/util/archive/zip.go Outdated Show resolved Hide resolved
pkg/util/archive/zip.go Outdated Show resolved Hide resolved
@GustavoCaso GustavoCaso force-pushed the replace-archive-package-to-create-zip-file branch from dce7de9 to b0fd718 Compare May 10, 2024 14:08
Copy link
Contributor

@pgimalac pgimalac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to have more tests around error paths, but this is good enough to merge

assert.Error(t, err, "must have a .zip extension")
}

func TestZip_ExtistingDestuination(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func TestZip_ExtistingDestuination(t *testing.T) {
func TestZip_ExistingDestination(t *testing.T) {

Copy link
Contributor

@sgnn7 sgnn7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GustavoCaso Great work! Left a lot of comments but majority of them are naming non-blocking nits.

pkg/util/archive/zip.go Outdated Show resolved Hide resolved
pkg/util/archive/zip.go Outdated Show resolved Hide resolved
pkg/util/archive/zip.go Outdated Show resolved Hide resolved

zipW := zip.NewWriter(out)
zipW.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(out, flate.DefaultCompression)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any downside to using BestCompression?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous code was using DefaultCompression that is why I decided to leave as it was. https://github.com/mholt/archiver/blob/e0c66cf15ec46b2e04e0c280b0b8178d14aa55dc/zip.go#L651

I'm ok changing it to BestCompression.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. Feel free to choose if you want to change it or not


zipW := zip.NewWriter(out)
zipW.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
return flate.NewWriter(out, flate.DefaultCompression)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big question here - were the flares compressed using custom/optimized Deflate through a dependency? If so, we may be regressing in speed and file size in the output so it would be good to do at least a side-by-side size comparison. The changeover is likely acceptable but we should at least understand the impact.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a great point.

There are two side to that question:

Do we want to reduce the number of imports?
Yes. In that case use the archive/zip package from the std
No. In that case use github.com/klauspost/compress/zip

Are performance degradation an issue for creating flares?
Yes. Use github.com/klauspost/compress/zip
No. Use archive/zip package from the std

in any case having a comparison table would be useful to answer the questions above. I will work on a comparison table 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having a comparison table would be useful to answer the questions above
I think just a simple flare from both implementations should be fine and we can just check the file size.

For your questions:

  • Do we want to reduce the number of imports? Yes
  • Are performance degradation an issue for creating flares? No, as long as they're not drastic

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image main_branch -> main custom_branch|custom_version -> this branch

Both archive are the same size. Uncompressed custom branch is 0.1 MB smaller.

I think we good with the change 😄


func TestZip_DoNotZipSymlinks(t *testing.T) {
tmpDestinationDir := t.TempDir()
zipTempLocation := filepath.Join(tmpDestinationDir, "destination.zip")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker I feel like a lot of these tests have some common code that could be deduped

pkg/util/archive/zip_test.go Show resolved Hide resolved
pkg/util/archive/zip_test.go Outdated Show resolved Hide resolved
pkg/util/archive/zip_test.go Outdated Show resolved Hide resolved
{"bad/file.txt", "Mwa-ha-ha"},
}

if createFileOutsideRoot {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also try at least minimally testing absolute paths (e.g. /usr/bin/badfile.sh)

@GustavoCaso GustavoCaso force-pushed the replace-archive-package-to-create-zip-file branch from fa1547c to 9f4f978 Compare May 13, 2024 12:57
@GustavoCaso GustavoCaso changed the title Replace github.com/mholt/archiver/v3 third party library with custom package pkg/util/archive [ASCII-1615] Replace github.com/mholt/archiver/v3 third party library with custom package pkg/util/archive May 13, 2024
@GustavoCaso
Copy link
Member Author

/merge

@dd-devflow
Copy link

dd-devflow bot commented May 15, 2024

🚂 MergeQueue

This merge request is not mergeable yet, because of pending checks/missing approvals. It will be added to the queue as soon as checks pass and/or get approvals.
Note: if you pushed new commits since the last approval, you may need additional approval.
You can remove it from the waiting list with /remove command.

Use /merge -c to cancel this operation!

@dd-devflow
Copy link

dd-devflow bot commented May 15, 2024

🚂 MergeQueue

Pull request added to the queue.

There are 2 builds ahead! (estimated merge in less than 1h)

Use /merge -c to cancel this operation!

@dd-mergequeue dd-mergequeue bot merged commit 0ae8904 into main May 15, 2024
191 of 196 checks passed
@dd-mergequeue dd-mergequeue bot deleted the replace-archive-package-to-create-zip-file branch May 15, 2024 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants