-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathesd_filehandler.py
More file actions
241 lines (195 loc) · 8.2 KB
/
Copy pathesd_filehandler.py
File metadata and controls
241 lines (195 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# coding=utf-8
# Copyright (c) 2014-2018, F5 Networks, Inc.
#
# 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 copy
import glob
import json
import os
import types
from oslo_log import log as logging
from f5_openstack_agent.lbaasv2.drivers.bigip import exceptions as f5_ex
from f5_openstack_agent.lbaasv2.drivers.bigip.resource_helper import \
BigIPResourceHelper
from f5_openstack_agent.lbaasv2.drivers.bigip.resource_helper import \
ResourceType
LOG = logging.getLogger(__name__)
class EsdTagProcessor(object):
"""Class processes json dictionary
It checks compares the tags from esdjson dictionary to list of valid tags
"""
_instance = None
esd_dict = {}
def __new__(cls, *args, **kwargs):
if not isinstance(cls._instance, cls):
cls._instance = object.__new__(cls, *args, **kwargs)
return cls._instance
def read_json(self, esddir):
esdJSONFileList = glob.glob(os.path.join(esddir, '*.json'))
esdJSONDict = {}
for fileList in esdJSONFileList:
try:
with open(fileList) as json_file:
# Reading each file to a dictionary
fileJSONDict = json.load(json_file)
# Combine all dictionaries to one
esdJSONDict.update(fileJSONDict)
except ValueError as err:
LOG.error('ESD JSON File is invalid: %s', err)
raise f5_ex.esdJSONFileInvalidException()
return esdJSONDict
def process_esd(self, bigips, esddir):
esd_json_backup = {}
esd_json_backup = copy.deepcopy(self.esd_dict)
try:
dict = self.read_json(esddir)
self.esd_dict = self.verify_esd_dict(bigips, dict)
except f5_ex.esdJSONFileInvalidException:
# if error happens, we set backup esd.
self.esd_dict = copy.deepcopy(esd_json_backup)
LOG.warning(
"ESD JSON File is invalid, "
"ESD JSON Dict is restored to %s",
esd_json_backup
)
raise
def get_esd(self, name):
return self.esd_dict.get(name, None)
def is_esd(self, name):
return self.get_esd(name) is not None
def resource_exists(self, bigip, tag_name, resource_type):
helper = BigIPResourceHelper(resource_type)
name = tag_name
# allow user to define chain cert name with or without '.crt'
if resource_type == ResourceType.ssl_cert_file and not \
name.endswith('.crt'):
name += '.crt'
return helper.exists_in_collection(bigip, name)
def get_resource_type(self, bigip, resource_type, value):
if resource_type == ResourceType.persistence:
return self.get_persistence_type(bigip, value)
else:
return resource_type
def get_persistence_type(self, bigip, value):
resource_types = [
ResourceType.cookie_persistence,
ResourceType.dest_addr_persistence,
ResourceType.source_addr_persistence,
ResourceType.hash_persistence,
ResourceType.msrdp_persistence,
ResourceType.sip_persistence,
ResourceType.ssl_persistence,
ResourceType.universal_persistence]
for resource_type in resource_types:
if self.resource_exists(bigip, value, resource_type):
return resource_type
return None
def is_valid_tag(self, tag):
return self.valid_esd_tags.get(tag, None) is not None
def is_valid_value(self, bigip, value, resource_type):
return self.resource_exists(bigip, value, resource_type)
def is_valid_value_list(self, bigip, value, resource_type):
for v in value:
if not self.resource_exists(bigip, v, resource_type):
return False
return True
def verify_esd_dict(self, bigips, esd_dict):
valid_esd_dict = {}
for esd in esd_dict:
# check that ESD is valid for every BIG-IP
valid_esd = True
for bigip in bigips:
valid_esd = self.verify_esd(bigip, esd, esd_dict[esd])
if not valid_esd:
break
if valid_esd:
# add non-empty valid ESD to return dict
valid_esd_dict[esd] = valid_esd
return valid_esd_dict
def verify_esd(self, bigip, name, esd):
valid_esd = {}
for tag in esd:
try:
self.verify_tag(tag)
self.verify_value(bigip, tag, esd[tag])
# add tag to valid ESD
valid_esd[tag] = esd[tag]
LOG.debug("Tag {0} is valid for ESD {1}.".format(tag, name))
except f5_ex.esdJSONFileInvalidException as err:
LOG.error('Tag {0} failed validation for ESD {1} and was not '
'added to ESD. Error: {2}'.
format(tag, name, err.message))
return valid_esd
def verify_value(self, bigip, tag, value):
tag_def = self.valid_esd_tags.get(tag)
# verify resource type
resource_type = self.get_resource_type(
bigip, tag_def['resource_type'], value)
if not resource_type:
msg = 'Unable to determine resource type for tag {0} and ' \
'value {1}'.format(tag, value)
raise f5_ex.esdJSONFileInvalidException(msg)
# verify value type
value_type = tag_def['value_type']
if not isinstance(value, value_type):
msg = 'Invalid value {0} for tag {1}. ' \
'Type must be {2}.'.format(value, tag, value_type)
raise f5_ex.esdJSONFileInvalidException(msg)
# verify value exists on BIG-IP
if isinstance(value, list):
is_valid = self.is_valid_value_list(bigip, value, resource_type)
else:
is_valid = self.is_valid_value(bigip, value, resource_type)
if not is_valid:
msg = ("Invalid value {0} for tag {1}".format(value, tag))
raise f5_ex.esdJSONFileInvalidException(msg)
def verify_tag(self, tag):
if not self.is_valid_tag(tag):
msg = 'Tag {0} is not valid.'.format(tag)
raise f5_ex.esdJSONFileInvalidException(msg)
# This dictionary contains all the tags
# that are listed in the esd confluence page.
# We are implementing the tags that can be applied only to listeners.
valid_esd_tags = {
'lbaas_ctcp': {
'resource_type': ResourceType.tcp_profile,
'value_type': types.StringTypes},
'lbaas_stcp': {
'resource_type': ResourceType.tcp_profile,
'value_type': types.StringTypes},
'lbaas_cssl_profile': {
'resource_type': ResourceType.client_ssl_profile,
'value_type': types.StringTypes},
'lbaas_sssl_profile': {
'resource_type': ResourceType.server_ssl_profile,
'value_type': types.StringTypes},
'lbaas_http_profile': {
'resource_type': ResourceType.http_profile,
'value_type': types.StringTypes},
'lbaas_irule': {
'resource_type': ResourceType.rule,
'value_type': types.ListType},
'lbaas_policy': {
'resource_type': ResourceType.l7policy,
'value_type': types.ListType},
'lbaas_persist': {
'resource_type': ResourceType.persistence,
'value_type': types.StringTypes},
'lbaas_fallback_persist': {
'resource_type': ResourceType.persistence,
'value_type': types.StringTypes},
'lbaas_oneconnect_profile': {
'resource_type': ResourceType.oneconnect,
'value_type': types.StringTypes}
}