|
| 1 | +""" |
| 2 | +Copyright (c) 2025 Cisco and/or its affiliates. |
| 3 | +This software is licensed to you under the terms of the Cisco Sample |
| 4 | +Code License, Version 1.1 (the "License"). You may obtain a copy of the |
| 5 | +License at |
| 6 | +
|
| 7 | +https://developer.cisco.com/docs/licenses |
| 8 | +
|
| 9 | +All use of the material herein must be in accordance with the terms of |
| 10 | +the License. All rights not expressly granted by the License are |
| 11 | +reserved. Unless required by applicable law or agreed to separately in |
| 12 | +writing, software distributed under the License is distributed on an "AS |
| 13 | +IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 14 | +or implied. |
| 15 | +""" |
| 16 | + |
| 17 | +# main.py |
| 18 | +import sys |
| 19 | +import argparse |
| 20 | +from sgt_sync.config import Config |
| 21 | +from sgt_sync.logging_config import setup_logging |
| 22 | +from sgt_sync.clients.ise_client import IseClient, IseClientError |
| 23 | +from sgt_sync.clients.secure_access_client import ( |
| 24 | + SecureAccessClient, |
| 25 | + SecureAccessClientError, |
| 26 | +) |
| 27 | +from sgt_sync.synchronizer import SgtSynchronizer |
| 28 | +from sgt_sync.models.sgt import SecurityGroupTag |
| 29 | + |
| 30 | +# Configure logging for the entire application |
| 31 | +logger = setup_logging() |
| 32 | + |
| 33 | + |
| 34 | +def print_sgts(title: str, sgts: list[SecurityGroupTag]): |
| 35 | + """Helper function to print a list of SGTs in a readable format.""" |
| 36 | + if not sgts: |
| 37 | + logger.info(f"No {title} SGTs found.") |
| 38 | + return |
| 39 | + |
| 40 | + logger.info(f"\n--- {title} ({len(sgts)} SGTs) ---") |
| 41 | + for sgt in sgts: |
| 42 | + logger.info( |
| 43 | + f" Key: {sgt.key}, Label: '{sgt.label}', Tag ID: {sgt.tag_id}, Status: {sgt.status}" |
| 44 | + ) |
| 45 | + logger.info(f"--- End of {title} ---") |
| 46 | + |
| 47 | + |
| 48 | +def main(): |
| 49 | + """Main function to run the SGT synchronization or perform CLI actions.""" |
| 50 | + parser = argparse.ArgumentParser( |
| 51 | + description="Synchronize Security Group Tags from ISE to Cisco Secure Access, or perform diagnostic actions." |
| 52 | + ) |
| 53 | + |
| 54 | + # Create a mutually exclusive group for commands that should not run together |
| 55 | + group = parser.add_mutually_exclusive_group() |
| 56 | + group.add_argument( |
| 57 | + "--list-ise", |
| 58 | + action="store_true", |
| 59 | + help="List all Security Group Tags found in Cisco Identity Services Engine (ISE).", |
| 60 | + ) |
| 61 | + group.add_argument( |
| 62 | + "--list-sa", |
| 63 | + action="store_true", |
| 64 | + help="List all Security Group Tags found in Cisco Secure Access (active and inactive).", |
| 65 | + ) |
| 66 | + group.add_argument( |
| 67 | + "--list-sa-inactive", |
| 68 | + action="store_true", |
| 69 | + help="List only the INACTIVE Security Group Tags found in Cisco Secure Access.", |
| 70 | + ) |
| 71 | + group.add_argument( |
| 72 | + "--diff-only", |
| 73 | + action="store_true", |
| 74 | + help="Show the difference between ISE and Secure Access SGTs without performing any synchronization (no changes applied).", |
| 75 | + ) |
| 76 | + args = parser.parse_args() |
| 77 | + |
| 78 | + try: |
| 79 | + # Load and validate configuration |
| 80 | + Config.validate() |
| 81 | + config = Config() |
| 82 | + logger.info("Configuration loaded and validated.") |
| 83 | + |
| 84 | + # Initialize clients |
| 85 | + ise_client = IseClient(config) |
| 86 | + sa_client = SecureAccessClient(config) |
| 87 | + logger.info("API clients initialized.") |
| 88 | + |
| 89 | + # Handle CLI arguments |
| 90 | + if args.list_ise: |
| 91 | + logger.info("Fetching SGTs from ISE...") |
| 92 | + ise_sgts = ise_client.get_sgts() |
| 93 | + print_sgts("ISE SGTs", ise_sgts) |
| 94 | + sys.exit(0) |
| 95 | + |
| 96 | + elif args.list_sa: |
| 97 | + logger.info("Fetching SGTs from Secure Access (all statuses)...") |
| 98 | + sa_sgts = sa_client.get_sgts() |
| 99 | + print_sgts("Secure Access SGTs (All)", sa_sgts) |
| 100 | + sys.exit(0) |
| 101 | + |
| 102 | + elif args.list_sa_inactive: |
| 103 | + logger.info("Fetching INACTIVE SGTs from Secure Access...") |
| 104 | + all_sa_sgts = sa_client.get_sgts() |
| 105 | + inactive_sa_sgts = [sgt for sgt in all_sa_sgts if sgt.status == "inactive"] |
| 106 | + print_sgts("Secure Access SGTs (Inactive)", inactive_sa_sgts) |
| 107 | + sys.exit(0) |
| 108 | + |
| 109 | + elif args.diff_only: |
| 110 | + logger.info( |
| 111 | + "Performing SGT difference analysis (diff-only mode). No changes will be applied." |
| 112 | + ) |
| 113 | + ise_sgts = ise_client.get_sgts() |
| 114 | + sa_sgts = sa_client.get_sgts() |
| 115 | + |
| 116 | + synchronizer = SgtSynchronizer(ise_client, sa_client) |
| 117 | + sgts_to_add_update, sgts_to_mark_inactive = synchronizer.diff_sgts( |
| 118 | + ise_sgts, sa_sgts |
| 119 | + ) |
| 120 | + |
| 121 | + print_sgts( |
| 122 | + "SGTs to Add/Update in Secure Access (would be added/modified)", |
| 123 | + sgts_to_add_update, |
| 124 | + ) |
| 125 | + print_sgts( |
| 126 | + "SGTs to Mark Inactive in Secure Access (would be set to inactive)", |
| 127 | + sgts_to_mark_inactive, |
| 128 | + ) |
| 129 | + sys.exit(0) |
| 130 | + |
| 131 | + # Full synchronization if no specific CLI arguments are provided |
| 132 | + else: |
| 133 | + logger.info( |
| 134 | + "No specific CLI arguments provided. Proceeding with full SGT synchronization." |
| 135 | + ) |
| 136 | + synchronizer = SgtSynchronizer(ise_client, sa_client) |
| 137 | + synchronizer.sync_sgts() |
| 138 | + logger.info("SGT synchronization finished successfully.") |
| 139 | + |
| 140 | + except ValueError as e: |
| 141 | + logger.critical(f"Configuration Error: {e}") |
| 142 | + sys.exit(1) |
| 143 | + except (IseClientError, SecureAccessClientError) as e: |
| 144 | + logger.critical(f"API Client Error: {e}") |
| 145 | + sys.exit(1) |
| 146 | + except Exception as e: |
| 147 | + logger.critical(f"An unexpected error occurred: {e}", exc_info=True) |
| 148 | + sys.exit(1) |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments