Skip to content

Commit 7e6ac00

Browse files
committed
feat: chapter 3 quiz
1 parent de6a6b6 commit 7e6ac00

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

modules/3-ssdlc.livemd

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Mix.install([
55
{:grading_client, path: "#{__DIR__}/grading_client"}
66
])
77

8+
System.put_env("envar_secret", "some-secret-password")
9+
810
:ok
911
```
1012

@@ -45,12 +47,40 @@ A very easy way to prevent secrets being added to files is to access them via En
4547

4648
_Use `System.get_env/1` on line 2._
4749

48-
```elixir
49-
# let's assume there is an environment variable named 'envar_secret'
50-
super_secret_password = "p@ssw0rd"
50+
<!-- livebook:{"attrs":"eyJzb3VyY2UiOiIjIEVTQ1Q6MVxuc3VwZXJfc2VjcmV0X3Bhc3N3b3JkID0gXCJwQHNzdzByZFwiIn0","chunks":null,"kind":"Elixir.GradingClient.GradedCell","livebook_object":"smart_cell"} -->
5151

52-
# DO NOT CHANGE CODE BELOW THIS COMMENT
53-
IO.puts(super_secret_password)
52+
```elixir
53+
result = super_secret_password = "p@ssw0rd"
54+
55+
[module_id, question_id] =
56+
"# ESCT:1\nsuper_secret_password = \"p@ssw0rd\""
57+
|> String.split("\n", parts: 2)
58+
|> hd()
59+
|> String.trim_leading("#")
60+
|> String.split(":", parts: 2)
61+
62+
module_id =
63+
case %{"ESCT" => ESCT, "OWASP" => OWASP}[String.trim(module_id)] do
64+
nil -> raise "invalid module id: #{module_id}"
65+
module_id -> module_id
66+
end
67+
68+
question_id =
69+
case Integer.parse(String.trim(question_id)) do
70+
{id, ""} -> id
71+
_ -> raise "invalid question id: #{question_id}"
72+
end
73+
74+
case GradingClient.check_answer(module_id, question_id, result) do
75+
:correct ->
76+
IO.puts([IO.ANSI.green(), "Correct!", IO.ANSI.reset()])
77+
78+
{:incorrect, help_text} when is_binary(help_text) ->
79+
IO.puts([IO.ANSI.red(), "Incorrect: ", IO.ANSI.reset(), help_text])
80+
81+
_ ->
82+
IO.puts([IO.ANSI.red(), "Incorrect.", IO.ANSI.reset()])
83+
end
5484
```
5585

5686
## Making Secret Rotation Easy

modules/grading_client/priv/answers.exs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ owasp_questions = [
2424
}
2525
]
2626

27+
esct_questions = [
28+
%{
29+
question_id: 1,
30+
answer: "some-secret-password",
31+
help_text: "Use System.get_env/1 to get the password from the environment variable."
32+
}
33+
]
34+
2735
List.flatten([
28-
to_answers.(OWASP, owasp_questions)
36+
to_answers.(OWASP, owasp_questions),
37+
to_answers.(ESCT, esct_questions)
2938
])

0 commit comments

Comments
 (0)