-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from kaleido-io/fabric-plugin
Fabric plugin
- Loading branch information
Showing
10 changed files
with
1,535 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright © 2021 Kaleido, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package fabric | ||
|
||
import ( | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/asn1" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"encoding/pem" | ||
"fmt" | ||
) | ||
|
||
var attributeTypeNames = map[string]string{ | ||
"2.5.4.6": "C", | ||
"2.5.4.10": "O", | ||
"2.5.4.11": "OU", | ||
"2.5.4.3": "CN", | ||
"2.5.4.5": "SERIALNUMBER", | ||
"2.5.4.7": "L", | ||
"2.5.4.8": "ST", | ||
"2.5.4.9": "STREET", | ||
"2.5.4.17": "POSTALCODE", | ||
} | ||
|
||
func getDNFromCertString(certString string) (string, error) { | ||
cert, err := getCertificateFromBytes(certString) | ||
if err != nil { | ||
return "", err | ||
} | ||
return getDN(&cert.Subject), nil | ||
} | ||
|
||
// borrowed from fabric-chaincode-go to guarantee the same | ||
// resolution of "DN" string from x509 certs | ||
// https://github.com/hyperledger/fabric-chaincode-go/blob/main/pkg/cid/cid.go | ||
func getDN(name *pkix.Name) string { | ||
r := name.ToRDNSequence() | ||
s := "" | ||
for i := 0; i < len(r); i++ { | ||
rdn := r[len(r)-1-i] | ||
if i > 0 { | ||
s += "," | ||
} | ||
for j, tv := range rdn { | ||
if j > 0 { | ||
s += "+" | ||
} | ||
typeString := tv.Type.String() | ||
typeName, ok := attributeTypeNames[typeString] | ||
if !ok { | ||
derBytes, err := asn1.Marshal(tv.Value) | ||
if err == nil { | ||
s += typeString + "=#" + hex.EncodeToString(derBytes) | ||
continue // No value escaping necessary. | ||
} | ||
typeName = typeString | ||
} | ||
valueString := fmt.Sprint(tv.Value) | ||
escaped := "" | ||
begin := 0 | ||
for idx, c := range valueString { | ||
if (idx == 0 && (c == ' ' || c == '#')) || | ||
(idx == len(valueString)-1 && c == ' ') { | ||
escaped += valueString[begin:idx] | ||
escaped += "\\" + string(c) | ||
begin = idx + 1 | ||
continue | ||
} | ||
switch c { | ||
case ',', '+', '"', '\\', '<', '>', ';': | ||
escaped += valueString[begin:idx] | ||
escaped += "\\" + string(c) | ||
begin = idx + 1 | ||
} | ||
} | ||
escaped += valueString[begin:] | ||
s += typeName + "=" + escaped | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func getCertificateFromBytes(certString string) (*x509.Certificate, error) { | ||
idbytes, err := base64.StdEncoding.DecodeString(certString) | ||
if err != nil { | ||
return nil, err | ||
} | ||
block, _ := pem.Decode(idbytes) | ||
cert, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cert, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright © 2021 Kaleido, Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package fabric | ||
|
||
import ( | ||
"github.com/hyperledger/firefly/internal/config" | ||
"github.com/hyperledger/firefly/internal/config/wsconfig" | ||
) | ||
|
||
const ( | ||
defaultBatchSize = 50 | ||
defaultBatchTimeout = 500 | ||
defaultPrefixShort = "fly" | ||
defaultPrefixLong = "firefly" | ||
) | ||
|
||
const ( | ||
// FabconnectConfigKey is a sub-key in the config to contain all the ethconnect specific config, | ||
FabconnectConfigKey = "fabconnect" | ||
|
||
// FabconnectConfigDefaultChannel is the default Fabric channel to use if no "ledger" is specified in requests | ||
FabconnectConfigDefaultChannel = "channel" | ||
// FabconnectConfigChaincode is the Fabric Firefly chaincode deployed to the Firefly channels | ||
FabconnectConfigChaincode = "chaincode" | ||
// FabconnectConfigSigner is the signer identity used to subscribe to FireFly chaincode events | ||
FabconnectConfigSigner = "signer" | ||
// FabconnectConfigTopic is the websocket listen topic that the node should register on, which is important if there are multiple | ||
// nodes using a single fabconnect | ||
FabconnectConfigTopic = "topic" | ||
// FabconnectConfigBatchSize is the batch size to configure on event streams, when auto-defining them | ||
FabconnectConfigBatchSize = "batchSize" | ||
// FabconnectConfigBatchTimeout is the batch timeout to configure on event streams, when auto-defining them | ||
FabconnectConfigBatchTimeout = "batchTimeout" | ||
// FabconnectConfigSkipEventstreamInit disables auto-configuration of event streams | ||
FabconnectConfigSkipEventstreamInit = "skipEventstreamInit" | ||
// FabconnectPrefixShort is used in the query string in requests to ethconnect | ||
FabconnectPrefixShort = "prefixShort" | ||
// FabconnectPrefixLong is used in HTTP headers in requests to ethconnect | ||
FabconnectPrefixLong = "prefixLong" | ||
) | ||
|
||
func (f *Fabric) InitPrefix(prefix config.Prefix) { | ||
fabconnectConf := prefix.SubPrefix(FabconnectConfigKey) | ||
wsconfig.InitPrefix(fabconnectConf) | ||
fabconnectConf.AddKnownKey(FabconnectConfigDefaultChannel) | ||
fabconnectConf.AddKnownKey(FabconnectConfigChaincode) | ||
fabconnectConf.AddKnownKey(FabconnectConfigSigner) | ||
fabconnectConf.AddKnownKey(FabconnectConfigTopic) | ||
fabconnectConf.AddKnownKey(FabconnectConfigSkipEventstreamInit) | ||
fabconnectConf.AddKnownKey(FabconnectConfigBatchSize, defaultBatchSize) | ||
fabconnectConf.AddKnownKey(FabconnectConfigBatchTimeout, defaultBatchTimeout) | ||
fabconnectConf.AddKnownKey(FabconnectPrefixShort, defaultPrefixShort) | ||
fabconnectConf.AddKnownKey(FabconnectPrefixLong, defaultPrefixLong) | ||
} |
Oops, something went wrong.