mirror of
https://github.com/kairos-io/provider-kairos.git
synced 2025-09-17 07:41:05 +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"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
nodepair "github.com/mudler/go-nodepair"
|
nodepair "github.com/mudler/go-nodepair"
|
||||||
qr "github.com/mudler/go-nodepair/qrcode"
|
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 {
|
func register(loglevel, arg, configFile, device string, reboot, poweroff bool) error {
|
||||||
b, _ := ioutil.ReadFile(configFile)
|
b, _ := ioutil.ReadFile(configFile)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
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
|
// dmesg -D to suppress tty ev
|
||||||
fmt.Println("Sending registration payload, please wait")
|
fmt.Println("Sending registration payload, please wait")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user