Remove panic() and handle it appropriately (#5807)

This is an effort to remove panic from the source. 
Add a new call called CriticialIf, that calls LogIf and exits. 
Replace panics with one of CriticalIf, FatalIf and a return of error.
This commit is contained in:
ebozduman
2018-04-19 17:24:43 -07:00
committed by kannappanr
parent 846f3e8f59
commit f16bfda2f2
21 changed files with 129 additions and 128 deletions

View File

@@ -81,30 +81,37 @@ func (cred Credentials) Equal(ccred Credentials) bool {
return cred.AccessKey == ccred.AccessKey && subtle.ConstantTimeCompare([]byte(cred.SecretKey), []byte(ccred.SecretKey)) == 1
}
// MustGetNewCredentials generates and returns new credential.
func MustGetNewCredentials() (cred Credentials) {
readBytes := func(size int) (data []byte) {
// GetNewCredentials generates and returns new credential.
func GetNewCredentials() (cred Credentials, err error) {
readBytes := func(size int) (data []byte, err error) {
data = make([]byte, size)
if n, err := rand.Read(data); err != nil {
panic(err)
var n int
if n, err = rand.Read(data); err != nil {
return nil, err
} else if n != size {
panic(fmt.Errorf("not enough data read. expected: %v, got: %v", size, n))
return nil, fmt.Errorf("Not enough data. Expected to read: %v bytes, got: %v bytes", size, n)
}
return
return data, nil
}
// Generate access key.
keyBytes := readBytes(accessKeyMaxLen)
keyBytes, err := readBytes(accessKeyMaxLen)
if err != nil {
return cred, err
}
for i := 0; i < accessKeyMaxLen; i++ {
keyBytes[i] = alphaNumericTable[keyBytes[i]%alphaNumericTableLen]
}
cred.AccessKey = string(keyBytes)
// Generate secret key.
keyBytes = readBytes(secretKeyMaxLen)
keyBytes, err = readBytes(secretKeyMaxLen)
if err != nil {
return cred, err
}
cred.SecretKey = string([]byte(base64.StdEncoding.EncodeToString(keyBytes))[:secretKeyMaxLen])
return cred
return cred, nil
}
// CreateCredentials returns new credential with the given access key and secret key.

View File

@@ -54,8 +54,11 @@ func TestIsSecretKeyValid(t *testing.T) {
}
}
func TestMustGetNewCredentials(t *testing.T) {
cred := MustGetNewCredentials()
func TestGetNewCredentials(t *testing.T) {
cred, err := GetNewCredentials()
if err != nil {
t.Fatalf("Failed to get a new credential")
}
if !cred.IsValid() {
t.Fatalf("Failed to get new valid credential")
}
@@ -106,7 +109,14 @@ func TestCreateCredentials(t *testing.T) {
}
func TestCredentialsEqual(t *testing.T) {
cred := MustGetNewCredentials()
cred, err := GetNewCredentials()
if err != nil {
t.Fatalf("Failed to get a new credential")
}
cred2, err := GetNewCredentials()
if err != nil {
t.Fatalf("Failed to get a new credential")
}
testCases := []struct {
cred Credentials
ccred Credentials
@@ -119,7 +129,7 @@ func TestCredentialsEqual(t *testing.T) {
// Empty credentials.
{Credentials{}, cred, false},
// Two different credentialss
{cred, MustGetNewCredentials(), false},
{cred, cred2, false},
// Access key is different in credentials to compare.
{cred, Credentials{AccessKey: "myuser", SecretKey: cred.SecretKey}, false},
// Secret key is different in credentials to compare.