Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

PMM-9947: Encryption PoC #222

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion commands/management/add_mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
package management

import (
"context"
"fmt"
"github.com/percona/pmm-admin/utils/encryption"
"github.com/sirupsen/logrus"
"os"
"strings"

Expand Down Expand Up @@ -112,6 +115,10 @@ func (cmd *addMongoDBCommand) GetCredentials() error {
}

func (cmd *addMongoDBCommand) Run() (commands.Result, error) {
return cmd.RunWithContext(context.TODO())
}

func (cmd *addMongoDBCommand) RunWithContext(ctx context.Context) (commands.Result, error) {
customLabels, err := commands.ParseCustomLabels(cmd.CustomLabels)
if err != nil {
return nil, err
Expand Down Expand Up @@ -150,6 +157,13 @@ func (cmd *addMongoDBCommand) Run() (commands.Result, error) {
}
}

encryptor := encryption.GetEncryptor(ctx)
password, err := encryptor.EncryptAsBlock(cmd.Password)
if err != nil {
logrus.Warnf("Failed to encrypt password: %s", err)
password = cmd.Password
}

params := &mongodb.AddMongoDBParams{
Body: mongodb.AddMongoDBBody{
NodeID: cmd.NodeID,
Expand All @@ -162,7 +176,7 @@ func (cmd *addMongoDBCommand) Run() (commands.Result, error) {
Cluster: cmd.Cluster,
ReplicationSet: cmd.ReplicationSet,
Username: cmd.Username,
Password: cmd.Password,
Password: password,
AgentPassword: cmd.AgentPassword,

QANMongodbProfiler: cmd.QuerySource == mongodbQuerySourceProfiler,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/AlekSi/pointer v1.2.0
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
github.com/go-openapi/runtime v0.24.0
github.com/percona/pmm v0.0.0-20220520150831-23069cdf1bb8
github.com/percona/pmm v0.0.0-20220523024928-d94947734674
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/percona/pmm v0.0.0-20220520150831-23069cdf1bb8 h1:P5iuV4GRUIviRg/5/FM6ZOKdiBPdwUPbrHld/epM3hk=
github.com/percona/pmm v0.0.0-20220520150831-23069cdf1bb8/go.mod h1:gr+WLd8clEAe2xMFgsGhpw9ziZc2UCWcfy6d3M6Aq00=
github.com/percona/pmm v0.0.0-20220523024928-d94947734674 h1:KWXwcENaXzZ7ep9zboSk6YnRrLM/O85+ptGQdlBQEJQ=
github.com/percona/pmm v0.0.0-20220523024928-d94947734674/go.mod h1:gr+WLd8clEAe2xMFgsGhpw9ziZc2UCWcfy6d3M6Aq00=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/percona/pmm-admin/utils/encryption"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -60,6 +61,7 @@ func main() {
return nil
}).Bool()

kingpin.CommandLine.DefaultEnvars()
cmd := kingpin.Parse()

logrus.SetFormatter(new(logger.TextFormatter)) // with levels and timestamps for debug and trace
Expand All @@ -75,6 +77,10 @@ func main() {
}

ctx, cancel := context.WithCancel(context.Background())
ctx, errEnc := encryption.InjectEncryptorIfNotPresent(ctx)
if errEnc != nil {
logrus.Panicf("Failed to inject encryptor: %v", errEnc)
}

// handle termination signals
signals := make(chan os.Signal, 1)
Expand Down
14 changes: 14 additions & 0 deletions utils/encryption/default-key.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAmsIPAv+mDTBX4kAVFeon
wLHcXOjzu/hgca1f4bCgsyTvoUdcg7EAIlpfv14nCQ+1zUXI3h0iWH/ZJsHNb7Wy
NYZZpkCIrWk9XUuPbzijazjLvBaMzyVLb8zQFESvuKumSOZ+WizvuHL2MGaJqLYI
2eVLDKX3TVeJCe8HK8KA6XUau28tNDEymf/Hyk7BQAINkQTnwIWIX+lzeGI+eMzT
uptDIf3OCvoe/a1qp0RP7jQ8bU2fj6SUB0Ts3FElqTsGZczP6zag20CR0hSzlqNI
785Mcv3tRxszwu+rET9CVyjRG9Y6X9TqPODbuM1n6aKla1X9Wkt386Li0TgXtF/S
tJA/BK7JrPrSRz+vKakhqqcmudPA5NeqdjC92jdxmtLObVm4L/OF0FwRYAEeYRVi
CZNTo8DwEjecYHy+FNutGxvOP/p15ip3YG6IHGp1kPoGdxwzAJQK957ZVqQUJCAC
M2lcNPEQ+muYRTMHLYuNMyVVW+OOdTFrFxUK/xisYhb7tJoN/aZUrww7KVDVD6AD
HImr1TL7hE9r/ko3e/0TQN8D+fgLPpKLyaguuLI2HyRalzFWuQSEWUOz/2IQ76kR
glL2yQVAOh8oG8sX6xXY1fFpfpvZd4VCWdWOQfW2tBqOKpcMgmkgxKctMUeHhhgx
GFI7b65SXK9uPB3Rs6EXbd8CAwEAAQ==
-----END PUBLIC KEY-----
33 changes: 33 additions & 0 deletions utils/encryption/encryption.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package encryption

import (
"context"
_ "embed"
"github.com/percona/pmm/utils/rsa_encryptor"
)

//go:embed default-key.pub
var publicKey []byte

const EncryptorKey = "encryptor"

func NewFromDefaultKey() (*rsa_encryptor.Service, error) {
return rsa_encryptor.NewFromPublicKey("d1", publicKey)
}

func InjectEncryptorIfNotPresent(ctx context.Context) (context.Context, error) {
encryptor := ctx.Value(EncryptorKey)
if encryptor == nil {
encryptor, err := NewFromDefaultKey()
if err != nil {
return nil, err
}
return context.WithValue(ctx, EncryptorKey, encryptor), nil
}

return ctx, nil
}

func GetEncryptor(ctx context.Context) *rsa_encryptor.Service {
return ctx.Value(EncryptorKey).(*rsa_encryptor.Service)
}