Issue with MAAS API Authentication - Unrecognised Signature Error

Issue with MAAS API Authentication - Unrecognised Signature Error

Summary:
/MAAS/api/2.0/machines/
When attempting to interact with the MAAS API using a GET request, we encountered the following error:

DEBUG openstack_dashboard.dashboards.project.maas.views
GET response content:
Unrecognised signature: method=GET op=None

Issue Details:

  • Error Message: Unrecognised signature: method=GET op=None
  • HTTP Method Used: GET
  • Request Details: The request was made to the MAAS API endpoint but was rejected due to an unrecognized or invalid signature.

If this is a bug please open it on LP and provide all the steps to reproduce.

What’s the status code of the response? If it’s 401 you need to ensure the apikey you are using is valid

def get(self, request, *args, **kwargs):

    maas_api_url = '/MAAS/api/2.0/machines/'
     api_key = 'NCYg8FKxeKfEpgFkaw exampleapikey eBe8cWkawg5D8z'
    
     headers = {'Authorization': f'Bearer {api_key}'}

    try:
         response = requests.get(maas_api_url, headers=headers)
        response.raise_for_status()  # Raise an exception for HTTP errors
        data = response.json()
     except requests.exceptions.RequestException as e:
      # Handle request errors
     return JsonResponse({'error': str(e)}, status=500)

   return JsonResponse(data)

This is the function I used to retrieve data. The same header works for machine addition, but it throws an Unrecognised Signature Error when used with GET requests. Please answer quickly; it would be appreciated.

Please see MAAS | How to authenticate to the MAAS API . In short you should not use headers = {'Authorization': f'Bearer {api_key}'}

from django.http import JsonResponse
import logging
from oauthlib.oauth1 import SIGNATURE_PLAINTEXT
from requests_oauthlib import OAuth1Session
import requests

Configure logging

logger = logging.getLogger(name)

def fetch_maas_nodes(maas_server_ip, api_key):
“”"
Fetch nodes from the MAAS API using OAuth1 authentication.

:param maas_server_ip: IP address of the MAAS server 
:param api_key: API key in the format 'CONSUMER_KEY:CONSUMER_TOKEN:SECRET'
:return: JSON response from the MAAS API or raises an exception if the request fails
"""
# Extract credentials from the API key
consumer_key, consumer_token, secret = api_key.split(":")

# MAAS API URL
maas_host = f"http://{maas_server_ip}:5240/MAAS"
url = f"{maas_host}/api/2.0/machines/"

# Create an OAuth1 session
maas = OAuth1Session(
    consumer_key,
    resource_owner_key=consumer_token,
    resource_owner_secret=secret,
    signature_method=SIGNATURE_PLAINTEXT
)


try:
    # Perform a GET request to fetch nodes
    response = maas.get(url, params={"op": "list_allocated"})
    response.raise_for_status()  # Raise an exception for HTTP errors
    return response.json()  # Return JSON response
except requests.exceptions.RequestException as e:
    # Handle and log exceptions
    logger.error(f"Error while communicating with MAAS API: {e}")
    raise  # Re-raise the exception to be handled by the caller

def index_view(request):
if request.method == ‘GET’:
try:
maas_server_ip = “Maasserverip”
api_key = ‘examplekey:MhEY5QZPds3fWKa47g2c:examplekey’

        data = fetch_maas_nodes(maas_server_ip, api_key)
        
        logger.debug(f"API Response Data: {data}")
        
        return JsonResponse(data, safe=False)
    except Exception as e:
        logger.error(f"Failed to fetch nodes: {e}")
        return JsonResponse({'error': str(e)}, status=500)

return JsonResponse({'error': 'This view only supports GET requests'}, status=405)

<Response [200]>
I received a response with status 200, but the data returned is an empty list (DEBUG API Response Data: [];).
However, in the MAAS UI, I see that there is one data entry available under MAAS/r/machines.
please guide me if anything i missed

what’s the status of that machine?

is shows like this Ubuntu 22.04 LTS

You are asking MAAS to return the ‘allocated’ machines and you have a ‘deployed’ machine. This is why you get an empty list.

Just remove params={"op": "list_allocated"} and simply call

response = maas.get(url)

if you want to get all the machines visible to the user that owns the credentials you are using

Thankyou so much , this is working fine now

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.