aws-hit-breaks 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aws_hit_breaks-1.0.0/LICENSE +21 -0
- aws_hit_breaks-1.0.0/PKG-INFO +86 -0
- aws_hit_breaks-1.0.0/README.md +49 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/__init__.py +14 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/auth/__init__.py +1 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/auth/iam_auth.py +317 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/cli/__init__.py +1 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/cli/interactive.py +247 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/cli/keyboard.py +251 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/cli/main.py +114 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/core/__init__.py +1 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/core/config.py +151 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/core/exceptions.py +44 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/__init__.py +19 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/autoscaling.py +271 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/base.py +139 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/ec2.py +241 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/ecs.py +263 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/models.py +40 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/operations.py +325 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/orchestrator.py +370 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/services/rds.py +329 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/state/__init__.py +5 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/state/snapshot_manager.py +261 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks/utils/__init__.py +1 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks.egg-info/PKG-INFO +86 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks.egg-info/SOURCES.txt +37 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks.egg-info/dependency_links.txt +1 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks.egg-info/entry_points.txt +2 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks.egg-info/requires.txt +15 -0
- aws_hit_breaks-1.0.0/aws_hit_breaks.egg-info/top_level.txt +1 -0
- aws_hit_breaks-1.0.0/pyproject.toml +77 -0
- aws_hit_breaks-1.0.0/setup.cfg +4 -0
- aws_hit_breaks-1.0.0/tests/test_cli_main.py +573 -0
- aws_hit_breaks-1.0.0/tests/test_config.py +199 -0
- aws_hit_breaks-1.0.0/tests/test_iam_auth.py +362 -0
- aws_hit_breaks-1.0.0/tests/test_interactive_cli.py +385 -0
- aws_hit_breaks-1.0.0/tests/test_pause_resume_operations.py +538 -0
- aws_hit_breaks-1.0.0/tests/test_service_discovery.py +326 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Karthick Arun
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aws-hit-breaks
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Emergency cost control for AWS accounts - pause all services without deletion
|
|
5
|
+
Author: AWS Break CLI Team
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/aicoder2009/awsbreak
|
|
8
|
+
Project-URL: Repository, https://github.com/aicoder2009/awsbreak
|
|
9
|
+
Project-URL: Issues, https://github.com/aicoder2009/awsbreak/issues
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Requires-Python: >=3.8
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: boto3>=1.34.0
|
|
23
|
+
Requires-Dist: click>=8.1.0
|
|
24
|
+
Requires-Dist: rich>=13.0.0
|
|
25
|
+
Requires-Dist: pydantic>=2.0.0
|
|
26
|
+
Requires-Dist: python-dateutil>=2.8.0
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
30
|
+
Requires-Dist: moto>=4.2.0; extra == "dev"
|
|
31
|
+
Requires-Dist: hypothesis>=6.90.0; extra == "dev"
|
|
32
|
+
Requires-Dist: black>=23.0.0; extra == "dev"
|
|
33
|
+
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
34
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pre-commit>=3.0.0; extra == "dev"
|
|
36
|
+
Dynamic: license-file
|
|
37
|
+
|
|
38
|
+
# AWS Breaks 🚨 ⚠️
|
|
39
|
+
|
|
40
|
+
Emergency cost control for AWS accounts. Stop all services to save money without deleting anything.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install aws-hit-breaks
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quick Start
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
# First time setup (creates secure IAM role)
|
|
52
|
+
aws-hit-breaks
|
|
53
|
+
|
|
54
|
+
# Pause all services to save money
|
|
55
|
+
aws-hit-breaks
|
|
56
|
+
|
|
57
|
+
# Resume services
|
|
58
|
+
aws-hit-breaks --resume
|
|
59
|
+
|
|
60
|
+
# See what would happen (safe mode)
|
|
61
|
+
aws-hit-breaks --dry-run
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Features
|
|
65
|
+
|
|
66
|
+
- 🛡️ **Secure**: Uses dedicated IAM role with minimal permissions
|
|
67
|
+
- 🎯 **Simple**: Just run `aws-hit-breaks` - no complex options
|
|
68
|
+
- 💰 **Cost Savings**: Shows estimated monthly savings
|
|
69
|
+
- 🔄 **Reversible**: Resume everything exactly as it was
|
|
70
|
+
- 🔍 **Safe**: Dry-run mode to preview changes
|
|
71
|
+
|
|
72
|
+
## Supported Services
|
|
73
|
+
|
|
74
|
+
- EC2 instances (stop/start)
|
|
75
|
+
- RDS databases (stop/start)
|
|
76
|
+
- ECS services (scale to zero/restore)
|
|
77
|
+
- Auto Scaling Groups (suspend/resume)
|
|
78
|
+
- Lambda provisioned concurrency (remove/restore)
|
|
79
|
+
|
|
80
|
+
## Security
|
|
81
|
+
|
|
82
|
+
AWS Hit Breaks requires you to create a dedicated IAM role with minimal required permissions. The tool provides a CloudFormation template for easy setup.
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
MIT License - see LICENSE file for details.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# AWS Breaks 🚨 ⚠️
|
|
2
|
+
|
|
3
|
+
Emergency cost control for AWS accounts. Stop all services to save money without deleting anything.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install aws-hit-breaks
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# First time setup (creates secure IAM role)
|
|
15
|
+
aws-hit-breaks
|
|
16
|
+
|
|
17
|
+
# Pause all services to save money
|
|
18
|
+
aws-hit-breaks
|
|
19
|
+
|
|
20
|
+
# Resume services
|
|
21
|
+
aws-hit-breaks --resume
|
|
22
|
+
|
|
23
|
+
# See what would happen (safe mode)
|
|
24
|
+
aws-hit-breaks --dry-run
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- 🛡️ **Secure**: Uses dedicated IAM role with minimal permissions
|
|
30
|
+
- 🎯 **Simple**: Just run `aws-hit-breaks` - no complex options
|
|
31
|
+
- 💰 **Cost Savings**: Shows estimated monthly savings
|
|
32
|
+
- 🔄 **Reversible**: Resume everything exactly as it was
|
|
33
|
+
- 🔍 **Safe**: Dry-run mode to preview changes
|
|
34
|
+
|
|
35
|
+
## Supported Services
|
|
36
|
+
|
|
37
|
+
- EC2 instances (stop/start)
|
|
38
|
+
- RDS databases (stop/start)
|
|
39
|
+
- ECS services (scale to zero/restore)
|
|
40
|
+
- Auto Scaling Groups (suspend/resume)
|
|
41
|
+
- Lambda provisioned concurrency (remove/restore)
|
|
42
|
+
|
|
43
|
+
## Security
|
|
44
|
+
|
|
45
|
+
AWS Hit Breaks requires you to create a dedicated IAM role with minimal required permissions. The tool provides a CloudFormation template for easy setup.
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT License - see LICENSE file for details.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AWS Hit Breaks - Emergency cost control for AWS accounts.
|
|
3
|
+
|
|
4
|
+
A CLI tool that acts like a car's brakes for AWS accounts, allowing users to pause
|
|
5
|
+
all running processes and services to stop incurring costs without permanent deletion.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__version__ = "1.0.0"
|
|
9
|
+
__author__ = "AWS Break CLI Team"
|
|
10
|
+
__email__ = "support@aws-hit-breaks.com"
|
|
11
|
+
|
|
12
|
+
from aws_hit_breaks.core.exceptions import AWSBreakError
|
|
13
|
+
|
|
14
|
+
__all__ = ["AWSBreakError"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Authentication and IAM role management package."""
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"""IAM role authentication and STS assume role functionality."""
|
|
2
|
+
|
|
3
|
+
import boto3
|
|
4
|
+
from botocore.exceptions import ClientError, NoCredentialsError, BotoCoreError
|
|
5
|
+
from typing import Dict, Optional, Any
|
|
6
|
+
import logging
|
|
7
|
+
from datetime import datetime, timedelta, timezone
|
|
8
|
+
|
|
9
|
+
from aws_hit_breaks.core.config import Config, ConfigManager
|
|
10
|
+
from aws_hit_breaks.core.exceptions import AuthenticationError, ConfigurationError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger(__name__)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class IAMRoleAuthenticator:
|
|
17
|
+
"""Handles IAM role authentication using STS assume role."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, config_manager: Optional[ConfigManager] = None):
|
|
20
|
+
"""Initialize the IAM role authenticator.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
config_manager: Optional configuration manager instance.
|
|
24
|
+
If None, creates a new one with default settings.
|
|
25
|
+
"""
|
|
26
|
+
self.config_manager = config_manager or ConfigManager()
|
|
27
|
+
self._cached_credentials: Optional[Dict[str, Any]] = None
|
|
28
|
+
self._credentials_expiry: Optional[datetime] = None
|
|
29
|
+
|
|
30
|
+
def get_aws_session(self, region: Optional[str] = None) -> boto3.Session:
|
|
31
|
+
"""Get an authenticated AWS session using the configured IAM role.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
region: Optional AWS region. If None, uses default from config.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
Authenticated boto3 Session object.
|
|
38
|
+
|
|
39
|
+
Raises:
|
|
40
|
+
ConfigurationError: If no IAM role is configured.
|
|
41
|
+
AuthenticationError: If role assumption fails.
|
|
42
|
+
"""
|
|
43
|
+
config = self._load_config()
|
|
44
|
+
|
|
45
|
+
# Use provided region or fall back to config default
|
|
46
|
+
session_region = region or config.default_region
|
|
47
|
+
|
|
48
|
+
# Get credentials (cached or fresh)
|
|
49
|
+
credentials = self._get_credentials(config.iam_role_arn)
|
|
50
|
+
|
|
51
|
+
# Create session with assumed role credentials
|
|
52
|
+
session = boto3.Session(
|
|
53
|
+
aws_access_key_id=credentials['AccessKeyId'],
|
|
54
|
+
aws_secret_access_key=credentials['SecretAccessKey'],
|
|
55
|
+
aws_session_token=credentials['SessionToken'],
|
|
56
|
+
region_name=session_region
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
return session
|
|
60
|
+
|
|
61
|
+
def get_aws_client(self, service_name: str, region: Optional[str] = None) -> Any:
|
|
62
|
+
"""Get an authenticated AWS service client.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
service_name: AWS service name (e.g., 'ec2', 'rds', 'ecs').
|
|
66
|
+
region: Optional AWS region. If None, uses default from config.
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
Authenticated boto3 client for the specified service.
|
|
70
|
+
|
|
71
|
+
Raises:
|
|
72
|
+
ConfigurationError: If no IAM role is configured.
|
|
73
|
+
AuthenticationError: If role assumption fails.
|
|
74
|
+
"""
|
|
75
|
+
session = self.get_aws_session(region)
|
|
76
|
+
return session.client(service_name)
|
|
77
|
+
|
|
78
|
+
def validate_role_access(self, role_arn: str) -> bool:
|
|
79
|
+
"""Validate that the IAM role can be assumed successfully.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
role_arn: IAM role ARN to validate.
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
True if role can be assumed, False otherwise.
|
|
86
|
+
"""
|
|
87
|
+
try:
|
|
88
|
+
# Try to assume the role
|
|
89
|
+
sts_client = boto3.client('sts')
|
|
90
|
+
response = sts_client.assume_role(
|
|
91
|
+
RoleArn=role_arn,
|
|
92
|
+
RoleSessionName='aws-hit-breaks-validation',
|
|
93
|
+
DurationSeconds=900 # 15 minutes minimum
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# If we get here, the role assumption worked
|
|
97
|
+
logger.info(f"Successfully validated IAM role: {role_arn}")
|
|
98
|
+
return True
|
|
99
|
+
|
|
100
|
+
except ClientError as e:
|
|
101
|
+
error_code = e.response.get('Error', {}).get('Code', 'Unknown')
|
|
102
|
+
logger.warning(f"Failed to validate IAM role {role_arn}: {error_code} - {e}")
|
|
103
|
+
return False
|
|
104
|
+
except (NoCredentialsError, BotoCoreError) as e:
|
|
105
|
+
logger.warning(f"AWS credentials or configuration error: {e}")
|
|
106
|
+
return False
|
|
107
|
+
|
|
108
|
+
def get_caller_identity(self) -> Dict[str, Any]:
|
|
109
|
+
"""Get the current caller identity information.
|
|
110
|
+
|
|
111
|
+
Returns:
|
|
112
|
+
Dictionary containing caller identity information.
|
|
113
|
+
|
|
114
|
+
Raises:
|
|
115
|
+
AuthenticationError: If unable to get caller identity.
|
|
116
|
+
"""
|
|
117
|
+
try:
|
|
118
|
+
session = self.get_aws_session()
|
|
119
|
+
sts_client = session.client('sts')
|
|
120
|
+
return sts_client.get_caller_identity()
|
|
121
|
+
except Exception as e:
|
|
122
|
+
raise AuthenticationError(f"Failed to get caller identity: {e}")
|
|
123
|
+
|
|
124
|
+
def _load_config(self) -> Config:
|
|
125
|
+
"""Load configuration and validate IAM role is configured.
|
|
126
|
+
|
|
127
|
+
Returns:
|
|
128
|
+
Configuration object.
|
|
129
|
+
|
|
130
|
+
Raises:
|
|
131
|
+
ConfigurationError: If no configuration exists or IAM role not configured.
|
|
132
|
+
"""
|
|
133
|
+
config = self.config_manager.load_config()
|
|
134
|
+
|
|
135
|
+
if config is None:
|
|
136
|
+
raise ConfigurationError(
|
|
137
|
+
"No configuration found. Please run the setup process to configure your IAM role."
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
if not config.iam_role_arn:
|
|
141
|
+
raise ConfigurationError(
|
|
142
|
+
"No IAM role configured. Please run the setup process to configure your IAM role."
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
return config
|
|
146
|
+
|
|
147
|
+
def _get_credentials(self, role_arn: str) -> Dict[str, Any]:
|
|
148
|
+
"""Get AWS credentials by assuming the specified IAM role.
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
role_arn: IAM role ARN to assume.
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
Dictionary containing AWS credentials.
|
|
155
|
+
|
|
156
|
+
Raises:
|
|
157
|
+
AuthenticationError: If role assumption fails.
|
|
158
|
+
"""
|
|
159
|
+
# Check if we have cached credentials that are still valid
|
|
160
|
+
if self._cached_credentials and self._credentials_expiry:
|
|
161
|
+
# Add 5 minute buffer before expiry (use timezone-aware comparison)
|
|
162
|
+
if datetime.now(timezone.utc) < (self._credentials_expiry - timedelta(minutes=5)):
|
|
163
|
+
logger.debug("Using cached AWS credentials")
|
|
164
|
+
return self._cached_credentials
|
|
165
|
+
|
|
166
|
+
try:
|
|
167
|
+
logger.info(f"Assuming IAM role: {role_arn}")
|
|
168
|
+
|
|
169
|
+
# Create STS client with current credentials
|
|
170
|
+
sts_client = boto3.client('sts')
|
|
171
|
+
|
|
172
|
+
# Assume the role
|
|
173
|
+
response = sts_client.assume_role(
|
|
174
|
+
RoleArn=role_arn,
|
|
175
|
+
RoleSessionName='aws-hit-breaks-session',
|
|
176
|
+
DurationSeconds=3600 # 1 hour
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
# Extract credentials
|
|
180
|
+
credentials = response['Credentials']
|
|
181
|
+
|
|
182
|
+
# Cache credentials and expiry time (keep timezone-aware)
|
|
183
|
+
self._cached_credentials = credentials
|
|
184
|
+
# AWS returns timezone-aware datetime, keep it that way for accurate comparison
|
|
185
|
+
self._credentials_expiry = credentials['Expiration'].astimezone(timezone.utc)
|
|
186
|
+
|
|
187
|
+
logger.info("Successfully assumed IAM role")
|
|
188
|
+
return credentials
|
|
189
|
+
|
|
190
|
+
except ClientError as e:
|
|
191
|
+
error_code = e.response.get('Error', {}).get('Code', 'Unknown')
|
|
192
|
+
error_message = e.response.get('Error', {}).get('Message', str(e))
|
|
193
|
+
|
|
194
|
+
if error_code == 'AccessDenied':
|
|
195
|
+
raise AuthenticationError(
|
|
196
|
+
f"Access denied when assuming role {role_arn}. "
|
|
197
|
+
"Please check that:\n"
|
|
198
|
+
"1. The role exists and is correctly configured\n"
|
|
199
|
+
"2. Your current AWS credentials have permission to assume this role\n"
|
|
200
|
+
"3. The role's trust policy allows your account/user to assume it"
|
|
201
|
+
)
|
|
202
|
+
elif error_code == 'InvalidUserID.NotFound':
|
|
203
|
+
raise AuthenticationError(
|
|
204
|
+
f"IAM role not found: {role_arn}. "
|
|
205
|
+
"Please check that the role ARN is correct and the role exists."
|
|
206
|
+
)
|
|
207
|
+
else:
|
|
208
|
+
raise AuthenticationError(
|
|
209
|
+
f"Failed to assume IAM role {role_arn}: {error_code} - {error_message}"
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
except NoCredentialsError:
|
|
213
|
+
raise AuthenticationError(
|
|
214
|
+
"No AWS credentials found. Please configure your AWS credentials using:\n"
|
|
215
|
+
"1. AWS CLI: aws configure\n"
|
|
216
|
+
"2. Environment variables: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY\n"
|
|
217
|
+
"3. IAM instance profile (if running on EC2)\n"
|
|
218
|
+
"4. AWS SSO: aws sso login"
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
except BotoCoreError as e:
|
|
222
|
+
raise AuthenticationError(f"AWS configuration error: {e}")
|
|
223
|
+
|
|
224
|
+
except Exception as e:
|
|
225
|
+
raise AuthenticationError(f"Unexpected error assuming IAM role: {e}")
|
|
226
|
+
|
|
227
|
+
def clear_cached_credentials(self) -> None:
|
|
228
|
+
"""Clear any cached credentials to force fresh authentication."""
|
|
229
|
+
self._cached_credentials = None
|
|
230
|
+
self._credentials_expiry = None
|
|
231
|
+
logger.debug("Cleared cached AWS credentials")
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def create_cloudformation_template() -> str:
|
|
235
|
+
"""Generate CloudFormation template for creating the required IAM role.
|
|
236
|
+
|
|
237
|
+
Returns:
|
|
238
|
+
CloudFormation template as a YAML string.
|
|
239
|
+
"""
|
|
240
|
+
template = """AWSTemplateFormatVersion: '2010-09-09'
|
|
241
|
+
Description: 'IAM role for AWS Hit Breaks CLI with minimal required permissions'
|
|
242
|
+
|
|
243
|
+
Parameters:
|
|
244
|
+
TrustedAccountId:
|
|
245
|
+
Type: String
|
|
246
|
+
Description: 'AWS Account ID that can assume this role (your account ID)'
|
|
247
|
+
Default: !Ref 'AWS::AccountId'
|
|
248
|
+
|
|
249
|
+
Resources:
|
|
250
|
+
AWSHitBreaksRole:
|
|
251
|
+
Type: AWS::IAM::Role
|
|
252
|
+
Properties:
|
|
253
|
+
RoleName: AWSHitBreaksRole
|
|
254
|
+
AssumeRolePolicyDocument:
|
|
255
|
+
Version: '2012-10-17'
|
|
256
|
+
Statement:
|
|
257
|
+
- Effect: Allow
|
|
258
|
+
Principal:
|
|
259
|
+
AWS: !Sub 'arn:aws:iam::${TrustedAccountId}:root'
|
|
260
|
+
Action: sts:AssumeRole
|
|
261
|
+
Condition:
|
|
262
|
+
StringEquals:
|
|
263
|
+
'sts:ExternalId': 'aws-hit-breaks-cli'
|
|
264
|
+
Policies:
|
|
265
|
+
- PolicyName: AWSHitBreaksPolicy
|
|
266
|
+
PolicyDocument:
|
|
267
|
+
Version: '2012-10-17'
|
|
268
|
+
Statement:
|
|
269
|
+
- Effect: Allow
|
|
270
|
+
Action:
|
|
271
|
+
# EC2 permissions
|
|
272
|
+
- ec2:DescribeInstances
|
|
273
|
+
- ec2:DescribeInstanceStatus
|
|
274
|
+
- ec2:StopInstances
|
|
275
|
+
- ec2:StartInstances
|
|
276
|
+
# RDS permissions
|
|
277
|
+
- rds:DescribeDBInstances
|
|
278
|
+
- rds:DescribeDBClusters
|
|
279
|
+
- rds:StopDBInstance
|
|
280
|
+
- rds:StartDBInstance
|
|
281
|
+
- rds:StopDBCluster
|
|
282
|
+
- rds:StartDBCluster
|
|
283
|
+
# ECS permissions
|
|
284
|
+
- ecs:DescribeServices
|
|
285
|
+
- ecs:DescribeClusters
|
|
286
|
+
- ecs:ListServices
|
|
287
|
+
- ecs:UpdateService
|
|
288
|
+
# Auto Scaling permissions
|
|
289
|
+
- autoscaling:DescribeAutoScalingGroups
|
|
290
|
+
- autoscaling:SuspendProcesses
|
|
291
|
+
- autoscaling:ResumeProcesses
|
|
292
|
+
- autoscaling:SetDesiredCapacity
|
|
293
|
+
# Lambda permissions
|
|
294
|
+
- lambda:ListFunctions
|
|
295
|
+
- lambda:GetProvisionedConcurrencyConfig
|
|
296
|
+
- lambda:PutProvisionedConcurrencyConfig
|
|
297
|
+
- lambda:DeleteProvisionedConcurrencyConfig
|
|
298
|
+
# Pricing API permissions
|
|
299
|
+
- pricing:GetProducts
|
|
300
|
+
Resource: '*'
|
|
301
|
+
|
|
302
|
+
Outputs:
|
|
303
|
+
RoleArn:
|
|
304
|
+
Description: 'ARN of the created IAM role'
|
|
305
|
+
Value: !GetAtt AWSHitBreaksRole.Arn
|
|
306
|
+
Export:
|
|
307
|
+
Name: !Sub '${AWS::StackName}-RoleArn'
|
|
308
|
+
|
|
309
|
+
SetupInstructions:
|
|
310
|
+
Description: 'Instructions for configuring AWS Hit Breaks CLI'
|
|
311
|
+
Value: !Sub |
|
|
312
|
+
1. Copy this role ARN: ${AWSHitBreaksRole.Arn}
|
|
313
|
+
2. Run: aws-hit-breaks configure
|
|
314
|
+
3. Paste the role ARN when prompted
|
|
315
|
+
"""
|
|
316
|
+
|
|
317
|
+
return template
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""CLI interface package for AWS Hit Breaks."""
|