1import ./make-test-python.nix (
2 { pkgs, lib, ... }:
3 {
4 name = "graylog";
5 meta.maintainers = [ ];
6
7 nodes.machine =
8 { pkgs, ... }:
9 {
10 virtualisation.memorySize = 4096;
11 virtualisation.diskSize = 1024 * 6;
12
13 services.mongodb.enable = true;
14 services.elasticsearch.enable = true;
15 services.elasticsearch.extraConf = ''
16 network.publish_host: 127.0.0.1
17 network.bind_host: 127.0.0.1
18 '';
19
20 services.graylog = {
21 enable = true;
22 passwordSecret = "YGhZ59wXMrYOojx5xdgEpBpDw2N6FbhM4lTtaJ1KPxxmKrUvSlDbtWArwAWMQ5LKx1ojHEVrQrBMVRdXbRyZLqffoUzHfssc";
23 elasticsearchHosts = [ "http://localhost:9200" ];
24
25 # `echo -n "nixos" | shasum -a 256`
26 rootPasswordSha2 = "6ed332bcfa615381511d4d5ba44a293bb476f368f7e9e304f0dff50230d1a85b";
27 };
28
29 environment.systemPackages = [ pkgs.jq ];
30
31 systemd.services.graylog.path = [ pkgs.netcat ];
32 systemd.services.graylog.preStart = ''
33 until nc -z localhost 9200; do
34 sleep 2
35 done
36 '';
37 };
38
39 testScript =
40 let
41 payloads.login = pkgs.writeText "login.json" (
42 builtins.toJSON {
43 host = "127.0.0.1:9000";
44 username = "admin";
45 password = "nixos";
46 }
47 );
48
49 payloads.input = pkgs.writeText "input.json" (
50 builtins.toJSON {
51 title = "Demo";
52 global = false;
53 type = "org.graylog2.inputs.gelf.udp.GELFUDPInput";
54 node = "@node@";
55 configuration = {
56 bind_address = "0.0.0.0";
57 decompress_size_limit = 8388608;
58 number_worker_threads = 1;
59 override_source = null;
60 port = 12201;
61 recv_buffer_size = 262144;
62 };
63 }
64 );
65
66 payloads.gelf_message = pkgs.writeText "gelf.json" (
67 builtins.toJSON {
68 host = "example.org";
69 short_message = "A short message";
70 full_message = "A long message";
71 version = "1.1";
72 level = 5;
73 facility = "Test";
74 }
75 );
76 in
77 ''
78 machine.start()
79 machine.wait_for_unit("graylog.service")
80
81 machine.wait_until_succeeds(
82 "journalctl -o cat -u graylog.service | grep 'Started REST API at <127.0.0.1:9000>'"
83 )
84
85 machine.wait_for_open_port(9000)
86 machine.succeed("curl -sSfL http://127.0.0.1:9000/")
87
88 machine.wait_until_succeeds(
89 "journalctl -o cat -u graylog.service | grep 'Graylog server up and running'"
90 )
91
92 session = machine.succeed(
93 "curl -X POST "
94 + "-sSfL http://127.0.0.1:9000/api/system/sessions "
95 + "-d $(cat ${payloads.login}) "
96 + "-H 'Content-Type: application/json' "
97 + "-H 'Accept: application/json' "
98 + "-H 'x-requested-by: cli' "
99 + "| jq .session_id | xargs echo"
100 ).rstrip()
101
102 machine.succeed(
103 "curl -X POST "
104 + f"-sSfL http://127.0.0.1:9000/api/system/inputs -u {session}:session "
105 + '-d $(cat ${payloads.input} | sed -e "s,@node@,$(cat /var/lib/graylog/server/node-id),") '
106 + "-H 'Accept: application/json' "
107 + "-H 'Content-Type: application/json' "
108 + "-H 'x-requested-by: cli' "
109 )
110
111 machine.wait_until_succeeds(
112 "journalctl -o cat -u graylog.service | grep -E 'Input \[GELF UDP/Demo/[[:alnum:]]{24}\] is now RUNNING'"
113 )
114
115 machine.wait_until_succeeds(
116 "test \"$(curl -sSfL 'http://127.0.0.1:9000/api/cluster/inputstates' "
117 + f"-u {session}:session "
118 + "-H 'Accept: application/json' "
119 + "-H 'Content-Type: application/json' "
120 + "-H 'x-requested-by: cli'"
121 + "| jq 'to_entries[]|.value|.[0]|.state' | xargs echo"
122 + ')" = "RUNNING"'
123 )
124
125 machine.succeed(
126 "echo -n $(cat ${payloads.gelf_message}) | nc -w10 -u 127.0.0.1 12201"
127 )
128
129 machine.succeed(
130 'test "$(curl -X GET '
131 + "-sSfL 'http://127.0.0.1:9000/api/search/universal/relative?query=*' "
132 + f"-u {session}:session "
133 + "-H 'Accept: application/json' "
134 + "-H 'Content-Type: application/json' "
135 + "-H 'x-requested-by: cli'"
136 + ' | jq \'.total_results\' | xargs echo)" = "1"'
137 )
138 '';
139 }
140)