Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bad mysql addon password decryption #4193

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions modules/orchestrator/components/addon/mysql/mysql.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import (
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-proto-go/orchestrator/addon/mysql/pb"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/orchestrator/dbclient"
"github.com/erda-project/erda/pkg/common/apis"
"github.com/erda-project/erda/pkg/crypto/encryption"
"github.com/erda-project/erda/pkg/mysqlhelper"
"github.com/erda-project/erda/pkg/strutil"
)
Expand All @@ -40,6 +42,8 @@ type mysqlService struct {
kms KMSWrapper
perm PermissionWrapper
db *dbclient.DBClient

encrypt *encryption.EnvEncrypt
}

// ListMySQLAccount returns a list of MySQL accounts
Expand Down Expand Up @@ -343,16 +347,25 @@ func (s *mysqlService) UpdateAttachmentAccount(ctx context.Context, req *pb.Upda
}

func (s *mysqlService) decrypt(acc *dbclient.MySQLAccount) (string, error) {
pass := "***fail***"
r, err := s.kms.Decrypt(acc.Password, acc.KMSKey)
if err != nil {
return pass, err
}
decodePasswordStr, err := base64.StdEncoding.DecodeString(r.PlaintextBase64)
if err != nil {
return pass, err
pass := acc.Password
if acc.KMSKey != "" {
dr, err := s.kms.Decrypt(acc.Password, acc.KMSKey)
if err != nil {
return pass, err
}
rawSecret, err := base64.StdEncoding.DecodeString(dr.PlaintextBase64)
if err != nil {
return pass, err
}
pass = string(rawSecret)
} else {
pass = string(decodePasswordStr)
// try to decrypt in old-style
_password, err := s.encrypt.DecryptPassword(acc.Password)
if err != nil {
logrus.Errorf("failed to decrypt password for mysql account %s: %s", acc.ID, err)
} else {
pass = _password
}
}
return pass, nil
}
Expand Down
10 changes: 10 additions & 0 deletions modules/orchestrator/components/addon/mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"github.com/erda-project/erda-infra/pkg/transport"
"github.com/erda-project/erda-proto-go/orchestrator/addon/mysql/pb"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/orchestrator/conf"
"github.com/erda-project/erda/modules/orchestrator/dbclient"
"github.com/erda-project/erda/pkg/common/apis"
"github.com/erda-project/erda/pkg/crypto/encryption"
"github.com/erda-project/erda/pkg/database/dbengine"
)

Expand Down Expand Up @@ -52,6 +54,14 @@ func (p *provider) Init(ctx servicehub.Context) error {
DB: p.DB,
},
},
encrypt: encryption.New(
encryption.WithRSAScrypt(encryption.NewRSAScrypt(encryption.RSASecret{
PublicKey: conf.PublicKey(),
PublicKeyDataType: encryption.Base64,
PrivateKey: conf.PrivateKey(),
PrivateKeyType: encryption.PKCS1,
PrivateKeyDataType: encryption.Base64,
}))),
}
if p.Register != nil {
pb.RegisterAddonMySQLServiceImp(p.Register, p.addonMySQLService, apis.Options())
Expand Down