Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Finish infra for sending raw ACME req, add tests for nonce
Fang-Pen Lin2a4b1223480e
diff --git a/backend/bdd/features/pki/acme/account.feature b/backend/bdd/features/pki/acme/account.featureindex 7e1d67a93d..60400cff05 100644--- a/backend/bdd/features/pki/acme/account.feature+++ b/backend/bdd/features/pki/acme/account.feature@@ -4,3 +4,4 @@ Feature: AccountGiven I have an ACME cert profile as "acme_profile"When I have an ACME client connecting to {BASE_URL}/api/v1/pki/acme/profiles/{acme_profile.id}/directoryThen I register a new ACME account with email [email protected] and EAB key id "{acme_profile.eab_kid}" with secret "{acme_profile.eab_secret}" as acme_account+ Then the value acme_account.uri with jq "." should match pattern {BASE_URL}/api/v1/pki/acme/profiles/{acme_profile.id}/accounts/(.+)diff --git a/backend/bdd/features/pki/acme/cert-profile.feature b/backend/bdd/features/pki/acme/cert-profile.featureindex 7ce1ecc8c6..2a325c7c10 100644--- a/backend/bdd/features/pki/acme/cert-profile.feature+++ b/backend/bdd/features/pki/acme/cert-profile.feature@@ -3,7 +3,7 @@ Feature: ACME Cert ProfileScenario: Create a cert profileGiven I make a random slug as profile_slugGiven I use AUTH_TOKEN for authentication- When I send a POST request to "/api/v1/pki/certificate-profiles" with JSON payload+ When I send a "POST" request to "/api/v1/pki/certificate-profiles" with JSON payload"""{"projectId": "{PROJECT_ID}",@@ -25,7 +25,7 @@ Feature: ACME Cert ProfileScenario: Reveal EAB secretGiven I make a random slug as profile_slugGiven I use AUTH_TOKEN for authentication- When I send a POST request to "/api/v1/pki/certificate-profiles" with JSON payload+ When I send a "POST" request to "/api/v1/pki/certificate-profiles" with JSON payload"""{"projectId": "{PROJECT_ID}",@@ -39,7 +39,7 @@ Feature: ACME Cert Profile"""Then the value response.status_code should be equal to 200And I memorize response with jq ".certificateProfile.id" as profile_id- When I send a GET request to "/api/v1/pki/certificate-profiles/{profile_id}/acme/eab-secret/reveal"+ When I send a "GET" request to "/api/v1/pki/certificate-profiles/{profile_id}/acme/eab-secret/reveal"Then the value response.status_code should be equal to 200Then the value response with jq ".eabKid" should be equal to "{profile_id}"Then the value response with jq ".eabSecret" should be presentdiff --git a/backend/bdd/features/pki/acme/challenge.feature b/backend/bdd/features/pki/acme/challenge.featureindex ba9970e430..ece895848a 100644--- a/backend/bdd/features/pki/acme/challenge.feature+++ b/backend/bdd/features/pki/acme/challenge.feature@@ -3,7 +3,6 @@ Feature: ChallengeScenario: Validate challengeGiven I have an ACME cert profile as "acme_profile"When I have an ACME client connecting to {BASE_URL}/api/v1/pki/acme/profiles/{acme_profile.id}/directory-# # TODO: make it I have an account already instead?Then I register a new ACME account with email [email protected] and EAB key id "{acme_profile.eab_kid}" with secret "{acme_profile.eab_secret}" as acme_accountWhen I create certificate signing request as csrThen I add names to certificate signing request csr@@ -20,4 +19,3 @@ Feature: ChallengeThen I tell ACME server that challenge is ready to be verifiedThen I poll and finalize the ACME order order as finalized_orderThen the value finalized_order.body with jq ".status" should be equal to "valid"- # TODO: check the fullchain pem content of the orderdiff --git a/backend/bdd/features/pki/acme/dicrectory.feature b/backend/bdd/features/pki/acme/dicrectory.featureindex 481a3337a8..dbb980ac34 100644--- a/backend/bdd/features/pki/acme/dicrectory.feature+++ b/backend/bdd/features/pki/acme/dicrectory.feature@@ -2,7 +2,7 @@ Feature: DirectoryScenario: Get the directory of ACME service urlsGiven I have an ACME cert profile as "acme_profile"- When I send a GET request to "/api/v1/pki/acme/profiles/{acme_profile.id}/directory"+ When I send a "GET" request to "/api/v1/pki/acme/profiles/{acme_profile.id}/directory"Then the response status code should be "200"Then the response body should match JSON value"""diff --git a/backend/bdd/features/pki/acme/nonce.feature b/backend/bdd/features/pki/acme/nonce.featureindex 7bbeb3b9db..e7325a12ba 100644--- a/backend/bdd/features/pki/acme/nonce.feature+++ b/backend/bdd/features/pki/acme/nonce.feature@@ -2,6 +2,28 @@ Feature: NonceScenario: Generate a new nonceGiven I have an ACME cert profile as "acme_profile"- When I send a HEAD request to "/api/v1/pki/acme/profiles/{acme_profile.id}/new-nonce"+ When I send a "HEAD" request to "/api/v1/pki/acme/profiles/{acme_profile.id}/new-nonce"Then the response status code should be "200"Then the response header "Replay-Nonce" should contains non-empty value++ Scenario: Send bad nonce+ Given I have an ACME cert profile as "acme_profile"+ When I have an ACME client connecting to {BASE_URL}/api/v1/pki/acme/profiles/{acme_profile.id}/directory+ Then I register a new ACME account with email [email protected] and EAB key id "{acme_profile.eab_kid}" with secret "{acme_profile.eab_secret}" as acme_account+ Then I memorize acme_account.uri with jq "capture("/(?<id>[^/]+)$") | .id" as account_id+ When I send a raw ACME request to "/api/v1/pki/acme/profiles/{acme_profile.id}/accounts/{account_id}/orders"+ """+ {+ "protected": {+ "alg": "RS256",+ "nonce": "oFvnlFP1wIhRlYS2jTaXbA",+ "url": "{BASE_URL}/api/v1/pki/acme/profiles/{acme_profile.id}/accounts/{account_id}/orders",+ "kid": "{acme_account.uri}"+ },+ "payload": {}+ }+ """+ Then the value response.status_code should be equal to 400+ Then the value response with jq ".type" should be equal to "urn:ietf:params:acme:error:badNonce"+ Then the value response with jq ".status" should be equal to 400+ Then the value response with jq ".detail" should be equal to "Invalid nonce"diff --git a/backend/bdd/features/steps/pki_acme.py b/backend/bdd/features/steps/pki_acme.pyindex d0fa627cd5..be995a2cfa 100644--- a/backend/bdd/features/steps/pki_acme.py+++ b/backend/bdd/features/steps/pki_acme.py@@ -3,7 +3,9 @@ import loggingimport osimport reimport threading+import urllib.parse+import acme.clientimport httpximport jqimport requests@@ -12,12 +14,14 @@ from faker import Fakerfrom acme import clientfrom acme import messagesfrom acme import standalone+from acme.jws import Signaturefrom behave.runner import Contextfrom behave import givenfrom behave import whenfrom behave import thenfrom josepy.jwk import JWKRSAfrom josepy import JSONObjectWithFields+from josepy import json_utilfrom cryptography.hazmat.primitives import serializationfrom cryptography.hazmat.primitives.asymmetric import rsafrom cryptography import x509@@ -152,7 +156,7 @@ def step_impl(context: Context, token_var: str):context.auth_token = eval_var(context, token_var)-@when('I send a {method} request to "{url}"')+@when('I send a "{method}" request to "{url}"')def step_impl(context: Context, method: str, url: str):logger.debug("Sending %s request to %s", method, url)response = context.http_client.request(@@ -166,7 +170,7 @@ def step_impl(context: Context, method: str, url: str):pass-@when('I send a {method} request to "{url}" with JSON payload')+@when('I send a "{method}" request to "{url}" with JSON payload')def step_impl(context: Context, method: str, url: str):json_payload = json.loads(context.text)json_payload = replace_vars(json_payload, context.vars)@@ -248,6 +252,43 @@ def step_impl(context: Context, email: str, kid: str, secret: str, account_var:context.vars[account_var] = acme_client.new_account(registration)+def send_raw_acme_req(context: Context, url: str):+ acme_client = context.acme_client+ content = json.loads(context.text)+ protected = replace_vars(content["protected"], context.vars)+ payload = (+ replace_vars(content["payload"], context.vars) if "payload" in content else None+ )+ alg = acme_client.net.alg+ encoded_payload = json.dumps(payload).encode() if payload else b""+ protected_headers = json.dumps(protected)+ signature = alg.sign(+ key=acme_client.net.key.key,+ msg=Signature._msg(protected_headers, encoded_payload),+ )+ jws = json.dumps(+ {+ "protected": json_util.encode_b64jose(protected_headers.encode()),+ "payload": json_util.encode_b64jose(encoded_payload),+ "signature": json_util.encode_b64jose(signature),+ }+ )+ base_url = context.vars["BASE_URL"]+ url = urllib.parse.urljoin(base_url, replace_vars(url, context.vars))+ response = acme_client.net._send_request(+ "POST",+ url,+ data=jws,+ headers={"Content-Type": acme.client.ClientNetwork.JOSE_CONTENT_TYPE},+ )+ context.vars["response"] = response+++@when('I send a raw ACME request to "{url}"')+def step_impl(context: Context, url: str):+ send_raw_acme_req(context, url)++@then("I submit the certificate signing request PEM {pem_var} certificate order to the ACME server as {order_var}")