-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#23] fail properly for undefined values during generation
- Loading branch information
Showing
8 changed files
with
173 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- coding: utf-8 -*- | ||
# ISC License | ||
# | ||
# Copyright 2019 FL Fintech E GmbH | ||
# | ||
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
# | ||
# Copyright © 2020 Clark Germany GmbH | ||
# Author: Aljosha Friemann <[email protected]> | ||
|
||
import base64 | ||
import hashlib | ||
import os | ||
import string | ||
from typing import Any | ||
|
||
try: | ||
from secrets import choice | ||
except ImportError: | ||
from random import SystemRandom | ||
|
||
choice = SystemRandom().choice | ||
|
||
|
||
def random_password(length: int) -> str: | ||
return "".join( | ||
choice(string.ascii_lowercase + string.digits) for _ in range(length) | ||
) | ||
|
||
|
||
def envvar(key: str, default=None) -> str: | ||
return os.environ.get(key, default) | ||
|
||
|
||
def b64encode(value: Any) -> str: | ||
result = None | ||
|
||
if isinstance(value, str): | ||
result = base64.b64encode(value.encode()).decode() | ||
elif isinstance(value, int): | ||
result = base64.b64encode(str(value).encode()).decode() | ||
elif isinstance(value, bytes): | ||
result = base64.b64encode(value).decode() | ||
else: | ||
raise TypeError("invalid input: {}".format(value)) | ||
|
||
return result | ||
|
||
|
||
def b64decode(value: Any) -> str: | ||
result = None | ||
|
||
if isinstance(value, str): | ||
result = base64.b64decode(value.encode()).decode() | ||
elif isinstance(value, bytes): | ||
result = base64.b64decode(value).decode() | ||
else: | ||
raise TypeError("invalid input: {}".format(value)) | ||
|
||
return result | ||
|
||
def hashf(value, method="sha256"): | ||
try: | ||
hash_method = getattr(hashlib, method)() | ||
except AttributeError: | ||
raise RuntimeError("No such hash method: {}".format(method)) | ||
|
||
if isinstance(value, str): | ||
hash_method.update(value.encode()) | ||
elif isinstance(value, bytes): | ||
hash_method.update(value) | ||
else: | ||
raise TypeError("invalid input: {}".format(value)) | ||
|
||
return hash_method.hexdigest() | ||
|
||
|
||
|
||
# vim: fenc=utf-8:ts=4:sw=4:expandtab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
# ISC License | ||
# | ||
# Copyright 2019 FL Fintech E GmbH | ||
# | ||
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
# | ||
# Copyright © 2020 Clark Germany GmbH | ||
# Author: Aljosha Friemann <[email protected]> | ||
|
||
from k8t.engine import build | ||
|
||
# vim: fenc=utf-8:ts=4:sw=4:expandtab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
# ISC License | ||
# | ||
# Copyright 2019 FL Fintech E GmbH | ||
# | ||
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
# | ||
# Copyright © 2020 Clark Germany GmbH | ||
# Author: Aljosha Friemann <[email protected]> | ||
|
||
import random | ||
|
||
from k8t.filters import b64decode, b64encode, hashf, random_password | ||
|
||
|
||
def test_b64encode(): | ||
string = "foobar" | ||
|
||
encoded = b64encode(string) | ||
|
||
assert encoded != string | ||
assert b64decode(encoded) == string | ||
|
||
|
||
def test_random_password(): | ||
length = int(random.uniform(1, 200)) | ||
|
||
assert len(random_password(length)) == length | ||
assert random_password(length) != random_password(length) | ||
|
||
|
||
def test_hashf(): | ||
string = "foobar" | ||
|
||
assert hashf(string) != string | ||
assert hashf(string) == hashf(string) | ||
assert hashf(string) != hashf("foobaz") | ||
|
||
# vim: fenc=utf-8:ts=4:sw=4:expandtab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# -*- coding: utf-8 -*- | ||
# ISC License | ||
# | ||
# Copyright 2019 FL Fintech E GmbH | ||
# | ||
# Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
|
||
# | ||
# Copyright © 2020 Clark Germany GmbH | ||
# Author: Aljosha Friemann <[email protected]> | ||
|
||
from k8t.templates import analyze | ||
|
||
# vim: fenc=utf-8:ts=4:sw=4:expandtab |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters