KerberosPrincipalFactory { public static KerberosPrincipal create(FileSystemOperations fsOperations, RemoteDirectoryManager remoteDirectoryManager, RunJobParameters parameters) throws IOException { Objects.requireNonNull(fsOperations, "FileSystemOperations must not be null!"); Objects.requireNonNull(remoteDirectoryManager, "RemoteDirectoryManager must not be null!"); Objects.requireNonNull(parameters, "Parameters must not be null!"); if (StringUtils.isNotBlank(parameters.getKeytab()) && StringUtils .isNotBlank(parameters.getPrincipal())) { String keytab = parameters.getKeytab(); String principal = parameters.getPrincipal(); if (parameters.isDistributeKeytab()) { return handleDistributedKeytab(fsOperations, remoteDirectoryManager, parameters, keytab, principal); } else { return handleNormalKeytab(keytab, principal); } } LOG.debug("Principal and keytab was null or empty, " + "returning null KerberosPrincipal!"); return null; } private  KerberosPrincipalFactory(); static KerberosPrincipal create(FileSystemOperations fsOperations,
                                         RemoteDirectoryManager remoteDirectoryManager,
                                         RunJobParameters parameters);  }
@Test public void testCreatePrincipalEmptyPrincipalAndKeytab() throws IOException { MockClientContext mockClientContext = new MockClientContext(DEFAULT_JOB_NAME); RunJobParameters parameters = mock(RunJobParameters.class); when(parameters.getPrincipal()).thenReturn(""); when(parameters.getKeytab()).thenReturn(""); FileSystemOperations fsOperations = new FileSystemOperations(mockClientContext); KerberosPrincipal result = KerberosPrincipalFactory.create(fsOperations, mockClientContext.getRemoteDirectoryManager(), parameters); assertNull(result); }
@Test public void testCreatePrincipalEmptyPrincipalString() throws IOException { MockClientContext mockClientContext = new MockClientContext(DEFAULT_JOB_NAME); RunJobParameters parameters = mock(RunJobParameters.class); when(parameters.getPrincipal()).thenReturn(""); when(parameters.getKeytab()).thenReturn("keytab"); FileSystemOperations fsOperations = new FileSystemOperations(mockClientContext); KerberosPrincipal result = KerberosPrincipalFactory.create(fsOperations, mockClientContext.getRemoteDirectoryManager(), parameters); assertNull(result); }
@Test public void testCreatePrincipalEmptyKeyTabString() throws IOException { MockClientContext mockClientContext = new MockClientContext(DEFAULT_JOB_NAME); RunJobParameters parameters = mock(RunJobParameters.class); when(parameters.getPrincipal()).thenReturn("principal"); when(parameters.getKeytab()).thenReturn(""); FileSystemOperations fsOperations = new FileSystemOperations(mockClientContext); KerberosPrincipal result = KerberosPrincipalFactory.create(fsOperations, mockClientContext.getRemoteDirectoryManager(), parameters); assertNull(result); }
@Test public void testCreatePrincipalNonEmptyPrincipalAndKeytab() throws IOException { MockClientContext mockClientContext = new MockClientContext(DEFAULT_JOB_NAME); RunJobParameters parameters = mock(RunJobParameters.class); when(parameters.getPrincipal()).thenReturn("principal"); when(parameters.getKeytab()).thenReturn("keytab"); FileSystemOperations fsOperations = new FileSystemOperations(mockClientContext); KerberosPrincipal result = KerberosPrincipalFactory.create(fsOperations, mockClientContext.getRemoteDirectoryManager(), parameters); assertNotNull(result); assertEquals("file: assertEquals("principal", result.getPrincipalName()); }
@Test public void testCreatePrincipalDistributedKeytab() throws IOException { MockClientContext mockClientContext = new MockClientContext(DEFAULT_JOB_NAME); String jobname = "testJobname"; String keytab = "testKeytab"; File keytabFile = createKeytabFile(keytab); RunJobParameters parameters = mock(RunJobParameters.class); when(parameters.getPrincipal()).thenReturn("principal"); when(parameters.getKeytab()).thenReturn(keytabFile.getAbsolutePath()); when(parameters.getName()).thenReturn(jobname); when(parameters.isDistributeKeytab()).thenReturn(true); FileSystemOperations fsOperations = new FileSystemOperations(mockClientContext); KerberosPrincipal result = KerberosPrincipalFactory.create(fsOperations, mockClientContext.getRemoteDirectoryManager(), parameters); Path stagingDir = mockClientContext.getRemoteDirectoryManager() .getJobStagingArea(parameters.getName(), true); String expectedKeytabFilePath = FileUtilitiesForTests.getFilename(stagingDir, keytab).getAbsolutePath(); assertNotNull(result); assertEquals(expectedKeytabFilePath, result.getKeytab()); assertEquals("principal", result.getPrincipalName()); }