Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pgadmin.browser.server_groups.servers.databases.schemas.utils \
import SchemaChildModule
from pgadmin.browser.server_groups.servers.utils import parse_priv_from_db, \
parse_priv_to_db
parse_priv_to_db, parse_seclabels_from_db
from pgadmin.browser.utils import PGChildNodeView
from pgadmin.tools.schema_diff.compare import SchemaDiffObjectCompare
from pgadmin.tools.schema_diff.node_registry import SchemaDiffRegistry
Expand Down Expand Up @@ -838,12 +838,8 @@ def _formatter(self, data, scid, seid):

# Need to format security labels according to client js collection
if 'securities' in data and data['securities'] is not None:
seclabels = []
for seclbls in data['securities']:
k, v = seclbls.split('=')
seclabels.append({'provider': k, 'label': v})

data['securities'] = seclabels
data['securities'] = parse_seclabels_from_db(
data['securities'])

# We need to parse & convert ACL coming from database to json format
sql = render_template("/".join([self.template_path, self._ACL_SQL]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pgadmin.browser.server_groups.servers.databases.schemas.utils \
import DataTypeReader
from pgadmin.browser.server_groups.servers.utils import parse_priv_from_db, \
parse_priv_to_db
parse_priv_to_db, parse_seclabels_from_db
from functools import wraps
import re

Expand Down Expand Up @@ -144,12 +144,8 @@ def column_formatter(conn, tid, clid, data, edit_types_list=None,

# Need to format security labels according to client js collection
if 'seclabels' in data and data['seclabels'] is not None:
seclabels = []
for seclbls in data['seclabels']:
k, v = seclbls.split('=')
seclabels.append({'provider': k, 'label': v})

data['seclabels'] = seclabels
data['seclabels'] = parse_seclabels_from_db(
data['seclabels'])

# Get formatted Column Options
if 'attfdwoptions' in data and data['attfdwoptions'] != '':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from pgadmin.browser.server_groups.servers.databases.schemas.utils \
import DataTypeReader, parse_rule_definition, check_pgstattuple
from pgadmin.browser.server_groups.servers.utils import parse_priv_from_db, \
parse_priv_to_db
parse_priv_to_db, parse_seclabels_from_db
from pgadmin.browser.utils import PGChildNodeView
from pgadmin.utils.compile_template_name import compile_template_path
from pgadmin.utils.driver import get_driver
Expand Down Expand Up @@ -185,12 +185,8 @@ def _formatter(self, did, scid, tid, data, with_serial_cols=True):
"""
# Need to format security labels according to client js collection
if 'seclabels' in data and data['seclabels'] is not None:
seclabels = []
for seclbls in data['seclabels']:
k, v = seclbls.split('=')
seclabels.append({'provider': k, 'label': v})

data['seclabels'] = seclabels
data['seclabels'] = parse_seclabels_from_db(
data['seclabels'])

# We need to parse & convert ACL coming from database to json format
sql = render_template("/".join([self.table_template_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from flask_babel import gettext
from pgadmin.browser.collection import CollectionNodeModule
from pgadmin.browser.server_groups.servers.utils import parse_priv_from_db, \
parse_priv_to_db
parse_priv_to_db, parse_seclabels_from_db
from pgadmin.browser.utils import PGChildNodeView
from pgadmin.utils.ajax import make_json_response, \
make_response as ajax_response, internal_server_error, gone
Expand Down Expand Up @@ -225,12 +225,8 @@ def _formatter(self, data, tsid=None):

# Need to format security labels according to client js collection
if 'seclabels' in data and data['seclabels'] is not None:
seclabels = []
for seclbls in data['seclabels']:
k, v = seclbls.split('=')
seclabels.append({'provider': k, 'label': v})

data['seclabels'] = seclabels
data['seclabels'] = parse_seclabels_from_db(
data['seclabels'])

# We need to parse & convert ACL coming from database to json format
SQL = render_template(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2026, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################

"""Unit tests for parse_seclabels_from_db() and the column formatter's use
of it. Security labels are free text, so a label may contain "=" and must
survive the "provider=label" round trip. No PostgreSQL server is needed."""

from unittest.mock import MagicMock, patch
from pgadmin.utils.route import BaseTestGenerator
from pgadmin.browser.server_groups.servers.utils import \
parse_seclabels_from_db

COLUMN_UTILS = ('pgadmin.browser.server_groups.servers.databases.schemas.'
'tables.columns.utils')


class TestParseSecLabelsFromDb(BaseTestGenerator):
"""Parsing of "provider=label" security label strings."""

scenarios = [
('Plain label',
dict(test_method='test_plain_label')),
('Label containing equals signs',
dict(test_method='test_label_with_equals')),
('Empty label',
dict(test_method='test_empty_label')),
('Several labels keep their order',
dict(test_method='test_several_labels')),
('No labels',
dict(test_method='test_no_labels')),
('Column formatter accepts a label containing equals signs',
dict(test_method='test_column_formatter')),
]

def setUp(self):
# Pure unit tests: no PostgreSQL server connection is needed.
pass

def runTest(self):
getattr(self, self.test_method)()

def test_plain_label(self):
self.assertEqual(
parse_seclabels_from_db(['selinux=system_u:object_r:t']),
[{'provider': 'selinux', 'label': 'system_u:object_r:t'}])

def test_label_with_equals(self):
# PostgreSQL Anonymizer masking rules contain SQL expressions.
self.assertEqual(
parse_seclabels_from_db([
'anon=MASKED WITH VALUE $$a=b$$',
'anon=MASKED WITH FUNCTION f(x) WHERE id = user_id']),
[{'provider': 'anon', 'label': 'MASKED WITH VALUE $$a=b$$'},
{'provider': 'anon',
'label': 'MASKED WITH FUNCTION f(x) WHERE id = user_id'}])

def test_empty_label(self):
self.assertEqual(parse_seclabels_from_db(['anon=']),
[{'provider': 'anon', 'label': ''}])

def test_several_labels(self):
self.assertEqual(
[r['provider'] for r in parse_seclabels_from_db(
['b=1', 'a=2', 'c=3=4'])],
['b', 'a', 'c'])

def test_no_labels(self):
self.assertEqual(parse_seclabels_from_db([]), [])

@patch(COLUMN_UTILS + '.render_template', return_value='SELECT 1;')
@patch(COLUMN_UTILS + '._fetch_inherited_tables',
return_value=(False, None))
def test_column_formatter(self, _mock_inherited, _mock_render):
from pgadmin.browser.server_groups.servers.databases.schemas.\
tables.columns.utils import column_formatter

conn = MagicMock()
conn.execute_dict.return_value = (True, {'rows': []})
data = dict(
name='name', atttypid=25, typname='text', cltype='text',
attlen=-1, attypmod=-1,
seclabels=['anon=MASKED WITH VALUE $$a=b$$'],
attnotnull=False, is_pk=False, attnum=2)

with patch(COLUMN_UTILS + '._check_primary_column'), \
patch(COLUMN_UTILS + '.fetch_length_precision',
side_effect=lambda d: d):
result = column_formatter(
conn, 1, 2, data, edit_types_list=[],
template_path='columns/sql/default')

self.assertEqual(
result['seclabels'],
[{'provider': 'anon', 'label': 'MASKED WITH VALUE $$a=b$$'}])
14 changes: 14 additions & 0 deletions web/pgadmin/browser/server_groups/servers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ def is_valid_ipaddress(address):
return False


def parse_seclabels_from_db(seclabels):
"""
Common utility function to parse security labels retrieved from database.

Each entry is a "provider=label" string. The provider name never contains
an "=", but the label is free text and may, so split on the first one only.
"""
result = []
for seclbls in seclabels:
provider, label = seclbls.split('=', 1)
result.append({'provider': provider, 'label': label})
return result


def parse_priv_from_db(db_privileges):
"""
Common utility function to parse privileges retrieved from database.
Expand Down
Loading