1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
17 CLI parsing for the sync utility.
18 convert flags/env variables to configuration
26 from urllib.parse import urlsplit
28 import policysync.clients as clients
29 import policysync.coroutines
30 from .util import get_module_logger
33 logger = get_module_logger(__name__)
35 APPLICATION_JSON = "application/json"
38 Config = collections.namedtuple(
39 'Config', ['out_file', 'check_period', 'filters', 'ids', 'client', 'bind'])
44 Parse the command into a config object
45 :param args: arguments list for parsing
46 :returns: Config for the policy sync
48 parser = argparse.ArgumentParser(
49 description="Keeps a file updated with policies matching a filter.",
50 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
56 default=os.environ.get("POLICY_SYNC_OUTFILE", "policies.json"),
57 help="Output file to dump to",
63 default=os.environ.get("POLICY_SYNC_DURATION", 1200),
64 help="frequency (in seconds) to conduct periodic check",
70 default=os.environ.get("POLICY_SYNC_FILTER", "[]"),
71 help="Regex of policies that you are interested in.",
76 default=os.environ.get("POLICY_SYNC_ID", "[]"),
77 help="Specific names of policies you are interested in.",
83 default=os.environ.get("POLICY_SYNC_PDP_USER", None),
84 help="PDP basic auth username",
89 default=os.environ.get("POLICY_SYNC_PDP_PASS", None),
90 help="PDP basic auth password",
96 default=os.environ.get("POLICY_SYNC_PDP_URL", None),
97 help="PDP to connect to",
103 default=os.environ.get("POLICY_SYNC_HTTP_BIND", "localhost:8000"),
104 help="The bind address for container metrics",
110 default=os.environ.get("POLICY_SYNC_HTTP_METRICS", True),
111 help="turn on or off the prometheus metrics",
117 default=os.environ.get("POLICY_SYNC_V0_ENABLE", False),
118 help="Turn on usage of the legacy v0 policy API",
124 default=os.environ.get("POLICY_SYNC_LOGGING_CONFIG", None),
125 help="Python formatted logging configuration file",
128 # V0 API specific configuration
130 "--v0-notify-endpoint",
132 default=os.environ.get(
133 "POLICY_SYNC_V0_NOTIFIY_ENDPOINT", "pdp/notifications"
135 help="Path of the v0 websocket notification",
139 "--v0-decision-endpoint",
141 default=os.environ.get("POLICY_SYNC_V0_DECISION_ENDPOINT", "pdp/api"),
142 help="path of the v0 decision endpoint",
145 # V1 API specific configuration
149 default=os.environ.get("POLICY_SYNC_V1_DMAAP_URL", None),
150 help="URL of the dmaap topic used in v1 api for notifications",
156 default=os.environ.get("POLICY_SYNC_V1_DMAAP_USER", None),
157 help="User to use with with the dmaap topic"
163 default=os.environ.get("POLICY_SYNC_V1_DMAAP_PASS", None),
164 help="Password to use with the dmaap topic"
168 "--v1-decision-endpoint",
170 default=os.environ.get(
171 "POLICY_SYNC_V1_PDP_DECISION_ENDPOINT",
172 "policy/pdpx/v1/decision"
174 help="Decision endpoint used in the v1 api for notifications",
177 args = parser.parse_args(args)
179 if args.logging_config:
180 logging.config.fileConfig(
182 disable_existing_loggers=False
185 handler = logging.StreamHandler()
186 formatter = logging.Formatter(
187 "[%(asctime)s][%(levelname)-5s]%(message)s"
189 root = logging.getLogger()
190 handler.setFormatter(formatter)
191 root.addHandler(handler)
192 root.setLevel(logging.INFO)
194 bind = args.http_bind if args.http_metrics else None
196 client = clients.get_client(
198 pdp_user=args.pdp_user,
199 pdp_password=args.pdp_pass,
201 v0_decision=args.v0_decision_endpoint,
202 v0_notifications=args.v0_notify_endpoint,
203 v1_decision=args.v1_decision_endpoint,
204 dmaap_url=args.v1_dmaap_topic,
205 dmaap_user=args.v1_dmaap_user,
206 dmaap_password=args.v1_dmaap_pass
210 bind = urlsplit("//" + bind)
214 check_period=args.duration,
215 filters=yaml.safe_load(args.filters),
216 ids=yaml.safe_load(args.ids),
224 Parse the arguments passed in via the command line and start the app
227 config = parsecmd(sys.argv[1:])
230 "There was no POLICY_SYNC_PDP_URL set or --pdp flag set"
233 policysync.coroutines.start_event_loop(config)