@@ -25,6 +25,8 @@ import (
25
25
26
26
"bufio"
27
27
28
+ "regexp"
29
+
28
30
"github.com/fsouza/go-dockerclient"
29
31
container "github.com/hyperledger/fabric/core/container/api"
30
32
"github.com/hyperledger/fabric/core/container/ccintf"
@@ -117,7 +119,10 @@ func (vm *DockerVM) createContainer(ctxt context.Context, client *docker.Client,
117
119
}
118
120
119
121
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
+ }
121
126
outputbuf := bytes .NewBuffer (nil )
122
127
opts := docker.BuildImageOptions {
123
128
Name : id ,
@@ -156,7 +161,10 @@ func (vm *DockerVM) Deploy(ctxt context.Context, ccid ccintf.CCID, args []string
156
161
157
162
//Start starts a container using a previously created docker image
158
163
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
+ }
160
168
client , err := cutil .NewDockerClient ()
161
169
if err != nil {
162
170
dockerLogger .Debugf ("start - cannot create client %s" , err )
@@ -283,7 +291,10 @@ func (vm *DockerVM) Start(ctxt context.Context, ccid ccintf.CCID, args []string,
283
291
284
292
//Stop stops a running chaincode
285
293
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
+ }
287
298
client , err := cutil .NewDockerClient ()
288
299
if err != nil {
289
300
dockerLogger .Debugf ("stop - cannot create client %s" , err )
@@ -324,7 +335,10 @@ func (vm *DockerVM) stopInternal(ctxt context.Context, client *docker.Client, id
324
335
325
336
//Destroy destroys an image
326
337
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
+ }
328
342
client , err := cutil .NewDockerClient ()
329
343
if err != nil {
330
344
dockerLogger .Errorf ("destroy-cannot create client %s" , err )
@@ -348,11 +362,29 @@ func (vm *DockerVM) Destroy(ctxt context.Context, ccid ccintf.CCID, force bool,
348
362
func (vm * DockerVM ) GetVMName (ccid ccintf.CCID ) (string , error ) {
349
363
name := ccid .GetName ()
350
364
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 ), "-" ))
353
376
} 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 )
357
388
}
389
+ return name , nil
358
390
}
0 commit comments