Add auth token

This commit is contained in:
Fang-Pen Lin
2025-11-03 13:10:04 -08:00
parent ebb4260386
commit 79e4231ddf
5 changed files with 61 additions and 5 deletions

View File

@@ -2,6 +2,12 @@ import os
import httpx
from behave.runner import Context
from dotenv import load_dotenv
import logging
logging.getLogger("httpx").setLevel(logging.DEBUG)
load_dotenv()
BASE_URL = os.environ.get("INFISICAL_API_URL", "http://localhost:8080")
PROJECT_ID = os.environ.get("PROJECT_ID", "c051e74c-48a7-4724-832c-d5b496698546")
@@ -18,6 +24,7 @@ def before_all(context: Context):
"PROJECT_ID": PROJECT_ID,
"CERT_CA_ID": CERT_CA_ID,
"CERT_TEMPLATE_ID": CERT_TEMPLATE_ID,
"AUTH_TOKEN": AUTH_TOKEN,
}
context.http_client = httpx.Client(
base_url=BASE_URL, # headers={"Authorization": f"Bearer {AUTH_TOKEN}"}

View File

@@ -2,6 +2,7 @@ Feature: ACME Cert Profile
Scenario: Create a cert profile
Given I make a random slug as profile_slug
Given I use AUTH_TOKEN for authentication
When I send a POST request to "/api/v1/pki/certificate-profiles" with JSON payload
"""
{
@@ -14,7 +15,7 @@ Feature: ACME Cert Profile
"acmeConfig": {}
}
"""
Then the value response.status should be equal to 201
Then the value response.status_code should be equal to 201
Then the value response with jq .eab_kid should be present
Then the value response with jq .eab_secret should be present
Then the value response with jq .slug should be equal to {profile_slug}

View File

@@ -1,11 +1,12 @@
import json
import logging
import re
import threading
import faker
import jq
import requests
import glom
from faker import Faker
from acme import client
from acme import messages
from acme import standalone
@@ -23,6 +24,8 @@ from cryptography.hazmat.primitives import hashes
ACC_KEY_BITS = 2048
ACC_KEY_PUBLIC_EXPONENT = 65537
logger = logging.getLogger(__name__)
faker = Faker()
class AcmeProfile:
@@ -108,6 +111,16 @@ def eval_var(context: Context, var_path: str, as_json: bool = True):
return value
def prepare_headers(context: Context) -> dict | None:
headers = {}
auth_token = getattr(context, "auth_token", None)
if auth_token is not None:
headers["authorization"] = "Bearer {}".format(auth_token)
if not headers:
return None
return headers
@given("I make a random {faker_type} as {var_name}")
def step_impl(context: Context, faker_type: str, var_name: str):
context.vars[var_name] = getattr(faker, faker_type)()
@@ -122,16 +135,28 @@ def step_impl(context: Context, profile_var: str):
context.vars[profile_var] = AcmeProfile(profile_id)
@given("I use {token_var} for authentication")
def step_impl(context: Context, token_var: str):
context.auth_token = eval_var(context, token_var)
@when('I send a {method} request to "{url}"')
def step_impl(context: Context, method: str, url: str):
context.response = context.http_client.request(method, url.format(**context.vars))
context.response = context.http_client.request(
method, url.format(**context.vars), headers=prepare_headers(context)
)
@when('I send a {method} request to "{url}" with JSON payload')
def step_impl(context: Context, method: str, url: str):
context.response = context.http_client.request(
method, url.format(**context.vars), json_payload=context.text
response = context.http_client.request(
method,
url.format(**context.vars),
headers=prepare_headers(context),
json=json.loads(context.text),
)
context.response = response
context.vars["response"] = response
@when("I have an ACME client connecting to {url}")

View File

@@ -7,6 +7,7 @@ requires-python = ">=3.12"
dependencies = [
"acme>=5.1.0",
"behave>=1.3.3",
"dotenv>=0.9.9",
"faker>=37.12.0",
"glom>=24.11.0",
"httpx>=0.28.1",

22
backend/bdd/uv.lock generated
View File

@@ -48,6 +48,7 @@ source = { virtual = "." }
dependencies = [
{ name = "acme" },
{ name = "behave" },
{ name = "dotenv" },
{ name = "faker" },
{ name = "glom" },
{ name = "httpx" },
@@ -59,6 +60,7 @@ dependencies = [
requires-dist = [
{ name = "acme", specifier = ">=5.1.0" },
{ name = "behave", specifier = ">=1.3.3" },
{ name = "dotenv", specifier = ">=0.9.9" },
{ name = "faker", specifier = ">=37.12.0" },
{ name = "glom", specifier = ">=24.11.0" },
{ name = "httpx", specifier = ">=0.28.1" },
@@ -298,6 +300,17 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/20/51/51ae3ab3b8553ec61f6558e9a0a9e8c500a9db844f9cf00a732b19c9a6ea/cucumber_tag_expressions-8.0.0-py3-none-any.whl", hash = "sha256:bfe552226f62a4462ee91c9643582f524af84ac84952643fb09057580cbb110a", size = 9726, upload-time = "2025-10-14T17:01:26.098Z" },
]
[[package]]
name = "dotenv"
version = "0.9.9"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "python-dotenv" },
]
wheels = [
{ url = "https://files.pythonhosted.org/packages/b2/b7/545d2c10c1fc15e48653c91efde329a790f2eecfbbf2bd16003b5db2bab0/dotenv-0.9.9-py2.py3-none-any.whl", hash = "sha256:29cf74a087b31dafdb5a446b6d7e11cbce8ed2741540e2339c69fbef92c94ce9", size = 1892, upload-time = "2025-02-19T22:15:01.647Z" },
]
[[package]]
name = "face"
version = "24.0.0"
@@ -475,6 +488,15 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/34/90/0200184d2124484f918054751ef997ed6409cb05b7e8dcbf5a22da4c4748/pyrfc3339-2.1.0-py3-none-any.whl", hash = "sha256:560f3f972e339f579513fe1396974352fd575ef27caff160a38b312252fcddf3", size = 6758, upload-time = "2025-08-23T16:40:30.49Z" },
]
[[package]]
name = "python-dotenv"
version = "1.2.1"
source = { registry = "https://pypi.org/simple" }
sdist = { url = "https://files.pythonhosted.org/packages/f0/26/19cadc79a718c5edbec86fd4919a6b6d3f681039a2f6d66d14be94e75fb9/python_dotenv-1.2.1.tar.gz", hash = "sha256:42667e897e16ab0d66954af0e60a9caa94f0fd4ecf3aaf6d2d260eec1aa36ad6", size = 44221, upload-time = "2025-10-26T15:12:10.434Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/14/1b/a298b06749107c305e1fe0f814c6c74aea7b2f1e10989cb30f544a1b3253/python_dotenv-1.2.1-py3-none-any.whl", hash = "sha256:b81ee9561e9ca4004139c6cbba3a238c32b03e4894671e181b671e8cb8425d61", size = 21230, upload-time = "2025-10-26T15:12:09.109Z" },
]
[[package]]
name = "requests"
version = "2.32.5"