Azure / Azure/azure-cli-extensions

Writing a cert file to a directory without permission system may fail

Open
#8,994 1 comment 0 reactions 0 assignees View on GitHub
Auto-Assign bug customer-reported Service Attention VM SSH
Dominant language
Python
Stars
454
Forks
1.7k
Avg merge
2d 19h
Merged PRs (30d)
64

Description

### Describe the bug

In [this line](https://github.com/Azure/azure-cli-extensions/blob/e21724da2eab3cd0224dd6d72c5513c691528a56/src/ssh/azext_ssh/custom.py#L347), after writing a certificate to file, the extension will set the mode. However if that file is located in a directory where the permission is not available, like a memory file system, function `set_mode` may either crash or raise an exception. There should be an option to disable setting the mode or ignore the failure with a warning message.

### Related command

`az ssh cert --public-key-file id_rsa.pub --file x:\memoryfs`

### Errors

```
...
File "C:\Users\heran.quan\.azure\cliextensions\ssh\azext_ssh\custom.py", line 242, in _get_and_write_certificate
_write_cert_file(certificate, cert_file)
File "C:\Users\heran.quan\.azure\cliextensions\ssh\azext_ssh\custom.py", line 347, in _write_cert_file
oschmod.set_mode(cert_file, 0o644)
File "C:\Users\heran.quan\.azure\cliextensions\ssh\oschmod\__init__.py", line 186, in set_mode
return win_set_permissions(path, new_mode)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\heran.quan\.azure\cliextensions\ssh\oschmod\__init__.py", line 403, in win_set_permissions
_win_set_permissions(path, mode, get_object_type(path))
File "C:\Users\heran.quan\.azure\cliextensions\ssh\oschmod\__init__.py", line 421, in _win_set_permissions
for _ in range(0, dacl.GetAceCount()):
^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'GetAceCount'
```

### Issue script & Debug output

```
import win32security # According to "https://pypi.org/project/Win32Security", "This project has been archived."
# x:\ is a memory file system and the ACL may not be available
sec_des = win32security.GetNamedSecurityInfo("x:\\", win32security.SE_FILE_OBJECT,win32security.DACL_SECURITY_INFORMATION)
dacl = sec_des.GetSecurityDescriptorDacl()
print(dacl)
```

### Expected behavior

Raise an exception and give us an option to not set mode
Or print an warning

### Environment Summary

```
{
"azure-cli": "2.75.0",
"azure-cli-core": "2.75.0",
"azure-cli-telemetry": "1.1.0",
"extensions": {
"ssh": "2.0.6"
}
}
```

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.