Skip to content

Commit

Permalink
Merge pull request #126 from s7techlab/identity-config
Browse files Browse the repository at this point in the history
identity config + observer
  • Loading branch information
vitiko authored Sep 22, 2023
2 parents 721cc29 + a9586a2 commit 432df04
Show file tree
Hide file tree
Showing 68 changed files with 4,527 additions and 1,164 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
# working-directory: somedir

# Optional: golangci-lint command line arguments.
args: --exclude SA1019
args: --exclude SA1019 --timeout=10m

# Optional: show only new issues if it's a pull request. The default value is `false`.
only-new-issues: true
Expand Down
28 changes: 0 additions & 28 deletions client/testing/channels_fetcher_mock.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/hyperledger/fabric-protos-go v0.0.0-20201028172056-a3136dde2354
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mitchellh/mapstructure v1.2.2
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.9.0
github.com/pelletier/go-toml v1.4.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/mozilla/tls-observatory v0.0.0-20180409132520-8791a200eb40/go.mod h1:SrKMQvPiws7F7iqYp8/TX+IhxCYhzr6N/1yb8cwHsGk=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
Expand Down
101 changes: 101 additions & 0 deletions identity/config/identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package config

import (
"errors"

"github.com/s7techlab/hlf-sdk-go/api"
"github.com/s7techlab/hlf-sdk-go/identity"
)

var (
ErrMSPIDEmpty = errors.New(`MSP ID is empty`)
ErrMSPPathEmpty = errors.New(`MSP path is empty`)

ErrMSPSignCertPathEmpty = errors.New(`MSP signcert path is empty`)
ErrMSPSignKeyPathEmpty = errors.New(`MSP signkey path is empty`)

ErrMSPSignCertEmpty = errors.New(`MSP signcert is empty`)
ErrMSPSignKeyEmpty = errors.New(`MSP signkey is empty`)

ErrSignerNotFound = errors.New(`signer not found`)
)

type (
MSP struct {
ID string `yaml:"id"`
Path string `yaml:"path"`

// SignCertPath and SignKeyPath take precedence over Path. If they are present, Path will be ignored
SignCertPath string `yaml:"signcert_path"`
SignKeyPath string `yaml:"signkey_path"`

// if SignCert and SignKey are present, Path, SignCertPath and SignKeyPath will be ignored
SignCert []byte `yaml:"signcert"`
SignKey []byte `yaml:"signkey"`
}
)

func (m MSP) MustSigner() api.Identity {
signer, err := m.Signer()
if err != nil {
panic(err)
}

return signer
}

func (m MSP) Signer() (api.Identity, error) {
mspConfig, err := m.MSP(identity.WithSkipConfig())
if err != nil {
return nil, err
}

signer := mspConfig.Signer()
if signer == nil {
return nil, ErrSignerNotFound
}

return signer, nil
}

func (m MSP) MSP(opts ...identity.MSPOpt) (identity.MSP, error) {
if m.ID == `` {
return nil, ErrMSPIDEmpty
}

// cert and key contents take precedence over Path and cert and key paths
if len(m.SignCert) != 0 || len(m.SignKey) != 0 {
if len(m.SignCert) == 0 {
return nil, ErrMSPSignCertEmpty
}

if len(m.SignKey) == 0 {
return nil, ErrMSPSignKeyEmpty
}

opts = append(opts, identity.WithSignCert(m.SignCert), identity.WithSignKey(m.SignKey))

return identity.MSPFromPath(m.ID, "", opts...)
}

// cert and key paths take precedence over Path
if m.SignCertPath != `` || m.SignKeyPath != `` {
if m.SignCertPath == `` {
return nil, ErrMSPSignCertPathEmpty
}

if m.SignKeyPath == `` {
return nil, ErrMSPSignKeyPathEmpty
}

opts = append(opts, identity.WithSignCertPath(m.SignCertPath), identity.WithSignKeyPath(m.SignKeyPath))

return identity.MSPFromPath(m.ID, "", opts...)
}

if m.Path == `` {
return nil, ErrMSPPathEmpty
}

return identity.MSPFromPath(m.ID, m.Path, opts...)
}
2 changes: 1 addition & 1 deletion identity/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func Certificate(certRaw []byte) (*x509.Certificate, error) {
return cert, nil
}

// Key parses raw key btes
// Key parses raw key bytes
func Key(keyRaw []byte) (interface{}, error) {
keyPEM, _ := pem.Decode(keyRaw)
if keyPEM == nil {
Expand Down
71 changes: 66 additions & 5 deletions identity/msp.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,21 @@ type (
MSPOpts struct {
mspPath string

// signCert and signKey take precedence over signCertPath and signKeyPath
signCert []byte
signKey []byte

signCertPath string
signKeyPath string

signCertsPath string
keystorePath string
adminCertsPath string
adminMSPPath string

userPaths []string

skipConfig bool
validateCertChain bool
logger *zap.Logger
}
Expand All @@ -59,7 +67,6 @@ type (
)

func applyDefaultMSPPaths(mspOpts *MSPOpts) {

if mspOpts.adminCertsPath == `` {
mspOpts.adminCertsPath = AdminCertsPath(mspOpts.mspPath)
}
Expand Down Expand Up @@ -97,12 +104,51 @@ func MSPFromConfig(fabricMspConfig *mspproto.FabricMSPConfig) (*MSPConfig, error
return mspConfig, nil
}

func WithSkipConfig() MSPOpt {
return func(mspOpts *MSPOpts) {
mspOpts.skipConfig = true
}
}

func WithAdminMSPPath(adminMSPPath string) MSPOpt {
return func(mspOpts *MSPOpts) {
mspOpts.adminMSPPath = adminMSPPath
}
}

func WithSignCertPath(signCertPath string) MSPOpt {
return func(mspOpts *MSPOpts) {
mspOpts.signCertPath = signCertPath
}
}

func WithSignKeyPath(signKeyPath string) MSPOpt {
return func(mspOpts *MSPOpts) {
mspOpts.signKeyPath = signKeyPath
}
}

func WithSignCert(signCert []byte) MSPOpt {
return func(mspOpts *MSPOpts) {
mspOpts.signCert = signCert
}
}

func WithSignKey(signKey []byte) MSPOpt {
return func(mspOpts *MSPOpts) {
mspOpts.signKey = signKey
}
}

func MustMSPFromPath(mspID, mspPath string, opts ...MSPOpt) *MSPConfig {
mspConfig, err := MSPFromPath(mspID, mspPath, opts...)
if err != nil {
panic(err)
}

return mspConfig
}

// MSPFromPath loads msp config from filesystem
func MSPFromPath(mspID, mspPath string, opts ...MSPOpt) (*MSPConfig, error) {
var err error
Expand All @@ -124,6 +170,18 @@ func MSPFromPath(mspID, mspPath string, opts ...MSPOpt) (*MSPConfig, error) {

mspConfig := &MSPConfig{}

if len(mspOpts.signCert) != 0 && len(mspOpts.signKey) != 0 {
mspConfig.signer, err = FromBytes(mspID, mspOpts.signCert, mspOpts.signKey)
if err != nil {
return nil, err
}
} else if mspOpts.signCertPath != "" && mspOpts.signKeyPath != "" {
mspConfig.signer, err = FromCertKeyPath(mspID, mspOpts.signCertPath, mspOpts.signKeyPath)
if err != nil {
return nil, err
}
}

// admin in separate msp path
if mspOpts.adminMSPPath != `` {
logger.Debug(`load admin identities from separate msp path`,
Expand All @@ -148,7 +206,8 @@ func MSPFromPath(mspID, mspPath string, opts ...MSPOpt) (*MSPConfig, error) {

if len(mspOpts.userPaths) > 0 {
for _, userPath := range mspOpts.userPaths {
users, err := ListFromPath(mspID, userPath, mspOpts.keystorePath)
var users []api.Identity
users, err = ListFromPath(mspID, userPath, mspOpts.keystorePath)
// usePaths set explicit, so if dir is not exists - error occurred
if err != nil {
return nil, fmt.Errorf(`read users identity from=%s: %w`, userPath, err)
Expand All @@ -160,15 +219,17 @@ func MSPFromPath(mspID, mspPath string, opts ...MSPOpt) (*MSPConfig, error) {
logger.Debug(`user identities loaded`, zap.Int(`num`, len(mspConfig.users)))
}

if mspOpts.signCertsPath != `` {
if mspOpts.signCertsPath != `` && mspConfig.signer == nil {
mspConfig.signer, err = FirstFromPath(mspID, mspOpts.signCertsPath, mspOpts.keystorePath)
if err != nil {
return nil, fmt.Errorf(`read signer identity from=%s: %w`, mspOpts.signCertsPath, err)
}
}

if mspConfig.mspConfig, err = FabricMSPConfigFromPath(mspID, mspOpts.mspPath); err != nil {
return nil, err
if !mspOpts.skipConfig {
if mspConfig.mspConfig, err = FabricMSPConfigFromPath(mspID, mspOpts.mspPath); err != nil {
return nil, err
}
}

if mspOpts.validateCertChain {

Check failure on line 235 in identity/msp.go

View workflow job for this annotation

GitHub Actions / lint (1.18.x, ubuntu-latest)

SA9003: empty branch (staticcheck)
Expand Down
2 changes: 1 addition & 1 deletion identity/msp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var _ = Describe(`Cert`, func() {
})
})

Context(`Peer from FabricMSPCofig`, func() {
Context(`Peer from FabricMSPConfig`, func() {

It(`allow to create msp from FabricMSPConfig`, func() {
msp, err := identity.MSPFromConfig(Org1MSPPeer.FabricMSPConfig())
Expand Down
8 changes: 8 additions & 0 deletions observer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Observer: Observing blocks and events for particular channel or peer

Main features:

* Block parsing to components (transactions, events, states etc)
* Auto reconnection when block or event stream interrupted
* Block and event transformation if needed

Loading

0 comments on commit 432df04

Please sign in to comment.