Esempio n. 1
0
 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))
Esempio n. 2
0
 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]),
                 ])
Esempio n. 3
0
 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
Esempio n. 4
0
                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
Esempio n. 5
0
 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
Esempio n. 6
0
 def test_6(self):
     self.assertEqual(plucks(self.simple, "c.II.*.z"), [])
Esempio n. 7
0
 def test_2_slice(self):
     self.assertEqual(plucks(self.simple, "b.:"), range(10))
Esempio n. 8
0
 def test_4(self):
     self.assertEqual(plucks(self.simple, "c.II.1.x"), "x2")
Esempio n. 9
0
 def test_slice_deep_reverse(self):
     self.assertEqual(plucks(self.users, "user.::-1.role"), ["d", "b"])
Esempio n. 10
0
 def test_2(self):
     self.assertEqual(plucks(self.simple, "b.3"), 3)
Esempio n. 11
0
 def test_person_5(self):
     self.assertEqual(plucks(self.users, "user.5.x"), None)
Esempio n. 12
0
 def test_slice_range(self):
     self.assertEqual(plucks(self.simple, "b.2:4"), self.simple['b'][2:4])
Esempio n. 13
0
 def test_5(self):
     self.assertEqual(plucks(self.simple, "c.II.*.x"), ["x1", "x2"])
Esempio n. 14
0
 def test_6(self):
     self.assertEqual(plucks(self.simple, "c.II.*.z"), [])
Esempio n. 15
0
 def test_3(self):
     self.assertEqual(plucks(self.simple, "c.I"), {"a": 1, "b": 2})
Esempio n. 16
0
 def test_4(self):
     self.assertEqual(plucks(self.simple, "c.II.1.x"), "x2")
Esempio n. 17
0
 def test_2_slice(self):
     self.assertEqual(plucks(self.simple, "b.:"), range(10))
Esempio n. 18
0
 def test_2_wildcard(self):
     self.assertEqual(plucks(self.simple, "b.*"), range(10))
Esempio n. 19
0
 def test_2(self):
     self.assertEqual(plucks(self.simple, "b.3"), 3)
Esempio n. 20
0
 def test_person_1_wildcard(self):
     self.assertEqual(plucks(self.users, "user.*.role"), ["b", "d"])
Esempio n. 21
0
 def test_person_1(self):
     self.assertEqual(plucks(self.users, "user.role"), ["b", "d"])
Esempio n. 22
0
 def test_person_2_wildcard(self):
     self.assertEqual(plucks(self.users, "user.*.name"), ["a", "c"])
Esempio n. 23
0
 def test_person_1_wildcard(self):
     self.assertEqual(plucks(self.users, "user.*.role"), ["b", "d"])
Esempio n. 24
0
 def test_slice_simplekey(self):
     self.assertEqual(plucks([1, 2], "0"), 1)
Esempio n. 25
0
 def test_person_2(self):
     self.assertEqual(plucks(self.users, "user.name"), ["a", "c"])
Esempio n. 26
0
 def test_1(self):
     self.assertEqual(plucks(self.simple, "a"), 1)
Esempio n. 27
0
 def test_person_2_wildcard(self):
     self.assertEqual(plucks(self.users, "user.*.name"), ["a", "c"])
Esempio n. 28
0
 def test_1(self):
     self.assertEqual(plucks(self.simple, "a"), 1)
Esempio n. 29
0
 def test_person_3(self):
     self.assertEqual(plucks(self.users, "user.*.username"), [])
Esempio n. 30
0
 def test_2_wildcard(self):
     self.assertEqual(plucks(self.simple, "b.*"), range(10))
Esempio n. 31
0
 def test_person_5(self):
     self.assertEqual(plucks(self.users, "user.5.x"), None)
Esempio n. 32
0
 def test_3(self):
     self.assertEqual(plucks(self.simple, "c.I"), {"a": 1, "b": 2})
Esempio n. 33
0
 def test_person_6(self):
     self.assertEqual(plucks(self.users, "user.*.role.id"), [])
Esempio n. 34
0
 def test_5(self):
     self.assertEqual(plucks(self.simple, "c.II.*.x"), ["x1", "x2"])
Esempio n. 35
0
 def test_slice_simplekey(self):
     self.assertEqual(plucks([1, 2], "0"), 1)
Esempio n. 36
0
 def test_person_1(self):
     self.assertEqual(plucks(self.users, "user.role"), ["b", "d"])
Esempio n. 37
0
 def test_slice_simpleslice(self):
     self.assertEqual(plucks([1, 2], "0:1"), [1])
Esempio n. 38
0
 def test_person_2(self):
     self.assertEqual(plucks(self.users, "user.name"), ["a", "c"])
Esempio n. 39
0
 def test_slice_range(self):
     self.assertEqual(plucks(self.simple, "b.2:4"), self.simple['b'][2:4])
Esempio n. 40
0
 def test_person_3(self):
     self.assertEqual(plucks(self.users, "user.*.username"), [])
Esempio n. 41
0
 def test_slice_reverse(self):
     self.assertEqual(plucks(self.simple, "b.::-1"), self.simple['b'][::-1])
Esempio n. 42
0
 def test_person_6(self):
     self.assertEqual(plucks(self.users, "user.*.role.id"), [])
Esempio n. 43
0
 def test_slice_reverserange(self):
     self.assertEqual(plucks(self.simple, "b.-2:-4:-1"), self.simple['b'][-2:-4:-1])
Esempio n. 44
0
 def test_slice_simpleslice(self):
     self.assertEqual(plucks([1, 2], "0:1"), [1])
Esempio n. 45
0
 def test_slice_lastkey(self):
     self.assertEqual(plucks(self.simple, "b.-1"), self.simple['b'][-1])
Esempio n. 46
0
                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
Esempio n. 47
0
 def test_slice_deep_reverse(self):
     self.assertEqual(plucks(self.users, "user.::-1.role"), ["d", "b"])
Esempio n. 48
0
    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")
Esempio n. 49
0
 def test_slice_lastslice(self):
     self.assertEqual(plucks(self.simple, "b.-1:"), self.simple['b'][-1:])
Esempio n. 50
0
                        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
Esempio n. 51
0
 def test_slice_reverserange(self):
     self.assertEqual(plucks(self.simple, "b.-2:-4:-1"),
                      self.simple['b'][-2:-4:-1])
Esempio n. 52
0
    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))
Esempio n. 53
0
 def test_slice_reverse(self):
     self.assertEqual(plucks(self.simple, "b.::-1"), self.simple['b'][::-1])