Skip to content

Commit 6deb33a

Browse files
author
Timmy Welch
committed
Update connectors, facts and operations for new MaskString
1 parent 6ca6575 commit 6deb33a

5 files changed

Lines changed: 36 additions & 18 deletions

File tree

src/pyinfra/connectors/util.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,10 @@ def make_unix_command(
409409
[
410410
"env",
411411
StringCommand("SUDO_ASKPASS=", QuoteString(_sudo_askpass_path), _separator=""),
412-
MaskString(
413-
"{0}={1}".format(
414-
SUDO_ASKPASS_ENV_VAR,
415-
StringCommand(QuoteString(_sudo_password)).get_raw_value(),
416-
)
412+
StringCommand(
413+
SUDO_ASKPASS_ENV_VAR,
414+
StringCommand(QuoteString(MaskString(_sudo_password))).get_raw_value(),
415+
_separator="=",
417416
),
418417
],
419418
)
@@ -440,11 +439,10 @@ def make_unix_command(
440439
command_bits.extend(
441440
[
442441
"env",
443-
MaskString(
444-
"{0}={1}".format(
445-
SU_ASKPASS_ENV_VAR,
446-
StringCommand(QuoteString(_su_password)).get_raw_value(),
447-
)
442+
StringCommand(
443+
SU_ASKPASS_ENV_VAR,
444+
StringCommand(QuoteString(MaskString(_su_password))).get_raw_value(),
445+
_separator="=",
448446
),
449447
QuoteString(_su_askpass_path),
450448
"|",

src/pyinfra/facts/mysql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def make_mysql_command(
3030

3131
if password:
3232
# Quote the password as it may contain special characters
33-
target_bits.append(MaskString('-p"{0}"'.format(password)))
33+
target_bits.append(StringCommand("-p", QuoteString(MaskString(password)), _separator=""))
3434

3535
if host:
3636
target_bits.append("-h{0}".format(host))

src/pyinfra/facts/postgres.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ def make_psql_command(
1616
port: str | int | None = None,
1717
executable="psql",
1818
) -> StringCommand:
19-
target_bits: list[str] = []
19+
target_bits: list[str | StringCommand] = []
2020

2121
if password:
22-
target_bits.append(MaskString('PGPASSWORD="{0}"'.format(password)))
22+
target_bits.append(
23+
StringCommand(
24+
"PGPASSWORD",
25+
QuoteString(MaskString(password)),
26+
_separator="=",
27+
)
28+
)
2329

2430
target_bits.append(executable)
2531

src/pyinfra/operations/mysql.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,15 @@ def user(
171171
return
172172

173173
if present and not is_present:
174-
sql_bits = ['CREATE USER "{0}"@"{1}"'.format(user, user_hostname)]
174+
sql_bits: list[str | StringCommand] = [
175+
'CREATE USER "{0}"@"{1}"'.format(user, user_hostname)
176+
]
175177
if password:
176-
sql_bits.append(MaskString('IDENTIFIED BY "{0}"'.format(password)))
178+
sql_bits.append(
179+
StringCommand(
180+
"IDENTIFIED BY", StringCommand('"', MaskString(password), '"', _separator="")
181+
)
182+
)
177183

178184
if require == "SSL":
179185
sql_bits.append("REQUIRE SSL")

src/pyinfra/operations/postgres.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def role(
141141

142142
# If we want the user and they don't exist
143143
if not is_present:
144-
sql_bits = ['CREATE ROLE "{0}"'.format(role)]
144+
sql_bits: list[str | StringCommand] = ['CREATE ROLE "{0}"'.format(role)]
145145

146146
for key, value in (
147147
("LOGIN", login),
@@ -158,7 +158,11 @@ def role(
158158
sql_bits.append("CONNECTION LIMIT {0}".format(connection_limit))
159159

160160
if password:
161-
sql_bits.append(MaskString("PASSWORD '{0}'".format(password)))
161+
sql_bits.append(
162+
StringCommand(
163+
"PASSWORD", StringCommand("'", MaskString(password), "'", _separator="")
164+
)
165+
)
162166

163167
yield make_execute_psql_command(
164168
StringCommand(*sql_bits),
@@ -196,7 +200,11 @@ def role(
196200
sql_bits.append("CONNECTION LIMIT {0}".format(connection_limit))
197201
should_execute = True
198202
if password:
199-
sql_bits.append(MaskString("PASSWORD '{0}'".format(password)))
203+
sql_bits.append(
204+
StringCommand(
205+
"PASSWORD", StringCommand("'", MaskString(password), "'", _separator="")
206+
)
207+
)
200208
should_execute = True
201209

202210
if should_execute:

0 commit comments

Comments
 (0)