Maintenance: error wrapping of alpine package
This commit is contained in:
@@ -49,6 +49,7 @@ var (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
errSetupRouting = errors.New("cannot setup routing")
|
errSetupRouting = errors.New("cannot setup routing")
|
||||||
|
errCreateUser = errors.New("cannot create user")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -177,7 +178,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation,
|
|||||||
const defaultUsername = "nonrootuser"
|
const defaultUsername = "nonrootuser"
|
||||||
nonRootUsername, err := alpineConf.CreateUser(defaultUsername, puid)
|
nonRootUsername, err := alpineConf.CreateUser(defaultUsername, puid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("%w: %s", errCreateUser, err)
|
||||||
}
|
}
|
||||||
if nonRootUsername != defaultUsername {
|
if nonRootUsername != defaultUsername {
|
||||||
logger.Info("using existing username %s corresponding to user id %d", nonRootUsername, puid)
|
logger.Info("using existing username %s corresponding to user id %d", nonRootUsername, puid)
|
||||||
|
|||||||
@@ -1,35 +1,47 @@
|
|||||||
package alpine
|
package alpine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrUserAlreadyExists = errors.New("user already exists")
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateUser creates a user in Alpine with the given UID.
|
// CreateUser creates a user in Alpine with the given UID.
|
||||||
func (c *configurator) CreateUser(username string, uid int) (createdUsername string, err error) {
|
func (c *configurator) CreateUser(username string, uid int) (createdUsername string, err error) {
|
||||||
UIDStr := fmt.Sprintf("%d", uid)
|
UIDStr := strconv.Itoa(uid)
|
||||||
u, err := c.osUser.LookupID(UIDStr)
|
u, err := c.osUser.LookupID(UIDStr)
|
||||||
_, unknownUID := err.(user.UnknownUserIdError)
|
_, unknownUID := err.(user.UnknownUserIdError)
|
||||||
if err != nil && !unknownUID {
|
if err != nil && !unknownUID {
|
||||||
return "", fmt.Errorf("cannot create user: %w", err)
|
return "", err
|
||||||
} else if u != nil {
|
}
|
||||||
|
|
||||||
|
if u != nil {
|
||||||
if u.Username == username {
|
if u.Username == username {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
return u.Username, nil
|
return u.Username, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err = c.osUser.Lookup(username)
|
u, err = c.osUser.Lookup(username)
|
||||||
_, unknownUsername := err.(user.UnknownUserError)
|
_, unknownUsername := err.(user.UnknownUserError)
|
||||||
if err != nil && !unknownUsername {
|
if err != nil && !unknownUsername {
|
||||||
return "", fmt.Errorf("cannot create user: %w", err)
|
return "", err
|
||||||
} else if u != nil {
|
|
||||||
return "", fmt.Errorf("cannot create user: user with name %s already exists for ID %s instead of %d",
|
|
||||||
username, u.Uid, uid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if u != nil {
|
||||||
|
return "", fmt.Errorf("%w: with name %s for ID %s instead of %d",
|
||||||
|
ErrUserAlreadyExists, username, u.Uid, uid)
|
||||||
|
}
|
||||||
|
|
||||||
file, err := c.openFile("/etc/passwd", os.O_APPEND|os.O_WRONLY, 0644)
|
file, err := c.openFile("/etc/passwd", os.O_APPEND|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("cannot create user: %w", err)
|
return "", err
|
||||||
}
|
}
|
||||||
s := fmt.Sprintf("%s:x:%d:::/dev/null:/sbin/nologin\n", username, uid)
|
s := fmt.Sprintf("%s:x:%d:::/dev/null:/sbin/nologin\n", username, uid)
|
||||||
_, err = file.WriteString(s)
|
_, err = file.WriteString(s)
|
||||||
@@ -37,5 +49,6 @@ func (c *configurator) CreateUser(username string, uid int) (createdUsername str
|
|||||||
_ = file.Close()
|
_ = file.Close()
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return username, file.Close()
|
return username, file.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user