Skip to content

Commit

Permalink
Merge pull request #70 from SEKOIA-IO/feat/env_json_base64
Browse files Browse the repository at this point in the history
feat: Add support for base64 encoded env variables
  • Loading branch information
Darkheir authored Aug 21, 2023
2 parents c2a407e + 0003be1 commit 018229c
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 297 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
repos:
- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
rev: 'v0.0.274'
rev: 'v0.0.285'
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.3.9] - 2023-08-21

### Added

- Add support for base64 encoded env variables

## [1.3.8] - 2023-07-13

### Fixed
Expand Down
591 changes: 299 additions & 292 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "sekoia-automation-sdk"

version = "1.3.8"
version = "1.3.9"
description = "SDK to create Sekoia.io playbook modules"
license = "MIT"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions sekoia_automation/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
help="Sekoia.io's automation helper to generate playbook modules",
rich_markup_mode="markdown",
)
OptionalPath = Optional[Path] # noqa: UP007
OptionalStr = Optional[str] # noqa: UP007
OptionalPath = Optional[Path]
OptionalStr = Optional[str]


@app.command(name="generate-files-from-code")
Expand Down
10 changes: 9 additions & 1 deletion sekoia_automation/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import base64
import json
import os
from pathlib import Path

VOLUME_PATH = "/symphony"


def _json_load(value: str):
try:
return json.loads(value)
except ValueError:
return json.loads(base64.b64decode(value))


def load_config(name: str, type_: str = "str", non_exist_ok=False):
path = Path(f"{VOLUME_PATH}/{name}")
if path.is_file():
Expand All @@ -14,7 +22,7 @@ def load_config(name: str, type_: str = "str", non_exist_ok=False):
return fd.read()

if value := os.environ.get(name.upper()):
return json.loads(value) if type_ == "json" else value
return _json_load(value) if type_ == "json" else value
if non_exist_ok:
return None
raise FileNotFoundError(f"{path} does not exists.")
6 changes: 6 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
from pathlib import Path

import pytest
Expand Down Expand Up @@ -32,3 +33,8 @@ def test_load_config_env(monkeypatch):
def test_load_config_env_json(monkeypatch):
monkeypatch.setenv("FOO", '{"foo": "bar"}')
assert load_config("foo", type_="json") == {"foo": "bar"}


def test_load_config_env_json_encoded(monkeypatch):
monkeypatch.setenv("FOO", base64.b64encode(b'{"foo": "bar"}').decode())
assert load_config("foo", type_="json") == {"foo": "bar"}

0 comments on commit 018229c

Please sign in to comment.