Skip to content

suggestions for "Improve /etc/hosts generation"#1

Closed
thaJeztah wants to merge 7 commits intorobmry:simplify_etchostsfrom
thaJeztah:simplify_etchosts_suggestions
Closed

suggestions for "Improve /etc/hosts generation"#1
thaJeztah wants to merge 7 commits intorobmry:simplify_etchostsfrom
thaJeztah:simplify_etchosts_suggestions

Conversation

@thaJeztah
Copy link

libnetwork/etchosts: Add: combine with "mergeRecords()"

The mergeRecords function wasn't actually merging anything, but only
appended records to the existing /etc/hosts content. However, doing so
was split across two functions; Add and mergeRecords();

  • Add() obtains a lock for the given path
  • then calls mergeRecords which reads the file-content and appends the
    new records to the content.
  • Closes the file and returns the new content
  • Then Add does a os.WriteFile to ... the same file

Given that we're appending, we won't have to read the file's content, and
we can append to the file itself.

libnetwork/etchosts: Delete: truncate file instead of close and write

We already have the filehandle open, so we could just truncate, and
overwrite the content.

libnetwork: Sandbox.buildHostsFile: rename var that shadowed type

libnetwork: Sandbox.buildHostsFile: remove intermediate var

Call the respective (etchosts.BuildNoIPv6 or etchosts.Build) functions
directly instead of using the intermediate buildf variable.

robmry and others added 7 commits November 6, 2024 09:34
This regex is constructed using user-input, which could technically
produce an invalid regex.

Given that we have an error-return to our availability, let's return
any error we get, instead of panicking.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
The `mergeRecords` function wasn't actually _merging_ anything, but only
appended records to the existing `/etc/hosts` content. However, doing so
was split across two functions; `Add` and `mergeRecords()`;

- `Add()` obtains a lock for the given path
- then calls `mergeRecords` which reads the file-content and appends the
  new records to the content.
- Closes the file and returns the new content
- Then `Add` does a `os.WriteFile` to ... the same file

Given that we're appending, we won't have to read the file's content, and
we can append to the file itself.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
We already have the filehandle open, so we could just truncate, and
overwrite the content.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Call the respective (`etchosts.BuildNoIPv6` or `etchosts.Build`) functions
directly instead of using the intermediate `buildf` variable.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@robmry robmry force-pushed the simplify_etchosts branch 2 times, most recently from 5e5cd8c to 7c1e41a Compare November 7, 2024 11:09
@robmry
Copy link
Owner

robmry commented Nov 7, 2024

Merged to master in moby#48823

@robmry robmry closed this Nov 7, 2024
@thaJeztah thaJeztah deleted the simplify_etchosts_suggestions branch November 7, 2024 15:57
robmry pushed a commit that referenced this pull request Jan 2, 2025
contains a fix for CVE-2024-45338 / https://go.dev/issue/70906,
but it doesn't affect our codebase:

    govulncheck -show=verbose ./...
    Scanning your code and 1260 packages across 211 dependent modules for known vulnerabilities...
    ...
    Vulnerability #1: GO-2024-3333
        Non-linear parsing of case-insensitive content in golang.org/x/net/html
      More info: https://pkg.go.dev/vuln/GO-2024-3333
      Module: golang.org/x/net
        Found in: golang.org/x/[email protected]
        Fixed in: golang.org/x/[email protected]

    Your code is affected by 0 vulnerabilities.
    This scan also found 0 vulnerabilities in packages you import and 1
    vulnerability in modules you require, but your code doesn't appear to call these
    vulnerabilities.

full diff: golang/net@v0.32.0...v0.33.0

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants