Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/
package org.elasticsearch.logstashbridge.script;

import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.FixForMultiProject;
import org.elasticsearch.ingest.common.ProcessorsWhitelistExtension;
import org.elasticsearch.logstashbridge.StableBridgeAPI;
import org.elasticsearch.logstashbridge.common.SettingsBridge;
Expand Down Expand Up @@ -66,7 +68,9 @@ private static ScriptService getScriptService(final Settings settings, final Lon
MustacheScriptEngine.NAME,
new MustacheScriptEngine(settings)
);
return new ScriptService(settings, scriptEngines, ScriptModule.CORE_CONTEXTS, timeProvider);
@FixForMultiProject // Should this be non-null?
final ProjectResolver projectResolver = null;
return new ScriptService(settings, scriptEngines, ScriptModule.CORE_CONTEXTS, timeProvider, projectResolver);
}

private static List<Whitelist> getPainlessBaseWhiteList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.elasticsearch.cluster.project.TestProjectResolvers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.time.DateFormatters;
import org.elasticsearch.index.mapper.DateFieldMapper;
Expand Down Expand Up @@ -106,7 +107,8 @@ public Set<ScriptContext<?>> getSupportedContexts() {
Settings.EMPTY,
engines,
Map.of(MovingFunctionScript.CONTEXT.name, MovingFunctionScript.CONTEXT),
() -> 1L
() -> 1L,
TestProjectResolvers.singleProject(randomProjectIdOrDefault())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ public boolean execute(Token token) {
}
};

ScriptService scriptService = new ScriptService(indexSettings, Collections.emptyMap(), Collections.emptyMap(), () -> 1L) {
ScriptService scriptService = new ScriptService(
indexSettings,
Collections.emptyMap(),
Collections.emptyMap(),
() -> 1L,
TestProjectResolvers.singleProject(randomProjectIdOrDefault())
) {
@Override
@SuppressWarnings("unchecked")
public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ public boolean execute(Token token) {
};

@SuppressWarnings("unchecked")
ScriptService scriptService = new ScriptService(indexSettings, Collections.emptyMap(), Collections.emptyMap(), () -> 1L) {
ScriptService scriptService = new ScriptService(
indexSettings,
Collections.emptyMap(),
Collections.emptyMap(),
() -> 1L,
TestProjectResolvers.singleProject(randomProjectIdOrDefault())
) {
@Override
public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
assertEquals(context, AnalysisPredicateScript.CONTEXT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.elasticsearch.ingest.common;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cluster.project.TestProjectResolvers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.ingest.TestIngestDocument;
import org.elasticsearch.script.CtxMap;
Expand Down Expand Up @@ -145,7 +146,8 @@ public void testInlineIsCompiled() throws Exception {
return null;
}), Map.of())),
new HashMap<>(ScriptModule.CORE_CONTEXTS),
() -> 1L
() -> 1L,
TestProjectResolvers.singleProject(randomProjectIdOrDefault())
);
factory = new ScriptProcessor.Factory(scriptService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.ingest.common;

import org.elasticsearch.cluster.project.TestProjectResolvers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.ingest.IngestDocument;
import org.elasticsearch.ingest.RandomDocumentPicks;
Expand Down Expand Up @@ -47,7 +48,8 @@ public void setupScripting() {
return null;
}), Map.of())),
new HashMap<>(ScriptModule.CORE_CONTEXTS),
() -> 1L
() -> 1L,
TestProjectResolvers.singleProject(randomProjectIdOrDefault())
);
script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Map.of());
ingestScriptFactory = scriptService.compile(script, IngestScript.CONTEXT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.lucene.search.DoubleValues;
import org.apache.lucene.search.DoubleValuesSource;
import org.apache.lucene.search.SortField;
import org.elasticsearch.cluster.project.TestProjectResolvers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.DoubleValuesScript;
import org.elasticsearch.script.Script;
Expand All @@ -39,7 +40,13 @@ public void setUp() throws Exception {
super.setUp();

engine = new ExpressionScriptEngine();
scriptService = new ScriptService(Settings.EMPTY, Map.of("expression", engine), ScriptModule.CORE_CONTEXTS, () -> 1L);
scriptService = new ScriptService(
Settings.EMPTY,
Map.of("expression", engine),
ScriptModule.CORE_CONTEXTS,
() -> 1L,
TestProjectResolvers.singleProject(randomProjectIdOrDefault())
);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

package org.elasticsearch.ingest;

import org.elasticsearch.cluster.project.TestProjectResolvers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptEngine;
Expand All @@ -33,7 +34,13 @@ public abstract class AbstractScriptTestCase extends ESTestCase {
public void init() throws Exception {
MustacheScriptEngine engine = new MustacheScriptEngine(Settings.EMPTY);
Map<String, ScriptEngine> engines = Collections.singletonMap(engine.getType(), engine);
scriptService = new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS, () -> 1L);
scriptService = new ScriptService(
Settings.EMPTY,
engines,
ScriptModule.CORE_CONTEXTS,
() -> 1L,
TestProjectResolvers.singleProject(randomProjectIdOrDefault())
);
}

protected TemplateScript.Factory compile(String template) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected void masterOperation(
ClusterState state,
ActionListener<AcknowledgedResponse> listener
) throws Exception {
ScriptService.deleteStoredScript(clusterService, request, listener);
ScriptService.deleteStoredScript(clusterService, projectResolver.getProjectId(), request, listener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.ActionFilters;
import org.elasticsearch.action.support.master.TransportMasterNodeReadAction;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.action.support.master.TransportMasterNodeReadProjectAction;
import org.elasticsearch.cluster.ProjectState;
import org.elasticsearch.cluster.block.ClusterBlockException;
import org.elasticsearch.cluster.block.ClusterBlockLevel;
import org.elasticsearch.cluster.project.ProjectResolver;
Expand All @@ -24,9 +24,7 @@
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

public class TransportGetStoredScriptAction extends TransportMasterNodeReadAction<GetStoredScriptRequest, GetStoredScriptResponse> {

private final ProjectResolver projectResolver;
public class TransportGetStoredScriptAction extends TransportMasterNodeReadProjectAction<GetStoredScriptRequest, GetStoredScriptResponse> {

@Inject
public TransportGetStoredScriptAction(
Expand All @@ -43,25 +41,25 @@ public TransportGetStoredScriptAction(
threadPool,
actionFilters,
GetStoredScriptRequest::new,
projectResolver,
GetStoredScriptResponse::new,
EsExecutors.DIRECT_EXECUTOR_SERVICE
);
this.projectResolver = projectResolver;
}

@Override
protected void masterOperation(
Task task,
GetStoredScriptRequest request,
ClusterState state,
ProjectState state,
ActionListener<GetStoredScriptResponse> listener
) throws Exception {
listener.onResponse(new GetStoredScriptResponse(request.id(), ScriptService.getStoredScript(state, request)));
listener.onResponse(new GetStoredScriptResponse(request.id(), ScriptService.getStoredScript(state.metadata(), request)));
}

@Override
protected ClusterBlockException checkBlock(GetStoredScriptRequest request, ClusterState state) {
return state.blocks().globalBlockedException(projectResolver.getProjectId(), ClusterBlockLevel.METADATA_READ);
protected ClusterBlockException checkBlock(GetStoredScriptRequest request, ProjectState state) {
return state.blocks().globalBlockedException(state.projectId(), ClusterBlockLevel.METADATA_READ);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected void masterOperation(
ClusterState state,
ActionListener<AcknowledgedResponse> listener
) throws Exception {
scriptService.putStoredScript(clusterService, request, listener);
scriptService.putStoredScript(clusterService, projectResolver.getProjectId(), request, listener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ static NodeConstruction prepareConstruction(
constructor.createClientAndRegistries(settingsModule.getSettings(), threadPool, searchModule, projectResolver);
DocumentParsingProvider documentParsingProvider = constructor.getDocumentParsingProvider();

ScriptService scriptService = constructor.createScriptService(settingsModule, threadPool, serviceProvider);
ScriptService scriptService = constructor.createScriptService(settingsModule, threadPool, serviceProvider, projectResolver);

constructor.createUpdateHelper(scriptService);

Expand Down Expand Up @@ -612,7 +612,12 @@ private void createClientAndRegistries(
});
}

private ScriptService createScriptService(SettingsModule settingsModule, ThreadPool threadPool, NodeServiceProvider serviceProvider) {
private ScriptService createScriptService(
SettingsModule settingsModule,
ThreadPool threadPool,
NodeServiceProvider serviceProvider,
ProjectResolver projectResolver
) {
Settings settings = settingsModule.getSettings();
ScriptModule scriptModule = new ScriptModule(settings, pluginsService.filterPlugins(ScriptPlugin.class).toList());

Expand All @@ -621,7 +626,8 @@ private ScriptService createScriptService(SettingsModule settingsModule, ThreadP
settings,
scriptModule.engines,
scriptModule.contexts,
threadPool::absoluteTimeInMillis
threadPool::absoluteTimeInMillis,
projectResolver
);
ScriptModule.registerClusterSettingsListeners(scriptService, settingsModule.getClusterSettings());
modules.add(b -> { b.bind(ScriptService.class).toInstance(scriptService); });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.cluster.InternalClusterInfoService;
import org.elasticsearch.cluster.NodeUsageStatsForThreadPoolsCollector;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.project.ProjectResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.breaker.CircuitBreaker;
import org.elasticsearch.common.network.NetworkModule;
Expand Down Expand Up @@ -64,9 +65,10 @@ ScriptService newScriptService(
Settings settings,
Map<String, ScriptEngine> engines,
Map<String, ScriptContext<?>> contexts,
LongSupplier timeProvider
LongSupplier timeProvider,
ProjectResolver projectResolver
) {
return new ScriptService(settings, engines, contexts, timeProvider);
return new ScriptService(settings, engines, contexts, timeProvider, projectResolver);
}

ClusterInfoService newClusterInfoService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.cluster.metadata.ProjectId;
import org.elasticsearch.common.breaker.CircuitBreaker;
import org.elasticsearch.common.breaker.CircuitBreakingException;
import org.elasticsearch.common.cache.Cache;
Expand Down Expand Up @@ -78,13 +79,14 @@ public class ScriptCache {
<FactoryType> FactoryType compile(
ScriptContext<FactoryType> context,
ScriptEngine scriptEngine,
ProjectId projectId,
String id,
String idOrCode,
ScriptType type,
Map<String, String> options
) {
String lang = scriptEngine.getType();
CacheKey cacheKey = new CacheKey(lang, idOrCode, context.name, options);
CacheKey cacheKey = new CacheKey(lang, projectId, idOrCode, context.name, options);

// Relying on computeIfAbsent to avoid multiple threads from compiling the same script
try {
Expand Down Expand Up @@ -204,12 +206,14 @@ public void onRemoval(RemovalNotification<CacheKey, Object> notification) {

private static final class CacheKey {
final String lang;
final ProjectId projectId;
final String idOrCode;
final String context;
final Map<String, String> options;

private CacheKey(String lang, String idOrCode, String context, Map<String, String> options) {
private CacheKey(String lang, ProjectId projectId, String idOrCode, String context, Map<String, String> options) {
this.lang = lang;
this.projectId = projectId;
this.idOrCode = idOrCode;
this.context = context;
this.options = options;
Expand Down
Loading