Skip to content

Commit 4661a95

Browse files
authored
Name Tree Schema (#18)
* schema: A first draft on schema tree * schema: bug fix * schema: Add schematized trust * schema: Register, LocalOnly * schema: SegmentedNode * schema: Fix RDR * schema: Policy cannot change Data Name * schema: Fix raw_packet * schema: Add document * examples: Modify the RDR example for tutorial
1 parent d88171b commit 4661a95

File tree

12 files changed

+1206
-0
lines changed

12 files changed

+1206
-0
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Authors
55
* Zhaoning Kong <https://jonnykong.com>
66
* Eric Newberry <https://ericnewberry.com>
77
* Junxiao Shi <https://yoursunny.com>
8+

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Table Of Contents
1717
src/app
1818
src/encoding/encoding
1919
src/security/security
20+
src/schema/schema
2021
src/misc
2122
src/examples/examples
2223
src/contribute_support

docs/src/schema/schema.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
:mod:`ndn.schema` package
2+
============================
3+
4+
Introduction
5+
------------
6+
7+
The :mod:`ndn.security` package provides an implementation of CNL application framework.
8+
9+
10+
CNL Static Namespace Tree
11+
-------------------------
12+
13+
.. automodule:: ndn.schema.schema_tree
14+
:members:
15+
16+
Utils
17+
-----
18+
19+
.. automodule:: ndn.schema.util
20+
:members:
21+
22+
Custom Nodes
23+
------------
24+
25+
.. automodule:: ndn.schema.simple_node
26+
:members:
27+
28+
Policy Types
29+
------------
30+
31+
.. automodule:: ndn.schema.policy
32+
:members:
33+
34+
Trust Policies
35+
--------------
36+
37+
.. automodule:: ndn.schema.simple_trust
38+
:members:
39+
40+
Cache Policies
41+
--------------
42+
43+
.. automodule:: ndn.schema.simple_cache
44+
:members:

examples/rdrnode.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (C) 2019-2020 Xinyu Ma
3+
#
4+
# This file is part of python-ndn.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
# -----------------------------------------------------------------------------
18+
import sys
19+
import asyncio as aio
20+
import logging
21+
from ndn.app import NDNApp
22+
from ndn.encoding import Name
23+
from ndn.schema import policy
24+
from ndn.schema.schema_tree import Node
25+
from ndn.schema.simple_node import RDRNode
26+
from ndn.schema.simple_cache import MemoryCache, MemoryCachePolicy
27+
from ndn.schema.simple_trust import SignedBy
28+
29+
30+
logging.basicConfig(format='[{asctime}]{levelname}:{message}',
31+
datefmt='%Y-%m-%d %H:%M:%S',
32+
level=logging.INFO,
33+
style='{')
34+
app = NDNApp()
35+
36+
37+
async def main():
38+
if len(sys.argv) <= 1:
39+
print(f'Usage: {sys.argv[0]} <name> [<file-path>]')
40+
exit(0)
41+
42+
# Make static tree
43+
root = Node()
44+
root['/<IDName>/KEY/<KeyID>/self/<CertID>'] = Node()
45+
root['/file/<FileName>'] = RDRNode()
46+
47+
# Set policies
48+
id_name = Name.Component.get_value(app.keychain.default_identity().name[0])
49+
cache = MemoryCache()
50+
root.set_policy(policy.Cache, MemoryCachePolicy(cache))
51+
root['/file/<FileName>'].set_policy(
52+
policy.DataValidator,
53+
SignedBy(root['/<IDName>/KEY/<KeyID>'],
54+
subject_to=lambda _, vars: vars['IDName'] == id_name))
55+
56+
# Store the certificate
57+
cert = app.keychain.default_identity().default_key().default_cert()
58+
await cache.save(Name.normalize(cert.name), cert.data)
59+
60+
# Attach the tree to the face
61+
await root.attach(app, '/')
62+
63+
filename = sys.argv[1]
64+
if len(sys.argv) > 2:
65+
# If it's the producer
66+
filepath = sys.argv[2]
67+
print(f'Read {filename} from file {filepath}...')
68+
# Provider with file
69+
with open(filepath, 'rb') as f:
70+
data = f.read()
71+
await root.match('/file/' + filename).provide(data, freshness_period=60000)
72+
# Wait for it to be cached
73+
await aio.sleep(0.1)
74+
else:
75+
# If it's the producer
76+
print(f'Try to fetch {filename}...')
77+
78+
# The file is ready!
79+
data, metadata = await root.match('/file/' + filename).need()
80+
print(f'Content size: {len(data)}')
81+
print(f'Content: {data[:70]} ...')
82+
print(f'Number of segments: {metadata["block_count"]}')
83+
print(f'Serving {filename}')
84+
85+
if __name__ == '__main__':
86+
app.run_forever(after_start=main())

src/ndn/schema/__init__.py

Whitespace-only changes.

src/ndn/schema/policy.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import abc
2+
from typing import Optional
3+
from ..encoding import SignaturePtrs, FormalName, InterestParam, BinaryStr
4+
from ..encoding.signer import Signer
5+
from ..types import Validator
6+
7+
8+
class Policy:
9+
"""
10+
Policy is an annotation attached to a node.
11+
"""
12+
def __init__(self):
13+
self.node = None
14+
15+
16+
class Cache(Policy, metaclass=abc.ABCMeta):
17+
"""
18+
Cache policy determines how Data packets are stored.
19+
"""
20+
@abc.abstractmethod
21+
async def search(self, match, name: FormalName, param: InterestParam):
22+
pass
23+
24+
@abc.abstractmethod
25+
async def save(self, match, name: FormalName, packet: BinaryStr):
26+
pass
27+
28+
29+
class InterestValidator(Policy, metaclass=abc.ABCMeta):
30+
"""
31+
InterestValidator policy describes how to verify an Interest packet.
32+
"""
33+
@abc.abstractmethod
34+
async def validate(self, match, sig_ptrs: SignaturePtrs) -> bool:
35+
pass
36+
37+
38+
class DataValidator(Policy, metaclass=abc.ABCMeta):
39+
"""
40+
DataValidator policy describes how to verify a Data packet.
41+
"""
42+
@abc.abstractmethod
43+
def get_validator(self, match) -> Validator:
44+
pass
45+
46+
47+
class Signing(Policy, metaclass=abc.ABCMeta):
48+
"""
49+
Signing policy gives a signer used to sign a packet.
50+
When a user uses signing policy, he needs to specify whether its
51+
:class:`InterestSigning` or :class:`DataSigning`.
52+
"""
53+
@abc.abstractmethod
54+
async def get_signer(self, match) -> Signer:
55+
pass
56+
57+
58+
class InterestSigning(Signing, metaclass=abc.ABCMeta):
59+
"""
60+
InterestSigning policy is a type used to indicate the Interest signer.
61+
Used as the type argument of set_policy.
62+
"""
63+
pass
64+
65+
66+
class DataSigning(Signing, metaclass=abc.ABCMeta):
67+
"""
68+
DataSigning policy is a type used to indicate the Data signer.
69+
Used as the type argument of set_policy.
70+
"""
71+
pass
72+
73+
74+
class Encryption(Policy, metaclass=abc.ABCMeta):
75+
"""
76+
Encryption policy encrypts and decrypts content.
77+
When a user uses encryption policy, he needs to specify whether its
78+
:class:`InterestEncryption` or :class:`DataEncryption`.
79+
"""
80+
@abc.abstractmethod
81+
async def decrypt(self, match, content: BinaryStr) -> Optional[BinaryStr]:
82+
pass
83+
84+
@abc.abstractmethod
85+
async def encrypt(self, match, content: BinaryStr) -> Optional[BinaryStr]:
86+
pass
87+
88+
89+
class InterestEncryption(Encryption, metaclass=abc.ABCMeta):
90+
"""
91+
InterestSigning policy is a type used to indicate the Interest encryption policy.
92+
Used as the type argument of set_policy.
93+
"""
94+
pass
95+
96+
97+
class DataEncryption(Encryption, metaclass=abc.ABCMeta):
98+
"""
99+
DataEncryption policy is a type used to indicate the Data encryption policy.
100+
Used as the type argument of set_policy.
101+
"""
102+
pass
103+
104+
105+
class LocalOnly(Policy):
106+
"""
107+
LocalOnly means the Data should be stored in the local storage.
108+
It prevents the node from sending Interest packets.
109+
"""
110+
pass
111+
112+
113+
class Register(Policy):
114+
"""
115+
Register policy indicates the node should be registered as a prefix in the forwarder.
116+
"""
117+
pass

0 commit comments

Comments
 (0)