Skip to content

Commit

Permalink
Merge pull request #49 from openziti/normalize-binding-addresses
Browse files Browse the repository at this point in the history
normalize binding addresses
  • Loading branch information
ekoby authored Apr 26, 2023
2 parents 8aaa612 + 8dd1405 commit d4d5ac8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ Or try our decorator pattern with a function annotation
@openziti.zitify(bindings={('127.0.0.1', 18080): {'ztx': '/path/to/identity.json', 'service': 'name-of-ziti-service'}})
def yourFunction():
```

The `binding` dictionary configures what happens when the code tries to open a server socket. Standard network addresses
are mapped to ziti service configurations. For example, with his configuration
```python
bindings = {
('0.0.0.0', 8080): { 'ztx': 'my-identity.json', 'service':'my-service' }
}
```
when application opens a server socket and binds to address `0.0.0.0:8080` it will actually bind to the ziti service named `my-service`.

Binding addresses can be specified with tuples, strings, or ints(ports). `('0.0.0.0', 8080)`, `'0.0.0.0:8080'`, `':8080'`, `8080`
are all considered and treated the same.

## Examples
Try it out yourself with one of our [examples](sample%2FREADME.md)
* [Flazk](sample/flask-of-ziti)
Expand Down
10 changes: 3 additions & 7 deletions sample/flask-of-ziti/helloFlazk.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright NetFoundry Inc.
# Copyright (c) NetFoundry Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -21,21 +21,17 @@


@openziti.zitify(bindings={
('127.0.0.1', 18080): bind_opts
':18080': bind_opts,
})
def runApp():
from waitress import serve
serve(app,host='127.0.0.1',port=18080)
serve(app,port=18080)


@app.route('/')
def hello_world(): # put application's code here
return 'Have some Ziti!'

@app.route('/json')
def get_json():
return '{ "name":"Ziti", "message":"Have some JSON Ziti"}'


if __name__ == '__main__':
bind_opts['ztx'] = sys.argv[1]
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ tag_prefix = v
parentdir_prefix = openziti-

[openziti]
ziti_sdk_version = 0.32.2
ziti_sdk_version = 0.32.3
34 changes: 30 additions & 4 deletions src/openziti/zitisock.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,40 @@
from . import context, zitilib


def process_bindings(orig):
"""normalize binding addresses"""
bindings = {}

if orig is not None:
for k in orig:
host = ''
port = 0
val = orig[k]
if isinstance(k, tuple):
host,port = k
elif isinstance(k, str):
l = k.split(':')
if len(l) == 1:
port = l[0]
else:
host, port = l
elif isinstance(k, int):
port = k

host = '0.0.0.0' if host == '' else host
bindings[(host, int(port))] = val

return bindings



class ZitiSocket(PySocket):
# pylint: disable=redefined-builtin
def __init__(self, af=-1, type=-1, proto=-1, fileno=None, opts=None):
zitilib.init()
if opts is None:
opts = {}
if opts.get('bindings') is None:
opts['bindings'] = {}
self._ziti_bindings = process_bindings(opts.get('bindings'))
self._bind_address = None
self._ziti_opts = opts
self._ziti_af = af
Expand Down Expand Up @@ -65,8 +91,8 @@ def close(self) -> None:

def bind(self, addr) -> None:
self._bind_address = addr
bindings = self._ziti_opts['bindings']
cfg = bindings.get(addr)
h, p = addr
cfg = self._ziti_bindings.get((h, int(p)))
if cfg is not None:
ztx = context.get_context(cfg['ztx'])
service = cfg['service']
Expand Down
39 changes: 39 additions & 0 deletions tests/util_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) NetFoundry Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest


class TestZitiModule(unittest.TestCase):
def test_normalize_addresses(self):
from openziti.zitisock import process_bindings
orig = {
('localhost', 8080): "address1",
('localhost', '8081'): "address2",
('', 8082): "address3",
'localhost:8083': "address4",
':8084': "address5",
8085: "address6",
}

expected = {
('localhost', 8080): "address1",
('localhost', 8081): "address2",
('0.0.0.0', 8082): "address3",
('localhost', 8083): "address4",
('0.0.0.0', 8084): "address5",
('0.0.0.0', 8085): "address6",
}

norm = process_bindings(orig)
self.assertEquals(norm, expected)

0 comments on commit d4d5ac8

Please sign in to comment.