AWS Keyspace(Cassandra) api error

0

I have been trying to get a list of keyspaces name using cassandra api but it throws an error.

GET : https://cassandra.us-east-1.amazonaws.com

Error: <UnknownOperationException/> Status Code: 404

Note: AWS Access Credentials has all the admin permissions to perform the action.

1 Answer
1

If you mean you have configured AWS Access Credentials on your system by using the AWS cli to run aws configure, you can list Keyspaces with the command aws keyspaces list-keyspaces. You can also use an AWS SDK to list your keyspaces, such as the boto3 SDK for Python.

You appear to want to list keyspaces using the REST API, which is possible. You can use the API for data language definition (DDL) operations. This API reference describes the supported DDL operations in detail. https://docs.aws.amazon.com/keyspaces/latest/APIReference/Welcome.html

From there, you can find a list of supported Actions, including "ListKeyspaces" https://docs.aws.amazon.com/keyspaces/latest/APIReference/API_Operations.html

But if you're not using the CLI or an SDK, you'll need to sign your requests manually, which is not trival. https://docs.aws.amazon.com/general/latest/gr/signing-aws-api-requests.html

Here is an example in Python

import json
import pprint
import urllib3


headers = {
    "X-Amz-Target": "KeyspacesService.ListKeyspaces",
    "Content-Type": "application/x-amz-json-1.0",
    "X-Amz-Date": "20230214T153319Z",
    "Authorization": f"AWS4-HMAC-SHA256 Credential={my_access_key}/20230214/us-east-2/cassandra/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature={my_signature}"
}

host = "cassandra.us-east-2.amazonaws.com"

http = urllib3.PoolManager()
r = http.request("POST", url=f"https://{host}", headers=headers)

pprint.pprint(json.loads(r.data.decode("utf-8")))

Here it is with the Python boto3 client

import boto3


client = boto3.client(
    "keyspaces",
    endpoint_url="https://cassandra.us-east-2.amazonaws.com",
    # endpoint_url='http://localhost:8000/',
)

response = client.list_keyspaces()

And if I swap-in localhost for the endpoint url and run a server locally to dump the headers, I can see these are the headers being sent to the Keyspaces API.

ERROR:root:Host: localhost:8000
Accept-Encoding: identity
X-Amz-Target: KeyspacesService.ListKeyspaces
Content-Type: application/x-amz-json-1.0
User-Agent: Boto3/1.26.70 Python/3.9.6 Darwin/21.6.0 Botocore/1.29.70
X-Amz-Date: 20230214T153319Z
Authorization: AWS4-HMAC-SHA256 Credential=<my_access_key>/20230214/us-east-2/cassandra/aws4_request, SignedHeaders=content-type;host;x-amz-date;x-amz-target, Signature=<my_signature>
amz-sdk-invocation-id: 20f45727-43a6-4f13-bbac-40502747a673
amz-sdk-request: attempt=5; max=5
Content-Length: 2
AWS
answered 2 years ago
  • Thank you for responding, Paul. I'll elaborate on the question. I've previously read those docs, and it works flawlessly with the AWS CLI and SDK, but not with the REST API. On referring to the following Document, this is the REST API: https://cassandra.us-east-1.amazonaws.com connected with AWS Keyspaces. https://docs.aws.amazon.com/general/latest/gr/keyspaces.html

    However, when I try to hit it with the signed request, it fails and produces an error Error: UnknownOperationException/>. The status code is 404. (I've tried the same with RDS, RedShift, & DocumentDB, and everything works except the Cassandra API.)

  • Oh I see. I've posted a Python example in case it's useful? I saw the UnknownOperationException error until I added the X-Amz-Target header. Not sure if this will apply to you though. Otherwise I'm stumped. Good luck!

  • Thank you for your thoughts, Paul. I tested it using your Python code as well as Postman and encountered the same error.

  • Yeah my pleasure. I'm sorry we didn't get there. I'm posting the boto3 code and even the headers its sending above in case it helps anyone, including future me. Also this is the boto3 signing code for reference. https://github.com/boto/botocore/blob/develop/botocore/signers.py

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions