Skip to content

Commit

Permalink
bugfix: create container 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 authored and ZYecho committed Jul 10, 2019
1 parent 48da05a commit 17e5968
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 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
2 changes: 1 addition & 1 deletion test/cli_create_log_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (suite *PouchCreateLogOptionsSuite) TestOK(c *check.C) {
driver: "json-file",
expected: nil,
}, {
cname: "TestCreateLogOptions_jsonfile_tag=1",
cname: "TestCreateLogOptions_jsonfile_tag_1",
driver: "json-file",
logOpts: []string{"tag=1"},
expected: map[string]string{
Expand Down
10 changes: 10 additions & 0 deletions test/cli_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,13 @@ 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)
defer DelContainerForceMultyTime(c, name)
if !strings.Contains(res.Stdout(), "Invalid container name") {
check.Commentf("Expected '%s', but got %q", "Invalid container name", res.Stdout())
}
}

0 comments on commit 17e5968

Please sign in to comment.