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"
"os"
"path"
"regexp"
"strconv"
libstrings "strings"
@ -158,6 +157,7 @@ type ioHandler interface {
ReadDir(dirname string) ([]os.FileInfo, error)
WriteFile(filename string, data []byte, perm os.FileMode) error
Readlink(name string) (string, error)
ReadFile(filename string) ([]byte, error)
}
//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)
}
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
func listAzureDiskPath(io ioHandler) []string {
azureDiskPath := "/dev/disk/azure/"
@ -237,18 +241,30 @@ func findDiskByLunWithConstraint(lun int, io ioHandler, exe exec.Interface, azur
if lun == l {
// find the matching LUN
// read vendor and model to ensure it is a VHD disk
vendor := path.Join(sys_path, name, "vendor")
model := path.Join(sys_path, name, "model")
out, err := exe.Command("cat", vendor, model).CombinedOutput()
vendorPath := path.Join(sys_path, name, "vendor")
vendorBytes, err := io.ReadFile(vendorPath)
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
}
matched, err := regexp.MatchString("^MSFT[ ]{0,}\nVIRTUAL DISK[ ]{0,}\n$", libstrings.ToUpper(string(out)))
if err != nil || !matched {
glog.V(4).Infof("azure disk - doesn't match VHD, output %v, error %v", string(out), err)
vendor := libstrings.TrimSpace(string(vendorBytes))
if libstrings.ToUpper(vendor) != "MSFT" {
glog.V(4).Infof("vendor doesn't match VHD, got %s", vendor)
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
dir := path.Join(sys_path, name, "block")
if dev, err := io.ReadDir(dir); err == nil {

View File

@ -19,6 +19,7 @@ package azure_dd
import (
"fmt"
"os"
"strings"
"testing"
"time"
@ -107,6 +108,16 @@ func (handler *fakeIOHandler) Readlink(name string) (string, error) {
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) {
var fcmd fakeexec.FakeCmd
fcmd = fakeexec.FakeCmd{