summaryrefslogtreecommitdiffstats
path: root/nixos/tests/web-apps/netbox/default.nix
blob: f69eb85cf72ec56bc8ed6bce21bab7b75843f7cc (plain)
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
{ lib, pkgs, ... }:
let
  ldapDomain = "example.org";
  ldapSuffix = "dc=example,dc=org";

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

  testUser = "alice";
  testPassword = "verySecure";
  testGroup = "netbox-users";
in
{
  name = "netbox";

  meta.maintainers = with lib.maintainers; [ minijackson ];

  skipTypeCheck = true;

  containers.machine = {
    boot.kernelParams = [
      # helps debugging seccomp filter issues
      "audit=1"
    ];
    services.netbox = {
      enable = true;

      nginx = {
        enable = true;
        hostname = "localhost";
      };

      ldapConfigFile = 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
      '';
    };

    # 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 users.models import User
        u = User.objects.get(username='netbox')
        u.set_password('netbox')
        u.save()
      '';
      createToken = pkgs.writeText "create-token-v2.py" ''
        from users.models import Token, User
        from users.choices import TokenVersionChoices
        u = User.objects.first()
        t = Token.objects.create(user=u, token="0123456789abcdef0123456789abcdef01234567", version=TokenVersionChoices.V2)
        print(t.get_auth_header_prefix())
      '';
      netboxVersion = pkgs.netbox.version;
    in
    builtins.replaceStrings
      [
        "\${changePassword}"
        "\${testUser}"
        "\${testPassword}"
        "\${testGroup}"
        "\${createToken}"
        "\${netboxVersion}"
      ]
      [
        "${changePassword}"
        "${testUser}"
        "${testPassword}"
        "${testGroup}"
        "${createToken}"
        "${netboxVersion}"
      ]
      (lib.readFile "${./testScript.py}");
}