我可以使用以下命令成功创建到Postgres数据库的连接:
my $settings = {
host => 'myhost',
db => 'mydb',
user => 'myuser',
passwd => 'mypasswd'
};
my $connection = DBI->connect(
'DBI:Pg:dbname=' . $settings->{'db'} . ';host=' . $settings->{'host'},
$settings->{'user'},
$settings->{'passwd'},
{
RaiseError => 1,
ShowErrorStatement => 0,
AutoCommit => 0
}
) or die DBI->errstr;
但是我在Perl模块中暴露了有价值的登录凭据(是的,我对其进行了更改)。目前,我使用psql
以交互方式发出查询。为了省去记住用户名/密码的麻烦,我将凭据放在一个具有权限600的文件(~/.pgpass)中。该文件如下所示:
# host:port:database:user:passwd
myhost:5432:mydb:myuser:mypasswd
如何安全地使用此文件("$ENV{HOME}/.pgpass"
)和DBI
模块来隐藏我的凭据?这是可以做到的吗?什么是最佳实践?
发布于 2013-11-14 22:24:34
是!是一种更好的方式。
在测试服务器和实时服务器之间轻松切换。
~/.pgpass
中(用于~/.pg_service.conf
(或/etc/pg_service.conf
) )中的psql
和~/.pg_service.conf
配置信息
例如:
#!/usr/bin/perl -T
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect
(
#"dbi:Pg:service=live",
"dbi:Pg:service=test",
undef,
undef,
{
AutoCommit => 0,
RaiseError => 1,
PrintError => 0
}
) or die DBI->errstr;
~/..pg_service.conf:
# http://www.postgresql.org/docs/9.2/static/libpq-pgservice.html
# /usr/local/share/postgresql/pg_service.conf.sample
# http://search.cpan.org/dist/DBD-Pg/Pg.pm
#
[test]
dbname=hotapp_test
user=hotusr_test
# localhost, no TCP nonsense needed:
host=/tmp
[live]
dbname=hotapp_live
user=hotusr_live
host=pgsql-server.example.org
~/.pgpass:
# http://www.postgresql.org/docs/9.2/static/libpq-pgpass.html
# hostname:port:database:username:password
localhost:5432:hotapp_test:hotusr_test:kq[O2Px7=g1
pgsql-server.example.org:5432:hotapp_live:hotusr_live:Unm£a7D(H
发布于 2013-05-16 18:53:51
根据上面的问题,
~/.pgpass
的文件中。undef
.文件中保持隐藏状态。
以下是对我有效的方法:
my $settings = {
host => 'myhost',
db => 'mydb',
user => 'myuser'
};
my $connection = DBI->connect(
'DBI:Pg:dbname=' . $settings->{'db'} . ';host=' . $settings->{'host'},
$settings->{'user'},
undef,
{
RaiseError => 1,
ShowErrorStatement => 0,
AutoCommit => 0
}
) or die DBI->errstr;
连接成功建立的原因是,由于某种原因(至少我不知道),实例在尝试连接时搜索~/.pgpass
文件。我知道这个文件有一些神奇之处,我只是不确定该怎么处理它。单据链接:
http://search.cpan.org/dist/DBI/DBI.pm#data_string_diff
注意到在该页面上搜索"pgpass“没有返回结果了吗?我拒绝把它全部读完。好吧,也许有一天..。
发布于 2013-05-16 12:40:08
open(my $fh, '<', "$ENV{HOME}/.pgpass") or die $!;
my $settings;
while (<>) {
chomp;
next if /^\s*(?:#.*)?\z/s;
@{$settings}{qw( host port database user passwd )} = split /:/;
}
die "No settings" if !$settings;
任何能够运行脚本的用户仍然能够看到凭证。
https://stackoverflow.com/questions/16579013
复制相似问题