forked from hyperledger/fabric
/
platform.go
137 lines (112 loc) · 3.85 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
/*
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"
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/core/chaincode/platforms/util"
)
var logger = flogging.MustGetLogger("chaincode.platform.java")
// Platform for java chaincodes in java
type Platform struct{}
// Name returns the name of this platform
func (p *Platform) Name() string {
return pb.ChaincodeSpec_JAVA.String()
}
//ValidatePath validates the java chaincode paths
func (p *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 (p *Platform) ValidateCodePackage(code []byte) error {
// 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 == io.EOF {
break
}
if err != nil {
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 (p *Platform) GetDeploymentPayload(path string) ([]byte, error) {
logger.Debugf("Packaging java project from path %s", path)
if path == "" {
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 path[len(path)-1] == '/' {
path = path[:len(path)-1]
}
buf := &bytes.Buffer{}
gw := gzip.NewWriter(buf)
tw := tar.NewWriter(gw)
excludedDirs := []string{"target", "build", "out"}
excludedFileTypes := map[string]bool{".class": true}
err := util.WriteFolderToTarPackage(tw, path, excludedDirs, nil, excludedFileTypes)
if err != nil {
logger.Errorf("Error writing java project to tar package %s", err)
return nil, fmt.Errorf("failed to create chaincode package: %s", err)
}
tw.Close()
gw.Close()
return buf.Bytes(), nil
}
func (p *Platform) GenerateDockerfile() (string, error) {
var buf []string
buf = append(buf, "FROM "+util.GetDockerImageFromConfig("chaincode.java.runtime"))
buf = append(buf, "ADD binpackage.tar /root/chaincode-java/chaincode")
dockerFileContents := strings.Join(buf, "\n")
return dockerFileContents, nil
}
func (p *Platform) DockerBuildOptions(path string) (util.DockerBuildOptions, error) {
return util.DockerBuildOptions{
Image: util.GetDockerImageFromConfig("chaincode.java.runtime"),
Cmd: "./build.sh",
}, nil
}