lexicon icon indicating copy to clipboard operation
lexicon copied to clipboard

Unable to query Cloudflare via Python

Open tiredofit opened this issue 4 years ago • 1 comments

Hi there, I've just tried to utilize the example in the examples folder for getting a list of records from cloudflare.

The following code

#!/usr/bin/python3

from lexicon.config import ConfigResolver
from lexicon.client import Client

lexicon_config = {
    "provider_name" : "cloudflare", # lexicon shortname for provider, see providers directory for available proviers
    "action": "list", # create, list, update, delete
    "domain": "example.com", # domain name
    "type": "CNAME", # specify a type for record filtering, case sensitive in some cases.
    "cloudflare": {
            "auth-token": "12345678",
            "auth-username": "[email protected]",
    }
}

config = ConfigResolver()
config.with_env().with_dict(dict_object=lexicon_config)               
client = Client(config)
results = client.execute()
print (results)

throws:

Traceback (most recent call last):
  File "./fuck", line 20, in <module>
    results = client.execute()
  File "/usr/lib/python3.8/site-packages/lexicon/client.py", line 81, in execute
    self.provider.authenticate()
  File "/usr/lib/python3.8/site-packages/lexicon/providers/base.py", line 74, in authenticate
    return self._authenticate()
  File "/usr/lib/python3.8/site-packages/lexicon/providers/cloudflare.py", line 52, in _authenticate
    payload = self._get("/zones", {"name": self.domain, "status": "active"})
  File "/usr/lib/python3.8/site-packages/lexicon/providers/base.py", line 159, in _get
    return self._request("GET", url, query_params=query_params)
  File "/usr/lib/python3.8/site-packages/lexicon/providers/cloudflare.py", line 204, in _request
    response.raise_for_status()
  File "/usr/lib/python3.8/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.cloudflare.com/client/v4/zones?name=example.com&status=active

Yet when I use the CLI

lexicon cloudflare --log_level DEBUG --auth-username [email protected] --auth-token 12345678 list example.com CNAME

It provides the list as expected. Can you advise what I may be missing?

tiredofit avatar Aug 24 '20 20:08 tiredofit

If you are using an API Token you do not need to provide a username according to the documentation here. For example I solved his by only using the environment variable LEXICON_CLOUDFLARE_AUTH_TOKEN.

versi786 avatar Dec 05 '20 01:12 versi786