-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow_cleanup.py
More file actions
1341 lines (1127 loc) · 59.2 KB
/
flow_cleanup.py
File metadata and controls
1341 lines (1127 loc) · 59.2 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Salesforce Flow Version Cleanup Script
This script helps clean up old Flow versions in Salesforce using the Tooling API.
It includes browser-based OAuth authentication and safety features for production instances.
Requirements:
- Python 3.7+
- requests library
- webbrowser library (built-in)
- json library (built-in)
- urllib.parse library (built-in)
"""
import requests
import webbrowser
import json
import urllib.parse
import sys
import argparse
import base64
import hashlib
import secrets
import threading
import socket
import time
import os
import re
from datetime import datetime
from http.server import HTTPServer, BaseHTTPRequestHandler
from typing import List, Dict, Optional, Tuple
def _prompt(prompt: str, default: Optional[str] = None) -> str:
"""Prompt for input; return value or default. If user types 'exit', exit the tool."""
value = input(prompt).strip()
if value.lower() == 'exit':
print("Exiting.")
sys.exit(0)
if default is not None and value == '':
return default
return value
class CallbackHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path.startswith('/callback'):
# Parse the query parameters
query_params = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query)
if 'code' in query_params:
self.server.auth_code = query_params['code'][0]
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(b'''
<html>
<body>
<h1>Authentication Successful!</h1>
<p>You can close this window and return to the terminal.</p>
</body>
</html>
''')
elif 'error' in query_params:
error = query_params['error'][0]
error_desc = query_params.get('error_description', ['Unknown error'])[0]
self.server.auth_error = f"{error}: {error_desc}"
self.send_response(400)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write(f'''
<html>
<body>
<h1>Authentication Failed</h1>
<p>Error: {error}</p>
<p>Description: {error_desc}</p>
</body>
</html>
'''.encode())
else:
self.send_response(404)
self.end_headers()
def log_message(self, format, *args):
# Suppress default logging
pass
class SalesforceFlowCleanup:
def __init__(self):
self.instance_url = None
self.access_token = None
self.api_version = "v60.0"
self.log_file = None
self.session_id = datetime.now().strftime("%Y%m%d_%H%M%S")
self.client_id = None
self.client_secret = None
# Ensure configs folder exists and move any root config files
self.ensure_configs_folder()
def ensure_configs_folder(self):
"""Ensure configs folder exists and move any config files from root for security"""
configs_dir = "configs"
os.makedirs(configs_dir, exist_ok=True)
# Find config files in root directory
moved_files = []
for filename in os.listdir('.'):
if filename.startswith('config') and filename.endswith('.json') and os.path.isfile(filename):
source_path = filename
dest_path = os.path.join(configs_dir, filename)
# Move the file
try:
# If destination exists, rename with timestamp
if os.path.exists(dest_path):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
name, ext = os.path.splitext(filename)
dest_path = os.path.join(configs_dir, f"{name}_{timestamp}{ext}")
os.rename(source_path, dest_path)
moved_files.append((filename, dest_path))
except Exception as e:
print(f"⚠️ Warning: Could not move {filename}: {e}")
# Display message if files were moved
if moved_files:
print("="*60)
print("🔒 SECURITY: Configuration Files Moved")
print("="*60)
print("The following configuration files were found in the root directory")
print("and have been automatically moved to the 'configs/' folder:")
print()
for filename, dest_path in moved_files:
print(f" • {filename} → {dest_path}")
print()
print("This ensures your configuration files with sensitive credentials")
print("will never be accidentally committed to git.")
print("="*60)
print()
def setup_logging(self):
"""Setup logging to file with masked sensitive information"""
# Ensure logs directory exists
logs_dir = "logs"
os.makedirs(logs_dir, exist_ok=True)
log_filename = os.path.join(logs_dir, f"flow_cleanup_{self.session_id}.log")
self.log_file = log_filename
# Create initial log entry
with open(log_filename, 'w') as f:
f.write(f"=== Salesforce Flow Cleanup Log ===\n")
f.write(f"Session ID: {self.session_id}\n")
f.write(f"Started: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"Instance: {self.instance_url}\n")
f.write("=" * 50 + "\n\n")
print(f"📝 Logging to: {log_filename}")
def log_message(self, message: str, mask_sensitive: bool = True):
"""Log message to file, optionally masking sensitive information"""
if not self.log_file:
return
# Mask sensitive information
if mask_sensitive:
message = self.mask_sensitive_data(message)
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
log_entry = f"[{timestamp}] {message}\n"
with open(self.log_file, 'a') as f:
f.write(log_entry)
def mask_sensitive_data(self, text: str) -> str:
"""Mask sensitive information in log messages"""
# Mask client IDs (Consumer Keys)
text = re.sub(r'client_id["\']?\s*[:=]\s*["\']?([A-Za-z0-9]{15,})',
r'client_id="***MASKED***"', text)
# Mask client secrets
text = re.sub(r'client_secret["\']?\s*[:=]\s*["\']?([A-Za-z0-9]{15,})',
r'client_secret="***MASKED***"', text)
# Mask access tokens
text = re.sub(r'access_token["\']?\s*[:=]\s*["\']?([A-Za-z0-9]{50,})',
r'access_token="***MASKED***"', text)
# Mask authorization codes
text = re.sub(r'code["\']?\s*[:=]\s*["\']?([A-Za-z0-9]{20,})',
r'code="***MASKED***"', text)
return text
def save_deletion_list(self, flows_to_delete: List[Dict]) -> str:
"""Save the list of flows to be deleted to a file"""
if not flows_to_delete:
return None
# Ensure deletion_lists directory exists
deletion_lists_dir = "deletion_lists"
os.makedirs(deletion_lists_dir, exist_ok=True)
filename = os.path.join(deletion_lists_dir, f"flows_to_delete_{self.session_id}.json")
# Prepare data for saving (remove sensitive info)
save_data = {
"session_id": self.session_id,
"timestamp": datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
"instance_url": self.instance_url,
"total_flows": len(flows_to_delete),
"flows": []
}
for flow in flows_to_delete:
flow_data = {
"id": flow['Id'],
"name": flow['Definition']['DeveloperName'],
"label": flow['Definition']['MasterLabel'],
"version": flow['VersionNumber'],
"status": flow['Status'],
"definition_id": flow['DefinitionId']
}
save_data["flows"].append(flow_data)
with open(filename, 'w') as f:
json.dump(save_data, f, indent=2)
return filename
def load_config_file(self, config_file: str) -> Dict:
"""Load configuration from JSON file"""
try:
with open(config_file, 'r') as f:
config = json.load(f)
# Validate required fields
required_fields = ['orgs']
for field in required_fields:
if field not in config:
raise ValueError(f"Missing required field: {field}")
# Validate org configurations
for i, org in enumerate(config['orgs']):
org_required = ['instance', 'client_id']
for field in org_required:
if field not in org:
raise ValueError(f"Org {i+1} missing required field: {field}")
# Set defaults
org.setdefault('client_secret', '')
org.setdefault('cleanup_type', '1')
org.setdefault('flow_names', [])
org.setdefault('skip_production_check', False)
org.setdefault('auto_confirm_production', False)
org.setdefault('callback_port', 8080)
return config
except FileNotFoundError:
print(f"❌ Configuration file not found: {config_file}")
return None
except json.JSONDecodeError as e:
print(f"❌ Invalid JSON in configuration file: {e}")
return None
except ValueError as e:
print(f"❌ Configuration validation error: {e}")
return None
def list_existing_configs(self) -> List[str]:
"""List existing config files in the configs directory"""
configs_dir = "configs"
if not os.path.exists(configs_dir):
return []
config_files = []
for filename in os.listdir(configs_dir):
if filename.endswith('.json') and filename != 'config_example.json':
config_files.append(filename)
return sorted(config_files)
def save_config(self, user_input: Dict, config_filename: str = None, add_to_existing: bool = False) -> bool:
"""Save configuration to a file"""
# Ensure configs directory exists
configs_dir = "configs"
os.makedirs(configs_dir, exist_ok=True)
# Determine filename
if config_filename:
if not config_filename.endswith('.json'):
config_filename += '.json'
config_path = os.path.join(configs_dir, config_filename)
else:
# Generate default name
instance_name = user_input['instance'].replace('https://', '').replace('.my.salesforce.com', '').replace('--', '_')
config_filename = f"config_{instance_name}.json"
config_path = os.path.join(configs_dir, config_filename)
# Prepare org configuration
org_config = {
"instance": user_input['instance'],
"client_id": self.client_id or "",
"client_secret": self.client_secret or "",
"cleanup_type": user_input['cleanup_type'],
"flow_names": user_input.get('flow_names', []),
"skip_production_check": False,
"auto_confirm_production": user_input.get('is_production', False),
"callback_port": user_input.get('port', 8080)
}
# Load existing config or create new
if add_to_existing and os.path.exists(config_path):
try:
with open(config_path, 'r') as f:
config = json.load(f)
# Add new org to existing config
config['orgs'].append(org_config)
except (FileNotFoundError, json.JSONDecodeError):
# If file exists but can't be read, create new
config = {"orgs": [org_config]}
else:
# Create new config
config = {"orgs": [org_config]}
# Save config
try:
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
print(f"✅ Configuration saved to: {config_path}")
return True
except Exception as e:
print(f"❌ Failed to save configuration: {e}")
return False
def offer_save_config(self, user_input: Dict) -> None:
"""Offer to save configuration after interactive mode (including when user chose not to use a config)."""
if not self.client_id:
# No credentials to save
return
print("\n" + "="*60)
print("💾 Save Configuration")
print("="*60)
save_choice = _prompt("Would you like to save this configuration for future use? (y/n): ").lower()
if save_choice not in ['y', 'yes']:
return
# List existing configs
existing_configs = self.list_existing_configs()
if existing_configs:
print("\nExisting configuration files:")
for i, config_file in enumerate(existing_configs, 1):
print(f" {i}. {config_file}")
print(f" {len(existing_configs) + 1}. Create new configuration file")
choice = _prompt(f"\nSelect option (1-{len(existing_configs) + 1}): ")
try:
choice_num = int(choice)
if 1 <= choice_num <= len(existing_configs):
# Add to existing config
config_filename = existing_configs[choice_num - 1]
self.save_config(user_input, config_filename, add_to_existing=True)
elif choice_num == len(existing_configs) + 1:
# Create new config
config_name = _prompt("Enter name for new configuration file (without .json): ")
if config_name:
self.save_config(user_input, config_name, add_to_existing=False)
else:
print("❌ No name provided. Configuration not saved.")
else:
print("❌ Invalid choice. Configuration not saved.")
except ValueError:
print("❌ Invalid input. Configuration not saved.")
else:
# No existing configs, create new
config_name = _prompt("Enter name for configuration file (without .json, or press Enter for default): ", default="")
if config_name:
self.save_config(user_input, config_name, add_to_existing=False)
else:
self.save_config(user_input, add_to_existing=False)
def prompt_cleanup_options(self, defaults: Dict = None) -> Tuple[str, List[str]]:
"""Prompt for cleanup type and flow scope. Used when not --silent.
defaults: optional dict with 'cleanup_type' and 'flow_names' to show as current/defaults."""
defaults = defaults or {}
default_type = defaults.get('cleanup_type', '1')
default_flows = defaults.get('flow_names', [])
print("\n=== Cleanup Options ===")
print("What type of cleanup do you want to do?")
print("1. All old Flow versions (not latest and not active)")
print("2. Specific Flows only (you'll provide the Flow API names)")
print("3. Browse flows with old versions (select from list after connecting)")
type_prompt = "Enter your choice (1, 2, or 3)"
if default_type:
type_prompt += f" [default: {default_type}]"
type_prompt += ": "
choice = _prompt(type_prompt, default_type)
if choice not in ('1', '2', '3'):
choice = '1'
flow_names = []
if choice == "2":
print("\n=== Specific Flow Selection ===")
print("Enter the API names of the Flows you want to clean up.")
if default_flows:
print(f"(Current in config: {', '.join(default_flows)})")
print("(Press Enter on an empty line when done)")
while True:
flow_name = _prompt("Flow API name: ", default="")
if not flow_name:
break
flow_names.append(flow_name)
if not flow_names and default_flows:
flow_names = default_flows
if not flow_names:
print("❌ No Flow names provided. Exiting.")
sys.exit(1)
print(f"✅ Selected {len(flow_names)} Flow(s): {', '.join(flow_names)}")
return choice, flow_names
def get_user_input(self, silent: bool = False, config_path: str = None) -> Dict[str, str]:
"""Get user input for instance and cleanup options.
When silent=True, config_path must be provided; config is used as-is (headless).
When silent=False, user is prompted for all options (cleanup type, all/specific flows, etc.)."""
print("=== Salesforce Flow Version Cleanup Tool ===")
print("Type 'exit' at any prompt to quit.\n")
# Headless: require config, no prompts
if silent:
if not config_path:
print("❌ --silent requires a config file. Use: python flow_cleanup.py --silent --config configs/your_config.json")
sys.exit(1)
path_to_load = config_path
if not os.path.isabs(config_path) and not os.path.isfile(config_path):
path_to_load = os.path.join("configs", config_path)
config = self.load_config_file(path_to_load)
if not config:
sys.exit(1)
return {'config': config, 'mode': 'batch', 'silent': True}
# Not silent: config file is optional; if used, we still prompt for cleanup options
use_config = False
config_file = config_path
if not config_file:
use_config_choice = _prompt("Do you want to use a configuration file? (y/n): ").lower()
use_config = use_config_choice in ['y', 'yes']
if use_config or config_file:
if not config_file:
existing_configs = self.list_existing_configs()
if existing_configs:
print("\nAvailable configuration files:")
for i, cfg in enumerate(existing_configs, 1):
print(f" {i}. {cfg}")
print(f" {len(existing_configs) + 1}. Enter custom path")
choice = _prompt(f"\nSelect option (1-{len(existing_configs) + 1}): ")
try:
choice_num = int(choice)
if 1 <= choice_num <= len(existing_configs):
config_file = os.path.join("configs", existing_configs[choice_num - 1])
elif choice_num == len(existing_configs) + 1:
config_file = _prompt("Enter path to configuration file: ")
else:
config_file = None
except ValueError:
config_file = None
else:
config_file = _prompt("Enter path to configuration file: ")
if config_file:
path_to_load = config_file
if not os.path.isabs(config_file) and not os.path.isfile(config_file):
path_to_load = os.path.join("configs", config_file)
config = self.load_config_file(path_to_load)
if config:
# Prompt for cleanup options (anything in config should be asked when not silent)
first_org = config['orgs'][0]
cleanup_type, flow_names = self.prompt_cleanup_options({
'cleanup_type': first_org.get('cleanup_type', '1'),
'flow_names': first_org.get('flow_names', [])
})
return {
'config': config,
'mode': 'batch',
'cleanup_type': cleanup_type,
'flow_names': flow_names,
'silent': False
}
if not config:
print("Falling back to interactive mode...")
# Interactive mode: no config or load failed — ask for everything
instance = _prompt("Enter your Salesforce instance URL (e.g., mycompany.my.salesforce.com): ")
if not instance.startswith('http'):
instance = f"https://{instance}"
if not instance.endswith('.my.salesforce.com'):
instance = f"{instance}.my.salesforce.com"
print("\n=== OAuth Callback Configuration ===")
print("The script will start a local server to receive OAuth callbacks.")
print("Default port: 8080")
port_input = _prompt("Enter callback port (press Enter for default 8080): ", default="")
if port_input:
try:
port = int(port_input)
if port < 1024 or port > 65535:
print("⚠️ Warning: Port should be between 1024-65535. Using default 8080.")
port = 8080
else:
print(f"✅ Using custom port: {port}")
print("⚠️ IMPORTANT: Update your Salesforce Connected App callback URL to:")
print(f" http://localhost:{port}/callback")
except ValueError:
port = 8080
else:
port = 8080
cleanup_type, flow_names = self.prompt_cleanup_options()
return {
'instance': instance,
'cleanup_type': cleanup_type,
'flow_names': flow_names,
'port': port,
'mode': 'interactive',
'silent': False
}
def get_client_credentials(self) -> tuple:
"""Get client ID and secret from user"""
print("\n=== Connected App Configuration ===")
print("Enter your Salesforce Connected App credentials:")
client_id = _prompt("Client ID (Consumer Key): ")
if not client_id:
print("❌ Client ID is required")
return None, None
client_secret = _prompt("Client Secret (Consumer Secret) [optional]: ", default="")
return client_id, client_secret
def authenticate(self, instance_url: str, client_id: str = None, client_secret: str = None, silent: bool = False, port: int = 8080) -> bool:
"""Authenticate using OAuth 2.0 Web Server Flow with PKCE and local callback server"""
if not silent:
print("\n=== Authentication ===")
print(f"Starting local callback server on port {port}...")
# Get client credentials
if not client_id:
client_id, client_secret = self.get_client_credentials()
if not client_id:
self.log_message("Authentication failed: No client ID provided")
return False
# Store credentials for potential config saving
self.client_id = client_id
self.client_secret = client_secret
else:
if not silent:
print("Using provided client credentials...")
# Store credentials for potential config saving
self.client_id = client_id
self.client_secret = client_secret
self.log_message(f"Authentication started for instance: {instance_url}")
self.log_message(f"Client ID provided: {client_id[:8]}...")
self.log_message(f"Using callback port: {port}")
# Use configurable port for callback server
redirect_uri = f"http://localhost:{port}/callback"
# Generate PKCE parameters
code_verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).decode('utf-8').rstrip('=')
code_challenge = base64.urlsafe_b64encode(hashlib.sha256(code_verifier.encode('utf-8')).digest()).decode('utf-8').rstrip('=')
# Start local server
try:
server = HTTPServer(('localhost', port), CallbackHandler)
server.auth_code = None
server.auth_error = None
# Start server in a separate thread
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
except OSError as e:
if e.errno == 48: # Address already in use
print(f"❌ Port {port} is already in use. Please close any other applications using this port.")
print(f"💡 You can try a different port or check what's using port {port} with:")
print(f" lsof -i :{port} (macOS/Linux) or netstat -ano | findstr :{port} (Windows)")
return False
else:
print(f"❌ Failed to start server on port {port}: {e}")
return False
try:
# Build authorization URL with PKCE
auth_params = {
'response_type': 'code',
'client_id': client_id,
'redirect_uri': redirect_uri,
'scope': 'api refresh_token',
'code_challenge': code_challenge,
'code_challenge_method': 'S256'
}
auth_url = f"{instance_url}/services/oauth2/authorize?" + urllib.parse.urlencode(auth_params)
print(f"Opening browser to: {auth_url}")
print("⏳ Waiting for you to complete authentication in your browser...")
webbrowser.open(auth_url)
# Wait for callback with proper timeout
timeout = 300 # 5 minutes
start_time = time.time()
last_progress_time = 0
while server.auth_code is None and server.auth_error is None:
time.sleep(0.1)
elapsed_time = time.time() - start_time
# Check for timeout
if elapsed_time > timeout:
print(f"⏰ Authentication timed out after {timeout} seconds")
break
# Show progress every 15 seconds
if elapsed_time - last_progress_time >= 15:
remaining = int(timeout - elapsed_time)
print(f"⏳ Still waiting for authentication... ({remaining} seconds remaining)")
last_progress_time = elapsed_time
# Check for errors
if server.auth_error:
print(f"❌ Authentication failed: {server.auth_error}")
self.log_message(f"Authentication failed: {server.auth_error}")
return False
if server.auth_code is None:
print("❌ Authentication timed out or was cancelled")
self.log_message("Authentication timed out or was cancelled")
return False
auth_code = server.auth_code
print("✅ Authorization code received!")
# Exchange code for token with PKCE
token_url = f"{instance_url}/services/oauth2/token"
token_data = {
'grant_type': 'authorization_code',
'client_id': client_id,
'redirect_uri': redirect_uri,
'code': auth_code,
'code_verifier': code_verifier
}
# Add client secret if provided
if client_secret:
token_data['client_secret'] = client_secret
try:
response = requests.post(token_url, data=token_data)
response.raise_for_status()
token_response = response.json()
self.access_token = token_response['access_token']
self.instance_url = instance_url
print("✅ Authentication successful!")
self.log_message("Authentication successful")
return True
except requests.exceptions.RequestException as e:
print(f"❌ Token exchange failed: {e}")
if hasattr(e, 'response') and e.response is not None:
try:
error_detail = e.response.json()
print(f"Error details: {error_detail}")
self.log_message(f"Token exchange failed: {error_detail}")
except:
print(f"Response text: {e.response.text}")
self.log_message(f"Token exchange failed: {e.response.text}")
return False
except KeyError:
print("❌ Invalid response from Salesforce. Check your Connected App configuration.")
self.log_message("Invalid response from Salesforce")
return False
finally:
# Clean up server
print("🔄 Shutting down callback server...")
server.shutdown()
server.server_close()
def check_if_production(self) -> bool:
"""Check if the current instance is production by querying Organization.IsSandbox"""
print("\n=== Checking Instance Type ===")
print("🔍 Determining if this is a production or sandbox instance...")
# SOQL query to check if this is a sandbox
soql_query = "SELECT IsSandbox, Name FROM Organization LIMIT 1"
query_url = f"{self.instance_url}/services/data/{self.api_version}/query"
params = {'q': soql_query}
headers = {
'Authorization': f'Bearer {self.access_token}',
'Content-Type': 'application/json'
}
try:
print("📡 Querying organization information...")
response = requests.get(query_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
org_info = result.get('records', [{}])[0]
is_sandbox = org_info.get('IsSandbox', True) # Default to True for safety
org_name = org_info.get('Name', 'Unknown')
if is_sandbox:
print(f"✅ Sandbox instance detected: {org_name}")
print("🧪 Safe to proceed with cleanup operations")
self.log_message(f"Sandbox instance detected: {org_name}")
return False
else:
print(f"🚨 PRODUCTION instance detected: {org_name}")
print("⚠️ Extra caution required for production operations")
self.log_message(f"PRODUCTION instance detected: {org_name}")
return True
except requests.exceptions.RequestException as e:
print(f"❌ Failed to check instance type: {e}")
print("⚠️ Assuming PRODUCTION for safety")
print("🚨 Extra caution will be required")
self.log_message(f"Failed to check instance type: {e}")
return True
def query_old_flow_versions(self) -> List[Dict]:
"""Query for old Flow versions that can be deleted"""
print("\n=== Querying Old Flow Versions ===")
print("🔍 Searching for old Flow versions that can be safely deleted...")
# SOQL query to find old Flow versions (single line to avoid URL encoding issues)
# Note: We'll query all Flow versions and filter out the latest ones programmatically
soql_query = "SELECT Id, MasterLabel, VersionNumber, Status, DefinitionId, Definition.DeveloperName, Definition.MasterLabel FROM Flow WHERE Status != 'Active' ORDER BY Definition.DeveloperName, VersionNumber DESC"
query_url = f"{self.instance_url}/services/data/{self.api_version}/tooling/query"
params = {'q': soql_query}
headers = {
'Authorization': f'Bearer {self.access_token}',
'Content-Type': 'application/json'
}
try:
print("📡 Sending query to Salesforce...")
self.log_message("Querying old Flow versions")
response = requests.get(query_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
all_flows = result.get('records', [])
print(f"✅ Query completed successfully!")
print(f"📊 Found {len(all_flows)} Flow versions (excluding active ones)")
# Filter out the latest version of each Flow definition
flows_to_delete = []
definition_latest_versions = {}
# First pass: find the latest version number for each definition
for flow in all_flows:
def_id = flow['DefinitionId']
version_num = flow['VersionNumber']
if def_id not in definition_latest_versions or version_num > definition_latest_versions[def_id]:
definition_latest_versions[def_id] = version_num
# Second pass: collect flows that are not the latest version
for flow in all_flows:
def_id = flow['DefinitionId']
version_num = flow['VersionNumber']
if version_num < definition_latest_versions[def_id]:
flows_to_delete.append(flow)
print(f"🔍 After filtering out latest versions: {len(flows_to_delete)} old Flow versions can be deleted")
self.log_message(f"Found {len(flows_to_delete)} old Flow versions to delete")
if flows_to_delete:
for i, flow in enumerate(flows_to_delete, 1):
print(f" {i:3d}. {flow['Definition']['DeveloperName']} v{flow['VersionNumber']} ({flow['Status']}) - {flow['Id']}")
else:
print(" No old Flow versions found (all versions are the latest).")
return flows_to_delete
except requests.exceptions.RequestException as e:
print(f"❌ Query failed: {e}")
if hasattr(e, 'response') and e.response is not None:
try:
error_detail = e.response.json()
print(f" Error details: {error_detail}")
self.log_message(f"Query failed: {error_detail}")
except:
print(f" Response text: {e.response.text}")
self.log_message(f"Query failed: {e.response.text}")
return []
def list_flows_with_old_version_counts(self) -> List[Dict]:
"""Query for distinct flows that have old versions, with count of versions that would be deleted.
Returns list of dicts: [{"developer_name": str, "count": int, "master_label": str}, ...]"""
print("\n=== Listing Flows with Old Versions ===")
print("🔍 Finding flows that have old versions to delete...")
soql_query = "SELECT Id, VersionNumber, DefinitionId, Definition.DeveloperName, Definition.MasterLabel FROM Flow WHERE Status != 'Active' ORDER BY Definition.DeveloperName, VersionNumber DESC"
query_url = f"{self.instance_url}/services/data/{self.api_version}/tooling/query"
params = {'q': soql_query}
headers = {
'Authorization': f'Bearer {self.access_token}',
'Content-Type': 'application/json'
}
try:
self.log_message("Querying flows for browse list")
response = requests.get(query_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
all_flows = result.get('records', [])
# Find latest version per definition
definition_latest = {}
for flow in all_flows:
def_id = flow['DefinitionId']
ver = flow['VersionNumber']
if def_id not in definition_latest or ver > definition_latest[def_id]:
definition_latest[def_id] = ver
# Count old (deletable) versions per definition and collect distinct flow info
definition_counts = {}
definition_labels = {}
for flow in all_flows:
def_id = flow['DefinitionId']
if flow['VersionNumber'] < definition_latest[def_id]:
definition_counts[def_id] = definition_counts.get(def_id, 0) + 1
definition_labels[def_id] = (
flow['Definition']['DeveloperName'],
flow['Definition'].get('MasterLabel') or flow['Definition']['DeveloperName']
)
flow_list = []
for def_id, count in definition_counts.items():
dev_name, master_label = definition_labels[def_id]
flow_list.append({
'developer_name': dev_name,
'count': count,
'master_label': master_label
})
flow_list.sort(key=lambda x: x['developer_name'].lower())
print(f"✅ Found {len(flow_list)} flow(s) with old versions")
self.log_message(f"Browse list: {len(flow_list)} flows with old versions")
return flow_list
except requests.exceptions.RequestException as e:
print(f"❌ Query failed: {e}")
if hasattr(e, 'response') and e.response is not None:
try:
error_detail = e.response.json()
self.log_message(f"Browse list query failed: {error_detail}")
except Exception:
self.log_message(f"Browse list query failed: {e.response.text}")
return []
def prompt_flow_selection_from_list(self, flow_list: List[Dict]) -> List[str]:
"""Display numbered list of flows with version counts; prompt user to select by number.
Accepts input like '1,3,5' or '1 3 5' or 'all'. Returns list of flow developer names."""
if not flow_list:
return []
print("\n=== Select Flows to Clean Up ===")
print("Flows with old versions (number = versions that will be deleted):")
print()
for i, item in enumerate(flow_list, 1):
print(f" {i:3d}. {item['developer_name']} ({item['count']} version{'s' if item['count'] != 1 else ''})")
print()
print("Enter the number(s) to clean up, separated by commas or spaces (e.g. 1,3,5 or 1 3 5), or 'all':")
raw = _prompt("Selection: ", default="").lower()
if not raw:
print("❌ No selection entered. Exiting.")
return []
if raw == 'all':
return [item['developer_name'] for item in flow_list]
# Parse numbers: allow "1,3,5" or "1 3 5" or "1, 3, 5"
parts = re.split(r'[\s,]+', raw)
indices = set()
for p in parts:
p = p.strip()
if not p:
continue
try:
num = int(p)
if 1 <= num <= len(flow_list):
indices.add(num - 1)
else:
print(f"⚠️ Ignoring out-of-range number: {num}")
except ValueError:
print(f"⚠️ Ignoring non-numeric input: {p}")
if not indices:
print("❌ No valid selection. Exiting.")
return []
selected = [flow_list[i]['developer_name'] for i in sorted(indices)]
print(f"✅ Selected {len(selected)} flow(s): {', '.join(selected)}")
return selected
def query_specific_flows(self, flow_names: List[str]) -> List[Dict]:
"""Query for specific Flow versions by name"""
print(f"\n=== Querying Specific Flows: {', '.join(flow_names)} ===")
print(f"🔍 Searching for old versions of: {', '.join(flow_names)}...")
# Build SOQL query for specific flows (single line to avoid URL encoding issues)
# Note: We'll query all versions and filter out the latest ones programmatically
flow_conditions = " OR ".join([f"Definition.DeveloperName = '{name}'" for name in flow_names])
soql_query = f"SELECT Id, MasterLabel, VersionNumber, Status, DefinitionId, Definition.DeveloperName, Definition.MasterLabel FROM Flow WHERE ({flow_conditions}) AND Status != 'Active' ORDER BY Definition.DeveloperName, VersionNumber DESC"
query_url = f"{self.instance_url}/services/data/{self.api_version}/tooling/query"
params = {'q': soql_query}
headers = {
'Authorization': f'Bearer {self.access_token}',
'Content-Type': 'application/json'
}
try:
print("📡 Sending query to Salesforce...")
self.log_message(f"Querying specific flows: {', '.join(flow_names)}")
response = requests.get(query_url, params=params, headers=headers)
response.raise_for_status()
result = response.json()
all_flows = result.get('records', [])
print(f"✅ Query completed successfully!")
print(f"📊 Found {len(all_flows)} Flow versions for specified flows (excluding active ones)")
# Filter out the latest version of each Flow definition
flows_to_delete = []
definition_latest_versions = {}
# First pass: find the latest version number for each definition
for flow in all_flows:
def_id = flow['DefinitionId']
version_num = flow['VersionNumber']
if def_id not in definition_latest_versions or version_num > definition_latest_versions[def_id]:
definition_latest_versions[def_id] = version_num
# Second pass: collect flows that are not the latest version
for flow in all_flows:
def_id = flow['DefinitionId']
version_num = flow['VersionNumber']
if version_num < definition_latest_versions[def_id]:
flows_to_delete.append(flow)
print(f"🔍 After filtering out latest versions: {len(flows_to_delete)} old versions can be deleted")
self.log_message(f"Found {len(flows_to_delete)} old versions for specified flows")
if flows_to_delete:
for i, flow in enumerate(flows_to_delete, 1):
print(f" {i:3d}. {flow['Definition']['DeveloperName']} v{flow['VersionNumber']} ({flow['Status']}) - {flow['Id']}")
else:
print(" No old versions found for the specified flows (all versions are the latest).")
return flows_to_delete
except requests.exceptions.RequestException as e:
print(f"❌ Query failed: {e}")
if hasattr(e, 'response') and e.response is not None: