ServiceApiUtil { public static void validateKerberosPrincipal( KerberosPrincipal kerberosPrincipal) throws IOException { if (!StringUtils.isEmpty(kerberosPrincipal.getPrincipalName())) { if (!kerberosPrincipal.getPrincipalName().contains("/")) { throw new IllegalArgumentException(String.format( RestApiErrorMessages.ERROR_KERBEROS_PRINCIPAL_NAME_FORMAT, kerberosPrincipal.getPrincipalName())); } } if (!StringUtils.isEmpty(kerberosPrincipal.getKeytab())) { try { URI keytabURI = new URI(kerberosPrincipal.getKeytab()); if (keytabURI.getScheme() == null) { throw new IllegalArgumentException(String.format( RestApiErrorMessages.ERROR_KEYTAB_URI_SCHEME_INVALID, kerberosPrincipal.getKeytab())); } } catch (URISyntaxException e) { throw new IllegalArgumentException( String.format(RestApiErrorMessages.ERROR_KEYTAB_URI_INVALID, e.getLocalizedMessage())); } } } static  String(String s); @VisibleForTesting static void setJsonSerDeser(JsonSerDeser jsd); @VisibleForTesting static void validateAndResolveService(Service service,
      SliderFileSystem fs, org.apache.hadoop.conf.Configuration conf); static void validateKerberosPrincipal(
      KerberosPrincipal kerberosPrincipal); static void validateNameFormat(String name,
      org.apache.hadoop.conf.Configuration conf); @VisibleForTesting static List<Component> getComponents(SliderFileSystem
      fs, String serviceName); static Service loadService(SliderFileSystem fs, String
      serviceName); static Service loadServiceUpgrade(SliderFileSystem fs,
      String serviceName, String version); static Service loadServiceFrom(SliderFileSystem fs,
      Path appDefPath); static Path getServiceJsonPath(SliderFileSystem fs, String serviceName); static void validateCompResourceSize(
      org.apache.hadoop.yarn.api.records.Resource maxResource,
      Service service); static Collection<Component> sortByDependencies(List<Component>
      components); static void createDirAndPersistApp(SliderFileSystem fs, Path appDir,
      Service service); static Path writeAppDefinition(SliderFileSystem fs, Path appDir,
      Service service); static List<Container> getLiveContainers(Service service,
      List<String> componentInstances); static void validateInstancesUpgrade(List<Container>
      liveContainers); static boolean isUpgradable(Container container); static List<Container> validateAndResolveCompsUpgrade(
      Service liveService, Collection<String> compNames); static List<Container> validateAndResolveCompsStable(
      Service liveService, Collection<String> compNames); static List<String> resolveCompsDependency(Service service); static void checkServiceDependencySatisified(Service service); static JsonSerDeser<Service> jsonSerDeser; static final JsonSerDeser<Container[]> CONTAINER_JSON_SERDE; static final JsonSerDeser<Component[]> COMP_JSON_SERDE; }
@Test public void testKerberosPrincipal() throws IOException { SliderFileSystem sfs = ServiceTestUtils.initMockFs(); Service app = createValidApplication("comp-a"); KerberosPrincipal kp = new KerberosPrincipal(); kp.setKeytab("/some/path"); kp.setPrincipalName("user/_HOST@domain.com"); app.setKerberosPrincipal(kp); try { ServiceApiUtil.validateKerberosPrincipal(app.getKerberosPrincipal()); Assert.fail(EXCEPTION_PREFIX + "service with invalid keytab URI scheme"); } catch (IllegalArgumentException e) { assertEquals( String.format(RestApiErrorMessages.ERROR_KEYTAB_URI_SCHEME_INVALID, kp.getKeytab()), e.getMessage()); } kp.setKeytab("/ blank / in / paths"); try { ServiceApiUtil.validateKerberosPrincipal(app.getKerberosPrincipal()); Assert.fail(EXCEPTION_PREFIX + "service with invalid keytab"); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains( RestApiErrorMessages.ERROR_KEYTAB_URI_INVALID.substring(0, RestApiErrorMessages.ERROR_KEYTAB_URI_INVALID.length() - 2))); } kp.setKeytab("file: try { ServiceApiUtil.validateKerberosPrincipal(app.getKerberosPrincipal()); } catch (IllegalArgumentException e) { Assert.fail(NO_EXCEPTION_PREFIX + e.getMessage()); } }
@Test public void testKerberosPrincipalNameFormat() throws IOException { Service app = createValidApplication("comp-a"); KerberosPrincipal kp = new KerberosPrincipal(); kp.setPrincipalName("user@domain.com"); app.setKerberosPrincipal(kp); try { ServiceApiUtil.validateKerberosPrincipal(app.getKerberosPrincipal()); Assert.fail(EXCEPTION_PREFIX + "service with invalid principal name " + "format."); } catch (IllegalArgumentException e) { assertEquals( String.format( RestApiErrorMessages.ERROR_KERBEROS_PRINCIPAL_NAME_FORMAT, kp.getPrincipalName()), e.getMessage()); } kp.setPrincipalName("user/_HOST@domain.com"); try { ServiceApiUtil.validateKerberosPrincipal(app.getKerberosPrincipal()); } catch (IllegalArgumentException e) { Assert.fail(NO_EXCEPTION_PREFIX + e.getMessage()); } kp.setPrincipalName(null); kp.setKeytab(null); try { ServiceApiUtil.validateKerberosPrincipal(app.getKerberosPrincipal()); } catch (NullPointerException e) { Assert.fail(NO_EXCEPTION_PREFIX + e.getMessage()); } }