diff --git a/shield/src/main/java/org/elasticsearch/shield/action/ShieldActionFilter.java b/shield/src/main/java/org/elasticsearch/shield/action/ShieldActionFilter.java index 5354d9fc802..20faeeea0e7 100644 --- a/shield/src/main/java/org/elasticsearch/shield/action/ShieldActionFilter.java +++ b/shield/src/main/java/org/elasticsearch/shield/action/ShieldActionFilter.java @@ -27,6 +27,7 @@ import org.elasticsearch.shield.crypto.CryptoService; import org.elasticsearch.shield.license.LicenseEventsNotifier; import org.elasticsearch.shield.license.LicenseService; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -148,7 +149,7 @@ public class ShieldActionFilter extends AbstractComponent implements ActionFilte } } - Response sign(Response response) { + Response sign(Response response) throws IOException { if (response instanceof SearchResponse) { SearchResponse searchResponse = (SearchResponse) response; @@ -174,8 +175,12 @@ public class ShieldActionFilter extends AbstractComponent implements ActionFilte @Override @SuppressWarnings("unchecked") public void onResponse(Response response) { - response = this.filter.sign(response); - innerListener.onResponse(response); + try { + response = this.filter.sign(response); + innerListener.onResponse(response); + } catch (IOException e) { + onFailure(e); + } } @Override diff --git a/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java b/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java index d19ccc26d8a..3dd57488b98 100644 --- a/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java +++ b/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java @@ -660,7 +660,11 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail { bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() { @Override public void beforeBulk(long executionId, BulkRequest request) { - authenticationService.attachUserHeaderIfMissing(request, auditUser.user()); + try { + authenticationService.attachUserHeaderIfMissing(request, auditUser.user()); + } catch (IOException e) { + throw new ElasticsearchException("failed to attach user header", e); + } } @Override diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/AuthenticationService.java b/shield/src/main/java/org/elasticsearch/shield/authc/AuthenticationService.java index 550eb942906..a563c3438fd 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/AuthenticationService.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/AuthenticationService.java @@ -10,6 +10,8 @@ import org.elasticsearch.rest.RestRequest; import org.elasticsearch.shield.User; import org.elasticsearch.transport.TransportMessage; +import java.io.IOException; + /** * Responsible for authenticating the Users behind requests */ @@ -46,7 +48,7 @@ public interface AuthenticationService { * case where there was no user associated with the request, if the defautl * token could not be authenticated. */ - User authenticate(String action, TransportMessage message, User fallbackUser); + User authenticate(String action, TransportMessage message, User fallbackUser) throws IOException; /** * Checks if there's alreay a user header attached to the given message. If missing, a new header is @@ -55,6 +57,6 @@ public interface AuthenticationService { * @param message The message * @param user The user to be attached if the header is missing */ - void attachUserHeaderIfMissing(TransportMessage message, User user); + void attachUserHeaderIfMissing(TransportMessage message, User user) throws IOException; } diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/InternalAuthenticationService.java b/shield/src/main/java/org/elasticsearch/shield/authc/InternalAuthenticationService.java index eb0591ffea0..38b0d80c13b 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/InternalAuthenticationService.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/InternalAuthenticationService.java @@ -105,7 +105,7 @@ public class InternalAuthenticationService extends AbstractComponent implements } @Override - public User authenticate(String action, TransportMessage message, User fallbackUser) { + public User authenticate(String action, TransportMessage message, User fallbackUser) throws IOException { User user = message.getFromContext(USER_KEY); if (user != null) { return user; @@ -127,7 +127,7 @@ public class InternalAuthenticationService extends AbstractComponent implements } @Override - public void attachUserHeaderIfMissing(TransportMessage message, User user) { + public void attachUserHeaderIfMissing(TransportMessage message, User user) throws IOException { if (message.hasHeader(USER_KEY)) { return; } diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectorySessionFactory.java b/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectorySessionFactory.java index c4803baa1a0..a7a37ad5296 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectorySessionFactory.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/activedirectory/ActiveDirectorySessionFactory.java @@ -119,7 +119,6 @@ public class ActiveDirectorySessionFactory extends SessionFactory { return new LdapSession(connectionLogger, connection, dn, groupResolver, timeout); } catch (LDAPException e) { connection.close(); - // TODO think more about this exception... throw authenticationError("unable to authenticate user [{}] to active directory domain [{}]", e, userName, domainName); } } diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapRealm.java b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapRealm.java index 57f79205b8a..f66e8011492 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapRealm.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapRealm.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.shield.authc.ldap; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.RestController; @@ -16,6 +17,8 @@ import org.elasticsearch.shield.authc.support.DnRoleMapper; import org.elasticsearch.shield.ssl.ClientSSLService; import org.elasticsearch.watcher.ResourceWatcherService; +import java.io.IOException; + /** * Authenticates username/password tokens against ldap, locates groups and maps them to roles. */ @@ -46,12 +49,16 @@ public class LdapRealm extends AbstractLdapRealm { @Override public LdapRealm create(RealmConfig config) { - SessionFactory sessionFactory = sessionFactory(config, clientSSLService); - DnRoleMapper roleMapper = new DnRoleMapper(TYPE, config, watcherService, null); - return new LdapRealm(config, sessionFactory, roleMapper); + try { + SessionFactory sessionFactory = sessionFactory(config, clientSSLService); + DnRoleMapper roleMapper = new DnRoleMapper(TYPE, config, watcherService, null); + return new LdapRealm(config, sessionFactory, roleMapper); + } catch (IOException e) { + throw new ElasticsearchException("failed to create realm [{}/{}]", e, LdapRealm.TYPE, config.name()); + } } - static SessionFactory sessionFactory(RealmConfig config, ClientSSLService clientSSLService) { + static SessionFactory sessionFactory(RealmConfig config, ClientSSLService clientSSLService) throws IOException { Settings searchSettings = config.settings().getAsSettings("user_search"); if (!searchSettings.names().isEmpty()) { if (config.settings().getAsArray(LdapSessionFactory.USER_DN_TEMPLATES_SETTING).length > 0) { diff --git a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapUserSearchSessionFactory.java b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapUserSearchSessionFactory.java index fe581371c77..33fce6902db 100644 --- a/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapUserSearchSessionFactory.java +++ b/shield/src/main/java/org/elasticsearch/shield/authc/ldap/LdapUserSearchSessionFactory.java @@ -7,7 +7,6 @@ package org.elasticsearch.shield.authc.ldap; import com.google.common.primitives.Ints; import com.unboundid.ldap.sdk.*; -import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; @@ -43,7 +42,7 @@ public class LdapUserSearchSessionFactory extends SessionFactory { private final String userAttribute; private final ServerSet serverSet; - public LdapUserSearchSessionFactory(RealmConfig config, ClientSSLService sslService) { + public LdapUserSearchSessionFactory(RealmConfig config, ClientSSLService sslService) throws IOException { super(config); Settings settings = config.settings(); userSearchBaseDn = settings.get("user_search.base_dn"); @@ -55,8 +54,6 @@ public class LdapUserSearchSessionFactory extends SessionFactory { serverSet = serverSet(settings, sslService); connectionPool = connectionPool(config.settings(), serverSet, timeout); groupResolver = groupResolver(settings); - - } static void filterOutSensitiveSettings(String realmName, ShieldSettingsFilter filter) { @@ -65,7 +62,7 @@ public class LdapUserSearchSessionFactory extends SessionFactory { filter.filterOut("shield.authc.realms." + realmName + "." + HOSTNAME_VERIFICATION_SETTING); } - static LDAPConnectionPool connectionPool(Settings settings, ServerSet serverSet, TimeValue timeout) { + static LDAPConnectionPool connectionPool(Settings settings, ServerSet serverSet, TimeValue timeout) throws IOException { SimpleBindRequest bindRequest = bindRequest(settings); int initialSize = settings.getAsInt("user_search.pool.initial_size", DEFAULT_CONNECTION_POOL_INITIAL_SIZE); int size = settings.getAsInt("user_search.pool.size", DEFAULT_CONNECTION_POOL_SIZE); @@ -88,8 +85,7 @@ public class LdapUserSearchSessionFactory extends SessionFactory { } return pool; } catch (LDAPException e) { - // TODO consider changing this to IOException and bubble it up - throw new ElasticsearchException("unable to connect to any LDAP servers", e); + throw new IOException("unable to connect to any LDAP servers", e); } } diff --git a/shield/src/main/java/org/elasticsearch/shield/crypto/CryptoService.java b/shield/src/main/java/org/elasticsearch/shield/crypto/CryptoService.java index 962716e6a28..ff8471fef3c 100644 --- a/shield/src/main/java/org/elasticsearch/shield/crypto/CryptoService.java +++ b/shield/src/main/java/org/elasticsearch/shield/crypto/CryptoService.java @@ -6,6 +6,7 @@ package org.elasticsearch.shield.crypto; import javax.crypto.SecretKey; +import java.io.IOException; /** * Service that provides cryptographic methods based on a shared system key @@ -16,11 +17,11 @@ public interface CryptoService { * Signs the given text and returns the signed text (original text + signature) * @param text the string to sign */ - String sign(String text); + String sign(String text) throws IOException; /** * Unsigns the given signed text, verifies the original text with the attached signature and if valid returns - * the unsigned (original) text. If signature verification fails a {@link SignatureException} is thrown. + * the unsigned (original) text. If signature verification fails a {@link IllegalArgumentException} is thrown. * @param text the string to unsign and verify */ String unsignAndVerify(String text); @@ -30,11 +31,11 @@ public interface CryptoService { * @param text the string to sign * @param key the key to sign the text with */ - String sign(String text, SecretKey key); + String sign(String text, SecretKey key) throws IOException; /** * Unsigns the given signed text, verifies the original text with the attached signature and if valid returns - * the unsigned (original) text. If signature verification fails a {@link SignatureException} is thrown. + * the unsigned (original) text. If signature verification fails a {@link IllegalArgumentException} is thrown. * @param text the string to unsign and verify * @param key the key to unsign the text with */ @@ -121,8 +122,8 @@ public interface CryptoService { * service. This provides the old keys back to the clients so that they may perform decryption and re-encryption * of data after a key has been changed * - * @param oldSystemKey - * @param oldEncryptionKey + * @param oldSystemKey the pre-existing system key + * @param oldEncryptionKey the pre-existing encryption key */ void onKeyChange(SecretKey oldSystemKey, SecretKey oldEncryptionKey); } diff --git a/shield/src/main/java/org/elasticsearch/shield/crypto/InternalCryptoService.java b/shield/src/main/java/org/elasticsearch/shield/crypto/InternalCryptoService.java index 8a560b9df7d..9b114b8afb2 100644 --- a/shield/src/main/java/org/elasticsearch/shield/crypto/InternalCryptoService.java +++ b/shield/src/main/java/org/elasticsearch/shield/crypto/InternalCryptoService.java @@ -139,12 +139,12 @@ public class InternalCryptoService extends AbstractLifecycleComponent