generated from golang-templates/seed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from FMotalleb/command-credential
Command-credential
- Loading branch information
Showing
11 changed files
with
200 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
package abstraction | ||
|
||
import "github.com/sirupsen/logrus" | ||
|
||
type ( | ||
ValidatableStr string | ||
Validatable interface { | ||
Validate() error | ||
Validatable interface { | ||
Validate(log *logrus.Entry) error | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package credential | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os/exec" | ||
osUser "os/user" | ||
"strconv" | ||
"syscall" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func Validate(log *logrus.Entry, usr string, grp string) error { | ||
cu, err := osUser.Current() | ||
if err != nil { | ||
return fmt.Errorf("cannot get current user error: %s", err) | ||
} | ||
if usr != "" && cu.Uid != "0" { | ||
return errors.New("cannot switch user of tasks without root privilege, if you need to use user in tasks run crontab-go as user root") | ||
} | ||
_, _, err = lookupUIDAndGID(usr, log) | ||
if err != nil { | ||
return fmt.Errorf("cannot get uid and gid of user `%s` error: %s", usr, err) | ||
} | ||
_, err = lookupGID(grp, log) | ||
if err != nil { | ||
return fmt.Errorf("cannot get gid of group `%s` error: %s", grp, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func SetUser(log *logrus.Entry, proc *exec.Cmd, usr string, grp string) { | ||
if usr == "" { | ||
log.Trace("no username given, running as current user") | ||
return | ||
} | ||
|
||
uid, gid, err := lookupUIDAndGID(usr, log) | ||
if err != nil { | ||
log.Panicf("cannot get uid and gid of user %s, error: %s", usr, err) | ||
} | ||
if grp != "" { | ||
gid, _ = lookupGID(grp, log) | ||
} | ||
|
||
setUID(log, proc, uid, gid) | ||
} | ||
|
||
func lookupGID(grp string, log *logrus.Entry) (gid uint32, err error) { | ||
g, err := osUser.LookupGroup(grp) | ||
if err != nil { | ||
log.Panicf("cannot find group with name %s in the os: %s, you've changed os users during application runtime", grp, err) | ||
} | ||
gidU, err := strconv.ParseUint(g.Gid, 10, 32) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return uint32(gidU), nil | ||
} | ||
|
||
func lookupUIDAndGID(usr string, log *logrus.Entry) (uid uint32, gid uint32, err error) { | ||
u, err := osUser.Lookup(usr) | ||
if err != nil { | ||
log.Panicf("cannot find user with name %s in the os: %s, you've changed os users during application runtime", usr, err) | ||
} | ||
uidU, err := strconv.ParseUint(u.Uid, 10, 32) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
gidU, err := strconv.ParseUint(u.Gid, 10, 32) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
return uint32(uidU), uint32(gidU), nil | ||
} | ||
|
||
func setUID( | ||
log *logrus.Entry, | ||
proc *exec.Cmd, | ||
uid uint32, | ||
gid uint32, | ||
) { | ||
log.Tracef("Setting: uid(%d) and gid(%d)", uid, gid) | ||
attrib := &syscall.SysProcAttr{} | ||
proc.SysProcAttr = attrib | ||
proc.SysProcAttr.Credential = &syscall.Credential{ | ||
Uid: uid, | ||
Gid: gid, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package credential | ||
|
||
import ( | ||
"os/exec" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func Validate(log *logrus.Entry, usr string, grp string) error { | ||
log.Warn("windows os does not have capability to set user dus validation will pass but will not work") | ||
return nil | ||
} | ||
|
||
func SetUser(log *logrus.Entry, _ *exec.Cmd, _ string, _ string) { | ||
log.Warn("cannot set user in windows platform") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package helpers | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func PanicOnErr(log *logrus.Entry, err error, message string) { | ||
if err != nil { | ||
log.Panicf(message, err) | ||
} | ||
} | ||
|
||
func FatalOnErr(log *logrus.Entry, err error, message string) { | ||
if err != nil { | ||
log.Fatalf(message, err) | ||
} | ||
} | ||
|
||
func WarnOnErr(log *logrus.Entry, err error, message string) { | ||
if err != nil { | ||
log.Warnf(message, err) | ||
} | ||
} |
Oops, something went wrong.