Skip to content

Commit 0df2472

Browse files
committed
Merge branch 'tg/hotreload-token-secret-key' into tg/support-new-bm-provider-id
2 parents a5ff17e + aa02635 commit 0df2472

3 files changed

Lines changed: 86 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ See [CAPH docs](https://syself.com/docs/caph/topics/baremetal/creating-workload-
6969

7070
## Usage
7171

72+
We recommend to mount the secret `hetzner` as volume and make it available for the container as `/etc/hetzner-secret`.
73+
Then the credentials are automatically reloaded, when the secret changes.
74+
When you use hot-reloading, the secret keys must be named `hcloud` (or `token`, for upstream hcloud-ccm compatibility), `robot-user`, and `robot-password`.
75+
You see an example in the [ccm helm chart](https://github.com/syself/charts/tree/main/charts/ccm-hetzner)
76+
7277
We recommend to mount the secret `hetzner` as volume and make it available for the container as
7378
`/etc/hetzner-secret`. Then the credentials are automatically reloaded, when the secret changes.
7479
When you use the hot-reloading, be sure that the keys in the secret use these names: "hcloud" (for

internal/credentials/hotreload.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ func GetInitialHcloudCredentialsFromDirectory(credentialsDir string) (string, er
240240
func readHcloudCredentials(credentialsDir string) (string, error) {
241241
var allErrors []error
242242
for _, key := range []string{"hcloud", "token"} {
243-
// upstream hcloud ccm expects by default the key "token" in the secret. To ease migration
244-
// (back and forward), we support that, too.
243+
// The upstream hcloud-ccm uses "token" as key name in the mounted secret.
244+
// To ease migration between both CCMs, we support both key names.
245245
hcloudTokenFile := filepath.Join(credentialsDir, key)
246246
data, err := os.ReadFile(hcloudTokenFile)
247247
if err != nil {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package credentials
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestReadHcloudCredentials(t *testing.T) {
11+
t.Run("reads hcloud key", func(t *testing.T) {
12+
dir := t.TempDir()
13+
err := os.WriteFile(filepath.Join(dir, "hcloud"), []byte(" token-from-hcloud \n"), 0o600)
14+
if err != nil {
15+
t.Fatalf("write hcloud token: %v", err)
16+
}
17+
18+
token, err := readHcloudCredentials(dir)
19+
if err != nil {
20+
t.Fatalf("readHcloudCredentials() error = %v", err)
21+
}
22+
if token != "token-from-hcloud" {
23+
t.Fatalf("readHcloudCredentials() token = %q, want %q", token, "token-from-hcloud")
24+
}
25+
})
26+
27+
t.Run("falls back to token key", func(t *testing.T) {
28+
dir := t.TempDir()
29+
err := os.WriteFile(filepath.Join(dir, "token"), []byte("token-from-token-key"), 0o600)
30+
if err != nil {
31+
t.Fatalf("write token key: %v", err)
32+
}
33+
34+
token, err := readHcloudCredentials(dir)
35+
if err != nil {
36+
t.Fatalf("readHcloudCredentials() error = %v", err)
37+
}
38+
if token != "token-from-token-key" {
39+
t.Fatalf("readHcloudCredentials() token = %q, want %q", token, "token-from-token-key")
40+
}
41+
})
42+
43+
t.Run("prefers hcloud key over token key", func(t *testing.T) {
44+
dir := t.TempDir()
45+
err := os.WriteFile(filepath.Join(dir, "hcloud"), []byte("first-token"), 0o600)
46+
if err != nil {
47+
t.Fatalf("write hcloud token: %v", err)
48+
}
49+
err = os.WriteFile(filepath.Join(dir, "token"), []byte("second-token"), 0o600)
50+
if err != nil {
51+
t.Fatalf("write token key: %v", err)
52+
}
53+
54+
token, err := readHcloudCredentials(dir)
55+
if err != nil {
56+
t.Fatalf("readHcloudCredentials() error = %v", err)
57+
}
58+
if token != "first-token" {
59+
t.Fatalf("readHcloudCredentials() token = %q, want %q", token, "first-token")
60+
}
61+
})
62+
63+
t.Run("returns error if no key exists", func(t *testing.T) {
64+
dir := t.TempDir()
65+
66+
_, err := readHcloudCredentials(dir)
67+
if err == nil {
68+
t.Fatal("readHcloudCredentials() error = nil, want non-nil")
69+
}
70+
71+
errText := err.Error()
72+
if !strings.Contains(errText, filepath.Join(dir, "hcloud")) {
73+
t.Fatalf("error does not mention hcloud key path: %q", errText)
74+
}
75+
if !strings.Contains(errText, filepath.Join(dir, "token")) {
76+
t.Fatalf("error does not mention token key path: %q", errText)
77+
}
78+
})
79+
}

0 commit comments

Comments
 (0)