Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SoftLayer/CLI/event_log/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Audit Logs."""
47 changes: 47 additions & 0 deletions SoftLayer/CLI/event_log/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Get Audit Logs."""
# :license: MIT, see LICENSE for more details.

import json

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting

COLUMNS = ['event', 'label', 'date', 'metadata']


@click.command()
@click.option('--date-min', '-d',
help='The earliest date we want to search for audit logs in mm/dd/yyyy format.')
@click.option('--date-max', '-D',
help='The latest date we want to search for audit logs in mm/dd/yyyy format.')
@click.option('--obj_event', '-e',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these other options still need to be switched over to use - instead of _ as well

help="The event we want to get audit logs for")
@click.option('--obj_id', '-i',
help="The id of the object we want to get audit logs for")
@click.option('--obj_type', '-t',
help="The type of the object we want to get audit logs for")
@click.option('--utc_offset', '-z',
help="UTC Offset for seatching with dates. The default is -0000")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

searching is misspelled here

@environment.pass_env
def cli(env, date_min, date_max, obj_event, obj_id, obj_type, utc_offset):
"""Get Audit Logs"""
mgr = SoftLayer.EventLogManager(env.client)

request_filter = mgr.build_filter(date_min, date_max, obj_event, obj_id, obj_type, utc_offset)
logs = mgr.get_event_logs(request_filter)

table = formatting.Table(COLUMNS)
table.align['metadata'] = "l"

for log in logs:
try:
metadata = json.dumps(json.loads(log['metaData']), indent=4, sort_keys=True)
except ValueError:
metadata = log['metaData']

table.add_row([log['eventName'], log['label'], log['eventCreateDate'], metadata])

env.fout(table)
26 changes: 26 additions & 0 deletions SoftLayer/CLI/event_log/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Get Audit Log Types."""
# :license: MIT, see LICENSE for more details.

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting

COLUMNS = ['types']


@click.command()
@environment.pass_env
def cli(env):
"""Get Audit Log Types"""
mgr = SoftLayer.EventLogManager(env.client)

event_log_types = mgr.get_event_log_types()

table = formatting.Table(COLUMNS)

for event_log_type in event_log_types:
table.add_row([event_log_type])

env.fout(table)
5 changes: 5 additions & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
('block:volume-order', 'SoftLayer.CLI.block.order:cli'),
('block:volume-set-lun-id', 'SoftLayer.CLI.block.lun:cli'),

('audit-log', 'SoftLayer.CLI.event_log'),
('audit-log:get', 'SoftLayer.CLI.event_log.get:cli'),
('audit-log:types', 'SoftLayer.CLI.event_log.types:cli'),

('file', 'SoftLayer.CLI.file'),
('file:access-authorize', 'SoftLayer.CLI.file.access.authorize:cli'),
('file:access-list', 'SoftLayer.CLI.file.access.list:cli'),
Expand Down Expand Up @@ -248,6 +252,7 @@
'SoftLayer.CLI.securitygroup.interface:add'),
('securitygroup:interface-remove',
'SoftLayer.CLI.securitygroup.interface:remove'),
('securitygroup:audit-log', 'SoftLayer.CLI.securitygroup.event_log:get_by_request_id'),

('sshkey', 'SoftLayer.CLI.sshkey'),
('sshkey:add', 'SoftLayer.CLI.sshkey.add:cli'),
Expand Down
32 changes: 32 additions & 0 deletions SoftLayer/CLI/securitygroup/event_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Get event logs relating to security groups"""
# :license: MIT, see LICENSE for more details.

import json

import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting

COLUMNS = ['event', 'label', 'date', 'metadata']


@click.command()
@click.argument('request_id')
@environment.pass_env
def get_by_request_id(env, request_id):
"""Search for event logs by request id"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(venv) ryans-mbp-2:softlayer-python rlrossit$ slcli sg audit-log -h
Usage: slcli sg audit-log [OPTIONS] REQUEST_ID

  Search for event logs by request id

Having it be called sg audit log, with the help text then showing "Search for event logs" is kind of confusing. It looks like throughout most of this PR audit log and event log are being used interchangeably. I'd settle on one of the names, and use it everywhere (file/class names, CLI, etc.)

mgr = SoftLayer.NetworkManager(env.client)

logs = mgr.get_event_logs_by_request_id(request_id)

table = formatting.Table(COLUMNS)
table.align['metadata'] = "l"

for log in logs:
metadata = json.dumps(json.loads(log['metaData']), indent=4, sort_keys=True)

table.add_row([log['eventName'], log['label'], log['eventCreateDate'], metadata])

env.fout(table)
12 changes: 12 additions & 0 deletions SoftLayer/CLI/securitygroup/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
'interface',
'ipAddress', ]

REQUEST_COLUMNS = ['requestId']


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -95,6 +97,11 @@ def add(env, securitygroup_id, network_component, server, interface):
if not success:
raise exceptions.CLIAbort("Could not attach network component")

table = formatting.Table(REQUEST_COLUMNS)
table.add_row([success['requestId']])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand All @@ -118,6 +125,11 @@ def remove(env, securitygroup_id, network_component, server, interface):
if not success:
raise exceptions.CLIAbort("Could not detach network component")

table = formatting.Table(REQUEST_COLUMNS)
table.add_row([success['requestId']])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename the success variable returned from detach, since using it in this situation reads weird. I'd just use ret like all of the other functions are using.


env.fout(table)


def _validate_args(network_component, server, interface):
use_server = bool(server and interface and not network_component)
Expand Down
27 changes: 25 additions & 2 deletions SoftLayer/CLI/securitygroup/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
'portRangeMax',
'protocol']

REQUEST_BOOL_COLUMNS = ['requestId', 'response']
REQUEST_RULES_COLUMNS = ['requestId', 'rules']


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -85,6 +88,11 @@ def add(env, securitygroup_id, remote_ip, remote_group,
if not ret:
raise exceptions.CLIAbort("Failed to add security group rule")

table = formatting.Table(REQUEST_RULES_COLUMNS)
table.add_row([ret['requestId'], str(ret['rules'])])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand Down Expand Up @@ -125,9 +133,16 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group,
if protocol:
data['protocol'] = protocol

if not mgr.edit_securitygroup_rule(securitygroup_id, rule_id, **data):
ret = mgr.edit_securitygroup_rule(securitygroup_id, rule_id, **data)

if not ret:
raise exceptions.CLIAbort("Failed to edit security group rule")

table = formatting.Table(REQUEST_BOOL_COLUMNS)
table.add_row([ret['requestId']])

env.fout(table)


@click.command()
@click.argument('securitygroup_id')
Expand All @@ -136,5 +151,13 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group,
def remove(env, securitygroup_id, rule_id):
"""Remove a rule from a security group."""
mgr = SoftLayer.NetworkManager(env.client)
if not mgr.remove_securitygroup_rule(securitygroup_id, rule_id):

ret = mgr.remove_securitygroup_rule(securitygroup_id, rule_id)

if not ret:
raise exceptions.CLIAbort("Failed to remove security group rule")

table = formatting.Table(REQUEST_BOOL_COLUMNS)
table.add_row([ret['requestId']])

env.fout(table)
127 changes: 127 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Event_Log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
getAllObjects = [
{
'accountId': 100,
'eventCreateDate': '2017-10-23T14:22:36.221541-05:00',
'eventName': 'Disable Port',
'ipAddress': '192.168.0.1',
'label': 'test.softlayer.com',
'metaData': '',
'objectId': 300,
'objectName': 'CCI',
'traceId': '100',
'userId': '',
'userType': 'SYSTEM'
},
{
'accountId': 100,
'eventCreateDate': '2017-10-18T09:40:41.830338-05:00',
'eventName': 'Security Group Rule Added',
'ipAddress': '192.168.0.1',
'label': 'test.softlayer.com',
'metaData': '{"securityGroupId":"200",'
'"securityGroupName":"test_SG",'
'"networkComponentId":"100",'
'"networkInterfaceType":"public",'
'"requestId":"53d0b91d392864e062f4958",'
'"rules":[{"ruleId":"100",'
'"remoteIp":null,"remoteGroupId":null,"direction":"ingress",'
'"ethertype":"IPv4",'
'"portRangeMin":2000,"portRangeMax":2001,"protocol":"tcp"}]}',
'objectId': 300,
'objectName': 'CCI',
'traceId': '59e767e9c2184',
'userId': 400,
'userType': 'CUSTOMER',
'username': 'user'
},
{
'accountId': 100,
'eventCreateDate': '2017-10-18T09:40:32.238869-05:00',
'eventName': 'Security Group Added',
'ipAddress': '192.168.0.1',
'label': 'test.softlayer.com',
'metaData': '{"securityGroupId":"200",'
'"securityGroupName":"test_SG",'
'"networkComponentId":"100",'
'"networkInterfaceType":"public",'
'"requestId":"96c9b47b9e102d2e1d81fba"}',
'objectId': 300,
'objectName': 'CCI',
'traceId': '59e767e03a57e',
'userId': 400,
'userType': 'CUSTOMER',
'username': 'user'
},
{
'accountId': 100,
'eventCreateDate': '2017-10-18T10:42:13.089536-05:00',
'eventName': 'Security Group Rule(s) Removed',
'ipAddress': '192.168.0.1',
'label': 'test_SG',
'metaData': '{"requestId":"2abda7ca97e5a1444cae0b9",'
'"rules":[{"ruleId":"800",'
'"remoteIp":null,"remoteGroupId":null,"direction":"ingress",'
'"ethertype":"IPv4",'
'"portRangeMin":2000,"portRangeMax":2001,"protocol":"tcp"}]}',
'objectId': 700,
'objectName': 'Security Group',
'traceId': '59e7765515e28',
'userId': 400,
'userType': 'CUSTOMER',
'username': 'user'
},
{
'accountId': 100,
'eventCreateDate': '2017-10-18T10:42:11.679736-05:00',
'eventName': 'Network Component Removed from Security Group',
'ipAddress': '192.168.0.1',
'label': 'test_SG',
'metaData': '{"requestId":"6b9a87a9ab8ac9a22e87a00",'
'"fullyQualifiedDomainName":"test.softlayer.com",'
'"networkComponentId":"100",'
'"networkInterfaceType":"public"}',
'objectId': 700,
'objectName': 'Security Group',
'traceId': '59e77653a1e5f',
'userId': 400,
'userType': 'CUSTOMER',
'username': 'user'
},
{
'accountId': 100,
'eventCreateDate': '2017-10-18T10:41:49.802498-05:00',
'eventName': 'Security Group Rule(s) Added',
'ipAddress': '192.168.0.1',
'label': 'test_SG',
'metaData': '{"requestId":"0a293c1c3e59e4471da6495",'
'"rules":[{"ruleId":"800",'
'"remoteIp":null,"remoteGroupId":null,"direction":"ingress",'
'"ethertype":"IPv4",'
'"portRangeMin":2000,"portRangeMax":2001,"protocol":"tcp"}]}',
'objectId': 700,
'objectName': 'Security Group',
'traceId': '59e7763dc3f1c',
'userId': 400,
'userType': 'CUSTOMER',
'username': 'user'
},
{
'accountId': 100,
'eventCreateDate': '2017-10-18T10:41:42.176328-05:00',
'eventName': 'Network Component Added to Security Group',
'ipAddress': '192.168.0.1',
'label': 'test_SG',
'metaData': '{"requestId":"4709e02ad42c83f80345904",'
'"fullyQualifiedDomainName":"test.softlayer.com",'
'"networkComponentId":"100",'
'"networkInterfaceType":"public"}',
'objectId': 700,
'objectName': 'Security Group',
'traceId': '59e77636261e7',
'userId': 400,
'userType': 'CUSTOMER',
'username': 'user'
}
]

getAllEventObjectNames = ['CCI', 'Security Group']
17 changes: 12 additions & 5 deletions SoftLayer/fixtures/SoftLayer_Network_SecurityGroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@
'createDate': '2017-05-05T12:44:43-06:00'}
editObject = True
deleteObject = True
addRules = True
editRules = True
removeRules = True
attachNetworkComponents = True
detachNetworkComponents = True
addRules = {"requestId": "addRules",
"rules": "[{'direction': 'ingress', "
"'portRangeMax': '', "
"'portRangeMin': '', "
"'ethertype': 'IPv4', "
"'securityGroupId': 100, "
"'remoteGroupId': '', "
"'id': 100}]"}
editRules = {'requestId': 'editRules'}
removeRules = {'requestId': 'removeRules'}
attachNetworkComponents = {'requestId': 'interfaceAdd'}
detachNetworkComponents = {'requestId': 'interfaceRemove'}
2 changes: 2 additions & 0 deletions SoftLayer/managers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from SoftLayer.managers.cdn import CDNManager
from SoftLayer.managers.dedicated_host import DedicatedHostManager
from SoftLayer.managers.dns import DNSManager
from SoftLayer.managers.event_log import EventLogManager
from SoftLayer.managers.file import FileStorageManager
from SoftLayer.managers.firewall import FirewallManager
from SoftLayer.managers.hardware import HardwareManager
Expand All @@ -32,6 +33,7 @@
'CDNManager',
'DedicatedHostManager',
'DNSManager',
'EventLogManager',
'FileStorageManager',
'FirewallManager',
'HardwareManager',
Expand Down
Loading