Administrative operations#
This notebook showcases the usage of admin operations provided by the library. These operations typically involve tasks related to managing the library environment, such as setting up configurations, managing dependencies, and performing administrative tasks.
[ ]:
!pip install "antimatter"
[7]:
import os
from antimatter import new_domain, Session
from antimatter.builders import *
from antimatter.datatype.datatypes import Datatype
Register a domain and create a read/write context#
[34]:
# Either create a new domain or use an existing one
if True:
sess = new_domain("[email protected]")
sess.add_write_context(
"write_ctx", WriteContextBuilder().\
set_summary("Sample write context").\
set_description("Sample description")
)
sess.add_read_context("read_ctx",
ReadContextBuilder().\
set_summary("Sample read context").\
set_description("Sample description")
)
print ("domain: %s" % (sess.domain_id))
# print ("api key: %s" % (sess.api_key))
# print(f"sess = Session(domain='{sess.domain_id}', api_key='{sess.api_key}')")
else:
sess = Session(domain='<domain_id>', api_key='<api_key>')
file_name = "/tmp/testdata.capsule"
domain: dm-2mMUsz12xJ2
List the write context#
[10]:
sess.list_write_context()
[10]:
[{'name': 'write_ctx',
'summary': 'Sample write context',
'description': 'Sample description',
'config': {'required_hooks': []},
'imported': False,
'source_domain_id': None,
'source_domain_name': None}]
Describe the write context#
[11]:
sess.describe_write_context('write_ctx')
[11]:
{'name': 'write_ctx',
'summary': 'Sample write context',
'description': 'Sample description',
'config': {'required_hooks': []},
'imported': False,
'source_domain_id': None,
'source_domain_name': None}
List the read context#
[12]:
sess.list_read_context()
[12]:
[{'name': 'read_ctx',
'summary': 'Sample read context',
'description': 'Sample description',
'read_parameters': [],
'imported': False,
'source_domain_id': None,
'source_domain_name': None}]
Describe the read context#
[13]:
sess.describe_read_context('read_ctx')
[13]:
{'name': 'read_ctx',
'summary': 'Sample read context',
'description': 'Sample description',
'required_hooks': [],
'read_parameters': [],
'rules': [],
'imported': False,
'source_domain_id': None,
'source_domain_name': None}
List the facts#
[14]:
sess.list_fact_types()
[14]:
[]
Add a fact type#
[15]:
sess.add_fact_type(
"is_project_member",
description="Team membership",
arguments={"name": "name of the member", "project": "name of the project"},
)
[16]:
sess.add_fact(
"is_project_member",
"bazProject",
"fooPerson",
)
sess.add_fact(
"is_project_member",
"bazProject",
"fooPerson2",
)
[16]:
{'id': 'ft-j7tw3t7wyy6lptko',
'name': 'is_project_member',
'arguments': ['bazProject', 'fooPerson2']}
[17]:
sess.add_fact(
"is_project_member",
"bazProject2",
"fooPerson3",
)
sess.add_fact(
"is_project_member",
"bazProject2",
"fooPerson4",
)
[17]:
{'id': 'ft-oafp7b36nhbjxvki',
'name': 'is_project_member',
'arguments': ['bazProject2', 'fooPerson4']}
[18]:
sess.get_fact_type('is_project_member')
[18]:
{'name': 'is_project_member',
'description': 'Team membership',
'arguments': [{'name': 'name', 'description': 'name of the member'},
{'name': 'project', 'description': 'name of the project'}],
'imported': False,
'source_domain_id': None,
'source_domain_name': None}
[23]:
facts = sess.list_facts('is_project_member')
facts
[23]:
[{'id': 'ft-4a1k36w07fogi7g6',
'name': 'is_project_member',
'arguments': ['bazProject', 'fooPerson']},
{'id': 'ft-j7tw3t7wyy6lptko',
'name': 'is_project_member',
'arguments': ['bazProject', 'fooPerson2']},
{'id': 'ft-oafp7b36nhbjxvki',
'name': 'is_project_member',
'arguments': ['bazProject2', 'fooPerson4']},
{'id': 'ft-z5bv3yls3ick3v68',
'name': 'is_project_member',
'arguments': ['bazProject2', 'fooPerson3']}]
Get a fact from the id#
[24]:
sess.get_fact('is_project_member', facts[0]['id'])
[24]:
{'id': 'ft-4a1k36w07fogi7g6',
'name': 'is_project_member',
'arguments': ['bazProject', 'fooPerson']}
Add rules to the read context#
Add rules to the read context which redacts all the “tag.antimatter.io/pii/email_address” & the “tag.antimatter.io/pii/credit_card” tags
[25]:
rule_builder = ReadContextRuleBuilder().add_match_expression(
source=Source.Tags,
key="tag.antimatter.io/pii/email_address",
operator=Operator.Exists
).set_action(Action.Redact).set_priority(10)
sess.add_read_context_rules("read_ctx", rule_builder=rule_builder)
[25]:
'rl-7l10kg1ic8bqmlb3'
[26]:
rule_builder = ReadContextRuleBuilder().add_match_expression(
source=Source.Tags,
key="tag.antimatter.io/pii/credit_card",
operator=Operator.Exists
).set_action(Action.Redact).set_priority(20)
sess.add_read_context_rules("read_ctx", rule_builder=rule_builder)
[26]:
'rl-9ybtntt9wk8ohlvh'
Describe the read context which should now show the updated rules#
[29]:
read_context = sess.describe_read_context("read_ctx")
read_context
[29]:
{'name': 'read_ctx',
'summary': 'Sample read context',
'description': 'Sample description',
'required_hooks': [],
'read_parameters': [],
'rules': [{'id': 'rl-7l10kg1ic8bqmlb3',
'match_expressions': [{'source': 'tags',
'key': 'tag.antimatter.io/pii/email_address',
'operator': 'Exists',
'values': None,
'value': None}],
'action': 'Redact',
'token_scope': None,
'token_format': None,
'facts': [],
'priority': 10,
'imported': False,
'source_domain_id': None,
'source_domain_name': None},
{'id': 'rl-9ybtntt9wk8ohlvh',
'match_expressions': [{'source': 'tags',
'key': 'tag.antimatter.io/pii/credit_card',
'operator': 'Exists',
'values': None,
'value': None}],
'action': 'Redact',
'token_scope': None,
'token_format': None,
'facts': [],
'priority': 20,
'imported': False,
'source_domain_id': None,
'source_domain_name': None}],
'imported': False,
'source_domain_id': None,
'source_domain_name': None}
Delete the last rule created#
[30]:
sess.delete_read_context_rule("read_ctx", read_context['rules'][-1]['id'])
Describe the read context which should now show just the first rule#
[32]:
sess.describe_read_context("read_ctx")
[32]:
{'name': 'read_ctx',
'summary': 'Sample read context',
'description': 'Sample description',
'required_hooks': [],
'read_parameters': [],
'rules': [{'id': 'rl-7l10kg1ic8bqmlb3',
'match_expressions': [{'source': 'tags',
'key': 'tag.antimatter.io/pii/email_address',
'operator': 'Exists',
'values': None,
'value': None}],
'action': 'Redact',
'token_scope': None,
'token_format': None,
'facts': [],
'priority': 10,
'imported': False,
'source_domain_id': None,
'source_domain_name': None}],
'imported': False,
'source_domain_id': None,
'source_domain_name': None}