Skip to content

Commit 9acc81f

Browse files
committed
fix salt.grains.core.get_server_id
1 parent 0dab3f4 commit 9acc81f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

pkg/hook-salt.py

+19
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,22 @@
8888
hiddenimports = HIDDEN_IMPORTS
8989
datas = DATAS
9090
binaries = BINARIES
91+
92+
def _patch_salt_grains_core_server_id():
93+
import subprocess
94+
import salt.config # must import before salt.grains.core
95+
import salt.grains.core
96+
import sys
97+
98+
with open('pkg/salt.grains.core.patch', 'rb') as fh:
99+
patch_data = fh.read()
100+
101+
process = subprocess.Popen(['patch',
102+
'--forward', # ignore patches that appear reversed or already applied
103+
'--batch', # ask no questions
104+
salt.grains.core.__file__],
105+
stdin=subprocess.PIPE)
106+
stdout, stderr = process.communicate(patch_data)
107+
sys.stderr.write('patching complete\n')
108+
109+
_patch_salt_grains_core_server_id()

pkg/salt.grains.core.patch

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--- core-with-stupid-forkbomb.py 2020-01-14 09:26:55.941413640 -0500
2+
+++ core-without-stupid-forkbomb.py 2020-02-27 16:07:18.924955413 -0500
3+
@@ -2775,7 +2775,6 @@
4+
5+
return grains
6+
7+
-
8+
def get_server_id():
9+
'''
10+
Provides an integer based on the FQDN of a machine.
11+
@@ -2793,18 +2792,14 @@
12+
if py_ver >= (3, 3):
13+
# Python 3.3 enabled hash randomization, so we need to shell out to get
14+
# a reliable hash.
15+
- id_hash = __salt__['cmd.run'](
16+
- [sys.executable, '-c', 'print(hash("{0}"))'.format(id_)],
17+
- env={'PYTHONHASHSEED': '0'}
18+
- )
19+
- try:
20+
- id_hash = int(id_hash)
21+
- except (TypeError, ValueError):
22+
- log.debug(
23+
- 'Failed to hash the ID to get the server_id grain. Result of '
24+
- 'hash command: %s', id_hash
25+
- )
26+
- id_hash = None
27+
+ #
28+
+ # but implementing a forkbomb would be not only annoying, it would also
29+
+ # be wreckless and stupid.
30+
+
31+
+ import hashlib
32+
+ md5 = hashlib.md5()
33+
+ md5.update( f'{id_}'.encode() )
34+
+ id_hash = int( md5.hexdigest(), 16 )
35+
if id_hash is None:
36+
# Python < 3.3 or error encountered above
37+
id_hash = hash(id_)

0 commit comments

Comments
 (0)