|
@@ -25,9 +25,10 @@ import org.apache.hadoop.crypto.key.KeyProviderCryptoExtension;
|
|
|
import org.apache.hadoop.crypto.key.KeyProviderCryptoExtension.EncryptedKeyVersion;
|
|
|
import org.apache.hadoop.crypto.key.kms.KMSRESTConstants;
|
|
|
import org.apache.hadoop.security.AccessControlException;
|
|
|
-import org.apache.hadoop.security.authentication.client.AuthenticationException;
|
|
|
+import org.apache.hadoop.security.UserGroupInformation;
|
|
|
import org.apache.hadoop.security.authorize.AuthorizationException;
|
|
|
import org.apache.hadoop.crypto.key.kms.KMSClientProvider;
|
|
|
+import org.apache.hadoop.security.token.delegation.web.HttpUserGroupInformation;
|
|
|
|
|
|
import javax.ws.rs.Consumes;
|
|
|
import javax.ws.rs.DELETE;
|
|
@@ -38,15 +39,13 @@ import javax.ws.rs.Path;
|
|
|
import javax.ws.rs.PathParam;
|
|
|
import javax.ws.rs.Produces;
|
|
|
import javax.ws.rs.QueryParam;
|
|
|
-import javax.ws.rs.core.Context;
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
import javax.ws.rs.core.Response;
|
|
|
-import javax.ws.rs.core.SecurityContext;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.net.URI;
|
|
|
import java.net.URISyntaxException;
|
|
|
-import java.security.Principal;
|
|
|
+import java.security.PrivilegedExceptionAction;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
@@ -74,15 +73,6 @@ public class KMS {
|
|
|
kmsAudit= KMSWebApp.getKMSAudit();
|
|
|
}
|
|
|
|
|
|
- private static Principal getPrincipal(SecurityContext securityContext)
|
|
|
- throws AuthenticationException{
|
|
|
- Principal user = securityContext.getUserPrincipal();
|
|
|
- if (user == null) {
|
|
|
- throw new AuthenticationException("User must be authenticated");
|
|
|
- }
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
private static final String UNAUTHORIZED_MSG_WITH_KEY =
|
|
|
"User:%s not allowed to do '%s' on '%s'";
|
|
@@ -90,20 +80,21 @@ public class KMS {
|
|
|
private static final String UNAUTHORIZED_MSG_WITHOUT_KEY =
|
|
|
"User:%s not allowed to do '%s'";
|
|
|
|
|
|
- private void assertAccess(KMSACLs.Type aclType, Principal principal,
|
|
|
+ private void assertAccess(KMSACLs.Type aclType, UserGroupInformation ugi,
|
|
|
KMSOp operation) throws AccessControlException {
|
|
|
- assertAccess(aclType, principal, operation, null);
|
|
|
+ assertAccess(aclType, ugi, operation, null);
|
|
|
}
|
|
|
|
|
|
- private void assertAccess(KMSACLs.Type aclType, Principal principal,
|
|
|
- KMSOp operation, String key) throws AccessControlException {
|
|
|
- if (!KMSWebApp.getACLs().hasAccess(aclType, principal.getName())) {
|
|
|
+ private void assertAccess(KMSACLs.Type aclType,
|
|
|
+ UserGroupInformation ugi, KMSOp operation, String key)
|
|
|
+ throws AccessControlException {
|
|
|
+ if (!KMSWebApp.getACLs().hasAccess(aclType, ugi)) {
|
|
|
KMSWebApp.getUnauthorizedCallsMeter().mark();
|
|
|
- kmsAudit.unauthorized(principal, operation, key);
|
|
|
+ kmsAudit.unauthorized(ugi, operation, key);
|
|
|
throw new AuthorizationException(String.format(
|
|
|
(key != null) ? UNAUTHORIZED_MSG_WITH_KEY
|
|
|
: UNAUTHORIZED_MSG_WITHOUT_KEY,
|
|
|
- principal.getName(), operation, key));
|
|
|
+ ugi.getShortUserName(), operation, key));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -123,15 +114,14 @@ public class KMS {
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
@SuppressWarnings("unchecked")
|
|
|
- public Response createKey(@Context SecurityContext securityContext,
|
|
|
- Map jsonKey) throws Exception {
|
|
|
+ public Response createKey(Map jsonKey) throws Exception {
|
|
|
KMSWebApp.getAdminCallsMeter().mark();
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
- String name = (String) jsonKey.get(KMSRESTConstants.NAME_FIELD);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
+ final String name = (String) jsonKey.get(KMSRESTConstants.NAME_FIELD);
|
|
|
KMSClientProvider.checkNotEmpty(name, KMSRESTConstants.NAME_FIELD);
|
|
|
assertAccess(KMSACLs.Type.CREATE, user, KMSOp.CREATE_KEY, name);
|
|
|
String cipher = (String) jsonKey.get(KMSRESTConstants.CIPHER_FIELD);
|
|
|
- String material = (String) jsonKey.get(KMSRESTConstants.MATERIAL_FIELD);
|
|
|
+ final String material = (String) jsonKey.get(KMSRESTConstants.MATERIAL_FIELD);
|
|
|
int length = (jsonKey.containsKey(KMSRESTConstants.LENGTH_FIELD))
|
|
|
? (Integer) jsonKey.get(KMSRESTConstants.LENGTH_FIELD) : 0;
|
|
|
String description = (String)
|
|
@@ -142,7 +132,7 @@ public class KMS {
|
|
|
assertAccess(KMSACLs.Type.SET_KEY_MATERIAL, user,
|
|
|
KMSOp.CREATE_KEY, name);
|
|
|
}
|
|
|
- KeyProvider.Options options = new KeyProvider.Options(
|
|
|
+ final KeyProvider.Options options = new KeyProvider.Options(
|
|
|
KMSWebApp.getConfiguration());
|
|
|
if (cipher != null) {
|
|
|
options.setCipher(cipher);
|
|
@@ -153,16 +143,23 @@ public class KMS {
|
|
|
options.setDescription(description);
|
|
|
options.setAttributes(attributes);
|
|
|
|
|
|
- KeyProvider.KeyVersion keyVersion = (material != null)
|
|
|
- ? provider.createKey(name, Base64.decodeBase64(material), options)
|
|
|
- : provider.createKey(name, options);
|
|
|
-
|
|
|
- provider.flush();
|
|
|
+ KeyProvider.KeyVersion keyVersion = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<KeyVersion>() {
|
|
|
+ @Override
|
|
|
+ public KeyVersion run() throws Exception {
|
|
|
+ KeyProvider.KeyVersion keyVersion = (material != null)
|
|
|
+ ? provider.createKey(name, Base64.decodeBase64(material), options)
|
|
|
+ : provider.createKey(name, options);
|
|
|
+ provider.flush();
|
|
|
+ return keyVersion;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
kmsAudit.ok(user, KMSOp.CREATE_KEY, name, "UserProvidedMaterial:" +
|
|
|
(material != null) + " Description:" + description);
|
|
|
|
|
|
- if (!KMSWebApp.getACLs().hasAccess(KMSACLs.Type.GET, user.getName())) {
|
|
|
+ if (!KMSWebApp.getACLs().hasAccess(KMSACLs.Type.GET, user)) {
|
|
|
keyVersion = removeKeyMaterial(keyVersion);
|
|
|
}
|
|
|
Map json = KMSServerJSONUtils.toJSON(keyVersion);
|
|
@@ -176,14 +173,21 @@ public class KMS {
|
|
|
|
|
|
@DELETE
|
|
|
@Path(KMSRESTConstants.KEY_RESOURCE + "/{name:.*}")
|
|
|
- public Response deleteKey(@Context SecurityContext securityContext,
|
|
|
- @PathParam("name") String name) throws Exception {
|
|
|
+ public Response deleteKey(@PathParam("name") final String name)
|
|
|
+ throws Exception {
|
|
|
KMSWebApp.getAdminCallsMeter().mark();
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
assertAccess(KMSACLs.Type.DELETE, user, KMSOp.DELETE_KEY, name);
|
|
|
KMSClientProvider.checkNotEmpty(name, "name");
|
|
|
- provider.deleteKey(name);
|
|
|
- provider.flush();
|
|
|
+
|
|
|
+ user.doAs(new PrivilegedExceptionAction<Void>() {
|
|
|
+ @Override
|
|
|
+ public Void run() throws Exception {
|
|
|
+ provider.deleteKey(name);
|
|
|
+ provider.flush();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
kmsAudit.ok(user, KMSOp.DELETE_KEY, name, "");
|
|
|
|
|
@@ -194,29 +198,36 @@ public class KMS {
|
|
|
@Path(KMSRESTConstants.KEY_RESOURCE + "/{name:.*}")
|
|
|
@Consumes(MediaType.APPLICATION_JSON)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
- public Response rolloverKey(@Context SecurityContext securityContext,
|
|
|
- @PathParam("name") String name, Map jsonMaterial)
|
|
|
- throws Exception {
|
|
|
+ public Response rolloverKey(@PathParam("name") final String name,
|
|
|
+ Map jsonMaterial) throws Exception {
|
|
|
KMSWebApp.getAdminCallsMeter().mark();
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
assertAccess(KMSACLs.Type.ROLLOVER, user, KMSOp.ROLL_NEW_VERSION, name);
|
|
|
KMSClientProvider.checkNotEmpty(name, "name");
|
|
|
- String material = (String)
|
|
|
+ final String material = (String)
|
|
|
jsonMaterial.get(KMSRESTConstants.MATERIAL_FIELD);
|
|
|
if (material != null) {
|
|
|
assertAccess(KMSACLs.Type.SET_KEY_MATERIAL, user,
|
|
|
KMSOp.ROLL_NEW_VERSION, name);
|
|
|
}
|
|
|
- KeyProvider.KeyVersion keyVersion = (material != null)
|
|
|
- ? provider.rollNewVersion(name, Base64.decodeBase64(material))
|
|
|
- : provider.rollNewVersion(name);
|
|
|
|
|
|
- provider.flush();
|
|
|
+ KeyProvider.KeyVersion keyVersion = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<KeyVersion>() {
|
|
|
+ @Override
|
|
|
+ public KeyVersion run() throws Exception {
|
|
|
+ KeyVersion keyVersion = (material != null)
|
|
|
+ ? provider.rollNewVersion(name, Base64.decodeBase64(material))
|
|
|
+ : provider.rollNewVersion(name);
|
|
|
+ provider.flush();
|
|
|
+ return keyVersion;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
kmsAudit.ok(user, KMSOp.ROLL_NEW_VERSION, name, "UserProvidedMaterial:" +
|
|
|
(material != null) + " NewVersion:" + keyVersion.getVersionName());
|
|
|
|
|
|
- if (!KMSWebApp.getACLs().hasAccess(KMSACLs.Type.GET, user.getName())) {
|
|
|
+ if (!KMSWebApp.getACLs().hasAccess(KMSACLs.Type.GET, user)) {
|
|
|
keyVersion = removeKeyMaterial(keyVersion);
|
|
|
}
|
|
|
Map json = KMSServerJSONUtils.toJSON(keyVersion);
|
|
@@ -226,14 +237,23 @@ public class KMS {
|
|
|
@GET
|
|
|
@Path(KMSRESTConstants.KEYS_METADATA_RESOURCE)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
- public Response getKeysMetadata(@Context SecurityContext securityContext,
|
|
|
- @QueryParam(KMSRESTConstants.KEY) List<String> keyNamesList)
|
|
|
- throws Exception {
|
|
|
+ public Response getKeysMetadata(@QueryParam(KMSRESTConstants.KEY)
|
|
|
+ List<String> keyNamesList) throws Exception {
|
|
|
KMSWebApp.getAdminCallsMeter().mark();
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
- String[] keyNames = keyNamesList.toArray(new String[keyNamesList.size()]);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
+ final String[] keyNames = keyNamesList.toArray(
|
|
|
+ new String[keyNamesList.size()]);
|
|
|
assertAccess(KMSACLs.Type.GET_METADATA, user, KMSOp.GET_KEYS_METADATA);
|
|
|
- KeyProvider.Metadata[] keysMeta = provider.getKeysMetadata(keyNames);
|
|
|
+
|
|
|
+ KeyProvider.Metadata[] keysMeta = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<KeyProvider.Metadata[]>() {
|
|
|
+ @Override
|
|
|
+ public KeyProvider.Metadata[] run() throws Exception {
|
|
|
+ return provider.getKeysMetadata(keyNames);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
Object json = KMSServerJSONUtils.toJSON(keyNames, keysMeta);
|
|
|
kmsAudit.ok(user, KMSOp.GET_KEYS_METADATA, "");
|
|
|
return Response.ok().type(MediaType.APPLICATION_JSON).entity(json).build();
|
|
@@ -242,36 +262,52 @@ public class KMS {
|
|
|
@GET
|
|
|
@Path(KMSRESTConstants.KEYS_NAMES_RESOURCE)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
- public Response getKeyNames(@Context SecurityContext securityContext)
|
|
|
- throws Exception {
|
|
|
+ public Response getKeyNames() throws Exception {
|
|
|
KMSWebApp.getAdminCallsMeter().mark();
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
assertAccess(KMSACLs.Type.GET_KEYS, user, KMSOp.GET_KEYS);
|
|
|
- Object json = provider.getKeys();
|
|
|
+
|
|
|
+ List<String> json = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<List<String>>() {
|
|
|
+ @Override
|
|
|
+ public List<String> run() throws Exception {
|
|
|
+ return provider.getKeys();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
kmsAudit.ok(user, KMSOp.GET_KEYS, "");
|
|
|
return Response.ok().type(MediaType.APPLICATION_JSON).entity(json).build();
|
|
|
}
|
|
|
|
|
|
@GET
|
|
|
@Path(KMSRESTConstants.KEY_RESOURCE + "/{name:.*}")
|
|
|
- public Response getKey(@Context SecurityContext securityContext,
|
|
|
- @PathParam("name") String name)
|
|
|
+ public Response getKey(@PathParam("name") String name)
|
|
|
throws Exception {
|
|
|
- return getMetadata(securityContext, name);
|
|
|
+ return getMetadata(name);
|
|
|
}
|
|
|
|
|
|
@GET
|
|
|
@Path(KMSRESTConstants.KEY_RESOURCE + "/{name:.*}/" +
|
|
|
KMSRESTConstants.METADATA_SUB_RESOURCE)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
- public Response getMetadata(@Context SecurityContext securityContext,
|
|
|
- @PathParam("name") String name)
|
|
|
+ public Response getMetadata(@PathParam("name") final String name)
|
|
|
throws Exception {
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
KMSClientProvider.checkNotEmpty(name, "name");
|
|
|
KMSWebApp.getAdminCallsMeter().mark();
|
|
|
assertAccess(KMSACLs.Type.GET_METADATA, user, KMSOp.GET_METADATA, name);
|
|
|
- Object json = KMSServerJSONUtils.toJSON(name, provider.getMetadata(name));
|
|
|
+
|
|
|
+ KeyProvider.Metadata metadata = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<KeyProvider.Metadata>() {
|
|
|
+ @Override
|
|
|
+ public KeyProvider.Metadata run() throws Exception {
|
|
|
+ return provider.getMetadata(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ Object json = KMSServerJSONUtils.toJSON(name, metadata);
|
|
|
kmsAudit.ok(user, KMSOp.GET_METADATA, name, "");
|
|
|
return Response.ok().type(MediaType.APPLICATION_JSON).entity(json).build();
|
|
|
}
|
|
@@ -280,14 +316,23 @@ public class KMS {
|
|
|
@Path(KMSRESTConstants.KEY_RESOURCE + "/{name:.*}/" +
|
|
|
KMSRESTConstants.CURRENT_VERSION_SUB_RESOURCE)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
- public Response getCurrentVersion(@Context SecurityContext securityContext,
|
|
|
- @PathParam("name") String name)
|
|
|
+ public Response getCurrentVersion(@PathParam("name") final String name)
|
|
|
throws Exception {
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
KMSClientProvider.checkNotEmpty(name, "name");
|
|
|
KMSWebApp.getKeyCallsMeter().mark();
|
|
|
assertAccess(KMSACLs.Type.GET, user, KMSOp.GET_CURRENT_KEY, name);
|
|
|
- Object json = KMSServerJSONUtils.toJSON(provider.getCurrentKey(name));
|
|
|
+
|
|
|
+ KeyVersion keyVersion = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<KeyVersion>() {
|
|
|
+ @Override
|
|
|
+ public KeyVersion run() throws Exception {
|
|
|
+ return provider.getCurrentKey(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ Object json = KMSServerJSONUtils.toJSON(keyVersion);
|
|
|
kmsAudit.ok(user, KMSOp.GET_CURRENT_KEY, name, "");
|
|
|
return Response.ok().type(MediaType.APPLICATION_JSON).entity(json).build();
|
|
|
}
|
|
@@ -295,14 +340,22 @@ public class KMS {
|
|
|
@GET
|
|
|
@Path(KMSRESTConstants.KEY_VERSION_RESOURCE + "/{versionName:.*}")
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
- public Response getKeyVersion(@Context SecurityContext securityContext,
|
|
|
- @PathParam("versionName") String versionName)
|
|
|
- throws Exception {
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ public Response getKeyVersion(
|
|
|
+ @PathParam("versionName") final String versionName) throws Exception {
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
KMSClientProvider.checkNotEmpty(versionName, "versionName");
|
|
|
KMSWebApp.getKeyCallsMeter().mark();
|
|
|
- KeyVersion keyVersion = provider.getKeyVersion(versionName);
|
|
|
assertAccess(KMSACLs.Type.GET, user, KMSOp.GET_KEY_VERSION);
|
|
|
+
|
|
|
+ KeyVersion keyVersion = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<KeyVersion>() {
|
|
|
+ @Override
|
|
|
+ public KeyVersion run() throws Exception {
|
|
|
+ return provider.getKeyVersion(versionName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
if (keyVersion != null) {
|
|
|
kmsAudit.ok(user, KMSOp.GET_KEY_VERSION, keyVersion.getName(), "");
|
|
|
}
|
|
@@ -316,13 +369,12 @@ public class KMS {
|
|
|
KMSRESTConstants.EEK_SUB_RESOURCE)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
public Response generateEncryptedKeys(
|
|
|
- @Context SecurityContext securityContext,
|
|
|
- @PathParam("name") String name,
|
|
|
+ @PathParam("name") final String name,
|
|
|
@QueryParam(KMSRESTConstants.EEK_OP) String edekOp,
|
|
|
@DefaultValue("1")
|
|
|
- @QueryParam(KMSRESTConstants.EEK_NUM_KEYS) int numKeys)
|
|
|
+ @QueryParam(KMSRESTConstants.EEK_NUM_KEYS) final int numKeys)
|
|
|
throws Exception {
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
KMSClientProvider.checkNotEmpty(name, "name");
|
|
|
KMSClientProvider.checkNotNull(edekOp, "eekOp");
|
|
|
|
|
@@ -330,12 +382,22 @@ public class KMS {
|
|
|
if (edekOp.equals(KMSRESTConstants.EEK_GENERATE)) {
|
|
|
assertAccess(KMSACLs.Type.GENERATE_EEK, user, KMSOp.GENERATE_EEK, name);
|
|
|
|
|
|
- List<EncryptedKeyVersion> retEdeks =
|
|
|
+ final List<EncryptedKeyVersion> retEdeks =
|
|
|
new LinkedList<EncryptedKeyVersion>();
|
|
|
try {
|
|
|
- for (int i = 0; i < numKeys; i ++) {
|
|
|
- retEdeks.add(provider.generateEncryptedKey(name));
|
|
|
- }
|
|
|
+
|
|
|
+ user.doAs(
|
|
|
+ new PrivilegedExceptionAction<Void>() {
|
|
|
+ @Override
|
|
|
+ public Void run() throws Exception {
|
|
|
+ for (int i = 0; i < numKeys; i++) {
|
|
|
+ retEdeks.add(provider.generateEncryptedKey(name));
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
|
throw new IOException(e);
|
|
|
}
|
|
@@ -359,16 +421,17 @@ public class KMS {
|
|
|
@Path(KMSRESTConstants.KEY_VERSION_RESOURCE + "/{versionName:.*}/" +
|
|
|
KMSRESTConstants.EEK_SUB_RESOURCE)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
- public Response decryptEncryptedKey(@Context SecurityContext securityContext,
|
|
|
- @PathParam("versionName") String versionName,
|
|
|
+ public Response decryptEncryptedKey(
|
|
|
+ @PathParam("versionName") final String versionName,
|
|
|
@QueryParam(KMSRESTConstants.EEK_OP) String eekOp,
|
|
|
Map jsonPayload)
|
|
|
throws Exception {
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
KMSClientProvider.checkNotEmpty(versionName, "versionName");
|
|
|
KMSClientProvider.checkNotNull(eekOp, "eekOp");
|
|
|
|
|
|
- String keyName = (String) jsonPayload.get(KMSRESTConstants.NAME_FIELD);
|
|
|
+ final String keyName = (String) jsonPayload.get(
|
|
|
+ KMSRESTConstants.NAME_FIELD);
|
|
|
String ivStr = (String) jsonPayload.get(KMSRESTConstants.IV_FIELD);
|
|
|
String encMaterialStr =
|
|
|
(String) jsonPayload.get(KMSRESTConstants.MATERIAL_FIELD);
|
|
@@ -376,14 +439,24 @@ public class KMS {
|
|
|
if (eekOp.equals(KMSRESTConstants.EEK_DECRYPT)) {
|
|
|
assertAccess(KMSACLs.Type.DECRYPT_EEK, user, KMSOp.DECRYPT_EEK, keyName);
|
|
|
KMSClientProvider.checkNotNull(ivStr, KMSRESTConstants.IV_FIELD);
|
|
|
- byte[] iv = Base64.decodeBase64(ivStr);
|
|
|
+ final byte[] iv = Base64.decodeBase64(ivStr);
|
|
|
KMSClientProvider.checkNotNull(encMaterialStr,
|
|
|
KMSRESTConstants.MATERIAL_FIELD);
|
|
|
- byte[] encMaterial = Base64.decodeBase64(encMaterialStr);
|
|
|
- KeyProvider.KeyVersion retKeyVersion =
|
|
|
- provider.decryptEncryptedKey(
|
|
|
- new KMSClientProvider.KMSEncryptedKeyVersion(keyName, versionName,
|
|
|
- iv, KeyProviderCryptoExtension.EEK, encMaterial));
|
|
|
+ final byte[] encMaterial = Base64.decodeBase64(encMaterialStr);
|
|
|
+
|
|
|
+ KeyProvider.KeyVersion retKeyVersion = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<KeyVersion>() {
|
|
|
+ @Override
|
|
|
+ public KeyVersion run() throws Exception {
|
|
|
+ return provider.decryptEncryptedKey(
|
|
|
+ new KMSClientProvider.KMSEncryptedKeyVersion(keyName,
|
|
|
+ versionName, iv, KeyProviderCryptoExtension.EEK,
|
|
|
+ encMaterial)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
retJSON = KMSServerJSONUtils.toJSON(retKeyVersion);
|
|
|
kmsAudit.ok(user, KMSOp.DECRYPT_EEK, keyName, "");
|
|
|
} else {
|
|
@@ -400,14 +473,23 @@ public class KMS {
|
|
|
@Path(KMSRESTConstants.KEY_RESOURCE + "/{name:.*}/" +
|
|
|
KMSRESTConstants.VERSIONS_SUB_RESOURCE)
|
|
|
@Produces(MediaType.APPLICATION_JSON)
|
|
|
- public Response getKeyVersions(@Context SecurityContext securityContext,
|
|
|
- @PathParam("name") String name)
|
|
|
+ public Response getKeyVersions(@PathParam("name") final String name)
|
|
|
throws Exception {
|
|
|
- Principal user = getPrincipal(securityContext);
|
|
|
+ UserGroupInformation user = HttpUserGroupInformation.get();
|
|
|
KMSClientProvider.checkNotEmpty(name, "name");
|
|
|
KMSWebApp.getKeyCallsMeter().mark();
|
|
|
assertAccess(KMSACLs.Type.GET, user, KMSOp.GET_KEY_VERSIONS, name);
|
|
|
- Object json = KMSServerJSONUtils.toJSON(provider.getKeyVersions(name));
|
|
|
+
|
|
|
+ List<KeyVersion> ret = user.doAs(
|
|
|
+ new PrivilegedExceptionAction<List<KeyVersion>>() {
|
|
|
+ @Override
|
|
|
+ public List<KeyVersion> run() throws Exception {
|
|
|
+ return provider.getKeyVersions(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
+
|
|
|
+ Object json = KMSServerJSONUtils.toJSON(ret);
|
|
|
kmsAudit.ok(user, KMSOp.GET_KEY_VERSIONS, name, "");
|
|
|
return Response.ok().type(MediaType.APPLICATION_JSON).entity(json).build();
|
|
|
}
|