Skip to content
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

#131: Add command line flag -addCauseToInternalServerError #136

Merged
merged 8 commits into from
Sep 8, 2023
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
7 changes: 4 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func main() {
var extensionRegistryURL = flag.String("extensionRegistryURL", "", "URL of the extension registry index used to find available extensions or the path of a local directory")
var serverAddress = flag.String("serverAddress", ":8080", `Server address, e.g. ":8080" (all network interfaces) or "localhost:8080" (only local interface)`)
var openAPIOutputPath = flag.String("openAPIOutputPath", "", "Generate the OpenAPI spec at the given path instead of starting the server")
var addCauseToInternalServerError = flag.Bool("addCauseToInternalServerError", false, "Add cause of internal server errors (status 500) to the error message. Don't use this in production!")
flag.Parse()
log.SetLevel(log.DebugLevel)
log.SetFormatter(&simpleFormatter{})
Expand All @@ -28,17 +29,17 @@ func main() {
panic(fmt.Sprintf("failed to generate OpenAPI to %q: %v", *openAPIOutputPath, err))
}
} else {
startServer(*extensionRegistryURL, *serverAddress)
startServer(*extensionRegistryURL, *serverAddress, *addCauseToInternalServerError)
}
}

func startServer(pathToExtensionFolder string, serverAddress string) {
func startServer(pathToExtensionFolder string, serverAddress string, addCauseToInternalServerError bool) {
log.Printf("Starting extension manager with extension folder %q", pathToExtensionFolder)
controller := extensionController.CreateWithConfig(extensionController.ExtensionManagerConfig{
ExtensionRegistryURL: pathToExtensionFolder,
ExtensionSchema: restAPI.EXTENSION_SCHEMA_NAME,
BucketFSBasePath: "/buckets/bfsdefault/default/"})
restApi := restAPI.Create(controller, serverAddress)
restApi := restAPI.Create(controller, serverAddress, addCauseToInternalServerError)
restApi.Serve()
}

Expand Down
2 changes: 1 addition & 1 deletion dependencies.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions doc/changes/changes_0.5.1.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Extension Manager 0.5.1, released 2023-??-??
# Extension Manager 0.5.1, released 2023-09-08

Code name: Support `select` parameter type

Expand All @@ -7,15 +7,17 @@ Code name: Support `select` parameter type
This release contains the following changes:
* It adds support for the `select` parameter type.
* In Integration test class `PreviousExtensionVersion` the builder property `adapterFileName` is now optional. This is useful for extensions that don't need an adapter file like Lua based virtual schemas.
* Command line flag `-addCauseToInternalServerError` of the standalone server now allows adding the root cause error message to internal server errors (status 500).

This is helpful during debugging because the error message contains the actual root cause and you don't need to check the log for errors. The Java integration test framework `extension-manager-integration-test-java` enables this flag automatically.

⚠️Warning⚠️: Do not enable this flag in production environments as this might leak internal information.

## Features

* #132: Added support for the `select` parameter type
* #134: Made adapter file optional for `PreviousExtensionVersion`

## Documentation

* #129: Improved description of deployment process
* #131: Added command line flag `-addCauseToInternalServerError`

## Dependency Updates

Expand All @@ -41,6 +43,7 @@ This release contains the following changes:
#### Test Dependency Updates

* Updated `org.mockito:mockito-junit-jupiter:5.4.0` to `5.5.0`
* Updated `org.slf4j:slf4j-jdk14:2.0.7` to `2.0.9`

#### Plugin Dependency Updates

Expand Down
2 changes: 1 addition & 1 deletion extension-manager-integration-test-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>2.0.7</version>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.net.ServerSocket;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -32,8 +33,9 @@ static ExtensionManagerProcess start(final Path extensionManagerBinary, final Pa
final int port = findOpenPort();
LOGGER.info(() -> "Starting extension manager " + extensionManagerBinary + " on port " + port
+ " with extension folder " + extensionFolder + "...");
final List<String> command = List.of(extensionManagerBinary.toString(), "-extensionRegistryURL",
extensionFolder.toString(), "-serverAddress", "localhost:" + port);
final List<String> command = new ArrayList<>(List.of(extensionManagerBinary.toString(), "-extensionRegistryURL",
extensionFolder.toString(), "-serverAddress", "localhost:" + port));
addFlagIfSupported(extensionManagerBinary, "-addCauseToInternalServerError", command);

final ServerStartupConsumer serverPortConsumer = new ServerStartupConsumer();
final SimpleProcess process = SimpleProcess.start(command,
Expand All @@ -49,6 +51,22 @@ static ExtensionManagerProcess start(final Path extensionManagerBinary, final Pa
return new ExtensionManagerProcess(process, port);
}

private static void addFlagIfSupported(final Path extensionManagerBinary, final String flag,
final List<String> command) {
if (supportsFlag(extensionManagerBinary, flag)) {
command.add(flag);
}
}

static boolean supportsFlag(final Path extensionManagerBinary, final String flag) {
final String helpContent = getHelpContent(extensionManagerBinary);
return helpContent.contains(flag);
}

static String getHelpContent(final Path extensionManagerBinary) {
return SimpleProcess.start(List.of(extensionManagerBinary.toString(), "-help"), Duration.ofSeconds(3));
}

private static int findOpenPort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@ExtendWith(MockitoExtension.class)
class PreviousVersionManagerTest {

private static final String BASE_URL = "https://extensions-internal.exasol.com";
@Mock
private ExtensionManagerSetup setupMock;
@Mock
Expand All @@ -56,8 +56,8 @@ void newVersion() {

@Test
void fetchExtension() throws IOException {
final String extensionId = testee.fetchExtension(URI.create(
"https://extensions-internal.exasol.com/com.exasol/s3-document-files-virtual-schema/2.6.2/s3-vs-extension.js"));
final String extensionId = testee.fetchExtension(
URI.create(BASE_URL + "/com.exasol/s3-document-files-virtual-schema/2.6.2/s3-vs-extension.js"));
final Path file = tempDir.resolve(extensionId);
assertAll(() -> assertTrue(Files.exists(file), "file downloaded"),
() -> assertThat(Files.size(file), equalTo(20875L)));
Expand All @@ -66,7 +66,7 @@ void fetchExtension() throws IOException {

@Test
void fetchExtensionFailsForMissingFile() throws IOException {
final URI uri = URI.create("https://extensions-internal.exasol.com/no-such-file");
final URI uri = URI.create(BASE_URL + "/no-such-file");
final IllegalStateException exception = assertThrows(IllegalStateException.class,
() -> testee.fetchExtension(uri));
assertThat(exception.getMessage(),
Expand All @@ -85,8 +85,8 @@ void fetchExtensionFails() throws IOException {

@Test
void prepareBucketFsFile() throws FileNotFoundException, BucketAccessException, TimeoutException {
testee.prepareBucketFsFile(URI.create(
"https://extensions-internal.exasol.com/com.exasol/s3-document-files-virtual-schema/2.6.2/s3-vs-extension.js"),
testee.prepareBucketFsFile(
URI.create(BASE_URL + "/com.exasol/s3-document-files-virtual-schema/2.6.2/s3-vs-extension.js"),
"filename");
verify(bucketMock).uploadFile(any(), eq("filename"));
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/restAPI/apiContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"github.com/exasol/extension-manager/pkg/extensionController"
)

func NewApiContext(controller extensionController.TransactionController) *ApiContext {
return &ApiContext{Controller: controller}
func NewApiContext(controller extensionController.TransactionController, addCauseToInternalServerError bool) *ApiContext {
return &ApiContext{
Controller: controller,
addCauseToInternalServerError: addCauseToInternalServerError,
}
}

type ApiContext struct {
Controller extensionController.TransactionController
Controller extensionController.TransactionController
addCauseToInternalServerError bool
}
11 changes: 7 additions & 4 deletions pkg/restAPI/dbConnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ import (
)

type generalHandlerFunc = func(writer http.ResponseWriter, request *http.Request)
type dbHandler = func(db *sql.DB, writer http.ResponseWriter, request *http.Request)
type dbHandler = func(db *sql.DB, writer http.ResponseWriter, request *http.Request) error

func adaptDbHandler(f dbHandler) generalHandlerFunc {
func adaptDbHandler(apiContext *ApiContext, handler dbHandler) generalHandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
db, err := openDBRequest(request)
if err != nil {
HandleError(request.Context(), writer, err)
handleError(request.Context(), apiContext, writer, err)
return
}
defer closeDBRequest(db)
f(db, writer, request)
err = handler(db, writer, request)
if err != nil {
handleError(request.Context(), apiContext, writer, err)
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/restAPI/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ const (
/* [impl -> dsn~go-library~1]. */
func AddPublicEndpoints(api *openapi.API, config extensionController.ExtensionManagerConfig) error {
controller := extensionController.CreateWithConfig(config)
return addPublicEndpointsWithController(api, controller)
return addPublicEndpointsWithController(api, false, controller)
}

/* [impl -> dsn~rest-interface~1] */
/* [impl -> dsn~openapi-spec~1]. */
func addPublicEndpointsWithController(api *openapi.API, controller extensionController.TransactionController) error {
func addPublicEndpointsWithController(api *openapi.API, addCauseToInternalServerError bool, controller extensionController.TransactionController) error {
api.AddTag(TagExtension, "List and install extensions")
api.AddTag(TagInstallation, "List and uninstall installed extensions")
api.AddTag(TagInstance, "Calls to list, create and remove instances of an extension")

apiContext := NewApiContext(controller)
apiContext := NewApiContext(controller, addCauseToInternalServerError)

if err := api.Get(ListAvailableExtensions(apiContext)); err != nil {
return err
Expand Down
12 changes: 5 additions & 7 deletions pkg/restAPI/requestCreateInstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,16 @@ func CreateInstance(apiContext *ApiContext) *openapi.Post {
AddParameter("extensionId", openapi.STRING, "ID of the installed extension for which to create an instance").
AddParameter("extensionVersion", openapi.STRING, "Version of the installed extension for which to create an instance").
Add("instances"),
HandlerFunc: adaptDbHandler(handleCreateInstance(apiContext)),
HandlerFunc: adaptDbHandler(apiContext, handleCreateInstance(apiContext)),
}
}

func handleCreateInstance(apiContext *ApiContext) dbHandler {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) error {
requestBody := CreateInstanceRequest{}
err := DecodeJSONBody(writer, request, &requestBody)
if err != nil {
HandleError(request.Context(), writer, err)
return
return err
}
var parameters []extensionController.ParameterValue
for _, p := range requestBody.ParameterValues {
Expand All @@ -53,11 +52,10 @@ func handleCreateInstance(apiContext *ApiContext) dbHandler {
extensionVersion := chi.URLParam(request, "extensionVersion")
instance, err := apiContext.Controller.CreateInstance(request.Context(), db, extensionId, extensionVersion, parameters)
if err != nil {
HandleError(request.Context(), writer, err)
return
return err
}
logrus.Debugf("Created instance %q", instance)
SendJSON(request.Context(), writer, CreateInstanceResponse{InstanceId: instance.Id, InstanceName: instance.Name})
return SendJSON(request.Context(), writer, CreateInstanceResponse{InstanceId: instance.Id, InstanceName: instance.Name})
}
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/restAPI/requestDeleteInstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ func DeleteInstance(apiContext *ApiContext) *openapi.Delete {
AddParameter("extensionVersion", openapi.STRING, "The version of the installed extension for which to delete an instance").
Add("instances").
AddParameter("instanceId", openapi.STRING, "The ID of the instance to delete"),
HandlerFunc: adaptDbHandler(handleDeleteInstance(apiContext)),
HandlerFunc: adaptDbHandler(apiContext, handleDeleteInstance(apiContext)),
}
}

func handleDeleteInstance(apiContext *ApiContext) dbHandler {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) error {
extensionId := chi.URLParam(request, "extensionId")
extensionVersion := chi.URLParam(request, "extensionVersion")
instanceId := chi.URLParam(request, "instanceId")
err := apiContext.Controller.DeleteInstance(request.Context(), db, extensionId, extensionVersion, instanceId)
if err != nil {
HandleError(request.Context(), writer, err)
return
return err
}
SendNoContent(request.Context(), writer)
return SendNoContent(request.Context(), writer)
}
}
9 changes: 4 additions & 5 deletions pkg/restAPI/requestGetExtensionDetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,20 @@ func GetExtensionDetails(apiContext *ApiContext) *openapi.Get {
Add("extensions").
AddParameter("extensionId", openapi.STRING, "ID of the extension").
AddParameter("extensionVersion", openapi.STRING, "Version of the extension"),
HandlerFunc: adaptDbHandler(handleGetParameterDefinitions(apiContext)),
HandlerFunc: adaptDbHandler(apiContext, handleGetParameterDefinitions(apiContext)),
}
}

func handleGetParameterDefinitions(apiContext *ApiContext) dbHandler {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) error {
extensionId := chi.URLParam(request, "extensionId")
extensionVersion := chi.URLParam(request, "extensionVersion")
definitions, err := apiContext.Controller.GetParameterDefinitions(request.Context(), db, extensionId, extensionVersion)
if err != nil {
HandleError(request.Context(), writer, err)
return
return err
}
response := ExtensionDetailsResponse{Id: extensionId, Version: extensionVersion, ParamDefinitions: convertParamDefinitions(definitions)}
SendJSON(request.Context(), writer, response)
return SendJSON(request.Context(), writer, response)
}
}

Expand Down
10 changes: 4 additions & 6 deletions pkg/restAPI/requestInstallExtension.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ func InstallExtension(apiContext *ApiContext) *openapi.Put {
AddParameter("extensionId", openapi.STRING, "ID of the extension to install").
AddParameter("extensionVersion", openapi.STRING, "Version of the extension to install").
Add("install"),
HandlerFunc: adaptDbHandler(handleInstallExtension(apiContext)),
HandlerFunc: adaptDbHandler(apiContext, handleInstallExtension(apiContext)),
}
}

func handleInstallExtension(apiContext *ApiContext) dbHandler {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) error {
extensionId := chi.URLParam(request, "extensionId")
extensionVersion := chi.URLParam(request, "extensionVersion")
err := apiContext.Controller.InstallExtension(request.Context(), db, extensionId, extensionVersion)

if err != nil {
HandleError(request.Context(), writer, err)
return
return err
}
SendNoContent(request.Context(), writer)
return SendNoContent(request.Context(), writer)
}
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/restAPI/requestListAvailableExtensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ func ListAvailableExtensions(apiContext *ApiContext) *openapi.Get {
}},
},
Path: newPathWithDbQueryParams().Add("extensions"),
HandlerFunc: adaptDbHandler(handleListAvailableExtensions(apiContext)),
HandlerFunc: adaptDbHandler(apiContext, handleListAvailableExtensions(apiContext)),
}
}

func handleListAvailableExtensions(apiContext *ApiContext) dbHandler {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) error {
extensions, err := apiContext.Controller.GetAllExtensions(request.Context(), db)
if err != nil {
HandleError(request.Context(), writer, err)
return
return err
}
response := convertResponse(extensions)
log.Debugf("Got %d available extensions", len(response.Extensions))
SendJSON(request.Context(), writer, response)
return SendJSON(request.Context(), writer, response)
}
}

Expand Down
9 changes: 4 additions & 5 deletions pkg/restAPI/requestListInstalledExtensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ func ListInstalledExtensions(apiContext *ApiContext) *openapi.Get {
}},
},
Path: newPathWithDbQueryParams().Add("installations"),
HandlerFunc: adaptDbHandler(handleListInstalledExtensions(apiContext)),
HandlerFunc: adaptDbHandler(apiContext, handleListInstalledExtensions(apiContext)),
}
}

func handleListInstalledExtensions(apiContext *ApiContext) dbHandler {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) {
return func(db *sql.DB, writer http.ResponseWriter, request *http.Request) error {
installations, err := apiContext.Controller.GetInstalledExtensions(request.Context(), db)
if err != nil {
HandleError(request.Context(), writer, err)
return
return err
}
response := createResponse(installations)
SendJSON(request.Context(), writer, response)
return SendJSON(request.Context(), writer, response)
}
}

Expand Down
Loading