apache / incubator-hugegraph

A graph database that supports more than 100+ billion data, high performance and scalability (Include OLTP Engine & REST-API & Backends)
https://hugegraph.apache.org
Apache License 2.0
2.58k stars 512 forks source link

[Question] CypherAPI #2545

Open xyk98 opened 1 month ago

xyk98 commented 1 month ago

Problem Type (问题类型)

server status (启动/运行异常)

Before submit

Environment (环境信息)

Your Question (问题描述)

After deploying hugegraph and hugegraph-hubble via docker, running the example using CypherAPI on the swagger page reported an error.

CypherAPI [GET] Input: graph:hugegraph cypher:match(n:person) return n.name as name order by n.name limit 1

Error message:

Curl: curl -X 'GET' \ 'http://172.18.30.240:18080/graphs/hugegraph/cypher?cypher=match%28n%3Aperson%29%20return%20n.name%20as%20name%20order%20by%20n.name%20limit%201' \ -H 'accept: application/json;charset=UTF-8'

Request URL: http://172.18.30.240:18080/graphs/hugegraph/cypher?cypher=match%28n%3Aperson%29%20return%20n.name%20as%20name%20order%20by%20n.name%20limit%201

Response body: { "exception": "class jakarta.ws.rs.NotAuthorizedException", "message": "HTTP 401 Unauthorized", "cause": "" }

docker-compose.yml: version: '3' services: hugegraph-server: image: hugegraph/hugegraph:1.2.0 container_name: hugegraph restart: always environment:

Vertex/Edge example (问题点 / 边数据举例)

No response

Schema [VertexLabel, EdgeLabel, IndexLabel] (元数据结构)

No response

dosubot[bot] commented 1 month ago

To resolve the HTTP 401 Unauthorized error when running the CypherAPI on the Swagger page after deploying HugeGraph and HugeGraph-Hubble via Docker, you need to ensure that the correct authorization headers are being sent with your requests. The CypherAPI class expects either a Basic or Bearer token for authorization.

Steps to Resolve

  1. Basic Authentication:

    • Encode your username and password in Base64 format.
    • Add the Authorization header with the value Basic <encoded-credentials>.
  2. Bearer Token Authentication:

    • Obtain a valid token.
    • Add the Authorization header with the value Bearer <token>.

Example for Basic Authentication

If your username is user and your password is password, you need to encode user:password in Base64. The encoded string for user:password is dXNlcjpwYXNzd29yZA==.

Add the following header to your request:

Authorization: Basic dXNlcjpwYXNzd29yZA==

Example for Bearer Token Authentication

If you have a token your_token, add the following header to your request:

Authorization: Bearer your_token

Code Snippet for Adding Headers in Swagger

If you are using Swagger UI, you can add the authorization header as follows:

// Add this script to your Swagger UI initialization code
swaggerUi.api.clientAuthorizations.add(
  'basicAuth',
  new SwaggerClient.ApiKeyAuthorization('Authorization', 'Basic dXNlcjpwYXNzd29yZA==', 'header')
);

// For Bearer token
swaggerUi.api.clientAuthorizations.add(
  'bearerAuth',
  new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer your_token', 'header')
);

Ensure that you replace dXNlcjpwYXNzd29yZA== and your_token with your actual encoded credentials and token, respectively.

Additionally, verify that the authentication settings in your gremlin-server.yaml and rest-server.properties files are correctly configured. The gremlin-server.yaml file should have the following authentication settings:

authentication: {
  authenticator: org.apache.hugegraph.auth.StandardAuthenticator,
  authenticationHandler: org.apache.hugegraph.auth.WsAndHttpBasicAuthHandler,
  config: {tokens: conf/rest-server.properties}
}

Ensure that the rest-server.properties file contains the correct token configurations as referenced in the gremlin-server.yaml file. This setup ensures that the server is properly configured to handle authentication requests.

The HugeGraphAuthProxy class in HugeGraph handles various authentication and authorization tasks, ensuring that the correct permissions are verified for different operations [1][2][3].

#### Details **About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

github-actions[bot] commented 2 weeks ago

Due to the lack of activity, the current issue is marked as stale and will be closed after 20 days, any update will remove the stale label