mirror of
https://github.com/kairos-io/provider-kairos.git
synced 2025-09-16 15:19:46 +00:00
Check that QR file paths are readable and are not directories (#141)
This is a possible fix for https://github.com/kairos-io/kairos/issues/568. Signed-off-by: Oz Tiram <oz.tiram@gmail.com>
This commit is contained in:
@@ -4,16 +4,50 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
nodepair "github.com/mudler/go-nodepair"
|
||||
qr "github.com/mudler/go-nodepair/qrcode"
|
||||
)
|
||||
|
||||
// isDirectory determines if a file represented
|
||||
// by `path` is a directory or not.
|
||||
func isDirectory(path string) (bool, error) {
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return fileInfo.IsDir(), err
|
||||
}
|
||||
|
||||
func isReadable(fileName string) bool {
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
if os.IsPermission(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
file.Close()
|
||||
return true
|
||||
}
|
||||
|
||||
func register(loglevel, arg, configFile, device string, reboot, poweroff bool) error {
|
||||
b, _ := ioutil.ReadFile(configFile)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
if arg != "" {
|
||||
isDir, err := isDirectory(arg)
|
||||
if err == nil && isDir {
|
||||
return fmt.Errorf("Cannot register with a directory, please pass a file.") //nolint:revive // This is a message printed to the user.
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if !isReadable(arg) {
|
||||
return fmt.Errorf("Cannot register with a file that is not readable.") //nolint:revive // This is a message printed to the user.
|
||||
}
|
||||
}
|
||||
// dmesg -D to suppress tty ev
|
||||
fmt.Println("Sending registration payload, please wait")
|
||||
|
||||
|
Reference in New Issue
Block a user