-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathconfig.py
More file actions
142 lines (119 loc) · 4.31 KB
/
config.py
File metadata and controls
142 lines (119 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 2025 Google LLC
#
# Licensed 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.
#
"""Google SecOps CLI config commands"""
from secops.cli.constants import CONFIG_FILE
from secops.cli.utils.common_args import (
add_chronicle_args,
add_common_args,
add_time_range_args,
)
from secops.cli.utils.config_utils import load_config, save_config
def setup_config_command(subparsers):
"""Set up the config command parser.
Args:
subparsers: Subparsers object to add to
"""
config_parser = subparsers.add_parser(
"config", help="Manage CLI configuration"
)
config_subparsers = config_parser.add_subparsers(help="Config command")
config_parser.set_defaults(func=lambda args: config_parser.print_help())
# Set config command
set_parser = config_subparsers.add_parser(
"set", help="Set configuration values"
)
set_parser.add_argument(
"--local",
action="store_true",
help="Save configuration to current directory (.secops/config.json)",
)
add_chronicle_args(set_parser)
add_common_args(set_parser)
add_time_range_args(set_parser)
set_parser.set_defaults(func=handle_config_set_command)
# View config command
view_parser = config_subparsers.add_parser(
"view", help="View current configuration"
)
view_parser.set_defaults(func=handle_config_view_command)
# Clear config command
clear_parser = config_subparsers.add_parser(
"clear", help="Clear current configuration"
)
clear_parser.set_defaults(func=handle_config_clear_command)
def handle_config_set_command(args, chronicle=None):
"""Handle config set command.
Args:
args: Command line arguments
chronicle: Not used for this command
"""
# If saving to local, we should probably start with empty or
# current local config to avoid polluting it with global values.
# But for now, we follow the pattern of loading merged config and
# updating it.
# Optimization: If --local, maybe we should only load local config?
# But load_config() returns merged.
# Let's use load_config() but be aware we might save merged values to local.
config = load_config()
# Update config with new values
if args.customer_id:
config["customer_id"] = args.customer_id
if args.project_id:
config["project_id"] = args.project_id
if args.region:
config["region"] = args.region
if hasattr(args, "api_version") and args.api_version:
config["api_version"] = args.api_version
if args.service_account:
config["service_account"] = args.service_account
if args.start_time:
config["start_time"] = args.start_time
if args.end_time:
config["end_time"] = args.end_time
if args.time_window is not None:
config["time_window"] = args.time_window
save_config(config, local=args.local)
target = "local" if args.local else "global"
print(f"Configuration saved to {target} config file")
# Unused argument
_ = (chronicle,)
def handle_config_view_command(args, chronicle=None):
"""Handle config view command.
Args:
args: Command line arguments
chronicle: Not used for this command
"""
config = load_config()
if not config:
print("No configuration found.")
return
print("Current configuration:")
for key, value in config.items():
print(f" {key}: {value}")
# Unused arguments
_ = (args, chronicle)
def handle_config_clear_command(args, chronicle=None):
"""Handle config clear command.
Args:
args: Command line arguments
chronicle: Not used for this command
"""
if CONFIG_FILE.exists():
CONFIG_FILE.unlink()
print("Configuration cleared.")
else:
print("No configuration found.")
# Unused arguments
_ = (args, chronicle)