[GH-ISSUE #88] API Multiple Connections #1

Closed
opened 2026-02-27 23:18:38 +03:00 by kerem · 2 comments
Owner

Originally created by @mgesca46 on GitHub (Nov 26, 2024).
Original GitHub issue: https://github.com/sophos/sophos-firewall-sdk/issues/88

Hi,

i have a question about the API with the following code:

from sophosfirewall_python.firewallapi import SophosFirewall, SophosFirewallZeroRecords, SophosFirewallAuthFailure, SophosFirewallAPIError
from flask import Flask
from flask import request

# Config Sophos API
sfos = SophosFirewall(
    username = "xxx",
    password = "xxxx",
    hostname = "xxxx",
    port = xxxx,
    verify = True
    )

def blockipminew(ips):
   # API Sophos
   print("--- Sophos API ---")
   try:
       response = sfos.login()
       print(f"Authentication Success MI New! {response}")
   except SophosFirewallAuthFailure as e:
       print(f"Authentication error MI New: {e}")
   try:
       response = sfos.get_ip_host(ip_address=ips)
   except SophosFirewallZeroRecords as e:
       print(f"IP {ips} Not Present on FW MI New")
       try:
           sfos.create_ip_host(name=f'BlockedIP-{ips}',ip_address=ips)
           print(f"IP:{ips} created on FW MI New")
       except SophosFirewallAPIError as sfae:
           print(f"{sfae}")
       try:
           print ("Update BlockedIPs Host Group on FW MI New")
           siplist = f'BlockedIP-{ips}'
           siplist = siplist.split()
           sfos.update_ip_hostgroup(name='BlockedIPs',host_list=siplist, action= 'add')
           print(f"IP {ips} created and updated on FW MI New")
       except SophosFirewallAPIError as sfae:
           sfos.remove(xml_tag="IPHost", name=f'BlockedIP-{ips}')

app = Flask(__name__)
@app.route('/', methods=['POST'])

def main():

    if request.method == 'POST':
        request_jdata = request.get_json()
        stream = request_jdata['event']['source_streams'][0]
        if stream == '659424f982907a72b3d57a02':
            sip = request_jdata['event']['fields']['Source_IP']
            blockipminew(sip)
            return "success", 200

if __name__ == '__main__':
  app.run(port=5001, host="0.0.0.0")

when i made a single POST request the API is able to create the ip host and update the host group.
But when a send 3/4 POST request at the same time the API is able to create the IP Host but is not able to update the host group.

This is occurred also with multiprocessing and threading.

Can you help me?

Thank you

Originally created by @mgesca46 on GitHub (Nov 26, 2024). Original GitHub issue: https://github.com/sophos/sophos-firewall-sdk/issues/88 Hi, i have a question about the API with the following code: ```python from sophosfirewall_python.firewallapi import SophosFirewall, SophosFirewallZeroRecords, SophosFirewallAuthFailure, SophosFirewallAPIError from flask import Flask from flask import request # Config Sophos API sfos = SophosFirewall( username = "xxx", password = "xxxx", hostname = "xxxx", port = xxxx, verify = True ) def blockipminew(ips): # API Sophos print("--- Sophos API ---") try: response = sfos.login() print(f"Authentication Success MI New! {response}") except SophosFirewallAuthFailure as e: print(f"Authentication error MI New: {e}") try: response = sfos.get_ip_host(ip_address=ips) except SophosFirewallZeroRecords as e: print(f"IP {ips} Not Present on FW MI New") try: sfos.create_ip_host(name=f'BlockedIP-{ips}',ip_address=ips) print(f"IP:{ips} created on FW MI New") except SophosFirewallAPIError as sfae: print(f"{sfae}") try: print ("Update BlockedIPs Host Group on FW MI New") siplist = f'BlockedIP-{ips}' siplist = siplist.split() sfos.update_ip_hostgroup(name='BlockedIPs',host_list=siplist, action= 'add') print(f"IP {ips} created and updated on FW MI New") except SophosFirewallAPIError as sfae: sfos.remove(xml_tag="IPHost", name=f'BlockedIP-{ips}') app = Flask(__name__) @app.route('/', methods=['POST']) def main(): if request.method == 'POST': request_jdata = request.get_json() stream = request_jdata['event']['source_streams'][0] if stream == '659424f982907a72b3d57a02': sip = request_jdata['event']['fields']['Source_IP'] blockipminew(sip) return "success", 200 if __name__ == '__main__': app.run(port=5001, host="0.0.0.0") ``` when i made a single POST request the API is able to create the ip host and update the host group. But when a send 3/4 POST request at the same time the API is able to create the IP Host but is not able to update the host group. This is occurred also with multiprocessing and threading. Can you help me? Thank you
kerem closed this issue 2026-02-27 23:18:38 +03:00
Author
Owner

@mamullen13316 commented on GitHub (Dec 11, 2024):

The update method works by first doing a get on the existing hostgroup, extracting the current members of the hostgroup and adding the new one to it. Then it submits the payload including all of the members. It has to be done that way due to how the API currently works. There's no "add to list" feature when updating a list in the API. If you try to use set operation of "update" and only include a new host in the payload, any existing members get erased. You have to first get the existing and add them to the payload along with any new additions.

Trying to update the same group at the same time might yield inconsistent results because of this. If two processes start at the same time updating a hostgroup, the second process might not see the host added by the first process when it does the get, because the first process didn't complete yet. Is that what you mean by it is not able to update the host group? One or more of the additions is missing?

I'd suggest doing some type of task queue to ensure that the updates are performed in the sequence that they are submitted to the app. For example: https://medium.com/@Aman-tech/celery-with-flask-d1f1c555ceb7

<!-- gh-comment-id:2536883589 --> @mamullen13316 commented on GitHub (Dec 11, 2024): The update method works by first doing a get on the existing hostgroup, extracting the current members of the hostgroup and adding the new one to it. Then it submits the payload including all of the members. It has to be done that way due to how the API currently works. There's no "add to list" feature when updating a list in the API. If you try to use set operation of "update" and only include a new host in the payload, any existing members get erased. You have to first get the existing and add them to the payload along with any new additions. Trying to update the same group at the same time might yield inconsistent results because of this. If two processes start at the same time updating a hostgroup, the second process might not see the host added by the first process when it does the get, because the first process didn't complete yet. Is that what you mean by it is not able to update the host group? One or more of the additions is missing? I'd suggest doing some type of task queue to ensure that the updates are performed in the sequence that they are submitted to the app. For example: https://medium.com/@Aman-tech/celery-with-flask-d1f1c555ceb7
Author
Owner

@mgesca46 commented on GitHub (Dec 12, 2024):

Hi @mamullen13316 ,

thank you for your reply.
I just changed the code to celery days ago.
Now seems to be working.

Thank you for your information.

I close the issue

<!-- gh-comment-id:2538185758 --> @mgesca46 commented on GitHub (Dec 12, 2024): Hi @mamullen13316 , thank you for your reply. I just changed the code to celery days ago. Now seems to be working. Thank you for your information. I close the issue
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/sophos-firewall-sdk#1
No description provided.