func (c *config) getPeerClientCert() (string, errors.Error) {
	clientCertLocation := c.peerConfig.GetString("peer.tls.clientCert.file")
	if clientCertLocation == "" {
		clientCertLocation = c.peerConfig.GetString("peer.tls.cert.file")
	}
	fileData, err := ioutil.ReadFile(c.translatePeerPath(clientCertLocation))
	if err != nil {
		return "", errors.WithMessage(errors.SystemError, err, "Failed to read peer's tls client cert file")
	}
	return string(fileData), nil
}
getPeerClientCert returns client tls cert
package config
import (
	"bytes"
	"io/ioutil"
	"path/filepath"
	"strings"
	"time"
	logging "github.com/hyperledger/fabric-sdk-go/pkg/common/logging"
	configmanagerApi "github.com/securekey/fabric-snaps/configmanager/api"
	configmgmtService "github.com/securekey/fabric-snaps/configmanager/pkg/service"
	httpsnapApi "github.com/securekey/fabric-snaps/httpsnap/api"
	"github.com/securekey/fabric-snaps/util/configcache"
	"github.com/securekey/fabric-snaps/util/errors"
	"github.com/spf13/viper"
)
const (
	peerConfigFileName  = "core"
	cmdRootPrefix       = "core"
	defaultTimeout      = time.Second * 5
	defaultCacheRefresh = time.Minute * 180
)
var logger = logging.NewLogger("httpsnap")
var defaultLogLevel = "info"
var peerConfigCache = configcache.New(peerConfigFileName, cmdRootPrefix, "/etc/hyperledger/fabric")
const FilePathSeparator = string(filepath.Separator)
type config struct {
	peerConfig     *viper.Viper
	httpSnapConfig *viper.Viper
	peerConfigPath string
	clientTLS     map[string]*httpsnapApi.ClientTLS
	headers       map[string]bool
	schemaConfigs map[string]*httpsnapApi.SchemaConfig
}
func NewConfig(peerConfigPath string, channelID string) (httpsnapApi.Config, bool, error) {
	peerConfig, err := peerConfigCache.Get(peerConfigPath)
	if err != nil {
		return nil, false, err
	}
	key := configmanagerApi.ConfigKey{MspID: peerConfig.GetString("peer.localMspId"), PeerID: peerConfig.GetString("peer.id"), AppName: "httpsnap"}
	cacheInstance := configmgmtService.GetInstance()
	if cacheInstance == nil {
		return nil, false, errors.New(errors.SystemError, "Cannot create cache instance")
	}
	configData, dirty, err := cacheInstance.Get(channelID, key)
	if err != nil {
		return nil, false, errors.WithMessage(errors.SystemError, err, "Failed cacheInstance")
	}
	if configData == nil {
		return nil, false, errors.New(errors.InitializeConfigError, "config data is empty")
	}
	httpSnapConfig := viper.New()
	httpSnapConfig.SetConfigType("YAML")
	err = httpSnapConfig.ReadConfig(bytes.NewBuffer(configData))
	if err != nil {
		return nil, false, errors.WithMessage(errors.InitializeConfigError, err, "snap_config_init_error")
	}
	httpSnapConfig.SetEnvPrefix(cmdRootPrefix)
	httpSnapConfig.AutomaticEnv()
	httpSnapConfig.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
	c := &config{peerConfig: peerConfig, httpSnapConfig: httpSnapConfig, peerConfigPath: peerConfigPath}
	err = c.preloadEntities()
	if err != nil {
		return nil, false, err
	}
	if dirty {
		err = c.initializeLogging()
		if err != nil {
			return nil, false, err
		}
	}
	return c, dirty, nil
}
func (c *config) initializeLogging() errors.Error {
	logLevel := c.httpSnapConfig.GetString("logging.level")
	if logLevel == "" {
		logLevel = defaultLogLevel
	}
	level, err := logging.LogLevel(logLevel)
	if err != nil {
		return errors.WithMessage(errors.InitializeLoggingError, err, "Error initializing log level")
	}
	logging.SetLevel("httpsnap", level)
	logger.Debugf("Httpsnap logging initialized. Log level: %s", logLevel)
	return nil
}
func (c *config) GetConfigPath(path string) string {
	basePath := filepath.Dir(c.httpSnapConfig.ConfigFileUsed())
	if filepath.IsAbs(path) {
		return path
	}
	return filepath.Join(basePath, path)
}
func (c *config) IsHeaderAllowed(name string) bool {
	val := c.headers[strings.ToLower(name)]
	return val
}
func (c *config) IsKeyCacheEnabled() bool {
	return c.httpSnapConfig.GetBool("cache.keycache.enabled")
}
func (c *config) KeyCacheRefreshInterval() time.Duration {
	refresh := c.httpSnapConfig.GetDuration("cache.keycache.refresh")
	if refresh == 0 {
		return defaultCacheRefresh
	}
	return refresh
}
func (c *config) GetCaCerts() ([]string, errors.Error) {
	caCerts := c.httpSnapConfig.GetStringSlice("tls.caCerts")
	absoluteCaCerts := make([]string, 0, len(caCerts))
	absoluteCaCerts = append(absoluteCaCerts, caCerts...)
	if len(absoluteCaCerts) == 0 && c.IsPeerTLSConfigEnabled() {
		return c.getPeerTLSRootCert()
	}
	return absoluteCaCerts, nil
}
func (c *config) GetClientCert() (string, errors.Error) {
	clientCert := c.httpSnapConfig.GetString("tls.clientCert")
	if clientCert == "" && c.IsPeerTLSConfigEnabled() {
		return c.getPeerClientCert()
	}
	return clientCert, nil
}
func (c *config) GetPeerClientKey() (string, errors.Error) {
	clientKeyLocation := c.peerConfig.GetString("peer.tls.clientKey.file")
	if clientKeyLocation == "" {
		clientKeyLocation = c.peerConfig.GetString("peer.tls.key.file")
	}
	fileData, err := ioutil.ReadFile(c.translatePeerPath(clientKeyLocation))
	if err != nil {
		return "", errors.WithMessage(errors.SystemError, err, "Failed to read peer's tls client key file")
	}
	return string(fileData), nil
}
func (c *config) IsSystemCertPoolEnabled() bool {
	return c.httpSnapConfig.GetBool("tls.enableSystemCertPool")
}
func (c *config) IsPeerTLSConfigEnabled() bool {
	return c.httpSnapConfig.GetBool("tls.allowPeerConfig")
}