This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
PMM-4879 Adding support for defaults-file. #356
Open
pkadej
wants to merge
14
commits into
percona:main
Choose a base branch
from
pkadej:PMM-4879-defaults-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c7dd697
PMM-4879 Adding support for defaults-file.
pkadej 3b2f119
PMM-4879 Code adjustments.
pkadej 4609d70
Merge branch 'main' into PMM-4879-defaults-file
ritbl fce92b6
PMM-4879 Linter & tests fix.
pkadej e7e3f27
PMM-4879 Adding tests.
pkadej be3fc93
PMM-4879 Adding support for configuration entry: socket.
pkadej e81f979
PMM-4879 Code review adjustments.
pkadej cb785cb
Merge branch 'main' into PMM-4879-defaults-file
pkadej d0bdf98
PMM-4879 Switching API to percona/pmm repository.
pkadej 48cef33
PMM-4879 Test fixes.
pkadej 0fc621f
PMM-4879 Test fixes.
pkadej 8bc76a5
PMM-4879 Switching API to percona/pmm repository.
pkadej 3d6e28a
PMM-4879 Some QA improvements.
pkadej a8bb846
Merge branch 'main' into PMM-4879-defaults-file
pkadej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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