Skip to content

Commit a864ec6

Browse files
committed
vmm-cli: Update allowed_envs when secrets updated
1 parent e0107ee commit a864ec6

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

vmm/src/vmm-cli.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ def create_vm(self, args) -> None:
603603

604604
def update_vm_env(self, vm_id: str, envs: Dict[str, str], kms_urls: Optional[List[str]] = None) -> None:
605605
"""Update environment variables for a VM"""
606+
envs = envs or {}
606607
# First get the VM info to retrieve the app_id
607608
vm_info_response = self.rpc_call('GetInfo', {'id': vm_id})
608609

@@ -611,6 +612,8 @@ def update_vm_env(self, vm_id: str, envs: Dict[str, str], kms_urls: Optional[Lis
611612

612613
app_id = vm_info_response['info']['app_id']
613614
print(f"Retrieved app ID: {app_id}")
615+
vm_configuration = vm_info_response['info'].get('configuration') or {}
616+
compose_file = vm_configuration.get('compose_file')
614617

615618
# Now get the encryption key for the app
616619
encrypt_pubkey = self.get_app_env_encrypt_pub_key(
@@ -620,8 +623,31 @@ def update_vm_env(self, vm_id: str, envs: Dict[str, str], kms_urls: Optional[Lis
620623
encrypted_env = encrypt_env(envs_list, encrypt_pubkey)
621624

622625
# Use UpdateApp with the VM ID
623-
self.rpc_call('UpgradeApp', {'id': vm_id,
624-
'encrypted_env': encrypted_env})
626+
payload = {'id': vm_id, 'encrypted_env': encrypted_env}
627+
628+
if compose_file:
629+
try:
630+
app_compose = json.loads(compose_file)
631+
except json.JSONDecodeError:
632+
app_compose = {}
633+
compose_changed = False
634+
allowed_envs = list(envs.keys())
635+
if app_compose.get('allowed_envs') != allowed_envs:
636+
app_compose['allowed_envs'] = allowed_envs
637+
compose_changed = True
638+
launch_token_value = envs.get('APP_LAUNCH_TOKEN')
639+
if launch_token_value is not None:
640+
launch_token_hash = hashlib.sha256(
641+
launch_token_value.encode('utf-8')
642+
).hexdigest()
643+
if app_compose.get('launch_token_hash') != launch_token_hash:
644+
app_compose['launch_token_hash'] = launch_token_hash
645+
compose_changed = True
646+
if compose_changed:
647+
payload['compose_file'] = json.dumps(
648+
app_compose, indent=4, ensure_ascii=False)
649+
650+
self.rpc_call('UpgradeApp', payload)
625651
print(f"Environment variables updated for VM {vm_id}")
626652

627653
def update_vm_user_config(self, vm_id: str, user_config: str) -> None:

0 commit comments

Comments
 (0)