Skip to content
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
29 changes: 21 additions & 8 deletions cterasdk/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _get_current_stage(self):
self.stage = status.wizard
return self.stage

def init_master(self, name, email, first_name, last_name, password, domain):
def init_master(self, name, email, first_name, last_name, password, domain, *, database=None):
"""
Initialize the CTERA Portal master server.

Expand All @@ -36,10 +36,11 @@ def init_master(self, name, email, first_name, last_name, password, domain):
:param str last_name: The last name of the new user
:param str password: Password for the new user
:param str domain: The domain suffix for CTERA Portal
:param cterasdk.core.types.RemoteDatabase database,optional database: Remote database parameter.
"""
self._get_current_stage()

params = Setup._init_server_params(ServerMode.Master)
params = Setup._init_server_params(ServerMode.Master, database)
self._init_server(params, True)

if self.stage == SetupWizardStage.Portal:
Expand All @@ -64,10 +65,10 @@ def init_master(self, name, email, first_name, last_name, password, domain):
logger.warning('Portal already initialized. %s', {'host': self._core.host()})
self._core.startup.wait()

def _init_slave(self, ipaddr, secret):
def _init_slave(self, ipaddr, secret, database):
self._get_current_stage()
response = self._core.ctera.execute('/setup/authenticaionMethod', 'askMasterForSlaveAuthenticaionMethod', ipaddr)
params = Setup._init_server_params(ServerMode.Slave)
params = Setup._init_server_params(ServerMode.Slave, database)
params.slaveSettings.masterIpAddr = ipaddr
if response == SlaveAuthenticaionMethod.Password:
params.slaveSettings.masterPassword = secret
Expand All @@ -86,7 +87,11 @@ def _init_server(self, params, wait=False):
if params.serverMode == ServerMode.Slave:
form.add('masterIpAddr', params.slaveSettings.masterIpAddr)

logger.info('Initializing server. %s', {'host': self._core.host(), 'mode': params.serverMode})
logger.info('Initializing server. %s', {
'host': self._core.host(),
'mode': params.serverMode,
'database': params.databaseMode
})
self._core.ctera.multipart('/setup', form)
if wait:
status = SetupWizardStatusMonitor(self._core).wait(SetupWizardStage.Server)
Expand All @@ -95,25 +100,33 @@ def _init_server(self, params, wait=False):
else:
logger.warning('Server already initialized. %s', {'host': self._core.host()})

def init_application_server(self, ipaddr, secret):
def init_application_server(self, ipaddr, secret, *, database=None):
"""
Initialize a CTERA Portal Application Server.

:param str ipaddr: The CTERA Portal master server IP address
:param str secret: A password or a PEM-encoded private key
:param cterasdk.core.types.RemoteDatabase database,optional database: Remote database parameter.
"""
self._init_slave(ipaddr, secret)
self._init_slave(ipaddr, secret, database)
logger.info('Initializing Application Server. %s', {'host': ipaddr})
self._core.startup.wait()

@staticmethod
def _init_server_params(mode):
def _init_server_params(mode, database):
params = Object()
params._classname = 'InitServerParams' # pylint: disable=protected-access
params.serverMode = mode
if mode == ServerMode.Slave:
params.slaveSettings = Object()
params.slaveSettings._classname = 'SlaveServerSettings' # pylint: disable=protected-access

if database is not None:
params.databaseMode = 'external'
params.externalDatabaseConfig = database
else:
params.databaseMode = 'embedded'

return params

def get_setup_status(self):
Expand Down
95 changes: 95 additions & 0 deletions cterasdk/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ..common import DateTimeUtils, StringCriteriaBuilder, PredefinedListCriteriaBuilder, CustomListCriteriaBuilder, Object, \
parse_base_object_ref
from ..lib.storage import commonfs
from ..lib.crypto import PrivateKey, X509Certificate

from .enum import PortalAccountType, CollaboratorType, FileAccessMode, PlanCriteria, TemplateCriteria, ProtectionLevel, \
BucketType, LocationType, Platform, RetentionMode, Duration, ExtendedAttributes, ConflictHandler, NativeFormat
Expand Down Expand Up @@ -1345,3 +1346,97 @@ def __repr__(self):
f'enforce={self.enforce}, '
f'deleted={self.deleted})'
)


class SSLMode:
"""
SSL/TLS Connection Verification Mode.

:ivar REQUIRE: Require an encrypted connection without certificate verification.
:ivar VERIFY_CA: Require encryption and verify the server certificate against the CA.
:ivar VERIFY_FULL: Require encryption, verify the CA, and validate the server hostname (default).
"""
REQUIRE = 'require'
VERIFY_CA = 'verify-ca'
VERIFY_FULL = 'verify-full'


class RemoteDatabase(Object): # pylint: disable=too-many-instance-attributes
"""
Remote database configuration.

:ivar host: Remote database hostname or IP address.
:ivar port: Remote database port.
:ivar name: Database name.
:ivar authMode: Authentication mode.
:ivar username: Authentication username.
:ivar password: Authentication password (optional).
:ivar sslMode: SSL/TLS connection mode.
:ivar sslCaCert: SSL CA certificate (optional).
:ivar authCert: Client authentication certificate (optional).
:ivar authKey: Client authentication private key (optional).
"""

def __init__(self, host, port, name, auth_mode, ssl_mode, ssl_ca_cert=None, username=None,
password=None, auth_cert=None, auth_key=None):
super().__init__()
self._classname = 'ExternalDatabaseConfig'
self.host = host
self.port = port
self.databaseName = name
self.authMode = auth_mode
self.username = username
self.sslMode = ssl_mode

if password is not None:
self.password = password
if ssl_ca_cert is not None:
self.sslCaCert = ssl_ca_cert
if auth_cert is not None:
self.authCert = auth_cert
if auth_key is not None:
self.authKey = auth_key

@staticmethod
def _raise_on_validation_error(ssl_mode):
if ssl_mode not in SSLMode.__dict__.values():
raise ValueError(f"Invalid SSL mode: {ssl_mode}")

@staticmethod
def credentials(host, port, name, username, password, ssl_mode, ssl_ca_cert=None):
"""
Connect to a remote database using password authentication.

:param str host: Remote database hostname or IP address.
:param int port: Remote database port.
:param str name: Database name.
:param str username: Authentication username.
:param str password: Authentication password.
:param cterasdk.core.types.SSLMode ssl_mode: SSL/TLS connection mode.
:param str,optional ssl_ca_cert: SSL CA certificate.
:rtype: cterasdk.core.types.RemoteDatabase
"""
RemoteDatabase._raise_on_validation_error(ssl_mode)
return RemoteDatabase(host, port, name, 'Credentials', ssl_mode,
X509Certificate.load_certificate(ssl_ca_cert) if ssl_ca_cert else None,
username=username, password=password)

@staticmethod
def certificate(host, port, name, auth_cert, auth_key, ssl_mode, ssl_ca_cert=None):
"""
Connect to a remote database using certificate authentication.

:param str host: Database hostname or IP address.
:param int port: Database port.
:param str name: Database name.
:param str auth_cert: Client authentication certificate.
:param str auth_key: Client authentication private key.
:param str ssl_mode: SSL/TLS connection mode.
:param str,optional ssl_ca_cert: SSL CA certificate.
:rtype: cterasdk.core.types.RemoteDatabase
"""
RemoteDatabase._raise_on_validation_error(ssl_mode)
return RemoteDatabase(host, port, name, 'Cert', ssl_mode,
X509Certificate.load_certificate(ssl_ca_cert) if ssl_ca_cert else None,
auth_cert=X509Certificate.load_certificate(auth_cert),
auth_key=PrivateKey.load_private_key(auth_key))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certificates omitted from setup payload

High Severity

RemoteDatabase.credentials and RemoteDatabase.certificate store X509Certificate and PrivateKey objects on sslCaCert, authCert, and authKey. The XML serializer only emits primitives and Object instances, so those fields are dropped from the setup request. Certificate authentication and custom CA verification never reach the Portal.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c0f8207. Configure here.

2 changes: 2 additions & 0 deletions docs/source/UserGuides/Miscellaneous/Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ Bug Fixes
^^^^^^^^^

* Add additional metadata to the ``export`` CLI command.
* Support initializing CTERA Portal using an external database server.

Related issues and pull requests on GitHub: `#378 <https://github.com/ctera/ctera-python-sdk/pull/378>`_
`#380 <https://github.com/ctera/ctera-python-sdk/pull/380>`_


2.20.50
Expand Down
77 changes: 77 additions & 0 deletions docs/source/UserGuides/Portal/Administration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,45 @@ Setup

admin.setup.init_master('admin', 'bruce.wayne@we.com', 'Bruce', 'Wayne', 'password1!', 'ctera.me')

"""Initialize the master server using a remote database, authenticating using a certificate"""

admin.setup.init_master(
name='admin',
email='admin@example.com',
first_name='John',
last_name='Doe',
password='SecurePassword123!',
domain='example.com',
database=RemoteDatabase.certificate(
host='db.example.com',
port=5432,
name='portal',
auth_cert='/path/to/client.crt',
auth_key='/path/to/client.key',
ssl_mode=SSLMode.VERIFY_FULL,
ssl_ca_cert='/path/to/ca.crt'
)
)

"""Initialize the master server using a remote database, authenticating using a username and password"""
admin.setup.init_master(
name='admin',
email='admin@example.com',
first_name='John',
last_name='Doe',
password='SecurePassword123!',
domain='example.com',
database=RemoteDatabase.password(
host='db.example.com',
port=5432,
name='portal',
username='dbuser',
password='dbpassword',
ssl_mode=SSLMode.VERIFY_FULL,
ssl_ca_cert='/path/to/ca.crt'
)
)

.. automethod:: cterasdk.core.setup.Setup.init_application_server
:noindex:

Expand All @@ -1974,6 +2013,44 @@ Setup
master_pk = """...PEM-encoded private key..."""
admin.setup.init_application_server(master_ipaddr, master_pk)

"""Join an Application Server to a Portal cluster using a remote database server, using username and password."""

master_ipaddr = '172.31.53.246'
master_pk = """...PEM-encoded private key..."""

admin.setup.init_application_server(
master_ipaddr,
master_pk,
database=RemoteDatabase.credentials(
host='db.example.com',
port=5432,
name='portal',
username='dbuser',
password='dbpassword',
ssl_mode=SSLMode.VERIFY_FULL,
ssl_ca_cert='/path/to/ca.crt'
)
)

"""Join an Application Server to a Portal cluster using a remote database server, authenticated using a certificate."""

master_ipaddr = '172.31.53.246'
master_pk = """...PEM-encoded private key..."""

admin.setup.init_application_server(
master_ipaddr,
master_pk,
database=RemoteDatabase.certificate(
host='db.example.com',
port=5432,
name='portal',
auth_cert='/path/to/client.crt',
auth_key='/path/to/client.key',
ssl_mode=SSLMode.VERIFY_FULL,
ssl_ca_cert='/path/to/ca.crt'
)
)


Miscellaneous
=============
Expand Down
1 change: 1 addition & 0 deletions tests/ut/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


class BaseTest(unittest.TestCase):
maxDiff = None

def setUp(self):
super().setUp()
Expand Down
1 change: 1 addition & 0 deletions tests/ut/core/admin/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,5 @@ def _get_init_server_params(mode, authentication_method=None, master_ipaddr=None
params.slaveSettings.masterPassword = master_secret
elif authentication_method == SlaveAuthenticaionMethod.PrivateKey:
params.slaveSettings.masterKey = master_secret
params.databaseMode = 'embedded'
return params
Loading