Metadata-Version: 2.4
Name: inmanta-module-netbird
Version: 0.2.0
Summary: Simple module to manage netbird resources
Author: Guillaume Everarts de Velp
Author-email: edvgui@gmail.com
License: ASL 2.0
Description-Content-Type: text/markdown
Requires-Dist: inmanta-module-std<9,>=4.0
Requires-Dist: inmanta-module-files<3,>=2.11.0
Requires-Dist: inmanta-core>=15.1.0

# inmanta-module-netbird

[![pypi version](https://img.shields.io/pypi/v/inmanta-module-netbird.svg)](https://pypi.python.org/pypi/inmanta-module-netbird/)
[![build status](https://img.shields.io/github/actions/workflow/status/edvgui/inmanta-module-netbird/continuous-integration.yml)](https://github.com/edvgui/inmanta-module-netbird/actions)

This package is an adapter that is meant to be used with the inmanta orchestrator: https://docs.inmanta.com

## Features

This module allows to manage [netbird](https://netbird.io) resources, through the netbird management api.

The module is a work in progress, it currently contains the base entities every
resource of this module builds upon:
1. `netbird::Api`: the endpoint and credentials used to reach the netbird management api.
2. `netbird::ResourceABC`: the base entity for all the resources managed by this module.
3. `netbird::JsonObjectABC`: the base entity for every object of the netbird api.

And the following resources, one per object of the api:
1. `netbird::User`: a user, or a service user, of the account.
2. `netbird::Group`: a group of peers, which every access rule of the account is
   expressed in terms of.
3. `netbird::SetupKey`: the token a peer registers itself with.  The api generates the
   key and shows it once, so the model never holds it: it is published as a fact.
4. `netbird::Peer`: a peer that joined the account.  It can not be created through the
   api — a peer registers itself — so this resource adopts one and manages what the api
   lets it change.
5. `netbird::Network`: a network of the account.
6. `netbird::NetworkResource`: an address, a subnet or a domain a network gives access
   to.
7. `netbird::NetworkRouter`: the peer, or the peers of the groups, routing towards them.
8. `netbird::NameserverGroup`: a set of dns servers and the peer groups resolving with
   them.  Its servers are `netbird::Nameserver` entities embedded in it, not a resource
   of their own.
9. `netbird::DnsSettings`: the dns settings of the account.  They are a singleton the
   api creates with the account and has no endpoint to delete, so this resource only
   ever updates them.
10. `netbird::DnsZone`: a dns zone the account serves, resolved by the peers it is
    distributed to.
11. `netbird::DnsZoneRecord`: a record held by one of those zones.
12. `netbird::Policy`: an access policy of the account — who reaches whom, and over
    what.  Its rule is a `netbird::PolicyRule` entity embedded in it, not a resource of
    its own: the api holds the rules of a policy in a list and keeps one of them.
13. `netbird::AccountSettings`: the network settings of the account — the dns domain of
    its peers, the ranges their overlay addresses come from, and who gets an ipv6 one.
    Like the dns settings, they always exist and are only ever updated.

Every netbird object is co-managed with whoever else edits the account: an attribute
left `null` in the model keeps the value the api currently holds, only the values the
model sets are enforced.  The api addresses its objects by opaque ids, and so does
this module: `netbird::ResourceABC.id` is a reference resolving the id of an object
from the facts its resource publishes, to be fed to whatever other resource points at
it.  The id is not the only fact: every value the api reports about an object is
published under its own key, managed by the model or not, so that a value the model
leaves alone can be referenced all the same, e.g.
`std::create_fact_reference(settings, "network_range")`.  A string is published as it
is, anything else json encoded.

## Example

```
import netbird

api = netbird::Api(
    agent_name="netbird",
    management_url="https://api.netbird.io",
    api_token=std::create_environment_reference("NETBIRD_TOKEN"),
)

netbird::User(
    api=api,
    name="Alice",
    email="alice@example.com",
    role="admin",
)
```

### Registering a peer

A peer can not be created through the api: it comes into existence by registering
itself.  The example below runs two netbird clients in containers, hands them the key
the module created, and manages the peers that show up on the account — with
[`inmanta-module-podman`](https://pypi.python.org/pypi/inmanta-module-podman/) running
the containers.

Two gateways rather than one, because a single peer has nobody to talk to: they join the
same account with the same reusable key, and reach each other over the overlay wherever
they sit on the underlay.  The test running this example checks exactly that, with each
client on a network of its own from which there is no path to the other.

The account they join is described by the same model: a group the key drops every peer
registering with it into, a nameserver group resolving the lab's own domain for the peers
of that group, and the account's dns settings.  None of it names an id — the key's
`auto_groups` and the nameserver group's `groups` read `netbird::Group.id`, the reference
on the fact the group's own resource publishes, and the value is resolved on the agent.
The group's `peers` are left null on purpose: the peers get there by registering, and
what the model does not set, it does not manage.

Two things tie it together.  The key is a secret the api only ever shows once, so the
model never holds the value: it is published as a fact when the key is created, and only
resolved on the agent, at the moment the environment file is written.  And each client
reports its container's hostname when it registers, which is what `netbird::Peer` finds
it back by.

The key can not be handed to the containers through `podman::Container.env`: the quadlet
file is rendered at compile time, where the key is still a reference rather than a
string.  It goes through an environment file, and the reference is created inside the
template — `files::jinja` declares its arguments as `object`, and the dsl refuses to
pass a reference there, so `netbird::SetupKey._key` can not be forwarded from the model.
The template reaches the same fact through the same `std::create_fact_reference` that
`_key` is built from.

The project provides the template the environment file is rendered from:

<x-example-netbird-client-template>

```
NB_SETUP_KEY={{ setup_key | std.create_fact_reference("key") }}
NB_MANAGEMENT_URL={{ management_url }}
```

</x-example-netbird-client-template>

<x-example-netbird-client>

```
import files
import mitogen
import netbird
import podman
import podman::services
import std

api = netbird::Api(
    agent_name="netbird",
    management_url="https://api.netbird.io",
    # A reference, not std::get_env: the token stays out of the desired state
    # and is resolved on the agent, at deploy time.
    api_token=std::create_environment_reference("NETBIRD_TOKEN"),
)

host = std::Host(
    name="localhost",
    os=std::linux,
    via=mitogen::Local(),
)

# Each client reports its own hostname when it registers, and the hostname is
# what identifies a peer here: the api offers no way to change it, while the
# name of a peer is one of the values this model rewrites.  Two gateways, so
# that there is an overlay to speak of: once both have joined the account they
# reach each other over it, wherever they sit on the underlay.
hostnames = ["lab-gateway-a", "lab-gateway-b"]

# Everything below runs rootless, as this unprivileged user.  It needs access to
# /dev/net/tun, and `loginctl enable-linger` on it, so that its units keep
# running while it is not logged in.
user = "netbird"

# The group the two gateways end up in.  Its `peers` are left null, so the
# model does not manage them: the clients join by registering with the key
# below, which is what the key's auto groups do.
gateways = netbird::Group(
    api=api,
    name="lab-gateways",
)

# The token both clients register with.  The api generates it and the model
# never sees the value: it is published as a fact when the key is created.  A
# reusable key, since more than one client joins with it.
setup_key = netbird::SetupKey(
    api=api,
    name="lab-gateways",
    type="reusable",
    expires_in=86400,
    # The id of a group the model never reads either: it is a reference on the
    # fact the group's own resource publishes.  Every peer registering with
    # this key lands in that group.
    auto_groups=[gateways.id],
    # The api refuses an auto group it doesn't know, so the group has to be
    # there first.  A reference is not a dependency of its own.
    requires=gateways,
)

# The dns the gateways resolve the lab's own domain with.  The api wants one to
# three servers, at least one group to distribute them to, and either the
# primary flag or a domain — never both, never neither.
netbird::NameserverGroup(
    api=api,
    name="lab-dns",
    enabled=true,
    groups=[gateways.id],
    domains=["lab.example.com"],
    nameservers=[
        netbird::Nameserver(ip="9.9.9.9", ns_type="udp", port=53),
        netbird::Nameserver(ip="1.1.1.1", ns_type="udp", port=53),
    ],
    requires=gateways,
)

# And netbird resolves for every peer of the account: no group opts out of it.
# These settings are a singleton the api creates with the account, so this
# resource only ever updates them — purging it is an error.
netbird::DnsSettings(
    api=api,
    disabled_management_groups=[],
)

for hostname in hostnames:
    # One configuration directory per client: they share the account and the
    # key, nothing else.
    config_dir = files::Directory(
        host=host,
        path=f"/home/netbird/.config/netbird/{hostname}",
        owner=user,
        create_parents=true,
    )

    # The key can not go through podman::Container.env: the quadlet file is
    # rendered at compile time, where the key is still a reference and not a
    # string.  It goes through an environment file instead, whose content stays
    # a reference until the agent writes it on the host.
    env_file = files::TextFile(
        host=host,
        # No need to require the directory, the files exporter wires that up.
        path=f"{config_dir.path}/client.env",
        content=files::jinja(
            "template:///netbird-client.env.j2",
            setup_key=setup_key,
            management_url=api.management_url,
        ),
        owner=user,
        # The key is a secret: only its owner gets to read it.
        permissions=600,
        # A fact reference is not a dependency: the key has to exist, and its
        # fact to be published, before the agent can resolve it here.
        requires=setup_key,
    )

    # The netbird client itself.  NET_ADMIN, NET_RAW and /dev/net/tun are what
    # it takes to bring the wireguard interface up.
    client = podman::Container(
        host=host,
        owner=user,
        name=hostname,
        hostname=hostname,
        image="docker.io/netbirdio/netbird:latest",
        env_file=env_file.path,
        add_capability=["NET_ADMIN", "NET_RAW"],
        add_device=["/dev/net/tun"],
        requires=env_file,
    )

    # podman::Container is not a resource of its own: it is rendered into a
    # quadlet unit, and that unit file is what gets deployed.
    service = podman::services::SystemdContainer(
        container=client,
        state="running",
        enabled=true,
        quadlet=true,
        systemd_unit_dir="/home/netbird/.config/systemd/user",
        systemd_container_dir="/home/netbird/.config/containers/systemd",
        systemctl_command=["systemctl", "--user"],
    )

    # And the peer the client registered.  This resource adopts a peer rather
    # than creating one, so it only deploys once the client has joined the
    # account: before that it skips, saying so.
    netbird::Peer(
        api=api,
        hostname=hostname,
        ssh_enabled=false,
        requires=service.resources,
    )
end

```

</x-example-netbird-client>

### Routing a network, and naming what is behind it

A netbird network is a container: the addresses it gives access to and the peers routing
towards them are objects of their own, addressed under its id.  The example below builds
one whole — the groups the account is expressed in terms of, the keys the peers join
with, the office subnet, the gateways routing it, and a dns zone naming the hosts behind
it.

The test running it deploys every bit of that against a real server and then checks that
it routes.  Two netbird clients run in containers on bridge networks isolated from each
other, and a third container stands in for a printer on the gateway's own bridge, running
no netbird client at all.  Before the network resource and its router are deployed the
client has no way to reach that printer; afterwards it reaches it by address, and once the
zone is deployed by name — over the overlay, through a peer forwarding for a host that
never heard of netbird.

Nothing in the model writes an id down.  The api addresses everything by opaque id, so
the keys point at their group with `gateways.id` and `clients.id`, the network resource at
its network with `network.id` and the records at their zone with `zone.id`: each of them
is a reference on the fact the other resource publishes, resolved on the agent at deploy
time.  The `requires` next to them are not redundant — a reference is not a dependency,
and the api refuses a resource in a network that does not exist yet.

Routing the subnet is not the same as being allowed to reach it: the `netbird::Policy`
is what the traffic goes through, from the group of the peers to the group of the
resource.  Nothing reaches a network resource without one — the account's own `Default`
policy only covers the peers of its `All` group, and a resource is in no group but the
ones it was given, which is why the resource gets a group of its own here.  The test
makes that point by purging the policy at the end: the route stays, and the pings stop.

The router routes for a group rather than for a named peer: `peer` and `peer_groups` are
mutually exclusive, and exactly one of them has to be set.  Going through a group means
the model does not have to know which peers are gateways today — whoever registers with
the gateway key starts routing.

<x-example-netbird-network>

```
import netbird
import std

api = netbird::Api(
    agent_name="netbird",
    management_url="https://api.netbird.io",
    # A reference, not std::get_env: the token stays out of the desired state
    # and is resolved on the agent, at deploy time.
    api_token=std::create_environment_reference("NETBIRD_TOKEN"),
)

# The service user whose access token the orchestrator drives the account
# with.  A service user can not log in and the api keeps no email address for
# it, so its name is all there is to identify it by.
netbird::User(
    api=api,
    name="inmanta",
    role="admin",
    is_service_user=true,
)

# The two groups the account is expressed in terms of: the peers routing
# towards the office lan, and the peers allowed to reach it.  Their `peers` are
# left null, so the model does not manage the membership — a peer gets there by
# registering with the key whose auto groups name the group.
gateways = netbird::Group(
    api=api,
    name="office-gateways",
)
clients = netbird::Group(
    api=api,
    name="office-clients",
)

# And the group the office lan itself is in.  The groups of a network resource
# are what the policies of the account point at, they are not the peers reaching
# it: a resource is a destination, not a member.
lan = netbird::Group(
    api=api,
    name="office-lan",
)

# One key per role, so that what a peer is follows from the key it joined with.
# The api generates the key and shows it once: the model never holds the value,
# it is published as a fact when the key is created.
netbird::SetupKey(
    api=api,
    name="office-gateways",
    type="reusable",
    expires_in=86400,
    auto_groups=[gateways.id],
    # The api refuses an auto group it doesn't know, and a reference is not a
    # dependency of its own.
    requires=gateways,
)
netbird::SetupKey(
    api=api,
    name="office-clients",
    type="reusable",
    expires_in=86400,
    auto_groups=[clients.id],
    requires=clients,
)

# The network holding what the gateways give access to.  A netbird network is
# nothing but that container: the addresses and the routers are objects of
# their own, addressed under its id.
network = netbird::Network(
    api=api,
    name="office",
    description="The office lan, reached through the gateways",
)

# What the network gives access to.  The api derives the type of a resource
# from its address — a host address, a subnet or a domain — so there is no type
# to set here.  Its groups are what the policy below points at, they are not the
# peers reaching it.
netbird::NetworkResource(
    api=api,
    _network=network.id,
    name="office-lan",
    address="10.10.0.0/24",
    enabled=true,
    groups=[lan.id],
    # The api refuses a resource in a network that does not exist, and silently
    # drops a group id it does not know.
    requires=[network, lan],
)

# And who routes the traffic there: the peers of the gateway group rather than
# one named peer.  Exactly one of `peer` and `peer_groups` may be set, the api
# refuses a router with neither and a router with both.
netbird::NetworkRouter(
    api=api,
    _network=network.id,
    # The api gives a router no name, it only knows it by what it routes for.
    # This one identifies the resource inmanta deploys, nothing else.
    _name="office-gateways",
    peer_groups=[gateways.id],
    metric=9999,
    # The hosts of the office lan know nothing of netbird, so the gateway
    # rewrites the source address of what it routes for them.
    masquerade=true,
    enabled=true,
    requires=[network, gateways],
)

# Routing the subnet is not the same as being allowed to reach it: a peer
# reaches the office lan because this policy says the peers of the client group
# may.  Nothing reaches a network resource without one — the account's own
# `Default` policy only covers the peers of its `All` group, and a resource is
# in no group but the ones it was given.
netbird::Policy(
    api=api,
    name="office",
    enabled=true,
    rule=netbird::PolicyRule(
        sources=[clients.id],
        destinations=[lan.id],
        # A rule needs a protocol and an action to be created at all, and the
        # api refuses ports on an `all` rule.
        protocol="all",
        action="accept",
        bidirectional=true,
        enabled=true,
    ),
    requires=[clients, lan],
)

# The zone that gives the addresses of that subnet names, resolved by the peers
# of the client group.  Its domain is what identifies it, and the api refuses a
# change to it once the zone exists.
zone = netbird::DnsZone(
    api=api,
    domain="office.example.com",
    name="office",
    enabled=true,
    # The domain is pushed to the peers as a search domain, so that they
    # resolve the names of the zone unqualified as well.
    enable_search_domain=true,
    distribution_groups=[clients.id],
    requires=clients,
)

# The records of that zone.  A record name is fully qualified inside the domain
# of its zone, without a trailing dot, and the api validates the content
# against the type: an address for an A record, a target name for a CNAME.
netbird::DnsZoneRecord(
    api=api,
    _zone=zone.id,
    name="printer.office.example.com",
    type="A",
    content="10.10.0.9",
    ttl=300,
    requires=zone,
)
netbird::DnsZoneRecord(
    api=api,
    _zone=zone.id,
    name="scanner.office.example.com",
    type="CNAME",
    content="printer.office.example.com",
    ttl=300,
    requires=zone,
)

```

</x-example-netbird-network>

Find more examples in the `tests` folder of this module!

## Development

```sh
python3 -m venv .venv
source .venv/bin/activate
make install
pytest tests
```

The tests deploy a netbird server locally, in a podman container of their own, and
run against its api.  The container is started again for each test, so that every
test sees a fresh, empty account.  Every peer the tests register runs in a bridge
network of its own, which the server container joins and which is isolated from the
others, so peers only ever reach each other over the netbird overlay.  They are skipped
when podman is not available.
