Skip to content

Commit eef54c7

Browse files
caladdbsanders
authored andcommitted
BUGFIX: Fix VM MAC generation
The old code used `hex` to convert some random bytes to hex. The `hex` command throws away leading zeros, resulting in MAC addresses that were too short. This code swaps that out for `secrets.hex_token` which will always return the correct number of characters.
1 parent 3644258 commit eef54c7

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

  • common/src/stack/command/stack/commands/report/vm

common/src/stack/command/stack/commands/report/vm/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
#
77

88
import stack.commands
9+
910
import random
1011
import uuid
1112
import jinja2
1213
from jinja2 import Environment, meta
1314
from pathlib import Path
15+
import secrets
16+
1417
from stack.kvm import Hypervisor
1518
from stack.exception import CommandError, ParamError
1619
from stack.argument_processors.host import HostArgProcessor
@@ -149,13 +152,11 @@ def run(self, param, args):
149152

150153
def getMAC(self):
151154
"""
152-
Generate a random mac address for
153-
virtual machine interfaces
155+
Generate a random mac address for virtual machine interfaces
154156
"""
155157

156-
# Convert 24 random bits to hex
157-
# and truncate the 0x
158-
mac = hex(random.getrandbits(24))[2:]
158+
# Generate 6 random hex chars
159+
mac = secrets.token_hex(3)
159160

160161
# Assign every 2 hex digits to the new mac address
161162
# The first 6 digits are the VirtIO mac manufacturer address

0 commit comments

Comments
 (0)