-
Notifications
You must be signed in to change notification settings - Fork 15
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
Modifying Firewall rules to provide Internet Access to T0/T1 #2327
Open
cptanalatriste
wants to merge
13
commits into
alan-turing-institute:develop
Choose a base branch
from
cptanalatriste:2283-internet-access-tier0-tier1
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+564
−208
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2daf019
[WIP] Adding an extra flag to config-SRE
cptanalatriste d0bd20c
[WIP] Setting up firewall tests
cptanalatriste 7325672
[WIP] The component test does not work
cptanalatriste a5a1301
Tests seem to be working
cptanalatriste 4ba2d7e
[WIP] My tests break other tests
cptanalatriste 57e5fdc
[WIP] Using network rules for allowing internet access
cptanalatriste 3f469a8
[WIP] Enable Mocks globally, and fixing tests broken by this
cptanalatriste c49a227
Remove unnecessary white space
cptanalatriste 4baa8bd
Merge branch 'develop' into 2283-internet-access-tier0-tier1
cptanalatriste a57a8bb
Fixing bug
cptanalatriste d1e4f23
[WIP] Enabling al sources on the network rule
cptanalatriste 6a0c4d7
Fixing tests, and relaxing DNS rules when internet is allowed
cptanalatriste 5e46b73
Fixing firewall tests
cptanalatriste File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
414 changes: 214 additions & 200 deletions
414
data_safe_haven/infrastructure/programs/sre/firewall.py
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,7 @@ def test_constructor_defaults( | |
) | ||
assert sre_config.admin_email_address == "[email protected]" | ||
assert sre_config.admin_ip_addresses == [] | ||
assert not sre_config.allow_workspace_internet | ||
assert sre_config.databases == [] | ||
assert sre_config.data_provider_ip_addresses == [] | ||
assert sre_config.remote_desktop == config_subsection_remote_desktop | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -530,6 +530,7 @@ def sre_config_yaml(request): | |
admin_email_address: [email protected] | ||
admin_ip_addresses: | ||
- 1.2.3.4/32 | ||
allow_workspace_internet: false | ||
data_provider_ip_addresses: [] | ||
databases: [] | ||
remote_desktop: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
from collections.abc import Callable | ||
from enum import Enum | ||
from functools import partial | ||
|
||
import pulumi | ||
import pulumi.runtime | ||
import pytest | ||
from pulumi_azure_native import network | ||
|
||
from data_safe_haven.infrastructure.programs.sre.firewall import ( | ||
SREFirewallComponent, | ||
SREFirewallProps, | ||
) | ||
|
||
from ..resource_assertions import assert_equal | ||
|
||
|
||
class MyMocks(pulumi.runtime.Mocks): | ||
def new_resource(self, args: pulumi.runtime.MockResourceArgs): | ||
resources = [args.name + "_id", args.inputs] | ||
return resources | ||
|
||
def call(self, _: pulumi.runtime.MockCallArgs): | ||
return {} | ||
|
||
|
||
# TODO: These breaks many other tests! | ||
pulumi.runtime.set_mocks( | ||
MyMocks(), | ||
preview=False, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def allow_internet_props_setter( | ||
location: str, | ||
resource_group_name: str, | ||
subnet_apt_proxy_server: network.GetSubnetResult, | ||
subnet_clamav_mirror: network.GetSubnetResult, | ||
subnet_firewall: network.GetSubnetResult, | ||
subnet_firewall_management: network.GetSubnetResult, | ||
subnet_guacamole_containers: network.GetSubnetResult, | ||
subnet_identity_containers: network.GetSubnetResult, | ||
subnet_user_services_software_repositories: network.GetSubnetResult, | ||
subnet_workspaces: network.GetSubnetResult, | ||
) -> Callable[[bool], SREFirewallProps]: | ||
|
||
def set_allow_workspace_internet( | ||
allow_workspace_internet: bool, # noqa: FBT001 | ||
) -> SREFirewallProps: | ||
return SREFirewallProps( | ||
allow_workspace_internet=allow_workspace_internet, | ||
location=location, | ||
resource_group_name=resource_group_name, | ||
route_table_name="test-route-table", # TODO: Move to fixture if works. | ||
subnet_apt_proxy_server=subnet_apt_proxy_server, | ||
subnet_clamav_mirror=subnet_clamav_mirror, | ||
subnet_firewall=subnet_firewall, | ||
subnet_firewall_management=subnet_firewall_management, | ||
subnet_guacamole_containers=subnet_guacamole_containers, | ||
subnet_identity_containers=subnet_identity_containers, | ||
subnet_user_services_software_repositories=subnet_user_services_software_repositories, | ||
subnet_workspaces=subnet_workspaces, | ||
) | ||
|
||
return set_allow_workspace_internet | ||
|
||
|
||
@pytest.fixture | ||
def allow_internet_component_setter( | ||
stack_name: str, | ||
allow_internet_props_setter: Callable[[bool], SREFirewallProps], | ||
tags: dict[str, str], | ||
) -> Callable[[bool], SREFirewallComponent]: | ||
|
||
def set_allow_workspace_internet(allow_workspace_internet) -> SREFirewallComponent: | ||
firewall_props: SREFirewallProps = allow_internet_props_setter( | ||
allow_workspace_internet | ||
) | ||
|
||
return SREFirewallComponent( | ||
name="firewall-name", | ||
stack_name=stack_name, | ||
props=firewall_props, | ||
tags=tags, | ||
) | ||
|
||
return set_allow_workspace_internet | ||
|
||
|
||
class InternetAccess(Enum): | ||
ENABLED = True | ||
DISABLED = False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we need this enum, it's probably useful outside just testing. Should it live in |
||
|
||
|
||
class TestSREFirewallProps: | ||
|
||
@pulumi.runtime.test | ||
def test_props_allow_workspace_internet_enabled( | ||
self, allow_internet_props_setter: Callable[[bool], SREFirewallProps] | ||
): | ||
firewall_props: SREFirewallProps = allow_internet_props_setter( | ||
allow_workspace_internet=True | ||
) | ||
pulumi.Output.from_input(firewall_props.allow_workspace_internet).apply( | ||
partial(assert_equal, True), run_with_unknowns=True # noqa: FBT003 | ||
) | ||
|
||
@pulumi.runtime.test | ||
def test_props_allow_workspace_internet_disabled( | ||
self, allow_internet_props_setter: Callable[[bool], SREFirewallProps] | ||
): | ||
firewall_props: SREFirewallProps = allow_internet_props_setter( | ||
allow_workspace_internet=False | ||
) | ||
pulumi.Output.from_input(firewall_props.allow_workspace_internet).apply( | ||
partial(assert_equal, False), run_with_unknowns=True # noqa: FBT003 | ||
) | ||
|
||
|
||
class TestSREFirewallComponent: | ||
|
||
@pulumi.runtime.test | ||
def test_component_allow_workspace_internet_enabled( | ||
self, | ||
allow_internet_component_setter: Callable[[bool], SREFirewallComponent], | ||
): | ||
firewall_component: SREFirewallComponent = allow_internet_component_setter( | ||
allow_workspace_internet=True | ||
) | ||
|
||
firewall_component.firewall.application_rule_collections.apply( | ||
partial(TestSREFirewallComponent.assert_allow_internet_access, InternetAccess.ENABLED), # type: ignore | ||
run_with_unknowns=True, | ||
) | ||
|
||
@pulumi.runtime.test | ||
def test_component_allow_workspace_internet_disabled( | ||
self, | ||
allow_internet_component_setter: Callable[[bool], SREFirewallComponent], | ||
): | ||
firewall_component: SREFirewallComponent = allow_internet_component_setter( | ||
allow_workspace_internet=False | ||
) | ||
|
||
firewall_component.firewall.application_rule_collections.apply( | ||
partial(TestSREFirewallComponent.assert_allow_internet_access, InternetAccess.DISABLED), # type: ignore | ||
run_with_unknowns=True, | ||
) | ||
|
||
@staticmethod | ||
def assert_allow_internet_access( | ||
internet_access: InternetAccess, | ||
application_rule_collections: ( | ||
list[network.outputs.AzureFirewallApplicationRuleCollectionResponse] | None | ||
), | ||
): | ||
|
||
if application_rule_collections is not None: | ||
|
||
workspace_deny_collection: list[ | ||
network.outputs.AzureFirewallApplicationRuleCollectionResponse | ||
] = [ | ||
rule_collection | ||
for rule_collection in application_rule_collections | ||
if rule_collection.name == "workspaces-deny" | ||
] | ||
|
||
if internet_access == InternetAccess.ENABLED: | ||
assert not workspace_deny_collection | ||
else: | ||
assert len(workspace_deny_collection) == 1 | ||
|
||
else: | ||
raise AssertionError() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Luckily, you don't need these :). See the
test_application_gateway.py
for an example of Pulumi runtime tests withoutset_mocks
.