-
Notifications
You must be signed in to change notification settings - Fork 176
Ensure that host file parsing doesn't drop entries when parsing. #1261
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
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e34cfe5
host file parsing fails without trailing newline
djeebus 5667197
clean up tests
djeebus 90cdc3c
linting
djeebus 3421f3d
take control of reading/writing so we can ensure the newline
djeebus 6776e01
Merge remote-tracking branch 'origin/main' into hosts
djeebus fdd95ae
linting
djeebus 9c77e33
clean up, thanks cursor!
djeebus 6d7e675
Merge branch 'main' into hosts
djeebus 339be1c
some optimizations
djeebus 25a71df
good catch, cursor!
djeebus fca216c
Merge branch 'main' into hosts
djeebus 9ab4357
don't return on failed hosts file rewrite
djeebus 86ed1d3
consolidate permissions
djeebus c8a08f1
better errors
djeebus 1bbf947
slots are 1-based, not 0-based
djeebus c589d1a
Merge remote-tracking branch 'origin/main' into hosts
djeebus de2f305
remove bug fix (moved to #1317)
djeebus ad59aa0
Merge remote-tracking branch 'origin/main' into hosts
djeebus 71d5c44
fix linting
djeebus aea3618
fix merge issue
djeebus 844735d
Merge branch 'main' into hosts
jakubno 162d46d
bump the envd version
djeebus d1333e2
Merge branch 'main' into hosts
jakubno 183d6f3
bump envd version for real
djeebus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
"fmt" | ||
"io" | ||
"net/http" | ||
"net/netip" | ||
"os" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
|
@@ -15,8 +17,11 @@ import ( | |
|
||
"github.com/e2b-dev/infra/packages/envd/internal/host" | ||
"github.com/e2b-dev/infra/packages/envd/internal/logs" | ||
"github.com/e2b-dev/infra/packages/shared/pkg/utils" | ||
) | ||
|
||
const hostsFilePermissions = 0o644 | ||
|
||
var ErrAccessTokenAlreadySet = errors.New("access token is already set") | ||
|
||
const ( | ||
|
@@ -119,19 +124,68 @@ func (a *API) SetupHyperloop(address string) { | |
a.hyperloopLock.Lock() | ||
defer a.hyperloopLock.Unlock() | ||
|
||
hosts, err := txeh.NewHosts(&txeh.HostsConfig{ReadFilePath: "/etc/hosts", WriteFilePath: "/etc/hosts"}) | ||
if err := rewriteHostsFile(address, "/etc/hosts"); err != nil { | ||
a.logger.Error().Err(err).Msg("failed to modify hosts file") | ||
} else { | ||
a.envVars.Store("E2B_EVENTS_ADDRESS", fmt.Sprintf("http://%s", address)) | ||
} | ||
} | ||
|
||
const eventsHost = "events.e2b.local" | ||
|
||
func rewriteHostsFile(address, path string) error { | ||
data, err := os.ReadFile(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to read hosts file: %w", err) | ||
} | ||
|
||
// the txeh library drops an entry if the file does not end with a newline | ||
if len(data) > 0 && data[len(data)-1] != '\n' { | ||
data = append(data, '\n') | ||
} | ||
|
||
hosts, err := txeh.NewHosts(&txeh.HostsConfig{RawText: utils.ToPtr(string(data))}) | ||
if err != nil { | ||
a.logger.Error().Msgf("Failed to create hosts: %v", err) | ||
return | ||
return fmt.Errorf("failed to create hosts: %w", err) | ||
} | ||
|
||
// Update /etc/hosts to point events.e2b.local to the hyperloop IP | ||
// This will remove any existing entries for events.e2b.local first | ||
hosts.AddHost(address, "events.e2b.local") | ||
err = hosts.Save() | ||
ipFamily, err := getIPFamily(address) | ||
if err != nil { | ||
return fmt.Errorf("failed to get ip family: %w", err) | ||
} | ||
|
||
if ok, current, _ := hosts.HostAddressLookup(eventsHost, ipFamily); ok && current == address { | ||
return nil // nothing to be done | ||
} | ||
|
||
hosts.AddHost(address, eventsHost) | ||
|
||
if err = os.WriteFile(path, []byte(hosts.RenderHostsFile()), hostsFilePermissions); err != nil { | ||
return fmt.Errorf("failed to save hosts file: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
var ( | ||
ErrInvalidAddress = errors.New("invalid IP address") | ||
ErrUnknownAddressFormat = errors.New("unknown IP address format") | ||
) | ||
|
||
func getIPFamily(address string) (txeh.IPFamily, error) { | ||
addressIP, err := netip.ParseAddr(address) | ||
if err != nil { | ||
a.logger.Error().Msgf("Failed to add events host entry: %v", err) | ||
return txeh.IPFamilyV4, fmt.Errorf("failed to parse IP address: %w", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
a.envVars.Store("E2B_EVENTS_ADDRESS", fmt.Sprintf("http://%s", address)) | ||
switch { | ||
case addressIP.Is4(): | ||
return txeh.IPFamilyV4, nil | ||
case addressIP.Is6(): | ||
return txeh.IPFamilyV6, nil | ||
default: | ||
return txeh.IPFamilyV4, fmt.Errorf("%w: %s", ErrUnknownAddressFormat, address) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package api | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSimpleCases(t *testing.T) { | ||
testCases := map[string]func(string) string{ | ||
"both newlines": func(s string) string { return s }, | ||
"no newline prefix": func(s string) string { return strings.TrimPrefix(s, "\n") }, | ||
"no newline suffix": func(s string) string { return strings.TrimSuffix(s, "\n") }, | ||
"no newline prefix or suffix": strings.TrimSpace, | ||
} | ||
|
||
for name, preprocessor := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
tempDir := t.TempDir() | ||
|
||
value := ` | ||
# comment | ||
127.0.0.1 one.host | ||
127.0.0.2 two.host | ||
` | ||
value = preprocessor(value) | ||
inputPath := filepath.Join(tempDir, "hosts") | ||
err := os.WriteFile(inputPath, []byte(value), hostsFilePermissions) | ||
require.NoError(t, err) | ||
|
||
err = rewriteHostsFile("127.0.0.3", inputPath) | ||
require.NoError(t, err) | ||
|
||
data, err := os.ReadFile(inputPath) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, `# comment | ||
127.0.0.1 one.host | ||
127.0.0.2 two.host | ||
127.0.0.3 events.e2b.local`, strings.TrimSpace(string(data))) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ const ( | |
) | ||
|
||
var ( | ||
Version = "0.3.5" | ||
Version = "0.3.6" | ||
|
||
commitSHA string | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.