leverage 1.12.3__tar.gz → 1.12.3rc2__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.3 → leverage-1.12.3rc2}/PKG-INFO +1 -1
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/__init__.py +1 -1
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/_utils.py +0 -72
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/container.py +130 -117
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/containers/kubectl.py +3 -6
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/terraform.py +6 -30
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/path.py +3 -2
- leverage-1.12.3rc2/leverage/scripts/__init__.py +0 -0
- leverage-1.12.3rc2/leverage/scripts/aws-mfa/aws-mfa-entrypoint.sh +296 -0
- leverage-1.12.3rc2/leverage/scripts/aws-sso/aws-sso-login.sh +70 -0
- leverage-1.12.3rc2/leverage/scripts/aws-sso/aws-sso-logout.sh +57 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/pyproject.toml +3 -2
- {leverage-1.12.3 → leverage-1.12.3rc2}/LICENSE.txt +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/README.md +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/_internals.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/_parsing.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/conf.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/leverage.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/logger.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/__init__.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/auth.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/aws.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/credentials.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/kubectl.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/project.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/run.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/shell.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/tfautomv.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/modules/utils.py +0 -0
- {leverage-1.12.3 → leverage-1.12.3rc2}/leverage/tasks.py +0 -0
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"""
|
|
2
2
|
General use utilities.
|
|
3
3
|
"""
|
|
4
|
-
import io
|
|
5
|
-
import os
|
|
6
|
-
import tarfile
|
|
7
|
-
from pathlib import Path
|
|
8
4
|
from subprocess import run
|
|
9
5
|
from subprocess import PIPE
|
|
10
6
|
|
|
@@ -14,7 +10,6 @@ from docker import DockerClient
|
|
|
14
10
|
from docker.models.containers import Container
|
|
15
11
|
|
|
16
12
|
from leverage import logger
|
|
17
|
-
from leverage.logger import raw_logger
|
|
18
13
|
|
|
19
14
|
|
|
20
15
|
def clean_exception_traceback(exception):
|
|
@@ -65,10 +60,6 @@ def git(command):
|
|
|
65
60
|
run(command, stdout=PIPE, stderr=PIPE, check=True)
|
|
66
61
|
|
|
67
62
|
|
|
68
|
-
def chain_commands(commands: list, chain: str = " && ") -> str:
|
|
69
|
-
return f'bash -c "{chain.join(commands)}"'
|
|
70
|
-
|
|
71
|
-
|
|
72
63
|
class CustomEntryPoint:
|
|
73
64
|
"""
|
|
74
65
|
Set a custom entrypoint on the container while entering the context.
|
|
@@ -109,35 +100,6 @@ class AwsCredsEntryPoint(CustomEntryPoint):
|
|
|
109
100
|
"AWS_CONFIG_FILE": self.container.environment["AWS_CONFIG_FILE"].replace(".aws", "tmp"),
|
|
110
101
|
}
|
|
111
102
|
)
|
|
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)
|
|
141
103
|
|
|
142
104
|
|
|
143
105
|
class ExitError(Exit):
|
|
@@ -169,40 +131,6 @@ class ContainerSession:
|
|
|
169
131
|
self.docker_client.api.remove_container(self.container_data)
|
|
170
132
|
|
|
171
133
|
|
|
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
|
-
|
|
206
134
|
def key_finder(d: dict, target: str, avoid: str = None):
|
|
207
135
|
"""
|
|
208
136
|
Iterate over a dict of dicts and/or lists of dicts, looking for a key with value "target".
|
|
@@ -2,6 +2,7 @@ import json
|
|
|
2
2
|
import os
|
|
3
3
|
import re
|
|
4
4
|
import webbrowser
|
|
5
|
+
from io import BytesIO
|
|
5
6
|
from pathlib import Path
|
|
6
7
|
from datetime import datetime
|
|
7
8
|
from time import sleep
|
|
@@ -11,15 +12,14 @@ from click.exceptions import Exit
|
|
|
11
12
|
import dockerpty
|
|
12
13
|
from configupdater import ConfigUpdater
|
|
13
14
|
from docker import DockerClient
|
|
14
|
-
from docker.errors import APIError
|
|
15
|
+
from docker.errors import APIError
|
|
15
16
|
from docker.types import Mount
|
|
16
|
-
from typing import Tuple
|
|
17
|
+
from typing import Tuple
|
|
17
18
|
|
|
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
|
|
22
|
+
from leverage.logger import 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,6 +67,7 @@ class LeverageContainer:
|
|
|
67
67
|
|
|
68
68
|
LEVERAGE_IMAGE = "binbash/leverage-toolbox"
|
|
69
69
|
SHELL = "/bin/bash"
|
|
70
|
+
CONTAINER_USER = "leverage"
|
|
70
71
|
|
|
71
72
|
def __init__(self, client, mounts: tuple = None, env_vars: dict = None):
|
|
72
73
|
"""Project related paths are determined and stored. Project configuration is loaded.
|
|
@@ -78,7 +79,7 @@ class LeverageContainer:
|
|
|
78
79
|
# Load configs
|
|
79
80
|
self.env_conf = load_env()
|
|
80
81
|
|
|
81
|
-
self.paths = PathsHandler(self.env_conf)
|
|
82
|
+
self.paths = PathsHandler(self.env_conf, self.CONTAINER_USER)
|
|
82
83
|
self.project = self.paths.project
|
|
83
84
|
|
|
84
85
|
# Set image to use
|
|
@@ -94,7 +95,7 @@ class LeverageContainer:
|
|
|
94
95
|
mounts = [Mount(source=source, target=target, type="bind") for source, target in mounts] if mounts else []
|
|
95
96
|
self.host_config = self.client.api.create_host_config(security_opt=["label:disable"], mounts=mounts)
|
|
96
97
|
self.container_config = {
|
|
97
|
-
"image": f"{self.image}:{self.
|
|
98
|
+
"image": f"{self.image}:{self.local_image_tag}",
|
|
98
99
|
"command": "",
|
|
99
100
|
"stdin_open": True,
|
|
100
101
|
"environment": env_vars or {},
|
|
@@ -138,43 +139,64 @@ class LeverageContainer:
|
|
|
138
139
|
|
|
139
140
|
raise ExitError(1, f"No valid region could be found at: {self.paths.cwd.as_posix()}")
|
|
140
141
|
|
|
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
|
+
|
|
141
165
|
def ensure_image(self):
|
|
142
|
-
"""
|
|
143
|
-
|
|
166
|
+
"""
|
|
167
|
+
Make sure the required local Docker image is available in the system. If not, build it.
|
|
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}")
|
|
144
175
|
if found_image:
|
|
176
|
+
logger.info("[green]✔ OK[/green]\n")
|
|
145
177
|
return
|
|
146
178
|
|
|
147
|
-
logger.info("
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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)
|
|
166
|
-
|
|
167
|
-
logger.info(next(stream)["status"])
|
|
179
|
+
logger.info(f"Image not found, building it...")
|
|
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
|
+
}
|
|
168
186
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
187
|
+
stream = self.client.api.build(
|
|
188
|
+
fileobj=self.local_image,
|
|
189
|
+
tag=image_name,
|
|
190
|
+
pull=True,
|
|
191
|
+
buildargs=build_args,
|
|
192
|
+
decode=True,
|
|
193
|
+
)
|
|
175
194
|
|
|
176
|
-
for
|
|
177
|
-
|
|
195
|
+
for line in stream:
|
|
196
|
+
if "stream" in line and line["stream"].startswith("Successfully built"):
|
|
197
|
+
logger.info("[green]✔ OK[/green]\n")
|
|
198
|
+
elif "errorDetail" in line:
|
|
199
|
+
raise ExitError(1, f"Failed building local image: {line['errorDetail']}")
|
|
178
200
|
|
|
179
201
|
def _create_container(self, tty, command="", *args):
|
|
180
202
|
"""Create the container that will run the command.
|
|
@@ -285,24 +307,26 @@ class LeverageContainer:
|
|
|
285
307
|
def docker_logs(self, container):
|
|
286
308
|
return self.client.api.logs(container).decode("utf-8")
|
|
287
309
|
|
|
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()
|
|
292
310
|
|
|
293
|
-
|
|
311
|
+
class SSOContainer(LeverageContainer):
|
|
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"
|
|
294
315
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
cmd = self.change_ownership_cmd(path, recursive=recursive)
|
|
301
|
-
with CustomEntryPoint(self, entrypoint=""):
|
|
302
|
-
self._exec(cmd)
|
|
316
|
+
# SSO constants
|
|
317
|
+
AWS_SSO_LOGIN_URL = "https://device.sso.{region}.amazonaws.com/?user_code={user_code}"
|
|
318
|
+
AWS_SSO_CODE_WAIT_SECONDS = 2
|
|
319
|
+
AWS_SSO_CODE_ATTEMPTS = 10
|
|
320
|
+
FALLBACK_LINK_MSG = "Opening the browser... if it fails, open this link in your browser:\n{link}"
|
|
303
321
|
|
|
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
|
+
)
|
|
304
329
|
|
|
305
|
-
class SSOContainer(LeverageContainer):
|
|
306
330
|
def get_sso_access_token(self):
|
|
307
331
|
with open(self.paths.sso_token_file) as token_file:
|
|
308
332
|
return json.loads(token_file.read())["accessToken"]
|
|
@@ -316,22 +340,56 @@ class SSOContainer(LeverageContainer):
|
|
|
316
340
|
conf.read(self.paths.host_aws_profiles_file)
|
|
317
341
|
return conf.get(f"profile {self.project}-sso", "sso_region").value
|
|
318
342
|
|
|
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
|
+
|
|
319
387
|
|
|
320
388
|
class AWSCLIContainer(SSOContainer):
|
|
321
389
|
"""Leverage Container specially tailored to run AWS CLI commands."""
|
|
322
390
|
|
|
323
391
|
AWS_CLI_BINARY = "/usr/local/bin/aws"
|
|
324
392
|
|
|
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
|
-
|
|
335
393
|
def __init__(self, client):
|
|
336
394
|
super().__init__(client)
|
|
337
395
|
|
|
@@ -345,14 +403,16 @@ class AWSCLIContainer(SSOContainer):
|
|
|
345
403
|
"SCRIPT_LOG_LEVEL": get_script_log_level(),
|
|
346
404
|
}
|
|
347
405
|
self.entrypoint = self.AWS_CLI_BINARY
|
|
348
|
-
self.mounts
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
406
|
+
self.mounts.extend(
|
|
407
|
+
[
|
|
408
|
+
Mount(source=self.paths.root_dir.as_posix(), target=self.paths.guest_base_path, type="bind"),
|
|
409
|
+
Mount(
|
|
410
|
+
source=self.paths.host_aws_credentials_dir.as_posix(),
|
|
411
|
+
target=self.paths.guest_aws_credentials_dir,
|
|
412
|
+
type="bind",
|
|
413
|
+
),
|
|
414
|
+
]
|
|
415
|
+
)
|
|
356
416
|
|
|
357
417
|
logger.debug(f"[bold cyan]Container configuration:[/bold cyan]\n{json.dumps(self.container_config, indent=2)}")
|
|
358
418
|
|
|
@@ -381,52 +441,6 @@ class AWSCLIContainer(SSOContainer):
|
|
|
381
441
|
self.entrypoint = self.AWS_CLI_BINARY
|
|
382
442
|
return exit_code, output
|
|
383
443
|
|
|
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
|
-
|
|
430
444
|
|
|
431
445
|
class TerraformContainer(SSOContainer):
|
|
432
446
|
"""Leverage container specifically tailored to run Terraform commands.
|
|
@@ -434,8 +448,7 @@ class TerraformContainer(SSOContainer):
|
|
|
434
448
|
|
|
435
449
|
TF_BINARY = "/bin/terraform"
|
|
436
450
|
|
|
437
|
-
TF_MFA_ENTRYPOINT = "/
|
|
438
|
-
TF_SSO_ENTRYPOINT = "/root/scripts/aws-sso/aws-sso-entrypoint.sh"
|
|
451
|
+
TF_MFA_ENTRYPOINT = "/opt/scripts/aws-mfa/aws-mfa-entrypoint.sh"
|
|
439
452
|
|
|
440
453
|
def __init__(self, client, mounts=None, env_vars=None):
|
|
441
454
|
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
|
|
7
|
+
from leverage._utils import 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(f"/home/{TerraformContainer.CONTAINER_USER}/.kube")
|
|
16
16
|
KUBECTL_CONFIG_FILE = KUBECTL_CONFIG_PATH / Path("config")
|
|
17
17
|
|
|
18
18
|
def __init__(self, client):
|
|
@@ -46,13 +46,10 @@ 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])
|
|
52
49
|
|
|
53
50
|
logger.info("Configuring context...")
|
|
54
51
|
with AwsCredsEntryPoint(self, override_entrypoint=""):
|
|
55
|
-
exit_code = self._start(
|
|
52
|
+
exit_code = self._start(add_eks_cluster_cmd)
|
|
56
53
|
if exit_code:
|
|
57
54
|
raise Exit(exit_code)
|
|
58
55
|
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
import os
|
|
2
1
|
import re
|
|
3
2
|
|
|
4
3
|
import click
|
|
5
|
-
import dockerpty
|
|
6
4
|
import hcl2
|
|
7
5
|
from click.exceptions import Exit
|
|
8
6
|
|
|
9
7
|
from leverage import logger
|
|
10
|
-
from leverage._internals import pass_container
|
|
11
|
-
from leverage.
|
|
12
|
-
from leverage._utils import tar_directory, AwsCredsContainer, LiveContainer, ExitError
|
|
8
|
+
from leverage._internals import pass_container, pass_state
|
|
9
|
+
from leverage._utils import ExitError
|
|
13
10
|
from leverage.container import TerraformContainer
|
|
14
11
|
from leverage.container import get_docker_client
|
|
15
12
|
from leverage.modules.utils import env_var_option, mount_option, auth_mfa, auth_sso
|
|
@@ -84,14 +81,7 @@ def init(context, tf: TerraformContainer, skip_validation, layers, args):
|
|
|
84
81
|
"""
|
|
85
82
|
Initialize this layer.
|
|
86
83
|
"""
|
|
87
|
-
|
|
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)
|
|
84
|
+
invoke_for_all_commands(layers, _init, args, skip_validation)
|
|
95
85
|
|
|
96
86
|
|
|
97
87
|
@terraform.command(context_settings=CONTEXT_SETTINGS)
|
|
@@ -323,23 +313,9 @@ def _init(tf, args):
|
|
|
323
313
|
|
|
324
314
|
tf.paths.check_for_layer_location()
|
|
325
315
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
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
|
-
)
|
|
316
|
+
exit_code = tf.start_in_layer("init", *args)
|
|
317
|
+
if exit_code:
|
|
318
|
+
raise Exit(exit_code)
|
|
343
319
|
|
|
344
320
|
|
|
345
321
|
@pass_container
|
|
@@ -134,7 +134,8 @@ class PathsHandler:
|
|
|
134
134
|
ACCOUNT_TF_VARS = "account.tfvars"
|
|
135
135
|
BACKEND_TF_VARS = "backend.tfvars"
|
|
136
136
|
|
|
137
|
-
def __init__(self, env_conf):
|
|
137
|
+
def __init__(self, env_conf: dict, container_user: str):
|
|
138
|
+
self.container_user = container_user
|
|
138
139
|
self.home = Path.home()
|
|
139
140
|
self.cwd = Path.cwd()
|
|
140
141
|
try:
|
|
@@ -186,7 +187,7 @@ class PathsHandler:
|
|
|
186
187
|
|
|
187
188
|
@property
|
|
188
189
|
def guest_aws_credentials_dir(self):
|
|
189
|
-
return f"/
|
|
190
|
+
return str(f"/home/{self.container_user}/tmp" / Path(self.project))
|
|
190
191
|
|
|
191
192
|
@property
|
|
192
193
|
def host_aws_profiles_file(self):
|
|
File without changes
|
|
@@ -0,0 +1,296 @@
|
|
|
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 "$@"
|
|
@@ -0,0 +1,70 @@
|
|
|
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"
|
|
@@ -0,0 +1,57 @@
|
|
|
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!"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "leverage"
|
|
3
|
-
version = "1.12.
|
|
3
|
+
version = "1.12.3rc2"
|
|
4
4
|
description = "Binbash Leverage Command-Line tool."
|
|
5
5
|
authors = ["BinBash Inc <leverage@binbash.com.ar>"]
|
|
6
6
|
license = "MIT"
|
|
@@ -22,7 +22,8 @@ classifiers = [
|
|
|
22
22
|
packages = [
|
|
23
23
|
{ include = "leverage" },
|
|
24
24
|
{ include = "leverage/modules" },
|
|
25
|
-
{ include = "leverage/containers" }
|
|
25
|
+
{ include = "leverage/containers" },
|
|
26
|
+
{ include = "leverage/scripts" }
|
|
26
27
|
]
|
|
27
28
|
|
|
28
29
|
[tool.poetry.dependencies]
|
|
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
|