Skip to content

replace() argument 2 must be str, not None when using per-server Password Exec Command with Service field (pg_service.conf) #10237

Description

@wassid

When a server is configured in pgAdmin using the Service field (pointing to a pg_service.conf entry) with host, port, and username left blank, and a per-server Password Exec Command is set, connecting to the server raises: replace() argument 2 must be str, not None

Steps to Reproduce

Create a pg_service.conf entry with host, port, dbname, and user defined.
In pgAdmin, register a new server. In the Connection tab, set only the Service field to the service name. Leave host, port, username, and password blank.
In the same Connection tab, set a Password Exec Command (e.g., a PowerShell script that retrieves an Azure AD access token via az account get-access-token).
Attempt to connect to the server.
Expected Result

The password exec command runs and its output is used as the password, establishing the connection successfully.

Actual Result

The connection fails immediately with: replace() argument 2 must be str, not None

Root Cause

The bug is in web/pgadmin/utils/passexec.py in the PasswordExec.get() method:

self.cmd = self.cmd.replace('%HOSTNAME%', self.host)
self.cmd = self.cmd.replace('%PORT%', str(self.port))
self.cmd = self.cmd.replace('%USERNAME%', driver.qtIdent(None, self.username))

When the server is configured with only the Service field set, self.host, self.port, and self.username are all None. Passing None as the second argument to str.replace() raises the error.

Suggested Fix

self.cmd = self.cmd.replace('%HOSTNAME%', self.host or '')
self.cmd = self.cmd.replace('%PORT%', str(self.port) if self.port is not None else '')
self.cmd = self.cmd.replace('%USERNAME%', driver.qtIdent(None, self.username) if self.username else '')

Environment

OS: Windows 11
pgAdmin version: 9.17
PostgreSQL: Azure Database for PostgreSQL (Flexible Server)
Authentication: Azure AD access token via az account get-access-token

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions