use of org.apache.accumulo.core.client.ClientConfiguration in project accumulo by apache.
the class ClientContext method convertClientConfig.
/**
* A utility method for converting client configuration to a standard configuration object for use internally.
*
* @param config
* the original {@link ClientConfiguration}
* @return the client configuration presented in the form of an {@link AccumuloConfiguration}
*/
public static AccumuloConfiguration convertClientConfig(final ClientConfiguration config) {
final AccumuloConfiguration defaults = DefaultConfiguration.getInstance();
return new AccumuloConfiguration() {
@Override
public String get(Property property) {
final String key = property.getKey();
// Attempt to load sensitive properties from a CredentialProvider, if configured
if (property.isSensitive()) {
org.apache.hadoop.conf.Configuration hadoopConf = getHadoopConfiguration();
if (null != hadoopConf) {
try {
char[] value = CredentialProviderFactoryShim.getValueFromCredentialProvider(hadoopConf, key);
if (null != value) {
log.trace("Loaded sensitive value for {} from CredentialProvider", key);
return new String(value);
} else {
log.trace("Tried to load sensitive value for {} from CredentialProvider, but none was found", key);
}
} catch (IOException e) {
log.warn("Failed to extract sensitive property ({}) from Hadoop CredentialProvider, falling back to base AccumuloConfiguration", key, e);
}
}
}
if (config.containsKey(key))
return config.getString(key);
else {
// Reconstitute the server kerberos property from the client config
if (Property.GENERAL_KERBEROS_PRINCIPAL == property) {
if (config.containsKey(ClientConfiguration.ClientProperty.KERBEROS_SERVER_PRIMARY.getKey())) {
// Avoid providing a realm since we don't know what it is...
return config.getString(ClientConfiguration.ClientProperty.KERBEROS_SERVER_PRIMARY.getKey()) + "/_HOST@" + SaslConnectionParams.getDefaultRealm();
}
}
return defaults.get(property);
}
}
@Override
public void getProperties(Map<String, String> props, Predicate<String> filter) {
defaults.getProperties(props, filter);
Iterator<String> keyIter = config.getKeys();
while (keyIter.hasNext()) {
String key = keyIter.next().toString();
if (filter.test(key))
props.put(key, config.getString(key));
}
// Automatically reconstruct the server property when converting a client config.
if (props.containsKey(ClientConfiguration.ClientProperty.KERBEROS_SERVER_PRIMARY.getKey())) {
final String serverPrimary = props.remove(ClientConfiguration.ClientProperty.KERBEROS_SERVER_PRIMARY.getKey());
if (filter.test(Property.GENERAL_KERBEROS_PRINCIPAL.getKey())) {
// Use the _HOST expansion. It should be unnecessary in "client land".
props.put(Property.GENERAL_KERBEROS_PRINCIPAL.getKey(), serverPrimary + "/_HOST@" + SaslConnectionParams.getDefaultRealm());
}
}
// Attempt to load sensitive properties from a CredentialProvider, if configured
org.apache.hadoop.conf.Configuration hadoopConf = getHadoopConfiguration();
if (null != hadoopConf) {
try {
for (String key : CredentialProviderFactoryShim.getKeys(hadoopConf)) {
if (!Property.isValidPropertyKey(key) || !Property.isSensitive(key)) {
continue;
}
if (filter.test(key)) {
char[] value = CredentialProviderFactoryShim.getValueFromCredentialProvider(hadoopConf, key);
if (null != value) {
props.put(key, new String(value));
}
}
}
} catch (IOException e) {
log.warn("Failed to extract sensitive properties from Hadoop CredentialProvider, falling back to accumulo-site.xml", e);
}
}
}
private org.apache.hadoop.conf.Configuration getHadoopConfiguration() {
String credProviderPaths = config.getString(Property.GENERAL_SECURITY_CREDENTIAL_PROVIDER_PATHS.getKey());
if (null != credProviderPaths && !credProviderPaths.isEmpty()) {
org.apache.hadoop.conf.Configuration hadoopConf = new org.apache.hadoop.conf.Configuration();
hadoopConf.set(CredentialProviderFactoryShim.CREDENTIAL_PROVIDER_PATH, credProviderPaths);
return hadoopConf;
}
log.trace("Did not find credential provider configuration in ClientConfiguration");
return null;
}
};
}
use of org.apache.accumulo.core.client.ClientConfiguration in project accumulo by apache.
the class SaslConnectionParamsTest method testDefaultParamsAsClient.
@Test
public void testDefaultParamsAsClient() throws Exception {
final KerberosToken token = EasyMock.createMock(KerberosToken.class);
testUser.doAs(new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
final ClientConfiguration clientConf = ClientConfiguration.loadDefault();
// The primary is the first component of the principal
final String primary = "accumulo";
clientConf.withSasl(true, primary);
assertEquals("true", clientConf.get(ClientProperty.INSTANCE_RPC_SASL_ENABLED));
final SaslConnectionParams saslParams = new SaslConnectionParams(clientConf, token);
assertEquals(primary, saslParams.getKerberosServerPrimary());
final QualityOfProtection defaultQop = QualityOfProtection.get(Property.RPC_SASL_QOP.getDefaultValue());
assertEquals(defaultQop, saslParams.getQualityOfProtection());
Map<String, String> properties = saslParams.getSaslProperties();
assertEquals(1, properties.size());
assertEquals(defaultQop.getQuality(), properties.get(Sasl.QOP));
assertEquals(username, saslParams.getPrincipal());
return null;
}
});
}
use of org.apache.accumulo.core.client.ClientConfiguration in project accumulo by apache.
the class SaslConnectionParamsTest method testDelegationTokenImpl.
@Test
public void testDelegationTokenImpl() throws Exception {
final DelegationTokenImpl token = new DelegationTokenImpl(new byte[0], new AuthenticationTokenIdentifier("user", 1, 10l, 20l, "instanceid"));
testUser.doAs(new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
final ClientConfiguration clientConf = ClientConfiguration.loadDefault();
// The primary is the first component of the principal
final String primary = "accumulo";
clientConf.withSasl(true, primary);
final AccumuloConfiguration rpcConf = ClientContext.convertClientConfig(clientConf);
assertEquals("true", clientConf.get(ClientProperty.INSTANCE_RPC_SASL_ENABLED));
final SaslConnectionParams saslParams = new SaslConnectionParams(rpcConf, token);
assertEquals(primary, saslParams.getKerberosServerPrimary());
final QualityOfProtection defaultQop = QualityOfProtection.get(Property.RPC_SASL_QOP.getDefaultValue());
assertEquals(defaultQop, saslParams.getQualityOfProtection());
assertEquals(SaslMechanism.DIGEST_MD5, saslParams.getMechanism());
assertNotNull(saslParams.getCallbackHandler());
assertEquals(SaslClientDigestCallbackHandler.class, saslParams.getCallbackHandler().getClass());
Map<String, String> properties = saslParams.getSaslProperties();
assertEquals(1, properties.size());
assertEquals(defaultQop.getQuality(), properties.get(Sasl.QOP));
assertEquals(username, saslParams.getPrincipal());
return null;
}
});
}
use of org.apache.accumulo.core.client.ClientConfiguration in project accumulo by apache.
the class SaslConnectionParamsTest method testEquality.
@Test
public void testEquality() throws Exception {
final KerberosToken token = EasyMock.createMock(KerberosToken.class);
SaslConnectionParams params1 = testUser.doAs(new PrivilegedExceptionAction<SaslConnectionParams>() {
@Override
public SaslConnectionParams run() throws Exception {
final ClientConfiguration clientConf = ClientConfiguration.loadDefault();
// The primary is the first component of the principal
final String primary = "accumulo";
clientConf.withSasl(true, primary);
final AccumuloConfiguration rpcConf = ClientContext.convertClientConfig(clientConf);
assertEquals("true", clientConf.get(ClientProperty.INSTANCE_RPC_SASL_ENABLED));
return new SaslConnectionParams(rpcConf, token);
}
});
SaslConnectionParams params2 = testUser.doAs(new PrivilegedExceptionAction<SaslConnectionParams>() {
@Override
public SaslConnectionParams run() throws Exception {
final ClientConfiguration clientConf = ClientConfiguration.loadDefault();
// The primary is the first component of the principal
final String primary = "accumulo";
clientConf.withSasl(true, primary);
final AccumuloConfiguration rpcConf = ClientContext.convertClientConfig(clientConf);
assertEquals("true", clientConf.get(ClientProperty.INSTANCE_RPC_SASL_ENABLED));
return new SaslConnectionParams(rpcConf, token);
}
});
assertEquals(params1, params2);
assertEquals(params1.hashCode(), params2.hashCode());
final DelegationTokenImpl delToken1 = new DelegationTokenImpl(new byte[0], new AuthenticationTokenIdentifier("user", 1, 10l, 20l, "instanceid"));
SaslConnectionParams params3 = testUser.doAs(new PrivilegedExceptionAction<SaslConnectionParams>() {
@Override
public SaslConnectionParams run() throws Exception {
final ClientConfiguration clientConf = ClientConfiguration.loadDefault();
// The primary is the first component of the principal
final String primary = "accumulo";
clientConf.withSasl(true, primary);
final AccumuloConfiguration rpcConf = ClientContext.convertClientConfig(clientConf);
assertEquals("true", clientConf.get(ClientProperty.INSTANCE_RPC_SASL_ENABLED));
return new SaslConnectionParams(rpcConf, delToken1);
}
});
assertNotEquals(params1, params3);
assertNotEquals(params1.hashCode(), params3.hashCode());
assertNotEquals(params2, params3);
assertNotEquals(params2.hashCode(), params3.hashCode());
final DelegationTokenImpl delToken2 = new DelegationTokenImpl(new byte[0], new AuthenticationTokenIdentifier("user", 1, 10l, 20l, "instanceid"));
SaslConnectionParams params4 = testUser.doAs(new PrivilegedExceptionAction<SaslConnectionParams>() {
@Override
public SaslConnectionParams run() throws Exception {
final ClientConfiguration clientConf = ClientConfiguration.loadDefault();
// The primary is the first component of the principal
final String primary = "accumulo";
clientConf.withSasl(true, primary);
final AccumuloConfiguration rpcConf = ClientContext.convertClientConfig(clientConf);
assertEquals("true", clientConf.get(ClientProperty.INSTANCE_RPC_SASL_ENABLED));
return new SaslConnectionParams(rpcConf, delToken2);
}
});
assertNotEquals(params1, params4);
assertNotEquals(params1.hashCode(), params4.hashCode());
assertNotEquals(params2, params4);
assertNotEquals(params2.hashCode(), params4.hashCode());
assertEquals(params3, params4);
assertEquals(params3.hashCode(), params4.hashCode());
}
use of org.apache.accumulo.core.client.ClientConfiguration in project accumulo by apache.
the class SaslConnectionParamsTest method testDefaultParams.
@Test
public void testDefaultParams() throws Exception {
final KerberosToken token = EasyMock.createMock(KerberosToken.class);
testUser.doAs(new PrivilegedExceptionAction<Void>() {
@Override
public Void run() throws Exception {
final ClientConfiguration clientConf = ClientConfiguration.loadDefault();
// The primary is the first component of the principal
final String primary = "accumulo";
clientConf.withSasl(true, primary);
final AccumuloConfiguration rpcConf = ClientContext.convertClientConfig(clientConf);
assertEquals("true", clientConf.get(ClientProperty.INSTANCE_RPC_SASL_ENABLED));
final SaslConnectionParams saslParams = new SaslConnectionParams(rpcConf, token);
assertEquals(primary, saslParams.getKerberosServerPrimary());
final QualityOfProtection defaultQop = QualityOfProtection.get(Property.RPC_SASL_QOP.getDefaultValue());
assertEquals(defaultQop, saslParams.getQualityOfProtection());
Map<String, String> properties = saslParams.getSaslProperties();
assertEquals(1, properties.size());
assertEquals(defaultQop.getQuality(), properties.get(Sasl.QOP));
assertEquals(username, saslParams.getPrincipal());
return null;
}
});
}
Aggregations