<jupyter_start><jupyter_text>Table of Contents<jupyter_code>%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')
import api
import imp
import araalictl
# if you are a api dev and make changes
imp.reload(araalictl)
imp.reload(api)
api.g_tenant = None<jupyter_output><empty_output><jupyter_text>Runtime Assessment<jupyter_code>assess = api.Assess()<jupyter_output><empty_output><jupyter_text>Summary<jupyter_code>api.Table(assess.summary())
api.Table(assess.top_consumed_services())<jupyter_output><empty_output><jupyter_text>Inactive Ports<jupyter_code>api.Table(assess.inactive_ports())<jupyter_output><empty_output><jupyter_text>Perimeter Egress - Orgs and Countries<jupyter_code>api.Table(assess.geo_org_accessors())<jupyter_output><empty_output><jupyter_text>Country<jupyter_code>links = _.links
api.Table(api.value_count([a["geo_service_info"][0]["country"] for a in links]))<jupyter_output><empty_output><jupyter_text>Org<jupyter_code>api.Table(api.value_count([a["geo_service_info"][0].get("org", "") for a in links]))<jupyter_output><empty_output><jupyter_text>Important Lenses<jupyter_code>api.Table(assess.starred_lenses())
api.Table(assess.databases())
api.Table(assess.dbaas())
api.Table(assess.important_processes())<jupyter_output><empty_output><jupyter_text>Get all lenses<jupyter_code>api.Table(api.Lens.get(enforced=False, starred=False, all=True),
          #lambda o: o["type"] == "fqdn"
         )
lens = _.links
api.Table(api.value_count([a.type() for a in lens]))
api.Table(lens, lambda o: o["type"] == "app")
api.Table(lens, lambda o: o["type"] == "ip")
api.Table(lens, lambda o: o["type"] == "fqdn")<jupyter_output><empty_output><jupyter_text>Count them<jupyter_code>len(lens)<jupyter_output><empty_output><jupyter_text>Pick one of the links<jupyter_code>api.Table([a for a in lens if a.obj.get("app", "") == "kube-system"])
api.Table([a for a in lens if a.obj.get("fqdn", "") == "github.com"])
api.Table([a for a in lens if a.obj.get("ip", "") == "169.254.169.254"])
mylens = _.links[0]<jupyter_output><empty_output><jupyter_text>Lens to App Policies or Links<jupyter_code>app = mylens.app()
api.Table(app.stats())
api.LinkTable(app.iterlinks())<jupyter_output><empty_output><jupyter_text>App to Template<jupyter_code>app.template()<jupyter_output><empty_output><jupyter_text>Store the template from output above into a variable<jupyter_code>t = _<jupyter_output><empty_output><jupyter_text>Indexed summary of the template<jupyter_code>api.Table(t.index)<jupyter_output><empty_output><jupyter_text>Nodes of the template - so we can do some editing<jupyter_code>api.Table(t.nodes())<jupyter_output><empty_output><jupyter_text>If you want to keep just one process/service and its links<jupyter_code>t.keep_node(0)<jupyter_output><empty_output><jupyter_text>View the locally modified template<jupyter_code>api.Table(t.nodes())<jupyter_output><empty_output><jupyter_text>If you want to delete some nodes<jupyter_code>t.delete_nodes(*range(3,4))
api.Table(t.nodes())<jupyter_output><empty_output><jupyter_text>If you want to update some nodes<jupyter_code>t.update_node(2, {"dns_pattern": ".+", "dst_port": 10250})
t.update_node(0, {"zone": ".+"})<jupyter_output><empty_output><jupyter_text>If you want to rename the node<jupyter_code>t.rename("any-kube-system-metrics-server")<jupyter_output><empty_output><jupyter_text>If you want to publish the template as a public one in Araali Marketplace<jupyter_code>t.public = True<jupyter_output><empty_output><jupyter_text>If you only want to save locally<jupyter_code>t.save()<jupyter_output><empty_output><jupyter_text>If you want to load from a locally saved template<jupyter_code>t = api.Template(fname="template-public-any-k8s-nodes-kubelet.yaml")<jupyter_output><empty_output><jupyter_text>If you want to push to Araali Marketplace<jupyter_code>t.push()
t<jupyter_output><empty_output><jupyter_text>Load up links so you can measure the efficacy of templates<jupyter_code>import copy
api.Runtime.zone2apps = None
run = api.Runtime()
run.refresh()
runlinks = copy.deepcopy(list(run.iterlinks()))<jupyter_output><empty_output><jupyter_text>Summary of alerts<jupyter_code>api.Table(run.link_stats(all=True, only_new=True, runlink=runlinks))<jupyter_output><empty_output><jupyter_text>Load all public teamplates in Araali Marketplace<jupyter_code>matchers = api.Templates(public=True)<jupyter_output><empty_output><jupyter_text>Add a local template to it<jupyter_code>t = api.Template(fname="template-any-kube-system-metrics-server.yaml")
matchers.add(t)<jupyter_output><empty_output><jupyter_text>View the list of templates that you will be matching with<jupyter_code>api.Table(matchers.templates)<jupyter_output><empty_output><jupyter_text>Run all the links through matchers with post filters<jupyter_code>api.LinkTable(matchers.run(runlinks, matched=False), 
          lambda o: o["type"] == "NAE",
          #lambda o: o["client"]["process"] == "chrome"
         )<jupyter_output><empty_output><jupyter_text>Analyze all the links that did not match<jupyter_code>links = _.links
len(links)<jupyter_output><empty_output><jupyter_text>Take a link and make a template out of it<jupyter_code>links[0].template()
t = _
t.rename("zone-app-process")
api.Table(t.index)
api.Table(t.nodes())
t.update_node(1, {"dns_pattern": ".+"})
t.save()
api.Table(api.value_count([a.type for a in links]))
api.Table(links, lambda o: o["client"]["process"] == "metrics-server")
api.Table(api.value_count([a.client.app for a in _.links]))
api.LinkTable([a for a in links if a.server.dns_pattern is None])
api.Table(api.value_count([a.server.subnet for a in links if hasattr(a.server, "subnet")]))
api.Table(api.value_count([a.server.dns_pattern for a in links]))
api.Table(api.value_count([a.client.process for a in links]))
api.Table(api.value_count([a.type for a in links]))<jupyter_output><empty_output><jupyter_text>Score after all the template based work<jupyter_code>api.Table(run.link_stats(all=True, only_new=True, runlink=runlinks))<jupyter_output><empty_output><jupyter_text>Analyze efficacy of individual matchers<jupyter_code>api.LinkTable([a for a in runlinks if hasattr(a, "policy")])
links = _.links
len(links)<jupyter_output><empty_output><jupyter_text>Frequency count<jupyter_code>api.Table(api.value_count([a.policy for a in links]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14413425345043915, 'fraction_numerical': 0.004548306148055207, 'mean_word_length': 9.122222222222222, 'pattern_counts': {'":': 4, '<': 145, '<?xml version=': 0, '>': 145, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '36', 'dir': 'jupyter-structured-clean-dedup', 'id': '340950', 'license': 'MIT', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2383, 'n_words': 377, 'path': 'python/araali_api_lens.ipynb', 'repo_name': 'SidharthAnand/api', 'size': '6376'}