leverage 1.12.3rc2__tar.gz → 1.12.4__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.
- {leverage-1.12.3rc2 → leverage-1.12.4}/PKG-INFO +3 -2
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/__init__.py +1 -1
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/_utils.py +72 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/container.py +117 -130
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/containers/kubectl.py +6 -3
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/terraform.py +30 -6
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/path.py +2 -3
- {leverage-1.12.3rc2 → leverage-1.12.4}/pyproject.toml +3 -4
- leverage-1.12.3rc2/leverage/scripts/__init__.py +0 -0
- leverage-1.12.3rc2/leverage/scripts/aws-mfa/aws-mfa-entrypoint.sh +0 -296
- leverage-1.12.3rc2/leverage/scripts/aws-sso/aws-sso-login.sh +0 -70
- leverage-1.12.3rc2/leverage/scripts/aws-sso/aws-sso-logout.sh +0 -57
- {leverage-1.12.3rc2 → leverage-1.12.4}/LICENSE.txt +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/README.md +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/_internals.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/_parsing.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/conf.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/leverage.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/logger.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/__init__.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/auth.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/aws.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/credentials.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/kubectl.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/project.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/run.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/shell.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/tfautomv.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/modules/utils.py +0 -0
- {leverage-1.12.3rc2 → leverage-1.12.4}/leverage/tasks.py +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: leverage
|
|
3
|
-
Version: 1.12.
|
|
3
|
+
Version: 1.12.4
|
|
4
4
|
Summary: Binbash Leverage Command-Line tool.
|
|
5
5
|
Home-page: https://github.com/binbashar/leverage
|
|
6
6
|
License: MIT
|
|
7
7
|
Keywords: leverage,binbash
|
|
8
8
|
Author: BinBash Inc
|
|
9
9
|
Author-email: leverage@binbash.com.ar
|
|
10
|
-
Requires-Python: >=3.8,<3.
|
|
10
|
+
Requires-Python: >=3.8,<3.13
|
|
11
11
|
Classifier: Development Status :: 5 - Production/Stable
|
|
12
12
|
Classifier: Intended Audience :: Developers
|
|
13
13
|
Classifier: License :: OSI Approved :: MIT License
|
|
@@ -16,6 +16,7 @@ Classifier: Programming Language :: Python :: 3.8
|
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.9
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.10
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
20
|
Classifier: Topic :: Software Development :: Build Tools
|
|
20
21
|
Requires-Dist: boto3 (==1.33.2)
|
|
21
22
|
Requires-Dist: click (==8.0.1)
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"""
|
|
2
2
|
General use utilities.
|
|
3
3
|
"""
|
|
4
|
+
import io
|
|
5
|
+
import os
|
|
6
|
+
import tarfile
|
|
7
|
+
from pathlib import Path
|
|
4
8
|
from subprocess import run
|
|
5
9
|
from subprocess import PIPE
|
|
6
10
|
|
|
@@ -10,6 +14,7 @@ from docker import DockerClient
|
|
|
10
14
|
from docker.models.containers import Container
|
|
11
15
|
|
|
12
16
|
from leverage import logger
|
|
17
|
+
from leverage.logger import raw_logger
|
|
13
18
|
|
|
14
19
|
|
|
15
20
|
def clean_exception_traceback(exception):
|
|
@@ -60,6 +65,10 @@ def git(command):
|
|
|
60
65
|
run(command, stdout=PIPE, stderr=PIPE, check=True)
|
|
61
66
|
|
|
62
67
|
|
|
68
|
+
def chain_commands(commands: list, chain: str = " && ") -> str:
|
|
69
|
+
return f'bash -c "{chain.join(commands)}"'
|
|
70
|
+
|
|
71
|
+
|
|
63
72
|
class CustomEntryPoint:
|
|
64
73
|
"""
|
|
65
74
|
Set a custom entrypoint on the container while entering the context.
|
|
@@ -100,6 +109,35 @@ class AwsCredsEntryPoint(CustomEntryPoint):
|
|
|
100
109
|
"AWS_CONFIG_FILE": self.container.environment["AWS_CONFIG_FILE"].replace(".aws", "tmp"),
|
|
101
110
|
}
|
|
102
111
|
)
|
|
112
|
+
# now return file ownership on the aws credentials files
|
|
113
|
+
self.container.change_file_ownership(self.container.paths.guest_aws_credentials_dir)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class AwsCredsContainer:
|
|
117
|
+
"""
|
|
118
|
+
Fetching AWS credentials by setting the SSO/MFA entrypoints on a living container.
|
|
119
|
+
This flow runs a command on a living container before any other command, leaving your AWS credentials ready
|
|
120
|
+
for authentication.
|
|
121
|
+
|
|
122
|
+
In the case of MFA, the env var tweaks (that happens at .auth_method()) must stay until the end of the block
|
|
123
|
+
given the container is reused for more commands.
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
def __init__(self, container: Container, tf_container):
|
|
127
|
+
self.container = container
|
|
128
|
+
self.tf_container = tf_container
|
|
129
|
+
|
|
130
|
+
def __enter__(self):
|
|
131
|
+
auth_method = self.tf_container.auth_method()
|
|
132
|
+
if not auth_method:
|
|
133
|
+
return
|
|
134
|
+
|
|
135
|
+
exit_code, output = self.container.exec_run(auth_method, environment=self.tf_container.environment)
|
|
136
|
+
raw_logger.info(output.decode("utf-8"))
|
|
137
|
+
|
|
138
|
+
def __exit__(self, *args, **kwargs):
|
|
139
|
+
# now return file ownership on the aws credentials files
|
|
140
|
+
self.tf_container.change_file_ownership(self.tf_container.paths.guest_aws_credentials_dir)
|
|
103
141
|
|
|
104
142
|
|
|
105
143
|
class ExitError(Exit):
|
|
@@ -131,6 +169,40 @@ class ContainerSession:
|
|
|
131
169
|
self.docker_client.api.remove_container(self.container_data)
|
|
132
170
|
|
|
133
171
|
|
|
172
|
+
class LiveContainer(ContainerSession):
|
|
173
|
+
"""
|
|
174
|
+
A container that run a command that "do nothing" indefinitely. The idea is to keep the container alive.
|
|
175
|
+
"""
|
|
176
|
+
|
|
177
|
+
COMMAND = "tail -f /dev/null"
|
|
178
|
+
|
|
179
|
+
def __init__(self, leverage_container, tty=True):
|
|
180
|
+
with CustomEntryPoint(leverage_container, self.COMMAND):
|
|
181
|
+
container_data = leverage_container._create_container(tty)
|
|
182
|
+
super().__init__(leverage_container.client, container_data)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def tar_directory(host_dir_path: Path) -> bytes:
|
|
186
|
+
"""
|
|
187
|
+
Compress a local directory on memory as a tar file and return it as bytes.
|
|
188
|
+
"""
|
|
189
|
+
bytes_array = io.BytesIO()
|
|
190
|
+
with tarfile.open(fileobj=bytes_array, mode="w") as tar_handler:
|
|
191
|
+
# walk through the directory tree
|
|
192
|
+
for root, dirs, files in os.walk(host_dir_path):
|
|
193
|
+
for f in files:
|
|
194
|
+
# and add each file to the tar file
|
|
195
|
+
file_path = Path(os.path.join(root, f))
|
|
196
|
+
tar_handler.add(
|
|
197
|
+
os.path.join(root, f),
|
|
198
|
+
arcname=file_path.relative_to(host_dir_path),
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
bytes_array.seek(0)
|
|
202
|
+
# return the whole tar file as a byte array
|
|
203
|
+
return bytes_array.read()
|
|
204
|
+
|
|
205
|
+
|
|
134
206
|
def key_finder(d: dict, target: str, avoid: str = None):
|
|
135
207
|
"""
|
|
136
208
|
Iterate over a dict of dicts and/or lists of dicts, looking for a key with value "target".
|
|
@@ -2,7 +2,6 @@ import json
|
|
|
2
2
|
import os
|
|
3
3
|
import re
|
|
4
4
|
import webbrowser
|
|
5
|
-
from io import BytesIO
|
|
6
5
|
from pathlib import Path
|
|
7
6
|
from datetime import datetime
|
|
8
7
|
from time import sleep
|
|
@@ -12,14 +11,15 @@ from click.exceptions import Exit
|
|
|
12
11
|
import dockerpty
|
|
13
12
|
from configupdater import ConfigUpdater
|
|
14
13
|
from docker import DockerClient
|
|
15
|
-
from docker.errors import APIError
|
|
14
|
+
from docker.errors import APIError, NotFound
|
|
16
15
|
from docker.types import Mount
|
|
17
|
-
from typing import Tuple
|
|
16
|
+
from typing import Tuple, Union
|
|
18
17
|
|
|
18
|
+
from leverage import __toolbox_version__
|
|
19
19
|
from leverage import logger
|
|
20
20
|
from leverage._utils import AwsCredsEntryPoint, CustomEntryPoint, ExitError, ContainerSession
|
|
21
21
|
from leverage.modules.auth import refresh_layer_credentials
|
|
22
|
-
from leverage.logger import raw_logger
|
|
22
|
+
from leverage.logger import console, raw_logger
|
|
23
23
|
from leverage.logger import get_script_log_level
|
|
24
24
|
from leverage.path import PathsHandler
|
|
25
25
|
from leverage.conf import load as load_env
|
|
@@ -67,7 +67,6 @@ class LeverageContainer:
|
|
|
67
67
|
|
|
68
68
|
LEVERAGE_IMAGE = "binbash/leverage-toolbox"
|
|
69
69
|
SHELL = "/bin/bash"
|
|
70
|
-
CONTAINER_USER = "leverage"
|
|
71
70
|
|
|
72
71
|
def __init__(self, client, mounts: tuple = None, env_vars: dict = None):
|
|
73
72
|
"""Project related paths are determined and stored. Project configuration is loaded.
|
|
@@ -79,7 +78,7 @@ class LeverageContainer:
|
|
|
79
78
|
# Load configs
|
|
80
79
|
self.env_conf = load_env()
|
|
81
80
|
|
|
82
|
-
self.paths = PathsHandler(self.env_conf
|
|
81
|
+
self.paths = PathsHandler(self.env_conf)
|
|
83
82
|
self.project = self.paths.project
|
|
84
83
|
|
|
85
84
|
# Set image to use
|
|
@@ -95,7 +94,7 @@ class LeverageContainer:
|
|
|
95
94
|
mounts = [Mount(source=source, target=target, type="bind") for source, target in mounts] if mounts else []
|
|
96
95
|
self.host_config = self.client.api.create_host_config(security_opt=["label:disable"], mounts=mounts)
|
|
97
96
|
self.container_config = {
|
|
98
|
-
"image": f"{self.image}:{self.
|
|
97
|
+
"image": f"{self.image}:{self.image_tag}",
|
|
99
98
|
"command": "",
|
|
100
99
|
"stdin_open": True,
|
|
101
100
|
"environment": env_vars or {},
|
|
@@ -139,64 +138,43 @@ class LeverageContainer:
|
|
|
139
138
|
|
|
140
139
|
raise ExitError(1, f"No valid region could be found at: {self.paths.cwd.as_posix()}")
|
|
141
140
|
|
|
142
|
-
@property
|
|
143
|
-
def local_image_tag(self):
|
|
144
|
-
return f"{self.image_tag}-{os.getgid()}-{os.getuid()}"
|
|
145
|
-
|
|
146
|
-
@property
|
|
147
|
-
def local_image(self) -> BytesIO:
|
|
148
|
-
"""Return the local image that will be built, as a file-like object."""
|
|
149
|
-
return BytesIO(
|
|
150
|
-
"""
|
|
151
|
-
ARG IMAGE_TAG
|
|
152
|
-
FROM binbash/leverage-toolbox:$IMAGE_TAG
|
|
153
|
-
|
|
154
|
-
ARG UNAME
|
|
155
|
-
ARG UID
|
|
156
|
-
ARG GID
|
|
157
|
-
RUN groupadd -g $GID -o $UNAME
|
|
158
|
-
RUN useradd -m -u $UID -g $GID -o -s /bin/bash $UNAME
|
|
159
|
-
USER $UNAME
|
|
160
|
-
""".encode(
|
|
161
|
-
"utf-8"
|
|
162
|
-
)
|
|
163
|
-
)
|
|
164
|
-
|
|
165
141
|
def ensure_image(self):
|
|
166
|
-
"""
|
|
167
|
-
|
|
168
|
-
If the image already exists, re-build it so changes in the arguments can take effect.
|
|
169
|
-
"""
|
|
170
|
-
logger.info(f"Checking for local docker image, tag: {self.local_image_tag}...")
|
|
171
|
-
image_name = f"{self.image}:{self.local_image_tag}"
|
|
172
|
-
|
|
173
|
-
# check first is our image is already available locally
|
|
174
|
-
found_image = self.client.api.images(f"{self.image}:{self.local_image_tag}")
|
|
142
|
+
"""Make sure the required Docker image is available in the system. If not, pull it from registry."""
|
|
143
|
+
found_image = self.client.api.images(f"{self.image}:{self.image_tag}")
|
|
175
144
|
if found_image:
|
|
176
|
-
logger.info("[green]✔ OK[/green]\n")
|
|
177
145
|
return
|
|
178
146
|
|
|
179
|
-
logger.info(
|
|
180
|
-
build_args = {
|
|
181
|
-
"IMAGE_TAG": self.image_tag,
|
|
182
|
-
"UNAME": self.CONTAINER_USER,
|
|
183
|
-
"GID": str(os.getgid()),
|
|
184
|
-
"UID": str(os.getuid()),
|
|
185
|
-
}
|
|
147
|
+
logger.info("Required Docker image not found.")
|
|
186
148
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
149
|
+
try:
|
|
150
|
+
stream = self.client.api.pull(repository=self.image, tag=self.image_tag, stream=True, decode=True)
|
|
151
|
+
except NotFound as e:
|
|
152
|
+
logger.error(
|
|
153
|
+
f"The specified toolbox version, '{self.image_tag}' (toolbox image '{self.image}:{self.image_tag}') can not be found. "
|
|
154
|
+
"If you come from a project created with an older version of Leverage CLI or have modified the 'build.env' file manually, "
|
|
155
|
+
f"please consider either deleting the file, or configuring a valid toolbox version to use. (i.e. 'TERRAFORM_IMAGE_TAG={__toolbox_version__}')"
|
|
156
|
+
)
|
|
157
|
+
raise Exit(1)
|
|
158
|
+
except APIError as pull:
|
|
159
|
+
pull.__traceback__ = None
|
|
160
|
+
pull.__context__.__traceback__ = None
|
|
161
|
+
logger.exception("Error pulling image:", exc_info=pull)
|
|
162
|
+
raise Exit(1)
|
|
163
|
+
except Exception as e:
|
|
164
|
+
logger.error(f"Not handled error while pulling the image: {e}")
|
|
165
|
+
raise Exit(1)
|
|
194
166
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
167
|
+
logger.info(next(stream)["status"])
|
|
168
|
+
|
|
169
|
+
imageinfo = []
|
|
170
|
+
with console.status("Pulling image..."):
|
|
171
|
+
for status in stream:
|
|
172
|
+
status = status["status"]
|
|
173
|
+
if status.startswith("Digest") or status.startswith("Status"):
|
|
174
|
+
imageinfo.append(status)
|
|
175
|
+
|
|
176
|
+
for info in imageinfo:
|
|
177
|
+
logger.info(info)
|
|
200
178
|
|
|
201
179
|
def _create_container(self, tty, command="", *args):
|
|
202
180
|
"""Create the container that will run the command.
|
|
@@ -307,26 +285,24 @@ class LeverageContainer:
|
|
|
307
285
|
def docker_logs(self, container):
|
|
308
286
|
return self.client.api.logs(container).decode("utf-8")
|
|
309
287
|
|
|
288
|
+
def change_ownership_cmd(self, path: Union[Path, str], recursive=True) -> str:
|
|
289
|
+
recursive = "-R " if recursive else ""
|
|
290
|
+
user_id = os.getuid()
|
|
291
|
+
group_id = os.getgid()
|
|
310
292
|
|
|
311
|
-
|
|
312
|
-
# SSO scripts
|
|
313
|
-
AWS_SSO_LOGIN_SCRIPT = "/opt/scripts/aws-sso/aws-sso-login.sh"
|
|
314
|
-
AWS_SSO_LOGOUT_SCRIPT = "/opt/scripts/aws-sso/aws-sso-logout.sh"
|
|
293
|
+
return f"chown {user_id}:{group_id} {recursive}{path}"
|
|
315
294
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
295
|
+
def change_file_ownership(self, path: Union[Path, str], recursive=True):
|
|
296
|
+
"""
|
|
297
|
+
Change the file/folder ownership from the internal docker user (usually root)
|
|
298
|
+
to the user executing the CLI.
|
|
299
|
+
"""
|
|
300
|
+
cmd = self.change_ownership_cmd(path, recursive=recursive)
|
|
301
|
+
with CustomEntryPoint(self, entrypoint=""):
|
|
302
|
+
self._exec(cmd)
|
|
321
303
|
|
|
322
|
-
def __init__(self, client, mounts=None, env_vars=None):
|
|
323
|
-
super().__init__(client, mounts=mounts, env_vars=env_vars)
|
|
324
|
-
self.mounts.extend(
|
|
325
|
-
[
|
|
326
|
-
Mount(source=(Path(__file__).parent / "scripts").as_posix(), target="/opt/scripts", type="bind"),
|
|
327
|
-
]
|
|
328
|
-
)
|
|
329
304
|
|
|
305
|
+
class SSOContainer(LeverageContainer):
|
|
330
306
|
def get_sso_access_token(self):
|
|
331
307
|
with open(self.paths.sso_token_file) as token_file:
|
|
332
308
|
return json.loads(token_file.read())["accessToken"]
|
|
@@ -340,56 +316,22 @@ class SSOContainer(LeverageContainer):
|
|
|
340
316
|
conf.read(self.paths.host_aws_profiles_file)
|
|
341
317
|
return conf.get(f"profile {self.project}-sso", "sso_region").value
|
|
342
318
|
|
|
343
|
-
def get_sso_code(self, container) -> str:
|
|
344
|
-
"""
|
|
345
|
-
Find and return the SSO user code by periodically checking the logs.
|
|
346
|
-
Up until N attempts.
|
|
347
|
-
"""
|
|
348
|
-
logger.info("Fetching SSO code...")
|
|
349
|
-
for _ in range(self.AWS_SSO_CODE_ATTEMPTS):
|
|
350
|
-
# pull logs periodically until we find our SSO code
|
|
351
|
-
logs = self.docker_logs(container)
|
|
352
|
-
if "Then enter the code:" in logs:
|
|
353
|
-
return logs.split("Then enter the code:")[1].split("\n")[2]
|
|
354
|
-
|
|
355
|
-
sleep(self.AWS_SSO_CODE_WAIT_SECONDS)
|
|
356
|
-
|
|
357
|
-
raise ExitError(1, "Get SSO code timed-out")
|
|
358
|
-
|
|
359
|
-
def get_sso_region(self):
|
|
360
|
-
# TODO: what about using the .region property we have now? that takes the value from the path of the layer
|
|
361
|
-
_, region = self.exec(f"configure get sso_region --profile {self.project}-sso")
|
|
362
|
-
return region
|
|
363
|
-
|
|
364
|
-
def sso_login(self) -> int:
|
|
365
|
-
region = self.get_sso_region()
|
|
366
|
-
|
|
367
|
-
with CustomEntryPoint(self, ""):
|
|
368
|
-
container = self._create_container(False, command=self.AWS_SSO_LOGIN_SCRIPT)
|
|
369
|
-
|
|
370
|
-
with ContainerSession(self.client, container):
|
|
371
|
-
# once inside this block, the SSO_LOGIN_SCRIPT is being executed in the "background"
|
|
372
|
-
# now let's grab the user code from the logs
|
|
373
|
-
user_code = self.get_sso_code(container)
|
|
374
|
-
# with the user code, we can now autocomplete the url
|
|
375
|
-
link = self.AWS_SSO_LOGIN_URL.format(region=region.strip(), user_code=user_code)
|
|
376
|
-
webbrowser.open_new_tab(link)
|
|
377
|
-
# The SSO code is only valid once: if the browser was able to open it, the fallback link will be invalid
|
|
378
|
-
logger.info(self.FALLBACK_LINK_MSG.format(link=link))
|
|
379
|
-
# now let's wait until the command locking the container resolve itself:
|
|
380
|
-
# aws sso login will wait for the user code
|
|
381
|
-
# once submitted to the browser, the authentication finish and the lock is released
|
|
382
|
-
exit_code = self.client.api.wait(container)["StatusCode"]
|
|
383
|
-
raw_logger.info(self.docker_logs(container))
|
|
384
|
-
|
|
385
|
-
return exit_code
|
|
386
|
-
|
|
387
319
|
|
|
388
320
|
class AWSCLIContainer(SSOContainer):
|
|
389
321
|
"""Leverage Container specially tailored to run AWS CLI commands."""
|
|
390
322
|
|
|
391
323
|
AWS_CLI_BINARY = "/usr/local/bin/aws"
|
|
392
324
|
|
|
325
|
+
# SSO scripts
|
|
326
|
+
AWS_SSO_LOGIN_SCRIPT = "/root/scripts/aws-sso/aws-sso-login.sh"
|
|
327
|
+
AWS_SSO_LOGOUT_SCRIPT = "/root/scripts/aws-sso/aws-sso-logout.sh"
|
|
328
|
+
|
|
329
|
+
# SSO constants
|
|
330
|
+
AWS_SSO_LOGIN_URL = "https://device.sso.{region}.amazonaws.com/?user_code={user_code}"
|
|
331
|
+
AWS_SSO_CODE_WAIT_SECONDS = 2
|
|
332
|
+
AWS_SSO_CODE_ATTEMPTS = 10
|
|
333
|
+
FALLBACK_LINK_MSG = "Opening the browser... if it fails, open this link in your browser:\n{link}"
|
|
334
|
+
|
|
393
335
|
def __init__(self, client):
|
|
394
336
|
super().__init__(client)
|
|
395
337
|
|
|
@@ -403,16 +345,14 @@ class AWSCLIContainer(SSOContainer):
|
|
|
403
345
|
"SCRIPT_LOG_LEVEL": get_script_log_level(),
|
|
404
346
|
}
|
|
405
347
|
self.entrypoint = self.AWS_CLI_BINARY
|
|
406
|
-
self.mounts
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
]
|
|
415
|
-
)
|
|
348
|
+
self.mounts = [
|
|
349
|
+
Mount(source=self.paths.root_dir.as_posix(), target=self.paths.guest_base_path, type="bind"),
|
|
350
|
+
Mount(
|
|
351
|
+
source=self.paths.host_aws_credentials_dir.as_posix(),
|
|
352
|
+
target=self.paths.guest_aws_credentials_dir,
|
|
353
|
+
type="bind",
|
|
354
|
+
),
|
|
355
|
+
]
|
|
416
356
|
|
|
417
357
|
logger.debug(f"[bold cyan]Container configuration:[/bold cyan]\n{json.dumps(self.container_config, indent=2)}")
|
|
418
358
|
|
|
@@ -441,6 +381,52 @@ class AWSCLIContainer(SSOContainer):
|
|
|
441
381
|
self.entrypoint = self.AWS_CLI_BINARY
|
|
442
382
|
return exit_code, output
|
|
443
383
|
|
|
384
|
+
def get_sso_code(self, container) -> str:
|
|
385
|
+
"""
|
|
386
|
+
Find and return the SSO user code by periodically checking the logs.
|
|
387
|
+
Up until N attempts.
|
|
388
|
+
"""
|
|
389
|
+
logger.info("Fetching SSO code...")
|
|
390
|
+
for _ in range(self.AWS_SSO_CODE_ATTEMPTS):
|
|
391
|
+
# pull logs periodically until we find our SSO code
|
|
392
|
+
logs = self.docker_logs(container)
|
|
393
|
+
if "Then enter the code:" in logs:
|
|
394
|
+
return logs.split("Then enter the code:")[1].split("\n")[2]
|
|
395
|
+
|
|
396
|
+
sleep(self.AWS_SSO_CODE_WAIT_SECONDS)
|
|
397
|
+
|
|
398
|
+
raise ExitError(1, "Get SSO code timed-out")
|
|
399
|
+
|
|
400
|
+
def get_sso_region(self):
|
|
401
|
+
# TODO: what about using the .region property we have now? that takes the value from the path of the layer
|
|
402
|
+
_, region = self.exec(f"configure get sso_region --profile {self.project}-sso")
|
|
403
|
+
return region
|
|
404
|
+
|
|
405
|
+
def sso_login(self) -> int:
|
|
406
|
+
region = self.get_sso_region()
|
|
407
|
+
|
|
408
|
+
with CustomEntryPoint(self, ""):
|
|
409
|
+
container = self._create_container(False, command=self.AWS_SSO_LOGIN_SCRIPT)
|
|
410
|
+
|
|
411
|
+
with ContainerSession(self.client, container):
|
|
412
|
+
# once inside this block, the SSO_LOGIN_SCRIPT is being executed in the "background"
|
|
413
|
+
# now let's grab the user code from the logs
|
|
414
|
+
user_code = self.get_sso_code(container)
|
|
415
|
+
# with the user code, we can now autocomplete the url
|
|
416
|
+
link = self.AWS_SSO_LOGIN_URL.format(region=region.strip(), user_code=user_code)
|
|
417
|
+
webbrowser.open_new_tab(link)
|
|
418
|
+
# The SSO code is only valid once: if the browser was able to open it, the fallback link will be invalid
|
|
419
|
+
logger.info(self.FALLBACK_LINK_MSG.format(link=link))
|
|
420
|
+
# now let's wait until the command locking the container resolve itself:
|
|
421
|
+
# aws sso login will wait for the user code
|
|
422
|
+
# once submitted to the browser, the authentication finish and the lock is released
|
|
423
|
+
exit_code = self.client.api.wait(container)["StatusCode"]
|
|
424
|
+
raw_logger.info(self.docker_logs(container))
|
|
425
|
+
# now return ownership of the token file back to the user
|
|
426
|
+
self.change_file_ownership(self.paths.guest_aws_credentials_dir)
|
|
427
|
+
|
|
428
|
+
return exit_code
|
|
429
|
+
|
|
444
430
|
|
|
445
431
|
class TerraformContainer(SSOContainer):
|
|
446
432
|
"""Leverage container specifically tailored to run Terraform commands.
|
|
@@ -448,7 +434,8 @@ class TerraformContainer(SSOContainer):
|
|
|
448
434
|
|
|
449
435
|
TF_BINARY = "/bin/terraform"
|
|
450
436
|
|
|
451
|
-
TF_MFA_ENTRYPOINT = "/
|
|
437
|
+
TF_MFA_ENTRYPOINT = "/root/scripts/aws-mfa/aws-mfa-entrypoint.sh"
|
|
438
|
+
TF_SSO_ENTRYPOINT = "/root/scripts/aws-sso/aws-sso-entrypoint.sh"
|
|
452
439
|
|
|
453
440
|
def __init__(self, client, mounts=None, env_vars=None):
|
|
454
441
|
super().__init__(client, mounts=mounts, env_vars=env_vars)
|
|
@@ -4,7 +4,7 @@ from click.exceptions import Exit
|
|
|
4
4
|
from docker.types import Mount
|
|
5
5
|
|
|
6
6
|
from leverage import logger
|
|
7
|
-
from leverage._utils import AwsCredsEntryPoint, ExitError
|
|
7
|
+
from leverage._utils import chain_commands, AwsCredsEntryPoint, ExitError
|
|
8
8
|
from leverage.container import TerraformContainer
|
|
9
9
|
|
|
10
10
|
|
|
@@ -12,7 +12,7 @@ class KubeCtlContainer(TerraformContainer):
|
|
|
12
12
|
"""Container specifically tailored to run kubectl commands."""
|
|
13
13
|
|
|
14
14
|
KUBECTL_CLI_BINARY = "/usr/local/bin/kubectl"
|
|
15
|
-
KUBECTL_CONFIG_PATH = Path(
|
|
15
|
+
KUBECTL_CONFIG_PATH = Path("/root/.kube")
|
|
16
16
|
KUBECTL_CONFIG_FILE = KUBECTL_CONFIG_PATH / Path("config")
|
|
17
17
|
|
|
18
18
|
def __init__(self, client):
|
|
@@ -46,10 +46,13 @@ class KubeCtlContainer(TerraformContainer):
|
|
|
46
46
|
# generate the command that will configure the new cluster
|
|
47
47
|
with AwsCredsEntryPoint(self, override_entrypoint=""):
|
|
48
48
|
add_eks_cluster_cmd = self._get_eks_kube_config()
|
|
49
|
+
# and the command that will set the proper ownership on the config file (otherwise the owner will be "root")
|
|
50
|
+
change_owner_cmd = self.change_ownership_cmd(self.KUBECTL_CONFIG_FILE, recursive=False)
|
|
51
|
+
full_cmd = chain_commands([add_eks_cluster_cmd, change_owner_cmd])
|
|
49
52
|
|
|
50
53
|
logger.info("Configuring context...")
|
|
51
54
|
with AwsCredsEntryPoint(self, override_entrypoint=""):
|
|
52
|
-
exit_code = self._start(
|
|
55
|
+
exit_code = self._start(full_cmd)
|
|
53
56
|
if exit_code:
|
|
54
57
|
raise Exit(exit_code)
|
|
55
58
|
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import os
|
|
1
2
|
import re
|
|
2
3
|
|
|
3
4
|
import click
|
|
5
|
+
import dockerpty
|
|
4
6
|
import hcl2
|
|
5
7
|
from click.exceptions import Exit
|
|
6
8
|
|
|
7
9
|
from leverage import logger
|
|
8
|
-
from leverage._internals import pass_container
|
|
9
|
-
from leverage.
|
|
10
|
+
from leverage._internals import pass_container
|
|
11
|
+
from leverage._internals import pass_state
|
|
12
|
+
from leverage._utils import tar_directory, AwsCredsContainer, LiveContainer, ExitError
|
|
10
13
|
from leverage.container import TerraformContainer
|
|
11
14
|
from leverage.container import get_docker_client
|
|
12
15
|
from leverage.modules.utils import env_var_option, mount_option, auth_mfa, auth_sso
|
|
@@ -81,7 +84,14 @@ def init(context, tf: TerraformContainer, skip_validation, layers, args):
|
|
|
81
84
|
"""
|
|
82
85
|
Initialize this layer.
|
|
83
86
|
"""
|
|
84
|
-
invoke_for_all_commands(layers, _init, args, skip_validation)
|
|
87
|
+
layers = invoke_for_all_commands(layers, _init, args, skip_validation)
|
|
88
|
+
|
|
89
|
+
# now change ownership on all the downloaded modules and providers
|
|
90
|
+
for layer in layers:
|
|
91
|
+
tf.change_file_ownership(tf.paths.guest_base_path / layer.relative_to(tf.paths.root_dir) / ".terraform")
|
|
92
|
+
# and then providers in the cache folder
|
|
93
|
+
if tf.paths.tf_cache_dir:
|
|
94
|
+
tf.change_file_ownership(tf.paths.tf_cache_dir)
|
|
85
95
|
|
|
86
96
|
|
|
87
97
|
@terraform.command(context_settings=CONTEXT_SETTINGS)
|
|
@@ -313,9 +323,23 @@ def _init(tf, args):
|
|
|
313
323
|
|
|
314
324
|
tf.paths.check_for_layer_location()
|
|
315
325
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
326
|
+
with LiveContainer(tf) as container:
|
|
327
|
+
# create the .ssh directory
|
|
328
|
+
container.exec_run("mkdir -p /root/.ssh")
|
|
329
|
+
# copy the entire ~/.ssh/ folder
|
|
330
|
+
tar_bytes = tar_directory(tf.paths.home / ".ssh")
|
|
331
|
+
# into /root/.ssh
|
|
332
|
+
container.put_archive("/root/.ssh/", tar_bytes)
|
|
333
|
+
# correct the owner of the files to match with the docker internal user
|
|
334
|
+
container.exec_run("chown root:root -R /root/.ssh/")
|
|
335
|
+
|
|
336
|
+
with AwsCredsContainer(container, tf):
|
|
337
|
+
dockerpty.exec_command(
|
|
338
|
+
client=tf.client.api,
|
|
339
|
+
container=container.id,
|
|
340
|
+
command="terraform init " + " ".join(args),
|
|
341
|
+
interactive=bool(int(os.environ.get("LEVERAGE_INTERACTIVE", 1))),
|
|
342
|
+
)
|
|
319
343
|
|
|
320
344
|
|
|
321
345
|
@pass_container
|
|
@@ -134,8 +134,7 @@ class PathsHandler:
|
|
|
134
134
|
ACCOUNT_TF_VARS = "account.tfvars"
|
|
135
135
|
BACKEND_TF_VARS = "backend.tfvars"
|
|
136
136
|
|
|
137
|
-
def __init__(self, env_conf
|
|
138
|
-
self.container_user = container_user
|
|
137
|
+
def __init__(self, env_conf):
|
|
139
138
|
self.home = Path.home()
|
|
140
139
|
self.cwd = Path.cwd()
|
|
141
140
|
try:
|
|
@@ -187,7 +186,7 @@ class PathsHandler:
|
|
|
187
186
|
|
|
188
187
|
@property
|
|
189
188
|
def guest_aws_credentials_dir(self):
|
|
190
|
-
return
|
|
189
|
+
return f"/root/tmp/{self.project}"
|
|
191
190
|
|
|
192
191
|
@property
|
|
193
192
|
def host_aws_profiles_file(self):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "leverage"
|
|
3
|
-
version = "1.12.
|
|
3
|
+
version = "1.12.4"
|
|
4
4
|
description = "Binbash Leverage Command-Line tool."
|
|
5
5
|
authors = ["BinBash Inc <leverage@binbash.com.ar>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -22,12 +22,11 @@ classifiers = [
|
|
|
22
22
|
packages = [
|
|
23
23
|
{ include = "leverage" },
|
|
24
24
|
{ include = "leverage/modules" },
|
|
25
|
-
{ include = "leverage/containers" }
|
|
26
|
-
{ include = "leverage/scripts" }
|
|
25
|
+
{ include = "leverage/containers" }
|
|
27
26
|
]
|
|
28
27
|
|
|
29
28
|
[tool.poetry.dependencies]
|
|
30
|
-
python = "~3.8 || ~3.9 || ~3.10 || ~3.11"
|
|
29
|
+
python = "~3.8 || ~3.9 || ~3.10 || ~3.11 || ~3.12"
|
|
31
30
|
click = "8.0.1"
|
|
32
31
|
yaenv = "1.4.1"
|
|
33
32
|
"ruamel.yaml" = "0.17.10"
|
|
File without changes
|
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
|
|
3
|
-
# -----------------------------------------------------------------------------
|
|
4
|
-
# - In a nutshell, what the script does is:
|
|
5
|
-
# -----------------------------------------------------------------------------
|
|
6
|
-
# 1. Figure out all the AWS profiles used by Terraform
|
|
7
|
-
# 2. For each profile:
|
|
8
|
-
# 2.1. Get the role, MFA serial number, and source profile
|
|
9
|
-
# 2.2. Figure out the OTP or prompt the user
|
|
10
|
-
# 2.3. Assume the role to create temporary credentials
|
|
11
|
-
# 2.4. Generate the AWS profiles config files
|
|
12
|
-
# 3. Pass the control back to the main process (e.g. Terraform)
|
|
13
|
-
# -----------------------------------------------------------------------------
|
|
14
|
-
|
|
15
|
-
set -o errexit
|
|
16
|
-
set -o pipefail
|
|
17
|
-
set -o nounset
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
# ---------------------------
|
|
21
|
-
# Formatting helpers
|
|
22
|
-
# ---------------------------
|
|
23
|
-
BOLD="\033[1m"
|
|
24
|
-
DATE="\033[0;90m"
|
|
25
|
-
ERROR="\033[41;37m"
|
|
26
|
-
INFO="\033[0;34m"
|
|
27
|
-
DEBUG="\033[0;32m"
|
|
28
|
-
RESET="\033[0m"
|
|
29
|
-
|
|
30
|
-
# ---------------------------
|
|
31
|
-
# Helper Functions
|
|
32
|
-
# ---------------------------
|
|
33
|
-
|
|
34
|
-
# Simple logging functions
|
|
35
|
-
function error { log "${ERROR}ERROR${RESET}\t$1" 0; }
|
|
36
|
-
function info { log "${INFO}INFO${RESET}\t$1" 1; }
|
|
37
|
-
function debug { log "${DEBUG}DEBUG${RESET}\t$1" 2; }
|
|
38
|
-
function log {
|
|
39
|
-
if [[ $MFA_SCRIPT_LOG_LEVEL -gt "$2" ]]; then
|
|
40
|
-
echo -e "${DATE}[$(date +"%H:%M:%S")]${RESET} $1"
|
|
41
|
-
fi
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
# Get the value of an entry in a config file
|
|
45
|
-
function get_config {
|
|
46
|
-
local config_file=$1
|
|
47
|
-
local config_key=$2
|
|
48
|
-
local config_value=$(grep -oEi "^$config_key\s+=.*\"([a-zA-Z0-9\-]+)\"" $config_file \
|
|
49
|
-
| grep -oEi "\".+\"" \
|
|
50
|
-
| sed 's/\"//g')
|
|
51
|
-
echo $config_value
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
# Get the value of an AWS profile attribute
|
|
55
|
-
function get_profile {
|
|
56
|
-
local aws_config="$1"
|
|
57
|
-
local aws_credentials="$2"
|
|
58
|
-
local profile_name="$3"
|
|
59
|
-
local profile_key="$4"
|
|
60
|
-
local profile_value=$(AWS_CONFIG_FILE=$aws_config; \
|
|
61
|
-
AWS_SHARED_CREDENTIALS_FILE=$aws_credentials; \
|
|
62
|
-
aws configure get "profile.$profile_name.$profile_key")
|
|
63
|
-
echo "$profile_value"
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
# -----------------------------------------------------------------------------
|
|
68
|
-
# Initialize variables
|
|
69
|
-
# -----------------------------------------------------------------------------
|
|
70
|
-
MFA_SCRIPT_LOG_LEVEL=$(printenv MFA_SCRIPT_LOG_LEVEL || echo 2)
|
|
71
|
-
BACKEND_CONFIG_FILE=$(printenv BACKEND_CONFIG_FILE)
|
|
72
|
-
COMMON_CONFIG_FILE=$(printenv COMMON_CONFIG_FILE)
|
|
73
|
-
SRC_AWS_CONFIG_FILE=$(printenv SRC_AWS_CONFIG_FILE)
|
|
74
|
-
SRC_AWS_SHARED_CREDENTIALS_FILE=$(printenv SRC_AWS_SHARED_CREDENTIALS_FILE)
|
|
75
|
-
TF_AWS_CONFIG_FILE=$(printenv AWS_CONFIG_FILE)
|
|
76
|
-
TF_AWS_SHARED_CREDENTIALS_FILE=$(printenv AWS_SHARED_CREDENTIALS_FILE)
|
|
77
|
-
AWS_CACHE_DIR=$(printenv AWS_CACHE_DIR || echo /tmp/cache)
|
|
78
|
-
AWS_REGION=$(get_config "$BACKEND_CONFIG_FILE" region)
|
|
79
|
-
AWS_OUTPUT=json
|
|
80
|
-
debug "${BOLD}BACKEND_CONFIG_FILE=${RESET}$BACKEND_CONFIG_FILE"
|
|
81
|
-
debug "${BOLD}SRC_AWS_CONFIG_FILE=${RESET}$SRC_AWS_CONFIG_FILE"
|
|
82
|
-
debug "${BOLD}SRC_AWS_SHARED_CREDENTIALS_FILE=${RESET}$SRC_AWS_SHARED_CREDENTIALS_FILE"
|
|
83
|
-
debug "${BOLD}TF_AWS_CONFIG_FILE=${RESET}$TF_AWS_CONFIG_FILE"
|
|
84
|
-
debug "${BOLD}TF_AWS_SHARED_CREDENTIALS_FILE=${RESET}$TF_AWS_SHARED_CREDENTIALS_FILE"
|
|
85
|
-
debug "${BOLD}AWS_REGION=${RESET}$AWS_REGION"
|
|
86
|
-
debug "${BOLD}AWS_OUTPUT=${RESET}$AWS_OUTPUT"
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
# -----------------------------------------------------------------------------
|
|
90
|
-
# Pre-run Steps
|
|
91
|
-
# -----------------------------------------------------------------------------
|
|
92
|
-
|
|
93
|
-
# Make some pre-validations
|
|
94
|
-
if [[ ! -f "$SRC_AWS_CONFIG_FILE" ]]; then
|
|
95
|
-
error "Unable to find 'AWS Config' file in path: $SRC_AWS_CONFIG_FILE"
|
|
96
|
-
exit 90
|
|
97
|
-
fi
|
|
98
|
-
if [[ ! -f "$SRC_AWS_SHARED_CREDENTIALS_FILE" ]]; then
|
|
99
|
-
error "Unable to find 'AWS Credentials' file in path: $SRC_AWS_SHARED_CREDENTIALS_FILE"
|
|
100
|
-
exit 91
|
|
101
|
-
fi
|
|
102
|
-
|
|
103
|
-
# Ensure cache credentials dir exists
|
|
104
|
-
mkdir -p "$AWS_CACHE_DIR"
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
# -----------------------------------------------------------------------------
|
|
108
|
-
# 1. Figure out all the AWS profiles used by Terraform
|
|
109
|
-
# -----------------------------------------------------------------------------
|
|
110
|
-
|
|
111
|
-
# Parse all available profiles in config.tf
|
|
112
|
-
RAW_PROFILES=()
|
|
113
|
-
if [[ -f "config.tf" ]] && PARSED_PROFILES=$(grep -v "lookup" config.tf | grep -E "^\s+profile"); then
|
|
114
|
-
while IFS= read -r line ; do
|
|
115
|
-
RAW_PROFILES+=("$(echo "$line" | sed 's/ //g' | sed 's/[\"\$\{\}]//g')")
|
|
116
|
-
done <<< "$PARSED_PROFILES"
|
|
117
|
-
fi
|
|
118
|
-
# Some profiles may be found in local.tf also
|
|
119
|
-
if [[ -f "locals.tf" ]] && PARSED_PROFILES=$(grep -E "^\s+profile" locals.tf); then
|
|
120
|
-
while IFS= read -r line ; do
|
|
121
|
-
RAW_PROFILES+=("$(echo "$line" | sed 's/ //g' | sed 's/[\"\$\{\}]//g')")
|
|
122
|
-
done <<< "$PARSED_PROFILES"
|
|
123
|
-
fi
|
|
124
|
-
|
|
125
|
-
set +e
|
|
126
|
-
# Now we need to replace any placeholders in the profiles
|
|
127
|
-
PROFILE_VALUE=$(get_config "$BACKEND_CONFIG_FILE" profile)
|
|
128
|
-
PROJECT_VALUE=$(get_config "$COMMON_CONFIG_FILE" project)
|
|
129
|
-
PROFILES=()
|
|
130
|
-
for i in "${RAW_PROFILES[@]}" ; do
|
|
131
|
-
TMP_PROFILE=$(echo "$i" | sed "s/profile=//" | sed "s/var.profile/${PROFILE_VALUE}/" | sed "s/var.project/${PROJECT_VALUE}/")
|
|
132
|
-
PROFILES+=("$TMP_PROFILE")
|
|
133
|
-
done
|
|
134
|
-
|
|
135
|
-
# And then we have to remove repeated profiles
|
|
136
|
-
UNIQ_PROFILES=($(echo "${PROFILES[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))
|
|
137
|
-
if [[ "${#UNIQ_PROFILES[@]}" -eq 0 ]]; then
|
|
138
|
-
error "Unable to find any profiles in config.tf"
|
|
139
|
-
exit 100
|
|
140
|
-
fi
|
|
141
|
-
info "${BOLD}MFA:${RESET} Found ${#UNIQ_PROFILES[@]} profile/s"
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
# -----------------------------------------------------------------------------
|
|
145
|
-
# 2. For each profile:
|
|
146
|
-
# -----------------------------------------------------------------------------
|
|
147
|
-
for i in "${UNIQ_PROFILES[@]}" ; do
|
|
148
|
-
info "${BOLD}MFA:${RESET} Attempting to get temporary credentials for profile ${BOLD}$i${RESET}"
|
|
149
|
-
|
|
150
|
-
# -----------------------------------------------------------------------------
|
|
151
|
-
# 2.1. Get the role, serial number and source profile from AWS config file
|
|
152
|
-
# -----------------------------------------------------------------------------
|
|
153
|
-
if ! MFA_ROLE_ARN=$(AWS_CONFIG_FILE="$SRC_AWS_CONFIG_FILE" && \
|
|
154
|
-
AWS_SHARED_CREDENTIALS_FILE="$SRC_AWS_SHARED_CREDENTIALS_FILE" && \
|
|
155
|
-
aws configure get role_arn --profile "$i" 2>&1); then
|
|
156
|
-
if [[ "$MFA_ROLE_ARN" == *"$i"* ]]; then
|
|
157
|
-
error "Credentials for profile $i have not been properly configured. Please check your configuration."
|
|
158
|
-
error "Check your AWS config file to look for the following profile entry: $i"
|
|
159
|
-
error "Check the following link for possible solutions: https://leverage.binbash.co/user-guide/troubleshooting/credentials/"
|
|
160
|
-
else
|
|
161
|
-
error "Missing 'role_arn'"
|
|
162
|
-
fi
|
|
163
|
-
exit 150
|
|
164
|
-
fi
|
|
165
|
-
debug "${BOLD}MFA_ROLE_ARN=${RESET}$MFA_ROLE_ARN"
|
|
166
|
-
MFA_SERIAL_NUMBER=$(get_profile "$SRC_AWS_CONFIG_FILE" "$SRC_AWS_SHARED_CREDENTIALS_FILE" "$i" mfa_serial)
|
|
167
|
-
debug "${BOLD}MFA_SERIAL_NUMBER=${RESET}$MFA_SERIAL_NUMBER"
|
|
168
|
-
MFA_PROFILE_NAME=$(get_profile "$SRC_AWS_CONFIG_FILE" "$SRC_AWS_SHARED_CREDENTIALS_FILE" "$i" source_profile)
|
|
169
|
-
debug "${BOLD}MFA_PROFILE_NAME=${RESET}$MFA_PROFILE_NAME"
|
|
170
|
-
# Validate all required fields
|
|
171
|
-
if [[ $MFA_SERIAL_NUMBER == "" ]]; then error "Missing 'mfa_serial'" && exit 151; fi
|
|
172
|
-
if [[ $MFA_PROFILE_NAME == "" ]]; then error "Missing 'source_profile'" && exit 152; fi
|
|
173
|
-
|
|
174
|
-
# -----------------------------------------------------------------------------
|
|
175
|
-
# 2.2. Figure out the OTP or prompt the user
|
|
176
|
-
# -----------------------------------------------------------------------------
|
|
177
|
-
# Loop a predefined number of times in case the OTP becomes invalid between
|
|
178
|
-
# the time it is generated and the time it is provided to the script
|
|
179
|
-
# -----------------------------------------------------------------------------
|
|
180
|
-
MAX_RETRIES=3
|
|
181
|
-
RETRIES_COUNT=0
|
|
182
|
-
OTP_FAILED=true
|
|
183
|
-
MFA_DURATION=3600
|
|
184
|
-
TEMP_FILE="$AWS_CACHE_DIR/$i"
|
|
185
|
-
debug "${BOLD}TEMP_FILE=${RESET}$TEMP_FILE"
|
|
186
|
-
|
|
187
|
-
while [[ $OTP_FAILED == true && $RETRIES_COUNT -lt $MAX_RETRIES ]]; do
|
|
188
|
-
|
|
189
|
-
#
|
|
190
|
-
# Check if cached credentials exist: look for a file that correspond to
|
|
191
|
-
# the current profile
|
|
192
|
-
#
|
|
193
|
-
if [[ -f "$TEMP_FILE" ]] && EXPIRATION_DATE=$(jq -r '.Credentials.Expiration' "$TEMP_FILE"); then
|
|
194
|
-
debug "Found cached credentials in ${BOLD}$TEMP_FILE${RESET}"
|
|
195
|
-
|
|
196
|
-
# Get expiration date/timestamp
|
|
197
|
-
EXPIRATION_DATE=$(echo "$EXPIRATION_DATE" | sed -e 's/T/ /' | sed -E 's/(Z|\+[0-9]{2}:[0-9]{2})$//')
|
|
198
|
-
debug "${BOLD}EXPIRATION_DATE=${RESET}$EXPIRATION_DATE"
|
|
199
|
-
EXPIRATION_TS=$(date -d "$EXPIRATION_DATE" +"%s" || date +"%s")
|
|
200
|
-
debug "${BOLD}EXPIRATION_TS=${RESET}$EXPIRATION_TS"
|
|
201
|
-
|
|
202
|
-
# Compare current timestamp (plus a margin) with the expiration timestamp
|
|
203
|
-
CURRENT_TS=$(date +"%s")
|
|
204
|
-
CURRENT_TS_PLUS_MARGIN=$(( "$CURRENT_TS" + (30 * 60) ))
|
|
205
|
-
debug "${BOLD}CURRENT_TS=${RESET}$CURRENT_TS"
|
|
206
|
-
debug "${BOLD}CURRENT_TS_PLUS_MARGIN=${RESET}$CURRENT_TS_PLUS_MARGIN"
|
|
207
|
-
if [[ CURRENT_TS_PLUS_MARGIN -lt $EXPIRATION_TS ]]; then
|
|
208
|
-
info "${BOLD}MFA:${RESET} Using cached credentials"
|
|
209
|
-
|
|
210
|
-
# Pretend the OTP succeeded and exit the while loop
|
|
211
|
-
OTP_FAILED=false
|
|
212
|
-
break
|
|
213
|
-
fi
|
|
214
|
-
fi
|
|
215
|
-
|
|
216
|
-
# Prompt user for MFA Token
|
|
217
|
-
echo -ne "${BOLD}MFA:${RESET} Please type in your OTP: "
|
|
218
|
-
if ! MFA_TOKEN_CODE=$(read MFA_TOKEN_CODE && echo "$MFA_TOKEN_CODE"); then
|
|
219
|
-
echo
|
|
220
|
-
error "Aborted!"
|
|
221
|
-
exit 156;
|
|
222
|
-
fi
|
|
223
|
-
debug "${BOLD}MFA_TOKEN_CODE=${RESET}$MFA_TOKEN_CODE"
|
|
224
|
-
|
|
225
|
-
# -----------------------------------------------------------------------------
|
|
226
|
-
# 2.3. Assume the role to generate the temporary credentials
|
|
227
|
-
# -----------------------------------------------------------------------------
|
|
228
|
-
MFA_ROLE_SESSION_NAME="$MFA_PROFILE_NAME-temp"
|
|
229
|
-
if ! MFA_ASSUME_ROLE_OUTPUT=$(AWS_CONFIG_FILE="$SRC_AWS_CONFIG_FILE" && \
|
|
230
|
-
AWS_SHARED_CREDENTIALS_FILE="$SRC_AWS_SHARED_CREDENTIALS_FILE" && \
|
|
231
|
-
aws sts assume-role \
|
|
232
|
-
--role-arn "$MFA_ROLE_ARN" \
|
|
233
|
-
--serial-number "$MFA_SERIAL_NUMBER" \
|
|
234
|
-
--role-session-name "$MFA_ROLE_SESSION_NAME" \
|
|
235
|
-
--duration-seconds "$MFA_DURATION" \
|
|
236
|
-
--token-code "$MFA_TOKEN_CODE" \
|
|
237
|
-
--profile "$MFA_PROFILE_NAME" 2>&1); then
|
|
238
|
-
# Check if STS call failed because of invalid token or user interruption
|
|
239
|
-
if [[ $MFA_ASSUME_ROLE_OUTPUT == *"invalid MFA"* ]]; then
|
|
240
|
-
OTP_FAILED=true
|
|
241
|
-
info "Unable to get valid credentials. Let's try again..."
|
|
242
|
-
elif [[ $MFA_ASSUME_ROLE_OUTPUT == *"Invalid length for parameter TokenCode, value:"* ]]; then
|
|
243
|
-
OTP_FAILED=true
|
|
244
|
-
info "Invalid token length, it must be 6 digits long. Let's try again..."
|
|
245
|
-
elif [[ $MFA_ASSUME_ROLE_OUTPUT == *"AccessDenied"* ]]; then
|
|
246
|
-
info "Access Denied error!"
|
|
247
|
-
exit 161
|
|
248
|
-
elif [[ $MFA_ASSUME_ROLE_OUTPUT == *"An error occurred"* ]]; then
|
|
249
|
-
info "An error occurred!"
|
|
250
|
-
exit 162
|
|
251
|
-
fi
|
|
252
|
-
debug "${BOLD}MFA_ASSUME_ROLE_OUTPUT=${RESET}${MFA_ASSUME_ROLE_OUTPUT}"
|
|
253
|
-
else
|
|
254
|
-
OTP_FAILED=false
|
|
255
|
-
echo "$MFA_ASSUME_ROLE_OUTPUT" > "$TEMP_FILE"
|
|
256
|
-
fi
|
|
257
|
-
debug "${BOLD}OTP_FAILED=${RESET}$OTP_FAILED"
|
|
258
|
-
RETRIES_COUNT=$((RETRIES_COUNT+1))
|
|
259
|
-
debug "${BOLD}RETRIES_COUNT=${RESET}$RETRIES_COUNT"
|
|
260
|
-
|
|
261
|
-
done
|
|
262
|
-
|
|
263
|
-
# Check if credentials were actually created
|
|
264
|
-
if [[ $OTP_FAILED == true ]]; then
|
|
265
|
-
error "Unable to get valid credentials after $MAX_RETRIES attempts"
|
|
266
|
-
exit 160
|
|
267
|
-
fi
|
|
268
|
-
|
|
269
|
-
# -----------------------------------------------------------------------------
|
|
270
|
-
# 2.4. Generate the AWS profiles config files
|
|
271
|
-
# -----------------------------------------------------------------------------
|
|
272
|
-
|
|
273
|
-
# Parse id, secret and session from the output above
|
|
274
|
-
AWS_ACCESS_KEY_ID=$(jq -r .Credentials.AccessKeyId "$TEMP_FILE")
|
|
275
|
-
AWS_SECRET_ACCESS_KEY=$(jq -r .Credentials.SecretAccessKey "$TEMP_FILE")
|
|
276
|
-
AWS_SESSION_TOKEN=$(jq -r .Credentials.SessionToken "$TEMP_FILE")
|
|
277
|
-
debug "${BOLD}AWS_ACCESS_KEY_ID=${RESET}${AWS_ACCESS_KEY_ID:0:4}**************"
|
|
278
|
-
debug "${BOLD}AWS_SECRET_ACCESS_KEY=${RESET}${AWS_SECRET_ACCESS_KEY:0:4}**************"
|
|
279
|
-
debug "${BOLD}AWS_SESSION_TOKEN=${RESET}${AWS_SESSION_TOKEN:0:4}**************"
|
|
280
|
-
|
|
281
|
-
# Create a profile block in the AWS credentials file using the credentials above
|
|
282
|
-
(AWS_CONFIG_FILE=$TF_AWS_CONFIG_FILE; \
|
|
283
|
-
AWS_SHARED_CREDENTIALS_FILE=$TF_AWS_SHARED_CREDENTIALS_FILE; \
|
|
284
|
-
aws configure set "profile.$i.aws_access_key_id" "$AWS_ACCESS_KEY_ID"; \
|
|
285
|
-
aws configure set "profile.$i.aws_secret_access_key" "$AWS_SECRET_ACCESS_KEY"; \
|
|
286
|
-
aws configure set "profile.$i.aws_session_token" "$AWS_SESSION_TOKEN"; \
|
|
287
|
-
aws configure set region "$AWS_REGION"; \
|
|
288
|
-
aws configure set output "$AWS_OUTPUT")
|
|
289
|
-
|
|
290
|
-
info "${BOLD}MFA:${RESET} Credentials written succesfully!"
|
|
291
|
-
done
|
|
292
|
-
|
|
293
|
-
# -----------------------------------------------------------------------------
|
|
294
|
-
# 3. Pass the control back to the main process
|
|
295
|
-
# -----------------------------------------------------------------------------
|
|
296
|
-
exec "$@"
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
|
|
3
|
-
set -o errexit
|
|
4
|
-
set -o pipefail
|
|
5
|
-
set -o nounset
|
|
6
|
-
|
|
7
|
-
# -----------------------------------------------------------------------------
|
|
8
|
-
# Formatting helpers
|
|
9
|
-
# -----------------------------------------------------------------------------
|
|
10
|
-
BOLD="\033[1m"
|
|
11
|
-
DATE="\033[0;90m"
|
|
12
|
-
ERROR="\033[41;37m"
|
|
13
|
-
INFO="\033[0;34m"
|
|
14
|
-
DEBUG="\033[0;32m"
|
|
15
|
-
RESET="\033[0m"
|
|
16
|
-
|
|
17
|
-
# -----------------------------------------------------------------------------
|
|
18
|
-
# Helper Functions
|
|
19
|
-
# -----------------------------------------------------------------------------
|
|
20
|
-
# Simple logging functions
|
|
21
|
-
function error { log "${ERROR}ERROR${RESET}\t$1" 0; }
|
|
22
|
-
function info { log "${INFO}INFO${RESET}\t$1" 1; }
|
|
23
|
-
function debug { log "${DEBUG}DEBUG${RESET}\t$1" 2; }
|
|
24
|
-
function log {
|
|
25
|
-
if [[ $SCRIPT_LOG_LEVEL -gt $2 ]]; then
|
|
26
|
-
printf "%b[%(%T)T]%b %b\n" "$DATE" "$(date +%s)" "$RESET" "$1"
|
|
27
|
-
fi
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
# -----------------------------------------------------------------------------
|
|
31
|
-
# Initialize variables
|
|
32
|
-
# -----------------------------------------------------------------------------
|
|
33
|
-
SCRIPT_LOG_LEVEL=${SCRIPT_LOG_LEVEL:-2}
|
|
34
|
-
PROJECT=$(hcledit -f "$COMMON_CONFIG_FILE" attribute get project | sed 's/"//g')
|
|
35
|
-
SSO_PROFILE_NAME=${SSO_PROFILE_NAME:-$PROJECT-sso}
|
|
36
|
-
SSO_CACHE_DIR=${SSO_CACHE_DIR:-/home/leverage/tmp/$PROJECT/sso/cache}
|
|
37
|
-
AWS_SSO_CACHE_DIR=/home/leverage/.aws/sso/cache
|
|
38
|
-
SSO_TOKEN_FILE_NAME='token'
|
|
39
|
-
debug "SCRIPT_LOG_LEVEL=$SCRIPT_LOG_LEVEL"
|
|
40
|
-
debug "COMMON_CONFIG_FILE=$COMMON_CONFIG_FILE"
|
|
41
|
-
debug "ACCOUNT_CONFIG_FILE=$ACCOUNT_CONFIG_FILE"
|
|
42
|
-
debug "BACKEND_CONFIG_FILE=$BACKEND_CONFIG_FILE"
|
|
43
|
-
debug "SSO_PROFILE_NAME=$SSO_PROFILE_NAME"
|
|
44
|
-
debug "SSO_CACHE_DIR=$SSO_CACHE_DIR"
|
|
45
|
-
debug "SSO_TOKEN_FILE_NAME=$SSO_TOKEN_FILE_NAME"
|
|
46
|
-
|
|
47
|
-
# Make sure cache dir exists
|
|
48
|
-
mkdir -p "$SSO_CACHE_DIR"
|
|
49
|
-
|
|
50
|
-
# -----------------------------------------------------------------------------
|
|
51
|
-
# Log in
|
|
52
|
-
# -----------------------------------------------------------------------------
|
|
53
|
-
info "Logging in..."
|
|
54
|
-
aws sso login --profile "$SSO_PROFILE_NAME"
|
|
55
|
-
|
|
56
|
-
# Store token in cache
|
|
57
|
-
debug "Caching token"
|
|
58
|
-
TOKEN_FILE="$SSO_CACHE_DIR/$SSO_TOKEN_FILE_NAME"
|
|
59
|
-
FILES=$(find "$AWS_SSO_CACHE_DIR" -maxdepth 1 -type f -name '*.json' -not -name 'botocore-client*' -exec ls {} \;)
|
|
60
|
-
for file in $FILES;
|
|
61
|
-
do
|
|
62
|
-
if (jq -er '.accessToken' $file >/dev/null);
|
|
63
|
-
then
|
|
64
|
-
cp $file "$TOKEN_FILE"
|
|
65
|
-
break
|
|
66
|
-
fi
|
|
67
|
-
done
|
|
68
|
-
debug "Token Expiration: $BOLD$(jq -r '.expiresAt' "$TOKEN_FILE")$RESET"
|
|
69
|
-
|
|
70
|
-
info "${BOLD}Successfully logged in!$RESET"
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
|
|
3
|
-
set -o errexit
|
|
4
|
-
set -o pipefail
|
|
5
|
-
set -o nounset
|
|
6
|
-
|
|
7
|
-
# -----------------------------------------------------------------------------
|
|
8
|
-
# Formatting helpers
|
|
9
|
-
# -----------------------------------------------------------------------------
|
|
10
|
-
BOLD="\033[1m"
|
|
11
|
-
DATE="\033[0;90m"
|
|
12
|
-
ERROR="\033[41;37m"
|
|
13
|
-
INFO="\033[0;34m"
|
|
14
|
-
DEBUG="\033[0;32m"
|
|
15
|
-
RESET="\033[0m"
|
|
16
|
-
|
|
17
|
-
# -----------------------------------------------------------------------------
|
|
18
|
-
# Helper Functions
|
|
19
|
-
# -----------------------------------------------------------------------------
|
|
20
|
-
# Simple logging functions
|
|
21
|
-
function error { log "${ERROR}ERROR${RESET}\t$1" 0; }
|
|
22
|
-
function info { log "${INFO}INFO${RESET}\t$1" 1; }
|
|
23
|
-
function debug { log "${DEBUG}DEBUG${RESET}\t$1" 2; }
|
|
24
|
-
function log {
|
|
25
|
-
if [[ $SCRIPT_LOG_LEVEL -gt $2 ]]; then
|
|
26
|
-
printf "%b[%(%T)T]%b %b\n" "$DATE" "$(date +%s)" "$RESET" "$1"
|
|
27
|
-
fi
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
# -----------------------------------------------------------------------------
|
|
31
|
-
# Initialize variables
|
|
32
|
-
# -----------------------------------------------------------------------------
|
|
33
|
-
SCRIPT_LOG_LEVEL=${SCRIPT_LOG_LEVEL:-2}
|
|
34
|
-
PROJECT=$(hcledit -f "$COMMON_CONFIG_FILE" attribute get project | sed 's/"//g')
|
|
35
|
-
SSO_CACHE_DIR=${SSO_CACHE_DIR:-/home/leverage/tmp/$PROJECT/sso/cache}
|
|
36
|
-
debug "SCRIPT_LOG_LEVEL=$SCRIPT_LOG_LEVEL"
|
|
37
|
-
debug "AWS_SHARED_CREDENTIALS_FILE=$AWS_SHARED_CREDENTIALS_FILE"
|
|
38
|
-
debug "AWS_CONFIG_FILE=$AWS_CONFIG_FILE"
|
|
39
|
-
debug "SSO_CACHE_DIR=$SSO_CACHE_DIR"
|
|
40
|
-
debug "PROJECT=$PROJECT"
|
|
41
|
-
|
|
42
|
-
# -----------------------------------------------------------------------------
|
|
43
|
-
# Log out
|
|
44
|
-
# -----------------------------------------------------------------------------
|
|
45
|
-
aws sso logout
|
|
46
|
-
|
|
47
|
-
# Clear sso token
|
|
48
|
-
debug "Removing SSO Tokens."
|
|
49
|
-
rm -f $SSO_CACHE_DIR/*
|
|
50
|
-
|
|
51
|
-
# Clear AWS CLI credentials
|
|
52
|
-
debug "Wiping current SSO credentials."
|
|
53
|
-
awk '/^\[/{if($0~/profile '"$PROJECT-sso"'/ || $0 == "[default]"){found=1}else{found=""}} found' "$AWS_CONFIG_FILE" > tempconf && mv tempconf "$AWS_CONFIG_FILE"
|
|
54
|
-
|
|
55
|
-
rm -f "$AWS_SHARED_CREDENTIALS_FILE"
|
|
56
|
-
|
|
57
|
-
debug "All credentials wiped!"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|