-
Notifications
You must be signed in to change notification settings - Fork 25.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanup ScriptType #21179
Cleanup ScriptType #21179
Changes from 2 commits
e95cb6e
c6aea81
c90eda6
f8eed68
d693613
cd0709b
c2a641f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,66 +24,117 @@ | |
import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
|
||
/** | ||
* The type of a script, more specifically where it gets loaded from: | ||
* - provided dynamically at request time | ||
* - loaded from an index | ||
* - loaded from file | ||
* ScriptType represents the way a script is stored and retrieved from the {@link ScriptService}. | ||
* It's also used to by {@link ScriptSettings} and {@link ScriptModes} to determine whether or not | ||
* a {@link Script} is allowed to be executed based on both default and user-defined settings. | ||
*/ | ||
public enum ScriptType { | ||
|
||
INLINE(0, "inline", "inline", false), | ||
STORED(1, "id", "stored", false), | ||
FILE(2, "file", "file", true); | ||
/** | ||
* INLINE scripts are specified in numerous queries and compiled on-the-fly. | ||
* They will be cached based on the lang and code of the script. | ||
* They are turned off by default for security purposes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are turned off for groovy by default because it is insecure but they are on by default for painless and mustache. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
*/ | ||
INLINE ( 0 , "inline" , new ParseField("inline") , false ), | ||
|
||
private final int val; | ||
private final ParseField parseField; | ||
private final String scriptType; | ||
private final boolean defaultScriptEnabled; | ||
/** | ||
* STORED scripts are saved as part of the {@link org.elasticsearch.cluster.ClusterState} | ||
* based on user requests. They will be cached when they are first used in a query. | ||
* They are turned off by default for security purposes. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added here too. |
||
*/ | ||
STORED ( 1 , "stored" , new ParseField("stored", "id") , false ), | ||
|
||
/** | ||
* FILE scripts are loaded from disk either on start-up or on-the-fly depending on | ||
* user-defined settings. They will be compiled and cached as soon as they are loaded | ||
* from disk. They are turned on by default as they should always be safe to execute. | ||
*/ | ||
FILE ( 2 , "file" , new ParseField("file") , true ); | ||
|
||
/** | ||
* Reads an int from the input stream and converts it to a {@link ScriptType}. | ||
* @return The ScriptType read from the stream. Throws an {@link IllegalStateException} if no ScriptType is found based on the id. | ||
*/ | ||
public static ScriptType readFrom(StreamInput in) throws IOException { | ||
int scriptTypeVal = in.readVInt(); | ||
for (ScriptType type : values()) { | ||
if (type.val == scriptTypeVal) { | ||
return type; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unexpected value read for ScriptType got [" + scriptTypeVal + "] expected one of [" | ||
+ INLINE.val + "," + FILE.val + "," + STORED.val + "]"); | ||
} | ||
int id = in.readVInt(); | ||
|
||
public static void writeTo(ScriptType scriptType, StreamOutput out) throws IOException{ | ||
if (scriptType != null) { | ||
out.writeVInt(scriptType.val); | ||
if (FILE.id == id) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe so There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit awkward because the id is not considered a compile-time constant since it's set through the enum constructor. One option would be to add compile time constants for each id and use that instead, but I think the if statement is probably simpler. |
||
return FILE; | ||
} else if (STORED.id == id) { | ||
return STORED; | ||
} else if (INLINE.id == id) { | ||
return INLINE; | ||
} else { | ||
out.writeVInt(INLINE.val); //Default to inline | ||
throw new IllegalStateException("Error reading ScriptType id [" + id + "] from stream, expected one of [" + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. :) |
||
FILE.id + " [" + FILE.name + "], " + STORED.id + " [" + STORED.name + "], " + INLINE.id + " [" + INLINE.name + "]]"); | ||
} | ||
} | ||
|
||
ScriptType(int val, String name, String scriptType, boolean defaultScriptEnabled) { | ||
this.val = val; | ||
this.parseField = new ParseField(name); | ||
this.scriptType = scriptType; | ||
this.defaultScriptEnabled = defaultScriptEnabled; | ||
/** | ||
* Writes an int to the output stream based on the id of the {@link ScriptType}. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to explain that wire format for the enum is an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. Should inherit the base class JavaDoc. |
||
* @param type The {@link ScriptType}. Must not be {@code null}. | ||
*/ | ||
public static void writeTo(ScriptType type, StreamOutput out) throws IOException { | ||
Objects.requireNonNull(type); | ||
out.writeVInt(type.id); | ||
} | ||
|
||
public ParseField getParseField() { | ||
return parseField; | ||
private final int id; | ||
private final String name; | ||
private final ParseField parseField; | ||
private final boolean defaultEnabled; | ||
|
||
/** | ||
* Standard constructor. | ||
* @param id A unique identifier for a type that can be read/written to a stream. | ||
* @param name A unique name for a type. | ||
* @param parseField Specifies the name used to parse input from queries. | ||
* @param defaultEnabled Whether or not an {@link ScriptType} can be run by default. | ||
*/ | ||
ScriptType(int id, String name, ParseField parseField, boolean defaultEnabled) { | ||
this.id = id; | ||
this.name = name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name is always the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. Forces the name of the ParseField to be the same as the name of the enum. |
||
this.parseField = parseField; | ||
this.defaultEnabled = defaultEnabled; | ||
} | ||
|
||
public boolean getDefaultScriptEnabled() { | ||
return defaultScriptEnabled; | ||
/** | ||
* @return The unique id for this {@link ScriptType}. | ||
*/ | ||
public int getId() { | ||
return id; | ||
} | ||
|
||
public String getScriptType() { | ||
return scriptType; | ||
/** | ||
* @return The unique name for this {@link ScriptType}. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* @return Specifies the name used to parse input from queries. | ||
*/ | ||
public ParseField getParseField() { | ||
return parseField; | ||
} | ||
|
||
/** | ||
* @return Whether or not a {@link ScriptType} can be run by default. Note | ||
* this can be potentially overriden by any {@link ScriptEngineService}. | ||
*/ | ||
public boolean isDefaultEnabled() { | ||
return defaultEnabled; | ||
} | ||
|
||
/** | ||
* @return The same as calling {@link #getName()}. | ||
*/ | ||
@Override | ||
public String toString() { | ||
return name().toLowerCase(Locale.ROOT); | ||
return getName(); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we can implement Writeable here then we don't need the static writeTo method anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this now. Will re-test and commit once the tests are good.