Skip to content

Commit

Permalink
[FAB-2882] Update SDK Go to work with v1.0.0-alpha:
Browse files Browse the repository at this point in the history
Change-Id: If96d7bfdb6c9ec7ff304a8274a288f37bf003a3b
Signed-off-by: Firas Qutishat <firas.qutishat@securekey.com>
  • Loading branch information
fqutishat committed Mar 27, 2017
1 parent c426926 commit da93bdc
Show file tree
Hide file tree
Showing 298 changed files with 13,363 additions and 16,081 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.db
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ This SDK is targeted both towards the external access to a Hyperledger Fabric bl

This project must be cloned into `$GOPATH/src/github.com/hyperledger`. Package names have been chosen to match the Hyperledger project.

Execute `go test` from the project root to build the library and run the basic headless tests.
Execute `go test` from the fabric-client and fabric-ca-client to build the library and run the basic headless tests.

Execute `go test` in the `integration_test` to run end-to-end tests. This requires you to have:
Execute `go test` in the `test/integration` to run end-to-end tests. This requires you to have:
- A working fabric and fabric-ca set up. Refer to the Hyperledger Fabric [documentation](https://github.com/hyperledger/fabric) on how to do this.
- Customized settings in the `integration_test/test_resources/config/config_test.yaml` in case your Hyperledger Fabric network is not running on `localhost` or is using different ports.

## Work in Progress

This client was last tested and found to be compatible with the following Hyperledger Fabric commit levels:
- fabric: `22d98b9e5ea36a6b209b3ea67def50a678718679`
- fabric-ca: `f18b6b769b80c889cb6b82ce34d755d9303ec881`
- fabric: v1.0.0-alpha
- fabric-ca: `4651512e4e85728e6ecaf21b8cba52f51ed16633`

64 changes: 44 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"

"github.com/op/go-logging"
Expand All @@ -34,10 +35,12 @@ import (

// PeerConfig ...
type PeerConfig struct {
Host string
Port string
EventHost string
EventPort string
Host string
Port string
EventHost string
EventPort string
TLSCertificate string
TLSServerHostOverride string
}

type fabricCAConfig struct {
Expand Down Expand Up @@ -106,20 +109,29 @@ func GetPeersConfig() []PeerConfig {
var port int
var eventHost string
var eventPort int

var tlsCertificate string
var tlsServerHostOverride string
if ok {
host, _ = mm["host"].(string)
port, _ = mm["port"].(int)
eventHost, _ = mm["event_host"].(string)
eventPort, _ = mm["event_port"].(int)
tlsCertificate, _ = mm["tls"].(map[string]interface{})["certificate"].(string)
tlsServerHostOverride, _ = mm["tls"].(map[string]interface{})["serverhostoverride"].(string)

} else {
mm1 := value.(map[interface{}]interface{})
host, _ = mm1["host"].(string)
port, _ = mm1["port"].(int)
eventHost, _ = mm1["event_host"].(string)
eventPort, _ = mm1["event_port"].(int)
tlsCertificate, _ = mm1["tls"].(map[string]interface{})["certificate"].(string)
tlsServerHostOverride, _ = mm1["tls"].(map[string]interface{})["serverhostoverride"].(string)

}
p := PeerConfig{Host: host, Port: strconv.Itoa(port), EventHost: eventHost, EventPort: strconv.Itoa(eventPort)}

p := PeerConfig{Host: host, Port: strconv.Itoa(port), EventHost: eventHost, EventPort: strconv.Itoa(eventPort),
TLSCertificate: tlsCertificate, TLSServerHostOverride: tlsServerHostOverride}
if p.Host == "" {
panic(fmt.Sprintf("host key not exist or empty for %s", key))
}
Expand All @@ -132,6 +144,11 @@ func GetPeersConfig() []PeerConfig {
if p.EventPort == "" {
panic(fmt.Sprintf("event_port not exist or empty for %s", key))
}
if IsTLSEnabled() && p.TLSCertificate == "" {
panic(fmt.Sprintf("tls.certificate not exist or empty for %s", key))
}

p.TLSCertificate = path.Join(os.Getenv("GOPATH"), p.TLSCertificate)
peersConfig = append(peersConfig, p)
}
return peersConfig
Expand All @@ -144,21 +161,18 @@ func IsTLSEnabled() bool {
}

// GetTLSCACertPool ...
func GetTLSCACertPool() *x509.CertPool {
func GetTLSCACertPool(tlsCertificate string) (*x509.CertPool, error) {
certPool := x509.NewCertPool()
if myViper.GetString("client.tls.certificate") != "" {
rawData, err := ioutil.ReadFile(myViper.GetString("tls.certificate"))
if tlsCertificate != "" {
rawData, err := ioutil.ReadFile(tlsCertificate)
if err != nil {
panic(err)
return nil, err
}

certPool.AddCert(loadCAKey(rawData))
}
return certPool
}

// GetTLSServerHostOverride ...
func GetTLSServerHostOverride() string {
return myViper.GetString("client.tls.serverhostoverride")
return certPool, nil
}

// IsSecurityEnabled ...
Expand Down Expand Up @@ -187,6 +201,21 @@ func GetOrdererHost() string {
return myViper.GetString("client.orderer.host")
}

// GetOrdererPort ...
func GetOrdererPort() string {
return strconv.Itoa(myViper.GetInt("client.orderer.port"))
}

// GetOrdererTLSServerHostOverride ...
func GetOrdererTLSServerHostOverride() string {
return myViper.GetString("client.orderer.tls.serverhostoverride")
}

// GetOrdererTLSCertificate ...
func GetOrdererTLSCertificate() string {
return path.Join(os.Getenv("GOPATH"), myViper.GetString("client.orderer.tls.certificate"))
}

// GetFabricCAID ...
func GetFabricCAID() string {
return myViper.GetString("client.fabricCA.id")
Expand Down Expand Up @@ -216,11 +245,6 @@ func GetKeyStorePath() string {
return myViper.GetString("client.keystore.path")
}

// GetOrdererPort ...
func GetOrdererPort() string {
return strconv.Itoa(myViper.GetInt("client.orderer.port"))
}

// loadCAKey
func loadCAKey(rawData []byte) *x509.Certificate {
block, _ := pem.Decode(rawData)
Expand Down
12 changes: 3 additions & 9 deletions fabric-ca-client/fabricca.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.
package fabricca

import (
"encoding/base64"
"fmt"
"os"

Expand Down Expand Up @@ -120,11 +119,11 @@ func (fabricCAServices *services) Enroll(enrollmentID string, enrollmentSecret s
Name: enrollmentID,
Secret: enrollmentSecret,
}
id, err := fabricCAServices.fabricCAClient.Enroll(req)
enrollmentResponse, err := fabricCAServices.fabricCAClient.Enroll(req)
if err != nil {
return nil, nil, fmt.Errorf("Enroll failed: %s", err)
}
return id.GetECert().Key(), id.GetECert().Cert(), nil
return enrollmentResponse.Identity.GetECert().Key(), enrollmentResponse.Identity.GetECert().Cert(), nil
}

// Register a User with the Fabric CA
Expand Down Expand Up @@ -160,13 +159,8 @@ func (fabricCAServices *services) Register(registrar fabricclient.User,
if err != nil {
return "", fmt.Errorf("Error Registering User: %s", err.Error())
}
// Decode enrolment secret
secret, err := base64.StdEncoding.DecodeString(response.Secret)
if err != nil {
return "", fmt.Errorf("Error decoding enrolment secret: %s", err.Error())
}

return string(secret), nil
return response.Secret, nil
}

// Revoke a User with the Fabric CA
Expand Down
5 changes: 3 additions & 2 deletions fabric-client/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type TransactionProposal struct {
type TransactionProposalResponse struct {
Endorser string
Err error
Status int32

proposal *TransactionProposal
proposalResponse *pb.ProposalResponse
Expand Down Expand Up @@ -141,10 +142,10 @@ type SignedEnvelope struct {
*/
func NewChain(name string, client Client) (Chain, error) {
if name == "" {
return nil, fmt.Errorf("Failed to create Chain. Missing requirement 'name' parameter.")
return nil, fmt.Errorf("failed to create Chain. Missing required 'name' parameter")
}
if client == nil {
return nil, fmt.Errorf("Failed to create Chain. Missing requirement 'clientContext' parameter.")
return nil, fmt.Errorf("failed to create Chain. Missing required 'clientContext' parameter")
}
p := make(map[string]Peer)
o := make(map[string]Orderer)
Expand Down
4 changes: 2 additions & 2 deletions fabric-client/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func TestChainMethods(t *testing.T) {
if err == nil {
t.Fatalf("NewChain didn't return error")
}
if err.Error() != "Failed to create Chain. Missing requirement 'name' parameter." {
if err.Error() != "failed to create Chain. Missing required 'name' parameter" {
t.Fatalf("NewChain didn't return right error")
}

_, err = NewChain("testChain", nil)
if err == nil {
t.Fatalf("NewChain didn't return error")
}
if err.Error() != "Failed to create Chain. Missing requirement 'clientContext' parameter." {
if err.Error() != "failed to create Chain. Missing required 'clientContext' parameter" {
t.Fatalf("NewChain didn't return right error")
}

Expand Down
26 changes: 16 additions & 10 deletions fabric-client/events/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ type EventsClient interface {

type eventsClient struct {
sync.RWMutex
peerAddress string
regTimeout time.Duration
stream ehpb.Events_ChatClient
adapter consumer.EventAdapter
peerAddress string
regTimeout time.Duration
stream ehpb.Events_ChatClient
adapter consumer.EventAdapter
TLSCertificate string
TLSServerHostOverride string
}

//NewEventsClient Returns a new grpc.ClientConn to the configured local PEER.
func NewEventsClient(peerAddress string, regTimeout time.Duration, adapter consumer.EventAdapter) (EventsClient, error) {
func NewEventsClient(peerAddress string, certificate string, serverhostoverride string, regTimeout time.Duration, adapter consumer.EventAdapter) (EventsClient, error) {
var err error
if regTimeout < 100*time.Millisecond {
regTimeout = 100 * time.Millisecond
Expand All @@ -61,15 +63,19 @@ func NewEventsClient(peerAddress string, regTimeout time.Duration, adapter consu
regTimeout = 60 * time.Second
err = fmt.Errorf("regTimeout > 60, setting to 60 sec")
}
return &eventsClient{sync.RWMutex{}, peerAddress, regTimeout, nil, adapter}, err
return &eventsClient{sync.RWMutex{}, peerAddress, regTimeout, nil, adapter, certificate, serverhostoverride}, err
}

//newEventsClientConnectionWithAddress Returns a new grpc.ClientConn to the configured local PEER.
func newEventsClientConnectionWithAddress(peerAddress string) (*grpc.ClientConn, error) {
func newEventsClientConnectionWithAddress(peerAddress string, certificate string, serverhostoverride string) (*grpc.ClientConn, error) {
var opts []grpc.DialOption
opts = append(opts, grpc.WithTimeout(time.Second*3))
if config.IsTLSEnabled() {
creds := credentials.NewClientTLSFromCert(config.GetTLSCACertPool(), config.GetTLSServerHostOverride())
tlsCaCertPool, err := config.GetTLSCACertPool(certificate)
if err != nil {
return nil, err
}
creds := credentials.NewClientTLSFromCert(tlsCaCertPool, serverhostoverride)
opts = append(opts, grpc.WithTransportCredentials(creds))
} else {
opts = append(opts, grpc.WithInsecure())
Expand Down Expand Up @@ -217,9 +223,9 @@ func (ec *eventsClient) processEvents() error {

//Start establishes connection with Event hub and registers interested events with it
func (ec *eventsClient) Start() error {
conn, err := newEventsClientConnectionWithAddress(ec.peerAddress)
conn, err := newEventsClientConnectionWithAddress(ec.peerAddress, ec.TLSCertificate, ec.TLSServerHostOverride)
if err != nil {
return fmt.Errorf("Could not create client conn to %s", ec.peerAddress)
return fmt.Errorf("Could not create client conn to %s (%v)", ec.peerAddress, err)
}

ies, err := ec.adapter.GetInterestedEvents()
Expand Down
Loading

0 comments on commit da93bdc

Please sign in to comment.