|
@@ -18,7 +18,6 @@
|
|
|
|
|
|
package org.apache.ambari.server.serveraction.kerberos;
|
|
|
|
|
|
-import org.apache.ambari.server.AmbariException;
|
|
|
import org.apache.ambari.server.utils.ShellCommandUtil;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -50,14 +49,16 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler {
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public void open(KerberosCredential administratorCredentials, String defaultRealm) throws AmbariException {
|
|
|
+ public void open(KerberosCredential administratorCredentials, String defaultRealm) throws KerberosOperationException {
|
|
|
setAdministratorCredentials(administratorCredentials);
|
|
|
setDefaultRealm(defaultRealm);
|
|
|
+ setOpen(true);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void close() throws AmbariException {
|
|
|
+ public void close() throws KerberosOperationException {
|
|
|
// There is nothing to do here.
|
|
|
+ setOpen(false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -68,11 +69,18 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler {
|
|
|
*
|
|
|
* @param principal a String containing the principal to test
|
|
|
* @return true if the principal exists; false otherwise
|
|
|
- * @throws AmbariException
|
|
|
+ * @throws KerberosKDCConnectionException if a connection to the KDC cannot be made
|
|
|
+ * @throws KerberosAdminAuthenticationException if the administrator credentials fail to authenticate
|
|
|
+ * @throws KerberosRealmException if the realm does not map to a KDC
|
|
|
+ * @throws KerberosOperationException if an unexpected error occurred
|
|
|
*/
|
|
|
@Override
|
|
|
public boolean principalExists(String principal)
|
|
|
- throws AmbariException {
|
|
|
+ throws KerberosOperationException {
|
|
|
+
|
|
|
+ if (!isOpen()) {
|
|
|
+ throw new KerberosOperationException("This operation handler has not be opened");
|
|
|
+ }
|
|
|
|
|
|
if (principal == null) {
|
|
|
return false;
|
|
@@ -80,30 +88,18 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler {
|
|
|
// Create the KAdmin query to execute:
|
|
|
String query = String.format("get_principal %s", principal);
|
|
|
|
|
|
+ ShellCommandUtil.Result result;
|
|
|
try {
|
|
|
- ShellCommandUtil.Result result = invokeKAdmin(query);
|
|
|
-
|
|
|
- if (result != null) {
|
|
|
- if (result.isSuccessful()) {
|
|
|
- String stdOut = result.getStdout();
|
|
|
-
|
|
|
- // If there is data from STDOUT, see if the following string exists:
|
|
|
- // Principal: <principal>
|
|
|
- return (stdOut != null) && stdOut.contains(String.format("Principal: %s", principal));
|
|
|
- } else {
|
|
|
- LOG.warn("Failed to query for principal {}:\n\tExitCode: {}\n\tSTDOUT: {}\n\tSTDERR: {}",
|
|
|
- principal, result.getExitCode(), result.getStdout(), result.getStderr());
|
|
|
- throw new AmbariException(String.format("Failed to query for principal %s", principal));
|
|
|
- }
|
|
|
- } else {
|
|
|
- String message = String.format("Failed to query for principal %s - Unknown reason", principal);
|
|
|
- LOG.warn(message);
|
|
|
- throw new AmbariException(message);
|
|
|
- }
|
|
|
- } catch (AmbariException e) {
|
|
|
+ result = invokeKAdmin(query);
|
|
|
+ } catch (KerberosOperationException e) {
|
|
|
LOG.error(String.format("Failed to query for principal %s", principal), e);
|
|
|
throw e;
|
|
|
}
|
|
|
+
|
|
|
+ // If there is data from STDOUT, see if the following string exists:
|
|
|
+ // Principal: <principal>
|
|
|
+ String stdOut = result.getStdout();
|
|
|
+ return (stdOut != null) && stdOut.contains(String.format("Principal: %s", principal));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -117,60 +113,41 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler {
|
|
|
* @param principal a String containing the principal add
|
|
|
* @param password a String containing the password to use when creating the principal
|
|
|
* @return an Integer declaring the generated key number
|
|
|
- * @throws AmbariException
|
|
|
+ * @throws KerberosKDCConnectionException if a connection to the KDC cannot be made
|
|
|
+ * @throws KerberosAdminAuthenticationException if the administrator credentials fail to authenticate
|
|
|
+ * @throws KerberosRealmException if the realm does not map to a KDC
|
|
|
+ * @throws KerberosOperationException if an unexpected error occurred
|
|
|
*/
|
|
|
@Override
|
|
|
public Integer createServicePrincipal(String principal, String password)
|
|
|
- throws AmbariException {
|
|
|
+ throws KerberosOperationException {
|
|
|
+
|
|
|
+ if (!isOpen()) {
|
|
|
+ throw new KerberosOperationException("This operation handler has not be opened");
|
|
|
+ }
|
|
|
|
|
|
if ((principal == null) || principal.isEmpty()) {
|
|
|
- throw new AmbariException("Failed to create new principal - no principal specified");
|
|
|
+ throw new KerberosOperationException("Failed to create new principal - no principal specified");
|
|
|
+ } else if ((password == null) || password.isEmpty()) {
|
|
|
+ throw new KerberosOperationException("Failed to create new principal - no password specified");
|
|
|
} else {
|
|
|
// Create the kdamin query: add_principal <-randkey|-pw <password>> <principal>
|
|
|
- StringBuilder queryBuilder = new StringBuilder();
|
|
|
-
|
|
|
- queryBuilder.append("add_principal");
|
|
|
-
|
|
|
- // If a password was not supplied, have the KDC generate a random key, else use the supplied
|
|
|
- // password
|
|
|
- if ((password == null) || password.isEmpty()) {
|
|
|
- queryBuilder.append(" -randkey");
|
|
|
- } else {
|
|
|
- queryBuilder.append(" -pw ");
|
|
|
- queryBuilder.append(password);
|
|
|
- }
|
|
|
-
|
|
|
- queryBuilder.append(" ");
|
|
|
- queryBuilder.append(principal);
|
|
|
-
|
|
|
+ ShellCommandUtil.Result result;
|
|
|
try {
|
|
|
- ShellCommandUtil.Result result = invokeKAdmin(queryBuilder.toString());
|
|
|
-
|
|
|
- if (result != null) {
|
|
|
- if (result.isSuccessful()) {
|
|
|
- String stdOut = result.getStdout();
|
|
|
-
|
|
|
- // If there is data from STDOUT, see if the following string exists:
|
|
|
- // Principal "<principal>" created
|
|
|
- if ((stdOut != null) && stdOut.contains(String.format("Principal \"%s\" created", principal))) {
|
|
|
- return getKeyNumber(principal);
|
|
|
- } else {
|
|
|
- throw new AmbariException(String.format("Failed to create service principal for %s", principal));
|
|
|
- }
|
|
|
- } else {
|
|
|
- LOG.warn("Failed to create service principal for {}:\n\tExitCode: {}\n\tSTDOUT: {}\n\tSTDERR: {}",
|
|
|
- principal, result.getExitCode(), result.getStdout(), result.getStderr());
|
|
|
- throw new AmbariException(String.format("Failed to create service principal for %s", principal));
|
|
|
- }
|
|
|
- } else {
|
|
|
- String message = String.format("Failed to create service principal for %s - Unknown reason", principal);
|
|
|
- LOG.warn(message);
|
|
|
- throw new AmbariException(message);
|
|
|
- }
|
|
|
- } catch (AmbariException e) {
|
|
|
+ result = invokeKAdmin(String.format("add_principal -pw %s %s", password, principal));
|
|
|
+ } catch (KerberosOperationException e) {
|
|
|
LOG.error(String.format("Failed to create new principal for %s", principal), e);
|
|
|
throw e;
|
|
|
}
|
|
|
+
|
|
|
+ // If there is data from STDOUT, see if the following string exists:
|
|
|
+ // Principal "<principal>" created
|
|
|
+ String stdOut = result.getStdout();
|
|
|
+ if ((stdOut != null) && stdOut.contains(String.format("Principal \"%s\" created", principal))) {
|
|
|
+ return getKeyNumber(principal);
|
|
|
+ } else {
|
|
|
+ throw new KerberosOperationException(String.format("Failed to create service principal for %s", principal));
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -183,50 +160,31 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler {
|
|
|
* @param principal a String containing the principal to update
|
|
|
* @param password a String containing the password to set
|
|
|
* @return an Integer declaring the new key number
|
|
|
- * @throws AmbariException
|
|
|
+ * @throws KerberosKDCConnectionException if a connection to the KDC cannot be made
|
|
|
+ * @throws KerberosAdminAuthenticationException if the administrator credentials fail to authenticate
|
|
|
+ * @throws KerberosRealmException if the realm does not map to a KDC
|
|
|
+ * @throws KerberosOperationException if an unexpected error occurred
|
|
|
*/
|
|
|
@Override
|
|
|
- public Integer setPrincipalPassword(String principal, String password) throws AmbariException {
|
|
|
+ public Integer setPrincipalPassword(String principal, String password) throws KerberosOperationException {
|
|
|
+ if (!isOpen()) {
|
|
|
+ throw new KerberosOperationException("This operation handler has not be opened");
|
|
|
+ }
|
|
|
+
|
|
|
if ((principal == null) || principal.isEmpty()) {
|
|
|
- throw new AmbariException("Failed to set password - no principal specified");
|
|
|
+ throw new KerberosOperationException("Failed to set password - no principal specified");
|
|
|
+ } else if ((password == null) || password.isEmpty()) {
|
|
|
+ throw new KerberosOperationException("Failed to set password - no password specified");
|
|
|
} else {
|
|
|
// Create the kdamin query: change_password <-randkey|-pw <password>> <principal>
|
|
|
- StringBuilder queryBuilder = new StringBuilder();
|
|
|
-
|
|
|
- queryBuilder.append("change_password");
|
|
|
-
|
|
|
- // If a password was not supplied, have the KDC generate a random key, else use the supplied
|
|
|
- // password
|
|
|
- if ((password == null) || password.isEmpty()) {
|
|
|
- queryBuilder.append(" -randkey");
|
|
|
- } else {
|
|
|
- queryBuilder.append(" -pw ");
|
|
|
- queryBuilder.append(password);
|
|
|
- }
|
|
|
-
|
|
|
- queryBuilder.append(" ");
|
|
|
- queryBuilder.append(principal);
|
|
|
-
|
|
|
try {
|
|
|
- ShellCommandUtil.Result result = invokeKAdmin(queryBuilder.toString());
|
|
|
-
|
|
|
- if (result != null) {
|
|
|
- if (result.isSuccessful()) {
|
|
|
- return getKeyNumber(principal);
|
|
|
- } else {
|
|
|
- LOG.warn("Failed to set password for {}:\n\tExitCode: {}\n\tSTDOUT: {}\n\tSTDERR: {}",
|
|
|
- principal, result.getExitCode(), result.getStdout(), result.getStderr());
|
|
|
- throw new AmbariException(String.format("Failed to update password for %s", principal));
|
|
|
- }
|
|
|
- } else {
|
|
|
- String message = String.format("Failed to set password for %s - Unknown reason", principal);
|
|
|
- LOG.warn(message);
|
|
|
- throw new AmbariException(message);
|
|
|
- }
|
|
|
- } catch (AmbariException e) {
|
|
|
+ invokeKAdmin(String.format("change_password -pw %s %s", password, principal));
|
|
|
+ } catch (KerberosOperationException e) {
|
|
|
LOG.error(String.format("Failed to set password for %s", principal), e);
|
|
|
throw e;
|
|
|
}
|
|
|
+
|
|
|
+ return getKeyNumber(principal);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -237,37 +195,33 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler {
|
|
|
*
|
|
|
* @param principal a String containing the principal to remove
|
|
|
* @return true if the principal was successfully removed; otherwise false
|
|
|
- * @throws AmbariException
|
|
|
+ * @throws KerberosKDCConnectionException if a connection to the KDC cannot be made
|
|
|
+ * @throws KerberosAdminAuthenticationException if the administrator credentials fail to authenticate
|
|
|
+ * @throws KerberosRealmException if the realm does not map to a KDC
|
|
|
+ * @throws KerberosOperationException if an unexpected error occurred
|
|
|
*/
|
|
|
@Override
|
|
|
- public boolean removeServicePrincipal(String principal) throws AmbariException {
|
|
|
+ public boolean removeServicePrincipal(String principal) throws KerberosOperationException {
|
|
|
+ if (!isOpen()) {
|
|
|
+ throw new KerberosOperationException("This operation handler has not be opened");
|
|
|
+ }
|
|
|
+
|
|
|
if ((principal == null) || principal.isEmpty()) {
|
|
|
- throw new AmbariException("Failed to remove new principal - no principal specified");
|
|
|
+ throw new KerberosOperationException("Failed to remove new principal - no principal specified");
|
|
|
} else {
|
|
|
+ ShellCommandUtil.Result result;
|
|
|
+
|
|
|
try {
|
|
|
- ShellCommandUtil.Result result = invokeKAdmin(String.format("delete_principal -force %s", principal));
|
|
|
-
|
|
|
- if (result != null) {
|
|
|
- if (result.isSuccessful()) {
|
|
|
- String stdOut = result.getStdout();
|
|
|
-
|
|
|
- // If there is data from STDOUT, see if the following string exists:
|
|
|
- // Principal "<principal>" created
|
|
|
- return (stdOut != null) && !stdOut.contains("Principal does not exist");
|
|
|
- } else {
|
|
|
- LOG.warn("Failed to remove service principal for {}:\n\tExitCode: {}\n\tSTDOUT: {}\n\tSTDERR: {}",
|
|
|
- principal, result.getExitCode(), result.getStdout(), result.getStderr());
|
|
|
- throw new AmbariException(String.format("Failed to remove service principal for %s", principal));
|
|
|
- }
|
|
|
- } else {
|
|
|
- String message = String.format("Failed to remove service principal for %s - Unknown reason", principal);
|
|
|
- LOG.warn(message);
|
|
|
- throw new AmbariException(message);
|
|
|
- }
|
|
|
- } catch (AmbariException e) {
|
|
|
+ result = invokeKAdmin(String.format("delete_principal -force %s", principal));
|
|
|
+ } catch (KerberosOperationException e) {
|
|
|
LOG.error(String.format("Failed to remove new principal for %s", principal), e);
|
|
|
throw e;
|
|
|
}
|
|
|
+
|
|
|
+ // If there is data from STDOUT, see if the following string exists:
|
|
|
+ // Principal "<principal>" created
|
|
|
+ String stdOut = result.getStdout();
|
|
|
+ return (stdOut != null) && !stdOut.contains("Principal does not exist");
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -276,62 +230,59 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler {
|
|
|
*
|
|
|
* @param principal a String declaring the principal to look up
|
|
|
* @return an Integer declaring the current key number
|
|
|
- * @throws AmbariException if an error occurs while looking up the relevant key number
|
|
|
+ * @throws KerberosKDCConnectionException if a connection to the KDC cannot be made
|
|
|
+ * @throws KerberosAdminAuthenticationException if the administrator credentials fail to authenticate
|
|
|
+ * @throws KerberosRealmException if the realm does not map to a KDC
|
|
|
+ * @throws KerberosOperationException if an unexpected error occurred
|
|
|
*/
|
|
|
- private Integer getKeyNumber(String principal) throws AmbariException {
|
|
|
+ private Integer getKeyNumber(String principal) throws KerberosOperationException {
|
|
|
+ if (!isOpen()) {
|
|
|
+ throw new KerberosOperationException("This operation handler has not be opened");
|
|
|
+ }
|
|
|
+
|
|
|
if ((principal == null) || principal.isEmpty()) {
|
|
|
- throw new AmbariException("Failed to get key number for principal - no principal specified");
|
|
|
+ throw new KerberosOperationException("Failed to get key number for principal - no principal specified");
|
|
|
} else {
|
|
|
// Create the kdamin query: get_principal <principal>
|
|
|
String query = String.format("get_principal %s", principal);
|
|
|
|
|
|
+ ShellCommandUtil.Result result;
|
|
|
try {
|
|
|
- ShellCommandUtil.Result result = invokeKAdmin(query);
|
|
|
-
|
|
|
- if (result != null) {
|
|
|
- if (result.isSuccessful()) {
|
|
|
- String stdOut = result.getStdout();
|
|
|
-
|
|
|
- if (stdOut == null) {
|
|
|
- LOG.warn("Failed to get key number for {}:\n\tExitCode: {}\n\tSTDOUT: NULL\n\tSTDERR: {}",
|
|
|
- principal, result.getExitCode(), result.getStderr());
|
|
|
- throw new AmbariException(String.format("Failed to get key number for %s", principal));
|
|
|
- }
|
|
|
-
|
|
|
- Matcher matcher = PATTERN_GET_KEY_NUMBER.matcher(stdOut);
|
|
|
-
|
|
|
- if (matcher.matches()) {
|
|
|
- NumberFormat numberFormat = NumberFormat.getIntegerInstance();
|
|
|
- String keyNumber = matcher.group(1);
|
|
|
-
|
|
|
- numberFormat.setGroupingUsed(false);
|
|
|
- try {
|
|
|
- Number number = numberFormat.parse(keyNumber);
|
|
|
- return (number == null) ? 0 : number.intValue();
|
|
|
- } catch (ParseException e) {
|
|
|
- LOG.warn("Failed to get key number for {} - invalid key number value ({}):\n\tExitCode: {}\n\tSTDOUT: NULL\n\tSTDERR: {}",
|
|
|
- principal, keyNumber, result.getExitCode(), result.getStderr());
|
|
|
- throw new AmbariException(String.format("Failed to get key number for %s", principal));
|
|
|
- }
|
|
|
- } else {
|
|
|
- LOG.warn("Failed to get key number for {} - unexpected STDOUT data:\n\tExitCode: {}\n\tSTDOUT: NULL\n\tSTDERR: {}",
|
|
|
- principal, result.getExitCode(), result.getStderr());
|
|
|
- throw new AmbariException(String.format("Failed to get key number for %s", principal));
|
|
|
- }
|
|
|
- } else {
|
|
|
- LOG.warn("Failed to get key number for {}:\n\tExitCode: {}\n\tSTDOUT: {}\n\tSTDERR: {}",
|
|
|
- principal, result.getExitCode(), result.getStdout(), result.getStderr());
|
|
|
- throw new AmbariException(String.format("Failed to get key number for %s", principal));
|
|
|
- }
|
|
|
- } else {
|
|
|
- String message = String.format("Failed to get key number for %s - Unknown reason", principal);
|
|
|
- LOG.warn(message);
|
|
|
- throw new AmbariException(message);
|
|
|
- }
|
|
|
- } catch (AmbariException e) {
|
|
|
+ result = invokeKAdmin(query);
|
|
|
+ } catch (KerberosOperationException e) {
|
|
|
LOG.error(String.format("Failed to get key number for %s", principal), e);
|
|
|
throw e;
|
|
|
}
|
|
|
+
|
|
|
+ String stdOut = result.getStdout();
|
|
|
+ if (stdOut == null) {
|
|
|
+ String message = String.format("Failed to get key number for %s:\n\tExitCode: %s\n\tSTDOUT: NULL\n\tSTDERR: %s",
|
|
|
+ principal, result.getExitCode(), result.getStderr());
|
|
|
+ LOG.warn(message);
|
|
|
+ throw new KerberosOperationException(message);
|
|
|
+ }
|
|
|
+
|
|
|
+ Matcher matcher = PATTERN_GET_KEY_NUMBER.matcher(stdOut);
|
|
|
+ if (matcher.matches()) {
|
|
|
+ NumberFormat numberFormat = NumberFormat.getIntegerInstance();
|
|
|
+ String keyNumber = matcher.group(1);
|
|
|
+
|
|
|
+ numberFormat.setGroupingUsed(false);
|
|
|
+ try {
|
|
|
+ Number number = numberFormat.parse(keyNumber);
|
|
|
+ return (number == null) ? 0 : number.intValue();
|
|
|
+ } catch (ParseException e) {
|
|
|
+ String message = String.format("Failed to get key number for %s - invalid key number value (%s):\n\tExitCode: %s\n\tSTDOUT: NULL\n\tSTDERR: %s",
|
|
|
+ principal, keyNumber, result.getExitCode(), result.getStderr());
|
|
|
+ LOG.warn(message);
|
|
|
+ throw new KerberosOperationException(message);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ String message = String.format("Failed to get key number for %s - unexpected STDOUT data:\n\tExitCode: %s\n\tSTDOUT: NULL\n\tSTDERR: %s",
|
|
|
+ principal, result.getExitCode(), result.getStderr());
|
|
|
+ LOG.warn(message);
|
|
|
+ throw new KerberosOperationException(message);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -340,77 +291,104 @@ public class MITKerberosOperationHandler extends KerberosOperationHandler {
|
|
|
*
|
|
|
* @param query a String containing the query to send to the kdamin command
|
|
|
* @return a ShellCommandUtil.Result containing the result of the operation
|
|
|
- * @throws AmbariException
|
|
|
+ * @throws KerberosKDCConnectionException if a connection to the KDC cannot be made
|
|
|
+ * @throws KerberosAdminAuthenticationException if the administrator credentials fail to authenticate
|
|
|
+ * @throws KerberosRealmException if the realm does not map to a KDC
|
|
|
+ * @throws KerberosOperationException if an unexpected error occurred
|
|
|
*/
|
|
|
private ShellCommandUtil.Result invokeKAdmin(String query)
|
|
|
- throws AmbariException {
|
|
|
+ throws KerberosOperationException {
|
|
|
ShellCommandUtil.Result result = null;
|
|
|
|
|
|
- if ((query != null) && !query.isEmpty()) {
|
|
|
- KerberosCredential administratorCredentials = getAdministratorCredentials();
|
|
|
- String defaultRealm = getDefaultRealm();
|
|
|
+ if ((query == null) || query.isEmpty()) {
|
|
|
+ throw new KerberosOperationException("Missing kadmin query");
|
|
|
+ }
|
|
|
+ KerberosCredential administratorCredentials = getAdministratorCredentials();
|
|
|
+ String defaultRealm = getDefaultRealm();
|
|
|
|
|
|
- List<String> command = new ArrayList<String>();
|
|
|
- File tempKeytabFile = null;
|
|
|
+ List<String> command = new ArrayList<String>();
|
|
|
+ File tempKeytabFile = null;
|
|
|
|
|
|
- try {
|
|
|
- String adminPrincipal = (administratorCredentials == null)
|
|
|
- ? null
|
|
|
- : administratorCredentials.getPrincipal();
|
|
|
+ try {
|
|
|
+ String adminPrincipal = (administratorCredentials == null)
|
|
|
+ ? null
|
|
|
+ : administratorCredentials.getPrincipal();
|
|
|
|
|
|
- if ((adminPrincipal == null) || adminPrincipal.isEmpty()) {
|
|
|
- // Set the kdamin interface to be kadmin.local
|
|
|
- command.add("kadmin.local");
|
|
|
- } else {
|
|
|
- String adminPassword = administratorCredentials.getPassword();
|
|
|
- String adminKeyTab = administratorCredentials.getKeytab();
|
|
|
-
|
|
|
- // Set the kdamin interface to be kadmin
|
|
|
- command.add("kadmin");
|
|
|
-
|
|
|
- // Add the administrative principal
|
|
|
- command.add("-p");
|
|
|
- command.add(adminPrincipal);
|
|
|
-
|
|
|
- if ((adminKeyTab != null) && !adminKeyTab.isEmpty()) {
|
|
|
- tempKeytabFile = createKeytabFile(adminKeyTab);
|
|
|
-
|
|
|
- if (tempKeytabFile != null) {
|
|
|
- // Add keytab file administrative principal
|
|
|
- command.add("-k");
|
|
|
- command.add("-t");
|
|
|
- command.add(tempKeytabFile.getAbsolutePath());
|
|
|
- }
|
|
|
- } else if (adminPassword != null) {
|
|
|
- // Add password for administrative principal
|
|
|
- command.add("-w");
|
|
|
- command.add(adminPassword);
|
|
|
+ if ((adminPrincipal == null) || adminPrincipal.isEmpty()) {
|
|
|
+ // Set the kdamin interface to be kadmin.local
|
|
|
+ command.add("kadmin.local");
|
|
|
+ } else {
|
|
|
+ String adminPassword = administratorCredentials.getPassword();
|
|
|
+ String adminKeyTab = administratorCredentials.getKeytab();
|
|
|
+
|
|
|
+ // Set the kdamin interface to be kadmin
|
|
|
+ command.add("kadmin");
|
|
|
+
|
|
|
+ // Add the administrative principal
|
|
|
+ command.add("-p");
|
|
|
+ command.add(adminPrincipal);
|
|
|
+
|
|
|
+ if ((adminKeyTab != null) && !adminKeyTab.isEmpty()) {
|
|
|
+ tempKeytabFile = createKeytabFile(adminKeyTab);
|
|
|
+
|
|
|
+ if (tempKeytabFile != null) {
|
|
|
+ // Add keytab file administrative principal
|
|
|
+ command.add("-k");
|
|
|
+ command.add("-t");
|
|
|
+ command.add(tempKeytabFile.getAbsolutePath());
|
|
|
}
|
|
|
+ } else if (adminPassword != null) {
|
|
|
+ // Add password for administrative principal
|
|
|
+ command.add("-w");
|
|
|
+ command.add(adminPassword);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- if ((defaultRealm != null) && !defaultRealm.isEmpty()) {
|
|
|
- // Add default realm clause
|
|
|
- command.add("-r");
|
|
|
- command.add(defaultRealm);
|
|
|
- }
|
|
|
+ if ((defaultRealm != null) && !defaultRealm.isEmpty()) {
|
|
|
+ // Add default realm clause
|
|
|
+ command.add("-r");
|
|
|
+ command.add(defaultRealm);
|
|
|
+ }
|
|
|
|
|
|
- // Add kadmin query
|
|
|
- command.add("-q");
|
|
|
- command.add(query.replace("\"", "\\\""));
|
|
|
+ // Add kadmin query
|
|
|
+ command.add("-q");
|
|
|
+ command.add(query.replace("\"", "\\\""));
|
|
|
|
|
|
- result = executeCommand(command.toArray(new String[command.size()]));
|
|
|
- } finally {
|
|
|
- // If a temporary keytab file was created, clean it up.
|
|
|
- if (tempKeytabFile != null) {
|
|
|
- if (!tempKeytabFile.delete()) {
|
|
|
- tempKeytabFile.deleteOnExit();
|
|
|
- }
|
|
|
+ result = executeCommand(command.toArray(new String[command.size()]));
|
|
|
+
|
|
|
+ if (!result.isSuccessful()) {
|
|
|
+ String message = String.format("Failed to execute kadmin:\n\tExitCode: %s\n\tSTDOUT: %s\n\tSTDERR: %s",
|
|
|
+ result.getExitCode(), result.getStdout(), result.getStderr());
|
|
|
+ LOG.warn(message);
|
|
|
+
|
|
|
+ // Test STDERR to see of any "expected" error conditions were encountered...
|
|
|
+ String stdErr = result.getStderr();
|
|
|
+ // Did admin credentials fail?
|
|
|
+ if (stdErr.contains("Client not found in Kerberos database")) {
|
|
|
+ throw new KerberosAdminAuthenticationException(stdErr);
|
|
|
+ } else if (stdErr.contains("Incorrect password while initializing")) {
|
|
|
+ throw new KerberosAdminAuthenticationException(stdErr);
|
|
|
+ }
|
|
|
+ // Did we fail to connect to the KDC?
|
|
|
+ else if (stdErr.contains("Cannot contact any KDC")) {
|
|
|
+ throw new KerberosKDCConnectionException(stdErr);
|
|
|
+ }
|
|
|
+ // Was the realm invalid?
|
|
|
+ else if (stdErr.contains("Missing parameters in krb5.conf required for kadmin client")) {
|
|
|
+ throw new KerberosRealmException(stdErr);
|
|
|
+ } else {
|
|
|
+ throw new KerberosOperationException("Unexpected error condition executing the kadmin command");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ // If a temporary keytab file was created, clean it up.
|
|
|
+ if (tempKeytabFile != null) {
|
|
|
+ if (!tempKeytabFile.delete()) {
|
|
|
+ tempKeytabFile.deleteOnExit();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
}
|