SteveMcGrath/pySecurityCenter

Error with SC 5.3.2

Closed this issue · 6 comments

Having issues logging into SC5 with pySecurityCenter. Here is an issue when running example ips_in_asset_lists.py.

Traceback (most recent call last):
File "ips_in_asset_lists.py", line 41, in
sc.login(username, password)
File "/Library/Python/2.7/site-packages/securitycenter/sc5.py", line 45, in login
self._token = resp.json()['response']['token']
KeyError: 'token'

Python 2.7.12
Security Center 5.3.2

I cannot replicate this, can you give further details? maybe some example code?

Here's my code.

#!/usr/bin/env/python

from boto import ec2
import getpass
from securitycenter import SecurityCenter5

def get_AWS_instances(AWS_Access_Key,AWS_Secret_Key):

    AWS_ACCESS_KEY_ID=AWS_Access_Key
    AWS_SECRET_ACCESS_KEY=AWS_Secret_Key
    ip_list = []
    noscan = 0

    ec2conn = ec2.connection.EC2Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    reservations = ec2conn.get_all_instances()
    instances = [i for r in reservations for i in r.instances]

    for i in instances:
        size = str(i.instance_type)
        current_state = str(i.state)

        if 'running' in current_state:
            if '.micro' in size:
                noscan += 1
            elif '.small' in size:
                noscan += 1
            else:
                ip_list.append(str(i.private_ip_address))

    print "Not including " + str(noscan) + " instances in scan profile."

    return ip_list


def add_asset(sc, asset_name, asset_ips, asset_description):
    sc.post('asset', json={
        'context': '',
        'createdTime': 0,
        'modifiedTime': 0,
        'status': -1,
        'type': 'static',
        'name': asset_name,
        'definedIPs': asset_ips,
        'description': asset_description,
        'tags': "",
    })

    print '** Imported %s using %s' % (asset_name, asset_ips)


def update_asset(sc, asset_id, asset_ips_updates):

    # Example Use:
    #   sc.patch('asset/45', json={'definedIPs':'10.0.0.1,10.0.0.2,10.0.0.3'})

    sc.patch('asset/%s' % asset_id, json={'definedIPs': ','.join(asset_ips_updates)})

    print "Asset : %s updated with %s IP Addresses : %s" % (asset_id, str(len(asset_ips_updates)), ','.join(asset_ips_updates))


if __name__ == '__main__':
    # Get the needed login info from the user...
    host = raw_input('SecurityCenter Server : ')
    scname = raw_input('SecurityCenter Username : ')
    scpass = getpass.getpass('SecurityCenter Password : ')
    AWS_Access_Key = getpass.getpass('AWS_ACCESS_KEY_ID : ')
    AWS_Secret_Key = getpass.getpass('AWS_SECRET_ACCESS_KEY : ')

    # Get all running instances, minus micros and smalls, from AWS account.
    available_ips = get_AWS_instances(AWS_Access_Key,AWS_Secret_Key)

    # Log into Security Center
    sc = SecurityCenter5(host)
    sc.login(scname,scpass)

    # Update SC5 asset record
    update_asset(sc, str(45), available_ips)

I updated my pysecuritycenter to the latest version and still getting the following error. Odd thing is, this above code works for one account and will not work for my 2 other AWS accounts.

Traceback (most recent call last):
File "AWS_to_SC5.py", line 76, in
sc.login(scname,scpass)
File "/Library/Python/2.7/site-packages/securitycenter/sc5.py", line 45, in login
self._token = resp.json()['response']['token']
KeyError: 'token'

is the login information valid?

Yes. Found the issue. We enabled session management with a limit of 1 session per each user account. The account being used for the update was logged in when the script kicked off. I was able to update all of my asset groups.

Glad to hear that the issue was external to the API ;) I'll close the issue now.