-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent overwriting existing host_uuid file (#48012)
In some circumstances, multiple Teleport processes may be trying to write the host_uuid file in the same data directory simultaneously. The last of the writers would win, and any process using a host UUID that did not match what ended up on disk could get into a perpertual state of being unable to connect to the cluster. To avoid the raciness, the host_uuid file writing process is no longer a blind upsert. Instead, special care is taken to ensure that there can only be a single writer, and that any subsequent updates to the file are aborted and the first value written is used instead.
- Loading branch information
1 parent
3700c1c
commit 5132716
Showing
14 changed files
with
338 additions
and
141 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,61 @@ | ||
// Teleport | ||
// Copyright (C) 2024 Gravitational, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package hostid | ||
|
||
import ( | ||
"errors" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/gravitational/trace" | ||
|
||
"github.com/gravitational/teleport/lib/utils" | ||
) | ||
|
||
const ( | ||
// FileName is the file name where the host UUID file is stored | ||
FileName = "host_uuid" | ||
) | ||
|
||
// GetPath returns the path to the host UUID file given the data directory. | ||
func GetPath(dataDir string) string { | ||
return filepath.Join(dataDir, FileName) | ||
} | ||
|
||
// ExistsLocally checks if dataDir/host_uuid file exists in local storage. | ||
func ExistsLocally(dataDir string) bool { | ||
_, err := ReadFile(dataDir) | ||
return err == nil | ||
} | ||
|
||
// ReadFile reads host UUID from the file in the data dir | ||
func ReadFile(dataDir string) (string, error) { | ||
out, err := utils.ReadPath(GetPath(dataDir)) | ||
if err != nil { | ||
if errors.Is(err, fs.ErrPermission) { | ||
//do not convert to system error as this loses the ability to compare that it is a permission error | ||
return "", trace.Wrap(err) | ||
} | ||
return "", trace.ConvertSystemError(err) | ||
} | ||
id := strings.TrimSpace(string(out)) | ||
if id == "" { | ||
return "", trace.NotFound("host uuid is empty") | ||
} | ||
return id, nil | ||
} |
This file contains 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,116 @@ | ||
//go:build !windows | ||
|
||
// Teleport | ||
// Copyright (C) 2024 Gravitational, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package hostid_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/gravitational/teleport/lib/utils" | ||
"github.com/gravitational/teleport/lib/utils/hostid" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
utils.InitLoggerForTests() | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestReadOrCreate(t *testing.T) { | ||
t.Parallel() | ||
|
||
dir := t.TempDir() | ||
|
||
var wg errgroup.Group | ||
concurrency := 10 | ||
ids := make([]string, concurrency) | ||
barrier := make(chan struct{}) | ||
|
||
for i := 0; i < concurrency; i++ { | ||
i := i | ||
wg.Go(func() error { | ||
<-barrier | ||
id, err := hostid.ReadOrCreateFile(dir) | ||
ids[i] = id | ||
return err | ||
}) | ||
} | ||
|
||
close(barrier) | ||
|
||
require.NoError(t, wg.Wait()) | ||
for _, id := range ids { | ||
assert.Equal(t, ids[0], id) | ||
} | ||
} | ||
|
||
func TestIdempotence(t *testing.T) { | ||
t.Parallel() | ||
|
||
// call twice, get same result | ||
dir := t.TempDir() | ||
id, err := hostid.ReadOrCreateFile(dir) | ||
require.Len(t, id, 36) | ||
require.NoError(t, err) | ||
uuidCopy, err := hostid.ReadOrCreateFile(dir) | ||
require.NoError(t, err) | ||
require.Equal(t, id, uuidCopy) | ||
} | ||
|
||
func TestBadLocation(t *testing.T) { | ||
t.Parallel() | ||
|
||
// call with a read-only dir, make sure to get an error | ||
id, err := hostid.ReadOrCreateFile("/bad-location") | ||
require.Empty(t, id) | ||
require.Error(t, err) | ||
require.Regexp(t, "^.*no such file or directory.*$", err.Error()) | ||
} | ||
|
||
func TestIgnoreWhitespace(t *testing.T) { | ||
t.Parallel() | ||
|
||
// newlines are getting ignored | ||
dir := t.TempDir() | ||
id := fmt.Sprintf("%s\n", uuid.NewString()) | ||
err := os.WriteFile(filepath.Join(dir, hostid.FileName), []byte(id), 0666) | ||
require.NoError(t, err) | ||
out, err := hostid.ReadFile(dir) | ||
require.NoError(t, err) | ||
require.Equal(t, strings.TrimSpace(id), out) | ||
} | ||
|
||
func TestRegenerateEmpty(t *testing.T) { | ||
t.Parallel() | ||
|
||
// empty UUID in file is regenerated | ||
dir := t.TempDir() | ||
err := os.WriteFile(filepath.Join(dir, hostid.FileName), nil, 0666) | ||
require.NoError(t, err) | ||
out, err := hostid.ReadOrCreateFile(dir) | ||
require.NoError(t, err) | ||
require.Len(t, out, 36) | ||
} |
Oops, something went wrong.