-
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.
move host id helpers into their own package
- Loading branch information
1 parent
a78936d
commit 6328758
Showing
16 changed files
with
282 additions
and
216 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
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,144 @@ | ||
// 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" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/google/renameio/v2" | ||
"github.com/google/uuid" | ||
"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 "", err | ||
} | ||
return "", trace.ConvertSystemError(err) | ||
} | ||
id := strings.TrimSpace(string(out)) | ||
if id == "" { | ||
return "", trace.NotFound("host uuid is empty") | ||
} | ||
return id, nil | ||
} | ||
|
||
// WriteFile writes host UUID into a file | ||
func WriteFile(dataDir string, id string) error { | ||
err := os.WriteFile(GetPath(dataDir), []byte(id), os.ModeExclusive|0400) | ||
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 err | ||
} | ||
return trace.ConvertSystemError(err) | ||
} | ||
return nil | ||
} | ||
|
||
// ReadOrCreateFile looks for a hostid file in the data dir. If present, | ||
// returns the UUID from it, otherwise generates one | ||
func ReadOrCreateFile(dataDir string) (string, error) { | ||
hostUUIDFileName := GetPath(dataDir) | ||
hostUUIDFileLock := hostUUIDFileName + ".lock" | ||
iterationLimit := 3 | ||
|
||
for i := 0; i < iterationLimit; i++ { | ||
id, err := ReadFile(dataDir) | ||
if err == nil { | ||
return id, nil | ||
} | ||
if !trace.IsNotFound(err) { | ||
return "", trace.Wrap(err) | ||
} | ||
|
||
// Checking error instead of the usual uuid.New() in case uuid generation | ||
// fails due to not enough randomness. It's been known to happen happen when | ||
// Teleport starts very early in the node initialization cycle and /dev/urandom | ||
// isn't ready yet. | ||
rawID, err := uuid.NewRandom() | ||
if err != nil { | ||
return "", trace.BadParameter("" + | ||
"Teleport failed to generate host UUID. " + | ||
"This may happen if randomness source is not fully initialized when the node is starting up. " + | ||
"Please try restarting Teleport again.") | ||
} | ||
|
||
id = rawID.String() | ||
|
||
writeFile := func() (string, error) { | ||
unlock, err := utils.FSTryWriteLock(hostUUIDFileLock) | ||
if err != nil { | ||
return "", trace.Wrap(err) | ||
} | ||
defer unlock() | ||
|
||
if read, err := ReadFile(dataDir); err == nil { | ||
return read, nil | ||
} else if !trace.IsNotFound(err) { | ||
return "", trace.Wrap(err) | ||
} | ||
|
||
if err := renameio.WriteFile(hostUUIDFileName, []byte(id), 0o400); err != nil { | ||
return "", trace.Wrap(err) | ||
} | ||
|
||
return id, nil | ||
} | ||
|
||
id, err = writeFile() | ||
if err != nil { | ||
if errors.Is(err, fs.ErrPermission) || errors.Is(err, utils.ErrUnsuccessfulLockTry) { | ||
time.Sleep(10 * time.Millisecond) | ||
continue | ||
} | ||
|
||
return "", trace.Wrap(err) | ||
} | ||
|
||
return id, nil | ||
} | ||
|
||
return "", trace.LimitExceeded("failed to obtain host uuid") | ||
} |
Oops, something went wrong.