Skip to content

Commit 9ae2672

Browse files
committed
[FAB-2929] Docker repository tags not sanitised
Chaincode cannot be instantiated (invoked, or queried) if network ID, peer ID or chain code name invalidate Docker's tagging rules. Sanitised according to Dockers naming rules [a-z0-9](?:[._-][a-z0-9])*. https://forums.docker.com/t/docker-registry-v2-spec-and-repository-naming-rule/5466 Change-Id: I15d8412cf80581126fd3d8fca0a66c9c0877853e Signed-off-by: Nick Murray <[email protected]>
1 parent a785a4c commit 9ae2672

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

core/container/dockercontroller/dockercontroller.go

+41-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525

2626
"bufio"
2727

28+
"regexp"
29+
2830
"github.com/fsouza/go-dockerclient"
2931
container "github.com/hyperledger/fabric/core/container/api"
3032
"github.com/hyperledger/fabric/core/container/ccintf"
@@ -117,7 +119,10 @@ func (vm *DockerVM) createContainer(ctxt context.Context, client *docker.Client,
117119
}
118120

119121
func (vm *DockerVM) deployImage(client *docker.Client, ccid ccintf.CCID, args []string, env []string, reader io.Reader) error {
120-
id, _ := vm.GetVMName(ccid)
122+
id, err := vm.GetVMName(ccid)
123+
if err != nil {
124+
return err
125+
}
121126
outputbuf := bytes.NewBuffer(nil)
122127
opts := docker.BuildImageOptions{
123128
Name: id,
@@ -156,7 +161,10 @@ func (vm *DockerVM) Deploy(ctxt context.Context, ccid ccintf.CCID, args []string
156161

157162
//Start starts a container using a previously created docker image
158163
func (vm *DockerVM) Start(ctxt context.Context, ccid ccintf.CCID, args []string, env []string, builder container.BuildSpecFactory) error {
159-
imageID, _ := vm.GetVMName(ccid)
164+
imageID, err := vm.GetVMName(ccid)
165+
if err != nil {
166+
return err
167+
}
160168
client, err := cutil.NewDockerClient()
161169
if err != nil {
162170
dockerLogger.Debugf("start - cannot create client %s", err)
@@ -283,7 +291,10 @@ func (vm *DockerVM) Start(ctxt context.Context, ccid ccintf.CCID, args []string,
283291

284292
//Stop stops a running chaincode
285293
func (vm *DockerVM) Stop(ctxt context.Context, ccid ccintf.CCID, timeout uint, dontkill bool, dontremove bool) error {
286-
id, _ := vm.GetVMName(ccid)
294+
id, err := vm.GetVMName(ccid)
295+
if err != nil {
296+
return err
297+
}
287298
client, err := cutil.NewDockerClient()
288299
if err != nil {
289300
dockerLogger.Debugf("stop - cannot create client %s", err)
@@ -324,7 +335,10 @@ func (vm *DockerVM) stopInternal(ctxt context.Context, client *docker.Client, id
324335

325336
//Destroy destroys an image
326337
func (vm *DockerVM) Destroy(ctxt context.Context, ccid ccintf.CCID, force bool, noprune bool) error {
327-
id, _ := vm.GetVMName(ccid)
338+
id, err := vm.GetVMName(ccid)
339+
if err != nil {
340+
return err
341+
}
328342
client, err := cutil.NewDockerClient()
329343
if err != nil {
330344
dockerLogger.Errorf("destroy-cannot create client %s", err)
@@ -348,11 +362,29 @@ func (vm *DockerVM) Destroy(ctxt context.Context, ccid ccintf.CCID, force bool,
348362
func (vm *DockerVM) GetVMName(ccid ccintf.CCID) (string, error) {
349363
name := ccid.GetName()
350364

351-
if ccid.NetworkID != "" {
352-
return fmt.Sprintf("%s-%s-%s", ccid.NetworkID, ccid.PeerID, name), nil
365+
//Convert to lowercase and replace any invalid characters with "-"
366+
r := regexp.MustCompile("[^a-zA-Z0-9-_.]")
367+
368+
if ccid.NetworkID != "" && ccid.PeerID != "" {
369+
name = strings.ToLower(
370+
r.ReplaceAllString(
371+
fmt.Sprintf("%s-%s-%s", ccid.NetworkID, ccid.PeerID, name), "-"))
372+
} else if ccid.NetworkID != "" {
373+
name = strings.ToLower(
374+
r.ReplaceAllString(
375+
fmt.Sprintf("%s-%s", ccid.NetworkID, name), "-"))
353376
} else if ccid.PeerID != "" {
354-
return fmt.Sprintf("%s-%s", ccid.PeerID, name), nil
355-
} else {
356-
return name, nil
377+
name = strings.ToLower(
378+
r.ReplaceAllString(
379+
fmt.Sprintf("%s-%s", ccid.PeerID, name), "-"))
380+
}
381+
382+
// Check name complies with Docker's repository naming rules
383+
r = regexp.MustCompile("^[a-z0-9]+(([._-][a-z0-9]+)+)?$")
384+
385+
if !r.MatchString(name) {
386+
dockerLogger.Errorf("Error constructing Docker VM Name. '%s' breaks Docker's repository naming rules", name)
387+
return name, fmt.Errorf("Error constructing Docker VM Name. '%s' breaks Docker's repository naming rules", name)
357388
}
389+
return name, nil
358390
}

0 commit comments

Comments
 (0)