Skip to content

Commit

Permalink
chg: usr: Improvements to server group creation, use grid-side input …
Browse files Browse the repository at this point in the history
…sanitization for post data
  • Loading branch information
ashmastaflash committed Dec 21, 2016
1 parent bee7855 commit 9759d4d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 144 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -27,6 +27,12 @@ Changes
provided in README.rst and README.md to published docs containing
testing procedure. [Ash Wilson]

Fix
~~~

- Fix: test: Corrected logic for running codeclimate (thanks @mong2)
[Ash Wilson]

v1.0 (2016-11-21)
-----------------

Expand Down
2 changes: 1 addition & 1 deletion cloudpassage/__init__.py
Expand Up @@ -36,5 +36,5 @@
print err_msg

__author__ = "CloudPassage"
__version__ = "1.0.1"
__version__ = "1.0.2"
__license__ = "BSD"
68 changes: 0 additions & 68 deletions cloudpassage/sanity.py
Expand Up @@ -4,74 +4,6 @@
from cloudpassage.exceptions import CloudPassageValidation


def validate_servergroup_create(server_group_attributes):
"""Validate ServerGroup creation arguments"""

val_struct = {
"firewall_policy_id": unicode,
"linux_firewall_policy_id": unicode,
"windows_firewall_policy_id": unicode,
"policy_ids": list,
"windows_policy_ids": list,
"fim_policy_ids": list,
"linux_fim_policy_ids": list,
"windows_fim_policy_ids": list,
"lids_policy_ids": list,
"tag": unicode,
"server_events_policy": unicode,
"alert_profiles": list,
"parent_id": unicode
}

for k, value in server_group_attributes.items():
if k in val_struct:
if isinstance(value, val_struct[k]):
continue
else:
raise TypeError("Type incorrect for %s. Is %s. Should be %s."
% (k, type(value), val_struct[k]))
else:
raise KeyError("Invalid server group attribute: %s") % k
return True


def validate_servergroup_update(server_group_attributes):
"""Validate ServerGroup update arguments"""

val_struct = {
"firewall_policy_id": str,
"linux_firewall_policy_id": str,
"windows_firewall_policy_id": str,
"policy_ids": list,
"windows_policy_ids": list,
"fim_policy_ids": list,
"linux_fim_policy_ids": list,
"windows_fim_policy_ids": list,
"lids_policy_ids": list,
"tag": str,
"name": str,
"special_events_policy": str,
"alert_profiles": list,
"parent_id": str
}

for k, value in server_group_attributes.items():
if k in val_struct:
if isinstance(value, val_struct[k]):
continue
elif (val_struct[k] == str) and (value is None):
continue
elif (val_struct[k] == str) and (isinstance(value, unicode)):
continue
else:
print "Failed to match"
raise TypeError("Type incorrect for %s. Is %s. Should be %s."
% (k, type(value), val_struct[k]))
else:
raise KeyError("Invalid server group attribute: %s") % k
return True


def validate_object_id(object_id):
"""Validates object ID (server_id, policy_id, etc...)
Expand Down
12 changes: 1 addition & 11 deletions cloudpassage/server_group.py
Expand Up @@ -2,7 +2,6 @@

import cloudpassage.utility as utility
import cloudpassage.sanity as sanity
from cloudpassage.exceptions import CloudPassageValidation
from cloudpassage.http_helper import HttpHelper


Expand Down Expand Up @@ -75,8 +74,7 @@ def create(self, group_name, **kwargs):
fim_policy_ids (list): List of Linux FIM policies
linux_fim_policy_ids (list): List of Linux FIM policies
windows_fim_policy_ids (list): List of Windows FIM policies
linux_lids_policy_ids (list): List of Linux LIDS policy IDs
windows_lids_policy_ids (list): List of Windows LIDS policy IDs
lids_policy_ids (list): List of LIDS policy IDs
tag (str): Server group tag-used for auto-assignment of group.
server_events_policy (str): Special events policy IDs
alert_profiles (list): List of alert profile IDs
Expand All @@ -88,10 +86,6 @@ def create(self, group_name, **kwargs):

endpoint = "/v1/groups"
group_data = {"name": group_name, "policy_ids": [], "tag": None}
try:
sanity.validate_servergroup_create(kwargs)
except TypeError as exc:
raise CloudPassageValidation(exc)
body = {"group": utility.merge_dicts(group_data, kwargs)}
request = HttpHelper(self.session)
response = request.post(endpoint, body)
Expand Down Expand Up @@ -146,10 +140,6 @@ def update(self, group_id, **kwargs):
endpoint = "/v1/groups/%s" % group_id
response = None
group_data = {}
try:
sanity.validate_servergroup_update(kwargs)
except TypeError as exc:
raise CloudPassageValidation(exc)
body = {"group": utility.merge_dicts(group_data, kwargs)}
request = HttpHelper(self.session)
response = request.put(endpoint, body)
Expand Down
17 changes: 13 additions & 4 deletions test_wrapper.sh
Expand Up @@ -2,12 +2,20 @@

cd /source

BASE="py.test --cov=cloudpassage tests/style tests/unit"
INTEGRATION="tests/integration"
BASE="py.test --cov=cloudpassage /source/tests/style /source/tests/unit"
INTEGRATION="py.test --cov=cloudpassage tests"
CODECLIMATE="codeclimate-test-reporter --file=/source/.coverage --debug"
SOURCE_CONFIG_TEMPLATE="/source/tests/configs/portal.yaml"
LOCAL_CONFIG_FILE="/source/tests/configs/portal.yaml.local"

# CodeClimate only records coverage for the default branch. So if you have
# convigured the CODECLIMATE_REPO_TOKEN variable, we switch to the `master`
# branch before running tests and submitting results.

if [ ${CODECLIMATE_REPO_TOKEN} ]; then
git checkout master
fi

if [ -z ${HALO_API_HOSTNAME} ]; then
export HALO_API_HOSTNAME=api.cloudpassage.com
fi
Expand All @@ -32,13 +40,14 @@ fi
# set, so we then run integration tests.

echo "Running style, unit, and integration tests"
${BASE} ${INTEGRATION}
${INTEGRATION}
RETCODE=$?
if [ ${RETCODE} != 0 ]; then
exit ${RETCODE}
fi

if [ -z ${CODECLIMATE_REPO_TOKEN} ]; then
# Finally, we use codeclimate-test-reporter to send in metrics.
if [ ${CODECLIMATE_REPO_TOKEN} ]; then
${CODECLIMATE}
fi

Expand Down
60 changes: 0 additions & 60 deletions tests/unit/test_unit_sanity.py
Expand Up @@ -4,66 +4,6 @@


class TestUnitSanity:
def test_servergroup_create_validate(self):
rejected = False
arguments = {"firewall_policy_id": unicode("12345"),
"linux_firewall_policy_id": None,
"windows_firewall_policy_id": None,
"policy_ids": ['12345'],
"windows_policy_ids": ['12345'],
"fim_policy_ids": ['12345'],
"linux_fim_policy_ids": ['12345'],
"windows_fim_policy_ids": ['12345'],
"lids_policy_ids": ['12345'],
"tag": None,
"name": "HelloWorld",
"special_events_policy": None,
"alert_profiles": "FAILURE"}
try:
sanity.validate_servergroup_create(arguments)
except TypeError:
rejected = True
assert rejected

def test_servergroup_update_validate(self):
accepted = True
arguments = {"firewall_policy_id": unicode("12345"),
"linux_firewall_policy_id": None,
"windows_firewall_policy_id": None,
"policy_ids": ['12345'],
"windows_policy_ids": ['12345'],
"fim_policy_ids": ['12345'],
"linux_fim_policy_ids": ['12345'],
"windows_fim_policy_ids": ['12345'],
"lids_policy_ids": ['12345'],
"tag": None,
"name": "HelloWorld",
"special_events_policy": None,
"alert_profiles": ['12345']}
try:
sanity.validate_servergroup_update(arguments)
except:
accepted = False
assert accepted

def test_servergroup_update_validate_bad_type(self):
rejected = False
arguments = {"firewall_policy_id": float(12345.0090011)}
try:
sanity.validate_servergroup_update(arguments)
except:
rejected = True
assert rejected

def test_servergroup_update_validate_bad_attribute(self):
rejected = False
arguments = {"cats": float(12345.0090011)}
try:
sanity.validate_servergroup_update(arguments)
except:
rejected = True
assert rejected

def test_valid_object_id(self):
sample_object_id = "951ffd865e4f11e59ba055477bd3e868"
assert sanity.validate_object_id(sample_object_id)
Expand Down

0 comments on commit 9759d4d

Please sign in to comment.