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

PMM-4879 Adding support for defaults-file. #356

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
5 changes: 5 additions & 0 deletions client/channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ func (c *Channel) runReceiver() {
ID: msg.Id,
Payload: p.PbmSwitchPitr,
}
case *agentpb.ServerMessage_ParseDefaultsFile:
c.requests <- &ServerRequest{
ID: msg.Id,
Payload: p.ParseDefaultsFile,
}

// responses
case *agentpb.ServerMessage_Pong:
Expand Down
30 changes: 17 additions & 13 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ const (

// Client represents pmm-agent's connection to nginx/pmm-managed.
type Client struct {
cfg *config.Config
supervisor supervisor
connectionChecker connectionChecker
softwareVersioner softwareVersioner
cfg *config.Config
supervisor supervisor
connectionChecker connectionChecker
softwareVersioner softwareVersioner
defaultsFileParser defaultsFileParser

l *logrus.Entry
backoff *backoff.Backoff
Expand All @@ -78,16 +79,17 @@ type Client struct {
// New creates new client.
//
// Caller should call Run.
func New(cfg *config.Config, supervisor supervisor, connectionChecker connectionChecker, sv softwareVersioner) *Client {
func New(cfg *config.Config, supervisor supervisor, connectionChecker connectionChecker, sv softwareVersioner, dfp defaultsFileParser) *Client {
return &Client{
cfg: cfg,
supervisor: supervisor,
connectionChecker: connectionChecker,
softwareVersioner: sv,
l: logrus.WithField("component", "client"),
backoff: backoff.New(backoffMinDelay, backoffMaxDelay),
done: make(chan struct{}),
dialTimeout: dialTimeout,
cfg: cfg,
supervisor: supervisor,
connectionChecker: connectionChecker,
softwareVersioner: sv,
l: logrus.WithField("component", "client"),
backoff: backoff.New(backoffMinDelay, backoffMaxDelay),
done: make(chan struct{}),
dialTimeout: dialTimeout,
defaultsFileParser: dfp,
}
}

Expand Down Expand Up @@ -436,6 +438,8 @@ func (c *Client) processChannelRequests(ctx context.Context) {
resp.Error = err.Error()
}
responsePayload = &resp
case *agentpb.ParseDefaultsFileRequest:
responsePayload = c.defaultsFileParser.ParseDefaultsFile(p)
default:
c.l.Errorf("Unhandled server request: %v.", req)
}
Expand Down
14 changes: 7 additions & 7 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestClient(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

cfg := &config.Config{}
client := New(cfg, nil, nil, nil)
client := New(cfg, nil, nil, nil, nil)
cancel()
err := client.Run(ctx)
assert.EqualError(t, err, "missing PMM Server address: context canceled")
Expand All @@ -93,7 +93,7 @@ func TestClient(t *testing.T) {
Address: "127.0.0.1:1",
},
}
client := New(cfg, nil, nil, nil)
client := New(cfg, nil, nil, nil, nil)
cancel()
err := client.Run(ctx)
assert.EqualError(t, err, "missing Agent ID: context canceled")
Expand All @@ -110,7 +110,7 @@ func TestClient(t *testing.T) {
Address: "127.0.0.1:1",
},
}
client := New(cfg, nil, nil, nil)
client := New(cfg, nil, nil, nil, nil)
err := client.Run(ctx)
assert.EqualError(t, err, "failed to dial: context deadline exceeded")
})
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestClient(t *testing.T) {
s.On("Changes").Return(make(<-chan *agentpb.StateChangedRequest))
s.On("QANRequests").Return(make(<-chan *agentpb.QANCollectRequest))

client := New(cfg, &s, nil, nil)
client := New(cfg, &s, nil, nil, nil)
err := client.Run(context.Background())
assert.NoError(t, err)
assert.Equal(t, serverMD, client.GetServerConnectMetadata())
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestClient(t *testing.T) {
},
}

client := New(cfg, nil, nil, nil)
client := New(cfg, nil, nil, nil, nil)
client.dialTimeout = 100 * time.Millisecond
err := client.Run(ctx)
assert.EqualError(t, err, "failed to get server metadata: rpc error: code = Canceled desc = context canceled", "%+v", err)
Expand Down Expand Up @@ -212,7 +212,7 @@ func TestGetActionTimeout(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(prototext.Format(tc.req), func(t *testing.T) {
client := New(nil, nil, nil, nil)
client := New(nil, nil, nil, nil, nil)
actual := client.getActionTimeout(tc.req)
assert.Equal(t, tc.expected, actual)
})
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestUnexpectedActionType(t *testing.T) {
s.On("Changes").Return(make(<-chan *agentpb.StateChangedRequest))
s.On("QANRequests").Return(make(<-chan *agentpb.QANCollectRequest))

client := New(cfg, s, nil, nil)
client := New(cfg, s, nil, nil, nil)
err := client.Run(context.Background())
assert.NoError(t, err)
assert.Equal(t, serverMD, client.GetServerConnectMetadata())
Expand Down
5 changes: 5 additions & 0 deletions client/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

//go:generate mockery -name=connectionChecker -case=snake -inpkg -testonly
//go:generate mockery -name=supervisor -case=snake -inpkg -testonly
//go:generate mockery -name=defaultsFileParser -case=snake -inpkg -testonly

// connectionChecker is a subset of methods of connectionchecker.ConnectionChecker used by this package.
// We use it instead of real type for testing and to avoid dependency cycle.
Expand All @@ -48,3 +49,7 @@ type supervisor interface {
// Collector added to use client as Prometheus collector
prometheus.Collector
}

type defaultsFileParser interface {
ParseDefaultsFile(req *agentpb.ParseDefaultsFileRequest) *agentpb.ParseDefaultsFileResponse
}
29 changes: 29 additions & 0 deletions client/mock_defaults_file_parser_test.go

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

4 changes: 3 additions & 1 deletion commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/percona/pmm-agent/client"
"github.com/percona/pmm-agent/config"
"github.com/percona/pmm-agent/connectionchecker"
"github.com/percona/pmm-agent/defaultsfile"
"github.com/percona/pmm-agent/versioner"
)

Expand Down Expand Up @@ -75,8 +76,9 @@ func run(ctx context.Context, cfg *config.Config, configFilepath string) {

supervisor := supervisor.NewSupervisor(ctx, &cfg.Paths, &cfg.Ports, &cfg.Server)
connectionChecker := connectionchecker.New(&cfg.Paths)
defaultsFileParser := defaultsfile.New()
v := versioner.New(&versioner.RealExecFunctions{})
client := client.New(cfg, supervisor, connectionChecker, v)
client := client.New(cfg, supervisor, connectionChecker, v, defaultsFileParser)
localServer := agentlocal.NewServer(cfg, supervisor, client, configFilepath)

go func() {
Expand Down
131 changes: 131 additions & 0 deletions defaultsfile/defaults_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// pmm-agent
// Copyright 2019 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package defaultsfile provides managing of defaults file.
package defaultsfile

import (
"fmt"
"os/user"
"path/filepath"
"strings"

"github.com/percona/pmm/api/agentpb"
"github.com/percona/pmm/api/inventorypb"
"github.com/pkg/errors"
"gopkg.in/ini.v1"
)

// Parser is a struct which is responsible for parsing defaults file.
type Parser struct{}

// New creates new DefaultsFileParser.
func New() *Parser {
return &Parser{}
}

type defaultsFile struct {
username string
password string
host string
port uint32
socket string
}

// ParseDefaultsFile parses given defaultsFile in request. It returns the database specs.
func (d *Parser) ParseDefaultsFile(req *agentpb.ParseDefaultsFileRequest) *agentpb.ParseDefaultsFileResponse {
var res agentpb.ParseDefaultsFileResponse
defaultsFile, err := parseDefaultsFile(req.ConfigPath, req.ServiceType)
if err != nil {
res.Error = err.Error()
return &res
}

res.Username = defaultsFile.username
res.Password = defaultsFile.password
res.Host = defaultsFile.host
res.Port = defaultsFile.port
res.Socket = defaultsFile.socket

return &res
}

func parseDefaultsFile(configPath string, serviceType inventorypb.ServiceType) (*defaultsFile, error) {
if len(configPath) == 0 {
return nil, errors.New("configPath for DefaultsFile is empty")
}

switch serviceType {
case inventorypb.ServiceType_MYSQL_SERVICE:
return parseMySQLDefaultsFile(configPath)
case inventorypb.ServiceType_EXTERNAL_SERVICE:
case inventorypb.ServiceType_HAPROXY_SERVICE:
case inventorypb.ServiceType_MONGODB_SERVICE:
case inventorypb.ServiceType_POSTGRESQL_SERVICE:
case inventorypb.ServiceType_PROXYSQL_SERVICE:
case inventorypb.ServiceType_SERVICE_TYPE_INVALID:
return nil, errors.Errorf("unimplemented service type %s", serviceType)
}

return nil, errors.Errorf("unimplemented service type %s", serviceType)
}

func parseMySQLDefaultsFile(configPath string) (*defaultsFile, error) {
configPath, err := expandPath(configPath)
if err != nil {
return nil, fmt.Errorf("fail to normalize path: %w", err)
}

cfg, err := ini.Load(configPath)
if err != nil {
return nil, fmt.Errorf("fail to read config file: %w", err)
}

cfgSection := cfg.Section("client")
port, _ := cfgSection.Key("port").Uint()

parsedData := &defaultsFile{
username: cfgSection.Key("user").String(),
password: cfgSection.Key("password").String(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later password need to be encrypted, please create related ticket so that we don't forget about it

host: cfgSection.Key("host").String(),
port: uint32(port),
BupycHuk marked this conversation as resolved.
Show resolved Hide resolved
socket: cfgSection.Key("socket").String(),
}

err = validateDefaultsFileResults(parsedData)
if err != nil {
return nil, err
}

return parsedData, nil
}

func validateDefaultsFileResults(data *defaultsFile) error {
if data.username == "" && data.password == "" && data.host == "" && data.port == 0 && data.socket == "" {
return errors.New("no data found in defaults file")
}
return nil
}

func expandPath(path string) (string, error) {
if strings.HasPrefix(path, "~/") {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("failed to expand path: %w", err)
}
return filepath.Join(usr.HomeDir, path[2:]), nil
}
return path, nil
}
Loading