summary refs log tree commit diff
path: root/nixos/tests/web-apps/netbox.nix
blob: 30de74f1886c0aaf72d5c0d60f92e8f12e606743 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
let
  ldapDomain = "example.org";
  ldapSuffix = "dc=example,dc=org";

  ldapRootUser = "admin";
  ldapRootPassword = "foobar";

  testUser = "alice";
  testPassword = "verySecure";
  testGroup = "netbox-users";
in import ../make-test-python.nix ({ lib, pkgs, netbox, ... }: {
  name = "netbox";

  meta = with lib.maintainers; {
    maintainers = [ minijackson n0emis ];
  };

  nodes.machine = { config, ... }: {
    services.netbox = {
      enable = true;
      package = netbox;
      secretKeyFile = pkgs.writeText "secret" ''
        abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
      '';

      enableLdap = true;
      ldapConfigPath = pkgs.writeText "ldap_config.py" ''
        import ldap
        from django_auth_ldap.config import LDAPSearch, PosixGroupType

        AUTH_LDAP_SERVER_URI = "ldap://localhost/"

        AUTH_LDAP_USER_SEARCH = LDAPSearch(
            "ou=accounts,ou=posix,${ldapSuffix}",
            ldap.SCOPE_SUBTREE,
            "(uid=%(user)s)",
        )

        AUTH_LDAP_GROUP_SEARCH = LDAPSearch(
            "ou=groups,ou=posix,${ldapSuffix}",
            ldap.SCOPE_SUBTREE,
            "(objectClass=posixGroup)",
        )
        AUTH_LDAP_GROUP_TYPE = PosixGroupType()

        # Mirror LDAP group assignments.
        AUTH_LDAP_MIRROR_GROUPS = True

        # For more granular permissions, we can map LDAP groups to Django groups.
        AUTH_LDAP_FIND_GROUP_PERMS = True
      '';
    };

    services.nginx = {
      enable = true;

      recommendedProxySettings = true;

      virtualHosts.netbox = {
        default = true;
        locations."/".proxyPass = "http://localhost:${toString config.services.netbox.port}";
        locations."/static/".alias = "/var/lib/netbox/static/";
      };
    };

    # Adapted from the sssd-ldap NixOS test
    services.openldap = {
      enable = true;
      settings = {
        children = {
          "cn=schema".includes = [
            "${pkgs.openldap}/etc/schema/core.ldif"
            "${pkgs.openldap}/etc/schema/cosine.ldif"
            "${pkgs.openldap}/etc/schema/inetorgperson.ldif"
            "${pkgs.openldap}/etc/schema/nis.ldif"
          ];
          "olcDatabase={1}mdb" = {
            attrs = {
              objectClass = [ "olcDatabaseConfig" "olcMdbConfig" ];
              olcDatabase = "{1}mdb";
              olcDbDirectory = "/var/lib/openldap/db";
              olcSuffix = ldapSuffix;
              olcRootDN = "cn=${ldapRootUser},${ldapSuffix}";
              olcRootPW = ldapRootPassword;
            };
          };
        };
      };
      declarativeContents = {
        ${ldapSuffix} = ''
          dn: ${ldapSuffix}
          objectClass: top
          objectClass: dcObject
          objectClass: organization
          o: ${ldapDomain}

          dn: ou=posix,${ldapSuffix}
          objectClass: top
          objectClass: organizationalUnit

          dn: ou=accounts,ou=posix,${ldapSuffix}
          objectClass: top
          objectClass: organizationalUnit

          dn: uid=${testUser},ou=accounts,ou=posix,${ldapSuffix}
          objectClass: person
          objectClass: posixAccount
          userPassword: ${testPassword}
          homeDirectory: /home/${testUser}
          uidNumber: 1234
          gidNumber: 1234
          cn: ""
          sn: ""

          dn: ou=groups,ou=posix,${ldapSuffix}
          objectClass: top
          objectClass: organizationalUnit

          dn: cn=${testGroup},ou=groups,ou=posix,${ldapSuffix}
          objectClass: posixGroup
          gidNumber: 2345
          memberUid: ${testUser}
        '';
      };
    };

    users.users.nginx.extraGroups = [ "netbox" ];

    networking.firewall.allowedTCPPorts = [ 80 ];
  };

  testScript = let
    changePassword = pkgs.writeText "change-password.py" ''
      from django.contrib.auth.models import User
      u = User.objects.get(username='netbox')
      u.set_password('netbox')
      u.save()
    '';
  in ''
    from typing import Any, Dict
    import json

    start_all()
    machine.wait_for_unit("netbox.target")
    machine.wait_until_succeeds("journalctl --since -1m --unit netbox --grep Listening")

    with subtest("Home screen loads"):
        machine.succeed(
            "curl -sSfL http://[::1]:8001 | grep '<title>Home | NetBox</title>'"
        )

    with subtest("Staticfiles are generated"):
        machine.succeed("test -e /var/lib/netbox/static/netbox.js")

    with subtest("Superuser can be created"):
        machine.succeed(
            "netbox-manage createsuperuser --noinput --username netbox --email netbox@example.com"
        )
        # Django doesn't have a "clean" way of inputting the password from the command line
        machine.succeed("cat '${changePassword}' | netbox-manage shell")

    machine.wait_for_unit("network.target")

    with subtest("Home screen loads from nginx"):
        machine.succeed(
            "curl -sSfL http://localhost | grep '<title>Home | NetBox</title>'"
        )

    with subtest("Staticfiles can be fetched"):
        machine.succeed("curl -sSfL http://localhost/static/netbox.js")
        machine.succeed("curl -sSfL http://localhost/static/docs/")

    with subtest("Can interact with API"):
        json.loads(
            machine.succeed("curl -sSfL -H 'Accept: application/json' 'http://localhost/api/'")
        )

    def login(username: str, password: str):
        encoded_data = json.dumps({"username": username, "password": password})
        uri = "/users/tokens/provision/"
        result = json.loads(
            machine.succeed(
                "curl -sSfL "
                "-X POST "
                "-H 'Accept: application/json' "
                "-H 'Content-Type: application/json' "
                f"'http://localhost/api{uri}' "
                f"--data '{encoded_data}'"
            )
        )
        return result["key"]

    with subtest("Can login"):
        auth_token = login("netbox", "netbox")

    def get(uri: str):
        return json.loads(
            machine.succeed(
                "curl -sSfL "
                "-H 'Accept: application/json' "
                f"-H 'Authorization: Token {auth_token}' "
                f"'http://localhost/api{uri}'"
            )
        )

    def delete(uri: str):
        return machine.succeed(
            "curl -sSfL "
            f"-X DELETE "
            "-H 'Accept: application/json' "
            f"-H 'Authorization: Token {auth_token}' "
            f"'http://localhost/api{uri}'"
        )


    def data_request(uri: str, method: str, data: Dict[str, Any]):
        encoded_data = json.dumps(data)
        return json.loads(
            machine.succeed(
                "curl -sSfL "
                f"-X {method} "
                "-H 'Accept: application/json' "
                "-H 'Content-Type: application/json' "
                f"-H 'Authorization: Token {auth_token}' "
                f"'http://localhost/api{uri}' "
                f"--data '{encoded_data}'"
            )
        )

    def post(uri: str, data: Dict[str, Any]):
      return data_request(uri, "POST", data)

    def patch(uri: str, data: Dict[str, Any]):
      return data_request(uri, "PATCH", data)

    with subtest("Can create objects"):
        result = post("/dcim/sites/", {"name": "Test site", "slug": "test-site"})
        site_id = result["id"]

        # Example from:
        # http://netbox.extra.cea.fr/static/docs/integrations/rest-api/#creating-a-new-object
        post("/ipam/prefixes/", {"prefix": "192.0.2.0/24", "site": site_id})

        result = post(
            "/dcim/manufacturers/",
            {"name": "Test manufacturer", "slug": "test-manufacturer"}
        )
        manufacturer_id = result["id"]

        # Had an issue with device-types before NetBox 3.4.0
        result = post(
            "/dcim/device-types/",
            {
                "model": "Test device type",
                "manufacturer": manufacturer_id,
                "slug": "test-device-type",
            },
        )
        device_type_id = result["id"]

    with subtest("Can list objects"):
        result = get("/dcim/sites/")

        assert result["count"] == 1
        assert result["results"][0]["id"] == site_id
        assert result["results"][0]["name"] == "Test site"
        assert result["results"][0]["description"] == ""

        result = get("/dcim/device-types/")
        assert result["count"] == 1
        assert result["results"][0]["id"] == device_type_id
        assert result["results"][0]["model"] == "Test device type"

    with subtest("Can update objects"):
        new_description = "Test site description"
        patch(f"/dcim/sites/{site_id}/", {"description": new_description})
        result = get(f"/dcim/sites/{site_id}/")
        assert result["description"] == new_description

    with subtest("Can delete objects"):
        # Delete a device-type since no object depends on it
        delete(f"/dcim/device-types/{device_type_id}/")

        result = get("/dcim/device-types/")
        assert result["count"] == 0

    with subtest("Can use the GraphQL API"):
        encoded_data = json.dumps({
            "query": "query { prefix_list { prefix, site { id, description } } }",
        })
        result = json.loads(
            machine.succeed(
                "curl -sSfL "
                "-H 'Accept: application/json' "
                "-H 'Content-Type: application/json' "
                f"-H 'Authorization: Token {auth_token}' "
                "'http://localhost/graphql/' "
                f"--data '{encoded_data}'"
            )
        )

        assert len(result["data"]["prefix_list"]) == 1
        assert result["data"]["prefix_list"][0]["prefix"] == "192.0.2.0/24"
        assert result["data"]["prefix_list"][0]["site"]["id"] == str(site_id)
        assert result["data"]["prefix_list"][0]["site"]["description"] == new_description

    with subtest("Can login with LDAP"):
        machine.wait_for_unit("openldap.service")
        login("alice", "${testPassword}")

    with subtest("Can associate LDAP groups"):
        result = get("/users/users/?username=${testUser}")

        assert result["count"] == 1
        assert any(group["name"] == "${testGroup}" for group in result["results"][0]["groups"])
  '';
})