Skip to content

Commit

Permalink
SQL command secret password support
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Mar 30, 2022
1 parent 591aae0 commit 56914c4
Showing 1 changed file with 16 additions and 9 deletions.
Expand Up @@ -2,6 +2,8 @@

import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.SecretTag;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
Expand All @@ -24,14 +26,14 @@ public class SQLCommand extends AbstractCommand implements Holdable {

public SQLCommand() {
setName("sql");
setSyntax("sql [id:<ID>] [disconnect/connect:<server> (username:<username>) (passwordfile:<file>) (ssl:true/{false})/query:<query>/update:<update>]");
setSyntax("sql [id:<ID>] [disconnect/connect:<server> (username:<username>) (password:<secret>) (ssl:true/{false})/query:<query>/update:<update>]");
setRequiredArguments(2, 5);
isProcedural = false;
}

// <--[command]
// @Name SQL
// @Syntax sql [id:<ID>] [disconnect/connect:<server> (username:<username>) (passwordfile:<file>) (ssl:true/{false})/query:<query>/update:<update>]
// @Syntax sql [id:<ID>] [disconnect/connect:<server> (username:<username>) (password:<secret>) (ssl:true/{false})/query:<query>/update:<update>]
// @Required 2
// @Maximum 5
// @Short Interacts with a MySQL server.
Expand All @@ -48,7 +50,7 @@ public SQLCommand() {
//
// When connecting, the server format is IP:Port/Database, EG 'localhost:3306/test'.
// You can also append options to the end, like 'localhost:3306/test?autoReconnect=true'
// You can create a simple text file to contain the password, and store it in 'plugins/Denizen' or a subfolder, and input it using the 'passwordfile' option.
// Store your password in the Denizen secrets file at 'plugins/Denizen/secrets.secret'. Refer to <@link ObjectType SecretTag> for usage info.
//
// You can switch whether SSL is used for the connection (defaults to false).
//
Expand All @@ -68,15 +70,15 @@ public SQLCommand() {
//
// @Usage
// Use to connect to an SQL server.
// - ~sql id:name connect:localhost:3306/test username:space passwordfile:pw.txt
// - ~sql id:name connect:localhost:3306/test username:space password:<secret[sql_pw]>
//
// @Usage
// Use to connect to an SQL server over an SSL connection.
// - ~sql id:name connect:localhost:3306/test username:space passwordfile:pw.txt ssl:true
// - ~sql id:name connect:localhost:3306/test username:space password:<secret[sql_pw]> ssl:true
//
// @Usage
// Use to connect to an SQL server with a UTF8 text encoding.
// - ~sql id:name connect:localhost:3306/test?characterEncoding=utf8 username:space passwordfile:pw.txt
// - ~sql id:name connect:localhost:3306/test?characterEncoding=utf8 username:space password:<secret[sql_pw]>
//
// @Usage
// Use to update an SQL server.
Expand Down Expand Up @@ -148,7 +150,7 @@ else if (!scriptEntry.hasObject("username")
}
else if (!scriptEntry.hasObject("password")
&& arg.matchesPrefix("password")) {
scriptEntry.addObject("password", arg.asElement());
scriptEntry.addObject("password", arg.object);
}
else if (!scriptEntry.hasObject("passwordfile")
&& arg.matchesPrefix("passwordfile")) {
Expand Down Expand Up @@ -179,7 +181,7 @@ public void execute(final ScriptEntry scriptEntry) {
ElementTag action = scriptEntry.getElement("action");
final ElementTag server = scriptEntry.getElement("server");
final ElementTag username = scriptEntry.getElement("username");
final ElementTag password = scriptEntry.getElement("password");
final ObjectTag password = scriptEntry.getElement("password");
final ElementTag passwordFile = scriptEntry.getElement("passwordfile");
final ElementTag ssl = scriptEntry.getElement("ssl");
final ElementTag sqlID = scriptEntry.getElement("sqlid");
Expand All @@ -205,7 +207,12 @@ public void execute(final ScriptEntry scriptEntry) {
}
String passwordRaw;
if (password != null) {
passwordRaw = password.asString();
if (password.canBeType(SecretTag.class)) {
passwordRaw = password.asType(SecretTag.class, scriptEntry.context).getValue();
}
else {
passwordRaw = password.toString();
}
}
else {
if (passwordFile == null) {
Expand Down

0 comments on commit 56914c4

Please sign in to comment.