Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New secrets format based on Config::General for named connections. #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions FS/FS/UID.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use Carp qw( carp croak cluck confess );
use DBI;
use IO::File;
use FS::CurrentUser;
use Config::General;

@ISA = qw(Exporter);
@EXPORT_OK = qw( checkeuid checkruid cgi setcgi adminsuidsetup forksuidsetup
Expand Down Expand Up @@ -147,7 +148,7 @@ sub db_setup {
$use_confcompat = 0;
}else{
die "NO CONFIGURATION RECORDS FOUND";
}


} else {
die "NO CONFIGURATION TABLE FOUND" unless $FS::Schema::setup_hack;
Expand All @@ -173,13 +174,18 @@ sub callback_setup {
}

sub myconnect {
my $handle = DBI->connect( getsecrets(), { 'AutoCommit' => 0,
'ChopBlanks' => 1,
'ShowErrorStatement' => 1,
'pg_enable_utf8' => 1,
#'mysql_enable_utf8' => 1,
}
)
my $options = shift || {};
unless (ref $options) {
# Handle being passed a username
$options = { user => $options };
}
my $handle = DBI->connect( getsecrets($options),
{ 'AutoCommit' => 0,
'ChopBlanks' => 1,
'ShowErrorStatement' => 1,
'pg_enable_utf8' => 1,
#'mysql_enable_utf8' => 1,
})
or die "DBI->connect error: $DBI::errstr\n";

if ( $schema ) {
Expand Down Expand Up @@ -314,10 +320,20 @@ the `/usr/local/etc/freeside/secrets' file.
=cut

sub getsecrets {
my $options = shift || { };

$options->{'ServerName'} ||= 'main';

my $secrets = Config::General->new("$conf_dir/secrets")
or die "Can't get secrets: $conf_dir/secrets: $!\n";

die "Could not find a $options->{'ServerName'} configuration. Is secrets file not in Config::General format?"
unless {$secrets->getall}->{'server'}->{$options->{'ServerName'}};

($datasrc, $db_user, $db_pass, $schema) = map {
{$secrets->getall}->{'server'}->{$options->{'ServerName'}}->{$_}}
qw/DSN Username Password Schema/;

($datasrc, $db_user, $db_pass, $schema) =
map { /^(.*)$/; $1 } readline(new IO::File "$conf_dir/secrets")
or die "Can't get secrets: $conf_dir/secrets: $!\n";
undef $driver_name;

($datasrc, $db_user, $db_pass);
Expand Down
28 changes: 28 additions & 0 deletions FS/bin/freeside-upgrade
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use FS::Misc::prune qw(prune_applications);
use FS::Conf;
use FS::Record qw(qsearch);
use FS::Upgrade qw(upgrade_schema upgrade_config upgrade upgrade_sqlradius);
use Config::General;

my $start = time;

Expand All @@ -27,6 +28,33 @@ $DRY_RUN = $opt_d;
my $user = shift or die &usage;
$FS::CurrentUser::upgrade_hack = 1;
$FS::UID::callback_hack = 1;

# Check for old secrets file format and upgrade, if neccessary
my $secrets_file = "%%%FREESIDE_CONF%%%/secrets";
my $conf = Config::General->new($secrets_file);

unless ({$conf->getall}->{'server'}->{'main'}) {
warn "secrets file is not in Config::General format";
my ($datasrc, $db_user, $db_pass, $schema) =
map { /^(.*)$/; $1 } readline(new IO::File $secrets_file);

my $new_config = {
server => {
'main' => {
ServerType => 'ReadWrite',
DSN => $datasrc,
Username => $db_user,
Password => $db_pass,
}
}
};

$new_config->{'server'}->{'main'}->{'Schema'} = $schema
if $schema;

$conf->save_file($secrets_file, $new_config);
}

my $dbh = adminsuidsetup($user);
$FS::UID::callback_hack = 0;

Expand Down