-
Notifications
You must be signed in to change notification settings - Fork 3
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 #17 from agorapulse/feature/auth-for-sandbox
auth for sandbox
- Loading branch information
Showing
11 changed files
with
272 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:root-dir: {asciidoctorconfigdir} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:includedir: {asciidoctorconfigdir} |
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
54 changes: 54 additions & 0 deletions
54
...console/src/main/groovy/com/agorapulse/micronaut/console/function/AuthConsoleHandler.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,54 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020-2021 Agorapulse. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.agorapulse.micronaut.console.function; | ||
|
||
import com.agorapulse.micronaut.console.ConsoleConfiguration; | ||
import com.agorapulse.micronaut.console.ConsoleService; | ||
import com.agorapulse.micronaut.console.Script; | ||
import com.agorapulse.micronaut.console.util.ExceptionSanitizer; | ||
import io.micronaut.context.ApplicationContext; | ||
import io.micronaut.function.FunctionBean; | ||
import io.micronaut.function.executor.FunctionInitializer; | ||
|
||
import javax.inject.Inject; | ||
import java.util.function.Function; | ||
|
||
@FunctionBean(value = "auth-console", method = "apply") | ||
public class AuthConsoleHandler extends FunctionInitializer implements Function<AuthorizedScript, String> { | ||
|
||
@Inject private ConsoleService service; | ||
@Inject private ConsoleConfiguration configuration; | ||
@Inject private ExceptionSanitizer sanitizer; | ||
|
||
@Override | ||
public String apply(AuthorizedScript authorizedScript) { | ||
Script script = new Script(configuration.getLanguage(), authorizedScript.getBody(), authorizedScript.getUser().toUser()); | ||
try { | ||
return service.execute(script).toString(); | ||
} catch (Throwable th) { | ||
return sanitizer.extractMessage(th) + "\n\nScript:\n" + script; | ||
} | ||
} | ||
|
||
public AuthConsoleHandler() { } | ||
|
||
public AuthConsoleHandler(ApplicationContext applicationContext) { | ||
super(applicationContext); | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
...t-console/src/main/groovy/com/agorapulse/micronaut/console/function/AuthorizedScript.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,79 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020-2021 Agorapulse. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.agorapulse.micronaut.console.function; | ||
|
||
import com.agorapulse.micronaut.console.User; | ||
import io.micronaut.core.annotation.Introspected; | ||
|
||
@Introspected | ||
public class AuthorizedScript { | ||
|
||
static class Executor { | ||
private String id; | ||
private String name; | ||
private String address; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getAddress() { | ||
return address; | ||
} | ||
|
||
public void setAddress(String address) { | ||
this.address = address; | ||
} | ||
|
||
public User toUser() { | ||
return new User(id, name, address); | ||
} | ||
} | ||
|
||
private String body; | ||
private Executor user = new Executor(); | ||
|
||
public String getBody() { | ||
return body; | ||
} | ||
|
||
public void setBody(String body) { | ||
this.body = body; | ||
} | ||
|
||
public Executor getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(Executor user) { | ||
this.user = user; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...e/src/test/groovy/com/agorapulse/micronaut/console/function/AuthConsoleHandlerSpec.groovy
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,57 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020-2021 Agorapulse. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.agorapulse.micronaut.console.function | ||
|
||
import com.agorapulse.testing.fixt.Fixt | ||
import groovy.transform.CompileDynamic | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
@CompileDynamic | ||
class AuthConsoleHandlerSpec extends Specification { | ||
|
||
@Shared @AutoCleanup AuthConsoleHandler handler = new AuthConsoleHandler() | ||
|
||
Fixt fixt = Fixt.create(ConsoleHandlerSpec) | ||
|
||
void 'execute simple groovy script'() { | ||
expect: | ||
handler.apply(script('"Hello World"')) == 'Hello World' | ||
} | ||
|
||
void 'execute script with prints'() { | ||
expect: | ||
handler.apply(script(fixt.readText('prints.groovy'))) == fixt.readText('prints.txt') | ||
} | ||
|
||
void 'execute script with exception'() { | ||
given: | ||
String errorResult = handler.apply(script(fixt.readText('error.groovy'))) | ||
expect: | ||
errorResult.startsWith('com.agorapulse.micronaut.console.ConsoleException: Exception during script execution') | ||
} | ||
|
||
private AuthorizedScript script(String body) { | ||
return new AuthorizedScript( | ||
body: body, | ||
user: new AuthorizedScript.Executor(id: 'executor', name: 'Script Executor', address: '/10.0.0.1') | ||
) | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...e/src/test/resources/com/agorapulse/micronaut/console/AuthConsoleHandlerSpec/error.groovy
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 @@ | ||
throw new RuntimeException("Does not work!") |
4 changes: 4 additions & 0 deletions
4
.../src/test/resources/com/agorapulse/micronaut/console/AuthConsoleHandlerSpec/prints.groovy
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,4 @@ | ||
println 'Hello Print' | ||
|
||
"Hello Result" | ||
|
5 changes: 5 additions & 0 deletions
5
...ole/src/test/resources/com/agorapulse/micronaut/console/AuthConsoleHandlerSpec/prints.txt
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,5 @@ | ||
# Out # | ||
Hello Print | ||
|
||
# Result # | ||
Hello Result |