public XContentThrowableRestResponse(RestRequest request, Throwable t) throws IOException { this(request, ((t instanceof ElasticsearchException) ? ((ElasticsearchException) t).status() : RestStatus.INTERNAL_SERVER_ERROR), t); }
private static boolean clusterHas5xNodes(ClusterState state) { return state.nodes().getMinNodeVersion().before(Version.V_6_0_0_alpha1); }
private Resolved(final Set<String> aliases, final Set<String> indices, final Set<String> allIndices, final Set<String> types) { super(); this.aliases = aliases; this.indices = indices; this.allIndices = allIndices; this.types = types; if(!aliases.isEmpty() || !indices.isEmpty() || !allIndices.isEmpty()) { if(types.isEmpty()) { throw new ElasticsearchException("Empty types for nonempty inidices or aliases"); } } }
private static boolean clusterHas5xIndices(ClusterState state) { final Iterator<IndexMetaData> indices = state.metaData().indices().valuesIt(); for(;indices.hasNext();) { final IndexMetaData indexMetaData = indices.next(); if(indexMetaData.getCreationVersion().before(Version.V_6_0_0_alpha1)) { return true; } } return false; } }
private void checkExpectedClientVersion() { /* * This is enclosed in a catch block to prevent an unchecked exception * from killing the startup thread. This check is just advisory -- the * most it does is log a warning -- so there's no reason to allow it to * emit a exception and potentially block graph startup. */ try { if (!Version.CURRENT.toString().equals(ElasticSearchConstants.ES_VERSION_EXPECTED)) { log.warn("ES client version ({}) does not match the version with which Titan was compiled ({}). This might cause problems.", Version.CURRENT, ElasticSearchConstants.ES_VERSION_EXPECTED); } else { log.debug("Found ES client version matching Titan's compile-time version: {} (OK)", Version.CURRENT); } } catch (RuntimeException e) { log.warn("Unable to check expected ES client version", e); } } }
private synchronized void notifyAboutChanges(Map<String, Settings> typeToConfig) { for (Map.Entry<String, ConfigurationChangeListener> entry : configTypeToChancheListener.entries()) { String type = entry.getKey(); ConfigurationChangeListener listener = entry.getValue(); Settings settings = typeToConfig.get(type); if (settings == null) { continue; } try { LOGGER.debug("Notify {} listener about change configuration with type {}", listener, type); listener.onChange(settings); } catch (Exception e) { LOGGER.error("{} listener errored: "+e, listener, e); throw ExceptionsHelper.convertToElastic(e); } } }
public void run() { SpecialPermission.check(); AccessController.doPrivileged((PrivilegedAction<Void>) () -> { this.runUnprivileged(); return null; }); }
@Override public void fillRoles(User user, AuthCredentials credentials) throws ElasticsearchSecurityException { final Settings cfg = getConfigSettings(); if (cfg == null) { throw new ElasticsearchSecurityException("Internal authentication backend not configured. May be Search Guard is not initialized. See http://docs.search-guard.com/v6/sgadmin"); } final List<String> roles = cfg.getAsList(credentials.getUsername() + ".roles", Collections.emptyList()); if(roles != null && !roles.isEmpty() && user != null) { user.addRoles(roles); } } }
private static void setCurrentThreadName(final String name) { final SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission(new SpecialPermission()); } AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { Thread.currentThread().setName(name); return null; } }); }
private boolean indexAvailable(ElasticsearchException ex) { return !CollectionUtils.contains(ex.getMetadata("es.index_uuid").iterator(), "_na_"); } }
private String sha256(Path p) { if(!Files.isRegularFile(p, LinkOption.NOFOLLOW_LINKS)) { return ""; } if(!Files.isReadable(p)) { log.debug("Unreadable file "+p+" found"); return ""; } try { MessageDigest digester = MessageDigest.getInstance("SHA256"); final String hash = org.bouncycastle.util.encoders.Hex.toHexString(digester.digest(Files.readAllBytes(p))); log.debug(hash +" :: "+p); return hash; } catch (Exception e) { throw new ElasticsearchSecurityException("Unable to digest file "+p, e); } }
public static void checkPath(String keystoreFilePath, String fileNameLogOnly) { if (keystoreFilePath == null || keystoreFilePath.length() == 0) { throw new ElasticsearchException("Empty file path for "+fileNameLogOnly); } if (Files.isDirectory(Paths.get(keystoreFilePath), LinkOption.NOFOLLOW_LINKS)) { throw new ElasticsearchException("Is a directory: " + keystoreFilePath+" Expected a file for "+fileNameLogOnly); } if(!Files.isReadable(Paths.get(keystoreFilePath))) { throw new ElasticsearchException("Unable to read " + keystoreFilePath + " ("+Paths.get(keystoreFilePath)+"). Please make sure this files exists and is readable regarding to permissions. Property: "+fileNameLogOnly); } }
private static List<String> getRemoteWords(String location) { SpecialPermission.check(); return AccessController.doPrivileged((PrivilegedAction<List<String>>) () -> { return getRemoteWordsUnprivileged(location); }); }
@Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (ex instanceof ElasticsearchException) { ElasticsearchException elasticsearchException = (ElasticsearchException) ex; if (!indexAvailable(elasticsearchException)) { return new NoSuchIndexException(elasticsearchException.getMetadata("es.index").toString(), ex); } } if (ex.getCause() instanceof ConnectException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } return null; }
private DB getConfigDb() { DB configDb = clusterClient.getDB(MongoDBRiver.MONGODB_CONFIG_DATABASE); if (configDb == null) { throw new ElasticsearchException( String.format("Could not get %s database from MongoDB", MongoDBRiver.MONGODB_CONFIG_DATABASE)); } return configDb; }
private DB getAdminDb() { DB adminDb = clusterClient.getDB(MongoDBRiver.MONGODB_ADMIN_DATABASE); if (adminDb == null) { throw new ElasticsearchException( String.format("Could not get %s database from MongoDB", MongoDBRiver.MONGODB_ADMIN_DATABASE)); } return adminDb; }
public static String serializeObject(final Serializable object) { if (object == null) { throw new IllegalArgumentException("object must not be null"); } try { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); final ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(object); final byte[] bytes = bos.toByteArray(); return BaseEncoding.base64().encode(bytes); } catch (final Exception e) { throw new ElasticsearchException(e.toString()); } }
public static Serializable deserializeObject(final String string) { if (string == null) { throw new IllegalArgumentException("string must not be null"); } SafeObjectInputStream in = null; try { final byte[] userr = BaseEncoding.base64().decode(string); final ByteArrayInputStream bis = new ByteArrayInputStream(userr); //NOSONAR in = new SafeObjectInputStream(bis); //NOSONAR return (Serializable) in.readObject(); } catch (final Exception e) { throw new ElasticsearchException(e); } finally { if (in != null) { try { in.close(); } catch (IOException e) { // ignore } } } }
@Test public void attributes_displays_cause_message_when_cause_is_ElasticSearchException_when_client_fails() { EsClient esClientMock = mock(EsClient.class); EsStateSection underTest = new EsStateSection(esClientMock); when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with ES cause", new ElasticsearchException("some cause message"))); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "State", "some cause message"); } }
@Test public void attributes_displays_cause_message_when_cause_is_ElasticSearchException_when_client_fails() { EsClient esClientMock = mock(EsClient.class); EsIndexesSection underTest = new EsIndexesSection(esClientMock); when(esClientMock.prepareStats()).thenThrow(new RuntimeException("RuntimeException with ES cause", new ElasticsearchException("some cause message"))); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "Error", "some cause message"); } }