Skip to content

Commit

Permalink
[2.5] Support log file rotation in admin commands (#2624)
Browse files Browse the repository at this point in the history
* support log file rotation

* apply the change to other places

* add docstr
  • Loading branch information
yanchengnv authored Jun 10, 2024
1 parent e084b54 commit 413ffa1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 3 additions & 1 deletion nvflare/fuel/hci/client/fl_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from nvflare.fuel.hci.client.api_status import APIStatus
from nvflare.fuel.hci.client.fl_admin_api_constants import FLDetailKey
from nvflare.fuel.hci.client.fl_admin_api_spec import APISyntaxError, FLAdminAPIResponse, FLAdminAPISpec, TargetType
from nvflare.fuel.hci.cmd_arg_utils import get_file_extension
from nvflare.security.logging import secure_format_exception

from .overseer_service_finder import ServiceFinderByOverseer
Expand Down Expand Up @@ -209,7 +210,8 @@ def _validate_file_string(self, file: str) -> str:
for p in paths:
if p == "..":
raise APISyntaxError(".. in file path is not allowed")
basename, file_extension = os.path.splitext(file)

file_extension = get_file_extension(file)
if file_extension not in [".txt", ".log", ".json", ".csv", ".sh", ".config", ".py"]:
raise APISyntaxError(
"this command cannot be applied to file {}. Only files with the following extensions are "
Expand Down
21 changes: 20 additions & 1 deletion nvflare/fuel/hci/cmd_arg_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,29 @@ def validate_path_string(path: str) -> str:
return path


def get_file_extension(file: str) -> str:
"""Get extension part of the specified file name.
If the file's name is ended with number, then the extension is before it.
Args:
file: the file name
Returns: extension part of the file name
"""
parts = file.split(".")
last_part = parts[-1]
if last_part.isnumeric():
parts.pop(-1)
file = ".".join(parts)
_, ex = os.path.splitext(file)
return ex


def validate_file_string(file: str) -> str:
"""Returns the file string if it is valid."""
validate_path_string(file)
basename, file_extension = os.path.splitext(file)
file_extension = get_file_extension(file)
if file_extension not in [".txt", ".log", ".json", ".csv", ".sh", ".config", ".py"]:
raise SyntaxError(
"this command cannot be applied to file {}. Only files with the following extensions are "
Expand Down
7 changes: 4 additions & 3 deletions nvflare/private/fed/server/shell_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import re
import subprocess
from typing import List

from nvflare.fuel.hci.cmd_arg_utils import join_args
from nvflare.fuel.hci.cmd_arg_utils import get_file_extension, join_args
from nvflare.fuel.hci.conn import Connection
from nvflare.fuel.hci.proto import MetaStatusValue, make_meta
from nvflare.fuel.hci.reg import CommandModule, CommandModuleSpec, CommandSpec
Expand Down Expand Up @@ -186,7 +185,9 @@ def validate_shell_command(self, args: List[str], parse_result):
return ".. in path name is not allowed"

if self.text_file_only:
basename, file_extension = os.path.splitext(f)
# check whether the file name is ended with numbers. If so, the actual file extension is before it.
# this is the case that when the log file (log.txt) is rotated, the previous file becomes log.txt.1.
file_extension = get_file_extension(f)
if file_extension not in [".txt", ".log", ".json", ".csv", ".sh", ".config", ".py"]:
return (
"this command cannot be applied to file {}. Only files with the following extensions "
Expand Down

0 comments on commit 413ffa1

Please sign in to comment.