Skip to content

Commit 6689e73

Browse files
author
Chris Elder
committed
[FAB-7886] Exclude non .json ext in deploy metadata
When using Golang CC, the presence of a *.txt file (containing multiple json indexes) does not cause any issues in creating indexes from valid index definition files, but while using Node CC, the peer crashes with an error. Add a check in the ValidateMetadataFile to verify the file extension is ".json". Add a return value to indicate if the file should be included since returning an error would cause the peer to crash. Change-Id: Iceeca2d6683490eda9330becbe95a8a4ecedb5c0 Signed-off-by: Chris Elder <chris.elder@us.ibm.com>
1 parent 5b4c925 commit 6689e73

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

core/common/ccprovider/metadata/validators.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ package metadata
88

99
import (
1010
"encoding/json"
11-
"errors"
1211
"fmt"
1312
"io/ioutil"
13+
"path/filepath"
1414
"reflect"
1515
"strings"
1616

@@ -36,18 +36,28 @@ func (e *UnhandledDirectoryError) Error() string {
3636
return e.err
3737
}
3838

39-
// InvalidFileError is returned for invalid metadata files
40-
type InvalidFileError struct {
39+
// BadExtensionError is returned for metadata files with extension other than .json
40+
type BadExtensionError struct {
4141
err string
4242
}
4343

44-
func (e *InvalidFileError) Error() string {
44+
func (e *BadExtensionError) Error() string {
45+
return e.err
46+
}
47+
48+
// InvalidIndexContentError is returned for metadata files with invalid content
49+
type InvalidIndexContentError struct {
50+
err string
51+
}
52+
53+
func (e *InvalidIndexContentError) Error() string {
4554
return e.err
4655
}
4756

4857
// ValidateMetadataFile checks that metadata files are valid
4958
// according to the validation rules of the metadata directory (metadataType)
5059
func ValidateMetadataFile(srcPath, metadataType string) error {
60+
5161
// Get the validator handler for the metadata directory
5262
fileValidator, ok := fileValidators[metadataType]
5363

@@ -56,10 +66,10 @@ func ValidateMetadataFile(srcPath, metadataType string) error {
5666
return &UnhandledDirectoryError{fmt.Sprintf("Metadata not supported in directory: %s", metadataType)}
5767
}
5868

59-
// If the file is not valid for the given metadata directory, return InvalidFileError
69+
// If the file is not valid for the given metadata directory, return an error
6070
err := fileValidator(srcPath)
6171
if err != nil {
62-
return &InvalidFileError{fmt.Sprintf("Metadata file [%s] failed validation: %s", srcPath, err)}
72+
return err
6373
}
6474

6575
// file is valid, return nil error
@@ -68,6 +78,14 @@ func ValidateMetadataFile(srcPath, metadataType string) error {
6878

6979
// couchdbIndexFileValidator implements fileValidator
7080
func couchdbIndexFileValidator(srcPath string) error {
81+
82+
ext := filepath.Ext(srcPath)
83+
84+
// if the file does not have a .json extension, then return as error
85+
if ext != ".json" {
86+
return &BadExtensionError{fmt.Sprintf("Index metadata file [%s] does not have a .json extension", srcPath)}
87+
}
88+
7189
fileBytes, err := ioutil.ReadFile(srcPath)
7290
if err != nil {
7391
return err
@@ -76,11 +94,16 @@ func couchdbIndexFileValidator(srcPath string) error {
7694
// if the content does not validate as JSON, return err to invalidate the file
7795
boolIsJSON, indexDefinition := isJSON(fileBytes)
7896
if !boolIsJSON {
79-
return errors.New("File is not valid JSON")
97+
return &InvalidIndexContentError{fmt.Sprintf("Index metadata file [%s] is not a valid JSON", srcPath)}
8098
}
8199

82100
// validate the index definition
83-
return validateIndexJSON(indexDefinition)
101+
err = validateIndexJSON(indexDefinition)
102+
if err != nil {
103+
return &InvalidIndexContentError{fmt.Sprintf("Index metadata file [%s] is not a valid index definition: %s", srcPath, err)}
104+
}
105+
106+
return nil
84107

85108
}
86109

core/common/ccprovider/metadata/validators_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ func TestBadIndexJSON(t *testing.T) {
4545

4646
err = ValidateMetadataFile(filename, "META-INF/statedb/couchdb/indexes")
4747

48-
assert.Error(t, err, "Should have received an InvalidFileError")
48+
assert.Error(t, err, "Should have received an InvalidIndexContentError")
4949

50-
// Type assertion on InvalidFileError
51-
_, ok := err.(*InvalidFileError)
52-
assert.True(t, ok, "Should have received an InvalidFileError")
50+
// Type assertion on InvalidIndexContentError
51+
_, ok := err.(*InvalidIndexContentError)
52+
assert.True(t, ok, "Should have received an InvalidIndexContentError")
5353

5454
t.Log("SAMPLE ERROR STRING:", err.Error())
5555
}
@@ -108,6 +108,27 @@ func TestCantReadFile(t *testing.T) {
108108

109109
err := ValidateMetadataFile(filename, "META-INF/statedb/couchdb/indexes")
110110
assert.Error(t, err, "Should have received error reading file")
111+
112+
}
113+
114+
func TestBadMetadataExtension(t *testing.T) {
115+
testDir := filepath.Join(packageTestDir, "BadMetadataExtension")
116+
cleanupDir(testDir)
117+
defer cleanupDir(testDir)
118+
119+
filename := filepath.Join(testDir, "META-INF/statedb/couchdb/indexes", "myIndex.go")
120+
filebytes := []byte(`{"index":{"fields":["data.docType","data.owner"]},"name":"indexOwner","type":"json"}`)
121+
122+
err := writeToFile(filename, filebytes)
123+
assert.NoError(t, err, "Error writing to file")
124+
125+
err = ValidateMetadataFile(filename, "META-INF/statedb/couchdb/indexes")
126+
assert.Error(t, err, "Should have received an BadExtensionError")
127+
128+
// Type assertion on BadExtensionError
129+
_, ok := err.(*BadExtensionError)
130+
assert.True(t, ok, "Should have received an BadExtensionError")
131+
111132
}
112133

113134
func TestIndexValidation(t *testing.T) {

0 commit comments

Comments
 (0)