Skip to content

Commit

Permalink
Merge pull request #18 from foundriesio/only-fix
Browse files Browse the repository at this point in the history
fix: fake-lmp-device-register to test the solution locally
  • Loading branch information
camilamacedo86 authored Nov 7, 2023
2 parents 0e354c0 + b9bcb8a commit 5d9584e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

### LOCAL FILES
data/*
var/*
21 changes: 10 additions & 11 deletions fake-lmp-device-register
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class Options(NamedTuple):
production: bool


def create_key(uuid: str, factory: str, production: bool) -> Tuple[bytes, bytes]:
key = subprocess.check_output(
["openssl", "ecparam", "-genkey", "-name", "prime256v1"]
def create_key(uuid: str, factory: str, production: bool, sota_dir: str) -> bytes:
keyfile_path = os.path.join(sota_dir, "pkey.pem")

subprocess.check_call(
["openssl", "ecparam", "-genkey", "-name", "prime256v1", "-out", keyfile_path]
)

with NamedTemporaryFile() as cnf:
Expand All @@ -52,17 +54,17 @@ extendedKeyUsage=critical, clientAuth
cnf.flush()

r = subprocess.run(
["openssl", "req", "-new", "-config", cnf.name, "-key", "-"],
input=key,
["openssl", "req", "-new", "-config", cnf.name, "-key", keyfile_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
r.check_returncode()

return key, r.stdout
return r.stdout


def main(opts: Options):
pkey, csr = create_key(opts.uuid, opts.factory, opts.production)
csr = create_key(opts.uuid, opts.factory, opts.production, opts.sota_dir)
data = {
"name": opts.name,
"uuid": opts.uuid,
Expand All @@ -85,9 +87,6 @@ def main(opts: Options):
if r.status_code != 201:
sys.exit("ERROR: HTTP_%d: %s" % (r.status_code, r.text))

with open(os.path.join(opts.sota_dir, "pkey.pem"), "wb") as f:
f.write(pkey)

for k, v in r.json().items():
with open(os.path.join(opts.sota_dir, k), mode="w") as f: # type: ignore
f.write(v)
Expand All @@ -107,7 +106,7 @@ def get_parser() -> ArgumentParser:
"--factory", "-f", required=True, help="Name of factory to register device in"
)
p.add_argument("--production", action="store_true", help="Make 'production' cert")
p.add_argument("--sota-dir", "-d", default="/var/sota", help="default=%(default)s")
p.add_argument("--sota-dir", "-d", default=os.path.join(os.getcwd(), "var", "sota"), help="default=%(default)s")
p.add_argument("--tags", "-t", default="master", help="default=%(default)s")
p.add_argument("--apps", "-a")
p.add_argument(
Expand Down

0 comments on commit 5d9584e

Please sign in to comment.