Skip to content

Commit

Permalink
Refactor permission settings
Browse files Browse the repository at this point in the history
Create a new private type to be able to easily add permissions.

Add the functions as methods onto this type.
  • Loading branch information
surminus committed Aug 14, 2023
1 parent 3de92a5 commit b48ed4a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 222 deletions.
47 changes: 4 additions & 43 deletions resources/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package resources
import (
"fmt"
"os"
"strconv"

"github.com/surminus/viaduct"
)
Expand All @@ -12,21 +11,10 @@ import (
type Directory struct {
// Path is the path of the directory
Path string
// Mode is the permissions set of the directory
Mode os.FileMode

// User sets the user permissions by user name
User string
// Group sets the group permissions by group name
Group string
// UID sets the user permissions by UID
UID int
// GID sets the group permissions by GID
GID int
// Root enforces the use of the root user
Root bool
// Delete removes the directory if set to true.
Delete bool

permissions
}

// Dir creates a new directory
Expand All @@ -46,31 +34,7 @@ func (d *Directory) PreflightChecks(log *viaduct.Logger) error {
return fmt.Errorf("Required parameter: Path")
}

// Set optional defaults here
if d.Mode == 0 {
d.Mode = os.ModeDir | 0755
} else {
// Explicity set modedir to avoid diffs
d.Mode = os.ModeDir | d.Mode
}

if d.User == "" && d.UID == 0 && !d.Root {
if uid, err := strconv.Atoi(viaduct.Attribute.User.Uid); err != nil {
return err
} else {
d.UID = uid
}
}

if d.Group == "" && d.GID == 0 && !d.Root {
if gid, err := strconv.Atoi(viaduct.Attribute.User.Gid); err != nil {
return err
} else {
d.GID = gid
}
}

return nil
return d.preflightPermissions(pdir)
}

func (d *Directory) OperationName() string {
Expand Down Expand Up @@ -108,12 +72,9 @@ func (d *Directory) createDirectory(log *viaduct.Logger) error {
log.Noop(d.Path)
}

return setDirectoryPermissions(
return d.setDirectoryPermissions(
log,
path,
d.UID, d.GID,
d.User, d.Group,
d.Mode,
true,
)
}
Expand Down
90 changes: 6 additions & 84 deletions resources/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"io"
"net/http"
"os"
"os/user"
"strconv"

humanize "github.com/dustin/go-humanize"
"github.com/surminus/viaduct"
Expand All @@ -22,18 +20,7 @@ type Download struct {
// NotIfExists will not download the file if it already exists
NotIfExists bool

// Mode is the permissions set of the file
Mode os.FileMode
// Root enforces using the root user
Root bool
// User sets the user permissions by user name
User string
// Group sets the group permissions by group name
Group string
// UID sets the user permissions by UID
UID int
// GID sets the group permissions by GID
GID int
permissions
}

func Wget(url, path string) *Download {
Expand All @@ -56,27 +43,7 @@ func (a *Download) PreflightChecks(log *viaduct.Logger) error {
return fmt.Errorf("required parameter: Path")
}

if a.Mode == 0 {
a.Mode = 0o644
}

if a.User == "" && a.UID == 0 && !a.Root {
if uid, err := strconv.Atoi(viaduct.Attribute.User.Uid); err != nil {
return err
} else {
a.UID = uid
}
}

if a.Group == "" && a.GID == 0 && !a.Root {
if gid, err := strconv.Atoi(viaduct.Attribute.User.Gid); err != nil {
return err
} else {
a.GID = gid
}
}

return nil
return a.preflightPermissions(pfile)
}

func (a *Download) OperationName() string {
Expand Down Expand Up @@ -125,53 +92,8 @@ func (a *Download) get(log *viaduct.Logger) error {
logmsg = fmt.Sprintf("%s -> %s (size: %s)", a.URL, path, humanize.Bytes(uint64(size)))
log.Info(logmsg)

uid := a.UID
gid := a.GID

if a.User != "" {
u, err := user.Lookup(a.User)
if err != nil {
return err
}

uid, err = strconv.Atoi(u.Uid)
if err != nil {
return err
}
}

if a.Group != "" {
g, err := user.LookupGroup(a.Group)
if err != nil {
return err
}

gid, err = strconv.Atoi(g.Gid)
if err != nil {
return err
}
}

chmodmsg := fmt.Sprintf("Permissions: %s -> %s", path, a.Mode)
chownmsg := fmt.Sprintf("Permissions: %s -> %d:%d", path, uid, gid)

if viaduct.MatchChown(path, uid, gid) {
log.Noop(chownmsg)
} else {
if err := os.Chown(path, uid, gid); err != nil {
return err
}
log.Info(chownmsg)
}

if viaduct.MatchChmod(path, a.Mode) {
log.Noop(chmodmsg)
} else {
if err := os.Chown(path, uid, gid); err != nil {
return err
}
log.Info(chownmsg)
}

return nil
return a.setFilePermissions(
log,
path,
)
}
42 changes: 4 additions & 38 deletions resources/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"log"
"os"
"strconv"
"text/template"
"time"

Expand All @@ -19,20 +18,10 @@ type File struct {
Path string
// Content is the content of the file
Content string
// Mode is the permissions set of the file
Mode os.FileMode
// Root enforces using the root user
Root bool
// User sets the user permissions by user name
User string
// Group sets the group permissions by group name
Group string
// UID sets the user permissions by UID
UID int
// GID sets the group permissions by GID
GID int
// Delete will delete the file rather than create it if set to true.
Delete bool

permissions
}

// Touch simply touches an empty file to disk
Expand Down Expand Up @@ -64,23 +53,7 @@ func (f *File) PreflightChecks(log *viaduct.Logger) error {
f.Mode = 0o644
}

if f.User == "" && f.UID == 0 && !f.Root {
if uid, err := strconv.Atoi(viaduct.Attribute.User.Uid); err != nil {
return err
} else {
f.UID = uid
}
}

if f.Group == "" && f.GID == 0 && !f.Root {
if gid, err := strconv.Atoi(viaduct.Attribute.User.Gid); err != nil {
return err
} else {
f.GID = gid
}
}

return nil
return f.preflightPermissions(pfile)
}

// EmbeddedFile is a small helper function to helper reading
Expand Down Expand Up @@ -163,14 +136,7 @@ func (f *File) createFile(log *viaduct.Logger) error {
log.Noop(path)
}

return setFilePermissions(log,
path,
f.UID,
f.GID,
f.User,
f.Group,
f.Mode,
)
return f.setFilePermissions(log, path)
}

// Delete deletes a file
Expand Down
45 changes: 4 additions & 41 deletions resources/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package resources
import (
"fmt"
"os"
"strconv"

"github.com/surminus/viaduct"
"gopkg.in/src-d/go-git.v4"
Expand All @@ -23,20 +22,10 @@ type Git struct {
RemoteName string
// Ensure will continue to pull the latest changes. Optional.
Ensure bool
// Mode is the permissions set of the directory
Mode os.FileMode
// Root enforces using the root user
Root bool
// User sets the user permissions by user name
User string
// Group sets the group permissions by group name
Group string
// UID sets the user permissions by UID
UID int
// GID sets the group permissions by GID
GID int
// Delete will remove the Git directory.
Delete bool

permissions
}

// Repo will add a new repository, and ensure that it stays up to date.
Expand Down Expand Up @@ -69,30 +58,7 @@ func (g *Git) PreflightChecks(log *viaduct.Logger) error {
g.RemoteName = "origin"
}

if g.Mode == 0 {
g.Mode = os.ModeDir | 0755
} else {
// Explicity set modedir to avoid diffs
g.Mode = os.ModeDir | g.Mode
}

if g.User == "" && g.UID == 0 && !g.Root {
if uid, err := strconv.Atoi(viaduct.Attribute.User.Uid); err != nil {
return err
} else {
g.UID = uid
}
}

if g.Group == "" && g.GID == 0 && !g.Root {
if gid, err := strconv.Atoi(viaduct.Attribute.User.Gid); err != nil {
return err
} else {
g.GID = gid
}
}

return nil
return g.preflightPermissions(pdir)
}

func (g *Git) OperationName() string {
Expand Down Expand Up @@ -181,12 +147,9 @@ func (g *Git) createGit(log *viaduct.Logger) error {
log.Info(logmsg)
}

return setDirectoryPermissions(
return g.setDirectoryPermissions(
log,
path,
g.UID, g.GID,
g.User, g.Group,
g.Mode,
true,
)
}
Expand Down
Loading

0 comments on commit b48ed4a

Please sign in to comment.