Skip to content

Commit

Permalink
bugfix: create contaienr with invalid name
Browse files Browse the repository at this point in the history
Signed-off-by: Lang Chi <21860405@zju.edu.cn>
  • Loading branch information
lang710 committed Jun 24, 2019
1 parent a43bd5f commit db4ee2b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 6 additions & 0 deletions daemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"reflect"
"regexp"
"strings"
"sync"

Expand All @@ -28,8 +29,13 @@ const (
CgroupSystemdDriver = "systemd"
// DefaultCgroupDriver is default cgroups driver
DefaultCgroupDriver = CgroupfsDriver
// ValidNameChars collects the characters allowed to represent a name, normally used to validate container and volume names.
ValidNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
)

// ValidNamePattern is a regular expression to validate names against the collection of restricted characters.
var ValidNamePattern = regexp.MustCompile(`^/?` + ValidNameChars + `+$`)

// Config refers to daemon's whole configurations.
type Config struct {
sync.Mutex `json:"-"`
Expand Down
8 changes: 5 additions & 3 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/alibaba/pouch/apis/opts"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/ctrd"
"github.com/alibaba/pouch/daemon/config"
daemon_config "github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/daemon/containerio"
"github.com/alibaba/pouch/daemon/events"
"github.com/alibaba/pouch/daemon/logger"
Expand Down Expand Up @@ -195,7 +195,7 @@ type ContainerManager struct {
IOs *containerio.Cache
ExecProcesses *collect.SafeMap

Config *config.Config
Config *daemon_config.Config

// Cache stores all containers in memory.
// Element operated in cache must have a type of *Container.
Expand All @@ -211,7 +211,7 @@ type ContainerManager struct {
}

// NewContainerManager creates a brand new container manager.
func NewContainerManager(ctx context.Context, store *meta.Store, cli ctrd.APIClient, imgMgr ImageMgr, volMgr VolumeMgr, cfg *config.Config, contPlugin hookplugins.ContainerPlugin, eventsService *events.Events) (*ContainerManager, error) {
func NewContainerManager(ctx context.Context, store *meta.Store, cli ctrd.APIClient, imgMgr ImageMgr, volMgr VolumeMgr, cfg *daemon_config.Config, contPlugin hookplugins.ContainerPlugin, eventsService *events.Events) (*ContainerManager, error) {
mgr := &ContainerManager{
Store: store,
NameToID: collect.NewSafeMap(),
Expand Down Expand Up @@ -395,6 +395,8 @@ func (mgr *ContainerManager) Create(ctx context.Context, name string, config *ty

if name == "" {
name = mgr.generateName(id)
} else if !daemon_config.ValidNamePattern.MatchString(name) {
return nil, fmt.Errorf("Invalid container name (%s), only %s are allowed", name, daemon_config.ValidNameChars)
} else if mgr.NameToID.Get(name).Exist() {
return nil, errors.Wrapf(errtypes.ErrAlreadyExisted, "container name %s", name)
}
Expand Down
9 changes: 9 additions & 0 deletions test/cli_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,12 @@ func (suite *PouchCreateSuite) TestCreateWithoutNvidiaConfig(c *check.C) {
}
c.Assert(result[0].HostConfig.Resources.NvidiaConfig, check.IsNil)
}

// TestCreateWithInvalidName tests creating container with invalid name.
func (suite *PouchRunSuite) TestCreateWithInvalidName(c *check.C) {
name := "new:invalid"
res := command.PouchRun("create", "--name", name, busyboxImage)
if !strings.Contains(res.Stdout(), "Invalid container name") {
check.Commentf("Expected '%s', but got %q", "Invalid container name", res.Stdout())
}
}

0 comments on commit db4ee2b

Please sign in to comment.