-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-secrets.py
More file actions
210 lines (159 loc) · 6.08 KB
/
migrate-secrets.py
File metadata and controls
210 lines (159 loc) · 6.08 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
#!/usr/bin/env python3
import os
import sys
import subprocess
from typing import List, Tuple
class Colors:
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
NC = '\033[0m' # No Color
def log_error(message: str) -> None:
"""Print error message in red."""
print(f"{Colors.RED}{message}{Colors.NC}", file=sys.stderr)
def log_success(message: str) -> None:
"""Print success message in green."""
print(f"{Colors.GREEN}{message}{Colors.NC}")
def log_warning(message: str) -> None:
"""Print warning message in yellow."""
print(f"{Colors.YELLOW}{message}{Colors.NC}")
def log_info(message: str) -> None:
"""Print info message."""
print(message)
def validate_parameters() -> Tuple[str, str, List[str], str]:
"""
Validate required environment variables and return them.
Returns:
Tuple of (github_token, github_repo, secret_names, environment)
Raises:
SystemExit if validation fails
"""
github_token = os.environ.get('GITHUB_TOKEN', '')
github_repo = os.environ.get('GITHUB_REPO', '')
secret_names_str = os.environ.get('SECRET_NAMES', '')
environment = os.environ.get('ENVIRONMENT', '')
if not github_token:
log_error("Error: GITHUB_TOKEN is required")
sys.exit(1)
if not github_repo:
log_error("Error: GITHUB_REPO is required (format: owner/repo)")
sys.exit(1)
if not secret_names_str:
log_error("Error: SECRET_NAMES is required (comma-separated list)")
sys.exit(1)
# Split and clean secret names
secret_names = [name.strip() for name in secret_names_str.split(',') if name.strip()]
return github_token, github_repo, secret_names, environment
def set_github_secret(
secret_name: str,
secret_value: str,
repo: str,
environment: str = None
) -> bool:
"""
Set a secret in GitHub using gh CLI.
Args:
secret_name: Name of the secret
secret_value: Value of the secret
repo: GitHub repository in owner/repo format
environment: Optional GitHub environment name
Returns:
True if successful, False otherwise
"""
try:
cmd = ['gh', 'secret', 'set', secret_name, '--repo', repo]
if environment:
cmd.extend(['--env', environment])
result = subprocess.run(
cmd,
input=secret_value,
text=True,
capture_output=True,
check=False
)
if result.returncode != 0:
log_error(f" Error: {result.stderr.strip()}")
return False
return True
except Exception as e:
log_error(f" Exception: {e}")
return False
def migrate_secrets(
secret_names: List[str],
github_repo: str,
environment: str = None
) -> Tuple[int, int, int]:
"""
Migrate secrets from Bitbucket to GitHub.
Args:
secret_names: List of secret names to migrate
github_repo: GitHub repository in owner/repo format
environment: Optional GitHub environment name
Returns:
Tuple of (success_count, failure_count, missing_count)
"""
success_count = 0
failure_count = 0
missing_count = 0
for secret_name in secret_names:
log_warning(f"\nProcessing secret: {secret_name}")
# Get secret value from environment variable
secret_value = os.environ.get(secret_name, '')
# GitHub doesn't allow secrets starting with "GITHUB_", so prefix with underscore
github_secret_name = secret_name
if secret_name.startswith("GITHUB_"):
github_secret_name = "_" + secret_name
log_warning(f" ⚠ Renaming to '{github_secret_name}' (GitHub reserves GITHUB_ prefix)")
if not secret_value:
log_error(f" ✗ Secret '{secret_name}' not found in Bitbucket pipeline variables")
missing_count += 1
continue
# Set the secret in GitHub
if environment:
log_info(f" → Setting as environment secret in environment: {environment}")
success = set_github_secret(github_secret_name, secret_value, github_repo, environment)
else:
log_info(" → Setting as repository secret")
success = set_github_secret(github_secret_name, secret_value, github_repo)
if success:
log_success(f" ✓ Successfully set secret: {github_secret_name}")
success_count += 1
else:
log_error(f" ✗ Failed to set secret: {github_secret_name}")
failure_count += 1
return success_count, failure_count, missing_count
def print_summary(success_count: int, failure_count: int, missing_count: int) -> None:
"""Print migration summary."""
log_success("\n================================")
log_success("Migration Summary")
log_success("================================")
log_success(f"Successfully migrated: {success_count}")
log_error(f"Failed: {failure_count}")
log_warning(f"Not found in Bitbucket: {missing_count}")
log_success("================================")
def main() -> None:
"""Main entry point for the secret migration script."""
# Enable debug mode if requested
debug = os.environ.get('DEBUG', 'false').lower() == 'true'
if debug:
log_info("Debug mode enabled")
# Validate parameters
# Note: github_token is validated but not used directly - the gh CLI
# automatically uses the GITHUB_TOKEN environment variable for authentication
github_token, github_repo, secret_names, environment = validate_parameters()
# Start migration
log_success(f"Starting secret migration to GitHub repository: {github_repo}")
# Migrate secrets
success_count, failure_count, missing_count = migrate_secrets(
secret_names,
github_repo,
environment
)
# Print summary
print_summary(success_count, failure_count, missing_count)
# Exit with error if any failures or missing secrets
if failure_count > 0 or missing_count > 0:
sys.exit(1)
log_success("\n✓ All secrets migrated successfully!")
if __name__ == '__main__':
main()