1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
3 # Copyright (C) 2022 Huawei Technologies Co., Ltd. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 # ============LICENSE_END=========================================================
18 CLI parsing for the sync utility.
19 convert flags/env variables to configuration
27 from urllib.parse import urlsplit
29 import policysync.clients as clients
30 import policysync.coroutines
31 from .util import get_module_logger
34 logger = get_module_logger(__name__)
36 APPLICATION_JSON = "application/json"
39 Config = collections.namedtuple(
40 'Config', ['out_file', 'check_period', 'filters', 'ids', 'client', 'bind'])
45 Parse the command into a config object
46 :param args: arguments list for parsing
47 :returns: Config for the policy sync
49 parser = argparse.ArgumentParser(
50 description="Keeps a file updated with policies matching a filter.",
51 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
57 default=os.environ.get("POLICY_SYNC_OUTFILE", "policies.json"),
58 help="Output file to dump to",
64 default=os.environ.get("POLICY_SYNC_DURATION", 1200),
65 help="frequency (in seconds) to conduct periodic check",
71 default=os.environ.get("POLICY_SYNC_FILTER", "[]"),
72 help="Regex of policies that you are interested in.",
77 default=os.environ.get("POLICY_SYNC_ID", "[]"),
78 help="Specific names of policies you are interested in.",
84 default=os.environ.get("POLICY_SYNC_PDP_USER", None),
85 help="PDP basic auth username",
90 default=os.environ.get("POLICY_SYNC_PDP_PASS", None),
91 help="PDP basic auth password",
97 default=os.environ.get("POLICY_SYNC_PDP_URL", None),
98 help="PDP to connect to",
104 default=os.environ.get("POLICY_SYNC_HTTP_BIND", "localhost:8000"),
105 help="The bind address for container metrics",
111 default=os.environ.get("POLICY_SYNC_HTTP_METRICS", True),
112 help="turn on or off the prometheus metrics",
118 default=os.environ.get("POLICY_SYNC_V0_ENABLE", False),
119 help="Turn on usage of the legacy v0 policy API",
125 default=os.environ.get("POLICY_SYNC_LOGGING_CONFIG", None),
126 help="Python formatted logging configuration file",
129 # V0 API specific configuration
131 "--v0-notify-endpoint",
133 default=os.environ.get(
134 "POLICY_SYNC_V0_NOTIFIY_ENDPOINT", "pdp/notifications"
136 help="Path of the v0 websocket notification",
140 "--v0-decision-endpoint",
142 default=os.environ.get("POLICY_SYNC_V0_DECISION_ENDPOINT", "pdp/api"),
143 help="path of the v0 decision endpoint",
146 # V1 API specific configuration
150 default=os.environ.get("POLICY_SYNC_V1_DMAAP_URL", None),
151 help="URL of the dmaap topic used in v1 api for notifications",
157 default=os.environ.get("POLICY_SYNC_V1_DMAAP_USER", None),
158 help="User to use with with the dmaap topic"
164 default=os.environ.get("POLICY_SYNC_V1_DMAAP_PASS", None),
165 help="Password to use with the dmaap topic"
169 "--v1-decision-endpoint",
171 default=os.environ.get(
172 "POLICY_SYNC_V1_PDP_DECISION_ENDPOINT",
173 "policy/pdpx/v1/decision"
175 help="Decision endpoint used in the v1 api for notifications",
178 args = parser.parse_args(args)
180 if args.logging_config:
181 logging.config.fileConfig(
183 disable_existing_loggers=False
186 handler = logging.StreamHandler()
187 formatter = logging.Formatter(
188 "[%(asctime)s][%(levelname)-5s]%(message)s"
190 root = logging.getLogger()
191 handler.setFormatter(formatter)
192 root.addHandler(handler)
193 root.setLevel(logging.INFO)
195 bind = args.http_bind if args.http_metrics else None
197 client = clients.get_client(
199 pdp_user=args.pdp_user,
200 pdp_password=args.pdp_pass,
202 v0_decision=args.v0_decision_endpoint,
203 v0_notifications=args.v0_notify_endpoint,
204 v1_decision=args.v1_decision_endpoint,
205 dmaap_url=args.v1_dmaap_topic,
206 dmaap_user=args.v1_dmaap_user,
207 dmaap_password=args.v1_dmaap_pass
211 bind = urlsplit("//" + bind)
215 check_period=args.duration,
216 filters=yaml.safe_load(args.filters),
217 ids=yaml.safe_load(args.ids),
225 Parse the arguments passed in via the command line and start the app
228 config = parsecmd(sys.argv[1:])
231 "There was no POLICY_SYNC_PDP_URL set or --pdp flag set"
234 policysync.coroutines.start_event_loop(config)