Skip to content

Commit 3d99a9b

Browse files
committed
Support creating jitp from API CACertificate create
1 parent 4a66355 commit 3d99a9b

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

lib/nerves_hub/devices.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,13 @@ defmodule NervesHub.Devices do
390390
|> Ecto.build_assoc(:ca_certificates)
391391
|> CACertificate.changeset(params)
392392
|> Repo.insert()
393+
|> case do
394+
{:ok, ca_certificate} ->
395+
{:ok, Repo.preload(ca_certificate, jitp: :product)}
396+
397+
err ->
398+
err
399+
end
393400
end
394401

395402
@spec create_ca_certificate_from_x509(Org.t(), X509.Certificate.t(), binary() | nil) ::

lib/nerves_hub_web/controllers/api/ca_certificate_controller.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ defmodule NervesHubWeb.API.CACertificateController do
3333
not_before: not_before,
3434
not_after: not_after,
3535
der: X509.Certificate.to_der(cert),
36-
description: Map.get(params, "description")
36+
description: Map.get(params, "description"),
37+
jitp: params["jitp"]
3738
},
3839
{:ok, ca_certificate} <- Devices.create_ca_certificate(org, params) do
3940
conn

test/nerves_hub_web/controllers/api/ca_certificate_controller_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ defmodule NervesHubWeb.API.CACertificateControllerTest do
2626
assert %{"description" => ^description} = resp_data
2727
end
2828

29+
test "supports valid JITP", %{conn: conn, org: org, product: %{id: pid, name: pname}} do
30+
ca_key = X509.PrivateKey.new_ec(:secp256r1)
31+
ca_cert = X509.Certificate.self_signed(ca_key, "CN=#{org.name}", template: :root_ca)
32+
serial = X509.Certificate.serial(ca_cert) |> to_string
33+
ca_cert_pem = X509.Certificate.to_pem(ca_cert)
34+
description = "My ca"
35+
36+
jitp = %{description: "Jitter", tags: ["howdy"], product_id: pid}
37+
params = %{cert: Base.encode64(ca_cert_pem), description: description, jitp: jitp}
38+
39+
conn = post(conn, Routes.api_ca_certificate_path(conn, :create, org.name), params)
40+
resp_data = json_response(conn, 201)["data"]
41+
assert %{"serial" => ^serial} = resp_data
42+
assert %{"description" => ^description} = resp_data
43+
44+
assert %{"description" => "Jitter", "tags" => ["howdy"], "product_name" => ^pname} =
45+
resp_data["jitp"]
46+
end
47+
2948
test "renders errors when data is invalid", %{conn: conn, org: org} do
3049
conn = post(conn, Routes.api_ca_certificate_path(conn, :create, org.name), cert: "")
3150

0 commit comments

Comments
 (0)