Skip to content

Commit

Permalink
[FAB-17439] Expose peer's MSPID to chaincode as an env var (#549)
Browse files Browse the repository at this point in the history
CORE_PEER_LOCALMSPID

Signed-off-by: Danny Cao <dcao@us.ibm.com>
  • Loading branch information
caod123 authored and denyeart committed Jan 27, 2020
1 parent be23695 commit 9bd0a95
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 8 deletions.
3 changes: 3 additions & 0 deletions core/container/dockercontroller/dockercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type DockerVM struct {
NetworkMode string
PlatformBuilder PlatformBuilder
LoggingEnv []string
MSPID string
}

// HealthCheck checks if the DockerVM is able to communicate with the Docker
Expand Down Expand Up @@ -269,6 +270,8 @@ func (vm *DockerVM) GetEnv(ccid string, tlsConfig *ccintf.TLSConfig) []string {
envs = append(envs, "CORE_PEER_TLS_ENABLED=false")
}

envs = append(envs, fmt.Sprintf("CORE_PEER_LOCALMSPID=%s", vm.MSPID))

return envs
}

Expand Down
4 changes: 3 additions & 1 deletion core/container/dockercontroller/dockercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ func TestGetArgs(t *testing.T) {
func TestGetEnv(t *testing.T) {
vm := &DockerVM{
LoggingEnv: []string{"LOG_ENV=foo"},
MSPID: "mspid",
}

t.Run("nil TLS config", func(t *testing.T) {
env := vm.GetEnv("test", nil)
assert.Equal(t, []string{"CORE_CHAINCODE_ID_NAME=test", "LOG_ENV=foo", "CORE_PEER_TLS_ENABLED=false"}, env)
assert.Equal(t, []string{"CORE_CHAINCODE_ID_NAME=test", "LOG_ENV=foo", "CORE_PEER_TLS_ENABLED=false", "CORE_PEER_LOCALMSPID=mspid"}, env)
})

t.Run("real TLS config", func(t *testing.T) {
Expand All @@ -132,6 +133,7 @@ func TestGetEnv(t *testing.T) {
"CORE_TLS_CLIENT_KEY_FILE=/etc/hyperledger/fabric/client_pem.key",
"CORE_TLS_CLIENT_CERT_FILE=/etc/hyperledger/fabric/client_pem.crt",
"CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/peer.crt",
"CORE_PEER_LOCALMSPID=mspid",
}, env)
})
}
Expand Down
10 changes: 7 additions & 3 deletions core/container/externalbuilder/externalbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,19 @@ type Builder struct {
Location string
Logger *flogging.FabricLogger
Name string
MSPID string
}

// CreateBuilders will construct builders from the peer configuration.
func CreateBuilders(builderConfs []peer.ExternalBuilder) []*Builder {
func CreateBuilders(builderConfs []peer.ExternalBuilder, mspid string) []*Builder {
var builders []*Builder
for _, builderConf := range builderConfs {
builders = append(builders, &Builder{
Location: builderConf.Path,
Name: builderConf.Name,
EnvWhitelist: builderConf.EnvironmentWhitelist,
Logger: logger.Named(builderConf.Name),
MSPID: mspid,
})
}
return builders
Expand Down Expand Up @@ -339,9 +341,10 @@ type runConfig struct {
ClientCert string `json:"client_cert"` // PEM encoded client certificate
ClientKey string `json:"client_key"` // PEM encoded client key
RootCert string `json:"root_cert"` // PEM encoded peer chaincode certificate
MSPID string `json:"mspid"`
}

func newRunConfig(ccid string, peerConnection *ccintf.PeerConnection) runConfig {
func newRunConfig(ccid string, peerConnection *ccintf.PeerConnection, mspid string) runConfig {
var tlsConfig ccintf.TLSConfig
if peerConnection.TLSConfig != nil {
tlsConfig = *peerConnection.TLSConfig
Expand All @@ -353,6 +356,7 @@ func newRunConfig(ccid string, peerConnection *ccintf.PeerConnection) runConfig
ClientCert: string(tlsConfig.ClientCert),
ClientKey: string(tlsConfig.ClientKey),
RootCert: string(tlsConfig.RootCert),
MSPID: mspid,
}
}

Expand All @@ -364,7 +368,7 @@ func (b *Builder) Run(ccid, bldDir string, peerConnection *ccintf.PeerConnection
return nil, errors.WithMessage(err, "could not create temp run dir")
}

rc := newRunConfig(ccid, peerConnection)
rc := newRunConfig(ccid, peerConnection, b.MSPID)
marshaledRC, err := json.Marshal(rc)
if err != nil {
return nil, errors.WithMessage(err, "could not marshal run config")
Expand Down
5 changes: 3 additions & 2 deletions core/container/externalbuilder/externalbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ var _ = Describe("externalbuilder", func() {
{Path: "bad1", Name: "bad1"},
{Path: "testdata/goodbuilder", Name: "goodbuilder"},
{Path: "bad2", Name: "bad2"},
}),
}, "mspid"),
DurablePath: durablePath,
}
})
Expand All @@ -127,7 +127,7 @@ var _ = Describe("externalbuilder", func() {
BeforeEach(func() {
detector.Builders = externalbuilder.CreateBuilders([]peer.ExternalBuilder{
{Path: "bad1", Name: "bad1"},
})
}, "mspid")
})

It("returns a nil instance", func() {
Expand Down Expand Up @@ -224,6 +224,7 @@ var _ = Describe("externalbuilder", func() {
Location: "testdata/goodbuilder",
Name: "goodbuilder",
Logger: logger,
MSPID: "mspid",
}

var err error
Expand Down
1 change: 1 addition & 0 deletions core/container/externalbuilder/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var _ = Describe("Instance", func() {
Builder: &externalbuilder.Builder{
Location: "testdata/goodbuilder",
Logger: logger,
MSPID: "mspid",
},
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

OUTPUT_JSON="$(jq -S . "$2/chaincode.json")"

EXPECTED_JSON="$(echo '{"chaincode_id":"test-ccid","peer_address":"fake-peer-address","client_cert":"fake-client-cert","client_key":"fake-client-key","root_cert":"fake-root-cert"}' | jq -S .)"
EXPECTED_JSON="$(echo '{"chaincode_id":"test-ccid","peer_address":"fake-peer-address","client_cert":"fake-client-cert","client_key":"fake-client-key","root_cert":"fake-root-cert","mspid":"mspid"}' | jq -S .)"

if [ "$OUTPUT_JSON" = "$EXPECTED_JSON" ] ; then
exit 0
Expand Down
1 change: 1 addition & 0 deletions integration/externalbuilders/binary/bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export CORE_PEER_TLS_ENABLED="true"
export CORE_TLS_CLIENT_CERT_FILE="$ARTIFACTS/client.crt"
export CORE_TLS_CLIENT_KEY_FILE="$ARTIFACTS/client.key"
export CORE_PEER_TLS_ROOTCERT_FILE="$ARTIFACTS/root.crt"
export CORE_PEER_LOCALMSPID="$(jq -r .mspid "$ARTIFACTS/chaincode.json")"

jq -r .client_cert "$ARTIFACTS/chaincode.json" > "$CORE_TLS_CLIENT_CERT_FILE"
jq -r .client_key "$ARTIFACTS/chaincode.json" > "$CORE_TLS_CLIENT_KEY_FILE"
Expand Down
3 changes: 2 additions & 1 deletion internal/peer/node/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,15 @@ func serve(args []string) error {
"CORE_CHAINCODE_LOGGING_SHIM=" + chaincodeConfig.ShimLogLevel,
"CORE_CHAINCODE_LOGGING_FORMAT=" + chaincodeConfig.LogFormat,
},
MSPID: mspID,
}
if err := opsSystem.RegisterChecker("docker", dockerVM); err != nil {
logger.Panicf("failed to register docker health check: %s", err)
}
}

externalVM := &externalbuilder.Detector{
Builders: externalbuilder.CreateBuilders(coreConfig.ExternalBuilders),
Builders: externalbuilder.CreateBuilders(coreConfig.ExternalBuilders, mspID),
DurablePath: externalBuilderOutput,
}

Expand Down

0 comments on commit 9bd0a95

Please sign in to comment.