Skip to content
6 changes: 2 additions & 4 deletions .github/check_entitlements.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/bin/bash


# Derive additional environment variables
TOKEN_URL="${OIDC_OP_TOKEN_ENDPOINT}"
OTDF_HOST_AND_PORT="${OPENTDF_PLATFORM_HOST}"
OTDF_CLIENT="${OPENTDF_CLIENT_ID}"
OTDF_CLIENT_SECRET="${OPENTDF_CLIENT_SECRET}"

# Enable debug mode
DEBUG=1

echo "🔧 Environment Configuration:"
echo " TOKEN_URL: ${TOKEN_URL}"
echo " OTDF_HOST_AND_PORT: ${OTDF_HOST_AND_PORT}"
Expand All @@ -28,6 +24,8 @@ get_token() {

echo "🔐 Getting access token..."
BEARER=$( get_token | jq -r '.access_token' )
# NOTE: It's always okay to print this token, because it will
# only be valid / available in dummy / dev scenarios
[[ "${DEBUG:-}" == "1" ]] && echo "Got Access Token: ${BEARER}"
echo ""

Expand Down
151 changes: 3 additions & 148 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,14 @@

import json
import logging
import os
import subprocess
import tempfile
from pathlib import Path

import pytest

from tests.support_cli_args import get_otdfctl_flags, get_platform_url
from tests.support_otdfctl_args import generate_tdf_files_for_target_mode

logger = logging.getLogger(__name__)
# from tests.config_pydantic import CONFIG_TDF

# Set up environment and configuration
original_env = os.environ.copy()
original_env["GRPC_ENFORCE_ALPN_ENABLED"] = "false"

platform_url = get_platform_url()
otdfctl_flags = get_otdfctl_flags()


def _generate_target_mode_tdf(
input_file: Path,
output_file: Path,
target_mode: str,
creds_file: Path,
attributes: list[str] | None = None,
mime_type: str | None = None,
) -> None:
"""
Generate a TDF file using otdfctl with a specific target mode.

Args:
input_file: Path to the input file to encrypt
output_file: Path where the TDF file should be created
target_mode: Target TDF spec version (e.g., "v4.2.2", "v4.3.1")
creds_file: Path to credentials file
attributes: Optional list of attributes to apply
mime_type: Optional MIME type for the input file
"""
# Ensure output directory exists
output_file.parent.mkdir(parents=True, exist_ok=True)

# Build otdfctl command
cmd = [
"otdfctl",
"encrypt",
"--host",
platform_url,
"--with-client-creds-file",
str(creds_file),
*otdfctl_flags,
"--tdf-type",
"tdf3",
"--target-mode",
target_mode,
"-o",
str(output_file),
]

# Add optional parameters
if attributes:
for attr in attributes:
cmd.extend(["--attr", attr])

if mime_type:
cmd.extend(["--mime-type", mime_type])

# Add input file
cmd.append(str(input_file))

# Run otdfctl command
result = subprocess.run(
cmd,
capture_output=True,
text=True,
env=original_env,
)

if result.returncode != 0:
logger.error(f"otdfctl command failed: {result.stderr}")
raise Exception(
f"Failed to generate TDF with target mode {target_mode}: "
f"stdout={result.stdout}, stderr={result.stderr}"
)


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -118,79 +42,10 @@ def sample_input_files(test_data_dir):
}


def _generate_tdf_files_for_target_mode(
target_mode: str,
temp_credentials_file: Path,
test_data_dir: Path,
sample_input_files: dict[str, Path],
) -> dict[str, Path]:
"""
Factory function to generate TDF files for a specific target mode.

Args:
target_mode: Target TDF spec version (e.g., "v4.2.2", "v4.3.1")
temp_credentials_file: Path to credentials file
test_data_dir: Base test data directory
sample_input_files: Dictionary of sample input files

Returns:
Dictionary mapping file types to their TDF file paths
"""
output_dir = test_data_dir / target_mode
tdf_files = {}

# Define the file generation configurations
file_configs = [
{
"key": "text",
"input_key": "text",
"output_name": "sample_text.txt.tdf",
"mime_type": "text/plain",
},
# {
# "key": "empty",
# "input_key": "empty",
# "output_name": "empty_file.txt.tdf",
# "mime_type": "text/plain",
# },
{
"key": "binary",
"input_key": "binary",
"output_name": "sample_binary.png.tdf",
"mime_type": "image/png",
},
{
"key": "with_attributes",
"input_key": "with_attributes",
"output_name": "sample_with_attributes.txt.tdf",
"mime_type": "text/plain",
},
]

try:
for config in file_configs:
tdf_path = output_dir / config["output_name"]
_generate_target_mode_tdf(
sample_input_files[config["input_key"]],
tdf_path,
target_mode,
temp_credentials_file,
# attributes=[CONFIG_TDF.TEST_OPENTDF_ATTRIBUTE_1] if config["key"] == "with_attributes" else None, # Temporarily disabled due to external KAS dependency
mime_type=config["mime_type"],
)
tdf_files[config["key"]] = tdf_path

return tdf_files

except Exception as e:
logger.error(f"Error generating {target_mode} TDF files: {e}")
raise Exception(f"Failed to generate {target_mode} TDF files: {e}") from e


@pytest.fixture(scope="session")
def tdf_v4_2_2_files(temp_credentials_file, test_data_dir, sample_input_files):
"""Generate TDF files with target mode v4.2.2."""
tdf_files = _generate_tdf_files_for_target_mode(
tdf_files = generate_tdf_files_for_target_mode(
"v4.2.2", temp_credentials_file, test_data_dir, sample_input_files
)
yield tdf_files
Expand All @@ -199,7 +54,7 @@ def tdf_v4_2_2_files(temp_credentials_file, test_data_dir, sample_input_files):
@pytest.fixture(scope="session")
def tdf_v4_3_1_files(temp_credentials_file, test_data_dir, sample_input_files):
"""Generate TDF files with target mode v4.3.1."""
tdf_files = _generate_tdf_files_for_target_mode(
tdf_files = generate_tdf_files_for_target_mode(
"v4.3.1", temp_credentials_file, test_data_dir, sample_input_files
)
yield tdf_files
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import Path

import pytest


Expand Down Expand Up @@ -75,3 +77,49 @@ def test_sample_file_contents(sample_input_files):
with open(attr_file) as f:
content = f.read()
assert "Classification: SECRET" in content


@pytest.mark.integration
def test_target_mode_fixtures_exist(all_target_mode_tdf_files):
"""Test that target mode fixtures generate TDF files correctly."""
# Check that we have both versions
assert "v4.2.2" in all_target_mode_tdf_files
assert "v4.3.1" in all_target_mode_tdf_files

# Check each version has the expected file types
for version in ["v4.2.2", "v4.3.1"]:
tdf_files = all_target_mode_tdf_files[version]

# Check all expected file types exist
expected_types = [
"text",
"binary",
"with_attributes",
] # Consider 'empty' as well
for file_type in expected_types:
assert file_type in tdf_files, f"Missing {file_type} TDF for {version}"

# Check the TDF file exists and is not empty
tdf_path = tdf_files[file_type]
assert isinstance(tdf_path, Path)
assert tdf_path.exists(), f"TDF file does not exist: {tdf_path}"
assert tdf_path.stat().st_size > 0, f"TDF file is empty: {tdf_path}"

# Check it's a valid ZIP file (TDF format)
with open(tdf_path, "rb") as f:
header = f.read(4)
assert header == b"PK\x03\x04", f"TDF file is not a valid ZIP: {tdf_path}"


@pytest.mark.integration
def test_v4_2_2_tdf_files(tdf_v4_2_2_files):
"""Test that v4.2.2 TDF fixtures work independently."""
assert "text" in tdf_v4_2_2_files
assert tdf_v4_2_2_files["text"].exists()


@pytest.mark.integration
def test_v4_3_1_tdf_files(tdf_v4_3_1_files):
"""Test that v4.3.1 TDF fixtures work independently."""
assert "text" in tdf_v4_3_1_files
assert tdf_v4_3_1_files["text"].exists()
Loading
Loading