-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use dynamic identity file names by default
Dynamically generated names supports up to four hex digits at the end to detect available file names. The range scan starts from VM SSH port to reduce collision probability. It is still possible to override dynamically generated file names using command line option during machine init. Signed-off-by: Arthur Sengileyev <[email protected]>
- Loading branch information
Showing
8 changed files
with
78 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package utils | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func FindTargetIdentityPath(sshDir string, srcIdentity string, prefix string, rangeStart int) (string, error) { | ||
identityFilepath := strings.TrimSpace(srcIdentity) | ||
if len(identityFilepath) == 0 { | ||
return FindAvailableIdentityPath(sshDir, prefix, rangeStart) | ||
} else if filepath.IsAbs(identityFilepath) { | ||
return identityFilepath, nil | ||
} | ||
return filepath.Join(sshDir, identityFilepath), nil | ||
} | ||
|
||
func FindAvailableIdentityPath(sshDir string, prefix string, rangeStart int) (string, error) { | ||
for i := 0; i < 65536; i++ { | ||
nextFilepath := filepath.Join(sshDir, fmt.Sprintf("%s-%04x", prefix, (rangeStart+i)%65536)) | ||
_, err1 := os.Stat(nextFilepath) | ||
_, err2 := os.Stat(nextFilepath + ".pub") | ||
if errors.Is(err1, fs.ErrNotExist) && errors.Is(err2, fs.ErrNotExist) { | ||
return nextFilepath, nil | ||
} | ||
} | ||
return "", errors.New("can't determine available identity filename") | ||
} |