Skip to content

Commit 93fb6af

Browse files
Add Sanbox guardrails for mutations (#167)
1 parent cf565ae commit 93fb6af

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

lib/console.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
defmodule Console do
22
def conf(key, default \\ nil), do: Application.get_env(:console, key, default)
33

4+
def sandbox?(), do: conf(:is_sandbox, false)
5+
6+
def demo_project?(), do: conf(:is_demo_project, false)
7+
48
def rand_str(size \\ 32) do
59
:crypto.strong_rand_bytes(size)
610
|> Base.url_encode64()

lib/console/configuration.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
defmodule Console.Configuration do
2-
defstruct [:git_commit, :is_demo_project]
2+
defstruct [:git_commit, :is_demo_project, :is_sandbox]
33

44
def new() do
55
%__MODULE__{
66
git_commit: Console.conf(:git_commit),
77
is_demo_project: Console.conf(:is_demo_project),
8+
is_sandbox: Console.sandbox?()
89
}
910
end
1011
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule Console.Middleware.Sandboxed do
2+
@behaviour Absinthe.Middleware
3+
alias Console.Schema.User
4+
5+
def call(resolution, _) do
6+
case Console.sandbox?() do
7+
true -> Absinthe.Resolution.put_result(resolution, {:error, "cannot perform this action in a sandbox environment"})
8+
_ -> resolution
9+
end
10+
end
11+
end

lib/console/graphql/users.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Console.GraphQl.Users do
22
use Console.GraphQl.Schema.Base
33
alias Console.GraphQl.Resolvers.User
4-
alias Console.Middleware.{Authenticated, AdminRequired, AllowJwt}
4+
alias Console.Middleware.{Authenticated, AdminRequired, AllowJwt, Sandboxed}
55
alias Console.Schema.Notification.{Severity, Status}
66

77
enum_from_list :permission, Console.Schema.Role, :permissions, []
@@ -216,6 +216,7 @@ defmodule Console.GraphQl.Users do
216216

217217
field :signup, :user do
218218
middleware AllowJwt
219+
middleware Sandboxed
219220
arg :invite_id, non_null(:string)
220221
arg :attributes, non_null(:user_attributes)
221222

@@ -232,13 +233,15 @@ defmodule Console.GraphQl.Users do
232233

233234
field :create_invite, :invite do
234235
middleware Authenticated
236+
middleware Sandboxed
235237
arg :attributes, non_null(:invite_attributes)
236238

237239
resolve safe_resolver(&User.create_invite/2)
238240
end
239241

240242
field :update_user, :user do
241243
middleware Authenticated
244+
middleware Sandboxed
242245
arg :id, :id
243246
arg :attributes, non_null(:user_attributes)
244247

@@ -248,6 +251,7 @@ defmodule Console.GraphQl.Users do
248251
field :create_group, :group do
249252
middleware Authenticated
250253
middleware AdminRequired
254+
middleware Sandboxed
251255
arg :attributes, non_null(:group_attributes)
252256

253257
resolve safe_resolver(&User.create_group/2)
@@ -256,6 +260,7 @@ defmodule Console.GraphQl.Users do
256260
field :delete_group, :group do
257261
middleware Authenticated
258262
middleware AdminRequired
263+
middleware Sandboxed
259264
arg :group_id, non_null(:id)
260265

261266
resolve safe_resolver(&User.delete_group/2)
@@ -264,6 +269,7 @@ defmodule Console.GraphQl.Users do
264269
field :update_group, :group do
265270
middleware Authenticated
266271
middleware AdminRequired
272+
middleware Sandboxed
267273
arg :group_id, non_null(:id)
268274
arg :attributes, non_null(:group_attributes)
269275

@@ -273,6 +279,7 @@ defmodule Console.GraphQl.Users do
273279
field :create_group_member, :group_member do
274280
middleware Authenticated
275281
middleware AdminRequired
282+
middleware Sandboxed
276283
arg :group_id, non_null(:id)
277284
arg :user_id, non_null(:id)
278285

@@ -282,6 +289,7 @@ defmodule Console.GraphQl.Users do
282289
field :delete_group_member, :group_member do
283290
middleware Authenticated
284291
middleware AdminRequired
292+
middleware Sandboxed
285293
arg :group_id, non_null(:id)
286294
arg :user_id, non_null(:id)
287295

@@ -291,6 +299,7 @@ defmodule Console.GraphQl.Users do
291299
field :create_role, :role do
292300
middleware Authenticated
293301
middleware AdminRequired
302+
middleware Sandboxed
294303
arg :attributes, non_null(:role_attributes)
295304

296305
resolve safe_resolver(&User.create_role/2)
@@ -299,6 +308,7 @@ defmodule Console.GraphQl.Users do
299308
field :update_role, :role do
300309
middleware Authenticated
301310
middleware AdminRequired
311+
middleware Sandboxed
302312
arg :id, non_null(:id)
303313
arg :attributes, non_null(:role_attributes)
304314

@@ -308,6 +318,7 @@ defmodule Console.GraphQl.Users do
308318
field :delete_role, :role do
309319
middleware Authenticated
310320
middleware AdminRequired
321+
middleware Sandboxed
311322
arg :id, non_null(:id)
312323

313324
resolve safe_resolver(&User.delete_role/2)

lib/console/graphql/webhooks.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Console.GraphQl.Webhooks do
22
use Console.GraphQl.Schema.Base
33
alias Console.GraphQl.Resolvers.Webhook
4-
alias Console.Middleware.{Authenticated}
4+
alias Console.Middleware.{Authenticated, Sandboxed}
55
alias Console.Schema
66

77
ecto_enum :webhook_type, Schema.Webhook.Type
@@ -33,13 +33,15 @@ defmodule Console.GraphQl.Webhooks do
3333
object :webhook_mutations do
3434
field :create_webhook, :webhook do
3535
middleware Authenticated
36+
middleware Sandboxed
3637
arg :attributes, non_null(:webhook_attributes)
3738

3839
resolve safe_resolver(&Webhook.create_webhook/2)
3940
end
4041

4142
field :delete_webhook, :webhook do
4243
middleware Authenticated
44+
middleware Sandboxed
4345
arg :id, non_null(:id)
4446

4547
resolve safe_resolver(&Webhook.delete_webhook/2)

rel/config/console.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ config :console,
7878
piazza_secret: get_env("PIAZZA_WEBHOOK_SECRET"),
7979
cluster_name: get_env("CLUSTER_NAME"),
8080
is_demo_project: !!get_env("IS_DEMO_PROJECT"),
81+
is_sandbox: !!get_env("CONSOLE_SANDBOX"),
8182
provider: provider
8283

8384
if String.starts_with?(git_url, "https") do

0 commit comments

Comments
 (0)