Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new region mount option to allow multiple mounts from different regions #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions man/mount.efs.8
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ this option is by default passed and the EFS file system is mounted over TLS\&.
\fBnotls\fR
Mounts the EFS file system without TLS, applies for Mac distributions only\&.
.TP
\fBregion\fR
Mounts the EFS from the specified region, overriding any config file value\&.
.TP
\fBtlsport=\fR\fIn\fR
Configure the TLS relay to listen on the specified port\&. By default, the \
tlsport is choosing randomly from port range defined in the config file located \
Expand Down
12 changes: 8 additions & 4 deletions src/mount_efs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
"noocsp",
"notls",
"ocsp",
"region",
"tls",
"tlsport",
"verify",
Expand Down Expand Up @@ -352,14 +353,17 @@ def fatal_error(user_message, log_message=None, exit_code=1):
sys.exit(exit_code)


def get_target_region(config):
def get_target_region(config, options):
def _fatal_error(message):
fatal_error(
'Error retrieving region. Please set the "region" parameter '
"in the efs-utils configuration file.",
message,
)

if "region" in options:
return options["region"]

try:
return config.get(CONFIG_SECTION, "region")
except NoOptionError:
Expand Down Expand Up @@ -1572,7 +1576,7 @@ def bootstrap_tls(
cert_details = {}
security_credentials = None
client_info = get_client_info(config)
region = get_target_region(config)
region = get_target_region(config, options)

if use_iam:
aws_creds_uri = options.get("awscredsuri")
Expand Down Expand Up @@ -2414,7 +2418,7 @@ def _validate_replacement_field_count(format_str, expected_ct):

if "{region}" in dns_name_format:
expected_replacement_field_ct += 1
format_args["region"] = get_target_region(config)
format_args["region"] = get_target_region(config, options)

if "{dns_name_suffix}" in dns_name_format:
expected_replacement_field_ct += 1
Expand Down Expand Up @@ -3087,7 +3091,7 @@ def get_botocore_client(config, service, options):
botocore_config = botocore.config.Config(use_fips_endpoint=True)

session = botocore.session.get_session()
region = get_target_region(config)
region = get_target_region(config, options)

if options and options.get("awsprofile"):
profile = options.get("awsprofile")
Expand Down
31 changes: 17 additions & 14 deletions test/mount_efs_test/test_get_target_instance_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def get_config(dns_name_format, region=None):
return config


def get_target_region_helper():
def get_target_region_helper(options={}):
config = get_config(DEFAULT_DNS_NAME_FORMAT)
return mount_efs.get_target_region(config)
return mount_efs.get_target_region(config, options)


def get_target_az_helper(options={}):
Expand All @@ -101,13 +101,13 @@ def get_target_az_helper(options={}):
def test_get_target_region_with_token(mocker):
mocker.patch("mount_efs.get_aws_ec2_metadata_token", return_value="ABCDEFG==")
mocker.patch("mount_efs.urlopen", return_value=MockUrlLibResponse())
assert "us-east-1" == get_target_region_helper()
assert "us-east-1" == get_target_region_helper({})


def test_get_target_region_without_token(mocker):
mocker.patch("mount_efs.get_aws_ec2_metadata_token", return_value=None)
mocker.patch("mount_efs.urlopen", return_value=MockUrlLibResponse())
assert "us-east-1" == get_target_region_helper()
assert "us-east-1" == get_target_region_helper({})


# Reproduce https://github.com/aws/efs-utils/issues/46
Expand All @@ -116,7 +116,7 @@ def test_get_target_region_token_endpoint_fetching_timeout(mocker):
mocker.patch(
"mount_efs.urlopen", side_effect=[socket.timeout, MockUrlLibResponse()]
)
assert "us-east-1" == get_target_region_helper()
assert "us-east-1" == get_target_region_helper({})


def test_get_target_region_token_fetch_httperror(mocker):
Expand All @@ -127,15 +127,15 @@ def test_get_target_region_token_fetch_httperror(mocker):
MockUrlLibResponse(),
],
)
assert "us-east-1" == get_target_region_helper()
assert "us-east-1" == get_target_region_helper({})


def test_get_target_region_token_fetch_unknownerror(mocker):
mocker.patch(
"mount_efs.urlopen",
side_effect=[Exception("Unknown Exception"), MockUrlLibResponse()],
)
assert "us-east-1" == get_target_region_helper()
assert "us-east-1" == get_target_region_helper({})


def test_get_target_region_py3_no_charset(mocker):
Expand All @@ -144,7 +144,7 @@ def test_get_target_region_py3_no_charset(mocker):
"mount_efs.urlopen",
return_value=MockUrlLibResponse(data=bytearray(INSTANCE_DOCUMENT, "us-ascii")),
)
assert "us-east-1" == get_target_region_helper()
assert "us-east-1" == get_target_region_helper({})


def test_get_target_region_py3_utf8_charset(mocker):
Expand All @@ -155,22 +155,22 @@ def test_get_target_region_py3_utf8_charset(mocker):
return_value=MockUrlLibResponse(data=bytearray(INSTANCE_DOCUMENT, charset)),
headers=MockHeaders(content_charset=charset),
)
assert "us-east-1" == get_target_region_helper()
assert "us-east-1" == get_target_region_helper({})


def test_get_target_region_from_metadata(mocker):
mocker.patch("mount_efs.get_aws_ec2_metadata_token", return_value=None)
mocker.patch("mount_efs.urlopen", return_value=MockUrlLibResponse())
config = get_config("{fs_id}.efs.{region}.{dns_name_suffix}", None)
assert TARGET_REGION == mount_efs.get_target_region(config)
assert TARGET_REGION == mount_efs.get_target_region(config, {})


def test_get_target_region_config_metadata_unavailable(mocker, capsys):
mocker.patch("mount_efs.get_aws_ec2_metadata_token", return_value=None)
mocker.patch("mount_efs.urlopen", side_effect=URLError("test error"))
config = get_config("{fs_id}.efs.{region}.{dns_name_suffix}")
with pytest.raises(SystemExit) as ex:
mount_efs.get_target_region(config)
mount_efs.get_target_region(config, {})

assert 0 != ex.value.code
out, err = capsys.readouterr()
Expand All @@ -187,7 +187,7 @@ def _test_get_target_region_error(mocker, capsys, response=None, error=None):
mocker.patch("mount_efs.urlopen", side_effect=error)

with pytest.raises(SystemExit) as ex:
get_target_region_helper()
get_target_region_helper({})

assert 0 != ex.value.code

Expand Down Expand Up @@ -228,13 +228,13 @@ def test_get_target_region_missing_region(mocker, capsys):

def test_get_target_region_from_config_variable(mocker):
config = get_config("{az}.{fs_id}.efs.us-east-2.{dns_name_suffix}", TARGET_REGION)
assert TARGET_REGION == mount_efs.get_target_region(config)
assert TARGET_REGION == mount_efs.get_target_region(config, {})


def _test_get_target_region_from_dns_format(mocker, config):
mocker.patch("mount_efs.get_aws_ec2_metadata_token", return_value=None)
mocker.patch("mount_efs.urlopen", side_effect=URLError("test error"))
assert TARGET_REGION == mount_efs.get_target_region(config)
assert TARGET_REGION == mount_efs.get_target_region(config, {})


def test_get_target_region_from_legacy_dns_name_format(mocker):
Expand Down Expand Up @@ -270,6 +270,9 @@ def test_get_target_az_not_present_in_options_and_instance_metadata(mocker):
Get target region from options
"""

def test_get_target_region_from_options(mocker):
assert TARGET_REGION == get_target_region_helper(options={"region": TARGET_REGION})


def test_get_target_az_from_options(mocker):
assert TARGET_AZ == get_target_az_helper(options={"az": TARGET_AZ})