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

Commit

Permalink
Encryption PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
ritbl committed May 23, 2022
1 parent 4d163c8 commit 4c8c493
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
5 changes: 5 additions & 0 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package commands

import (
"context"
"github.com/percona/pmm-agent/utils/encryption"
"os"
"os/signal"

Expand All @@ -35,6 +36,10 @@ import (
func Run() {
l := logrus.WithField("component", "main")
ctx, cancel := context.WithCancel(context.Background())
ctx, err := encryption.InjectEncryptorIfNotPresent(ctx)
if err != nil {
l.Fatalf("Failed to inject encryptor: %s.", err)
}
defer l.Info("Done.")

// handle termination signals
Expand Down
9 changes: 9 additions & 0 deletions connectionchecker/connection_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"database/sql"
"fmt"
"github.com/percona/pmm-agent/utils/encryption"
"io"
"math"
"net/http"
Expand Down Expand Up @@ -65,6 +66,14 @@ func (cc *ConnectionChecker) Check(ctx context.Context, msg *agentpb.CheckConnec
defer cancel()
}

encryptor := encryption.GetEncryptor(ctx)
dsn, err := encryptor.DecryptDsn(msg.Dsn)
if err != nil {
cc.l.Debugf("Failed to decrypt DSN: %s", err)
} else {
msg.Dsn = dsn
}

switch msg.Type {
case inventorypb.ServiceType_MYSQL_SERVICE:
return cc.checkMySQLConnection(ctx, msg.Dsn, msg.TextFiles, msg.TlsSkipVerify, id)
Expand Down
52 changes: 52 additions & 0 deletions utils/encryption/default-key
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCawg8C/6YNMFfi
QBUV6ifAsdxc6PO7+GBxrV/hsKCzJO+hR1yDsQAiWl+/XicJD7XNRcjeHSJYf9km
wc1vtbI1hlmm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OqsVptGwM7IJDTpodMeAvBNDVzVNN0G1fnYCrD/IFLPbUw5
8kmEijWu8jZ6zOJAp1NztCzrz574kAcvHj7PTcCzv+U830NNzcRiRSYEOi9s76Ie
8eNFeR5eDvwveBA177yvc3ZKynF3j4CoTXLRbU6Z9VGSH1NYrL8+xDddK0Z2iUct
vEi09+sqZMJM9MvdSMwZbNKGFKjM1UaPUdzd+UECggEBAJRWM//mQ+bMWQ6ILXRf
2y+xG63N85l+CEcyhUjz/0IgPzewjrwOu70+Nlw5yqzriILaL/kPKXvCc8Bo/XvD
CxES6Im+aZ1jfAbez+uaaYdeZYYP/3pNRgezDR+a4VGqrM6428rB5PESd72r6iMc
NE8LAIAdk7CbtHT2Hp03sPMbPaHLZbX9ZNb5IBR1jnfBJ35WQoHnfTpq9qJOiC9U
HlDntG+Wt6rlobmLldFcM8bjj/MRZSaJrlfEzmhLbNfsHQmWk2zKj4xaGdU9Y8aU
b7jm0t4qHVRxi7NIy7pzxVxk93r5YoR60TLoPYGYMdZnTmDqUk4ZVjrmCYc0Y3UN
7I0CggEADTR7I5Pcgn1T5LkcZlESFfwGGkq5F3I1DckHz55jkdPS1bsFSAnJLnhS
Rw9iij+YxkpbFgmUlm2U4JGJFMg2IyZmBFuEAyKn3F3LuRR93Tw3LVJ0Rp2FuvUe
BAU5ZdcV9ZqBDCbYbiGmT6tTTuUaoJVqHWI/rWReRQH/mBSWE5WSdxs9RCk5ItkI
y0lN4ciGLSEtCJ0nPS5gsZSAjfR91rA8LzxfWGSvfGLBnQ9yZk+dWMEoJ1YfHdID
qPo3RhWm1PofU1GI1ZEmMSMSqpk+Ii+eeusWTbPhutodeQTi4fwvxsKscLEXND04
281eBVUKTQll92Pc9h3YBktcWHXwfQ==
-----END PRIVATE 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
var privateKey []byte

const EncryptorKey = "encryptor"

func NewFromDefaultKey() (*rsa_encryptor.Service, error) {
return rsa_encryptor.NewFromPrivateKey("d1", privateKey)
}

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)
}

0 comments on commit 4c8c493

Please sign in to comment.