Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/urunc/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ var createCommand = &cli.Command{
func createUnikontainer(cmd *cli.Command, uruncCfg *unikontainers.UruncConfig) (err error) {
err = nil
containerID := cmd.Args().First()
if containerID == "" {
err = fmt.Errorf("container id cannot be empty")
if err = unikontainers.ValidateID(containerID); err != nil {
return err
}
metrics.SetLoggerContainerID(containerID)
Expand Down
5 changes: 3 additions & 2 deletions cmd/urunc/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/urfave/cli/v3"
"github.com/urunc-dev/urunc/pkg/unikontainers"
)

var deleteCommand = &cli.Command{
Expand Down Expand Up @@ -58,8 +59,8 @@ status of "ubuntu01" as "stopped" the following will delete resources held for
if err != nil {
if errors.Is(err, os.ErrNotExist) {
containerID := cmd.Args().First()
if containerID == "" {
return ErrEmptyContainerID
if err = unikontainers.ValidateID(containerID); err != nil {
return err
}
rootDir := cmd.String("root")
containerDir := filepath.Join(rootDir, containerID)
Expand Down
6 changes: 2 additions & 4 deletions cmd/urunc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ const (
maxArgs // Checks for a maximum number of arguments.
)

var ErrEmptyContainerID = errors.New("container ID can not be empty")

// checkArgs checks the number of arguments provided in the command-line context
// against the expected number, based on the specified checkType.
func checkArgs(cmd *cli.Command, expected, checkType int) error {
Expand Down Expand Up @@ -69,8 +67,8 @@ func checkArgs(cmd *cli.Command, expected, checkType int) error {

func getUnikontainer(cmd *cli.Command) (*unikontainers.Unikontainer, error) {
containerID := cmd.Args().First()
if containerID == "" {
return nil, ErrEmptyContainerID
if err := unikontainers.ValidateID(containerID); err != nil {
return nil, err
}

// We have already made sure in main.go that root is not nil
Expand Down
26 changes: 26 additions & 0 deletions pkg/unikontainers/unikontainers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var uniklog = logrus.WithField("subsystem", "unikontainers")
var ErrQueueProxy = errors.New("this a queue proxy container")
var ErrNotUnikernel = errors.New("this is not a unikernel container")
var ErrNotExistingNS = errors.New("the namespace does not exist")
var ErrInvalidContainerID = errors.New("invalid container ID")

// Unikontainer holds the data necessary to create, manage and delete unikernel containers
type Unikontainer struct {
Expand All @@ -63,8 +64,30 @@ type Unikontainer struct {
Conn *net.UnixConn
}

// ValidateID checks containerID against the allowed character set,
func ValidateID(id string) error {
if id == "" {
return ErrInvalidContainerID
}
for i := 0; i < len(id); i++ {
c := id[i]
switch {
case c >= 'a' && c <= 'z':
case c >= 'A' && c <= 'Z':
case c >= '0' && c <= '9':
case c == '_', c == '+', c == '-', c == '.':
default:
return fmt.Errorf("%w: invalid character %q in id %q", ErrInvalidContainerID, c, id)
}
}
return nil
}

// New parses the bundle and creates a new Unikontainer object
func New(bundlePath string, containerID string, rootDir string, cfg *UruncConfig) (*Unikontainer, error) {
if err := ValidateID(containerID); err != nil {
return nil, err
}
spec, err := loadSpec(bundlePath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -113,6 +136,9 @@ func New(bundlePath string, containerID string, rootDir string, cfg *UruncConfig

// Get retrieves unikernel data from disk to create a Unikontainer object
func Get(containerID string, rootDir string) (*Unikontainer, error) {
if err := ValidateID(containerID); err != nil {
return nil, err
}
u := &Unikontainer{}
containerDir := filepath.Join(rootDir, containerID)
stateFilePath := filepath.Join(containerDir, stateFilename)
Expand Down