def print_stats_for_neuronset(paths): stats = neuronset_apical_stats(paths) print("Apical dendrites stats ({} neurons, {} dendrites):".format( len(paths), plucks(stats, 'meta.n_samples'))) print(textwrap.indent(pformat(stats), ' ' * 4), "\n") stats = neuronset_basal_stats(paths) print("Basal dendrites stats ({} neurons, {} dendrites):".format( len(paths), plucks(stats, 'meta.n_samples'))) print(textwrap.indent(pformat(stats), ' ' * 4))
def compute_score(statuses): return dict(score=sum(plucks(statuses, 'score')), scores=[ len([s.score for s in statuses if s.code == 100]), len([s.score for s in statuses if s.code == 101]), len([s.score for s in statuses if s.code == 200]), ])
def get_namespaces(): """ Collect and return Kubernetes Namespace object information as a nested dictionary with the following information: {<NAMESPACE_NAME>: {<LABELS>;,}} Kubernetes returns a very complicated json object with nested dictionaries, lists, etc. So Plucky is used to simplify data retrieval. """ namespace_info = defaultdict(dict) for entry in functions.section_text_to_json(section): namespace_name = plucks(entry, 'metadata.name') # <LABELS> labels = [] if plucks(entry, 'metadata.labels'): for label in functions.traverse( plucks(entry, 'metadata.labels')): labels.append(f"{label[0]}={label[1]}") else: labels.append('!*=!*') namespace_info[namespace_name]['labels'] = labels return namespace_info
def get_services(): """ Collect and return Kubernetes Service object information as a nested dictionary with the following information: {<SERVICE NAME>: {<NAMESPACE>, <TYPE>, <LABELS>, <IP>, <PORTS>}} Kubernetes returns a very complicated json object with nested dictionaries, lists, etc. So Plucky is used to simplify data retrieval. """ # TODO the service info hasnt been vetted very well. go through and make sure its grabbing everything service_info = defaultdict(dict) for entry in functions.section_text_to_json(section): service_name = plucks(entry, 'metadata.name') # <NAMESPACE> service_info[service_name]['namespace'] = plucks( entry, 'metadata.namespace') # <TYPE> service_info[service_name]['type'] = plucks( entry, 'spec.type') # <LABELS> labels = [] if plucks(entry, 'spec.selector'): for label in functions.traverse( plucks(entry, 'spec.selector')): labels.append(f"{label[0]}={label[1]}") else: labels.append('!*=!*') service_info[service_name]['labels'] = labels # <IP> if plucks(entry, 'status.loadBalancer.ingress.ip'): service_info[service_name]['ip'] = ''.join( plucks(entry, 'status.loadBalancer.ingress.ip')) # <PORTS> if str(plucks(entry, 'spec.ports.port')): service_info[service_name]['ports'] = ''.join( str(plucks(entry, 'spec.ports.port'))) return service_info
def get_pod_info(): """ Collect and return Kubernetes pod information as a list of dictionaries with the following information: {<POD_NAME>:, <NAMESPACE>:, <LABELS>:} Likely to add <IMAGE>:, <RESOURCE_LIMITS>: Kubernetes returns a very complicated json object with nested dictionaries, lists, etc. So Plucky is used to simplify data retrieval. """ pod_info = [] for entry in functions.section_text_to_json(section): d = {} # <POD_NAME> d['name'] = plucks(entry, 'metadata.name') # <NAMESPACE> d['namespace'] = plucks(entry, 'metadata.namespace') # <LABELS> labels = [] if plucks(entry, 'metadata.labels'): for label in functions.traverse( plucks(entry, 'metadata.labels')): labels.append(f"{label[0]}={label[1]}") else: labels.append('!') d['labels'] = labels # <IMAGE> d['image'] = plucks(entry, 'spec.containers.image') # <RESOURCE_LIMITS> limits = [] if plucks(entry, 'spec.containers.resources.limits'): for limit in plucks( entry, 'spec.containers.resources.limits'): for k, v in limit.items(): limits.append(f'{k}={v}') else: limits = '*:*' d['limits'] = limits pod_info.append(d) return pod_info
def test_6(self): self.assertEqual(plucks(self.simple, "c.II.*.z"), [])
def test_2_slice(self): self.assertEqual(plucks(self.simple, "b.:"), range(10))
def test_4(self): self.assertEqual(plucks(self.simple, "c.II.1.x"), "x2")
def test_slice_deep_reverse(self): self.assertEqual(plucks(self.users, "user.::-1.role"), ["d", "b"])
def test_2(self): self.assertEqual(plucks(self.simple, "b.3"), 3)
def test_person_5(self): self.assertEqual(plucks(self.users, "user.5.x"), None)
def test_slice_range(self): self.assertEqual(plucks(self.simple, "b.2:4"), self.simple['b'][2:4])
def test_5(self): self.assertEqual(plucks(self.simple, "c.II.*.x"), ["x1", "x2"])
def test_6(self): self.assertEqual(plucks(self.simple, "c.II.*.z"), [])
def test_3(self): self.assertEqual(plucks(self.simple, "c.I"), {"a": 1, "b": 2})
def test_4(self): self.assertEqual(plucks(self.simple, "c.II.1.x"), "x2")
def test_2_slice(self): self.assertEqual(plucks(self.simple, "b.:"), range(10))
def test_2_wildcard(self): self.assertEqual(plucks(self.simple, "b.*"), range(10))
def test_2(self): self.assertEqual(plucks(self.simple, "b.3"), 3)
def test_person_1_wildcard(self): self.assertEqual(plucks(self.users, "user.*.role"), ["b", "d"])
def test_person_1(self): self.assertEqual(plucks(self.users, "user.role"), ["b", "d"])
def test_person_2_wildcard(self): self.assertEqual(plucks(self.users, "user.*.name"), ["a", "c"])
def test_person_1_wildcard(self): self.assertEqual(plucks(self.users, "user.*.role"), ["b", "d"])
def test_slice_simplekey(self): self.assertEqual(plucks([1, 2], "0"), 1)
def test_person_2(self): self.assertEqual(plucks(self.users, "user.name"), ["a", "c"])
def test_1(self): self.assertEqual(plucks(self.simple, "a"), 1)
def test_person_2_wildcard(self): self.assertEqual(plucks(self.users, "user.*.name"), ["a", "c"])
def test_1(self): self.assertEqual(plucks(self.simple, "a"), 1)
def test_person_3(self): self.assertEqual(plucks(self.users, "user.*.username"), [])
def test_2_wildcard(self): self.assertEqual(plucks(self.simple, "b.*"), range(10))
def test_person_5(self): self.assertEqual(plucks(self.users, "user.5.x"), None)
def test_3(self): self.assertEqual(plucks(self.simple, "c.I"), {"a": 1, "b": 2})
def test_person_6(self): self.assertEqual(plucks(self.users, "user.*.role.id"), [])
def test_5(self): self.assertEqual(plucks(self.simple, "c.II.*.x"), ["x1", "x2"])
def test_slice_simplekey(self): self.assertEqual(plucks([1, 2], "0"), 1)
def test_person_1(self): self.assertEqual(plucks(self.users, "user.role"), ["b", "d"])
def test_slice_simpleslice(self): self.assertEqual(plucks([1, 2], "0:1"), [1])
def test_person_2(self): self.assertEqual(plucks(self.users, "user.name"), ["a", "c"])
def test_slice_range(self): self.assertEqual(plucks(self.simple, "b.2:4"), self.simple['b'][2:4])
def test_person_3(self): self.assertEqual(plucks(self.users, "user.*.username"), [])
def test_slice_reverse(self): self.assertEqual(plucks(self.simple, "b.::-1"), self.simple['b'][::-1])
def test_person_6(self): self.assertEqual(plucks(self.users, "user.*.role.id"), [])
def test_slice_reverserange(self): self.assertEqual(plucks(self.simple, "b.-2:-4:-1"), self.simple['b'][-2:-4:-1])
def test_slice_simpleslice(self): self.assertEqual(plucks([1, 2], "0:1"), [1])
def test_slice_lastkey(self): self.assertEqual(plucks(self.simple, "b.-1"), self.simple['b'][-1])
def getFirewallInfo(): """ Collect and return Kubernetes Network Policy object information as a list of dictionaries. Inputs: kubectl get networkpolicy -A -o json Returns: {<NAME>:, <NAMESPACE>:, <ACTION>:, <SOURCE>:, <DEST>:, <PROTOCOL>:, <PORT>} """ firewall_rules = [ ] # list to hold dictionaries of test results for entry in functions.section_text_to_json(section): # TODO figure out how to level this so I can use a common function in each if/else block # If spec.ingress exists then the pod selector is the destination and the spec.ingress is the source def build_acl_dict(entry, action, direction): results = {} # <ACL_NAME> results.update( {'name': plucks(entry, 'metadata.name')}) # <NAMESPACE> results.update({ 'namespace': plucks(entry, 'metadata.namespace') }) # <ACTION> results.update({'action': action}) # <PORTS> ports = [] protocols = [] if plucks(entry, f"spec.{direction}.ports"): for i in functions.traverse( plucks(entry, f"spec.{direction}.ports")): if 'protocol' in i: protocols.append(f"{i[1]}") if 'port' in i: ports.append(f"{i[1]}") if ports: results.update({'ports': ports}) results.update({'protocols': protocols}) if not ports: results.update({'ports': '*'}) results.update({'protocols': '*'}) # <SOURCE> if direction == 'ingress': results.update({ 'source': k8s_selectors( plucks(entry, 'spec.ingress.from')) }) # <DESTINATION> if plucks( entry, 'spec.podSelector' ): # an empty pod selector allows all in the NS results.update({ 'destination': k8s_selectors( plucks(entry, 'spec.podSelector')) }) else: results.update({'destination': '*'}) if direction == 'egress': # <SOURCE> if plucks( entry, 'spec.podSelector' ): # an empty pod selector allows all in the NS results.update({ 'source': k8s_selectors( plucks(entry, 'spec.podSelector')) }) else: results.update({'source': '*'}) # <DESTINATION> results.update({ 'destination': k8s_selectors( plucks(entry, 'spec.egress.to')) }) return results if 'Ingress' in plucks(entry, 'spec.policyTypes'): if plucks(entry, 'spec.ingress'): # allow firewall_rules.append( build_acl_dict(entry, 'allow', 'ingress')) else: # If spec.ingress is missing then no traffic is allowed to the podSelector target firewall_rules.append( build_acl_dict(entry, 'deny', 'ingress')) # If spec.egress exists then the pod selector is the source and the spec.egress is the destination if 'Egress' in plucks(entry, 'spec.policyTypes'): if plucks(entry, 'spec.egress'): # allow firewall_rules.append( build_acl_dict(entry, 'allow', 'egress')) else: # empty or missing egress target blocks all egress traffic firewall_rules.append( build_acl_dict(entry, 'deny', 'egress')) return firewall_rules
def test_slice_deep_reverse(self): self.assertEqual(plucks(self.users, "user.::-1.role"), ["d", "b"])
def __init__(self): with open("Config.json") as tsk: self.t = json.load(tsk) self.forever = True self.url = "https://mosaic-platform.mesh.mx/stores/size/content?api_key=0ce5f6f477676d95569067180bc4d46d&channel=iphone-mosaic" self.webhook = self.t["Webhook"] self.testMode = self.t['testMode'] self.session = Session() self.pidArray = [] self.scrapeArray = [] self.removepids = [] self.sleep = self.t["Delay"] self.headers = { "user-agent": "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25" } proxy = ProxyManager().get_next_proxy(self.t['randomProxy']) if proxy == "": log("No proxies added to list, using local IP \n", color='blue') else: log('[{}] adding proxy to task \n'.format(proxy), color='blue') p = { 'http': 'http://{}'.format(proxy), 'https': 'https://{}'.format(proxy) } self.session.proxies = p try: self.page = self.session.get( url=self.url, headers=self.headers).json()["products"] except Exception: log( "Cannot get information off app, please check your internet and try again", "error") exit() print("Current shoes on app:") print("") for products in range(len(self.page)): self.pidArray.append(self.page[products]["ID"]) print("- {}".format(self.page[products]["name"])) if self.testMode == True and self.webhook != "": slackMessage = { "attachments": [{ "author_name": "New Item", "fallback": "", "title": "{}".format(self.page[products]["name"]), "title_link": "https://size-mosaic-webapp.mesh.mx/#/product/{}". format(self.page[products]["ID"]), "fields": [{ "title": "PID:", "value": "{}".format(self.page[products]['ID']), "short": True }, { "title": "Sizes:", "value": "{}".format( plucky.plucks(self.page[products]["options"], 'name')), "short": True }, { "title": "Release Date", "value": "{}, {} GMT".format( self.page[products]['launchDate'].split("T") [0], re.search('T(.*):', self.page[products] ["launchDate"]).group(1)), "short": False }], "image_url": "{}".format( self.page[products]["mainImage"]["original"]), "color": "#00FF00" }] } self.session.post(url=self.webhook, data=json.dumps(slackMessage)) print("") print("Number of shoes on app: {}".format(len(self.pidArray))) print("") if (self.testMode and self.webhook == ""): log("Cannot run test mode", "info", nocolor="Please add a webhook") log("Monitor Started Succesfully", "success")
def test_slice_lastslice(self): self.assertEqual(plucks(self.simple, "b.-1:"), self.simple['b'][-1:])
def build_acl_dict(entry, action, direction): results = {} # <ACL_NAME> results.update( {'name': plucks(entry, 'metadata.name')}) # <NAMESPACE> results.update({ 'namespace': plucks(entry, 'metadata.namespace') }) # <ACTION> results.update({'action': action}) # <PORTS> ports = [] protocols = [] if plucks(entry, f"spec.{direction}.ports"): for i in functions.traverse( plucks(entry, f"spec.{direction}.ports")): if 'protocol' in i: protocols.append(f"{i[1]}") if 'port' in i: ports.append(f"{i[1]}") if ports: results.update({'ports': ports}) results.update({'protocols': protocols}) if not ports: results.update({'ports': '*'}) results.update({'protocols': '*'}) # <SOURCE> if direction == 'ingress': results.update({ 'source': k8s_selectors( plucks(entry, 'spec.ingress.from')) }) # <DESTINATION> if plucks( entry, 'spec.podSelector' ): # an empty pod selector allows all in the NS results.update({ 'destination': k8s_selectors( plucks(entry, 'spec.podSelector')) }) else: results.update({'destination': '*'}) if direction == 'egress': # <SOURCE> if plucks( entry, 'spec.podSelector' ): # an empty pod selector allows all in the NS results.update({ 'source': k8s_selectors( plucks(entry, 'spec.podSelector')) }) else: results.update({'source': '*'}) # <DESTINATION> results.update({ 'destination': k8s_selectors( plucks(entry, 'spec.egress.to')) }) return results
def test_slice_reverserange(self): self.assertEqual(plucks(self.simple, "b.-2:-4:-1"), self.simple['b'][-2:-4:-1])
def scrape(self): try: while self.forever: scrape = self.session.get(url=self.url).json()["products"] for products in range(len(scrape)): self.scrapeArray.append(scrape[products]["ID"]) if self.scrapeArray[products] not in self.pidArray: slackMessage = { "attachments": [{ "author_name": "New Item", "fallback": "", "title": "{}".format(scrape[products]["name"]), "title_link": "https://size-mosaic-webapp.mesh.mx/#/product/{}" .format(scrape[products]["ID"]), "fields": [{ "title": "PID:", "value": "{}".format(scrape[products]['ID']), "short": True }, { "title": "Sizes:", "value": "{}".format( plucky.plucks( scrape[products]["options"], 'name')), "short": True }, { "title": "Release Date", "value": "{}, {} GMT".format( scrape[products]['launchDate'].split( "T")[0], re.search( 'T(.*):', scrape[products] ["launchDate"]).group(1)), "short": False }], "image_url": "{}".format( scrape[products]["mainImage"]["original"]), "color": "#00FF00" }] } self.session.post(url=self.webhook, data=json.dumps(slackMessage)) log( "PID: {},Name: {} added to app".format( scrape[products]["ID"], scrape[products]["name"]), "success", "addedPIDs.txt") self.pidArray.append(scrape[products]["ID"]) for pids in range(len(self.pidArray)): if self.pidArray[pids] not in self.scrapeArray: self.removepids.append(pids) count = 0 for rPids in range(len(self.removepids)): log( "PID: {} removed from app".format( self.pidArray[self.removepids[rPids - count]]), "error") self.pidArray.pop(int(self.removepids[rPids]) - count) count = count + 1 self.scrapeArray = [] self.removepids = [] sleep(int(self.sleep)) except Exception: log("Something went wrong. Restarting Monitor", "error") self.scrape() sleep(int(self.sleep))
def test_slice_reverse(self): self.assertEqual(plucks(self.simple, "b.::-1"), self.simple['b'][::-1])