Skip to content

Commit

Permalink
fix(api): refactor project variables migration (#5054)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored Mar 9, 2020
1 parent 73eeaa6 commit c44a078
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
17 changes: 8 additions & 9 deletions engine/api/migrate/refactor_proj_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,15 @@ func refactorProjectVariables(ctx context.Context, db *gorp.DbMap, id int64) err
}
v.Value = s
} else {
s, err = stringIfValid("cipher_value", cipherValue)
if err != nil {
return err
}

btes, err := secret.Decrypt([]byte(s))
if err != nil {
return err
s, _ = stringIfValid("cipher_value", cipherValue)
// ignore the error, to keep NULL value
if s != "" {
btes, err := secret.Decrypt([]byte(s))
if err != nil {
return err
}
v.Value = string(btes)
}
v.Value = string(btes)
}

if err := project.UpdateVariable(tx, pID, &v, nil, nil); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions engine/api/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ func Decrypt(data []byte) ([]byte, error) {

if key == nil {
log.Error(context.TODO(), "Missing key, init failed?")
return nil, sdk.ErrSecretKeyFetchFailed
return nil, sdk.WithStack(sdk.ErrSecretKeyFetchFailed)
}

if len(data) < (nonceSize + macSize) {
log.Error(context.TODO(), "cannot decrypt secret, got invalid data")
return nil, sdk.ErrInvalidSecretFormat
return nil, sdk.WithStack(sdk.ErrInvalidSecretFormat)
}

// Split actual data, hmac and nonce
Expand All @@ -135,12 +135,12 @@ func Decrypt(data []byte) ([]byte, error) {
h.Write(data)
mac := h.Sum(nil)
if !hmac.Equal(mac, tag) {
return nil, fmt.Errorf("invalid hmac")
return nil, sdk.WithStack(fmt.Errorf("invalid hmac"))
}
// uncipher data
c, err := aes.NewCipher(key[:ckeySize])
if err != nil {
return nil, err
return nil, sdk.WithStack(fmt.Errorf("unable to create cypher block: %v", err))
}
ctr := cipher.NewCTR(c, data[:nonceSize])
ctr.XORKeyStream(out, data[nonceSize:])
Expand Down

0 comments on commit c44a078

Please sign in to comment.