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

[BUG] Trying APIs on opensearch-project.github.io/opensearch-api-specification is broken #216

Open
dblock opened this issue Apr 3, 2024 · 12 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@dblock
Copy link
Member

dblock commented Apr 3, 2024

What is the bug?

Try invoking an API in https://opensearch-project.github.io/opensearch-api-specification/. It fails because it thinks OpenSearch is running on https://opensearch-project.github.io.

What is the expected behavior?

Connect https://opensearch-project.github.io/opensearch-api-specification/ to https://playground.opensearch.org so that APIs can be attempted.

Do you have any screenshots?

Screenshot 2024-04-03 at 11 47 05 AM

@dblock dblock added bug Something isn't working untriaged good first issue Good for newcomers and removed untriaged labels Apr 3, 2024
@prudhvigodithi
Copy link
Contributor

Hey @dblock following this Dockerfile I was able to install the plugin opensearch-api-2.12.0.0.zip and invoke the URL https://127.0.0.1:9200/_plugins/api which works fine, do you know the endpoint to get the above screen you posted in the description? with https://127.0.0.1:9200/opensearch-api-specification/ I get the error as

{"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index [opensearch-api-specification]","index":"opensearch-api-specification","resource.id":"opensearch-api-specification","resource.type":"index_or_alias","index_uuid":"_na_"}],"type":"index_not_found_exception","reason":"no such index [opensearch-api-specification]","index":"opensearch-api-specification","resource.id":"opensearch-api-specification","resource.type":"index_or_alias","index_uuid":"_na_"},"status":404}

@prudhvigodithi
Copy link
Contributor

prudhvigodithi commented Apr 7, 2024

I understood now, the swagger endpoint is coming from https://github.com/opensearch-project/opensearch-api-specification/blob/main/index.html and served with jekyll, not with opensearch-api-2.12.0.0.zip plugin.

@dblock
Copy link
Member Author

dblock commented Apr 8, 2024

@prudhvigodithi That is correct. This issue has nothing to do with the opensearch-api-2.12.0.0.zip plugin.

@prudhvigodithi
Copy link
Contributor

Thanks for confirming @dblock, how about we integrate Swagger into an opensearch-api plugin and provide comprehensive documentation with a user-friendly interface for interacting with OpenSearch API?. Once we have this it would be straight forward to install the plugin the in https://playground.opensearch.org/ and expose the Swagger UI using kubernetes ingress path.

Another approach would be updating the url https://github.com/opensearch-project/opensearch-api-specification/blob/main/index.html#L14 here with https://playground.opensearch.org/_plugins/api (make sure the opensearch-api plugin is installed) and access directly from the repo GH pages URL https://opensearch-project.github.io/opensearch-api-specification/.

Adding @bbarani @Flyingliuhub

@dblock
Copy link
Member Author

dblock commented Apr 8, 2024

Thanks for confirming @dblock, how about we integrate Swagger into an opensearch-api plugin and provide comprehensive documentation with a user-friendly interface for interacting with OpenSearch API?.

This is a really good idea. I opened dblock/opensearch-api#3.

However, to fix this particular issue I'd prefer if we made jekyll build (this code) write https://playground.opensearch.org/ in the right places and enable CORS on https://playground.opensearch.org. Seems simpler, no?

@prudhvigodithi
Copy link
Contributor

However, to fix this particular issue I'd prefer if we made jekyll build (this code) write https://playground.opensearch.org/ in the right places and enable CORS on https://playground.opensearch.org. Seems simpler, no?

Yes dB that should work as well, only thing we need to make sure is the API's part of opensearch-openapi.yaml should be same and supported by https://playground.opensearch.org/ (to avoid issues with API change with versioning). To achieve this we just need to update the CORS settings of the cluster and update the opensearch-openapi.yaml with request URL.

Option 1:

Add the following settings to playground cluster opensearch.yml, the http.cors.allow-origin can be better secured to avoid *.

http.cors.enabled : true
http.cors.allow-origin: "*"
http.cors.allow-methods: OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.allow-headers: X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization  

Update the opensearch-openapi.yaml with request URL server (example as follows)

openapi: 3.1.0
info:
  title: OpenSearch API
  description: OpenSearch API
  version: 1.0.0
servers:
  - url: http://localhost:9200 ## Replace with the playground URL
paths:
  /:
  .
  .
  .

Now with jekyll serve swagger UI should have the passed in server (playground) URL for users to test (we should handle the username and password securely during the request invoke).

Screenshot 2024-04-08 at 7 43 05 PM

Option 2:

But if the plugin opensearch-api has the swagger UI along with _plugins/api we just need to add the following in index.html to dynamically fetch the cluster API's along with swagger UI and test the API's.

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>OpenSearch API Spec</title>
    <link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" />
</head>

<body>
    <div id="swagger-ui"></div>
    <script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
    <script>
        window.onload = function () {
            // Basic Authentication credentials
            var username = 'admin';
            var password = 'myStrongPassword123@456';
            var basicAuthHeader = 'Basic ' + btoa(username + ':' + password);

            // API URL
            var apiUrl = 'http://localhost:9200/_plugins/api';
            fetch(apiUrl, {
                method: 'GET',
                headers: {
                    'Authorization': basicAuthHeader
                }
            })
            .then(function(response) {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            })
            .then(function(data) {
                // Fix paths in the API specification
                fixPaths(data);

                const ui = SwaggerUIBundle({
                    spec: data,
                    dom_id: '#swagger-ui',
                    deepLinking: true,
                    presets: [
                        SwaggerUIBundle.presets.apis,
                        SwaggerUIBundle.SwaggerUIStandalonePreset
                    ],
                    plugins: [
                        SwaggerUIBundle.plugins.DownloadUrl
                    ],
                    requestInterceptor: function(request) {
                        request.headers.Authorization = basicAuthHeader;
                        return request;
                    }
                });
                window.ui = ui;
            })
            .catch(function(error) {
                console.error('Error fetching API:', error);
            });
        }

        function fixPaths(data) {
            for (var path in data.paths) {
                var fixedPath = path.replace(/__/g, "_");
                if (fixedPath !== path) {
                    data.paths[fixedPath] = data.paths[path];
                    delete data.paths[path];
                }
            }
        }
    </script>
</body>

</html>

@dblock
Copy link
Member Author

dblock commented Apr 9, 2024

I like option 1. The spec is compatible with OpenSearch 2.x.

Option 2 requires changes to the plugin, the plugin to be installed, and so on, which seems like a lot of work for something we can achieve with option 1.

@dblock
Copy link
Member Author

dblock commented Apr 9, 2024

@prudhvigodithi Are you going to try and implement this?

@prudhvigodithi
Copy link
Contributor

Once we have the CORS enabled and a secure credential for authentication from https://playground.opensearch.org/ we can then come back to modify the opensearch-api-specification swagger setup to talk to playground. @dblock I can contribute to opensearch-api-specification swagger setup but need some changes from https://playground.opensearch.org/ cluster, adding @Flyingliuhub to take a look.
@bbarani.

@dblock dblock removed the untriaged label Apr 15, 2024
@dblock
Copy link
Member Author

dblock commented Jun 7, 2024

@prudhvigodithi @Flyingliuhub Is this something that we can do? Is there a GitHub issue for CORS/auth?

@Flyingliuhub
Copy link
Member

@prudhvigodithi @Flyingliuhub Is this something that we can do? Is there a GitHub issue for CORS/auth?

@prudhvigodithi , let's discuss and see what we can do here.

@prudhvigodithi
Copy link
Contributor

Sure @Flyingliuhub.

@dblock dblock changed the title [BUG] Trying APIs on https://opensearch-project.github.io/opensearch-api-specification/ is broken [BUG] Trying APIs on opensearch-project.github.io/opensearch-api-specification is broken Jul 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants