use of javax.security.auth.login.Configuration in project calcite-avatica by apache.
the class KerberosConnectionTest method noPreviousContextOnLogin.
@Test
public void noPreviousContextOnLogin() throws Exception {
KerberosConnection krbUtil = mock(KerberosConnection.class);
Subject subject = new Subject();
Subject loggedInSubject = new Subject();
Configuration conf = mock(Configuration.class);
LoginContext context = mock(LoginContext.class);
// Call the real login(LoginContext, Configuration, Subject) method
when(krbUtil.login(nullable(LoginContext.class), any(Configuration.class), any(Subject.class))).thenCallRealMethod();
// Return a fake LoginContext
when(krbUtil.createLoginContext(conf)).thenReturn(context);
// Return a fake Subject from that fake LoginContext
when(context.getSubject()).thenReturn(loggedInSubject);
Entry<LoginContext, Subject> pair = krbUtil.login(null, conf, subject);
// Verify we get the fake LoginContext and Subject
assertEquals(context, pair.getKey());
assertEquals(loggedInSubject, pair.getValue());
// login should be called on the LoginContext
verify(context).login();
}
use of javax.security.auth.login.Configuration in project zm-mailbox by Zimbra.
the class Krb5Login method withPassword.
public static LoginContext withPassword(String name, final String password) throws LoginException {
Krb5Config kc = Krb5Config.getInstance();
kc.setPrincipal(name);
kc.setUseTicketCache(false);
kc.setStoreKey(false);
Configuration dc = new DynamicConfiguration(S_CONFIG_NAME, new AppConfigurationEntry[] { kc });
CallbackHandler handler = new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callback;
pc.setPassword(password.toCharArray());
}
}
}
};
return new LoginContext(S_CONFIG_NAME, null, handler, dc);
}
use of javax.security.auth.login.Configuration in project zm-mailbox by Zimbra.
the class Krb5Login method withKeyTab.
/**
* Constructs a new Krb5Config entry with the specified
* principal and keytab, logs in with that entry, and
* then removes that entry and returns the new LoginContext.
* <p>Equivalent to the following calls:
*<pre>
* Krb5Config kc = Krb5Config.getInstance();
* kc.setPrincipal(principal);
* kc.setKeyTab(keytab);
* kc.setStoreKey(true);
* LoginContext lc = Login.login(kc);
*</pre>
*/
public static LoginContext withKeyTab(String principal, String keytab) throws LoginException {
/*
* com.sun.security.auth.module.Krb5LoginModule required
* useKeyTab=true
* debug=true
* keyTab="/apps/workgroup-audit/keytab/keytab.workgroup-audit"
* doNotPrompt=true
* storeKey=true
* principal="service/workgroup-audit@stanford.edu"
* useTicketCache=true
*/
Krb5Config kc = Krb5Config.getInstance();
// kc.setDebug(true);
kc.setPrincipal(principal);
kc.setKeyTab(keytab);
kc.setStoreKey(true);
kc.setDoNotPrompt(true);
kc.setUseTicketCache(true);
Configuration dc = new DynamicConfiguration(S_CONFIG_NAME, new AppConfigurationEntry[] { kc });
return new LoginContext(S_CONFIG_NAME, null, null, dc);
}
use of javax.security.auth.login.Configuration in project apache-kafka-on-k8s by banzaicloud.
the class JaasContextTest method testMultipleLoginModules.
@Test
public void testMultipleLoginModules() throws Exception {
StringBuilder builder = new StringBuilder();
int moduleCount = 3;
Map<Integer, Map<String, Object>> moduleOptions = new HashMap<>();
for (int i = 0; i < moduleCount; i++) {
Map<String, Object> options = new HashMap<>();
options.put("index", "Index" + i);
options.put("module", "Module" + i);
moduleOptions.put(i, options);
String module = jaasConfigProp("test.Module" + i, LoginModuleControlFlag.REQUIRED, options);
builder.append(' ');
builder.append(module);
}
String jaasConfigProp = builder.toString();
String clientContextName = "CLIENT";
Configuration configuration = new JaasConfig(clientContextName, jaasConfigProp);
AppConfigurationEntry[] dynamicEntries = configuration.getAppConfigurationEntry(clientContextName);
assertEquals(moduleCount, dynamicEntries.length);
for (int i = 0; i < moduleCount; i++) {
AppConfigurationEntry entry = dynamicEntries[i];
checkEntry(entry, "test.Module" + i, LoginModuleControlFlag.REQUIRED, moduleOptions.get(i));
}
String serverContextName = "SERVER";
writeConfiguration(serverContextName, jaasConfigProp);
AppConfigurationEntry[] staticEntries = Configuration.getConfiguration().getAppConfigurationEntry(serverContextName);
for (int i = 0; i < moduleCount; i++) {
AppConfigurationEntry staticEntry = staticEntries[i];
checkEntry(staticEntry, dynamicEntries[i].getLoginModuleName(), LoginModuleControlFlag.REQUIRED, dynamicEntries[i].getOptions());
}
}
use of javax.security.auth.login.Configuration in project apache-kafka-on-k8s by banzaicloud.
the class JaasContext method defaultContext.
private static JaasContext defaultContext(JaasContext.Type contextType, String listenerContextName, String globalContextName) {
String jaasConfigFile = System.getProperty(JaasUtils.JAVA_LOGIN_CONFIG_PARAM);
if (jaasConfigFile == null) {
if (contextType == Type.CLIENT) {
LOG.debug("System property '" + JaasUtils.JAVA_LOGIN_CONFIG_PARAM + "' and Kafka SASL property '" + SaslConfigs.SASL_JAAS_CONFIG + "' are not set, using default JAAS configuration.");
} else {
LOG.debug("System property '" + JaasUtils.JAVA_LOGIN_CONFIG_PARAM + "' is not set, using default JAAS " + "configuration.");
}
}
Configuration jaasConfig = Configuration.getConfiguration();
AppConfigurationEntry[] configEntries = null;
String contextName = globalContextName;
if (listenerContextName != null) {
configEntries = jaasConfig.getAppConfigurationEntry(listenerContextName);
if (configEntries != null)
contextName = listenerContextName;
}
if (configEntries == null)
configEntries = jaasConfig.getAppConfigurationEntry(globalContextName);
if (configEntries == null) {
String listenerNameText = listenerContextName == null ? "" : " or '" + listenerContextName + "'";
String errorMessage = "Could not find a '" + globalContextName + "'" + listenerNameText + " entry in the JAAS " + "configuration. System property '" + JaasUtils.JAVA_LOGIN_CONFIG_PARAM + "' is " + (jaasConfigFile == null ? "not set" : jaasConfigFile);
throw new IllegalArgumentException(errorMessage);
}
return new JaasContext(contextName, contextType, jaasConfig, null);
}
Aggregations