forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.go
208 lines (172 loc) · 6.01 KB
/
platform.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
*/
package node
import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/chaincode/platforms"
"github.com/hyperledger/fabric/core/chaincode/platforms/ccmetadata"
"github.com/hyperledger/fabric/core/chaincode/platforms/util"
cutil "github.com/hyperledger/fabric/core/container/util"
pb "github.com/hyperledger/fabric/protos/peer"
)
var logger = flogging.MustGetLogger("chaincode.platform.node")
// Platform for chaincodes written in Go
type Platform struct {
}
// Returns whether the given file or directory exists or not
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
// Name returns the name of this platform
func (nodePlatform *Platform) Name() string {
return pb.ChaincodeSpec_NODE.String()
}
// ValidateSpec validates Go chaincodes
func (nodePlatform *Platform) ValidatePath(rawPath string) error {
path, err := url.Parse(rawPath)
if err != nil || path == nil {
return fmt.Errorf("invalid path: %s", err)
}
//Treat empty scheme as a local filesystem path
if path.Scheme == "" {
pathToCheck, err := filepath.Abs(rawPath)
if err != nil {
return fmt.Errorf("error obtaining absolute path of the chaincode: %s", err)
}
exists, err := pathExists(pathToCheck)
if err != nil {
return fmt.Errorf("error validating chaincode path: %s", err)
}
if !exists {
return fmt.Errorf("path to chaincode does not exist: %s", rawPath)
}
}
return nil
}
func (nodePlatform *Platform) ValidateCodePackage(code []byte) error {
if len(code) == 0 {
// Nothing to validate if no CodePackage was included
return nil
}
// FAB-2122: Scan the provided tarball to ensure it only contains source-code under
// the src folder.
//
// It should be noted that we cannot catch every threat with these techniques. Therefore,
// the container itself needs to be the last line of defense and be configured to be
// resilient in enforcing constraints. However, we should still do our best to keep as much
// garbage out of the system as possible.
re := regexp.MustCompile(`^(/)?(src|META-INF)/.*`)
is := bytes.NewReader(code)
gr, err := gzip.NewReader(is)
if err != nil {
return fmt.Errorf("failure opening codepackage gzip stream: %s", err)
}
tr := tar.NewReader(gr)
var foundPackageJson = false
for {
header, err := tr.Next()
if err != nil {
// We only get here if there are no more entries to scan
break
}
// --------------------------------------------------------------------------------------
// Check name for conforming path
// --------------------------------------------------------------------------------------
if !re.MatchString(header.Name) {
return fmt.Errorf("illegal file detected in payload: \"%s\"", header.Name)
}
if header.Name == "src/package.json" {
foundPackageJson = true
}
// --------------------------------------------------------------------------------------
// Check that file mode makes sense
// --------------------------------------------------------------------------------------
// Acceptable flags:
// ISREG == 0100000
// -rw-rw-rw- == 0666
//
// Anything else is suspect in this context and will be rejected
// --------------------------------------------------------------------------------------
if header.Mode&^0100666 != 0 {
return fmt.Errorf("illegal file mode detected for file %s: %o", header.Name, header.Mode)
}
}
if !foundPackageJson {
return fmt.Errorf("no package.json found at the root of the chaincode package")
}
return nil
}
// Generates a deployment payload by putting source files in src/$file entries in .tar.gz format
func (nodePlatform *Platform) GetDeploymentPayload(path string) ([]byte, error) {
var err error
// --------------------------------------------------------------------------------------
// Write out our tar package
// --------------------------------------------------------------------------------------
payload := bytes.NewBuffer(nil)
gw := gzip.NewWriter(payload)
tw := tar.NewWriter(gw)
folder := path
if folder == "" {
return nil, errors.New("ChaincodeSpec's path cannot be empty")
}
// trim trailing slash if it exists
if folder[len(folder)-1] == '/' {
folder = folder[:len(folder)-1]
}
logger.Debugf("Packaging node.js project from path %s", folder)
if err = cutil.WriteFolderToTarPackage(tw, folder, []string{"node_modules"}, nil, nil); err != nil {
logger.Errorf("Error writing folder to tar package %s", err)
return nil, fmt.Errorf("Error writing Chaincode package contents: %s", err)
}
// Write the tar file out
if err := tw.Close(); err != nil {
return nil, fmt.Errorf("Error writing Chaincode package contents: %s", err)
}
tw.Close()
gw.Close()
return payload.Bytes(), nil
}
func (nodePlatform *Platform) GenerateDockerfile() (string, error) {
var buf []string
buf = append(buf, "FROM "+cutil.GetDockerfileFromConfig("chaincode.node.runtime"))
buf = append(buf, "ADD binpackage.tar /usr/local/src")
dockerFileContents := strings.Join(buf, "\n")
return dockerFileContents, nil
}
func (nodePlatform *Platform) GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error {
codepackage := bytes.NewReader(code)
binpackage := bytes.NewBuffer(nil)
err := util.DockerBuild(util.DockerBuildOptions{
Cmd: fmt.Sprint("cp -R /chaincode/input/src/. /chaincode/output && cd /chaincode/output && npm install --production"),
InputStream: codepackage,
OutputStream: binpackage,
})
if err != nil {
return err
}
return cutil.WriteBytesToPackage("binpackage.tar", binpackage.Bytes(), tw)
}
//GetMetadataProvider fetches metadata provider given deployment spec
func (nodePlatform *Platform) GetMetadataProvider(code []byte) platforms.MetadataProvider {
return &ccmetadata.TargzMetadataProvider{Code: code}
}