Skip to content

Commit

Permalink
(feat): Reduce complexity of permissions declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Dec 22, 2023
1 parent a4dc296 commit 9636f7e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 61 deletions.
1 change: 0 additions & 1 deletion cmd/ebs-bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func main() {
validators := []config.Validator{
config.NewFileSystemValidator(),
config.NewModeValidator(),
config.NewPermissionsValidator(),
config.NewResizeThresholdValidator(),
config.NewDeviceValidator(lds),
config.NewMountPointValidator(),
Expand Down
6 changes: 3 additions & 3 deletions internal/action/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ func (a *ChangePermissionsAction) SetMode(mode config.Mode) Action {
}

func (a *ChangePermissionsAction) Prompt() string {
return fmt.Sprintf("Would you like to change permissions of %s to %s", a.path, a.perms)
return fmt.Sprintf("Would you like to change permissions of %s to %#o", a.path, a.perms)
}

func (a *ChangePermissionsAction) Refuse() string {
return fmt.Sprintf("Refused to to change permissions of %s to %s", a.path, a.perms)
return fmt.Sprintf("Refused to to change permissions of %s to %#o", a.path, a.perms)
}

func (a *ChangePermissionsAction) Success() string {
return fmt.Sprintf("Successfully change permissions of %s to %s", a.path, a.perms)
return fmt.Sprintf("Successfully change permissions of %s to %#o", a.path, a.perms)
}
22 changes: 3 additions & 19 deletions internal/config/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,6 @@ func (apv *MountPointValidator) Validate(c *Config) error {
return nil
}

type PermissionsValidator struct{}

func NewPermissionsValidator() *PermissionsValidator {
return &PermissionsValidator{}
}

func (apv *PermissionsValidator) Validate(c *Config) error {
for _, device := range c.Devices {
_, err := device.Permissions.ToFileMode()
if err != nil {
return err
}
}
return nil
}

type OwnerValidator struct {
ownerService service.OwnerService
}
Expand Down Expand Up @@ -153,14 +137,14 @@ func NewResizeThresholdValidator() *ResizeThresholdValidator {

func (rtv *ResizeThresholdValidator) Validate(c *Config) error {
if c.Defaults.ResizeThreshold < 0 || c.Defaults.ResizeThreshold > 100 {
return fmt.Errorf("🔴 %g must be a floating point between 0 and 100", c.Defaults.ResizeThreshold)
return fmt.Errorf("🔴 '%g' must be a floating point between 0 and 100", c.Defaults.ResizeThreshold)
}
if c.overrides.ResizeThreshold < 0 || c.overrides.ResizeThreshold > 100 {
return fmt.Errorf("🔴 %g must be a floating point between 0 and 100", c.overrides.ResizeThreshold)
return fmt.Errorf("🔴 '%g' must be a floating point between 0 and 100", c.overrides.ResizeThreshold)
}
for name, device := range c.Devices {
if device.ResizeThreshold < 0 || device.ResizeThreshold > 100 {
return fmt.Errorf("🔴 %s: %g must be a floating point between 0 and 100", name, device.ResizeThreshold)
return fmt.Errorf("🔴 %s: '%g' must be a floating point between 0 and 100", name, device.ResizeThreshold)
}
}
return nil
Expand Down
10 changes: 5 additions & 5 deletions internal/layer/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (fdl *ChangePermissionsLayer) Modify(c *config.Config) ([]action.Action, er
if cd.MountPoint == "" {
continue
}
if cd.Permissions == "" {
if cd.Permissions == 0 {
continue
}

Expand All @@ -37,7 +37,7 @@ func (fdl *ChangePermissionsLayer) Modify(c *config.Config) ([]action.Action, er
return nil, fmt.Errorf("🔴 %s is either not a directory or does not exist", cd.MountPoint)
}

if d.Permissions.Equals(cd.Permissions) {
if d.Permissions == cd.Permissions {
continue
}
mode := c.GetMode(name)
Expand All @@ -53,7 +53,7 @@ func (fdl *ChangePermissionsLayer) Validate(c *config.Config) error {
continue
}
// When permissions is not provided
if cd.Permissions == "" {
if cd.Permissions == 0 {
continue
}

Expand All @@ -62,8 +62,8 @@ func (fdl *ChangePermissionsLayer) Validate(c *config.Config) error {
return fmt.Errorf("🔴 %s: Failed ownership validation checks. %s is either not a directory or does not exist", name, cd.MountPoint)
}

if d.Permissions.NotEquals(cd.Permissions) {
return fmt.Errorf("🔴 %s: Failed permissions validation checks. %s Permissions Expected=%s, Actual=%s", name, cd.MountPoint, cd.Permissions, d.Permissions)
if d.Permissions != cd.Permissions {
return fmt.Errorf("🔴 %s: Failed permissions validation checks. %s Permissions Expected=%#o, Actual=%#o", name, cd.MountPoint, cd.Permissions, d.Permissions)
}
}
return nil
Expand Down
46 changes: 19 additions & 27 deletions internal/model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

import (
"fmt"
"os"
"io/fs"
"strconv"
)

Expand All @@ -24,37 +24,29 @@ type File struct {
Permissions Permissions
}

type Permissions string
type Permissions uint32

func FromFileMode(fm os.FileMode) Permissions {
return Permissions(fmt.Sprintf("%o", fm))
func (p Permissions) Perm() fs.FileMode {
return fs.FileMode(p)
}

func (p Permissions) ToFileMode() (os.FileMode, error) {
if p == "" {
return os.FileMode(0), nil
func (p *Permissions) UnmarshalYAML(unmarshal func(interface{}) error) error {
var ps string;
if err := unmarshal(&ps); err != nil {
return err
}
// Base: 8
// Width: 32 bits
mode, err := strconv.ParseUint(string(p), 8, 32)
if err != nil {
return os.FileMode(0), fmt.Errorf("🔴 %s is not a valid permissions option", string(p))
if ps == "" {
*p = Permissions(0)
return nil
}
return os.FileMode(mode), nil
}

func (pa Permissions) Equals(pb Permissions) bool {
fma, err := pa.ToFileMode()
// Base: 8, Bit Length: 32
mode, err := strconv.ParseUint(ps, 8, 32)
if err != nil {
return false
return fmt.Errorf("🔴 invalid permission value. '%v' must be a valid octal number", ps)
}
fmb, err := pb.ToFileMode()
if err != nil {
return false
if mode > 0777 {
return fmt.Errorf("🔴 invalid permission value. '%#o' exceeds the maximum allowed value (0777)", mode)
}
return fma == fmb
}

func (p Permissions) NotEquals(pb Permissions) bool {
return !p.Equals(pb)
}
*p = Permissions(mode)
return nil
}
8 changes: 2 additions & 6 deletions internal/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (ufs *UnixFileService) GetFile(file string) (*model.File, error) {
InodeNo: int(stat.Ino),
Uid: int(stat.Uid),
Gid: int(stat.Gid),
Permissions: model.FromFileMode(info.Mode().Perm()),
Permissions: model.Permissions(info.Mode().Perm()),
Type: ft,
}, nil
}
Expand All @@ -66,9 +66,5 @@ func (ufs *UnixFileService) ChangeOwner(file string, uid int, gid int) error {
}

func (ufs *UnixFileService) ChangePermissions(file string, perms model.Permissions) error {
fm, err := perms.ToFileMode()
if err != nil {
return err
}
return os.Chmod(file, fm)
return os.Chmod(file, perms.Perm())
}

0 comments on commit 9636f7e

Please sign in to comment.