#!/usr/bin/env python3

# Microsoft Azure Linux Agent
#
# Copyright 2018 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.
#

import json

from typing import Any, Dict

from tests_e2e.tests.lib.network_security_rule import NetworkSecurityRule
from tests_e2e.tests.lib.update_arm_template import UpdateArmTemplate


class DenyOutboundConnections(UpdateArmTemplate):
    """
    Updates the ARM template to add a security rule that denies all outbound connections.
    """
    def update(self, template: Dict[str, Any], is_lisa_template: bool) -> None:
        NetworkSecurityRule(template, is_lisa_template).add_security_rule(
            json.loads("""{
                "name": "waagent-no-outbound",
                "properties": {
                    "description": "Denies all outbound connections.",
                    "protocol": "*",
                    "sourcePortRange": "*",
                    "destinationPortRange": "*",
                    "sourceAddressPrefix": "*",
                    "destinationAddressPrefix": "Internet",
                    "access": "Deny",
                    "priority": 200,
                    "direction": "Outbound"
                }
            }"""))
