Skip to content

Commit

Permalink
PMM-13129 Basics for settings and migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriCtvrtka committed Jun 27, 2024
1 parent e3fe487 commit 29d90df
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 236 deletions.
3 changes: 3 additions & 0 deletions api/serverpb/json/client/server/change_settings_responses.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions api/serverpb/json/client/server/get_settings_responses.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions api/serverpb/json/serverpb.json
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@
"type": "boolean",
"x-order": 15
},
"encrypted_items": {
"description": "Contains all already encrypted tables in format db.table.",
"type": "array",
"items": {
"type": "string"
},
"x-order": 17
},
"metrics_resolutions": {
"description": "MetricsResolutions represents Prometheus exporters metrics resolutions.",
"type": "object",
Expand Down Expand Up @@ -531,6 +539,14 @@
"type": "boolean",
"x-order": 15
},
"encrypted_items": {
"description": "Contains all already encrypted tables in format db.table.",
"type": "array",
"items": {
"type": "string"
},
"x-order": 17
},
"metrics_resolutions": {
"description": "MetricsResolutions represents Prometheus exporters metrics resolutions.",
"type": "object",
Expand Down
418 changes: 215 additions & 203 deletions api/serverpb/server.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/serverpb/server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ message Settings {
bool enable_access_control = 21;
// Default Access Control role ID for new users.
uint32 default_role_id = 22;
// Contains all already encrypted tables in format db.table.
repeated string encrypted_items = 23;
}

message GetSettingsRequest {}
Expand Down
16 changes: 16 additions & 0 deletions api/swagger/swagger-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -3262,6 +3262,14 @@
"type": "integer",
"format": "int64",
"x-order": 16
},
"encrypted_items": {
"description": "Contains all already encrypted tables in format db.table.",
"type": "array",
"items": {
"type": "string"
},
"x-order": 17
}
},
"x-order": 0
Expand Down Expand Up @@ -3455,6 +3463,14 @@
"type": "integer",
"format": "int64",
"x-order": 16
},
"encrypted_items": {
"description": "Contains all already encrypted tables in format db.table.",
"type": "array",
"items": {
"type": "string"
},
"x-order": 17
}
},
"x-order": 0
Expand Down
16 changes: 16 additions & 0 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,14 @@
"type": "integer",
"format": "int64",
"x-order": 16
},
"encrypted_items": {
"description": "Contains all already encrypted tables in format db.table.",
"type": "array",
"items": {
"type": "string"
},
"x-order": 17
}
},
"x-order": 0
Expand Down Expand Up @@ -611,6 +619,14 @@
"type": "integer",
"format": "int64",
"x-order": 16
},
"encrypted_items": {
"description": "Contains all already encrypted tables in format db.table.",
"type": "array",
"items": {
"type": "string"
},
"x-order": 17
}
},
"x-order": 0
Expand Down
81 changes: 49 additions & 32 deletions managed/models/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"

"github.com/lib/pq"
"github.com/percona/pmm/managed/utils/encryption"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -1063,38 +1064,54 @@ func SetupDB(ctx context.Context, sqlDB *sql.DB, params SetupDBParams) (*reform.
return nil, err
}

// host, p, err := net.SplitHostPort(params.Address)
// if err != nil {
// return nil, err
// }

// port, err := strconv.ParseInt(p, 10, 16)
// if err != nil {
// return nil, err
// }

// c := &encryption.DatabaseConnection{
// Host: host,
// Port: int16(port),
// User: params.Username,
// Password: params.Password,
// SSLMode: params.SSLMode,
// SSLCAPath: params.SSLCAPath,
// SSLKeyPath: params.SSLKeyPath,
// SSLCertPath: params.SSLCertPath,
// EncryptedItems: []encryption.EncryptedItem{
// {
// Database: "pmm-managed",
// Table: "agents",
// Identificators: []string{"agent_id"},
// Columns: []string{"username", "password"},
// },
// },
// }

// if err := encryption.EncryptDB(ctx, c); err != nil {
// return nil, err
// }
settings, err := GetSettings(sqlDB)
if err != nil {
return nil, err
}

if len(settings.EncryptedItems) > 0 {
return db, nil
}

host, p, err := net.SplitHostPort(params.Address)
if err != nil {
return nil, err
}

port, err := strconv.ParseInt(p, 10, 16)
if err != nil {
return nil, err
}

c := &encryption.DatabaseConnection{
Host: host,
Port: int16(port),
User: params.Username,
Password: params.Password,
SSLMode: params.SSLMode,
SSLCAPath: params.SSLCAPath,
SSLKeyPath: params.SSLKeyPath,
SSLCertPath: params.SSLCertPath,
EncryptedItems: []encryption.EncryptedItem{
{
Database: "pmm-managed",
Table: "agents",
Identificators: []string{"agent_id"},
Columns: []string{"username", "password"},
},
},
}

if err := encryption.EncryptDB(ctx, c); err != nil {
return nil, err
}

_, err = UpdateSettings(sqlDB, &ChangeSettingsParams{
EncryptedItems: []string{"pmm-managed.agents"},
})
if err != nil {
return nil, err
}

return db, nil
}
Expand Down
3 changes: 3 additions & 0 deletions managed/models/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type Settings struct {
// Enabled is true if access control is enabled.
Enabled bool `json:"enabled"`
} `json:"access_control"`

// Contains all already encrypted tables in format db.table.
EncryptedItems []string `json:"encrypted_items"`
}

// STTCheckIntervals represents intervals between STT checks.
Expand Down
7 changes: 7 additions & 0 deletions managed/models/settings_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ type ChangeSettingsParams struct {

// DefaultRoleID sets a default role to be assigned to new users.
DefaultRoleID int

// List of tables in format db.table to be encrypted.
EncryptedItems []string
}

// SetPMMServerID should be run on start up to generate unique PMM Server ID.
Expand Down Expand Up @@ -262,6 +265,10 @@ func UpdateSettings(q reform.DBTX, params *ChangeSettingsParams) (*Settings, err
settings.DefaultRoleID = params.DefaultRoleID
}

if len(params.EncryptedItems) != 0 {
settings.EncryptedItems = params.EncryptedItems
}

err = SaveSettings(q, settings)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion managed/utils/encryption/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"database/sql"
"encoding/base64"
"errors"
"fmt"
"os"
"slices"

Expand Down Expand Up @@ -156,7 +157,7 @@ func (e *Encryption) Decrypt(cipherText string) (string, error) {
}
decoded, err := base64.StdEncoding.DecodeString(cipherText)
if err != nil {
return cipherText, err
return cipherText, fmt.Errorf("value %s is probably not encrypted, error: %v", cipherText, err)
}
secret, err := e.Primitive.Decrypt(decoded, []byte(""))
if err != nil {
Expand Down

0 comments on commit 29d90df

Please sign in to comment.