|
| 1 | +# Copyright The Cloud Custodian Authors. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import logging |
| 5 | +from c7n.utils import type_schema |
| 6 | +from c7n.filters.core import ValueFilter |
| 7 | + |
| 8 | +from huaweicloudsdkcore.exceptions import exceptions |
| 9 | +from huaweicloudsdkwaf.v1.model.update_lts_info_config_request import UpdateLtsInfoConfigRequest |
| 10 | +from huaweicloudsdkwaf.v1.model.update_lts_info_config_request_body import UpdateLtsInfoConfigRequestBody |
| 11 | +from huaweicloudsdkwaf.v1.model.lts_id_info import LtsIdInfo |
| 12 | + |
| 13 | +from c7n_huaweicloud.actions.base import HuaweiCloudBaseAction |
| 14 | +from c7n_huaweicloud.provider import resources |
| 15 | +from c7n_huaweicloud.query import QueryResourceManager, TypeInfo |
| 16 | + |
| 17 | +log = logging.getLogger('custodian.huaweicloud.resources.waf') |
| 18 | + |
| 19 | + |
| 20 | +@resources.register('waf-policy') |
| 21 | +class WafPolicy(QueryResourceManager): |
| 22 | + """Huawei Cloud Web Application Firewall (WAF) Policy Resource |
| 23 | +
|
| 24 | + :example: |
| 25 | +
|
| 26 | + .. code-block:: yaml |
| 27 | +
|
| 28 | + policies: |
| 29 | + - name: waf-policy-list |
| 30 | + resource: huaweicloud.waf-policy |
| 31 | + """ |
| 32 | + |
| 33 | + class resource_type(TypeInfo): |
| 34 | + """Define WAF policy resource metadata and type information""" |
| 35 | + service = 'waf-policy' # Specify the corresponding Huawei Cloud service name |
| 36 | + # Specify the API operation, result list key name, and pagination parameters for enumerating resources |
| 37 | + # 'list_policy' is the API method name |
| 38 | + # 'items' is the field name containing the instance list in the response |
| 39 | + # None indicates no pagination is used |
| 40 | + enum_spec = ('list_policy', 'items', "waf") |
| 41 | + id = 'id' # Specify the resource unique identifier field name |
| 42 | + name = 'name' # Specify the resource name field |
| 43 | + date = 'timestamp' # Specify the resource creation time field |
| 44 | + arn_type = 'waf-policy' # Resource ARN type |
| 45 | + tag_resource_type = None # Tag not supported |
| 46 | + |
| 47 | + |
| 48 | +@resources.register('waf-log-config') |
| 49 | +class WafLogConfig(QueryResourceManager): |
| 50 | + """Huawei Cloud Web Application Firewall (WAF) Log Configuration Resource |
| 51 | +
|
| 52 | + :example: |
| 53 | +
|
| 54 | + .. code-block:: yaml |
| 55 | +
|
| 56 | + policies: |
| 57 | + - name: waf-log-config-list |
| 58 | + resource: huaweicloud.waf-log-config |
| 59 | + """ |
| 60 | + |
| 61 | + class resource_type(TypeInfo): |
| 62 | + """Define WAF log configuration resource metadata and type information""" |
| 63 | + service = 'waf-log-config' # Specify the corresponding Huawei Cloud service name |
| 64 | + # Specify the API operation, result list key name, and pagination parameters for enumerating resources |
| 65 | + # 'show_lts_info_config' is the API method name |
| 66 | + # 'lts_info' is the field name containing the instance info in the response |
| 67 | + # None indicates no pagination is used |
| 68 | + enum_spec = ('show_lts_info_config', "[ @ ]", None) |
| 69 | + id = 'id' # Specify the resource unique identifier field name |
| 70 | + name = 'id' # Specify the resource name field |
| 71 | + arn_type = 'waf-log-config' # Resource ARN type |
| 72 | + tag_resource_type = None # Tag not supported |
| 73 | + |
| 74 | + |
| 75 | +@WafLogConfig.filter_registry.register('enabled') |
| 76 | +class LogEnabledFilter(ValueFilter): |
| 77 | + """Filter by WAF log configuration enabled status |
| 78 | +
|
| 79 | + :example: |
| 80 | +
|
| 81 | + .. code-block:: yaml |
| 82 | +
|
| 83 | + policies: |
| 84 | + - name: waf-logs-disabled |
| 85 | + resource: huaweicloud.waf-log-config |
| 86 | + filters: |
| 87 | + - type: enabled |
| 88 | + value: false # Not enabled |
| 89 | + """ |
| 90 | + schema = type_schema('enabled', rinherit=ValueFilter.schema) |
| 91 | + |
| 92 | + def __init__(self, data, manager=None): |
| 93 | + """Initialize the filter |
| 94 | +
|
| 95 | + :param data: Filter configuration data |
| 96 | + :param manager: Resource manager |
| 97 | + """ |
| 98 | + super(LogEnabledFilter, self).__init__(data, manager) |
| 99 | + self.data['key'] = 'enabled' |
| 100 | + |
| 101 | + |
| 102 | +@WafLogConfig.action_registry.register('update') |
| 103 | +class UpdateLogConfig(HuaweiCloudBaseAction): |
| 104 | + """Update WAF log configuration |
| 105 | +
|
| 106 | + This operation allows enabling/disabling WAF log configuration and setting log group and log stream information. |
| 107 | +
|
| 108 | + :example: |
| 109 | +
|
| 110 | + .. code-block:: yaml |
| 111 | +
|
| 112 | + policies: |
| 113 | + - name: enable-waf-logs |
| 114 | + resource: huaweicloud.waf-log-config |
| 115 | + filters: |
| 116 | + - type: enabled |
| 117 | + value: false # Not enabled |
| 118 | + actions: |
| 119 | + - type: update |
| 120 | + enabled: true # Enabled |
| 121 | + lts_id_info: |
| 122 | + lts_group_id: "4bcff74d-f649-41c8-8325-1b0a264ff683" |
| 123 | + lts_access_stream_id: "0a7ef713-cc3e-418d-abda-85df04db1a3c" |
| 124 | + lts_attack_stream_id: "f4fa07f6-277b-4e4a-a257-26508ece81e6" |
| 125 | + """ |
| 126 | + schema = type_schema( |
| 127 | + 'update', |
| 128 | + enabled={'type': 'boolean'}, |
| 129 | + lts_id_info={ |
| 130 | + 'type': 'object', |
| 131 | + 'properties': { |
| 132 | + 'lts_group_id': {'type': 'string'}, |
| 133 | + 'lts_access_stream_id': {'type': 'string'}, |
| 134 | + 'lts_attack_stream_id': {'type': 'string'} |
| 135 | + }, |
| 136 | + 'additionalProperties': False |
| 137 | + } |
| 138 | + ) |
| 139 | + |
| 140 | + def perform_action(self, resource): |
| 141 | + """Perform action on a single resource |
| 142 | +
|
| 143 | + :param resource: Resource to perform action on |
| 144 | + :return: API response |
| 145 | + """ |
| 146 | + client = self.manager.get_client() |
| 147 | + resource_id = resource['id'] |
| 148 | + |
| 149 | + # Construct log information object |
| 150 | + lts_id_info_data = self.data.get('lts_id_info') |
| 151 | + lts_id_info = None |
| 152 | + |
| 153 | + if lts_id_info_data: |
| 154 | + lts_id_info = LtsIdInfo( |
| 155 | + lts_group_id=lts_id_info_data.get('lts_group_id'), |
| 156 | + lts_access_stream_id=lts_id_info_data.get('lts_access_stream_id'), |
| 157 | + lts_attack_stream_id=lts_id_info_data.get('lts_attack_stream_id') |
| 158 | + ) |
| 159 | + |
| 160 | + # Construct request body |
| 161 | + request_body = UpdateLtsInfoConfigRequestBody( |
| 162 | + enabled=self.data.get('enabled'), |
| 163 | + lts_id_info=lts_id_info |
| 164 | + ) |
| 165 | + |
| 166 | + # Construct request |
| 167 | + request = UpdateLtsInfoConfigRequest( |
| 168 | + ltsconfig_id=resource_id, |
| 169 | + body=request_body |
| 170 | + ) |
| 171 | + |
| 172 | + # Get enterprise project ID from resource data |
| 173 | + if 'enterprise_project_id' in resource: |
| 174 | + request.enterprise_project_id = resource['enterprise_project_id'] |
| 175 | + |
| 176 | + try: |
| 177 | + # Call API to update log configuration |
| 178 | + response = client.update_lts_info_config(request) |
| 179 | + log.info(f"Updated WAF log configuration: {resource_id}") |
| 180 | + return response |
| 181 | + except exceptions.ClientRequestException as e: |
| 182 | + log.error(f"Failed to update WAF log configuration: {e.status_code}, {e.request_id}, {e.error_code}, {e.error_msg}") |
| 183 | + raise |
0 commit comments