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

feat: security feature - login history #560

Open
wants to merge 5 commits into
base: POLARDB_15_STABLE
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: security feature - login history
Show the user the last login information.
  • Loading branch information
manfuldrifter committed Feb 26, 2025
commit 95f6d6f8b7d560b7611dd5d9f556f1a571bee7f5
641 changes: 325 additions & 316 deletions external/polar_login_history/polar_login_history.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/backend/catalog/Catalog.pm
Original file line number Diff line number Diff line change
@@ -313,7 +313,7 @@ sub ParseData
# We're treating the input line as a piece of Perl, so we
# need to use string eval here. Tell perlcritic we know what
# we're doing.
eval '$hash_ref = ' . $_; ## no critic (ProhibitStringyEval)
eval '$hash_ref = ' . $_; ## no critic (ProhibitStringyEval)
if (!ref $hash_ref)
{
die "$input_file: error parsing line $.:\n$_\n";
5 changes: 3 additions & 2 deletions src/backend/commands/user.c
Original file line number Diff line number Diff line change
@@ -54,6 +54,7 @@ check_password_hook_type check_password_hook = NULL;
* POLAR: login history
*/
polar_remove_login_history_hook_type polar_remove_login_history_hook = NULL;

/* POLAR end */

static void AddRoleMems(const char *rolename, Oid roleid,
@@ -1080,8 +1081,8 @@ DropRole(DropRoleStmt *stmt)
CommandCounterIncrement();

/*
* POLAR: login history
* Delete the login history information related to the role.
* POLAR: login history Delete the login history information related
* to the role.
*/
if (polar_remove_login_history_hook)
polar_remove_login_history_hook(roleid);
7 changes: 3 additions & 4 deletions src/backend/tcop/postgres.c
Original file line number Diff line number Diff line change
@@ -4511,8 +4511,7 @@ PostgresMain(const char *dbname, const char *username)
int microsecs = 0;

/*
* POLAR: login history
* Indicate that the login process starts.
* POLAR: login history Indicate that the login process starts.
*/
polar_login_flag = true;
/* POLAR end */
@@ -4686,8 +4685,8 @@ PostgresMain(const char *dbname, const char *username)
MemoryContextSwitchTo(TopMemoryContext);

/*
* POLAR: login history
* Record the successful login information of the user.
* POLAR: login history Record the successful login information of the
* user.
*/
if (polar_update_login_history_hook)
polar_update_login_history_hook(true);
6 changes: 3 additions & 3 deletions src/backend/utils/error/elog.c
Original file line number Diff line number Diff line change
@@ -139,8 +139,9 @@ int polar_audit_log_flush_timeout = 0;
/*
* POLAR: login history
*/
bool polar_login_flag = false;
bool polar_login_flag = false;
polar_update_login_history_hook_type polar_update_login_history_hook = NULL;

/* POLAR end */

#ifdef HAVE_SYSLOG
@@ -549,8 +550,7 @@ errfinish(const char *filename, int lineno, const char *funcname)
ErrorContextCallback *econtext;

/*
* POLAR: login history
* Record the failed login information of the user.
* POLAR: login history Record the failed login information of the user.
* Most of the errors caused by users during login are fatal.
*/
if (edata->elevel == FATAL && polar_update_login_history_hook)
10 changes: 4 additions & 6 deletions src/test/perl/PostgreSQL/Test/Cluster.pm
Original file line number Diff line number Diff line change
@@ -1619,8 +1619,8 @@ sub get_free_port
{
foreach my $addr (qw(127.0.0.1),
($use_tcp && $PostgreSQL::Test::Utils::windows_os)
? qw(127.0.0.2 127.0.0.3 0.0.0.0)
: ())
? qw(127.0.0.2 127.0.0.3 0.0.0.0)
: ())
{
if (!can_bind($addr, $port))
{
@@ -4288,8 +4288,7 @@ sub polar_wait_for_startup

##########################################################################

package PostgreSQL::Test::Cluster::V_11
; ## no critic (ProhibitMultiplePackages)
package PostgreSQL::Test::Cluster::V_11; ## no critic (ProhibitMultiplePackages)

# parent.pm is not present in all perl versions before 5.10.1, so instead
# do directly what it would do for this:
@@ -4319,8 +4318,7 @@ sub init

##########################################################################

package PostgreSQL::Test::Cluster::V_10
; ## no critic (ProhibitMultiplePackages)
package PostgreSQL::Test::Cluster::V_10; ## no critic (ProhibitMultiplePackages)

# use parent -norequire, qw(PostgreSQL::Test::Cluster::V_11);
push @PostgreSQL::Test::Cluster::V_10::ISA, 'PostgreSQL::Test::Cluster::V_11';
13 changes: 6 additions & 7 deletions src/test/polar_pl/t/046_polar_login_history.pl
Original file line number Diff line number Diff line change
@@ -16,19 +16,18 @@
$node->start;

$node->append_conf('postgresql.conf',
"polar_internal_shared_preload_libraries = 'polar_login_history'");
"polar_internal_shared_preload_libraries = 'polar_login_history'");
$node->restart;

$node->append_conf('postgresql.conf',
"polar_login_history.enable = on");
$node->append_conf('postgresql.conf', "polar_login_history.enable = on");
$node->restart;

is( $node->safe_psql('postgres', 'show polar_login_history.enable;'),
'on', 'login history function is enabled');
is($node->safe_psql('postgres', 'show polar_login_history.enable;'),
'on', 'login history function is enabled');

$node->safe_psql('postgres', 'create user zhangsan;');
is( $node->psql('postgres', undef, extra_params => [ '-U', 'zhangsan' ]),
0, 'login success');
is($node->psql('postgres', undef, extra_params => [ '-U', 'zhangsan' ]),
0, 'login success');
$node->safe_psql('postgres', 'drop user zhangsan;');

$node->stop;
2 changes: 1 addition & 1 deletion src/tools/polar_sort_subdir.pl
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@
my %patterns = (
'SUBDIRS \+=' => {},
'ALWAYS_SUBDIRS \+=' => {}
# Add more patterns as needed
# Add more patterns as needed
);

foreach my $pattern (keys %patterns)