Giter Site home page Giter Site logo

cloudflare / python-cloudflare Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cloudflare-api/python-cloudflare-v4

673.0 26.0 158.0 1.02 MB

Python wrapper for the Cloudflare Client API v4

License: MIT License

Python 96.65% Makefile 1.72% Roff 1.63%

python-cloudflare's Introduction

cloudflare-python

Warning

Soon there will be two Python packages for accessing Cloudflare's API.

  1. This original package, which was initially introduced here.
  2. A ground-up rewrite of the SDK, released under 3.*, at some point in the future. See here

If you like using this package in it's present form, it is highly recommended that you pin to the 2.* releases now.

$ cat ${YOUR_PROJECT}/requirements.txt
cloudflare==2.19.*
$

For manual upgrades; the following will work cleanly:

$ pip install --upgrade cloudflare==2.19.*
...
Successfully installed cloudflare-2.19.4
$

Warning

Release 2.20.* is now available and it will produce a warning message explaining all this via stderr (the standard error output). This messages does not stop the program from operating, it's just a warning. If you wish to surpress this message (which is a bad idea because pinning to 2.19.* is the right thing to do), then do the following in your code:

    cf = CloudFlare.CloudFlare(..., warnings=False)

Or, if you use cli4, then the following.

$ cli4 -w False ...

Warning

Release 3.* will not be code-compatible/call-compatible with previous releases (i.e. release 1.* and 2.*).

When you see this README complete change you will know that 3.* has been released; however, until then, this code will be released under a 2.19.* release number.

Package stats

Downloads Downloads Downloads Downloads Downloads

Instant how-to-use example

If you want to call the following API call:

    https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_record_id}

It would translates to the following Python code:

    results = cf.zones.dns_records(zone_id, dns_record_id)

Many more examples are below and/or in the examples folder.

Installation

Two methods are provided to install this software. Use PyPi (see package details) or GitHub (see package details).

Via PyPI

$ sudo pip install cloudflare
$

Yes - that simple! (the sudo may not be needed in some cases).

Via github

$ git clone https://github.com/cloudflare/python-cloudflare
$ cd python-cloudflare
$ ./setup.py build
$ sudo ./setup.py install
$

Or whatever variance of that you want to use. There is a Makefile included.

Cloudflare name change - dropping the capital F

In Sepember/October 2016 the company modified its company name and dropped the capital F. However, for now (and for backward compatibility reasons) the class name stays the same.

Cloudflare API version 4

The Cloudflare API can be found here. Each API call is provided via a similarly named function within the CloudFlare class. A full list is provided below.

Example code

All example code is available on GitHub (see package in the examples folder).

Blog

This package was initially introduced here via Cloudflare's blog.

Getting Started

A very simple listing of zones within your account; including the IPv6 status of the zone.

import CloudFlare

def main():
    cf = CloudFlare.CloudFlare()
    zones = cf.zones.get()
    for zone in zones:
        zone_id = zone['id']
        zone_name = zone['name']
        print("zone_id=%s zone_name=%s" % (zone_id, zone_name))

if __name__ == '__main__':
    main()

This example works when there are less than 50 zones (50 is the default number of values returned from a query like this).

Now lets expand on that and add code to show the IPv6 and SSL status of the zones. Lets also query 100 zones.

import CloudFlare

def main():
    cf = CloudFlare.CloudFlare()
    zones = cf.zones.get(params = {'per_page':100})
    for zone in zones:
        zone_id = zone['id']
        zone_name = zone['name']

        settings_ssl = cf.zones.settings.ssl.get(zone_id)
        ssl_status = settings_ssl['value']

        settings_ipv6 = cf.zones.settings.ipv6.get(zone_id)
        ipv6_status = settings_ipv6['value']

        print("zone_id=%s zone_name=%s" % (zone_id, zone_name))
        print("ssl_status=%s ipv6_status=%s" % (ssl_status, ipv6_status))

if __name__ == '__main__':
    main()

In order to query more than a single page of zones, we would have to use the raw mode (described more below). We can loop over many get calls and pass the page parameter to facilitate the paging.

Raw mode is only needed when a get request has the possibility of returning many items.

import CloudFlare

def main():
    cf = CloudFlare.CloudFlare(raw=True)
    page_number = 0
    while True:
        page_number += 1
        raw_results = cf.zones.get(params={'per_page':5,'page':page_number})
        zones = raw_results['result']

        for zone in zones:
            zone_id = zone['id']
            zone_name = zone['name']
            print("zone_id=%s zone_name=%s" % (zone_id, zone_name))

        total_pages = raw_results['result_info']['total_pages']
        if page_number == total_pages:
            break

if __name__ == '__main__':
    main()

A more complex example follows.

import CloudFlare

def main():
    zone_name = 'example.com'

    cf = CloudFlare.CloudFlare()

    # query for the zone name and expect only one value back
    try:
        zones = cf.zones.get(params = {'name':zone_name,'per_page':1})
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/zones.get %d %s - api call failed' % (e, e))
    except Exception as e:
        exit('/zones.get - %s - api call failed' % (e))

    if len(zones) == 0:
        exit('No zones found')

    # extract the zone_id which is needed to process that zone
    zone_id = zones[0]['id']

    # request the DNS records from that zone
    try:
        dns_records = cf.zones.dns_records.get(zone_id)
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('/zones/dns_records.get %d %s - api call failed' % (e, e))

    # print the results - first the zone name
    print("zone_id=%s zone_name=%s" % (zone_id, zone_name))

    # then all the DNS records for that zone
    for dns_record in dns_records:
        r_name = dns_record['name']
        r_type = dns_record['type']
        r_value = dns_record['content']
        r_id = dns_record['id']
        print('\t', r_id, r_name, r_type, r_value)

    exit(0)

if __name__ == '__main__':
    main()

Providing Cloudflare Username and API Key

When you create a CloudFlare class you can pass some combination of these four core parameters.

  • email - The account email (only if an API Key is being used)
  • api - The API Key (if coding prior to Issue-114 being merged)
  • token - The API Token (if coding after to Issue-114)
  • certtoken - Optional Origin-CA Certificate Token

This parameter controls how the data is returned from a successful call (see notes below).

  • raw - An optional Raw flag (True/False) - defaults to False

Timeouts (10s) and Retries (5) are configured by default. Should you wish to override them, use these settings:

  • global_request_timeout - How long before each API call to Cloudflare should time out (in seconds)
  • max_requests_retries - How many times to retry an API call when DNS lookups, socket connections, or connect timeouts occur.

NOTE: max_request_retries is only available when use_sessions is not disabled.

The following paramaters are for debug and/or development usage

  • debug - An optional Debug flag (True/False) - defaults to False
  • use_sessions - An optional Use-Sessions flag (True/False) - defaults to True
  • profile - An optional Profile name (the default is Cloudflare)
  • base_url - An optional Base URL (only used for development)

email=None, key=None, token=None, certtoken=None, debug=False, raw=False, use_sessions=True, profile=None, base_url=None):

Issue-114

After Issue-114 was coded and merged, the use of token and key changed; however, is backward compatible (amazingly!).

If you are using only the API Token, then don't include the API Email. If you are coding prior to Issue-114, then the API Key can also be used as an API Token if the API Email is not used.

Python code to create class

import CloudFlare

# A minimal call - reading values from environment variables or configuration file
cf = CloudFlare.CloudFlare()

# A minimal call with debug enabled
cf = CloudFlare.CloudFlare(debug=True)

# An authenticated call using an API Token (note the missing email)
cf = CloudFlare.CloudFlare(token='00000000000000000000000000000000')

# An authenticated call using an API Email and API Key
cf = CloudFlare.CloudFlare(email='[email protected]', key='00000000000000000000000000000000')

# An authenticated call using an API Token and CA-Origin info
cf = CloudFlare.CloudFlare(token='00000000000000000000000000000000', certtoken='v1.0-...')

# An authenticated call using an API Email, API Key, and CA-Origin info
cf = CloudFlare.CloudFlare(email='[email protected]', key='00000000000000000000000000000000', certtoken='v1.0-...')

# An authenticated call using using a stored profile (see below)
cf = CloudFlare.CloudFlare(profile="CompanyX"))

If the account email and API key are not passed when you create the class, then they are retrieved from either the users exported shell environment variables or the .cloudflare.cfg or ~/.cloudflare.cfg or ~/.cloudflare/cloudflare.cfg files, in that order.

If you're using an API Token, any cloudflare.cfg file must either not contain an email and key attribute (or they can be zero length strings) and the CLOUDFLARE_EMAIL CLOUDFLARE_API_KEY environment variable must be unset (or zero length strings), otherwise the token (CLOUDFLARE_API_TOKEN or token attribute) will not be used.

There is one call that presently doesn't need any email or token certification (the /ips call); hence you can test without any values saved away.

Using shell environment variables

Note (for latest version of code):

  • CLOUDFLARE_EMAIL has replaced CF_API_EMAIL.
  • CLOUDFLARE_API_KEY has replaced CF_API_KEY.
  • CLOUDFLARE_API_TOKEN has replaced CF_API_TOKEN.
  • CLOUDFLARE_API_CERTKEY has replaced CF_API_CERTKEY.

Additionally, these two variables are available for testing purposes:

  • CLOUDFLARE_API_EXTRAS has replaced CF_API_EXTRAS.
  • CLOUDFLARE_API_URL has replaced CF_API_URL.

The older environment variable names can still be used.

$ export CLOUDFLARE_EMAIL='[email protected]'
$ export CLOUDFLARE_API_KEY='00000000000000000000000000000000'
$ export CLOUDFLARE_API_CERTKEY='v1.0-...'
$

Or if using API Token.

$ export CLOUDFLARE_API_TOKEN='00000000000000000000000000000000'
$ export CLOUDFLARE_API_CERTKEY='v1.0-...'
$

These are optional environment variables; however, they do override the values set within a configuration file.

Using configuration file to store email and keys

The default profile name is Cloudflare for obvious reasons.

$ cat ~/.cloudflare/cloudflare.cfg
[Cloudflare]
email = [email protected] # Do not set if using an API Token
key = 00000000000000000000000000000000
certtoken = v1.0-...
extras =
$

More than one profile can be stored within that file. Here's an example for a work and home setup (in this example work has an API Token and home uses email/key).

$ cat ~/.cloudflare/cloudflare.cfg
[Work]
token = 00000000000000000000000000000000
[Home]
email = [email protected]
key = 00000000000000000000000000000000
$

To select a profile, use the --profile profile-name option for cli4 command or use profile="profile-name" in the library call.

$ cli4 --profile Work /zones | jq '.[]|.name' | wc -l
      13
$

$ cli4 --profile Home /zones | jq '.[]|.name' | wc -l
       1
$

Here is the same in code.

#!/usr/bin/env python

import CloudFlare

def main():
    cf = CloudFlare.CloudFlare(profile="Work")
    ...

Passing your own HTTP headers to API calls

There are very specific case where a user of the library needs to add custom headers to all HTTP calls. This is rarly needed.

The addition headers can be passed via the confuration file as follows:

$ cat ~/.cloudflare/cloudflare.cfg
...
http_headers =
        X-Header1:value
        X-Header2: value1 value2 value3
        X-Header3: "this is life as we know it"
        X-Header4: 'two single quotes'
        X-Header5:
...
$

Each line should have a header noun, a colon, and a verb.

You can also pass these via Python calls.

    import CloudFlare

    http_headers = [
        'X-Header1:value',
        'X-Header2: value1 value2 value3',
        'X-Header3: "this is life as we know it"',
        'X-Header4: \'two single quotes\'',
        'X-Header5:',
    ]
    cf = CloudFlare.CloudFlare(http_headers=http_headers)
...

These header values can also be passed via cli4 command (many times) - use the -v option to see the debug messages:

$ cli4 -v --header 'X-something:' --header 'X-whatever:whatever' /zones > /tmp/results.json
...
            --header "X-something: " \
            --header "X-whatever: whatever " \
...
$

Advanced use of configuration file for authentication based on method

The configuration file can have values that are both generic and specific to the method. Here's an example where a project has a different API Token for reading and writing values.

$ cat ~/.cloudflare/cloudflare.cfg
[Work]
token = 0000000000000000000000000000000000000000
token.get = 0123456789012345678901234567890123456789
$

When a GET call is processed then the second token is used. For all other calls the first token is used. Here's a more explict verion of that config:

$ cat ~/.cloudflare/cloudflare.cfg
[Work]
token.delete = 0000000000000000000000000000000000000000
token.get = 0123456789012345678901234567890123456789
token.patch = 0000000000000000000000000000000000000000
token.post = 0000000000000000000000000000000000000000
token.put = 0000000000000000000000000000000000000000
$

This can be used with email values also.

About /certificates and certtoken

The CLOUDFLARE_API_CERTKEY or certtoken values are used for the Origin-CA /certificates API calls. You can leave certtoken in the configuration with a blank value (or omit the option variable fully).

The extras values are used when adding API calls outside of the core codebase. Technically, this is only useful for internal testing within Cloudflare. You can leave extras in the configuration with a blank value (or omit the option variable fully).

Exceptions and return values

Response data

The response is build from the JSON in the API call. It contains the results values; but does not contain the paging values.

You can return all the paging values by calling the class with raw=True. Here's an example without paging.

#!/usr/bin/env python

import json
import CloudFlare

def main():
    cf = CloudFlare.CloudFlare()
    zones = cf.zones.get(params={'per_page':5})
    print("len=%d" % (zones.length()))

if __name__ == '__main__':
    main()

The results are as follows.

5

When you add the raw option; the APIs full structure is returned. This means the paging values can be seen.

#!/usr/bin/env python

import json
import CloudFlare

def main():
    cf = CloudFlare.CloudFlare(raw=True)
    zones = cf.zones.get(params={'per_page':5})
    print("len=%d" % (zones.length()))
    print(json.dumps(zones, indent=4, sort_keys=True))

if __name__ == '__main__':
    main()

This produces.

5
{
    "result": [
        ...
    ],
    "result_info": {
        "count": 5,
        "page": 1,
        "per_page": 5,
        "total_count": 31,
        "total_pages": 7
    }
}

A full example of paging is provided below.

Exceptions

The library will raise CloudFlareAPIError when the API call fails. The exception returns both an integer and textual message in one value.

import CloudFlare

    ...
    try
        r = ...
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        exit('api error: %d %s' % (e, e))
    ...

The other raised response is CloudFlareInternalError which can happen when calling an invalid method.

In some cases more than one error is returned. In this case the return value e is also an array. You can iterate over that array to see the additional error.

import sys
import CloudFlare

    ...
    try
        r = ...
    except CloudFlare.exceptions.CloudFlareAPIError as e:
        if len(e) > 0:
            sys.stderr.write('api error - more than one error value returned!\n')
            for x in e:
                sys.stderr.write('api error: %d %s\n' % (x, x))
        exit('api error: %d %s' % (e, e))
    ...

Exception handling

Here's code using the CLI command cli4 of the responses passed back in exceptions.

First a simple get with a clean (non-error) response.

$ cli4 /zones/:example.com/dns_records | jq -c '.[]|{"name":.name,"type":.type,"content":.content}'
{"name":"example.com","type":"MX","content":"something.example.com"}
{"name":"something.example.com","type":"A","content":"10.10.10.10"}
$

Next a simple/single error response. This is simulated by providing incorrect authentication information.

$ CLOUDFLARE_EMAIL='[email protected]' cli4 /zones/
cli4: /zones - 9103 Unknown X-Auth-Key or X-Auth-Email
$

More than one call can be done on the same command line. In this mode, the connection is preserved between calls.

$ cli4 /user/organizations /user/invites
...
$

Note that the output is presently two JSON structures one after the other - so less useful that you may think.

Finally, a command that provides more than one error response. This is simulated by passing an invalid IPv4 address to a DNS record creation.

$ cli4 --post name='foo' type=A content="NOT-A-VALID-IP-ADDRESS" /zones/:example.com/dns_records
cli4: /zones/:example.com/dns_records - 9005 Content for A record is invalid. Must be a valid IPv4 address
cli4: /zones/:example.com/dns_records - 1004 DNS Validation Error
$

Included example code

The examples folder contains many examples in both simple and verbose formats.

You can see the installed path of these files directly via cli4 -e (or cli4 --examples) command.

$ cli4 -e
Python .py files:
	...
	/opt/homebrew/lib/python3.11/site-packages/examples/example_always_use_https.py
	...
Bash .sh files:
	...
	/opt/homebrew/lib/python3.11/site-packages/examples/example_paging_thru_zones.sh
	...
$

The exact path will vary depending on your system. The above example is MacOS and Python 3.9 hence the /opt/homebrew/lib/python3.11/site-packages/ path. One Linux, the Python pip command may install the code is a system location like /usr/lib/python3/dist-packages or ~/.local/lib/python3.9/site-packages/ or something different. The cli4 -e command will try to decode the location and display the example files.

If you are running release before Python 3.9 then you will be asked to install the following:

$ pip install importlib_resources
...
$

It will show up if you are running on an older system. For example, this is the results from running on Win7:

U:\Users\Bobby>cli4 -e
Module "importlib_resources" missing - please "pip install importlib_resources" as your Python version is lower than 3.9

U:\Users\Bobby>python -V
Python 3.8.3

U:\Users\Bobby>

Upgrading from an older version of Python is always recommended. Upgrading from Win7 is by-default even more important!

A DNS zone code example

#!/usr/bin/env python

import sys
import CloudFlare

def main():
    zone_name = sys.argv[1]
    cf = CloudFlare.CloudFlare()
    zone_info = cf.zones.post(data={'jump_start':False, 'name': zone_name})
    zone_id = zone_info['id']

    dns_records = [
        {'name':'foo', 'type':'AAAA', 'content':'2001:d8b::1'},
        {'name':'foo', 'type':'A', 'content':'192.168.0.1'},
        {'name':'duh', 'type':'A', 'content':'10.0.0.1', 'ttl':120},
        {'name':'bar', 'type':'CNAME', 'content':'foo'},
        {'name':'shakespeare', 'type':'TXT', 'content':"What's in a name? That which we call a rose by any other name ..."}
    ]

    for dns_record in dns_records:
        r = cf.zones.dns_records.post(zone_id, data=dns_record)
    exit(0)

if __name__ == '__main__':
    main()

A DNS zone delete code example (be careful)

#!/usr/bin/env python

import sys
import CloudFlare

def main():
    zone_name = sys.argv[1]
    cf = CloudFlare.CloudFlare()
    zone_info = cf.zones.get(params={'name': zone_name})
    zone_id = zone_info[0]['id']

    dns_name = sys.argv[2]
    dns_records = cf.zones.dns_records.get(zone_id, params={'name':dns_name + '.' + zone_name})
    for dns_record in dns_records:
        dns_record_id = dns_record['id']
        r = cf.zones.dns_records.delete(zone_id, dns_record_id)
    exit(0)

if __name__ == '__main__':
    main()

CLI

All API calls can be called from the command line via the cli4 command. Additionally, the cli4 command will convert domain name or account name prefixed with a colon (:) into the correct identifier. e.g. to view example.com you can use cli4 /zones/:example.com. You can pass the zone identifier (or account identifier or any identifier) with a colon followed by the identifier as a hex number 32 characters long.

$ cli4 [-V|--version] [-h|--help] [-v|--verbose] \
    [-e|--examples] \
    [-q|--quiet] \
    [-j|--json] [-y|--yaml] [-n|--ndjson] [-i|--image] \
    [-r|--raw] \
    [-d|--dump] \
    [-A|--openapi url] \
    [-b|--binary] \
    [-p|--profile profile-name] \
    [-h|--header additional-header] \
    [-w|--warnings [True|False]] \
    [--get|--patch|--post|--put|--delete] \
    [item=value|item=@filename|@filename ...] /command ...

CLI parameters for POST/PUT/PATCH

For API calls that need to pass data or parameters there is various formats to use.

The simplest form is item=value. This passes the value as a string within the APIs JSON data.

If you need a numeric value passed then == can be used to force the value to be treated as a numeric value within the APIs JSON data. For example: item==value.

if you need to pass a list of items; then [] can be used. For example:

pool_id1="11111111111111111111111111111111"
pool_id2="22222222222222222222222222222222"
pool_id3="33333333333333333333333333333333"
cli4 --post global_pools="[ ${pool_id1}, ${pool_id2}, ${pool_id3} ]" region_pools="[ ]" /user/load_balancers/maps

Data or parameters can be either named or unnamed. It can not be both. Named is the majority format; as described above. Unnamed parameters simply don't have anything before the = sign, as in =value. This format is presently only used by the Cloudflare Load Balancer API calls. For example:

cli4 --put ="00000000000000000000000000000000" /user/load_balancers/maps/:00000000000000000000000000000000/region/:WNAM

Data can also be uploaded from file contents. Using the item=@filename format will open the file and the contents uploaded in the POST.

CLI output

The default output from the CLI command is in JSON. It can also output YAML format (i.e. human readable). This is controled by the --yaml or --json flags (JSON is the default). There is also a --ndjson flag for use with line based JSON data - this is mainly used for log data.

Additonally the output can be plain text or binary image format depending on the results from the API call (some calls results in non JSON results). The --image flag will return the data in the same format as the API's results.

Simple CLI calls

  • cli4 /user/billing/profile

  • cli4 /user/invites

  • cli4 /zones/:example.com

  • cli4 /zones/:example.com/dnssec

  • cli4 /zones/:example.com/settings/ipv6

  • cli4 --put /zones/:example.com/activation_check

  • cli4 /zones/:example.com/keyless_certificates

  • cli4 /zones/:example.com/analytics/dashboard

More complex CLI calls

Here is the creation of a DNS entry, followed by a listing of that entry and then the deletion of that entry.

$ $ cli4 --post name="test" type="A" content="10.0.0.1" /zones/:example.com/dns_records
{
    "id": "00000000000000000000000000000000",
    "name": "test.example.com",
    "type": "A",
    "content": "10.0.0.1",
    ...
}
$

$ cli4 /zones/:example.com/dns_records/:test.example.com | jq '{"id":.id,"name":.name,"type":.type,"content":.content}'
{
  "id": "00000000000000000000000000000000",
  "name": "test.example.com",
  "type": "A",
  "content": "10.0.0.1"
}

$ cli4 --delete /zones/:example.com/dns_records/:test.example.com | jq -c .
{"id":"00000000000000000000000000000000"}
$

There's the ability to handle dns entries with multiple values. This produces more than one API call within the command.

$ cli4 /zones/:example.com/dns_records/:test.example.com | jq -c '.[]|{"id":.id,"name":.name,"type":.type,"content":.content}'
{"id":"00000000000000000000000000000000","name":"test.example.com","type":"A","content":"192.168.0.1"}
{"id":"00000000000000000000000000000000","name":"test.example.com","type":"AAAA","content":"2001:d8b::1"}
$

Here are the cache purging commands.

$ cli4 --delete purge_everything=true /zones/:example.com/purge_cache | jq -c .
{"id":"00000000000000000000000000000000"}
$

$ cli4 --delete files='[http://example.com/css/styles.css]' /zones/:example.com/purge_cache | jq -c .
{"id":"00000000000000000000000000000000"}
$

$ cli4 --delete files='[http://example.com/css/styles.css,http://example.com/js/script.js]' /zones/:example.com/purge_cache | jq -c .
{"id":"00000000000000000000000000000000"}
$

$ cli4 --delete tags='[tag1,tag2,tag3]' /zones/:example.com/purge_cache | jq -c .
cli4: /zones/:example.com/purge_cache - 1107 Only enterprise zones can purge by tag.
$

A somewhat useful listing of available plans for a specific zone.

$ cli4 /zones/:example.com/available_plans | jq -c '.[]|{"id":.id,"name":.name}'
{"id":"00000000000000000000000000000000","name":"Pro Website"}
{"id":"00000000000000000000000000000000","name":"Business Website"}
{"id":"00000000000000000000000000000000","name":"Enterprise Website"}
{"id":"0feeeeeeeeeeeeeeeeeeeeeeeeeeeeee","name":"Free Website"}
$

Cloudflare CA CLI calls

Here's some Cloudflare CA calls. Note the need of the zone_id= parameter with the basic /certificates call.

$ cli4 /zones/:example.com | jq -c '.|{"id":.id,"name":.name}'
{"id":"12345678901234567890123456789012","name":"example.com"}
$

$ cli4 zone_id=12345678901234567890123456789012 /certificates | jq -c '.[]|{"id":.id,"expires_on":.expires_on,"hostnames":.hostnames,"certificate":.certificate}'
{"id":"123456789012345678901234567890123456789012345678","expires_on":"2032-01-29 22:36:00 +0000 UTC","hostnames":["*.example.com","example.com"],"certificate":"-----BEGIN CERTIFICATE-----\n ... "}
{"id":"123456789012345678901234567890123456789012345678","expires_on":"2032-01-28 23:23:00 +0000 UTC","hostnames":["*.example.com","example.com"],"certificate":"-----BEGIN CERTIFICATE-----\n ... "}
{"id":"123456789012345678901234567890123456789012345678","expires_on":"2032-01-28 23:20:00 +0000 UTC","hostnames":["*.example.com","example.com"],"certificate":"-----BEGIN CERTIFICATE-----\n ... "}
$

A certificate can be viewed via a simple GET request.

$ cli4 /certificates/:123456789012345678901234567890123456789012345678
{
    "certificate": "-----BEGIN CERTIFICATE-----\n ... ",
    "expires_on": "2032-01-29 22:36:00 +0000 UTC",
    "hostnames": [
        "*.example.com",
        "example.com"
    ],
    "id": "123456789012345678901234567890123456789012345678",
    "request_type": "origin-rsa"
}
$

Creating a certificate. This is done with a POST request. Note the use of == in order to pass a decimal number (vs. string) in JSON. The CSR is not shown for simplicity sake.

$ CSR=`cat example.com.csr`
$ cli4 --post hostnames='["example.com","*.example.com"]' requested_validity==365 request_type="origin-ecc" csr="$CSR" /certificates
{
    "certificate": "-----BEGIN CERTIFICATE-----\n ... ",
    "csr": "-----BEGIN CERTIFICATE REQUEST-----\n ... ",
    "expires_on": "2018-09-27 21:47:00 +0000 UTC",
    "hostnames": [
        "*.example.com",
        "example.com"
    ],
    "id": "123456789012345678901234567890123456789012345678",
    "request_type": "origin-ecc",
    "requested_validity": 365
}
$

Deleting a certificate can be done with a DELETE call.

$ cli4 --delete /certificates/:123456789012345678901234567890123456789012345678
{
    "id": "123456789012345678901234567890123456789012345678",
    "revoked_at": "0000-00-00T00:00:00Z"
}
$

Paging CLI calls

The --raw command provides access to the paging returned values. See the API documentation for all the info. Here's an example of how to page thru a list of zones (it's included in the examples folder as example_paging_thru_zones.sh). Note the use of == to pass a number vs a string as paramater.

:
tmp=/tmp/$$_
trap "rm ${tmp}; exit 0" 0 1 2 15
PAGE=0
while true
do
        cli4 --raw per_page==5 page==${PAGE} /zones > ${tmp}
        domains=`jq -c '.|.result|.[]|.name' < ${tmp} | tr -d '"'`
        result_info=`jq -c '.|.result_info' < ${tmp}`
        COUNT=`      echo "${result_info}" | jq .count`
        PAGE=`       echo "${result_info}" | jq .page`
        PER_PAGE=`   echo "${result_info}" | jq .per_page`
        TOTAL_COUNT=`echo "${result_info}" | jq .total_count`
        TOTAL_PAGES=`echo "${result_info}" | jq .total_pages`
        echo COUNT=${COUNT} PAGE=${PAGE} PER_PAGE=${PER_PAGE} TOTAL_COUNT=${TOTAL_COUNT} TOTAL_PAGES=${TOTAL_PAGES} -- ${domains}
        if [ "${PAGE}" == "${TOTAL_PAGES}" ]
        then
                ## last section
                break
        fi
        # grab the next page
        PAGE=`expr ${PAGE} + 1`
done

It produces the following results.

COUNT=5 PAGE=1 PER_PAGE=5 TOTAL_COUNT=31 TOTAL_PAGES=7 -- accumsan.example auctor.example consectetur.example dapibus.example elementum.example
COUNT=5 PAGE=2 PER_PAGE=5 TOTAL_COUNT=31 TOTAL_PAGES=7 -- felis.example iaculis.example ipsum.example justo.example lacus.example
COUNT=5 PAGE=3 PER_PAGE=5 TOTAL_COUNT=31 TOTAL_PAGES=7 -- lectus.example lobortis.example maximus.example morbi.example pharetra.example
COUNT=5 PAGE=4 PER_PAGE=5 TOTAL_COUNT=31 TOTAL_PAGES=7 -- porttitor.example potenti.example pretium.example purus.example quisque.example
COUNT=5 PAGE=5 PER_PAGE=5 TOTAL_COUNT=31 TOTAL_PAGES=7 -- sagittis.example semper.example sollicitudin.example suspendisse.example tortor.example
COUNT=1 PAGE=7 PER_PAGE=5 TOTAL_COUNT=31 TOTAL_PAGES=7 -- varius.example vehicula.example velit.example velit.example vitae.example
COUNT=5 PAGE=6 PER_PAGE=5 TOTAL_COUNT=31 TOTAL_PAGES=7 -- vivamus.example

Paging thru lists (using cursors)

Some API calls use cursors to read beyond the initally returned values. See the API page in order to see which API calls do this.

$ ACCOUNT_ID="00000000000000000000000000000000"
$ LIST_ID="00000000000000000000000000000000"
$
$ cli4 --raw /accounts/::${ACCOUNT_ID}/rules/lists/::${LIST_ID}/items > /tmp/page1.json
$ after=`jq -r '.result_info.cursors.after' < /tmp/page1.json`
$ echo "after=$after"
after=Mxm4GVmKjYbFjy2VxMPipnJigm1M_s6lCS9ABR9wx-RM2A
$

Once we have the after value, we can pass it along in order to read the next hunk of values. We finish when after returns as null (or isn't present).

$ cli4 --raw cursor="$after" /accounts/::${ACCOUNT_ID}/rules/lists/::${LIST_ID}/items > /tmp/page2.json
$ after=`jq -r '.result_info.cursors.after' < /tmp/page2.json`
$ echo "after=$after"
after=null
$

We can see the results now in two files.

$ jq -c '.result[]' < /tmp/page1.json | wc -l
      25
$

$ jq -c '.result[]' < /tmp/page2.json | wc -l
       5
$

$ for f in /tmp/page?.json ; do jq -r '.result[]|.id,.ip,.comment' < $f | paste - - - ; done | column -s'   ' -t
0fe44928258549feb47126a966fbf4a0  0.0.0.0           all zero
2e1e02120f5e466f8c0e26375e4cf4c8  1.0.0.1           Cloudflare DNS a
9ca5fd0ac6f54fdbb9dedd3fb72ce2da  1.1.1.1           Cloudflare DNS b
b3654987446743738c782f36ebe074f5  10.0.0.0/8        RFC1918 space
90bec8ce37d242faa2e27d1e78c1d8e2  103.21.244.0/22   Cloudflare IP
970a3c810cda41af9bef2c36a1892f7e  103.22.200.0/22   Cloudflare IP
3ec8516158bf4f3cac18210f611ee541  103.31.4.0/22     Cloudflare IP
ee9d268367204e6bb8e5e4c907f22de8  104.16.0.0/12     Cloudflare IP
93ae02eda9774c45840af367a02fe529  108.162.192.0/18  Cloudflare IP
62891ebf6db44aa494d79a6401af185e  131.0.72.0/22     Cloudflare IP
cac40cd940cc470582b8c912a8a12bea  141.101.64.0/18   Cloudflare IP
f6d5eacd81a2407f8e0d81caee21e7f8  162.158.0.0/15    Cloudflare IP
3d538dfc38ab471d9d3fe78332acfa4e  172.16.0.0/12     RFC1918 space
f353cb8f98424837ad35382a22b9debe  172.64.0.0/13     Cloudflare IP
78f3e1a0bafc41f88d4d40ad49a642e0  173.245.48.0/20   Cloudflare IP
c23a545475c54c32a7681c6b508d3e80  188.114.96.0/20   Cloudflare IP
f693237c9e294fe481221cbc2d7c20ef  190.93.240.0/20   Cloudflare IP
6d465ab3a0994c07827ebdcf8f34d977  192.168.0.0/16    RFC1918 space
1ad1e634b3664bac939086185c62faf7  197.234.240.0/22  Cloudflare IP
5d2968e7b3114d8e869a379d71c8ba86  198.41.128.0/17   Cloudflare IP
6a69de60b31448fa864f0a3ac5abe8d0  224.0.0.0/24      Multicast
30749cce89af4ab3a80e308294f46a46  240.0.0.0/4       Class E
2b32c67ea4d044628abe39f28662d8f0  255.255.255.255   all ones
cc7cd828b2fb4bcfb9391c2d3ef8d068  2400:cb00::/32    Cloudflare IP
b30d4cbd7dcd48729e8ebeda552e48a8  2405:8100::/32    Cloudflare IP
49db60758c8344959c338a74afc9748a  2405:b500::/32    Cloudflare IP
96e9eca1923c40d5a84865145f5a5d6a  2606:4700::/32    Cloudflare IP
21bc52a26e10405d89b7180ddcf49302  2803:f800::/32    Cloudflare IP
ff78f842188e4b869eb5389ae9ab8f41  2a06:98c0::/29    Cloudflare IP
0880cdfc40b14f6fa0639522a728859d  2c0f:f248::/32    Cloudflare IP
$

The result_info.cursors area also contains a before value for reverse scrolling.

As with per_page scrolling, raw mode is used.

DNSSEC CLI calls

$ cli4 /zones/:example.com/dnssec | jq -c '{"status":.status}'
{"status":"disabled"}
$

$ cli4 --patch status=active /zones/:example.com/dnssec | jq -c '{"status":.status}'
{"status":"pending"}
$

$ cli4 /zones/:example.com/dnssec
{
    "algorithm": "13",
    "digest": "41600621c65065b09230ebc9556ced937eb7fd86e31635d0025326ccf09a7194",
    "digest_algorithm": "SHA256",
    "digest_type": "2",
    "ds": "example.com. 3600 IN DS 2371 13 2 41600621c65065b09230ebc9556ced937eb7fd86e31635d0025326ccf09a7194",
    "flags": 257,
    "key_tag": 2371,
    "key_type": "ECDSAP256SHA256",
    "modified_on": "2016-05-01T22:42:15.591158Z",
    "public_key": "mdsswUyr3DPW132mOi8V9xESWE8jTo0dxCjjnopKl+GqJxpVXckHAeF+KkxLbxILfDLUT0rAK9iUzy1L53eKGQ==",
    "status": "pending"
}
$

Zone file upload (i.e. import) CLI calls (uses BIND format files)

Refer to Import DNS records on API documentation for this feature.

$ cat zone.txt
example.com.            IN      SOA     somewhere.example.com. someone.example.com. (
                                2017010101
                                3H
                                15
                                1w
                                3h
                        )

record1.example.com.    IN      A       10.0.0.1
record2.example.com.    IN      AAAA    2001:d8b::2
record3.example.com.    IN      CNAME   record1.example.com.
record4.example.com.    IN      TXT     "some text"
$

$ cli4 --post [email protected] /zones/:example.com/dns_records/import
{
    "recs_added": 4,
    "total_records_parsed": 4
}
$

Zone file upload (i.e. import) Python calls (uses BIND format files)

Because import is a keyword (or reserved word) in Python we append a _ (underscore) to the verb in order to use. The cli4 command does not need this edit.

    #
    # "import" is a reserved word and hence we add '_' to the end of verb.
    #
    r = cf.zones.dns_records.import_.post(zone_id, files={'file':fd})

See examples/example_dns_import.py for working code.

Zone file download (i.e. export) CLI calls (uses BIND format files)

The following is documented within the Advanced option of the DNS page within the Cloudflare portal.

$ cli4 /zones/:example.com/dns_records/export | egrep -v '^;;|^$'
$ORIGIN .
@       3600    IN      SOA     example.com.    root.example.com.       (
                2025552311      ; serial
                7200            ; refresh
                3600            ; retry
                86400           ; expire
                3600)           ; minimum
example.com.    300     IN      NS      REPLACE&ME$WITH^YOUR@NAMESERVER.
record4.example.com.    300     IN      TXT     "some text"
record3.example.com.    300     IN      CNAME   record1.example.com.
record1.example.com.    300     IN      A       10.0.0.1
record2.example.com.    300     IN      AAAA    2001:d8b::2
$

The egrep is used for documentation brevity.

This can also be done via Python code with the following example.

#!/usr/bin/env python
import sys
import CloudFlare

def main():
    zone_name = sys.argv[1]
    cf = CloudFlare.CloudFlare()

    zones = cf.zones.get(params={'name': zone_name})
    zone_id = zones[0]['id']

    dns_records = cf.zones.dns_records.export.get(zone_id)
    for l in dns_records.splitlines():
        if len(l) == 0 or l[0] == ';':
            continue
        print(l)
    exit(0)

if __name__ == '__main__':
    main()

Cloudflare Workers

Cloudflare Workers are described on the Cloudflare blog at here and here, with the beta release announced here.

The Python libraries now support the Cloudflare Workers API calls. The following javascript is lifted from https://cloudflareworkers.com/ and slightly modified.

$ cat modify-body.js
addEventListener("fetch", event => {
  event.respondWith(fetchAndModify(event.request));
});

async function fetchAndModify(request) {
  console.log("got a request:", request);

  // Send the request on to the origin server.
  const response = await fetch(request);

  // Read response body.
  const text = await response.text();

  // Modify it.
  const modified = text.replace(
  "<body>",
  "<body style=\"background: #ff0;\">");

  // Return modified response.
  return new Response(modified, {
    status: response.status,
    statusText: response.statusText,
    headers: response.headers
  });
}
$

Here's the website with it's simple <body> statement

$ curl -sS https://example.com/ | fgrep '<body'
  <body>
$

Now lets add the script. Looking above, you will see that it's simple action is to modify the <body> statement and make the background yellow.

$ cli4 --put @- /zones/:example.com/workers/script < modify-body.js
{
    "etag": "1234567890123456789012345678901234567890123456789012345678901234",
    "id": "example-com",
    "modified_on": "2018-02-15T00:00:00.000000Z",
    "script": "addEventListener(\"fetch\", event => {\n  event.respondWith(fetchAndModify(event.request));\n});\n\nasync function fetchAndModify(request) {\n  console.log(\"got a request:\", request);\n\n  // Send the request on to the origin server.\n  const response = await fetch(request);\n\n  // Read response body.\n  const text = await response.text();\n\n  // Modify it.\n  const modified = text.replace(\n  \"<body>\",\n  \"<body style=\\\"background: #ff0;\\\">\");\n\n  // Return modified response.\n  return new Response(modified, {\n    status: response.status,\n    statusText: response.statusText,\n    headers: response.headers\n  });\n}\n",
    "size": 603
}
$

The following call checks that the script is associated with the zone. In this case, it's the only script added by this user.

$ cli4 /user/workers/scripts
[
    {
        "created_on": "2018-02-15T00:00:00.000000Z",
        "etag": "1234567890123456789012345678901234567890123456789012345678901234",
        "id": "example-com",
        "modified_on": "2018-02-15T00:00:00.000000Z"
    }
]
$

Next step is to make sure a route is added for that script on that zone.

$ cli4 --post pattern="example.com/*" script="example-com" /zones/:example.com/workers/routes
{
    "id": "12345678901234567890123456789012"
}
$

$ cli4 /zones/:example.com/workers/routes
[
    {
        "id": "12345678901234567890123456789012",
        "pattern": "example.com/*",
        "script": "example-com"
    }
]
$

With that script added to the zone and the route added, we can now see the website has been modified because of the Cloudflare Worker.

$ curl -sS https://example.com/ | fgrep '<body'
  <body style="background: #ff0;">
$

All this can be removed; hence bringing the website back to its initial state.

$ cli4 --delete /zones/:example.com/workers/script
12345678901234567890123456789012
$ cli4 --delete /zones/:example.com/workers/routes/:12345678901234567890123456789012
true
$

$ curl -sS https://example.com/ | fgrep '<body'
  <body>
$

Refer to the Cloudflare Workers API documentation for more information.

Cloudflare Instant Logs

Please see https://developers.cloudflare.com/logs/instant-logs for all the information on how to use this feature. The cli4 command along with the Python libaries can be used to control the instant logs; however, the websocket reading is outside the scope of this library.

To query the states of the instant logs:

$ cli4 /zones/:example.com/logpush/edge/jobs | jq .
[]
$

To add monitoring:

$ cli4 --post \
        ='{
                "fields": "ClientIP,ClientRequestHost,ClientRequestMethod,ClientRequestURI,EdgeEndTimestamp,EdgeResponseBytes,EdgeResponseStatus,EdgeStartTimestamp,RayID",
                "sample": 1,
                "filter": "",
                "kind": "instant-logs"
        }' \
        /zones/:example.com/logpush/edge/jobs | jq .
{
  "destination_conf": "wss://logs.cloudflare.com/instant-logs/ws/sessions/00000000000000000000000000000000",
  "fields": "ClientIP,ClientRequestHost,ClientRequestMethod,ClientRequestURI,EdgeEndTimestamp,EdgeResponseBytes,EdgeResponseStatus,EdgeStartTimestamp,RayID",
  "filter": "",
  "kind": "instant-logs",
  "sample": 1,
  "session_id": "00000000000000000000000000000000"
}
$

To see the results:

$ cli4 /zones/:example.com/logpush/edge/jobs | jq .
[
  {
    "fields": "ClientIP,ClientRequestHost,ClientRequestMethod,ClientRequestURI,EdgeEndTimestamp,EdgeResponseBytes,EdgeResponseStatus,EdgeStartTimestamp,RayID",
    "filter": "",
    "kind": "instant-logs",
    "sample": 1,
    "session_id": "00000000000000000000000000000000"
  }
]
$

Cloudflare GraphQL

The GraphQL interface can be accessed via the command line or via Python.

    query="""
      query {
        viewer {
            zones(filter: {zoneTag: "%s"} ) {
            httpRequests1dGroups(limit:40, filter:{date_lt: "%s", date_gt: "%s"}) {
              sum { countryMap { bytes, requests, clientCountryName } }
              dimensions { date }
            }
          }
        }
      }
    """ % (zone_id, date_before[0:10], date_after[0:10])

    r = cf.graphql.post(data={'query':query})

    httpRequests1dGroups = zone_info = r['data']['viewer']['zones'][0]['httpRequests1dGroups']

See the examples/example_graphql.sh and examples/example_graphql.py files for working examples. Here is the working example of the shell version:

$ examples/example_graphql.sh example.com
2020-07-14T02:00:00Z    34880
2020-07-14T03:00:00Z    18953
2020-07-14T04:00:00Z    28700
2020-07-14T05:00:00Z    2358
2020-07-14T06:00:00Z    34905
2020-07-14T07:00:00Z    779
2020-07-14T08:00:00Z    35450
2020-07-14T10:00:00Z    17803
2020-07-14T11:00:00Z    32678
2020-07-14T12:00:00Z    19947
2020-07-14T13:00:00Z    4956
2020-07-14T14:00:00Z    34585
2020-07-14T15:00:00Z    3022
2020-07-14T16:00:00Z    5224
2020-07-14T18:00:00Z    79482
2020-07-14T21:00:00Z    10609
2020-07-14T22:00:00Z    5740
2020-07-14T23:00:00Z    2545
2020-07-15T01:00:00Z    10777
$

For more information on how to use GraphQL at Cloudflare, refer to the Cloudflare GraphQL Analytics API. It contains a full overview of Cloudflare's GraphQL features and keywords.

Cloudflare AI

See https://blog.cloudflare.com/workers-ai-update-stable-diffusion-code-llama-workers-ai-in-100-cities/ for the introduction, along with https://developers.cloudflare.com/workers-ai/models/ for the nitty gritty details.

There are three AI calls included within the example folder.

Image creation.

$ python examples/example_ai_images.py A happy llama running through an orange cloud > /tmp/image.png
$
$ file /tmp/image.png
/tmp/image.png: PNG image data, 1024 x 1024, 8-bit/color RGB, non-interlaced
$

Translation.

$ python examples/example_ai_translate.py I\'ll have an order of the moule frites
Je vais avoir une commande des frites de moule
$

Speech Recognition with the openai/whisper model.

The following downloads a speech as an mp3 file and passes it to the AI API. It does a very good job transcribing; however, there's a good chance these mp3 files were use for training. That said, the example code is here to show how the API works vs testing the AI/ML quality.

$ python examples/example_ai_speechrecognition.py
mp3 received: length=700367
My fellow Americans, Michelle and I have been so touched by all the well wishes that we've received over the past few weeks. But tonight, tonight it's my turn to say thanks.
$

This is presently work-in-progress because of the non-Python calling method. The syntax could change in the future.

They can also be called via cli4.

$ cli4 --image --post text="I'll have an order of the moule frites" source_lang=english target_lang=french /accounts/:AccountID/ai/run/@cf/meta/m2m100-1.2b
{'translated_text': 'Je vais avoir une commande des frites de moule'}
$

Presently you will need the following in your cloudflare.cfg file.

$ cat ~/.cloudflare/cloudflare.cfg
[CloudFlare]
global_request_timeout = 120
max_request_retries = 1
extras =
    /accounts/:id/ai/run/@cf/meta/llama-2-7b-chat-fp16
    /accounts/:id/ai/run/@cf/meta/llama-2-7b-chat-int8
    /accounts/:id/ai/run/@cf/mistral/mistral-7b-instruct-v0.1
    /accounts/:id/ai/run/@cf/openai/whisper
    /accounts/:id/ai/run/@cf/meta/m2m100-1.2b
    /accounts/:id/ai/run/@cf/huggingface/distilbert-sst-2-int8
    /accounts/:id/ai/run/@cf/microsoft/resnet-50
    /accounts/:id/ai/run/@cf/stabilityai/stable-diffusion-xl-base-1.0
    /accounts/:id/ai/run/@cf/baai/bge-base-en-v1.5
    /accounts/:id/ai/run/@cf/baai/bge-large-en-v1.5
    /accounts/:id/ai/run/@cf/baai/bge-small-en-v1.5

$

As the @ (at) symbol and the . (dot) symbol aren't allowed in python variable names; you'll have the replace @cf with at_cf and . with _. There's already notes above that state that - (dash) is replaced with _ in the code. That will be needed with some model names.

The cli4 command does not need this edit. It is done on the fly!

For example, the following code is valid:

    r = cf.accounts.ai.run.at_cf.openai.whisper.post(account_id, data=audio_data)
    r = cf.accounts.ai.run.at_cf.meta.m2m100_1_2b.post(account_id, data=translate_data)
    r = cf.accounts.ai.run.at_cf.stabilityai.stable_diffusion_xl_base_1_0.post(account_id, data=image_create_data)

Or you can use the find() call can will do this conversion for you.

    translate_data = {'text':"I'll have an order of the moule frites", 'source_lang':'english', 'target_lang':'french'}

    m = cf.find('/accounts/:id/ai/run/@cf/meta/m2m100-1.2b')
    r = m.post(account_id, data=translate_data)
    print(r['translated_text'])

You will also have to run with a version of the library above 2.18.2.

Implemented API calls

The --dump argument to cli4 will produce a list of all the call implemented within the library.

$ cli4 --dump
/certificates
/ips
/organizations
...
/zones/ssl/analyze
/zones/ssl/certificate_packs
/zones/ssl/verification
$

Table of commands

An automatically generated table of commands is provided here.

Adding extra API calls manually

Extra API calls can be added via the configuration file

$ cat ~/.cloudflare/cloudflare.cfg
[Cloudflare]
extras =
    /client/v4/command
    /client/v4/command/:command_identifier
    /client/v4/command/:command_identifier/settings
$

While it's easy to call anything within Cloudflare's API, it's not very useful to add items in here as they will simply return API URL errors. Technically, this is only useful for internal testing within Cloudflare.

Issues

The following error can be caused by an out of date SSL/TLS library and/or out of date Python.

/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning

The solution can be found here and/or here.

Python 2.x vs 3.x support

As of May/June 2016 the code is now tested against pylint. This was required in order to move the codebase into Python 3.x. The motivation for this came from Danielle Madeley (danni).

While the codebase has been edited to run on Python 3.x, there's not been enough Python 3.x testing performed. If you can help in this regard; please contact the maintainers.

As of January 2020 the code is Python3 clean.

As of January 2020 the code is shipped up to pypi with Python2 support removed.

As of January 2020 the code is Python3.8 clean. The new SyntaxWarning messages (i.e. SyntaxWarning: "is" with a literal. Did you mean "=="?) meant minor edits were needed.

As of late 2023 the code is Python3.11 clean.

As of April 2024 the code is officially marked as 3.x only (3.6 and above to be specific) such that it can become PEP561 specific.

pypi and GitHub signed releases

As of October/2022, the code is signed by the maintainers personal email address: [email protected] 7EA1 39C4 0C1C 842F 9D41 AAF9 4A34 925D 0517 2859

Credit

This is based on work by Felix Wong (gnowxilef) found here. It has been seriously expanded upon.

Changelog

An automatically generated CHANGELOG is provided here.

Copyright

Copyright (c) 2016 thru 2024, Cloudflare. All rights reserved. Previous portions copyright Felix Wong (gnowxilef).

python-cloudflare's People

Contributors

aaranmcguire avatar acdha avatar ad-m avatar bellardia avatar bjoernpetersen avatar changaco avatar corywright avatar crlorentzen avatar daic115 avatar dargor avatar dkoston avatar fawaf avatar felixschwarz avatar ggerasimov avatar hlx98007 avatar issackelly avatar jacobbednarz avatar jaredpage avatar mahtin avatar martin40701 avatar mattjanssen avatar mnordhoff avatar nicholaskuechler avatar nijel avatar patryk avatar phntom avatar rita3ko avatar tugzrida avatar xens avatar yesbox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-cloudflare's Issues

Unable to pull ELS schema fields via cli4

Per https://support.cloudflare.com/hc/en-us/articles/115003165991-Enterprise-Log-Share-ELS-Migrating-to-New-Endpoint, I should be able to pull down the list of defined fields for the received-time-ordered logs via cli4 --get /zones/:zone_id/logs/received/fields, but no go. I've even added the path entries to .cloudflare/cloudflare.cfg, and still no go. Initial error was "/zones/:zone_id/logs - not found", now I get "/zones/:zone_id/logs/received - not found".

HTTPErrors should be caught and rethrown

I'm using the cloudflare.zones.custom_hostnames.delete endpoint and the endpoint throws a 500 error when a custom domain is in the state crypto.custom_hostnames.search.select.unknown_state. Instead, this should be caught and rethrown as a CloudFlareError so that all errors can be caught without specifying multiple exception classes.

HTTPError: 500 Server Error: Internal Server Error for url: https://api.cloudflare.com/client/v4/zones/...

screen shot 2018-08-06 at 11 49 24 am

Unable to update TTL

If you call cli4 like so:
python -m cli4 --put name=$record type=A ttl=120 content=$ip /zones/:$domain/dns_records/:$record
you will get the following error:
cli4: /zones/:<ZONE_NAME>/dns_records/:<RECORD> - 1004 DNS Validation Error
Without the TTL Attribute this works fine and returns the Expected Record in JSON Format.

Unable to connect to logpush endoint via cli4

Trying to check and configure logpush jobs and was hoping to use cli4 to do it quickly, but it doesn't know the logpush endpoint. It just returns the error /zones/:zoneid/logpush - not found. Confirmed that it does work as expected via curl.

Tested using version 2.3.0

Incorrect number of records in zone

Hi!
I have the two sets of records in my zone: blablabla and blablabla-cdn. Content of all records are the same, for example:

Type Name Value
A blablabla 1.2.3.4
A blablabla-cdn 1.2.3.4
A blablabla 5.6.7.8
A blablabla-cdn 5.6.7.8

When I am trying to get this record via API I get the right number of blablabla-cdn records, but an incorrect number of blablabla records. I have 11 records in each set and I get 11 for blablabla-cdn and 4 for blablabla.
Here is code what I use:

cf = CloudFlare.CloudFlare(email=str(CF_API_EMAIL), token=str(CF_API_KEY))
dns_records = cf.zones.dns_records.get(zone_id)
for record in dns_records:
    if (re.match('blablabla', record['name'])):
        print(json.dumps(record,indent=4))

Is this a bug or I make something wrong?
Thanks!

"put" not available for dns_records

Hi,

the readme states that put is available for dns_records-operations:

GET PUT POST PATCH DELETE API call
(...) (...) (...) (...) (...) (...)
GET PUT POST   DELETE /zones/:identifier/dns_records
(...) (...) (...) (...) (...) (...)

However when using dns_records.put() (cf.zones.dns_records.put(zone_id, data=record)) the SDK throws an error: CloudFlare.exceptions.CloudFlareAPIError: Method PUT not available for that URI.

At least the readme should be correct about the available methods, but ideally an idempotent PUT should be available for dns_records to avoid duplicate records.

Can't find ip in user firewall: You must specify identifier1

Before inserting block rule to user/firewall/access_rules/rules I trying to find IP, if it is already blocked:
result = cf.user.firewall.access_rules.rules.get(data={'configuration': { 'target': 'ip', 'value': bad_guy_ip }})

but I get exception:

File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/CloudFlare/cloudflare.py", line 618, in get
    params, data)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/CloudFlare/cloudflare.py", line 75, in call_with_auth
    params, data, files)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/CloudFlare/cloudflare.py", line 424, in _call
    params, data, files)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/CloudFlare/cloudflare.py", line 319, in _raw
    params, data, files)
  File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/CloudFlare/cloudflare.py", line 143, in _network
    raise CloudFlareAPIError(0, 'You must specify identifier1')
CloudFlare.exceptions.CloudFlareAPIError: You must specify identifier1

so how to specify it, and why I need to do it in user-level, this rules are 'global'?

api_key vs token gotcha

So we can create an API object that authenticates using the email / API key combo or using an API token. That the token parameter to the initializer is overloaded for the two different uses (whether or not email is also specified is what decides which it is) is already a bit questionable IMHO, but ok, I can live with that.

But check this out: if you happen to have the CF_API_EMAIL environment variable defined then it will always interpret the token parameter as an API key not an API token... but it will apparently try to use it, even if the CF_API_KEY env var is also set. Or perhaps it stuffs all of the above into the headers. The result is that one gets "6003 Invalid request headers - api call failed" which isn't much of a clue to what's going on. Either there should be a more specific error, or it should just use the env vars, ignore the token param, and issue a warning.

"logger" isn't an actual requirement

Went to install the CloudFlare python library and was surprised it was pulling in a module for logging called logger. Looked through the CF code and as best as i can tell it's not importing logger anywhere, which is good b/c that module as a single .py and config file with the following which would mess up the logging config for most anything utilizing logging.

import logging
import logging.config
import os
import sys
import types

logging.getLogger('paramiko').setLevel(logging.WARNING)
logging.getLogger('requests').setLevel(logging.WARNING)

config_file = os.path.join(os.path.dirname(__file__), 'logging.conf')

logging.config.fileConfig(config_file, disable_existing_loggers=False)
logger = logging.getLogger()


def error(self, msg, *args, **kwargs):
    self.error(msg, *args, **kwargs)
    sys.exit(1)

logger.interrupt = types.MethodType(error, logger)
logger.info = logger.info
logger.warn = logger.warn

Pretty sure that line should just be dropped from the requirements list since everything being used is part of the stdlib.

requirements.txt should specify the request version

the json keyword on the session put & post methods requires requests>=2.4.2. Old Ubuntu LTS versions ship with an older version which will cause an exception: CloudFlare.exceptions.CloudFlareAPIError: connection failed.

Not clear how to edit page rules

I make following request

for r in rules:
        if r['actions'].pop()['id'] == 'always_use_https':
            #switch_rules.append(r)
            a = r['id']
            cf.zones.pagerules.patch(zones['id'], a, params={'status:disabled'})

how to pass optional parameters ?
I got error like connection failed

Adding MX records does not work Priority is not set

I am trying to add MX records for some of my domains.

using the following

cli4 -v --post name="africlip.io" type="MX" PRIORITY=10  content="exch3.email.newsclip-sa.co.za"  /zones/:africlip.io/dns_records

I keep getting the same error. I have tried various quoting options.
PRIORITY="10" lower case priority=10.
Same error every time

2019-07-24 15:28:28,421 - Python Cloudflare API v4 - DEBUG - Response: error 1004 DNS Validation Error
cli4: /zones/:africlip.io/dns_records - 1004 DNS Validation Error

2019-07-24 15:28:27,072 - Python Cloudflare API v4 - DEBUG - Call: zones,None,None,None,None,None
2019-07-24 15:28:27,072 - Python Cloudflare API v4 - DEBUG - Call: optional params and data {'name': 'africlip.io', 'per_page': 1} None
2019-07-24 15:28:27,072 - Python Cloudflare API v4 - DEBUG - Call: method and url GET https://api.cloudflare.com/client/v4/zones
2019-07-24 15:28:27,072 - Python Cloudflare API v4 - DEBUG - Call: headers {'User-Agent': 'python-cloudflare/2.3.0/python-requests/2.22.0/python/3.7.3', 'X-Auth-Email': '[email protected]', 'X-Auth-Key': 'REDACTED', 'Content-Type': 'application/json'}
2019-07-24 15:28:27,072 - Python Cloudflare API v4 - DEBUG - Call: doit!
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Call: done!
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Response: url https://api.cloudflare.com/client/v4/zones?name=africlip.io&per_page=1
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Response: 200, application/json, {"result":[{"id":"redacted","name":"africlip.io","status":"active","paused":false,"type":"full","development_mode":0,"name_servers":["hans.ns.cloudflare.com","melinda.ns.cloudflare.com"],"original_name_servers":["titan.is.co.za","jupiter.is.co.za","demeter.is.co.za"],"original_registrar":null,"original_dnshost":null,"modified_on":"2019-04-16T07:27:18.194789Z","created_on":"2019-01-16T06:54:13.079417Z","activated_on":"2019-01-18T18:56:56.812146Z","meta":{"step":3,"wildcard_proxiable":false,"custom_certificate_quota":0,"page_rule_quota":3,"phishing_detected":false,"multiple_railguns_allowed":false},"owner":{"id":"redacted","type":"user","email":"redacted"},"account":{"id":"redacted","name":"Newsclip Primary Account"},"permissions":["#access:edit","#access:read","#analytics:read","#app:edit","#auditlogs:read","#billing:read","#cache_purge:edit","#dns_records:edit","#dns_records:read","#lb:edit","#lb:read","#legal:read","#logs:edit","#logs:read","#member:read","#organization:edit","#organization:read","#ssl:edit","#ssl:read","#stream:edit","#stream:read","#subscription:edit","#subscription:read","#waf:edit","#waf:read","#webhooks:edit","#webhooks:read","#worker:edit","#worker:read","#zone:edit","#zone:read","#zone_settings:edit","#zone_settings:read"],"plan":{"id":"0feeeeeeeeeeeeeeeeeeeeeeeeeeeeee","name":"Free Website","price":0,"currency":"USD","frequency":"","is_subscribed":true,"can_subscribe":false,"legacy_id":"free","legacy_discount":false,"externally_managed":false}}],"result_info":{"page":1,"per_page":1,"total_pages":1,"count":1,"total_count":1},"success":true,"errors":[],"messages":[]}
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Response: [{'id': 'redacted', 'name': 'africlip.io', 'status': 'active', 'paused': False, 'type': 'full', 'development_mode': 0, 'name_servers': ['hans.ns.cloudflare.com', 'melinda.ns.cloudflare.com'], 'original_name_servers': ['titan.is.co.za', 'jupiter.is.co.za', 'demeter.is.co.za'], 'original_registrar': None, 'original_dnshost': None, 'modified_on': '2019-04-16T07:27:18.194789Z', 'created_on': '2019-01-16T06:54:13.079417Z', 'activated_on': '2019-01-18T18:56:56.812146Z', 'meta': {'step': 3, 'wildcard_proxiable': False, 'custom_certificate_quota': 0, 'page_rule_quota': 3, 'phishing_detected': False, 'multiple_railguns_allowed': False}, 'owner': {'id': 'redacted', 'type': 'user', 'email': '[email protected]'}, 'account': {'id': 'redacted', 'name': 'Newsclip Primary Account'}, 'permissions': ['#access:edit', '#access:read', '#analytics:read', '#app:edit', '#auditlogs:read', '#billing:read', '#cache_purge:edit', '#dns_records:edit', '#dns_records:read', '#lb:edit', '#lb:read', '#legal:read', '#logs:edit', '#logs:read', '#member:read', '#organization:edit', '#organization:read', '#ssl:edit', '#ssl:read', '#stream:edit', '#stream:read', '#subscription:edit', '#subscription:read', '#waf:edit', '#waf:read', '#webhooks:edit', '#webhooks:read', '#worker:edit', '#worker:read', '#zone:edit', '#zone:read', '#zone_settings:edit', '#zone_settings:read'], 'plan': {'id': '0feeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 'name': 'Free Website', 'price': 0, 'currency': 'USD', 'frequency': '', 'is_subscribed': True, 'can_subscribe': False, 'legacy_id': 'free', 'legacy_discount': False, 'externally_managed': False}}]
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Call: zones,redacted,dns_records,None,None,None
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Call: optional params and data None {'name': 'africlip.io', 'type': 'MX', 'priority': '10', 'content': 'exch3.email.newsclip-sa.co.za'}
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Call: method and url POST https://api.cloudflare.com/client/v4/zones/redacted/dns_records
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Call: headers {'User-Agent': 'python-cloudflare/2.3.0/python-requests/2.22.0/python/3.7.3', 'X-Auth-Email': '[email protected]', 'X-Auth-Key': 'REDACTED', 'Content-Type': 'application/json'}
2019-07-24 15:28:27,744 - Python Cloudflare API v4 - DEBUG - Call: doit!
2019-07-24 15:28:28,420 - Python Cloudflare API v4 - DEBUG - Call: done!
2019-07-24 15:28:28,420 - Python Cloudflare API v4 - DEBUG - Response: url https://api.cloudflare.com/client/v4/zones/redacted/dns_records
2019-07-24 15:28:28,420 - Python Cloudflare API v4 - DEBUG - Response: 400, application/json, {"success":false,"errors":[{"code":1004,"message":"DNS Validation Error","error_chain":[{"code":9104,"message":"priority must be an int between 0 and 65535."}]}],"messages":[],"result":null}

I am not sure what else I can do here.

2019-07-24 15:28:28,421 - Python Cloudflare API v4 - DEBUG - Response: error 1004 DNS Validation Error
cli4: /zones/:africlip.io/dns_records - 1004 DNS Validation Error


cli4 -V
Cloudflare library version: 2.3.0

Let me know if I can provide any other details

Modify WAF rules

Hello.

Could you please help me?
How I can change settings for waf rules via cli4?

For example I need to change
curl -X PATCH "https://api.cloudflare.com/client/v4/zones/$zone_id/firewall/waf/packages/1e334934fd7ae32ad705667f8c1057aa/rules/100047WP" -H "Content-Type:application/json" -H "X-Auth-Key:$token" -H "X-Auth-Email:$login" --data '{"mode":"challenge"}'

Via cli4 it doesn't work :(
cli4 --patch mode=challenge /zones/:example.com/firewall/waf/packages/:1e334934fd7ae32ad705667f8c1057aa/rules/100047WP

Thank you for your work.

Why can you only see 25 pieces of information when you look at the firewall whitelist?

Hello!
I found a question in use, hoping to help me to answer it.
When I called the query firewall interface, I found that I could only get 25 pieces of information at a time.
How can I get all the information at once?
######################################################
def main():
cf = CloudFlare.CloudFlare(email= email,token=token)
zones = cf.zones.firewall.access_rules.rules(Zone_ID)
print(len(zones['result_info']))
######################################################
My e-mail address is yuan.22811422.com
Thank you and your team for contributing such practical code!
A beginner's respect!

Regression in 1.7.0 when adding TXT records

  • Expected result: Either a successful or "record already exists" result from API, as seen from version 1.6.2.
  • Instead got error: CloudFlare.exceptions.CloudFlareAPIError: DNS Validation Error, on version 1.7.0

Sample code to reproduce (replace relevant parts)

import CloudFlare
cf = CloudFlare.CloudFlare("[email]", "[key]")
zones = cf.zones.get(params={'name': "[domain]",'per_page': 1})
zone_id = zones[0]["id"]
print(cf.zones.dns_records.post(zone_id, data={'type': 'TXT', 'name': 'testtxt', 'content': 'test', 'ttl': 120}))

This bug will affect services that make use of TXT records (e.g. LetsEncrypt) when using the latest version of the Cloudflare module

Please update examples

The examples are a bit outdated I believe, as I'm trying to write a supybot/limnoria plugin and I'm not able to get the package to load in any fashion other than to have it say that one or the other is not an attribute

import Cloudflare

class Cloudflare(callbacks.Plugin):
    """Allows access to the Cloudflare (tm) API"""
    threaded = True
    email = conf.supybot.plugins.Cloudflare.api.get('email')
    key = conf.supybot.plugins.Cloudflare.api.get('key')
    cf_send = Cloudflare.Cloudflare(email=email, token=key)

    def zones(self, irc, msg, args):
        """takes no arguments
        Lists the zones on the account."""
        listofzones = cf_send.zones.get()
        zonelist = []
        for zone in listofzones:
            zone_id = zone['id']
            zone_name = zone['name']
            zonelist.append("%s->%s" % (zone_id, zone_name))
        irc.reply("%s" % (" \xB7 ".join(zonelist)), notice=True, private=True)
    zones = wrap(zones, ['admin'])

Even when using import CloudFlare as it says in the example, I get the following.

14:27 <~Ken> !load Cloudflare
14:27 <+ElectroCode> Ken: Error: No module named 'CloudFlare'

Here is a traceback using import Cloudflare as cf

  File "/home/bots/electro/plugins/Cloudflare/plugin.py", line 53, in Cloudflare
    cf_send = cf.CloudFlare(email=email, token=key)
AttributeError: module 'Cloudflare' has no attribute 'CloudFlare'

DNS record deletion causes exception

DNS record deletion causes exception - it seems like API returns error, but without 'code' (it's not mandatory)

_raw() call returns {'error': 'You cannot use this API for domains with a .cf, .ga, .gq, .ml, or .tk TLD (top-level domain). To configure the DNS settings for this domain, use the Cloudflare Dashboard.'}

Exception trace:

In [10]: record = cf.zones.dns_records.get(zone['id'])[0]                                                                                                                                                                                     

In [11]: record['zone_id'], record['id']                                                                                                                                                                                                      
Out[11]: ('9d2e3522d6f813a1d32c85d0fe137443', 'a47180b9c7daa2e8ec92fcc7a5fb2da8')

In [12]: zone = cf.zones.get(params={'name': '42cc-testing-site.tk'})[0]                                                                                                                                                                      

In [13]: record = cf.zones.dns_records.get(zone['id'])[0]                                                                                                                                                                                     

In [14]: record['zone_id'], record['id']                                                                                                                                                                                                      
Out[14]: ('9d2e3522d6f813a1d32c85d0fe137443', 'a47180b9c7daa2e8ec92fcc7a5fb2da8')

In [15]: cf.zones.dns_records.delete(record['zone_id'], record['id'])                                                                                                                                                                         
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-15-06326d1a5ea4> in <module>
----> 1 cf.zones.dns_records.delete(record['zone_id'], record['id'])

~/.virtualenvs/pbn/lib/python3.6/site-packages/CloudFlare/cloudflare.py in delete(self, identifier1, identifier2, identifier3, params, data)
    683             return self._base.call_with_auth('DELETE', self._parts,
    684                                              identifier1, identifier2, identifier3,
--> 685                                              params, data)
    686 
    687     class _AddWithAuthUnwrapped(object):

~/.virtualenvs/pbn/lib/python3.6/site-packages/CloudFlare/cloudflare.py in call_with_auth(self, method, parts, identifier1, identifier2, identifier3, params, data, files)
    121             return self._call(method, headers, parts,
    122                               identifier1, identifier2, identifier3,
--> 123                               params, data, files)
    124 
    125         def call_with_auth_unwrapped(self, method, parts,

~/.virtualenvs/pbn/lib/python3.6/site-packages/CloudFlare/cloudflare.py in _call(self, method, headers, parts, identifier1, identifier2, identifier3, params, data, files)
    486             if response_data['success'] is False:
    487                 errors = response_data['errors'][0]
--> 488                 code = errors['code']
    489                 if 'message' in errors:
    490                     message = errors['message']

KeyError: 'code'

Fully support Python 3

This module is not fully compatible with Python 3. There have been various attempts to fix this (c.f. #1, #3, #14, #21), but none of them have been merged and they were all incomplete. One particular area that hasn't been addressed in any of the pull requests is /examples, which relies heavily on Python 2 behavior (primarily print as a statement rather than a function). Please finish the migration to support Python 3. This is holding up packaging of the module for Fedora, which in turn is holding up packaging of the certbot-dns-cloudflare module for Fedora/EPEL.

Consistent issue with creating certain CNAMEs

I have a set of records that I am trying to create as CNAMEs in CloudFlare. Many of them are successful, however there are certain records that always fail to create and throw an error that is not terribly descriptive. I have found one reference to it in your codebase, but do not see why this error would be thrown for the same set of records everytime a call is attempted in trying to create them while other records succeed.

I have reached out to CloudFlare support, but they will not help since the error is coming from the Python module.

The error is: You must specify identifier1. The requests are being made with a token as opposed to a username and password. The call being made is:

CF_TOKEN = '<string of API token>'
CF_ZONE = '<string of zone ID>'
cf = CloudFlare.CloudFlare(token=CF_TOKEN)
new_cname = {'name': '<string name of CNAME record>',
                 'type': 'CNAME',
                 'content': '<string value of CNAME record>',}
try:
    cf.zones.dns_records.post(CF_ZONE, data=new_cname)
except CloudFlare.cloudflare.CloudFlareAPIError as e:
    print('There was a problem creating the DNS validation record for {} '.format('<string name of CNAME record>' + str(e))

An example of a failing new_cname dict is:

{'name': '_19b3a279aa2cb69039bd37c85c557c92.api.robinpowered.com.', 'type': 'CNAME', 'content': '_8d9d08826d5f4a56f590a47d674d5740.nhqijqilxf.acm-validations.aws.'}

Why would this record fail while others do not? Is there a better way to get a more useful error message?

[Question] DNS Zone Import

Hi,

I'm trying to call the dns_records/import endpoint using this python lib, but I can't find how to do it.

Please someone provide an example (add this one to examples directory).

Regards

Example for deleting DNS record

Hi,

Can you please provide an example of how to delete a specific DNS record (like a CNAME) from a given zone?

I checked the examples directory, the README and read the code, but I'm still not absolutely certain how this is done. Getting this wrong could mean accidentally deleting the entire zone instead of a specific DNS record so any help with this would be super appreciated.

Thanks,

Michael

Invalid or missing WAF Rule mode

Hello,

I am using Cloudflare's Python library to make some API calls. The following call generates API error every time:

cf.zones.firewall.waf.packages.rules.patch(ZoneID,RulesetID,RuleID, params={"mode":"off"})

The error is

File "/usr/lib/python2.7/site-packages/CloudFlare/cloudflare.py", line 625, in patch
    params, data)
  File "/usr/lib/python2.7/site-packages/CloudFlare/cloudflare.py", line 75, in call_with_auth
    params, data, files)
  File "/usr/lib/python2.7/site-packages/CloudFlare/cloudflare.py", line 467, in _call
    raise CloudFlareAPIError(code, message, error_chain)
CloudFlare.exceptions.CloudFlareAPIError: Invalid or missing WAF Rule mode

Everything is fine when submitting this call using curl as described here:

https://api.cloudflare.com/#waf-rules-edit-rule

Here's the code I'm using:

import CloudFlare

priv_key = "xxxx"
email = "xxxxx"
source = "xxxxx" # source zone id
target = "xxxxx" # target zone id
owasp = "xxx" # OWASP ruleset id

def replicate_rules():
    cf = CloudFlare.CloudFlare(email=email, token=priv_key, raw=True)
    for i in range (1,26):
        rules = cf.zones.firewall.waf.packages.rules(source,owasp, params={"per_page":100, "page":i})
        for x in rules["result"]:
                                   cf.zones.firewall.waf.packages.rules.patch(target,owasp,x["id"], params={"mode":"x['mode']"})



Inconsistent encoding between zone name and dns_records name for internationalized domains

Hi,
Take a look at this:

>>> my_domain = cf.zones.get(params = { 'name': 'xn--sanremomlheim-3ob.de' })
>>> my_domain[0]['name']
u'sanremom\u0102\u017alheim.de'
>>> my_domain = cf.zones.get(params = { 'name': 'sanremomülheim.de' })
>>> my_domain[0]['name']
u'sanremom\u0102\u017alheim.de'
>>> dns_records = cf.zones.dns_records.get(my_domain[0]['id'])
>>> dns_records[0]['name']
u'www.sanremom\xfclheim.de'
>>> print my_domain[0]['name']
sanremomĂźlheim.de
>>> print dns_records[0]['name']
www.sanremomülheim.de

The way the domain name is represented is different, sanremom\xfclheim.de vs sanremom\u0102\u017alheim.de.

Is this a bug?

How could I test if my_domain[0]['name'] is in dns_records[0]['name'] in python?

Thanks,
Max

Error 6007 when POST-ing to /user/load_balancers/monitors

When executing cli4 -v --post expected_codes=2xx interval=15 retries=2 path=/status type=https /user/load_balancers/monitors, I get a json: cannot unmarshal string into Go value of type uint error as return. The example comes from the CTM Setup Instructions (load-balancing-early-access-guide-20161002.pdf), so the content should be valid.

Doing a POST to for an example /user/load_balancers/notifiers works, and creates a notifier as expected.

Is this likely to be a bug in cli4, a problem with Cloudflare's API, or an error on my part?

Command output:

robert@butch ~/s/m/s/cloudflare-ctm> cli4 --version
Cloudflare library version: 1.3.2
robert@butch ~/s/m/s/cloudflare-ctm> cli4 -v --post expected_codes=2xx interval=15 retries=2 path=/status type=https /user/load_balancers/monitors
2016-11-29 10:04:37,744 - Python Cloudflare API v4 - DEBUG - Call: user/load_balancers/monitors,None,None,None,None
2016-11-29 10:04:37,744 - Python Cloudflare API v4 - DEBUG - Call: optional params and data None {'expected_codes': '2xx', 'path': '/status', 'interval': '15', 'type': 'https', 'retries': '2'}
2016-11-29 10:04:37,744 - Python Cloudflare API v4 - DEBUG - Call: method and url POST https://api.cloudflare.com/client/v4/user/load_balancers/monitors
2016-11-29 10:04:37,744 - Python Cloudflare API v4 - DEBUG - Call: headers {'X-Auth-Email': 'REDACTED', 'X-Auth-Key': 'REDACTED', 'Content-Type': 'application/json'}
2016-11-29 10:04:37,744 - Python Cloudflare API v4 - DEBUG - Call: doit!
2016-11-29 10:04:38,734 - Python Cloudflare API v4 - DEBUG - Call: done!
2016-11-29 10:04:38,738 - Python Cloudflare API v4 - DEBUG - Response: url https://api.cloudflare.com/client/v4/user/load_balancers/monitors
2016-11-29 10:04:38,738 - Python Cloudflare API v4 - DEBUG - Response: data {
  "result": null,
  "success": false,
  "errors": [
    {
      "code": 6007,
      "error": "json: cannot unmarshal string into Go value of type uint"
    }
  ],
  "messages": []
}

2016-11-29 10:04:38,739 - Python Cloudflare API v4 - DEBUG - Response: error 6007 json: cannot unmarshal string into Go value of type uint
cli4: /user/load_balancers/monitors - 6007 json: cannot unmarshal string into Go value of type uint

zone list is slow

hi,

I have an account of 206 domains, while I try to get the zone list using the API, even with 'per_page' set to 50, I found that there are 50 requests for the first 50 zones.

However, if I use 'curl' to get the data using the same API, all 50 results would be returned in one single request.

thanks

Add support for Access API

I would like to be able to use Access Apps and Access Policy through the API client and the cli4 tool but it doesn't seem like it's in there.

If I wanted to add it myself is it just a matter of adding the right entries to to api_v4.py?

Getting "Failed to read certificate from Database" when trying to pull Origin CA certs

Works fine with the following curls:

curl -H "X-Auth-User-Service-Key: $CF_API_CERTKEY" "https://api.cloudflare.com/client/v4/certificates?zone_id=[ZONE_ID]"

curl -H "X-Auth-User-Service-Key: $CF_API_CERTKEY" "https://api.cloudflare.com/client/v4/certificates/[CERT_ID]

Am trying to call with cf.certificates.get(zone_id). User error or bug?

Issue trying to post new firewall rules

Hi guys,

Thanks for the excellent API work, love it so far.

I've been trying to create firewall rules using:

`` def ban_ip_for_zone_id(self, ip, zone_id):
rule = {'mode': 'block', 'configuration': {'target': 'ip', 'value': ip},
'notes': 'Automatic block placed via Python script for IP : ' + str(ip)}
print(json.dumps(rule))
self.cf.zones.firewall.access_rules.rules.post(zone_id, data=json.dumps(rule))

without a lot of success. I keep getting this error:
CloudFlare.exceptions.CloudFlareAPIError: firewallaccessrules.api.bad_json

The JSON I get back to send in is:
{"notes": "Automatic block placed via Python script for IP : 103.89.91.156", "configuration": {"target": "ip", "value": "103.89.91.156"}, "mode": "block"}

I already JSON linted this thing. Am I missing something on the data structure? Your input is appreciated.

--quiet option yields TypeError

Python 3.6.9 and 3.5.2 (virtual envs, cloudflare-2.3.1)

$ cli4 -q /ips
Traceback (most recent call last):
  File "/tmp/python3-venv-20191218/bin/cli4", line 8, in <module>
    sys.exit(main())
  File "/tmp/python3-venv-20191218/lib/python3.6/site-packages/cli4/__main__.py", line 13, in main
    cli4(args)
  File "/tmp/python3-venv-20191218/lib/python3.6/site-packages/cli4/cli4.py", line 367, in cli4
    do_it(args)
  File "/tmp/python3-venv-20191218/lib/python3.6/site-packages/cli4/cli4.py", line 362, in do_it
    write_results(results, output)
  File "/tmp/python3-venv-20191218/lib/python3.6/site-packages/cli4/cli4.py", line 199, in write_results
    sys.stdout.write(results)
TypeError: write() argument must be str, not dict

Or python 2.7.12 (Ubuntu 16.04 w/ pip install cloudflare, 2.3.1)

$ cli4 -q /ips
Traceback (most recent call last):
  File "/usr/local/bin/cli4", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python2.7/dist-packages/cli4/__main__.py", line 13, in main
    cli4(args)
  File "/usr/local/lib/python2.7/dist-packages/cli4/cli4.py", line 367, in cli4
    do_it(args)
  File "/usr/local/lib/python2.7/dist-packages/cli4/cli4.py", line 362, in do_it
    write_results(results, output)
  File "/usr/local/lib/python2.7/dist-packages/cli4/cli4.py", line 199, in write_results
    sys.stdout.write(results)
TypeError: expected a string or other character buffer object

The --quiet option sets output to None, but write_results doesn't handle this case. So in this example, results is a dict, is not modified within write_results, and so the call to sys.stdout.write throws the above exception because it is not a string.

Expected results: --quiet should not output anything, not even a newline

zones.purge_cache.post() returns an incomplete response

Accordingly to the oficial documentation [1], the API call to purge specific URLs of a zone should return a dictionary like this one:

{
  "success": true,
  "errors": [],
  "messages": [],
  "result": {
    "id": "9a7806061c88ada191ed06f989cc3dac"
  }
}

However, in the response object I'm receiving only the inner dictionary, the one that corresponds to the "result" key. I'm not receiving the parent dictionary with the other keys (success, errors, messages). Could this be a bug?

I'm using the version 2.1.0 of the library.
This is my code:

>>> r = cf.zones.purge_cache.post(
            my_zone_id, 
            data={'files': [
                'https://mydomain.com/image.jpg',
                'http://mydomain.com/otherimage.jpg']
            })
>>> print r
>>> {'id':'23fe45167b3dad00ae810cd597587da1'}
>>> print r.get('success')
>>> None

[1] https://api.cloudflare.com/#zone-purge-files-by-url

Allow for timeout and retry configuration of the underlying `requests.Session`

Environment

cloudflare==2.1.0
requests==2.18.4

Current behavior

Using this module from inside China means that requests going to the Cloudflare API are quite unreliable. We find our code based on this module behaves consistently when run from outside of China, but in China will often hang for the first request, causing our script to run for a very long time. Since we run this script every minute on our ops server to update DNS records, if this script runs long, we will get an accumulation of instances running the script, which would eventually overload the server.

This one run got stuck for over 8 minutes:

^CTraceback (most recent call last):
  File "/srv/managebac/shared/bin/cloudflare_update.py", line 100, in <module>
    cf_zone = cf_get_zone(cf_client, zone_domain)
  File "/srv/managebac/shared/bin/cloudflare_update.py", line 37, in cf_get_zone
    zones = cf_client.zones.get(params={"name": zone_domain})
  File "/usr/local/lib/python2.7/dist-packages/CloudFlare/cloudflare.py", line 618, in get
    params, data)
  File "/usr/local/lib/python2.7/dist-packages/CloudFlare/cloudflare.py", line 75, in call_with_auth
    params, data, files)
  File "/usr/local/lib/python2.7/dist-packages/CloudFlare/cloudflare.py", line 424, in _call
    params, data, files)
  File "/usr/local/lib/python2.7/dist-packages/CloudFlare/cloudflare.py", line 319, in _raw
    params, data, files)
  File "/usr/local/lib/python2.7/dist-packages/CloudFlare/cloudflare.py", line 188, in _network
    data=data)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 521, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 850, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connection.py", line 284, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python2.7/dist-packages/urllib3/connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python2.7/dist-packages/urllib3/util/connection.py", line 73, in create_connection
    sock.connect(sa)
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
KeyboardInterrupt

real	8m9.595s
user	0m0.268s
sys	0m0.032s

Expected behavior

The library should follow the advice from http://docs.python-requests.org/en/master/user/quickstart/#timeouts and set a default timeout for all requests, and, if possible, make this user-configurable.

You can tell Requests to stop waiting for a response after a given number of seconds with the timeout parameter. Nearly all production code should use this parameter in nearly all requests. Failure to do so can cause your program to hang indefinitely:

A "nice to have" would be for the library to retry failed requests using urllib's HTTPAdapter. See http://docs.python-requests.org/en/master/_modules/requests/adapters/ and https://stackoverflow.com/a/15431343/3409092.

Error trying to change zone setting "always_use_https"

I can successfully change other similar zone settings, like "automatic_https_rewrites", and it works perfectly, however when I try to change "always_use_https" I receive this error:

Traceback (most recent call last): File "/home/gonguinguen/medios/gluon/restricted.py", line 227, in restricted exec ccode in environment File "/home/gonguinguen/medios/applications/webmedios/controllers/admin.py", line 598, in <module> File "/home/gonguinguen/medios/gluon/globals.py", line 393, in <lambda> self._caller = lambda f: f() File "/home/gonguinguen/medios/applications/webmedios/controllers/admin.py", line 593, in test cf.zones.settings.always_use_https.patch(zone_id, data={"value": "on"}) AttributeError: '_add_with_auth' object has no attribute 'always_use_https'

Notice the call I use is the same for both settings, as told by the official Cloudflare documentation. But the error happens only with "always_use_https".

I tried doing my self the call using python requests.

import requests
result = requests.patch('https://api.cloudflare.com/client/v4/zones/%s/settings/always_use_https' % 
    zone_id,
    data={'value': 'off'},
    headers={
        "Content-Type": "application/json",
        "X-Auth-Key": cloudflare_user_api_key,
        "X-Auth-Email": cloudflare_email
    }
)

But this returns the error code 6007 with the message "Malformed JSON in request body"

I've contacted Cloudflare support, and they told me that the call is right.
Is this a bug in python-cloudflare library? Could it be related to requests module?

UPDATE
I've tried directly using the curl command and it works ok, so it isn't a bug in the Cloudflare API. Also, I've tried with several different zones, and the problem remains.

Python 3.8 warnings about "is with a literal"

Python 3.8 is now displaying warnings when is is used with a literal.

Here are the warnings with 2.4.0:

/usr/lib/python3.8/site-packages/CloudFlare/cloudflare.py:839: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if email is '':
/usr/lib/python3.8/site-packages/CloudFlare/cloudflare.py:841: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if token is '':
/usr/lib/python3.8/site-packages/CloudFlare/cloudflare.py:843: SyntaxWarning: "is" with a literal. Did you mean "=="?
  if certtoken is '':

See python/cpython#9642 for more info

I am also submitting a PR with a fix.

Thanks

Trying to use X-Auth-Key instead of X-Auth-Token?

Hi,

First time user of this module, with v. 2.3.0 from pypi.

I am doing the very simple example of listing zones. I generated an API token.

 def get_cf_dns(cftoken):                          
      cf = CloudFlare.CloudFlare(debug=True, token=cftoken)                                                                                        
      zones = cf.zones.get()                        
      for zone in zones:                            
          zone_id = zone['id']                      
          zone_name = zone['name']                  
          print(zone_id, zone_name)

The output throws me off with

2019-10-21 19:58:22,764 - Python Cloudflare API v4 - DEBUG - Response: url https://api.cloudflare.com/client/v4/zones
2019-10-21 19:58:22,764 - Python Cloudflare API v4 - DEBUG - Response: 400, application/json, {"success":false,"errors":[{"code":6003,"message":"Invalid request headers","error_chain":[{"code":6103,"message":"Invalid format for X-Auth-Key header"}]}],"messages":[],"result":null}
2019-10-21 19:58:22,764 - Python Cloudflare API v4 - DEBUG - Response: error 6003 Invalid request headers
Traceback (most recent call last):
  File "./main.py", line 39, in <module>
    get_cf_dns(cftoken)
  File "./main.py", line 30, in get_cf_dns
    zones = cf.zones.get()
  File "/home/xxx/.virtualenvs/cloudflare_vault_py-a7RkIZod/lib/python3.7/site-packages/CloudFlare/cloudflare.py", line 618, in get
    params, data)
  File "/home/xxx/.virtualenvs/cloudflare_vault_py-a7RkIZod/lib/python3.7/site-packages/CloudFlare/cloudflare.py", line 75, in call_with_auth
    params, data, files)
  File "/home/xxx/.virtualenvs/cloudflare_vault_py-a7RkIZod/lib/python3.7/site-packages/CloudFlare/cloudflare.py", line 467, in _call
    raise CloudFlareAPIError(code, message, error_chain)
CloudFlare.exceptions.CloudFlareAPIError: Invalid request headers

It's talking about the X-Auth-Key header, whereas I am guessing it should not since it is really a token and I am not passing any email parameter?

I validated my token against CF with curl, all good and sound:

{
  "result": {
    "id": "xxx",
    "status": "active"
  },
  "success": true,
  "errors": [],
  "messages": [
    {
      "code": 10000,
      "message": "This API Token is valid and active",
      "type": null
    }
  ]
}

Thanks.

No firewall.rules in module

I trying to use this api method, but i get error:

Traceback (most recent call last):
  File "Projects/tg/cloudflare_bot/main.py", line 138, in <module>
    pprint(cf.get_fw_rules(data['id']))
  File "Projects/tg/cloudflare_bot/main.py", line 28, in get_fw_rules
    return self.zones.firewall.rules.get(identifier)
AttributeError: '_add_unused' object has no attribute 'rules'

Code:


class CFAPI(CloudFlare):

    def __init__(self, token, email):
        super().__init__(token=token, email=email, debug=True)

    def get_fw_rules(self, identifier):
        #print(dir(self.zones.firewall))
        return self.zones.firewall.rules.get(identifier)

cf = CFAPI(CLOUDFLARE_API, CLOUDFLARE_EMAIL)
pprint(cf.get_fw_rules(data['id']))

I tried to check this with dir(self.zones.firewall), but I didn’t get rules in the list. Can you fix this?

Support the accounts endpoint

From: https://api.cloudflare.com/#organizations-properties

NOTE: This API is deprecated, please use equivalent /accounts API endpoints where possible. Account APIs provide a broader range of features, and are backwards-compatible to organization APIs.
>>> cf.accounts.get()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'CloudFlare' object has no attribute 'accounts'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.