forked from intel/dffml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import abc | ||
from typing import Union | ||
|
||
from ..df.base import BaseDataFlowObjectContext, BaseDataFlowObject | ||
from ..util.entrypoint import base_entry_point | ||
|
||
|
||
class BaseSecretContext(BaseDataFlowObjectContext): | ||
def __init__(self, parent: "BaseSecret"): | ||
self.parent = parent | ||
|
||
@abc.abstractmethod | ||
async def get(self, name: str) -> Union[bytes, None]: | ||
""" | ||
Get value mapped to name | ||
""" | ||
|
||
@abc.abstractmethod | ||
async def set(self, name: str, value: bytes): | ||
""" | ||
Store value and map it to name | ||
""" | ||
|
||
|
||
@base_entry_point("dffml.secret", "secret") | ||
class BaseSecret(BaseDataFlowObject): | ||
""" | ||
Base Class for secret storage | ||
""" | ||
|
||
def __call__(self) -> BaseSecretContext: | ||
return self.CONTEXT(self) |
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,47 @@ | ||
import warnings | ||
from typing import Union, Any | ||
|
||
from ..base import config | ||
from ..record import Record | ||
from ..util.entrypoint import entrypoint | ||
from .base import BaseSecretContext, BaseSecret | ||
from ..source.memory import MemorySourceContext | ||
from ..source.ini import INISourceConfig, INISource | ||
|
||
|
||
@config | ||
class INISecretConfig: | ||
filename: str | ||
readwrite: bool = True | ||
allowempty: bool = False | ||
|
||
|
||
class INISecretContext(BaseSecretContext, MemorySourceContext): | ||
async def get(self, name: str) -> Union[None, str]: | ||
record = self.parent.mem.get("secrets", None) | ||
if not record: | ||
return None | ||
return record.features().get(name, None) | ||
|
||
async def set(self, name: str, value: Any): | ||
record = Record("secrets", data={"features": {name: value}}) | ||
record.merge(self.parent.mem.get("secrets", Record("secrets"))) | ||
await self.update(record) | ||
|
||
|
||
@entrypoint("ini") | ||
class INISecret(BaseSecret, INISource): | ||
|
||
CONTEXT = INISecretContext | ||
CONFIG = INISecretConfig | ||
|
||
def __init__(self, config: INISourceConfig): | ||
warnings.warn( | ||
""" | ||
INSECURE This just writes secret to an ini file | ||
""" | ||
) | ||
BaseSecret.__init__(self, config) | ||
INISource.__init__(self, config) |
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
Empty file.
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,48 @@ | ||
import io | ||
import os | ||
import pathlib | ||
import inspect | ||
import tempfile | ||
import contextlib | ||
|
||
from dffml.secret.ini import INISecret | ||
from dffml.util.asynctestcase import AsyncTestCase | ||
|
||
|
||
class TestINISecret(AsyncTestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.secret_file = tempfile.NamedTemporaryFile( | ||
prefix="secret_", suffix=".ini" | ||
) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.secret_file.close() | ||
|
||
async def test_set_get(self): | ||
async with INISecret(filename=self.secret_file.name) as secret_store: | ||
async with secret_store() as secret_ctx: | ||
await secret_ctx.set(name="foo", value="bar") | ||
await secret_ctx.set(name="steins", value="gate") | ||
|
||
contents = pathlib.Path(self.secret_file.name).read_text().strip() | ||
|
||
self.assertIn( | ||
"[secrets]", contents, | ||
) | ||
self.assertIn( | ||
"steins = gate", contents, | ||
) | ||
self.assertIn( | ||
"foo = bar", contents, | ||
) | ||
|
||
async with INISecret(filename=self.secret_file.name) as secret_store: | ||
async with secret_store() as secret_ctx: | ||
value = await secret_ctx.get(name="foo") | ||
self.assertEqual(value, "bar") | ||
value = await secret_ctx.get(name="steins") | ||
self.assertEqual(value, "gate") | ||
value = await secret_ctx.get(name="non_existent") | ||
self.assertFalse(value) |