-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1351 from akto-api-security/feature/pre_script
Feature/pre script
- Loading branch information
Showing
9 changed files
with
469 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
apps/dashboard/src/main/java/com/akto/action/testing/ScriptAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.akto.action.testing; | ||
|
||
import java.util.UUID; | ||
|
||
import org.apache.commons.lang3.NotImplementedException; | ||
import org.bson.conversions.Bson; | ||
|
||
import com.akto.action.UserAction; | ||
import com.akto.dao.context.Context; | ||
import com.akto.dao.testing.config.TestScriptsDao; | ||
import com.akto.dto.testing.config.TestScript; | ||
import com.akto.util.DashboardMode; | ||
import com.mongodb.BasicDBObject; | ||
import com.mongodb.client.model.Filters; | ||
import com.mongodb.client.model.Updates; | ||
import com.opensymphony.xwork2.Action; | ||
|
||
public class ScriptAction extends UserAction { | ||
|
||
@Override | ||
public String execute() throws Exception { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private TestScript testScript; | ||
|
||
public String addScript() { | ||
|
||
if (!DashboardMode.isSaasDeployment()) { | ||
return Action.ERROR.toUpperCase(); | ||
} | ||
if (this.testScript == null || this.testScript.getJavascript() == null) { | ||
return Action.ERROR.toUpperCase(); | ||
} | ||
|
||
TestScriptsDao.instance.insertOne( | ||
new TestScript( | ||
UUID.randomUUID().toString(), | ||
this.testScript.getJavascript(), | ||
TestScript.Type.PRE_REQUEST, | ||
getSUser().getLogin(), | ||
Context.now() | ||
) | ||
); | ||
|
||
return Action.SUCCESS.toUpperCase(); | ||
} | ||
|
||
public String fetchScript() { | ||
this.testScript = TestScriptsDao.instance.findOne(new BasicDBObject()); | ||
return Action.SUCCESS.toUpperCase(); | ||
} | ||
|
||
public String updateScript() { | ||
|
||
if (!DashboardMode.isSaasDeployment()) { | ||
return Action.ERROR.toUpperCase(); | ||
} | ||
|
||
if (this.testScript == null || this.testScript.getJavascript() == null) { | ||
return Action.ERROR.toUpperCase(); | ||
} | ||
|
||
Bson filterQ = Filters.eq("_id", testScript.getId()); | ||
Bson updateQ = | ||
Updates.combine( | ||
Updates.set(TestScript.JAVASCRIPT, this.testScript.getJavascript()), | ||
Updates.set(TestScript.AUTHOR, getSUser().getLogin()), | ||
Updates.set(TestScript.LAST_UPDATED_AT, Context.now()) | ||
); | ||
TestScriptsDao.instance.updateOne(filterQ, updateQ); | ||
return Action.SUCCESS.toUpperCase(); | ||
} | ||
|
||
public TestScript getTestScript() { | ||
return testScript; | ||
} | ||
|
||
public void setTestScript(TestScript testScript) { | ||
this.testScript = testScript; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
libs/dao/src/main/java/com/akto/dao/testing/config/TestScriptsDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.akto.dao.testing.config; | ||
|
||
import com.akto.dao.*; | ||
import com.akto.dto.testing.config.TestScript; | ||
import com.mongodb.BasicDBObject; | ||
|
||
public class TestScriptsDao extends AccountsContextDao<TestScript> { | ||
|
||
public static final TestScriptsDao instance = new TestScriptsDao(); | ||
|
||
private TestScriptsDao() {} | ||
|
||
public TestScript fetchTestScript() { | ||
return TestScriptsDao.instance.findOne(new BasicDBObject()); | ||
} | ||
|
||
@Override | ||
public String getCollName() { | ||
return "test_collection_properties"; | ||
} | ||
|
||
@Override | ||
public Class<TestScript> getClassT() { | ||
return TestScript.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.