diff --git a/README.md b/README.md index 3c2cd0b73..6a1f1d19a 100644 --- a/README.md +++ b/README.md @@ -52,14 +52,31 @@ You'll need recent versions of the following tools installed: With the above installed, you should be able to navigate into the project root and just run: -``` -$ vagrant up +```bash +vagrant up ``` This will download the VM base box, provision it with dependencies, bootstrap the FireGuard DB, launch the FireGuard Services, and print instructions for connecting to the Web UI. +## Creating Additional Users + +FireGuard creates the first user for you upon installation and prints the +credentials after `vagrant up` completes in the step above. + +You may create additional users with the following command: + +```bash +sudo -u fireguard /opt/fireguard/bin/fireguard rpc 'FgHttp.Users.create_user( + email: "USER_EMAIL", + password: "USER_PASSWORD", + password_confirmation: "USER_PASSWORD" +)' +``` + +This will create a user you can use to log into the Web UI. + ## Contributing See [CONTRIBUTING.md](CONTRIBUTING.md). diff --git a/ansible/playbook.yml b/ansible/playbook.yml index 30c547427..cc2e738db 100644 --- a/ansible/playbook.yml +++ b/ansible/playbook.yml @@ -82,3 +82,11 @@ shell: | cd /home/vagrant/fireguard dpkg -i fireguard_amd64.deb + - name: Create user + become: fireguard + shell: | + cd /home/vagrant/fireguard + scripts/create_user.sh + register: create_user + - name: Display user credentials + debug: msg="{{ create_user.stdout }}" diff --git a/apps/fg_http/lib/fg_http/users.ex b/apps/fg_http/lib/fg_http/users.ex index 0e6a09b7f..d96e9dfeb 100644 --- a/apps/fg_http/lib/fg_http/users.ex +++ b/apps/fg_http/lib/fg_http/users.ex @@ -14,7 +14,13 @@ defmodule FgHttp.Users do def get_user!(id), do: Repo.get!(User, id) - def create_user(attrs \\ %{}) do + def create_user(attrs) when is_list(attrs) do + attrs + |> Enum.into(%{}) + |> create_user() + end + + def create_user(attrs) when is_map(attrs) do %User{} |> User.create_changeset(attrs) |> Repo.insert() diff --git a/apps/fg_http/test/fg_http/users_test.exs b/apps/fg_http/test/fg_http/users_test.exs new file mode 100644 index 000000000..39718d283 --- /dev/null +++ b/apps/fg_http/test/fg_http/users_test.exs @@ -0,0 +1,18 @@ +defmodule FgHttp.UsersTest do + use FgHttp.DataCase + + alias FgHttp.Users + alias FgHttp.Users.User + + describe "users" do + @valid_user [ + email: "admin@fireguard.dev", + password: "test-password", + password_confirmation: "test-password" + ] + + test "create_user" do + assert {:ok, %User{} = _user} = Users.create_user(@valid_user) + end + end +end diff --git a/scripts/build_deb.sh b/scripts/build_deb.sh index b929996f7..b27903d47 100755 --- a/scripts/build_deb.sh +++ b/scripts/build_deb.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -e od=$(pwd) mkdir -p pkg/debian/opt diff --git a/scripts/build_release.sh b/scripts/build_release.sh index c8d1562b9..9ea7f9849 100755 --- a/scripts/build_release.sh +++ b/scripts/build_release.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -e od=$(pwd) mix local.hex --force && mix local.rebar --force diff --git a/scripts/create_user.sh b/scripts/create_user.sh new file mode 100755 index 000000000..02cfa4cf6 --- /dev/null +++ b/scripts/create_user.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -e + +email="$(openssl rand -hex 2)@fireguard.local" +password="$(openssl rand -base64 12)" +/opt/fireguard/bin/fireguard eval "FgHttp.Users.create_user( + email: \"${email}\", + password: \"${password}\", + password_confirmation: \"${password}\" +)" + +echo "FireGuard user created! Save this information becasue it will NOT be shown again." +echo "Email: ${email}" +echo "Password: ${password}" diff --git a/scripts/start_fg_http.sh b/scripts/start_fg_http.sh index 929279e92..3882a24e1 100755 --- a/scripts/start_fg_http.sh +++ b/scripts/start_fg_http.sh @@ -1,4 +1,5 @@ #!/usr/bin/env sh +set -e # migrate DB fg_http/bin/fg_http eval "FgHttp.Release.migrate"