From e0da76f9f62e075463ae04c48b71172464c71b3b Mon Sep 17 00:00:00 2001 From: Connor Rigby Date: Fri, 11 Aug 2017 00:17:50 -0700 Subject: [PATCH] start implemening auth in dev/prod environments --- .gitignore | 3 +++ config/config.exs | 11 +++++++++++ config/hardware/host/dev.exs | 11 +++++------ config/hardware/host/test.exs | 7 +++++-- lib/farmbot/bootstrap/authorization.ex | 5 +++++ lib/farmbot/bootstrap/supervisor.ex | 2 +- .../host/farmbot/host/{system.ex => system_tasks.ex} | 4 +++- 7 files changed, 33 insertions(+), 10 deletions(-) rename nerves/host/farmbot/host/{system.ex => system_tasks.ex} (67%) diff --git a/.gitignore b/.gitignore index d40c0dac..7cfad041 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,6 @@ dump.rdb # this file isnt stored here but just in case. fwup-key.priv .env + +# secret config stuffs for dev environment. +config/auth_secret.exs diff --git a/config/config.exs b/config/config.exs index 82b8a228..05d1548f 100644 --- a/config/config.exs +++ b/config/config.exs @@ -24,6 +24,17 @@ config :tzdata, :autoupdate, :disabled # Path for the `fs` module to watch. config :fs, path: "/tmp/images" +# Configure your our system. +# Default implementation needs no special stuff. +config :farmbot, :init, [ + +] + +# Configure Farmbot Behaviours. +config :farmbot, :behaviour, [ + authorization: Farmbot.Bootstrap.Authorization, +] + # import config specific to our nerves_target IO.puts "using #{target} - #{env} configuration." import_config "hardware/#{target}/#{env}.exs" diff --git a/config/hardware/host/dev.exs b/config/hardware/host/dev.exs index c06e24cf..947e771d 100644 --- a/config/hardware/host/dev.exs +++ b/config/hardware/host/dev.exs @@ -1,12 +1,11 @@ use Mix.Config -config :farmbot, :init, [ - -] +# dev environment doesn't need any special init stuff. +config :farmbot, :init, [] # Configure Farmbot Behaviours. config :farmbot, :behaviour, - # Should implement Farmbot.Bootstrap.Authorization behaviour. - authorization: Farmbot.Host.Authorization, # Should implement Farmbot.System behaviour. - system_tasks: Farmbot.Host.System + system_tasks: Farmbot.Host.SystemTasks + +import_config "../../auth_secret.exs" diff --git a/config/hardware/host/test.exs b/config/hardware/host/test.exs index 9e0317f6..4ea50de4 100644 --- a/config/hardware/host/test.exs +++ b/config/hardware/host/test.exs @@ -7,10 +7,13 @@ config :farmbot, :authorization, [ token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbkBhZG1pbi5jb20iLCJpYXQiOjE1MDI0Mjc2MDUsImp0aSI6IjA3ZDk1MmRjLWQ2MDktNGRiYi04NTcwLTIxMTNkM2Q2ZWMzOSIsImlzcyI6Ii8vMTkyLjE2OC4yOS4yMDQ6MzAwMCIsImV4cCI6MTUwNTg4MzYwNSwibXF0dCI6IjE5Mi4xNjguMjkuMjA0Iiwib3NfdXBkYXRlX3NlcnZlciI6Imh0dHBzOi8vYXBpLmdpdGh1Yi5jb20vcmVwb3MvZmFybWJvdC9mYXJtYm90X29zL3JlbGVhc2VzL2xhdGVzdCIsImZ3X3VwZGF0ZV9zZXJ2ZXIiOiJodHRwczovL2FwaS5naXRodWIuY29tL3JlcG9zL0Zhcm1ib3QvZmFybWJvdC1hcmR1aW5vLWZpcm13YXJlL3JlbGVhc2VzL2xhdGVzdCIsImJvdCI6ImRldmljZV8yIn0.twtAOS9PXtkSB-a3Qjm3HrJSM7Nfm7oWOc8d4BFb7nBOaB3eGDUB4sb5u7pjAgMo37egIbAatKKkHlh_o9uNDJvrs9aPsLAn6O8dN_CDCkenjNyrbQjI8i_hjtL28X9AyHLOe1G0A8V8nRs4hZ8x5AcQSH8DkhaaRaBuh-0u1Yesfo7nwFIl34LTCoZ9amrdzHvIn0xP35BaPoEGziqolqtNJ5an2Bps4JGBV_kNSlODwGxPFESHO3uL1PrTgaXkM3_FSZpY7NxbgxC50ok9TspeMRLjluqntzyJG1EadxgHkbVOG0kuPH6R3Pa6UfkDNzBv7DWDHk4mOYPcDdABbQ" ] -config :farmbot, :init, [ -] +# We reconfigure this later in tests. +config :farmbot, :init, [] +# Replace some things to stub them out. config :farmbot, :behaviour, [ + # Auth needs to be stubbed to not use configuration. authorization: Farmbot.Test.Authorization, + # SystemTasks here, don't actually stop the vm so we can test them. system_tasks: Farmbot.Test.SystemTasks ] diff --git a/lib/farmbot/bootstrap/authorization.ex b/lib/farmbot/bootstrap/authorization.ex index e5f44f54..e22844b8 100644 --- a/lib/farmbot/bootstrap/authorization.ex +++ b/lib/farmbot/bootstrap/authorization.ex @@ -18,4 +18,9 @@ defmodule Farmbot.Bootstrap.Authorization do Should return {:ok, token} | {:error, term} """ @callback authorize(email, password, server) :: {:ok, token} | {:error, term} + + @doc "Authorizes with the farmbot api." + def authorize(email, password, server) do + {:error, :noimpl} + end end diff --git a/lib/farmbot/bootstrap/supervisor.ex b/lib/farmbot/bootstrap/supervisor.ex index b9d9d991..3b2ffe49 100644 --- a/lib/farmbot/bootstrap/supervisor.ex +++ b/lib/farmbot/bootstrap/supervisor.ex @@ -103,7 +103,7 @@ defmodule Farmbot.Bootstrap.Supervisor do ] opts = [strategy: :one_for_all] supervise(children, opts) - {:error, reason} -> {:error, reason} + {:error, reason} -> Farmbot.System.factory_reset(reason) end end end diff --git a/nerves/host/farmbot/host/system.ex b/nerves/host/farmbot/host/system_tasks.ex similarity index 67% rename from nerves/host/farmbot/host/system.ex rename to nerves/host/farmbot/host/system_tasks.ex index e7b94652..164c24c3 100644 --- a/nerves/host/farmbot/host/system.ex +++ b/nerves/host/farmbot/host/system_tasks.ex @@ -1,9 +1,11 @@ -defmodule Farmbot.Host.System do +defmodule Farmbot.Host.SystemTasks do @moduledoc "Host implementation for Farmbot.System." @behaviour Farmbot.System + require Logger def factory_reset(reason) do + Logger.debug "Host factory reset: #{inspect reason}" shutdown(reason) end