MonkeyCanCode commented on code in PR #4770: URL: https://github.com/apache/polaris/pull/4770#discussion_r3432927069
########## client/python/apache_polaris/cli/profile_config.py: ########## @@ -0,0 +1,83 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +import json +import os +import stat +import tempfile +from typing import Any, Dict + +from apache_polaris.cli.constants import CONFIG_DIR, CONFIG_FILE + +CONFIG_FILE_MODE = 0o600 +MASKED_CLIENT_SECRET = "********" + + +def mask_client_secret(client_secret: str | None) -> str | None: + if client_secret is None: + return None + if client_secret == "": + return "" + return MASKED_CLIENT_SECRET + + +def format_profile_for_display(profile: dict) -> dict: + displayed = dict(profile) + if "client_secret" in displayed: + displayed["client_secret"] = mask_client_secret(displayed["client_secret"]) + return displayed + + +def ensure_config_file_permissions(path: str | None = None) -> None: + if path is None: + path = CONFIG_FILE + if not os.path.exists(path): + return + current_mode = stat.S_IMODE(os.stat(path).st_mode) + if current_mode != CONFIG_FILE_MODE: + os.chmod(path, CONFIG_FILE_MODE) + + +def load_profiles() -> Dict[str, Dict[str, Any]]: + if not os.path.exists(CONFIG_FILE): + return {} + ensure_config_file_permissions() Review Comment: It is not idea to change file permission here as the load operation should be read-only. ########## client/python/tests/test_profile_config.py: ########## @@ -0,0 +1,134 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +import json +import os +import stat +import tempfile +import unittest +from unittest.mock import patch + Review Comment: Consider adding the missing tests to handle edge cases such as: 1. mask_client_secret(None) 2. mask_client_secret("") 3. ensure_config_file_permissions on non-existed file ########## client/python/apache_polaris/cli/command/profiles.py: ########## @@ -59,15 +61,11 @@ class ProfilesCommand(Command): def _load_profiles(self) -> Dict[str, Dict[str, Any]]: if not os.path.exists(CONFIG_FILE): Review Comment: With the introduction of `profile_config.py`, this file exists check is no longer needed as `load_profiles()` also perform the same. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
