-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.go
169 lines (140 loc) · 5.07 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
/*
Copyright DTCC 2016 All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package java
import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"net/url"
"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.java")
// Platform for java chaincodes in java
type Platform struct {
}
// Name returns the name of this platform
func (javaPlatform *Platform) Name() string {
return pb.ChaincodeSpec_JAVA.String()
}
//ValidatePath validates the java chaincode paths
func (javaPlatform *Platform) ValidatePath(rawPath string) error {
path, err := url.Parse(rawPath)
if err != nil || path == nil {
logger.Errorf("invalid chaincode path %s %v", rawPath, err)
return fmt.Errorf("invalid path: %s", err)
}
return nil
}
func (javaPlatform *Platform) ValidateCodePackage(code []byte) error {
if len(code) == 0 {
// Nothing to validate if no CodePackage was included
return nil
}
// File to be valid should match first RegExp and not match second one.
filesToMatch := regexp.MustCompile(`^(/)?src/((src|META-INF)/.*|(build\.gradle|settings\.gradle|pom\.xml))`)
filesToIgnore := regexp.MustCompile(`.*\.class$`)
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)
for {
header, err := tr.Next()
if err != nil {
if err == io.EOF {
// We only get here if there are no more entries to scan
break
} else {
return err
}
}
// --------------------------------------------------------------------------------------
// Check name for conforming path
// --------------------------------------------------------------------------------------
if !filesToMatch.MatchString(header.Name) || filesToIgnore.MatchString(header.Name) {
return fmt.Errorf("illegal file detected in payload: \"%s\"", header.Name)
}
// --------------------------------------------------------------------------------------
// 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)
}
}
return nil
}
// WritePackage writes the java chaincode package
func (javaPlatform *Platform) GetDeploymentPayload(path string) ([]byte, error) {
logger.Debugf("Packaging java project from path %s", path)
var err error
// --------------------------------------------------------------------------------------
// Write out our tar package
// --------------------------------------------------------------------------------------
payload := bytes.NewBuffer(nil)
gw := gzip.NewWriter(payload)
tw := tar.NewWriter(gw)
folder := path
if folder == "" {
logger.Error("ChaincodeSpec's path cannot be empty")
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]
}
if err = cutil.WriteJavaProjectToPackage(tw, folder); err != nil {
logger.Errorf("Error writing java project to tar package %s", err)
return nil, fmt.Errorf("Error writing Chaincode package contents: %s", err)
}
tw.Close()
gw.Close()
return payload.Bytes(), nil
}
func (javaPlatform *Platform) GenerateDockerfile() (string, error) {
var buf []string
buf = append(buf, "FROM "+cutil.GetDockerfileFromConfig("chaincode.java.runtime"))
buf = append(buf, "ADD binpackage.tar /root/chaincode-java/chaincode")
dockerFileContents := strings.Join(buf, "\n")
return dockerFileContents, nil
}
func (javaPlatform *Platform) GenerateDockerBuild(path string, code []byte, tw *tar.Writer) error {
codepackage := bytes.NewReader(code)
binpackage := bytes.NewBuffer(nil)
buildOptions := util.DockerBuildOptions{
Image: cutil.GetDockerfileFromConfig("chaincode.java.runtime"),
Cmd: "./build.sh",
InputStream: codepackage,
OutputStream: binpackage,
}
logger.Debugf("Executing docker build %v, %v", buildOptions.Image, buildOptions.Cmd)
err := util.DockerBuild(buildOptions)
if err != nil {
logger.Errorf("Can't build java chaincode %v", err)
return err
}
resultBytes := binpackage.Bytes()
return cutil.WriteBytesToPackage("binpackage.tar", resultBytes, tw)
}
//GetMetadataProvider fetches metadata provider given deployment spec
func (javaPlatform *Platform) GetMetadataProvider(code []byte) platforms.MetadataProvider {
return &ccmetadata.TargzMetadataProvider{Code: code}
}