-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this was done for code found in fabric-sdk-go/pkg/context/api/core/network.go also renamed urlutil package to endpoint and moved TLSConfig impl in there Change-Id: I179589a09d9ebc320d2a02558d250a4249f97808 Signed-off-by: Baha Shaaban <baha.shaaban@securekey.com>
- Loading branch information
Baha Shaaban
committed
Mar 9, 2018
1 parent
c2e04b5
commit c9bd65a
Showing
17 changed files
with
373 additions
and
160 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
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,116 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package endpoint | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"regexp" | ||
|
||
"github.com/hyperledger/fabric-sdk-go/pkg/errors/status" | ||
"github.com/hyperledger/fabric-sdk-go/pkg/logging" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var logger = logging.NewLogger("fabsdk/core") | ||
|
||
// IsTLSEnabled is a generic function that expects a URL and verifies if it has | ||
// a prefix HTTPS or GRPCS to return true for TLS Enabled URLs or false otherwise | ||
func IsTLSEnabled(url string) bool { | ||
tlsURL := strings.ToLower(url) | ||
if strings.HasPrefix(tlsURL, "https://") || strings.HasPrefix(tlsURL, "grpcs://") { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// ToAddress is a utility function to trim the GRPC protocol prefix as it is not needed by GO | ||
// if the GRPC protocol is not found, the url is returned unchanged | ||
func ToAddress(url string) string { | ||
if strings.HasPrefix(url, "grpc://") { | ||
return strings.TrimPrefix(url, "grpc://") | ||
} | ||
if strings.HasPrefix(url, "grpcs://") { | ||
return strings.TrimPrefix(url, "grpcs://") | ||
} | ||
return url | ||
} | ||
|
||
//AttemptSecured is a utility function which verifies URL and returns if secured connections needs to established | ||
// for protocol 'grpcs' in URL returns true | ||
// for protocol 'grpc' in URL returns false | ||
// for no protocol mentioned, returns !allowInSecure | ||
func AttemptSecured(url string, allowInSecure bool) bool { | ||
ok, err := regexp.MatchString(".*(?i)s://", url) | ||
if ok && err == nil { | ||
return true | ||
} else if strings.Contains(url, "://") { | ||
return false | ||
} else { | ||
return !allowInSecure | ||
} | ||
} | ||
|
||
// TLSConfig TLS configuration used in the sdk's configs. | ||
type TLSConfig struct { | ||
// the following two fields are interchangeable. | ||
// If Path is available, then it will be used to load the cert | ||
// if Pem is available, then it has the raw data of the cert it will be used as-is | ||
// Certificate root certificate path | ||
Path string | ||
// Certificate actual content | ||
Pem string | ||
} | ||
|
||
// Bytes returns the tls certificate as a byte array by loading it either from the embedded Pem or Path | ||
func (cfg TLSConfig) Bytes() ([]byte, error) { | ||
var bytes []byte | ||
var err error | ||
|
||
if cfg.Pem != "" { | ||
bytes = []byte(cfg.Pem) | ||
} else if cfg.Path != "" { | ||
bytes, err = ioutil.ReadFile(cfg.Path) | ||
|
||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to load pem bytes from path %s", cfg.Path) | ||
} | ||
} | ||
|
||
return bytes, nil | ||
} | ||
|
||
// TLSCert returns the tls certificate as a *x509.Certificate by loading it either from the embedded Pem or Path | ||
func (cfg TLSConfig) TLSCert() (*x509.Certificate, error) { | ||
bytes, err := cfg.Bytes() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return loadCert(bytes) | ||
} | ||
|
||
// loadCAKey | ||
func loadCert(rawData []byte) (*x509.Certificate, error) { | ||
block, _ := pem.Decode(rawData) | ||
|
||
if block != nil { | ||
pub, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "certificate parsing failed") | ||
} | ||
|
||
return pub, nil | ||
} | ||
|
||
// return an error with an error code for clients to test against status.EmptyCert code | ||
return nil, status.New(status.ClientStatus, status.EmptyCert.ToInt32(), "pem data missing", nil) | ||
} |
Oops, something went wrong.