Skip to content

setup-secrets

lib.setup-secrets.mkScript

Build an executable shell script from script, with getKubeSecret and setKubeSecret helper functions available to it, and kubectl already pointed at the cluster via KUBECONFIG.

getKubeSecret <namespace> <name> <field> prints the base64-decoded value of a field in a Kubernetes secret. setKubeSecret <namespace> <name> <field> <value> [<field> <value> ...] creates or updates a secret with the given field/value pairs.

Sources and destinations that talk to the cluster are best built with the helpers below, which put destinations in the cluster group (run after k3s, see the k3s module) and retry while the cluster comes up.

Arguments

pkgs: nixpkgs

script (String): Shell script appended after the helpers are defined

Example

setup-secrets.sources.UNIFI_PASSWORD = {
  description = "Unifi password";
  cmd = setup-secrets.mkScript pkgs "getKubeSecret unifi unifi-credentials password";
};

lib.setup-secrets.kubeSecretSource

A setup-secrets source that reads one field of a Kubernetes Secret.

A missing Secret or a missing field prints nothing, a value nobody has supplied yet. With generate, it makes a new value from that shell command instead. An API that doesn't answer fails the source, so a value is never replaced because the cluster was down. The source names the Secret as its storedIn, so the setup-secrets TUI lets you edit it.

Arguments

pkgs: nixpkgs

An attribute set: - description (String): shown in the form - namespace, name, field (String): the Secret and its field - generate (String, optional): shell command that prints a new value

Example

setup-secrets.sources.APP_PASSWORD = setup-secrets.kubeSecretSource pkgs {
  description = "App password";
  namespace = "app";
  name = "app-secrets";
  field = "password";
  generate = "head -c 1024 /dev/urandom | tr -dc A-Za-z0-9 | head -c 32";
};

lib.setup-secrets.kubeExecSource

A setup-secrets source that runs a command in a container of a workload, after waiting for the workload to roll out, for values a workload makes itself (an API key in its configuration). It reads something setup-secrets never writes, so the value can only be changed in the workload.

Arguments

pkgs: nixpkgs

An attribute set: - description (String): shown in the form - namespace, workload (String): workload as kubectl takes it, deploy/sonarr - container (String) - command (String): shell words passed to kubectl exec, quoted as needed - same (String, optional): shell words passed to kubectl exec that print a cheap token that changes with the value, the mtime of the file it is read from (stat -c %Y /data/config.xml). Without it the source always counts as changed, so the destinations that need it always run

Example

setup-secrets.sources.SONARR_API_KEY = setup-secrets.kubeExecSource pkgs {
  description = "Sonarr API Key";
  namespace = "sonarr";
  workload = "deploy/sonarr";
  container = "sonarr";
  command = "xq -q 'Config>ApiKey' /data/config.xml";
};

lib.setup-secrets.kubeSecretDestination

A setup-secrets destination that writes one Kubernetes Secret, in the cluster group.

Arguments

pkgs: nixpkgs

An attribute set: - logPrefix (String) - namespace, name (String): the Secret - data (AttrsOf String): field of the Secret to the source whose value it gets; every source is required - restart (ListOf String, default [ ]): workloads in the Secret's namespace (deployment/grafana) restarted when a write changes the Secret, for pods that start without it (optional: true) and read it only at start. One not created yet is skipped, as it starts with the Secret - enable (Bool, default true)

Example

setup-secrets.destinations = [
  (setup-secrets.kubeSecretDestination pkgs {
    logPrefix = "Unifi credentials";
    namespace = "unifi";
    name = "unifi-credentials";
    data.password = "UNIFI_PASSWORD";
  })
];

lib.setup-secrets.clusterDestination

A setup-secrets destination in the cluster group with a script of its own, for what kubeSecretDestination doesn't cover (several Secrets, a value derived in the script).

Arguments

pkgs: nixpkgs

An attribute set: - logPrefix (String) - requires, wants (ListOf String, default [ ]) - script (String): run through mkScript - outputs (ListOf String, default [ ]): what the script writes, kube:<namespace>/<name> for a Secret - enable (Bool, default true)