Skip to content
This repository has been archived by the owner on Jul 6, 2023. It is now read-only.

Commit

Permalink
api: input validation
Browse files Browse the repository at this point in the history
It is necessary to validate the inputs before storing/using them
further in the code. This patch adds validation functions to all the
request structures defined in pkg/glusterfs/api/types.go . These
validation functions are then provided to a validation package that can
be called on a message buffer containing unmarshalled structure from
JSON input.

Signed-off-by: Raghavendra Talur <rtalur@redhat.com>
Reviewed-by: John Mulligan <jmulligan@redhat.com>
Reviewed-by: Michael Adam <obnox@redhat.com>
  • Loading branch information
raghavendra-talur authored and obnoxxx committed Dec 18, 2017
1 parent 9cccea2 commit 787bae4
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 4 deletions.
13 changes: 13 additions & 0 deletions apps/glusterfs/app_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func (a *App) DeviceAdd(w http.ResponseWriter, r *http.Request) {
return
}

err = msg.Validate()
if err != nil {
http.Error(w, "validation failed: "+err.Error(), http.StatusBadRequest)
logger.LogError("validation failed: " + err.Error())
return
}

// Check the message has devices
if msg.Name == "" {
http.Error(w, "no devices added", http.StatusBadRequest)
Expand Down Expand Up @@ -323,6 +330,12 @@ func (a *App) DeviceSetState(w http.ResponseWriter, r *http.Request) {
http.Error(w, "request unable to be parsed", 422)
return
}
err = msg.Validate()
if err != nil {
http.Error(w, "validation failed: "+err.Error(), http.StatusBadRequest)
logger.LogError("validation failed: " + err.Error())
return
}

// Check for valid id, return immediately if not valid
err = a.db.View(func(tx *bolt.Tx) error {
Expand Down
13 changes: 13 additions & 0 deletions apps/glusterfs/app_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func (a *App) NodeAdd(w http.ResponseWriter, r *http.Request) {
return
}

err = msg.Validate()
if err != nil {
http.Error(w, "validation failed: "+err.Error(), http.StatusBadRequest)
logger.LogError("validation failed: " + err.Error())
return
}

// Check information in JSON request
if len(msg.Hostnames.Manage) == 0 {
http.Error(w, "Manage hostname missing", http.StatusBadRequest)
Expand Down Expand Up @@ -330,6 +337,12 @@ func (a *App) NodeSetState(w http.ResponseWriter, r *http.Request) {
http.Error(w, "request unable to be parsed", 422)
return
}
err = msg.Validate()
if err != nil {
http.Error(w, "validation failed: "+err.Error(), http.StatusBadRequest)
logger.LogError("validation failed: " + err.Error())
return
}

// Check state is supported
err = a.db.View(func(tx *bolt.Tx) error {
Expand Down
12 changes: 12 additions & 0 deletions apps/glusterfs/app_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func (a *App) VolumeCreate(w http.ResponseWriter, r *http.Request) {
http.Error(w, "request unable to be parsed", 422)
return
}
err = msg.Validate()
if err != nil {
http.Error(w, "validation failed: "+err.Error(), http.StatusBadRequest)
logger.LogError("validation failed: " + err.Error())
return
}

switch {
case msg.Gid < 0:
Expand Down Expand Up @@ -287,6 +293,12 @@ func (a *App) VolumeExpand(w http.ResponseWriter, r *http.Request) {
return
}
logger.Debug("Msg: %v", msg)
err = msg.Validate()
if err != nil {
http.Error(w, "validation failed: "+err.Error(), http.StatusBadRequest)
logger.LogError("validation failed: " + err.Error())
return
}

if msg.Size < 1 {
http.Error(w, "Invalid volume size", http.StatusBadRequest)
Expand Down
4 changes: 2 additions & 2 deletions executors/sshexec/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *SshExecutor) DeviceSetup(host, device, vgid string) (d *executors.Devic

// Setup commands
commands := []string{
fmt.Sprintf("pvcreate --metadatasize=128M --dataalignment=256K %v", device),
fmt.Sprintf("pvcreate --metadatasize=128M --dataalignment=256K '%v'", device),
fmt.Sprintf("vgcreate %v %v", s.vgName(vgid), device),
}

Expand Down Expand Up @@ -65,7 +65,7 @@ func (s *SshExecutor) DeviceTeardown(host, device, vgid string) error {
// Setup commands
commands := []string{
fmt.Sprintf("vgremove %v", s.vgName(vgid)),
fmt.Sprintf("pvremove %v", device),
fmt.Sprintf("pvremove '%v'", device),
}

// Execute command
Expand Down
10 changes: 8 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ import:
- ssh/agent
- package: k8s.io/client-go
version: v3.0.0-beta.0
- package: github.com/go-ozzo/ozzo-validation
version: v3.3
121 changes: 121 additions & 0 deletions pkg/glusterfs/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,36 @@ package api

import (
"fmt"
"regexp"
"sort"

"github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)

var (
// Restricting the deviceName to much smaller subset of Unix Path
// as unix path takes almost everything except NULL
deviceNameRe = regexp.MustCompile("^/[a-zA-Z0-9_./-]+$")

// Volume name constraints decided by looking at
// "cli_validate_volname" function in cli-cmd-parser.c of gluster code
volumeNameRe = regexp.MustCompile("^[a-zA-Z0-9_-]+$")

blockVolNameRe = regexp.MustCompile("^[a-zA-Z0-9_-]+$")
)

// ValidateUUID is written this way because heketi UUID does not
// conform to neither UUID v4 nor v5.
func ValidateUUID(value interface{}) error {
s, _ := value.(string)
err := validation.Validate(s, validation.RuneLength(32, 32), is.Hexadecimal)
if err != nil {
return fmt.Errorf("not a valid UUID")
}
return nil
}

// State
type EntryState string

Expand All @@ -31,6 +58,15 @@ const (
EntryStateFailed EntryState = "failed"
)

func ValidateEntryState(value interface{}) error {
s, _ := value.(string)
err := validation.Validate(s, validation.Required, validation.In(EntryStateOnline, EntryStateOffline, EntryStateFailed))
if err != nil {
return fmt.Errorf("state requested is not valid")
}
return nil
}

type DurabilityType string

const (
Expand All @@ -39,11 +75,26 @@ const (
DurabilityEC DurabilityType = "disperse"
)

func ValidateDurabilityType(value interface{}) error {
s, _ := value.(string)
err := validation.Validate(s, validation.Required, validation.In(DurabilityReplicate, DurabilityDistributeOnly, DurabilityEC))
if err != nil {
return fmt.Errorf("durability type requested is not valid")
}
return nil
}

// Common
type StateRequest struct {
State EntryState `json:"state"`
}

func (statereq StateRequest) Validate() error {
return validation.ValidateStruct(&statereq,
validation.Field(&statereq.State, validation.Required, validation.By(ValidateEntryState)),
)
}

// Storage values in KB
type StorageSize struct {
Total uint64 `json:"total"`
Expand All @@ -56,6 +107,35 @@ type HostAddresses struct {
Storage sort.StringSlice `json:"storage"`
}

func ValidateManagementHostname(value interface{}) error {
s, _ := value.(sort.StringSlice)
for _, fqdn := range s {
err := validation.Validate(fqdn, validation.Required, is.Host)
if err != nil {
return fmt.Errorf("Manage hostname should be valid hostname")
}
}
return nil
}

func ValidateStorageHostname(value interface{}) error {
s, _ := value.(sort.StringSlice)
for _, ip := range s {
err := validation.Validate(ip, validation.Required, is.Host)
if err != nil {
return fmt.Errorf("Storage hostname should be valid IP")
}
}
return nil
}

func (hostadd HostAddresses) Validate() error {
return validation.ValidateStruct(&hostadd,
validation.Field(&hostadd.Manage, validation.Required, validation.By(ValidateManagementHostname)),
validation.Field(&hostadd.Storage, validation.Required, validation.By(ValidateStorageHostname)),
)
}

// Brick
type BrickInfo struct {
Id string `json:"id"`
Expand All @@ -73,11 +153,24 @@ type Device struct {
Name string `json:"name"`
}

func (dev Device) Validate() error {
return validation.ValidateStruct(&dev,
validation.Field(&dev.Name, validation.Required, validation.Match(deviceNameRe)),
)
}

type DeviceAddRequest struct {
Device
NodeId string `json:"node"`
}

func (devAddReq DeviceAddRequest) Validate() error {
return validation.ValidateStruct(&devAddReq,
validation.Field(&devAddReq.Device, validation.Required),
validation.Field(&devAddReq.NodeId, validation.Required, validation.By(ValidateUUID)),
)
}

type DeviceInfo struct {
Device
Storage StorageSize `json:"storage"`
Expand All @@ -97,6 +190,14 @@ type NodeAddRequest struct {
ClusterId string `json:"cluster"`
}

func (req NodeAddRequest) Validate() error {
return validation.ValidateStruct(&req,
validation.Field(&req.Zone, validation.Required, validation.Min(1)),
validation.Field(&req.Hostnames, validation.Required),
validation.Field(&req.ClusterId, validation.Required, validation.By(ValidateUUID)),
)
}

type NodeInfo struct {
NodeAddRequest
Id string `json:"id"`
Expand Down Expand Up @@ -160,6 +261,20 @@ type VolumeCreateRequest struct {
} `json:"snapshot"`
}

func (volCreateRequest VolumeCreateRequest) Validate() error {
return validation.ValidateStruct(&volCreateRequest,
validation.Field(&volCreateRequest.Size, validation.Required, validation.Min(1)),
validation.Field(&volCreateRequest.Clusters, validation.By(ValidateUUID)),
validation.Field(&volCreateRequest.Name, validation.Match(volumeNameRe)),
validation.Field(&volCreateRequest.Durability, validation.Skip),
validation.Field(&volCreateRequest.Gid, validation.Skip),
validation.Field(&volCreateRequest.GlusterVolumeOptions, validation.Skip),
// This is possibly a bug in validation lib, ignore next two lines for now
// validation.Field(&volCreateRequest.Snapshot.Enable, validation.In(true, false)),
// validation.Field(&volCreateRequest.Snapshot.Factor, validation.Min(1.0)),
)
}

type VolumeInfo struct {
VolumeCreateRequest
Id string `json:"id"`
Expand All @@ -186,6 +301,12 @@ type VolumeExpandRequest struct {
Size int `json:"expand_size"`
}

func (volExpandReq VolumeExpandRequest) Validate() error {
return validation.ValidateStruct(&volExpandReq,
validation.Field(&volExpandReq.Size, validation.Required, validation.Min(1)),
)
}

// Constructors

func NewVolumeInfoResponse() *VolumeInfoResponse {
Expand Down

0 comments on commit 787bae4

Please sign in to comment.