Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit f853354

Browse files
author
Baha Shaaban
committed
[FAB-7307]Config in []byte & embed cert in config
This change introduces 'ConfigBytes' and 'ConfigType' in the SDK options to allow loading configs from bytes array as opposed to from a file path. The function InitConfigFromBytes has been introduced in pkg/config/config.go to load the configs from the bytes array argument. Since viper needs to know the type of config being passed to it, 'ConfigType' was added for this purpose. This change also introduces the embedding of certs/keys in the sdk's config directly as opposed to passing file path values in the configs. Embedded certs/keys should always be set under 'pem' fields while files will remain under 'path' fields. Look at test/fixtures/config/config_test_pem.yaml for a sample config with embedded certs/keys. Three new functions were added to the Config interface in api/apiconfig/configprovider.go to support CA server certs/keys. These three functions are 'CAClientKeyPem', 'CAClientCertPem' and 'CAServerCertPems'. The client must decide if they want to use the Pem or the Path version of these functions and must set the config accordingly. The configs will load everything found in the byte array (or config file). If it has both 'pem' and 'path', then both will be loaded. Change-Id: I858623b0aa98e1000ed3c86edb2e136a831933ed Signed-off-by: Baha Shaaban <baha.shaaban@securekey.com>
1 parent 9dad8ae commit f853354

File tree

12 files changed

+886
-25
lines changed

12 files changed

+886
-25
lines changed

api/apiconfig/configprovider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import (
1515
type Config interface {
1616
Client() (*ClientConfig, error)
1717
CAConfig(org string) (*CAConfig, error)
18+
CAServerCertPems(org string) ([]string, error)
1819
CAServerCertFiles(org string) ([]string, error)
20+
CAClientKeyPem(org string) (string, error)
1921
CAClientKeyFile(org string) (string, error)
22+
CAClientCertPem(org string) (string, error)
2023
CAClientCertFile(org string) (string, error)
2124
TimeoutOrDefault(TimeoutType) time.Duration
2225
MspID(org string) (string, error)

api/apiconfig/mocks/mockconfig.gen.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/apiconfig/network.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,15 @@ type TLSConfig struct {
135135

136136
// MutualTLSConfig Mutual TLS configurations
137137
type MutualTLSConfig struct {
138+
Pem []string
138139
// Certfiles root certificates for TLS validation (Comma serparated path list)
139140
Path string
140141
// Client client TLS information
141142
Client struct {
143+
KeyPem string
142144
// Keyfile client key path
143145
Keyfile string
146+
CertPem string
144147
// Certfile client cert path
145148
Certfile string
146149
}

def/fabapi/context/defprovider/sdk.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func NewDefaultProviderFactory() *DefaultProviderFactory {
3333

3434
// NewConfigProvider creates a Config using the SDK's default implementation
3535
func (f *DefaultProviderFactory) NewConfigProvider(o opt.ConfigOpts, a opt.SDKOpts) (apiconfig.Config, error) {
36+
// configBytes takes precedence over configFile
37+
if a.ConfigBytes != nil && len(a.ConfigBytes) > 0 {
38+
return configImpl.InitConfigFromBytes(a.ConfigBytes, a.ConfigType)
39+
}
3640
return configImpl.InitConfig(a.ConfigFile)
3741
}
3842

def/fabapi/fabapi.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
type Options struct {
3030
// Quick access options
3131
ConfigFile string
32+
ConfigByte []byte
33+
ConfigType string
3234

3335
// Options for default providers
3436
ConfigOpts opt.ConfigOpts
@@ -85,7 +87,9 @@ type ProviderInit interface {
8587
func NewSDK(options Options) (*FabricSDK, error) {
8688
// Construct SDK opts from the quick access options in setup
8789
sdkOpts := opt.SDKOpts{
88-
ConfigFile: options.ConfigFile,
90+
ConfigFile: options.ConfigFile,
91+
ConfigBytes: options.ConfigByte,
92+
ConfigType: options.ConfigType,
8993
}
9094

9195
sdk := FabricSDK{

def/fabapi/fabapi_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
77
package fabapi
88

99
import (
10+
"os"
1011
"testing"
1112

1213
"github.com/hyperledger/fabric-sdk-go/def/fabapi/opt"
@@ -190,3 +191,67 @@ func TestNewDefaultTwoValidSDK(t *testing.T) {
190191
t.Fatalf("Failed to create new 'orgchannel' channel client: %s", err)
191192
}
192193
}
194+
195+
func TestNewDefaultSDKFromByte(t *testing.T) {
196+
cBytes, err := loadConfigBytesFromFile(t, "../../test/fixtures/config/config_test.yaml")
197+
if err != nil {
198+
t.Fatalf("Failed to load sample bytes from File. Error: %s", err)
199+
}
200+
setup := Options{
201+
ConfigByte: cBytes,
202+
ConfigType: "yaml",
203+
StateStoreOpts: opt.StateStoreOpts{
204+
Path: "/tmp/state",
205+
},
206+
}
207+
208+
sdk, err := NewSDK(setup)
209+
if err != nil {
210+
t.Fatalf("Error initializing SDK: %s", err)
211+
}
212+
213+
if sdk == nil {
214+
t.Fatalf("SDK should not be empty when initialized")
215+
}
216+
217+
setup = Options{
218+
ConfigByte: cBytes,
219+
ConfigType: "json",
220+
StateStoreOpts: opt.StateStoreOpts{
221+
Path: "/tmp/state",
222+
},
223+
}
224+
225+
defer func() {
226+
if r := recover(); r == nil {
227+
t.Errorf("The code did not panic")
228+
}
229+
}()
230+
231+
// new SDK expected to panic due to wrong config type which didn't load the configs
232+
NewSDK(setup)
233+
234+
}
235+
236+
func loadConfigBytesFromFile(t *testing.T, filePath string) ([]byte, error) {
237+
// read test config file into bytes array
238+
f, err := os.Open(filePath)
239+
if err != nil {
240+
t.Fatalf("Failed to read config file. Error: %s", err)
241+
}
242+
defer f.Close()
243+
fi, err := f.Stat()
244+
if err != nil {
245+
t.Fatalf("Failed to read config file stat. Error: %s", err)
246+
}
247+
s := fi.Size()
248+
cBytes := make([]byte, s, s)
249+
n, err := f.Read(cBytes)
250+
if err != nil {
251+
t.Fatalf("Failed to read test config for bytes array testing. Error: %s", err)
252+
}
253+
if n == 0 {
254+
t.Fatalf("Failed to read test config for bytes array testing. Mock bytes array is empty")
255+
}
256+
return cBytes, err
257+
}

def/fabapi/opt/opt.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@ package opt
88

99
// SDKOpts provides bootstrap setup
1010
type SDKOpts struct {
11+
//ConfigFile to load from a predefined path
1112
ConfigFile string
13+
//ConfigBytes to load from an bytes array
14+
ConfigBytes []byte
15+
//ConfigType to specify the type of the config (mainly used with ConfigBytes as ConfigFile has a file extension to specify the type)
16+
// valid values: yaml, json, etc.
17+
ConfigType string
1218
}
1319

1420
// ConfigOpts provides setup parameters for Config

0 commit comments

Comments
 (0)