Reply

API to feed a RPZ from a CSV file

New Member
Posts: 3
5788     0

Hi!!

Do you know the API call to import a CSV file to load into a Infoblox RPZ zone ?

 

Thanks

Re: API to feed a RPZ from a CSV file

Moderator
Moderator
Posts: 321
5789     0

See this thread also:

 

https://community.infoblox.com/t5/API-Integration/Importing-a-CSV-file-to-Infoblox-using-webAPI/m-p/... 

 

#
# CSV Upload for RPZ records
#
# file to be uploaded is "rpz.csv in the same directory
#

# generate an upload url

curl -k1 -u admin:infoblox -X POST "https://192.168.1.2/wapi/v2.8/fileop?_function=uploadinit&filename=rpz_import.csv"

# returns:

{
    "token": "eJytUEFOwzAQvPsj7aVJNkmdhltRqYSECmrhvEpsp1hK7MV2qpbXYyPBkRMHW+OdHc/uCGHphk6d\nWbyENT64WQTrGAFbCm0G24/2mlmj0gk3Up7tutDhUQ2MSiYQ+1mPQRtEJrUIjCq2lFSz00JdSbsb\nBj2pBaM128O6gbLiBS+yuuQt3wDzp8XsxkjzKHgPgfxdnkORtRnwjOepglLH0QIOelSobe7UB2q5\nent5et7uVlAAh6ps6qKGtoQyd/SJeiLrQib8Jf7cJBMtI9pEj7/VsalN7TKuGDEUUZH7mEd3VnmY\n6F8GAvixQGWEldqcU7WMXvePhwSr34bJyhQepDx329ctHh/26b1mPhwJeMoamkgOWo3SY7Ao7ESd\n+1Zt2CHt3neE2tAc8KKc19Ykro1cn30B6CueVQ==\n",
    "url": "https://192.168.1.2/http_direct_file_io/req_id-UPLOAD-1016132740419212/rpz_import.csv"
}

# upload the file to the url returned above

curl -k1 -u admin:infoblox -X POST -F "name=rpz.csv" -F "filedata=@./rpz_import.csv" "https://192.168.1.2/http_direct_file_io/req_id-UPLOAD-1016132740419212/rpz_import.csv"


# start the import using the token returned above

curl -k1 -u admin:infoblox -X POST "https://192.168.1.2/wapi/v2.8/fileop?_function=csv_import" -H "Content-Type: application/json" -d \
'{
    "action":"START",
    "operation":"INSERT",
    "token": "eJytUEFOwzAQvPsj7aVJNkmdhltRqYSECmrhvEpsp1hK7MV2qpbXYyPBkRMHW+OdHc/uCGHphk6d\nWbyENT64WQTrGAFbCm0G24/2mlmj0gk3Up7tutDhUQ2MSiYQ+1mPQRtEJrUIjCq2lFSz00JdSbsb\nBj2pBaM128O6gbLiBS+yuuQt3wDzp8XsxkjzKHgPgfxdnkORtRnwjOepglLH0QIOelSobe7UB2q5\nent5et7uVlAAh6ps6qKGtoQyd/SJeiLrQib8Jf7cJBMtI9pEj7/VsalN7TKuGDEUUZH7mEd3VnmY\n6F8GAvixQGWEldqcU7WMXvePhwSr34bJyhQepDx329ctHh/26b1mPhwJeMoamkgOWo3SY7Ao7ESd\n+1Zt2CHt3neE2tAc8KKc19Ykro1cn30B6CueVQ==\n",
    "on_error": "CONTINUE"
}'

# returns

{
    "csv_import_task": {
        "_ref": "csvimporttask/b25lLmNzdl9pbXBvcnRfdGFzayQxNQ:2",
        "admin_name": "admin",
        "file_name": "rpz_import.csv",
        "file_size": 259,
        "import_id": 15,
        "lines_failed": 0,
        "lines_processed": 0,
        "lines_warning": 0,
        "on_error": "CONTINUE",
        "operation": "INSERT",
        "separator": "COMMA",
        "start_time": 1571232519,
        "status": "PENDING",
        "update_method": "OVERRIDE"
    }
}
# check on the task

curl -k1 -u admin:infoblox -X GET "https://192.168.1.2/wapi/v2.8/csvimporttask/b25lLmNzdl9pbXBvcnRfdGFzayQxNQ:2"

# returns

{
    "_ref": "csvimporttask/b25lLmNzdl9pbXBvcnRfdGFzayQxNQ:2",
    "admin_name": "admin",
    "end_time": 1571232527,
    "file_name": "rpz_import.csv",
    "file_size": 259,
    "import_id": 15,
    "lines_failed": 0,
    "lines_processed": 3,
    "lines_warning": 0,
    "on_error": "CONTINUE",
    "operation": "INSERT",
    "separator": "COMMA",
    "start_time": 1571232526,
    "status": "COMPLETED",
    "update_method": "OVERRIDE"
}

 

Re: API to feed a RPZ from a CSV file

Moderator
Moderator
Posts: 321
5789     0

Here's the example CSV file:

 

header-responsepolicycnamerecord,fqdn*,_new_fqdn,canonical_name,comment,disabled,parent_zone,ttl,view
responsepolicycnamerecord,*.example.com.blacklist,,,,FALSE,blacklist,,Internal
responsepolicycnamerecord,example.com.blacklist,,,,FALSE,blacklist,,Internal

Re: API to feed a RPZ from a CSV file

New Member
Posts: 3
5789     0

Hello,

I am trying to import domains to an RPZ from a stream object instead of a CSV file, but when I do it I get the error that the file header is not valid.

 

{
"text": "CSV Import file is not valid. First line of the file must be either a valid version number or a valid header line.",
"code": "Client.Ibap.Data",
"Error": "AdmConDataError: None (IBDataError: IB.Data:CSV Import file is not valid. First line of the file must be either a valid version number or a valid header line.)"
}

 

Below is the Python2 code I wrote for the purpose. Do you know what the problem might be? The ib_file object, which is being imported, has the following format, which is the correct CSV format, but for some reason it cannot be imported via Python.

 

header-responsepolicycnamerecord,fqdn*,canonical_name,disabled,parent_zone,view
responsepolicycnamerecord,blah.com.threatqrpz,blah.com,FALSE,threatqrpz,default
responsepolicycnamerecord,duck.duck.org.threatqrpz,duck.duck.org,FALSE,threatqrpz,default
responsepolicycnamerecord,badguy.com.threatqrpz,badguy.com,FALSE,threatqrpz,default

 

##### Re-write the API call to use StringIO instead of one IOC at a time
iocs = ['blah.com', 'duck.duck.org', 'badguy.com']
infoblox_rpz = 'threatqrpz'
dns_view = 'default'
ib_filename = 'infoblox_test_add'

# Add all the domains to a list
ls = [[u'responsepolicycnamerecord', u'{}.{}'.format(data_value, infoblox_rpz), u'{}'.format(data_value), u'FALSE', u'{}'.format(infoblox_rpz), u'{}'.format(dns_view)] for data_value in iocs]
ls.insert(0, [u'header-responsepolicycnamerecord', u'fqdn*', u'canonical_name', u'disabled', u'parent_zone', u'view'])

# Build a stream object
ib_file = None
output = io.BytesIO()
writer = csv.writer(output, delimiter=',')
writer.writerows(ls)
ib_file = output.getvalue()


### 1) Generate an upload url
ep = 'https://{}/wapi/v2.10/fileop'.format(ib_host)
params = {
'_function': 'uploadinit',
'filename': '{}.csv'.format(ib_filename)
}

resp_auth = api_session.post(ep, params=params)

# Get the auth token and the upload URL
token = resp_auth.json().get('token', '')
upload_url = resp_auth.json().get('url', '')

if resp_auth.status_code == 200 and not token:
print 'Infoblox did not return an authentication token. Exiting...'

if resp_auth.status_code == 200 and not upload_url:
print 'Infoblox did not return an upload URL. Exiting...'


### 2) Upload the file to the url returned above
f = {'file': ('{}.csv'.format(ib_filename), ib_file)}

resp_upload = api_session.post(upload_url, files=f)

if not resp_upload.status_code == 200:
print 'The upload failed'


### 3) Start the import
ib_action = 'add'

ib_map = {
'add': 'INSERT',
'merge': 'MERGE',
'delete': 'DELETE'
}

ep = 'https://{}/wapi/v2.10/fileop'.format(ib_host)

params = {
'_function': 'csv_import'
}

data = {
'action': 'START',
'operation': ib_map[ib_action],
'on_error': 'CONTINUE',
'token': token
}

resp_start = api_session.post(
ep,
params=params,
data=json.dumps(data)
)  

 

Showing results for 
Search instead for 
Did you mean: 

Recommended for You