Skip to content

Commit

Permalink
vpn: ship our own container image (#2909)
Browse files Browse the repository at this point in the history
* vpn: ship our own container image

The container image used in the VPN chart should be reproducible and
stable. We're sticking close to the original nixery.dev version by
building the image with nix ourselves, and then publishing the single
layer from the result with Bazel OCI rules. The resulting image should
be handled similar to s3proxy: it's built as a part of the Constellation
release process and then consumed from a Helm chart in our registry.

Co-authored-by: Malte Poll <[email protected]>
  • Loading branch information
burgerdev and malt3 authored Feb 15, 2024
1 parent 896f68c commit 473001b
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 40 deletions.
8 changes: 8 additions & 0 deletions WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ nixpkgs_flake_package(
package = "uplosi",
)

nixpkgs_flake_package(
name = "vpn_oci_image",
build_file_content = """exports_files(["layer.tar"])""",
nix_flake_file = "//:flake.nix",
nix_flake_lock_file = "//:flake.lock",
package = "vpn",
)

nixpkgs_package(
name = "diffutils",
repository = "@nixpkgs",
Expand Down
8 changes: 8 additions & 0 deletions bazel/oci/containers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def containers():
"repotag_file": "//bazel/release:s3proxy_tag.txt",
"used_by": ["config"],
},
{
"identifier": "vpn",
"image_name": "vpn",
"name": "vpn",
"oci": "//nix/container/vpn",
"repotag_file": "//bazel/release:vpn_tag.txt",
"used_by": ["config"],
},
]

def helm_containers():
Expand Down
11 changes: 0 additions & 11 deletions dev-docs/howto/vpn/helm/files/strongswan/charon-logging.conf

This file was deleted.

10 changes: 1 addition & 9 deletions dev-docs/howto/vpn/helm/templates/operator-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,5 @@ spec:
containers:
- name: operator
image: {{ .Values.image | quote }}
command: ["sh", "/scripts/entrypoint.sh"]
command: ["/bin/operator.sh"]
env: {{- include "..commonEnv" . | nindent 10 }}
volumeMounts:
- name: scripts
mountPath: "/scripts"
readOnly: true
volumes:
- name: scripts
configMap:
name: {{ include "..fullname" . }}-operator
19 changes: 2 additions & 17 deletions dev-docs/howto/vpn/helm/templates/strongswan-statefulset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,22 @@ spec:
containers:
- name: strongswan
image: {{ .Values.image | quote }}
command: ["sh", "-x", "/entrypoint.sh"]
command: ["/bin/strongswan.sh"]
securityContext:
capabilities:
add: ["NET_ADMIN"]
volumeMounts:
- name: files
mountPath: "/entrypoint.sh"
subPath: "entrypoint.sh"
readOnly: true
- name: files
mountPath: "/etc/strongswan.d/charon-logging.conf"
subPath: "charon-logging.conf"
readOnly: true
- name: config
mountPath: "/etc/swanctl/swanctl.conf"
subPath: "swanctl.conf"
readOnly: true
- name: cilium-setup
image: {{ .Values.image | quote }}
command: ["sh", "/scripts/sidecar.sh"]
command: ["/bin/sidecar.sh"]
env: {{- include "..commonEnv" . | nindent 10 }}
securityContext:
privileged: true
volumeMounts:
- name: files
mountPath: "/scripts"
readOnly: true
volumes:
- name: files
configMap:
name: {{ include "..fullname" . }}-strongswan
- name: config
secret:
secretName: {{ include "..fullname" . }}-strongswan
3 changes: 1 addition & 2 deletions dev-docs/howto/vpn/helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ ipsec:
# Address of the peer's gateway router.
peer: ""

# required tools: sh nsenter ip pidof jq kubectl charon
image: "nixery.dev/shell/util-linux/iproute2/procps/jq/kubernetes/strongswan"
image: "ghcr.io/edgelesssys/constellation/vpn@sha256:34e28ced172d04dfdadaadbefb1a53b5857cb24fb24e275fbbc537f3639a789e"
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@

packages.libvirtd_base = callPackage ./nix/container/libvirtd_base.nix { pkgs = pkgsUnstable; pkgsLinux = import nixpkgsUnstable { system = "x86_64-linux"; }; };

packages.vpn = callPackage ./nix/container/vpn/vpn.nix { pkgs = pkgsUnstable; pkgsLinux = import nixpkgsUnstable { system = "x86_64-linux"; }; };

packages.awscli2 = pkgsUnstable.awscli2;

packages.bazel_6 = pkgsUnstable.bazel_6;
Expand Down
11 changes: 11 additions & 0 deletions nix/container/vpn/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_oci//oci:defs.bzl", "oci_image")

oci_image(
name = "vpn",
base = "@distroless_static_linux_amd64",
entrypoint = ["/bin/sh"],
tars = [
"@vpn_oci_image//:layer.tar",
],
visibility = ["//visibility:public"],
)
2 changes: 2 additions & 0 deletions ...wto/vpn/helm/files/operator/entrypoint.sh → nix/container/vpn/operator.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/sh

set -u

signaled() {
exit 143
}
Expand Down
File renamed without changes.
7 changes: 6 additions & 1 deletion ...o/vpn/helm/files/strongswan/entrypoint.sh → nix/container/vpn/strongswan.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/sh

# The charon binary is not included in the PATH generated by nixery.dev, find it manually.
set -eux

mkdir -p /var/run
export SWANCTL_DIR=/etc/swanctl

# The charon binary is not included in the PATH generated by writeShellCommand, find it manually.
charon="$(dirname "$(readlink -f "$(command -v charon-systemd)")")/../libexec/ipsec/charon"

"${charon}" &
Expand Down
82 changes: 82 additions & 0 deletions nix/container/vpn/vpn.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{ pkgs
, pkgsLinux
, stdenv
}:
let
passwd = pkgs.writeTextDir "etc/passwd" ''
root:x:0:0:root:/root:/bin/sh
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
'';
group = pkgs.writeTextDir "etc/group" ''
root:x:0:
nobody:x:65534:
'';

strongswanScript = pkgsLinux.writeShellApplication {
name = "strongswan.sh";
runtimeInputs = with pkgsLinux; [
coreutils
strongswan
];
text = ./strongswan.sh;
};

sidecarScript = pkgsLinux.writeShellApplication {
name = "sidecar.sh";
runtimeInputs = with pkgsLinux; [
coreutils
iproute2
jq
util-linux
procps
];
text = ./sidecar.sh;
};

operatorScript = pkgsLinux.writeShellApplication {
name = "operator.sh";
runtimeInputs = with pkgsLinux; [
coreutils
kubernetes
jq
];
text = ./operator.sh;
};

image = pkgs.dockerTools.buildImage {
name = "ghcr.io/edgelesssys/constellation/vpn";
copyToRoot = with pkgsLinux.dockerTools; [
passwd
group
strongswanScript
sidecarScript
operatorScript
binSh
];
config = {
Cmd = [ "/bin/entrypoint.sh" ];
};
};

in

stdenv.mkDerivation {
name = "image";

src = image;

buildInputs = with pkgs; [ gnutar jq ];


installPhase = ''
mkdir -p "$out/tmp"
pushd "$out/tmp"
tar -xf ${image}
layer="$(jq -r '.[0].Layers[0]' <manifest.json)"
chmod -R u+w "."
mv "$layer" "$out/layer.tar"
popd
rm -rf -- "$out/tmp"
'';

}

0 comments on commit 473001b

Please sign in to comment.