Skip to content

Commit 3a14af9

Browse files
author
Zhao Chaoyi
committed
[FAB-6281] Add validation for chaincode install
node.js chaincode should have a 'package.json' at the root of chaincode package Change-Id: I2a6a3ee9efc92f2f6ddba0dff83a3da68677817a Signed-off-by: Zhao Chaoyi <cychaoz@cn.ibm.com>
1 parent 900850f commit 3a14af9

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

core/chaincode/platforms/node/platform.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func (nodePlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeployment
8989
}
9090
tr := tar.NewReader(gr)
9191

92+
var foundPackageJson = false
9293
for {
9394
header, err := tr.Next()
9495
if err != nil {
@@ -102,7 +103,9 @@ func (nodePlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeployment
102103
if !re.MatchString(header.Name) {
103104
return fmt.Errorf("illegal file detected in payload: \"%s\"", header.Name)
104105
}
105-
106+
if header.Name == "src/package.json" {
107+
foundPackageJson = true
108+
}
106109
// --------------------------------------------------------------------------------------
107110
// Check that file mode makes sense
108111
// --------------------------------------------------------------------------------------
@@ -116,6 +119,9 @@ func (nodePlatform *Platform) ValidateDeploymentSpec(cds *pb.ChaincodeDeployment
116119
return fmt.Errorf("illegal file mode detected for file %s: %o", header.Name, header.Mode)
117120
}
118121
}
122+
if !foundPackageJson {
123+
return fmt.Errorf("no package.json found at the root of the chaincode package")
124+
}
119125

120126
return nil
121127
}

core/chaincode/platforms/node/platform_test.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestValidateDeploymentSpec(t *testing.T) {
7979
t.Fatal(err)
8080
}
8181

82-
cp, err := writeCodePackage(tmpfile.Name(), "filename.txt")
82+
cp, err := writeCodePackage(tmpfile.Name(), "filename.txt", 0100744)
8383
if err != nil {
8484
t.Fatal(err)
8585
}
@@ -92,7 +92,7 @@ func TestValidateDeploymentSpec(t *testing.T) {
9292
t.Fatalf("should have returned error about illegal file detected, but got '%s'", err)
9393
}
9494

95-
cp, err = writeCodePackage(tmpfile.Name(), "src/filename.txt")
95+
cp, err = writeCodePackage(tmpfile.Name(), "src/filename.txt", 0100744)
9696
if err != nil {
9797
t.Fatal(err)
9898
}
@@ -104,6 +104,30 @@ func TestValidateDeploymentSpec(t *testing.T) {
104104
} else if !strings.HasPrefix(err.Error(), "illegal file mode detected for file") {
105105
t.Fatalf("should have returned error about illegal file mode detected, but got '%s'", err)
106106
}
107+
108+
cp, err = writeCodePackage(tmpfile.Name(), "src/filename.txt", 0100666)
109+
if err != nil {
110+
t.Fatal(err)
111+
}
112+
113+
cds.CodePackage = cp
114+
err = platform.ValidateDeploymentSpec(cds)
115+
if err == nil {
116+
t.Fatal("should have failed to validate because no 'package.json' found")
117+
} else if !strings.HasPrefix(err.Error(), "no package.json found at the root of the chaincode package") {
118+
t.Fatalf("should have returned error about no package.json found, but got '%s'", err)
119+
}
120+
121+
cp, err = writeCodePackage(tmpfile.Name(), "src/package.json", 0100666)
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
126+
cds.CodePackage = cp
127+
err = platform.ValidateDeploymentSpec(cds)
128+
if err != nil {
129+
t.Fatalf("should have returned no errors, but got '%s'", err)
130+
}
107131
}
108132

109133
func TestGetDeploymentPayload(t *testing.T) {
@@ -148,6 +172,9 @@ func TestGenerateDockerBuild(t *testing.T) {
148172
{
149173
"name": "fabric-shim-test",
150174
"version": "1.0.0-snapshot",
175+
"script": {
176+
"start": "node chaincode.js"
177+
},
151178
"dependencies": {
152179
"is-sorted": "*"
153180
}
@@ -207,12 +234,12 @@ func TestGenerateDockerBuild(t *testing.T) {
207234
}
208235
}
209236

210-
func writeCodePackage(file string, packagePath string) ([]byte, error) {
237+
func writeCodePackage(file string, packagePath string, mode int64) ([]byte, error) {
211238
payload := bytes.NewBuffer(nil)
212239
gw := gzip.NewWriter(payload)
213240
tw := tar.NewWriter(gw)
214241

215-
if err := writeFileToPackage(file, packagePath, tw); err != nil {
242+
if err := writeFileToPackage(file, packagePath, tw, mode); err != nil {
216243
return nil, fmt.Errorf("Error writing Chaincode package contents: %s", err)
217244
}
218245

@@ -227,7 +254,7 @@ func writeCodePackage(file string, packagePath string) ([]byte, error) {
227254
return payload.Bytes(), nil
228255
}
229256

230-
func writeFileToPackage(localpath string, packagepath string, tw *tar.Writer) error {
257+
func writeFileToPackage(localpath string, packagepath string, tw *tar.Writer, mode int64) error {
231258
fd, err := os.Open(localpath)
232259
if err != nil {
233260
return fmt.Errorf("%s: %s", localpath, err)
@@ -247,7 +274,8 @@ func writeFileToPackage(localpath string, packagepath string, tw *tar.Writer) er
247274
//Let's take the variance out of the tar, make headers identical by using zero time
248275
oldname := header.Name
249276
header.Name = packagepath
250-
header.Mode = 0100744
277+
header.Mode = mode
278+
//header.Mode = 0100744
251279

252280
if err = tw.WriteHeader(header); err != nil {
253281
return fmt.Errorf("Error write header for (path: %s, oldname:%s,newname:%s,sz:%d) : %s", localpath, oldname, packagepath, header.Size, err)

0 commit comments

Comments
 (0)