Skip to content

Commit

Permalink
fix(bug): do recovery for interface panic as error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alice52 committed Dec 22, 2023
1 parent 372129b commit db6ba3f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build-jasypt-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ on:
push:
branches:
- master
paths:
- '**/github.com/alice52/viper-go/**'
- '.github/workflows/build-viper-go.yml'
# paths:
# - '**/github.com/alice52/viper-go/**'
# - '.github/workflows/build-viper-go.yml'
pull_request:

jobs:
Expand All @@ -18,7 +18,7 @@ jobs:
fail-fast: false
matrix:
os: [ 'ubuntu-latest', 'macOS-latest' ]
go: [ '1.18.x', '1.19.x', '1.20.x', '1.21.x' ]
go: [ '1.19.x', '1.20.x', '1.21.x' ] # '1.18.x',
runs-on: ${{ matrix.os }}
if: "!contains(github.event.head_commit.message, 'ci skip')"
env:
Expand Down
14 changes: 10 additions & 4 deletions crypt/encryptor/default_encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func (c *DefaultAES) GetConfig() config.Config {
return c.Config
}

func (c *DefaultAES) EncryptWrapper(message string) (string, error) {
func (c *DefaultAES) EncryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)
encrypted, err := c.Encrypt(message)
if err != nil {
return "", err
Expand All @@ -33,7 +34,9 @@ func (c *DefaultAES) EncryptWrapper(message string) (string, error) {
return c.Prefix + encrypted + c.Suffix, nil
}

func (c *DefaultAES) Encrypt(message string) (string, error) {
func (c *DefaultAES) Encrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)

block, err := buildCipher(c.Password)
if err != nil {
return "", err
Expand All @@ -53,7 +56,8 @@ func (c *DefaultAES) Encrypt(message string) (string, error) {
return base64.StdEncoding.EncodeToString(seal), nil
}

func (c *DefaultAES) DecryptWrapper(message string) (string, error) {
func (c *DefaultAES) DecryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)
if c.NeedDecrypt(message) {
s := len(c.Prefix)
e := len(message) - len(c.Suffix)
Expand All @@ -63,7 +67,9 @@ func (c *DefaultAES) DecryptWrapper(message string) (string, error) {
return message, nil
}

func (c *DefaultAES) Decrypt(message string) (string, error) {
func (c *DefaultAES) Decrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)

data, err := base64.StdEncoding.DecodeString(message)
if err != nil {
return "", err
Expand Down
14 changes: 13 additions & 1 deletion crypt/encryptor/encryptor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package encryptor

import "github.com/alice52/jasypt-go/config"
import (
"errors"
"fmt"
"github.com/alice52/jasypt-go/config"
)

type Encryptor interface {
GetConfig() config.Config
Expand All @@ -10,3 +14,11 @@ type Encryptor interface {
EncryptWrapper(message string) (string, error)
DecryptWrapper(message string) (string, error)
}

func RecoveryPanicAsError(err error) {
defer func() {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("recovered from panic: %v", r))
}
}()
}
16 changes: 12 additions & 4 deletions crypt/encryptor/pbe_aes_encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func (c *PBEWithAES) GetConfig() config.Config {
return c.Config
}

func (c *PBEWithAES) EncryptWrapper(message string) (string, error) {
func (c *PBEWithAES) EncryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)

encrypted, err := c.Encrypt(message)
if err != nil {
return "", err
Expand All @@ -37,7 +39,9 @@ func (c *PBEWithAES) EncryptWrapper(message string) (string, error) {
return c.Prefix + encrypted + c.Suffix, nil
}

func (c *PBEWithAES) Encrypt(message string) (string, error) {
func (c *PBEWithAES) Encrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)

saltGenerator, ivGenerator, password := c.SaltGenerator, c.IvGenerator, c.Password
_, _, koi, ab := c.Prefix, c.Suffix, c.keyObtainIterations, c.algorithmBlockSize

Expand Down Expand Up @@ -66,7 +70,9 @@ func (c *PBEWithAES) Encrypt(message string) (string, error) {
return base64.StdEncoding.EncodeToString(result), nil
}

func (c *PBEWithAES) DecryptWrapper(message string) (string, error) {
func (c *PBEWithAES) DecryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)

if c.NeedDecrypt(message) {
s := len(c.Prefix)
e := len(message) - len(c.Suffix)
Expand All @@ -76,7 +82,9 @@ func (c *PBEWithAES) DecryptWrapper(message string) (string, error) {
return message, nil
}

func (c *PBEWithAES) Decrypt(message string) (string, error) {
func (c *PBEWithAES) Decrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)

saltGenerator, ivGenerator, password := c.SaltGenerator, c.IvGenerator, c.Password
_, _, koi, ab := c.Prefix, c.Suffix, c.keyObtainIterations, c.algorithmBlockSize

Expand Down
16 changes: 12 additions & 4 deletions crypt/encryptor/pbe_des_encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ func NewPBEWithDES(conf config.Config) *PBEWithDES {
}
}

func (c *PBEWithDES) EncryptWrapper(message string) (string, error) {
func (c *PBEWithDES) EncryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)

encrypted, err := c.Encrypt(message)
if err != nil {
return "", err
Expand All @@ -35,7 +37,9 @@ func (c *PBEWithDES) EncryptWrapper(message string) (string, error) {
return c.Prefix + encrypted + c.Suffix, nil
}

func (c *PBEWithDES) Encrypt(message string) (string, error) {
func (c *PBEWithDES) Encrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)

saltGenerator, ivGenerator, password := c.SaltGenerator, c.IvGenerator, c.Password
_, _, koi, ab := c.Prefix, c.Suffix, c.keyObtainIterations, c.algorithmBlockSize

Expand All @@ -61,7 +65,9 @@ func (c *PBEWithDES) Encrypt(message string) (string, error) {
return base64.StdEncoding.EncodeToString(result), nil
}

func (c *PBEWithDES) DecryptWrapper(message string) (string, error) {
func (c *PBEWithDES) DecryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)

if c.NeedDecrypt(message) {
s := len(c.Prefix)
e := len(message) - len(c.Suffix)
Expand All @@ -71,7 +77,9 @@ func (c *PBEWithDES) DecryptWrapper(message string) (string, error) {
return message, nil
}

func (c *PBEWithDES) Decrypt(message string) (string, error) {
func (c *PBEWithDES) Decrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)

saltGenerator, ivGenerator, password := c.SaltGenerator, c.IvGenerator, c.Password
_, _, koi, ab := c.Prefix, c.Suffix, c.keyObtainIterations, c.algorithmBlockSize

Expand Down
3 changes: 2 additions & 1 deletion viper/viper_jasypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"github.com/spf13/viper"
)

func Unmarshal(v *viper.Viper, etor encryptor.Encryptor, rawVal any, opts ...viper.DecoderConfigOption) error {
func Unmarshal(v *viper.Viper, etor encryptor.Encryptor, rawVal any, opts ...viper.DecoderConfigOption) (err error) {
encryptor.RecoveryPanicAsError(err)

jc := etor.GetConfig()
for _, k := range v.AllKeys() {
Expand Down

0 comments on commit db6ba3f

Please sign in to comment.