This repository has been archived by the owner on Aug 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Ds10 #21
Open
uover82
wants to merge
5
commits into
deep-security:ds10
Choose a base branch
from
uover82:ds10
base: ds10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ds10 #21
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
664d604
added sample, tenant/ tenant template support, unit tests
uover82 9ff28ef
added draft tenant.update
uover82 f81fe54
add tenant.update, fix tenant.add/timezone
uover82 577eb6b
add tenant.update unit test, clean up sample
uover82 5c7ec94
update tenant.add module_visible support
uover82 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import core | ||
import translation | ||
import json | ||
import urllib | ||
|
||
class Tenants(core.CoreDict): | ||
def __init__(self, manager=None): | ||
core.CoreDict.__init__(self) | ||
self.manager = manager | ||
self.log = self.manager.log if self.manager else None | ||
|
||
def get(self): | ||
""" | ||
Get all of the Tenants from Deep Security | ||
""" | ||
|
||
rest_call = self.manager._get_request_format(api=self.manager.API_TYPE_REST) | ||
|
||
rest_call['call'] = 'tenants' | ||
rest_call['query'] = {} | ||
rest_call['query']['sID'] = self.manager._sessions[self.manager.API_TYPE_REST] | ||
|
||
response = self.manager._request(rest_call, auth_required=False) | ||
|
||
return response | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one of those trivial things, but if |
||
|
||
class Tenant(core.CoreObject): | ||
def __init__(self, manager=None, api_response=None, log_func=None): | ||
core.CoreObject.__init__(self) | ||
self.manager = manager | ||
self.log = self.manager.log if self.manager else None | ||
if api_response: self._set_properties(api_response, log_func) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coding style question for the maintainers, but I think the newline makes this more clear unless using the full ternary. |
||
|
||
def add(self, admin_acct=None, admin_pw=None, admin_eml=None, name=None, modules_visible=['AM']): | ||
""" | ||
Add a Tenant | ||
""" | ||
|
||
rest_call = self.manager._get_request_format(api=self.manager.API_TYPE_REST) | ||
|
||
rest_call['call'] = 'tenants' | ||
rest_call['data'] = { | ||
'createTenantRequest': { | ||
'createOptions': { | ||
'adminAccount': admin_acct, | ||
'adminPassword': admin_pw, | ||
'adminEmail': admin_eml, | ||
'activationCodes': [ 'AM' ] | ||
}, | ||
'tenantElement': { | ||
'tenantID': 11, | ||
'name': name, | ||
'language': 'en', | ||
'country': 'US', | ||
'timeZone': 'US/Pacific', | ||
'modulesVisible': modules_visible | ||
}, | ||
'sessionId': self.manager._sessions[self.manager.API_TYPE_REST] | ||
} | ||
} | ||
|
||
response = self.manager._request(rest_call, auth_required=False) | ||
|
||
return response | ||
|
||
def get(self, tenant_id=None, tenant_name=None, tenant_state=None, max_items=None, tenant_idop=None): | ||
""" | ||
Describe one/ more Tenants | ||
""" | ||
|
||
rest_call = self.manager._get_request_format(api=self.manager.API_TYPE_REST) | ||
|
||
rest_call['query'] = {} | ||
rest_call['query']['sID'] = self.manager._sessions[self.manager.API_TYPE_REST] | ||
|
||
if tenant_state: | ||
rest_call['call'] = 'tenants/state/'+tenant_state | ||
if max_items: | ||
rest_call['query']['maxItems'] = max_items | ||
if tenant_id: | ||
rest_call['query']['tenantID'] = str(tenant_id) | ||
if tenant_idop: | ||
rest_call['query']['tenantIDOp'] = tenant_idop | ||
elif tenant_id: | ||
rest_call['call'] = 'tenants/id/'+str(tenant_id) | ||
elif tenant_name: | ||
rest_call['call'] = 'tenants/name/'+urllib.quote(tenant_name) | ||
else: | ||
rest_call['call'] = 'tenants' | ||
|
||
response = self.manager._request(rest_call, auth_required=False) | ||
|
||
return response | ||
|
||
def update(self, tenant_name=None, modules_visible=None): | ||
""" | ||
Update a Tenant by name | ||
""" | ||
|
||
rest_call = self.manager._get_request_format(api=self.manager.API_TYPE_REST) | ||
|
||
rest_call['query'] = {} | ||
rest_call['query']['sID'] = self.manager._sessions[self.manager.API_TYPE_REST] | ||
rest_call['call'] = 'tenants/name/'+urllib.quote(tenant_name) | ||
|
||
rest_call['data'] = { | ||
'updateTenantRequest': { | ||
'tenantElement': { | ||
'modulesVisible': modules_visible | ||
}, | ||
'sessionId': self.manager._sessions[self.manager.API_TYPE_REST] | ||
} | ||
} | ||
|
||
response = self.manager._request(rest_call, auth_required=False) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import core | ||
import translation | ||
import json | ||
import urllib | ||
|
||
class TenantTemplate(core.CoreObject): | ||
def __init__(self, manager=None, api_response=None, log_func=None): | ||
core.CoreObject.__init__(self) | ||
self.manager = manager | ||
self.log = self.manager.log if self.manager else None | ||
if api_response: self._set_properties(api_response, log_func) | ||
|
||
def add(self, tenant_id=None): | ||
""" | ||
Add a Tenant Template | ||
""" | ||
|
||
rest_call = self.manager._get_request_format(api=self.manager.API_TYPE_REST) | ||
|
||
rest_call['call'] = 'tenanttemplate' | ||
rest_call['data'] = { | ||
'createTenantTemplateRequest': { | ||
'tenantId': tenant_id, | ||
'sessionId': self.manager._sessions[self.manager.API_TYPE_REST] | ||
} | ||
} | ||
|
||
response = self.manager._request(rest_call, auth_required=False) | ||
|
||
return response | ||
|
||
def get(self): | ||
""" | ||
Describe a Tenant Template | ||
""" | ||
|
||
rest_call = self.manager._get_request_format(api=self.manager.API_TYPE_REST) | ||
|
||
rest_call['query'] = {} | ||
rest_call['query']['sID'] = self.manager._sessions[self.manager.API_TYPE_REST] | ||
|
||
rest_call['call'] = 'tenanttemplate' | ||
|
||
response = self.manager._request(rest_call, auth_required=False) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import deepsecurity, json | ||
|
||
# 1. Create a manager object and authenticate. Usage via the API mirrors the | ||
# web administration console for permissions. This defaults to Deep Security | ||
# as a Service | ||
mgr = deepsecurity.dsm.Manager(hostname="dsm.cleardata.com", port="443", username="<username>", password="<password>", tenantname="primary", ignore_ssl_validation=True) | ||
#mgr = deepsecurity.dsm.Manager(username=user, password=pwd, tenantname=tenant_name) | ||
# Create same object against your own Deep Security Manager with a self-signed SSL certificate | ||
#mgr = deepsecurity.dsm.Manager(hostname=hostname, username=user, password=pwd, ignore_ssl_validation=True) | ||
|
||
# 2. With the object created, you have to authenticate | ||
mgr.sign_in() | ||
|
||
# 3. The Manager() object won't have any data populated yet but does have a number of properties | ||
# all work in a similar manner | ||
mgr.tenant.add(admin_acct='testadmin', admin_pw='Ou812345!', admin_eml='[email protected]', name='mytenanttest') | ||
#print json.dumps(mgr.tenants.get(),indent=2,sort_keys=True) | ||
#print json.dumps(mgr.tenant.get(tenant_id=11),indent=2,sort_keys=True) | ||
print json.dumps(mgr.tenant.get(tenant_name='mytenanttest'),indent=2,sort_keys=True) | ||
#print mgr.tenant.get(tenant_state='active', tenant_id=6, tenant_idop='eq') | ||
#mgr.tenanttemplate.add(tenant_id=11) | ||
#print json.dumps(mgr.tenanttemplate.get(),indent=2,sort_keys=True) | ||
#mgr.tenant.update(tenant_name='mytenanttest',modules_visible=['AM','FW']) | ||
''' | ||
mgr.policies.get() | ||
mgr.rules.get() | ||
mgr.ip_lists.get() | ||
mgr.cloud_accounts.get() | ||
mgr.computer_groups.get() | ||
mgr.computers.get() | ||
|
||
# 4. Each of these properties inherits from core.CoreDict which exposes the .get() and other | ||
# useful methods. .get() can be filtered for various properties in order to reduce the | ||
# amount of data you're getting from the Manager(). By default .get() will get all | ||
# of the data it can. | ||
# | ||
# core.CoreDict also exposes a .find() method which is extremely useful for searching | ||
# for specific objects that meet various criteria. .find() takes a set of keyword arguments | ||
# that translate to properties on the objects in the core.CoreDict | ||
# | ||
# For example, this simple loop shows all computers that are currently 'Unmanaged' by | ||
# by Deep Security | ||
for computer_id in mgr.computers.find(overall_status='Unmanaged.*'): | ||
computer = mgr.computers[computer_id] | ||
print "{}\t{}\t{}".format(computer.name, computer.display_name, computer.overall_status) | ||
|
||
# For example, here's all the computers that are running Windows and have the security | ||
# policy "Store UI" or "Shipping" | ||
for computer_id in mgr.computers.find(platform='Windows.*', policy_name=['Store UI', 'Shipping']): | ||
computer = mgr.computers[computer_id] | ||
print "{}\t{}\t{}".format(computer.name, computer.display_name, computer.overall_status) | ||
|
||
# The .find() method takes uses a regex for string comparison and direct comparison for | ||
# other objects. It's extremely flexible and works for all core.CoreDict objects | ||
|
||
# 5. You can also take actions on each of these objects. Where it makes sense, the relevant API | ||
# methods have been added to the object itself. | ||
# | ||
# For example, if you want to scan a set of computers for malware | ||
#mgr.computer[1].scan_for_malware() | ||
|
||
# Apply the same logic for a ComputerGroup | ||
#mgr.computer_group[1].scan_for_malware() | ||
|
||
# Of course, you can use the .find() method on all Computers or ComputerGroups to filter the | ||
# request with a finer granularity | ||
for computer_id in mgr.computers.find(platform='Windows.*', policy_name=['Store UI', 'Shipping']): | ||
computer = mgr.computers[computer_id] | ||
computer.scan_for_malware() | ||
|
||
# This applies to any type of scan or action: | ||
# .scan_for_integrity() | ||
# .scan_for_recommendations() | ||
# .assign_policy() | ||
# ... | ||
|
||
# 6. Adding an AWS account is a good example of a unique property for the | ||
# environments.CloudAccounts object | ||
#mgr.cloud_accounts.add_aws_account(friendly_name, aws_access_key=AWS_ACCESS_KEY, aws_secret_key=AWS_SECRET_KEY) | ||
|
||
# This would add the AWS account and all regions to Deep Security in order to sync | ||
# the inventory of EC2 instances automatically | ||
# | ||
# The IAM identity for the access/secret key needs: | ||
# - ec2::describeInstances | ||
# - ec2::describeImages | ||
# - ec2::describeTags | ||
|
||
# 7. Old school but key. API access is the same as a user logging in. If you are going to | ||
# start a large number of session, you'll need to finish each of them to avoid | ||
# exception being thrown. | ||
# | ||
# This function is also called automatically with the object's destructor | ||
''' | ||
mgr.sign_out() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For starters, this is indented by two spaces which should be corrected to four. Next, the third parameter to
setattr
,lambda: 'PUT'
doesn't really make sense to me. Why not justsetattr(url_request, 'get_method', 'PUT')
?