Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/emr/emrfsutils.py
1567 views
1
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
14
from awscli.customizations.emr import constants
15
from awscli.customizations.emr import emrutils
16
from awscli.customizations.emr import exceptions
17
from botocore.compat import OrderedDict
18
19
20
CONSISTENT_OPTIONAL_KEYS = ['RetryCount', 'RetryPeriod']
21
CSE_KMS_REQUIRED_KEYS = ['KMSKeyId']
22
CSE_CUSTOM_REQUIRED_KEYS = ['CustomProviderLocation', 'CustomProviderClass']
23
CSE_PROVIDER_TYPES = [constants.EMRFS_KMS, constants.EMRFS_CUSTOM]
24
ENCRYPTION_TYPES = [constants.EMRFS_CLIENT_SIDE, constants.EMRFS_SERVER_SIDE]
25
26
CONSISTENT_OPTION_NAME = "--emrfs Consistent=true/false"
27
CSE_OPTION_NAME = '--emrfs Encryption=ClientSide'
28
CSE_KMS_OPTION_NAME = '--emrfs Encryption=ClientSide,ProviderType=KMS'
29
CSE_CUSTOM_OPTION_NAME = '--emrfs Encryption=ClientSide,ProviderType=Custom'
30
31
32
def build_bootstrap_action_configs(region, emrfs_args):
33
bootstrap_actions = []
34
35
_verify_emrfs_args(emrfs_args)
36
37
if _need_to_configure_cse(emrfs_args, 'CUSTOM'):
38
# Download custom encryption provider from Amazon S3 to EMR Cluster
39
bootstrap_actions.append(
40
emrutils.build_bootstrap_action(
41
path=constants.EMRFS_CSE_CUSTOM_S3_GET_BA_PATH,
42
name=constants.S3_GET_BA_NAME,
43
args=[constants.S3_GET_BA_SRC,
44
emrfs_args.get('CustomProviderLocation'),
45
constants.S3_GET_BA_DEST,
46
constants.EMRFS_CUSTOM_DEST_PATH,
47
constants.S3_GET_BA_FORCE]))
48
49
emrfs_setup_ba_args = _build_ba_args_to_setup_emrfs(emrfs_args)
50
bootstrap_actions.append(
51
emrutils.build_bootstrap_action(
52
path=emrutils.build_s3_link(
53
relative_path=constants.CONFIG_HADOOP_PATH,
54
region=region),
55
name=constants.EMRFS_BA_NAME,
56
args=emrfs_setup_ba_args))
57
58
return bootstrap_actions
59
60
61
def build_emrfs_confiuration(emrfs_args):
62
_verify_emrfs_args(emrfs_args)
63
emrfs_properties = _build_emrfs_properties(emrfs_args)
64
65
if _need_to_configure_cse(emrfs_args, 'CUSTOM'):
66
emrfs_properties[constants.EMRFS_CSE_CUSTOM_PROVIDER_URI_KEY] = \
67
emrfs_args.get('CustomProviderLocation')
68
69
emrfs_configuration = {
70
'Classification': constants.EMRFS_SITE,
71
'Properties': emrfs_properties}
72
73
return emrfs_configuration
74
75
76
def _verify_emrfs_args(emrfs_args):
77
# Encryption should have a valid value
78
if 'Encryption' in emrfs_args \
79
and emrfs_args['Encryption'].upper() not in ENCRYPTION_TYPES:
80
raise exceptions.UnknownEncryptionTypeError(
81
encryption=emrfs_args['Encryption'])
82
83
# Only one of SSE and Encryption should be configured
84
if 'SSE' in emrfs_args and 'Encryption' in emrfs_args:
85
raise exceptions.BothSseAndEncryptionConfiguredError(
86
sse=emrfs_args['SSE'], encryption=emrfs_args['Encryption'])
87
88
# CSE should be configured correctly
89
# ProviderType should be present and should have valid value
90
# Given the type, the required parameters should be present
91
if ('Encryption' in emrfs_args and
92
emrfs_args['Encryption'].upper() == constants.EMRFS_CLIENT_SIDE):
93
if 'ProviderType' not in emrfs_args:
94
raise exceptions.MissingParametersError(
95
object_name=CSE_OPTION_NAME, missing='ProviderType')
96
elif emrfs_args['ProviderType'].upper() not in CSE_PROVIDER_TYPES:
97
raise exceptions.UnknownCseProviderTypeError(
98
provider_type=emrfs_args['ProviderType'])
99
elif emrfs_args['ProviderType'].upper() == 'KMS':
100
_verify_required_args(emrfs_args.keys(), CSE_KMS_REQUIRED_KEYS,
101
CSE_KMS_OPTION_NAME)
102
elif emrfs_args['ProviderType'].upper() == 'CUSTOM':
103
_verify_required_args(emrfs_args.keys(), CSE_CUSTOM_REQUIRED_KEYS,
104
CSE_CUSTOM_OPTION_NAME)
105
106
# No child attributes should be present if the parent feature is not
107
# configured
108
if 'Consistent' not in emrfs_args:
109
_verify_child_args(emrfs_args.keys(), CONSISTENT_OPTIONAL_KEYS,
110
CONSISTENT_OPTION_NAME)
111
if not _need_to_configure_cse(emrfs_args, 'KMS'):
112
_verify_child_args(emrfs_args.keys(), CSE_KMS_REQUIRED_KEYS,
113
CSE_KMS_OPTION_NAME)
114
if not _need_to_configure_cse(emrfs_args, 'CUSTOM'):
115
_verify_child_args(emrfs_args.keys(), CSE_CUSTOM_REQUIRED_KEYS,
116
CSE_CUSTOM_OPTION_NAME)
117
118
119
def _verify_required_args(actual_keys, required_keys, object_name):
120
if any(x not in actual_keys for x in required_keys):
121
missing_keys = list(
122
sorted(set(required_keys).difference(set(actual_keys))))
123
raise exceptions.MissingParametersError(
124
object_name=object_name, missing=emrutils.join(missing_keys))
125
126
127
def _verify_child_args(actual_keys, child_keys, parent_object_name):
128
if any(x in actual_keys for x in child_keys):
129
invalid_keys = list(
130
sorted(set(child_keys).intersection(set(actual_keys))))
131
raise exceptions.InvalidEmrFsArgumentsError(
132
invalid=emrutils.join(invalid_keys),
133
parent_object_name=parent_object_name)
134
135
136
def _build_ba_args_to_setup_emrfs(emrfs_args):
137
emrfs_properties = _build_emrfs_properties(emrfs_args)
138
139
return _create_ba_args(emrfs_properties)
140
141
142
def _build_emrfs_properties(emrfs_args):
143
"""
144
Assumption: emrfs_args is valid i.e. all required attributes are present
145
"""
146
emrfs_properties = OrderedDict()
147
148
if _need_to_configure_consistent_view(emrfs_args):
149
_update_properties_for_consistent_view(emrfs_properties, emrfs_args)
150
151
if _need_to_configure_sse(emrfs_args):
152
_update_properties_for_sse(emrfs_properties, emrfs_args)
153
154
if _need_to_configure_cse(emrfs_args, 'KMS'):
155
_update_properties_for_cse(emrfs_properties, emrfs_args, 'KMS')
156
157
if _need_to_configure_cse(emrfs_args, 'CUSTOM'):
158
_update_properties_for_cse(emrfs_properties, emrfs_args, 'CUSTOM')
159
160
if 'Args' in emrfs_args:
161
for arg_value in emrfs_args.get('Args'):
162
key, value = emrutils.split_to_key_value(arg_value)
163
emrfs_properties[key] = value
164
165
return emrfs_properties
166
167
168
def _need_to_configure_consistent_view(emrfs_args):
169
return 'Consistent' in emrfs_args
170
171
172
def _need_to_configure_sse(emrfs_args):
173
return 'SSE' in emrfs_args \
174
or ('Encryption' in emrfs_args and
175
emrfs_args['Encryption'].upper() == constants.EMRFS_SERVER_SIDE)
176
177
178
def _need_to_configure_cse(emrfs_args, cse_type):
179
return ('Encryption' in emrfs_args and
180
emrfs_args['Encryption'].upper() == constants.EMRFS_CLIENT_SIDE and
181
'ProviderType' in emrfs_args and
182
emrfs_args['ProviderType'].upper() == cse_type)
183
184
185
def _update_properties_for_consistent_view(emrfs_properties, emrfs_args):
186
emrfs_properties[constants.EMRFS_CONSISTENT_KEY] = \
187
str(emrfs_args['Consistent']).lower()
188
189
if 'RetryCount' in emrfs_args:
190
emrfs_properties[constants.EMRFS_RETRY_COUNT_KEY] = \
191
str(emrfs_args['RetryCount'])
192
193
if 'RetryPeriod' in emrfs_args:
194
emrfs_properties[constants.EMRFS_RETRY_PERIOD_KEY] = \
195
str(emrfs_args['RetryPeriod'])
196
197
198
def _update_properties_for_sse(emrfs_properties, emrfs_args):
199
sse_value = emrfs_args['SSE'] if 'SSE' in emrfs_args else True
200
# if 'SSE' is not in emrfs_args then 'Encryption' must be 'ServerSide'
201
202
emrfs_properties[constants.EMRFS_SSE_KEY] = str(sse_value).lower()
203
204
205
def _update_properties_for_cse(emrfs_properties, emrfs_args, cse_type):
206
emrfs_properties[constants.EMRFS_CSE_KEY] = 'true'
207
if cse_type == 'KMS':
208
emrfs_properties[
209
constants.EMRFS_CSE_ENCRYPTION_MATERIALS_PROVIDER_KEY] = \
210
constants.EMRFS_CSE_KMS_PROVIDER_FULL_CLASS_NAME
211
212
emrfs_properties[constants.EMRFS_CSE_KMS_KEY_ID_KEY] =\
213
emrfs_args['KMSKeyId']
214
215
elif cse_type == 'CUSTOM':
216
emrfs_properties[
217
constants.EMRFS_CSE_ENCRYPTION_MATERIALS_PROVIDER_KEY] = \
218
emrfs_args['CustomProviderClass']
219
220
221
def _update_emrfs_ba_args(ba_args, key_value):
222
ba_args.append(constants.EMRFS_BA_ARG_KEY)
223
ba_args.append(key_value)
224
225
226
def _create_ba_args(emrfs_properties):
227
ba_args = []
228
for key, value in emrfs_properties.items():
229
key_value = key
230
if value:
231
key_value = key_value + "=" + value
232
_update_emrfs_ba_args(ba_args, key_value)
233
234
return ba_args
235
236