Skip to content

Commit ddd4334

Browse files
authored
Merge pull request #195 from docker/fix/linux-keychain-tests
fix: linux keychain tests
2 parents 6308082 + a277046 commit ddd4334

9 files changed

Lines changed: 146 additions & 36 deletions

File tree

.github/workflows/keychain.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ jobs:
2020
matrix:
2121
subtest:
2222
- fedora-43-gnome-keyring
23-
- fedora-43-kdewallet
2423
- ubuntu-24-gnome-keyring
25-
- ubuntu-24-kdewallet
24+
# disabled kdewallet tests since it prompts for a password in a
25+
# headless environment... need to still fix this
26+
# - fedora-43-kdewallet
27+
# - ubuntu-24-kdewallet
2628
steps:
2729
- name: Checkout
2830
uses: actions/checkout@v4
@@ -39,7 +41,7 @@ jobs:
3941
endpoint: "docker/secrets-engine"
4042
install: true
4143
- name: Test
42-
run: DOCKER_TARGET=${{ matrix.subtest }} make keychain-linux-unit-tests
44+
run: DOCKER_TARGET=${{ matrix.subtest }} make keychain-linux-ci-unit-tests
4345
tests-windows:
4446
permissions:
4547
id-token: write

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ unit-tests:
7070
exit $$err; \
7171
fi
7272

73-
keychain-linux-unit-tests:
73+
keychain-linux-ci-unit-tests:
7474
@docker buildx build $(DOCKER_BUILD_ARGS) --target=$(DOCKER_TARGET) --file store/Dockerfile .
7575

76+
keychain-linux-unit-tests:
77+
docker buildx bake --set '*.args.GO_VERSION=${GO_VERSION}' --file store/docker-bake.hcl
78+
7679
keychain-unit-tests:
7780
CGO_ENABLED=1 go test -v $$(go list ./store/keychain/...)
7881

store/Dockerfile

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ ARG GO_VERSION=latest
33
FROM --platform=${BUILDPLATFORM} golang:${GO_VERSION}-alpine AS go-base
44

55
FROM --platform=${BUILDPLATFORM} fedora:43 AS fedora43
6-
RUN dnf install -y gnome-keyring kf6-kwallet dbus-daemon
6+
RUN dnf install -y gnome-keyring kf6-kwallet dbus-daemon bash
77
COPY --from=go-base /usr/local/go /usr/local/go
88
ENV PATH="/usr/local/go/bin:${PATH}"
99
RUN useradd -ms /bin/bash user
@@ -12,7 +12,7 @@ WORKDIR /app
1212
RUN --mount=type=bind,target=.
1313

1414
FROM --platform=${BUILDPLATFORM} ubuntu:24.04 AS ubuntu24
15-
RUN apt update && apt install -y --no-install-recommends libglib2.0-bin dbus gnome-keyring kwalletmanager
15+
RUN apt update && apt install -y --no-install-recommends libglib2.0-bin dbus gnome-keyring kwalletmanager libqca-qt5-2-plugins bash
1616
COPY --from=go-base /usr/local/go /usr/local/go
1717
ENV PATH="/usr/local/go/bin:${PATH}"
1818
RUN useradd -ms /bin/bash user
@@ -27,8 +27,7 @@ WORKDIR /app
2727
RUN --mount=type=bind,target=. \
2828
--mount=type=cache,target=/go/pkg/mod \
2929
--mount=type=cache,target=/root/.cache/go-build \
30-
/app/store/scripts/gnome-keyring \
31-
go test -v ./store/keychain/...
30+
bash -c "set -euxo pipefail; /app/store/scripts/gnome-keyring"
3231

3332
FROM fedora43 AS fedora-43-kdewallet
3433
ENV CGO_ENABLED=0
@@ -37,8 +36,7 @@ WORKDIR /app
3736
RUN --mount=type=bind,target=. \
3837
--mount=type=cache,target=/go/pkg/mod \
3938
--mount=type=cache,target=/root/.cache/go-build \
40-
/app/store/scripts/kdewallet \
41-
go test -v ./store/keychain/...
39+
bash -c "set -euxo pipefail; /app/store/scripts/kdewallet"
4240

4341
FROM ubuntu24 AS ubuntu-24-gnome-keyring
4442
ENV CGO_ENABLED=0
@@ -47,8 +45,7 @@ WORKDIR /app
4745
RUN --mount=type=bind,target=. \
4846
--mount=type=cache,target=/go/pkg/mod \
4947
--mount=type=cache,target=/root/.cache/go-build \
50-
/app/store/scripts/gnome-keyring \
51-
go test -v ./store/keychain/...
48+
bash -c "set -euxo pipefail; /app/store/scripts/gnome-keyring"
5249

5350
FROM ubuntu24 AS ubuntu-24-kdewallet
5451
ENV CGO_ENABLED=0
@@ -57,5 +54,4 @@ WORKDIR /app
5754
RUN --mount=type=bind,target=. \
5855
--mount=type=cache,target=/go/pkg/mod \
5956
--mount=type=cache,target=/root/.cache/go-build \
60-
/app/store/scripts/kdewallet \
61-
go test -v ./store/keychain/...
57+
bash -c "set -euxo pipefail; /app/store/scripts/kdewallet"

store/docker-bake.hcl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
group "default" {
2+
targets = [
3+
"fedora_43_gnome_keyring",
4+
# disabling kdewallet tests for now, it doesn't work in headless mode
5+
# it just prompts anyway...
6+
# "fedora_43_kdewallet",
7+
# "ubuntu_24_kdewallet",
8+
"ubuntu_24_gnome_keyring"
9+
]
10+
}
11+
12+
variable "GO_VERSION" {
13+
default = "1.24"
14+
}
15+
16+
target "fedora_43_gnome_keyring" {
17+
dockerfile = "store/Dockerfile"
18+
target = "fedora-43-gnome-keyring"
19+
context = "."
20+
args = {
21+
GO_VERSION = GO_VERSION
22+
}
23+
}
24+
25+
target "fedora_43_kdewallet" {
26+
dockerfile = "store/Dockerfile"
27+
target = "fedora-43-kdewallet"
28+
context = "."
29+
args = {
30+
GO_VERSION = GO_VERSION
31+
}
32+
}
33+
34+
target "ubuntu_24_kdewallet" {
35+
dockerfile = "store/Dockerfile"
36+
target = "ubuntu-24-kdewallet"
37+
context = "."
38+
args = {
39+
GO_VERSION = GO_VERSION
40+
}
41+
}
42+
43+
target "ubuntu_24_gnome_keyring" {
44+
dockerfile = "store/Dockerfile"
45+
target = "ubuntu-24-gnome-keyring"
46+
context = "."
47+
args = {
48+
GO_VERSION = GO_VERSION
49+
}
50+
}

store/docs/test.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ enabled to support macOS.
1212
The cross distro linux tests can be run via:
1313

1414
` ` `console
15-
DOCKER_TARGET=ubuntu-24-gnome-keyring make keychain-linux-unit-tests
15+
make keychain-linux-unit-tests
1616
` ` `
1717

18+
This test uses the store/docker-bake.hcl file to parallelize the test across
19+
multiple environments.
20+
1821
For Linux keychain we have four sub-tests:
1922

2023
```mermaid
@@ -30,5 +33,11 @@ flowchart TD
3033
- `fedora-43-gnome-keyring`
3134
- `fedora-43-kdewallet`
3235

36+
To run a targeted test, re-use the CI make target:
37+
38+
```console
39+
DOCKER_TARGET=ubuntu-24-gnome-keyring make keychain-linux-ci-unit-tests
40+
```
41+
3342
This will use `buildkit` to target only the `ubuntu-24-gnome-keyring` label inside
3443
the `store/Dockerfile`.

store/keychain/keychain.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,14 @@ func (k *keychainStore[T]) safelyCleanMetadata(attributes map[string]string) {
9494
keys := slices.Collect(maps.Keys(attributes))
9595
for _, key := range keys {
9696
after, found := strings.CutPrefix(key, "x_")
97+
// this preserves metadata set by the caller.
98+
// we are restoring it by stripping the "x_" prefix.
9799
if found {
98100
attributes[after] = attributes[key]
99-
delete(attributes, key)
100101
}
102+
// delete should always happen since we also want to remove attributes
103+
// there were never prefixed. In this case we are just dropping them
104+
// entirely. e.g. "xdg:scheme" set by the linux keychain internally.
105+
delete(attributes, key)
101106
}
102107
}

store/keychain/keychain_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,22 @@ func TestSafelyCleanMetadata(t *testing.T) {
305305
kc.safelyCleanMetadata(attributes)
306306
assert.Empty(t, attributes)
307307
})
308+
309+
t.Run("underlying store attributes are always removed", func(t *testing.T) {
310+
attributes := map[string]string{
311+
secretIDKey: "username",
312+
serviceGroupKey: "com.test.test",
313+
serviceNameKey: "test",
314+
"x_something": "something",
315+
// xdg:scheme is added by the underlying linux keychain after we
316+
// have prefixed key's with 'x_'
317+
"xdg:scheme": "org.freedesktop.Secret.Generic",
318+
}
319+
kc.safelyCleanMetadata(attributes)
320+
assert.EqualValues(t, map[string]string{
321+
"something": "something",
322+
}, attributes)
323+
})
308324
}
309325

310326
func TestInternalMetadata(t *testing.T) {

store/scripts/gnome-keyring

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
# 1. Check relevant binaries available
44
# 2. Creates the necessary files for the keyring daemon
@@ -8,7 +8,7 @@
88
# 6. Waits for the keyring to be active by polling dbus over `gdbus`
99
# 7. Check if the registered `org.freedesktop.secrets` backend matches what we expect.
1010

11-
set -eux pipefail
11+
set -euxo pipefail
1212

1313
if test -z $(command -v gnome-keyring-daemon); then
1414
echo "gnome-keyring-daemon is not installed"
@@ -20,8 +20,13 @@ if test -z $(command -v dbus-daemon); then
2020
exit 1
2121
fi
2222

23-
mkdir -p ~/.local/share/keyrings
24-
touch ~/.local/share/keyrings/login.keyring
23+
if ! test -d ~/.local/share/keyrings; then
24+
mkdir -p ~/.local/share/keyrings
25+
fi
26+
27+
if ! test -e ~/.local/share/keyrings/login.keyring; then
28+
touch ~/.local/share/keyrings/login.keyring
29+
fi
2530

2631
# create fake passwordless 'login' keyring
2732
echo '[keyring]
@@ -79,7 +84,11 @@ fi
7984

8085
exe=$(readlink -f /proc/$pid/exe)
8186

82-
if [[ "$exe" != *gnome-keyring-daemon* ]]; then
83-
echo "dbus org.freedesktop.secrets is not using gnome-keyring-daemon"
84-
exit 1
87+
if [[ "$exe" == *gnome-keyring-daemon* ]]; then
88+
echo "dbus org.freedesktop.secrets is using gnome-keyring-daemon"
89+
go test -v -count=1 ./store/keychain/...
90+
exit 0
8591
fi
92+
93+
echo "dbus org.freedesktop.secrets is not using gnome-keyring-daemon. Using ${exe}"
94+
exit 1

store/scripts/kdewallet

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/bash
22

33
# 1. Check relevant binaries available
44
# 2. Creates the necessary files for the keyring daemon
@@ -8,7 +8,7 @@
88
# 6. Waits for the keyring to be active by polling dbus over `gdbus`
99
# 7. Check if the registered `org.freedesktop.secrets` backend matches what we expect.
1010

11-
set -eux pipefail
11+
set -euxo pipefail
1212

1313
kwalletd=$(command -v kwalletd5 || command -v kwalletd6)
1414

@@ -22,19 +22,33 @@ if test -z $(command -v dbus-daemon); then
2222
exit 1
2323
fi
2424

25-
mkdir -p ~/.local/share/dbus-1/services
26-
touch ~/.local/share/dbus-1/services/org.freedesktop.secrets.service
25+
if ! test -d ~/.local/share/dbus-1/services; then
26+
mkdir -p ~/.local/share/dbus-1/services
27+
fi
28+
29+
if ! test -e ~/.local/share/dbus-1/services/org.freedesktop.secrets.service; then
30+
touch ~/.local/share/dbus-1/services/org.freedesktop.secrets.service
31+
fi
2732

28-
echo "[D-BUS Service]
33+
echo '[D-BUS Service]
2934
Name=org.freedesktop.secrets
30-
Exec=${kwalletd}" > ~/.local/share/dbus-1/services/org.freedesktop.secrets.service
35+
Exec=${kwalletd}' > ~/.local/share/dbus-1/services/org.freedesktop.secrets.service
3136

3237

33-
mkdir -p ~/.local/share/kwalletd
34-
mkdir ~/.config
38+
if ! test -d ~/.local/share/kwalletd; then
39+
mkdir -p ~/.local/share/kwalletd
40+
fi
3541

36-
echo -e "[Wallet]\nFirst Use=false\nDefault Wallet=kwallet" > ~/.config/kwalletrc
37-
echo -e "[Wallet]\nVersion=1" > ~/.local/share/kwalletd/kwallet.kwl
42+
if ! test -d ~/.config; then
43+
mkdir ~/.config
44+
fi
45+
46+
echo '[Wallet]
47+
First Use=false
48+
Default Wallet=kwallet' > ~/.config/kwalletrc
49+
50+
echo '[Wallet]
51+
Version=1' > ~/.local/share/kwalletd/kwallet.kwl
3852

3953
export QT_QPA_PLATFORM=minimal
4054

@@ -86,7 +100,13 @@ fi
86100

87101
exe=$(readlink -f /proc/$pid/exe)
88102

89-
if [[ "$exe" != "/usr/bin/ksecretd" ]]; then
90-
echo "dbus org.freedesktop.secrets is not using ${kwalletd}"
91-
exit 1
103+
# ksecretd which is part of kwalletd6
104+
# kwalletd5 is part of kwalletd5
105+
if [[ "$exe" == "/usr/bin/ksecretd" || "$exe" == "/usr/bin/kwalletd5" ]]; then
106+
echo "dbus org.freedesktop.secrets is using ${exe}"
107+
go test -v -count=1 ./store/keychain/...
108+
exit 0
92109
fi
110+
111+
echo "dbus org.freedesktop.secrets is not using kwalletd5 or kwalletd6. Using: ${exe}"
112+
exit 1

0 commit comments

Comments
 (0)