azure: Don't exec 'cat' to read files.

This commit is contained in:
Jan Safranek 2017-08-22 10:01:33 +02:00
parent 07dea6b447
commit 9484d243ab
2 changed files with 35 additions and 8 deletions

View File

@ -21,7 +21,6 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
"regexp"
"strconv" "strconv"
libstrings "strings" libstrings "strings"
@ -158,6 +157,7 @@ type ioHandler interface {
ReadDir(dirname string) ([]os.FileInfo, error) ReadDir(dirname string) ([]os.FileInfo, error)
WriteFile(filename string, data []byte, perm os.FileMode) error WriteFile(filename string, data []byte, perm os.FileMode) error
Readlink(name string) (string, error) Readlink(name string) (string, error)
ReadFile(filename string) ([]byte, error)
} }
//TODO: check if priming the iscsi interface is actually needed //TODO: check if priming the iscsi interface is actually needed
@ -176,6 +176,10 @@ func (handler *osIOHandler) Readlink(name string) (string, error) {
return os.Readlink(name) return os.Readlink(name)
} }
func (handler *osIOHandler) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
}
// exclude those used by azure as resource and OS root in /dev/disk/azure // exclude those used by azure as resource and OS root in /dev/disk/azure
func listAzureDiskPath(io ioHandler) []string { func listAzureDiskPath(io ioHandler) []string {
azureDiskPath := "/dev/disk/azure/" azureDiskPath := "/dev/disk/azure/"
@ -237,18 +241,30 @@ func findDiskByLunWithConstraint(lun int, io ioHandler, exe exec.Interface, azur
if lun == l { if lun == l {
// find the matching LUN // find the matching LUN
// read vendor and model to ensure it is a VHD disk // read vendor and model to ensure it is a VHD disk
vendor := path.Join(sys_path, name, "vendor") vendorPath := path.Join(sys_path, name, "vendor")
model := path.Join(sys_path, name, "model") vendorBytes, err := io.ReadFile(vendorPath)
out, err := exe.Command("cat", vendor, model).CombinedOutput()
if err != nil { if err != nil {
glog.V(4).Infof("azure disk - failed to cat device vendor and model, err: %v", err) glog.Errorf("failed to read device vendor, err: %v", err)
continue continue
} }
matched, err := regexp.MatchString("^MSFT[ ]{0,}\nVIRTUAL DISK[ ]{0,}\n$", libstrings.ToUpper(string(out))) vendor := libstrings.TrimSpace(string(vendorBytes))
if err != nil || !matched { if libstrings.ToUpper(vendor) != "MSFT" {
glog.V(4).Infof("azure disk - doesn't match VHD, output %v, error %v", string(out), err) glog.V(4).Infof("vendor doesn't match VHD, got %s", vendor)
continue continue
} }
modelPath := path.Join(sys_path, name, "model")
modelBytes, err := io.ReadFile(modelPath)
if err != nil {
glog.Errorf("failed to read device model, err: %v", err)
continue
}
model := libstrings.TrimSpace(string(modelBytes))
if libstrings.ToUpper(model) != "VIRTUAL DISK" {
glog.V(4).Infof("model doesn't match VHD, got %s", model)
continue
}
// find a disk, validate name // find a disk, validate name
dir := path.Join(sys_path, name, "block") dir := path.Join(sys_path, name, "block")
if dev, err := io.ReadDir(dir); err == nil { if dev, err := io.ReadDir(dir); err == nil {

View File

@ -19,6 +19,7 @@ package azure_dd
import ( import (
"fmt" "fmt"
"os" "os"
"strings"
"testing" "testing"
"time" "time"
@ -107,6 +108,16 @@ func (handler *fakeIOHandler) Readlink(name string) (string, error) {
return "/dev/azure/disk/sda", nil return "/dev/azure/disk/sda", nil
} }
func (handler *fakeIOHandler) ReadFile(filename string) ([]byte, error) {
if strings.HasSuffix(filename, "vendor") {
return []byte("Msft \n"), nil
}
if strings.HasSuffix(filename, "model") {
return []byte("Virtual Disk \n"), nil
}
return nil, fmt.Errorf("unknown file")
}
func TestIoHandler(t *testing.T) { func TestIoHandler(t *testing.T) {
var fcmd fakeexec.FakeCmd var fcmd fakeexec.FakeCmd
fcmd = fakeexec.FakeCmd{ fcmd = fakeexec.FakeCmd{