forked from cloud-custodian/cloud-custodian
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azure - cosmosdb firewall action (cloud-custodian#4627)
- Loading branch information
1 parent
d115940
commit c2048ef
Showing
10 changed files
with
722 additions
and
174 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
docs/source/azure/examples/firewall-cosmosaction-networking.rst
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,45 @@ | ||
Firewall - Update CosmosDB Rules | ||
============================================ | ||
|
||
In this example we identify Cosmos DB accounts that either have no firewall | ||
configured or which have one configured which is allowing access outside of | ||
expected ranges. | ||
|
||
We then reconfigure that firewall to known-safe defaults which include a bypass for | ||
all of the Azure Cloud as well as additional space in our data center. | ||
|
||
Virtual network rules are not specified so they will not be modified. | ||
|
||
.. code-block:: yaml | ||
policies: | ||
- name: cosmos-firewall-enable | ||
description: | | ||
Find all incorrect firewalls and enable | ||
with a set of defaults | ||
resource: azure.cosmosdb | ||
filters: | ||
- or: | ||
- type: value | ||
key: properties.ipRangeFilter | ||
value: empty # The firewall is disabled | ||
- not: | ||
- type: firewall-rules | ||
only: # Should *only* allow access within the specified maximums here | ||
- 19.0.0.0/16 | ||
- 20.0.1.2 | ||
- ServiceTags.AzureCloud | ||
actions: | ||
- type: set-firewall-rules | ||
append: False | ||
bypass-rules: # Enable firewall and allow all Azure Cloud | ||
- AzureCloud | ||
- Portal | ||
ip-rules: # and some external IP space | ||
- 19.0.0.0/16 | ||
- 20.0.1.2 | ||
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
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,82 @@ | ||
# Copyright 2019 Microsoft Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from abc import abstractmethod | ||
|
||
from c7n_azure.actions.base import AzureBaseAction | ||
from c7n_azure.utils import resolve_service_tag_alias | ||
from netaddr import IPAddress | ||
|
||
from c7n.filters.core import type_schema | ||
|
||
|
||
class SetFirewallAction(AzureBaseAction): | ||
|
||
schema = type_schema( | ||
'set-firewall-rules', | ||
required=[], | ||
**{ | ||
'append': {'type': 'boolean', 'default': True}, | ||
'bypass-rules': {'type': 'array'}, | ||
'ip-rules': {'type': 'array', 'items': {'type': 'string'}}, | ||
'virtual-network-rules': {'type': 'array', 'items': {'type': 'string'}} | ||
} | ||
) | ||
|
||
@abstractmethod | ||
def __init__(self, data, manager=None): | ||
super(SetFirewallAction, self).__init__(data, manager) | ||
|
||
def _prepare_processing(self): | ||
self.client = self.manager.get_client() | ||
self.append = self.data.get('append', True) | ||
|
||
@abstractmethod | ||
def _process_resource(self, resource): | ||
pass | ||
|
||
def _build_bypass_rules(self, existing_bypass, new_rules): | ||
if self.append: | ||
without_duplicates = [r for r in existing_bypass if r not in new_rules] | ||
new_rules.extend(without_duplicates) | ||
return ','.join(new_rules or ['None']) | ||
|
||
def _build_vnet_rules(self, existing_vnet, new_rules): | ||
if self.append: | ||
without_duplicates = [r for r in existing_vnet if r not in new_rules] | ||
new_rules.extend(without_duplicates) | ||
return new_rules | ||
|
||
def _build_ip_rules(self, existing_ip, new_rules): | ||
rules = [] | ||
for rule in new_rules: | ||
# attempt to resolve this rule as a service tag alias | ||
# if it isn't a valid alias then we'll get `None` back. | ||
resolved_set = resolve_service_tag_alias(rule) | ||
if resolved_set: | ||
# this is a service tag alias, so we need to insert the whole | ||
# aliased array into the ruleset | ||
ranges = list(resolved_set.iter_cidrs()) | ||
for r in range(len(ranges)): | ||
if len(ranges[r]) == 1: | ||
ranges[r] = IPAddress(ranges[r].first) | ||
rules.extend(map(str, ranges)) | ||
else: | ||
# just a normal rule, append | ||
rules.append(rule) | ||
|
||
if self.append: | ||
without_duplicates = [r for r in existing_ip if r not in rules] | ||
rules.extend(without_duplicates) | ||
return rules |
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
Oops, something went wrong.