|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | + |
| 20 | +import json |
| 21 | +import logging |
| 22 | +from apache_ranger.exceptions import RangerServiceException |
| 23 | +from apache_ranger.client.ranger_client import RangerClientHttp |
| 24 | +from apache_ranger.model.ranger_kms import RangerKey |
| 25 | +from apache_ranger.model.ranger_kms import RangerKeyVersion |
| 26 | +from apache_ranger.model.ranger_kms import RangerKeyMetadata |
| 27 | +from apache_ranger.model.ranger_kms import RangerEncryptedKeyVersion |
| 28 | +from apache_ranger.utils import * |
| 29 | + |
| 30 | +LOG = logging.getLogger(__name__) |
| 31 | + |
| 32 | +# |
| 33 | +# Python client for KMS REST APIs |
| 34 | +# More details in https://hadoop.apache.org/docs/current/hadoop-kms/index.html#KMS_HTTP_REST_API |
| 35 | +# |
| 36 | +class RangerKMSClient: |
| 37 | + def __init__(self, url, auth): |
| 38 | + self.client_http = RangerClientHttp(url, auth) |
| 39 | + |
| 40 | + logging.getLogger("requests").setLevel(logging.WARNING) |
| 41 | + |
| 42 | + |
| 43 | + def create_key(self, key): |
| 44 | + resp = self.client_http.call_api(RangerKMSClient.CREATE_KEY, request_data=key) |
| 45 | + |
| 46 | + return type_coerce(resp, RangerKeyVersion) |
| 47 | + |
| 48 | + def rollover_key(self, key_name, material=None): |
| 49 | + resp = self.client_http.call_api(RangerKMSClient.ROLLOVER_KEY.format_path({ 'name': key_name }), request_data={ 'material': material}) |
| 50 | + |
| 51 | + return type_coerce(resp, RangerKeyVersion) |
| 52 | + |
| 53 | + def invalidate_cache_for_key(self, key_name): |
| 54 | + self.client_http.call_api(RangerKMSClient.INVALIDATE_CACHE_FOR_KEY.format_path({ 'name': key_name })) |
| 55 | + |
| 56 | + def delete_key(self, key_name): |
| 57 | + self.client_http.call_api(RangerKMSClient.DELETE_KEY.format_path({ 'name': key_name })) |
| 58 | + |
| 59 | + def get_key_metadata(self, key_name): |
| 60 | + resp = self.client_http.call_api(RangerKMSClient.GET_KEY_METADATA.format_path({ 'name': key_name })) |
| 61 | + |
| 62 | + return type_coerce(resp, RangerKeyMetadata) |
| 63 | + |
| 64 | + def get_current_key(self, key_name): |
| 65 | + resp = self.client_http.call_api(RangerKMSClient.GET_CURRENT_KEY.format_path({ 'name': key_name })) |
| 66 | + |
| 67 | + return type_coerce(resp, RangerKeyVersion) |
| 68 | + |
| 69 | + def generate_encrypted_key(self, key_name, num_keys): |
| 70 | + resp = self.client_http.call_api(RangerKMSClient.GENERATE_ENCRYPTED_KEY.format_path({'name': key_name}), query_params={'eek_op': 'generate', 'num_keys': num_keys}) |
| 71 | + |
| 72 | + return type_coerce_list(resp, RangerEncryptedKeyVersion) |
| 73 | + |
| 74 | + def decrypt_encrypted_key(self, key_name, version_name, iv, material): |
| 75 | + resp = self.client_http.call_api(RangerKMSClient.DECRYPT_ENCRYPTED_KEY.format_path({'version_name': version_name}), request_data={'name': key_name, 'iv': iv, 'material': material}, query_params={'eek_op': 'decrypt'}) |
| 76 | + |
| 77 | + return type_coerce(resp, RangerKeyVersion) |
| 78 | + |
| 79 | + def reencrypt_encrypted_key(self, key_name, version_name, iv, material): |
| 80 | + resp = self.client_http.call_api(RangerKMSClient.REENCRYPT_ENCRYPTED_KEY.format_path({'version_name': version_name}), request_data={'name': key_name, 'iv': iv, 'material': material}, query_params={'eek_op': 'reencrypt'}) |
| 81 | + |
| 82 | + return type_coerce(resp, RangerEncryptedKeyVersion) |
| 83 | + |
| 84 | + def batch_reencrypt_encrypted_keys(self, key_name, encrypted_key_versions): |
| 85 | + resp = self.client_http.call_api(RangerKMSClient.BATCH_REENCRYPT_ENCRYPTED_KEYS.format_path({'name': key_name}), request_data=encrypted_key_versions) |
| 86 | + |
| 87 | + return type_coerce_list(resp, RangerEncryptedKeyVersion) |
| 88 | + |
| 89 | + def get_key_version(self, version_name): |
| 90 | + resp = self.client_http.call_api(RangerKMSClient.GET_KEY_VERSION.format_path({ 'version_name': version_name })) |
| 91 | + |
| 92 | + return type_coerce(resp, RangerKeyVersion) |
| 93 | + |
| 94 | + def get_key_versions(self, key_name): |
| 95 | + resp = self.client_http.call_api(RangerKMSClient.GET_KEY_VERSIONS.format_path({ 'name': key_name})) |
| 96 | + |
| 97 | + return type_coerce_list(resp, RangerKeyVersion) |
| 98 | + |
| 99 | + def get_key_names(self): |
| 100 | + resp = self.client_http.call_api(RangerKMSClient.GET_KEYS_NAMES) |
| 101 | + |
| 102 | + return resp |
| 103 | + |
| 104 | + def get_keys_metadata(self, key_names): |
| 105 | + resp = self.client_http.call_api(RangerKMSClient.GET_KEYS_METADATA, query_params={'key': key_names}) |
| 106 | + |
| 107 | + return type_coerce_list(resp, RangerKeyMetadata) |
| 108 | + |
| 109 | + # Ranger KMS |
| 110 | + def get_key(self, key_name): |
| 111 | + resp = self.client_http.call_api(RangerKMSClient.GET_KEY.format_path({ 'name': key_name })) |
| 112 | + |
| 113 | + return type_coerce(resp, RangerKeyMetadata) |
| 114 | + |
| 115 | + # Ranger KMS |
| 116 | + def kms_status(self): |
| 117 | + resp = self.client_http.call_api(RangerKMSClient.KMS_STATUS) |
| 118 | + |
| 119 | + return resp |
| 120 | + |
| 121 | + # URIs |
| 122 | + URI_KEYS = "kms/v1/keys" |
| 123 | + URI_KEY_BY_NAME = "kms/v1/key/{name}" |
| 124 | + URI_KEY_INVALIDATE_CACHE = URI_KEY_BY_NAME + "/_invalidatecache" |
| 125 | + URI_KEY_METADATA = URI_KEY_BY_NAME + "/_metadata" |
| 126 | + URI_CURRENT_KEY = URI_KEY_BY_NAME + "/_currentversion" |
| 127 | + URI_KEY_EEK = URI_KEY_BY_NAME + "/_eek" |
| 128 | + URI_BATCH_REENCRYPT_KEYS = URI_KEY_BY_NAME + "/_reencryptbatch" |
| 129 | + URI_KEY_VERSIONS = URI_KEY_BY_NAME + "/_versions" |
| 130 | + URI_KEY_VERSION_BY_NAME = "kms/v1/keyversion/{version_name}" |
| 131 | + URI_KEY_VERSION_BY_NAME_EEK = URI_KEY_VERSION_BY_NAME + "/_eek" |
| 132 | + URI_KEYS_NAMES = URI_KEYS + "/names" |
| 133 | + URI_KEYS_METADATA = URI_KEYS + "/metadata" |
| 134 | + |
| 135 | + # Ranger KMS |
| 136 | + URI_KMS_STATUS = "kms/api/status" |
| 137 | + |
| 138 | + |
| 139 | + # APIs |
| 140 | + CREATE_KEY = API(URI_KEYS, HttpMethod.POST, HTTPStatus.CREATED) |
| 141 | + ROLLOVER_KEY = API(URI_KEY_BY_NAME, HttpMethod.POST, HTTPStatus.OK) |
| 142 | + INVALIDATE_CACHE_FOR_KEY = API(URI_KEY_INVALIDATE_CACHE, HttpMethod.POST, HTTPStatus.OK) |
| 143 | + DELETE_KEY = API(URI_KEY_BY_NAME, HttpMethod.DELETE, HTTPStatus.OK) |
| 144 | + GET_KEY_METADATA = API(URI_KEY_METADATA, HttpMethod.GET, HTTPStatus.OK) |
| 145 | + GET_CURRENT_KEY = API(URI_CURRENT_KEY, HttpMethod.GET, HTTPStatus.OK) |
| 146 | + GENERATE_ENCRYPTED_KEY = API(URI_KEY_EEK, HttpMethod.GET, HTTPStatus.OK) |
| 147 | + DECRYPT_ENCRYPTED_KEY = API(URI_KEY_VERSION_BY_NAME_EEK, HttpMethod.POST, HTTPStatus.OK) |
| 148 | + REENCRYPT_ENCRYPTED_KEY = API(URI_KEY_VERSION_BY_NAME_EEK, HttpMethod.POST, HTTPStatus.OK) |
| 149 | + BATCH_REENCRYPT_ENCRYPTED_KEYS = API(URI_BATCH_REENCRYPT_KEYS, HttpMethod.POST, HTTPStatus.OK) |
| 150 | + GET_KEY_VERSION = API(URI_KEY_VERSION_BY_NAME, HttpMethod.GET, HTTPStatus.OK) |
| 151 | + GET_KEY_VERSIONS = API(URI_KEY_VERSIONS, HttpMethod.GET, HTTPStatus.OK) |
| 152 | + GET_KEYS_NAMES = API(URI_KEYS_NAMES, HttpMethod.GET, HTTPStatus.OK) |
| 153 | + GET_KEYS_METADATA = API(URI_KEYS_METADATA, HttpMethod.GET, HTTPStatus.OK) |
| 154 | + |
| 155 | + # Ranger KMS |
| 156 | + GET_KEY = API(URI_KEY_BY_NAME, HttpMethod.GET, HTTPStatus.OK) |
| 157 | + KMS_STATUS = API(URI_KMS_STATUS, HttpMethod.GET, HTTPStatus.OK) |
0 commit comments