Skip to content

Commit

Permalink
use constants for file permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
meyerjrr committed Sep 19, 2024
1 parent 188c9f0 commit 3e8c4dc
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
9 changes: 7 additions & 2 deletions pkg/accessrequest/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
"github.com/common-fate/granted/pkg/config"
)

const (
// permission for user to read/write.
USER_READ_WRITE_PERM = 0644
)

type Role struct {
Account string `json:"account"`
Role string `json:"role"`
Expand Down Expand Up @@ -46,7 +51,7 @@ func (r Role) Save() error {
}

file := filepath.Join(configFolder, "latest-role")
return os.WriteFile(file, roleBytes, 0644)
return os.WriteFile(file, roleBytes, USER_READ_WRITE_PERM)
}

func LatestRole() (*Role, error) {
Expand Down Expand Up @@ -91,7 +96,7 @@ func (p Profile) Save() error {
}

file := filepath.Join(configFolder, "latest-profile")
return os.WriteFile(file, profileBytes, 0644)
return os.WriteFile(file, profileBytes, USER_READ_WRITE_PERM)
}

func LatestProfile() (*Profile, error) {
Expand Down
24 changes: 17 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ import (
"github.com/common-fate/granted/internal/build"
)

const (
// permission for user to read/write.
USER_READ_WRITE_PERM = 0644
)

const (
// permission for user to read/write.
USER_READ_WRITE_EXECUTE_PERM = 0700
)

type BrowserLaunchTemplate struct {
// UseForkProcess specifies whether to use forkprocess to launch the browser.
//
Expand Down Expand Up @@ -137,7 +147,7 @@ func SetupConfigFolder() error {
return err
}
if _, err := os.Stat(grantedFolder); os.IsNotExist(err) {
err := os.Mkdir(grantedFolder, 0644)
err := os.Mkdir(grantedFolder, USER_READ_WRITE_PERM)
if err != nil {
return err
}
Expand All @@ -153,14 +163,14 @@ func SetupZSHAutoCompleteFolderAssume() (string, error) {
}
zshPath := path.Join(grantedFolder, "zsh_autocomplete")
if _, err := os.Stat(zshPath); os.IsNotExist(err) {
err := os.Mkdir(zshPath, 0700)
err := os.Mkdir(zshPath, USER_READ_WRITE_EXECUTE_PERM)
if err != nil {
return "", err
}
}
zshPath = path.Join(zshPath, build.AssumeScriptName())
if _, err := os.Stat(zshPath); os.IsNotExist(err) {
err := os.Mkdir(zshPath, 0700)
err := os.Mkdir(zshPath, USER_READ_WRITE_EXECUTE_PERM)
if err != nil {
return "", err
}
Expand All @@ -176,14 +186,14 @@ func SetupZSHAutoCompleteFolderGranted() (string, error) {
}
zshPath := path.Join(grantedFolder, "zsh_autocomplete")
if _, err := os.Stat(zshPath); os.IsNotExist(err) {
err := os.Mkdir(zshPath, 0700)
err := os.Mkdir(zshPath, USER_READ_WRITE_EXECUTE_PERM)
if err != nil {
return "", err
}
}
zshPath = path.Join(zshPath, build.GrantedBinaryName())
if _, err := os.Stat(zshPath); os.IsNotExist(err) {
err := os.Mkdir(zshPath, 0700)
err := os.Mkdir(zshPath, USER_READ_WRITE_EXECUTE_PERM)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -269,7 +279,7 @@ func Load() (*Config, error) {
return nil, err
}

file, err := os.OpenFile(configFilePath, os.O_RDWR|os.O_CREATE, 0600)
file, err := os.OpenFile(configFilePath, os.O_RDWR|os.O_CREATE, USER_READ_WRITE_PERM)
if err != nil {
return nil, err
}
Expand All @@ -291,7 +301,7 @@ func (c *Config) Save() error {
return err
}

file, err := os.OpenFile(configFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
file, err := os.OpenFile(configFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, USER_READ_WRITE_PERM)
if err != nil {
return err
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/frecency/frecency.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/common-fate/granted/pkg/config"
)

const (
// permission for user to read/write.
USER_READ_WRITE_PERM = 0644
)

// change these to play with the weights
// values between 0 and 1
// 0 will exclude the metric all together from the ordering
Expand Down Expand Up @@ -70,14 +75,14 @@ func Load(fecencyStoreKey string) (*FrecencyStore, error) {

// check if the providers file exists
if _, err = os.Stat(c.path); os.IsNotExist(err) {
err := os.MkdirAll(configFolder, 0644)
err := os.MkdirAll(configFolder, USER_READ_WRITE_PERM)
if err != nil {
return nil, err
}
return &c, nil
}

file, err := os.OpenFile(c.path, os.O_RDWR|os.O_CREATE, 0600)
file, err := os.OpenFile(c.path, os.O_RDWR|os.O_CREATE, USER_READ_WRITE_PERM)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -186,7 +191,7 @@ func (store *FrecencyStore) save() error {
// store.Entries = store.Entries[0 : len(store.Entries)-1]
// }

file, err := os.OpenFile(store.path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
file, err := os.OpenFile(store.path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, USER_READ_WRITE_PERM)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/granted/exp/request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ import (
"gopkg.in/ini.v1"
)

const (
// permission for user to read/write.
USER_READ_WRITE_PERM = 0644
)

var Command = cli.Command{
Name: "request",
Usage: "Request access to a role",
Expand Down Expand Up @@ -726,7 +731,7 @@ func updateCachedAccessRule(ctx context.Context, opts updateCacheOpts) error {
return err
}

err = os.WriteFile(filename, ruleBytes, 0644)
err = os.WriteFile(filename, ruleBytes, USER_READ_WRITE_PERM)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/granted/registry/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
// permission for user to read/write/execute.
// permission for user to read/write.
USER_READ_WRITE_PERM = 0644
)

Expand Down
9 changes: 7 additions & 2 deletions pkg/shells/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import (
"strings"
)

const (
// permission for user to read/write.
USER_READ_WRITE_PERM = 0644
)

// AppendLine writes a line to a file if it does not already exist
func AppendLine(file string, line string) error {
b, err := os.ReadFile(file)
Expand All @@ -19,7 +24,7 @@ func AppendLine(file string, line string) error {
}

// open the file for writing
out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0644)
out, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, USER_READ_WRITE_PERM)
if err != nil {
return err
}
Expand Down Expand Up @@ -73,7 +78,7 @@ func RemoveLine(file string, lineToRemove string) error {
}

output := strings.Join(ignored, "\n")
err = os.WriteFile(file, []byte(output), 0644)
err = os.WriteFile(file, []byte(output), USER_READ_WRITE_PERM)
if err != nil {
return err
}
Expand Down

0 comments on commit 3e8c4dc

Please sign in to comment.