Skip to content

Commit 433fe7a

Browse files
authored
Merge pull request #58 from pieterlange/fix/check-hostkeys
Use secure defaults (check hostkeys)
2 parents 1e31f00 + 899cf59 commit 433fe7a

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

cmd/git-sync/main.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ var flPassword = flag.String("password", envString("GIT_SYNC_PASSWORD", ""),
6666

6767
var flSSH = flag.Bool("ssh", envBool("GIT_SYNC_SSH", false),
6868
"use SSH for git operations")
69+
var flSSHKnownHosts = flag.Bool("ssh-known-hosts", envBool("GIT_KNOWN_HOSTS", false),
70+
"enable SSH known_hosts verification")
6971

7072
var log = newLoggerOrDie()
7173

@@ -152,7 +154,7 @@ func main() {
152154
}
153155

154156
if *flSSH {
155-
if err := setupGitSSH(); err != nil {
157+
if err := setupGitSSH(*flSSHKnownHosts); err != nil {
156158
fmt.Fprintf(os.Stderr, "ERROR: can't configure SSH: %v\n", err)
157159
os.Exit(1)
158160
}
@@ -468,10 +470,11 @@ func setupGitAuth(username, password, gitURL string) error {
468470
return nil
469471
}
470472

471-
func setupGitSSH() error {
473+
func setupGitSSH(setupKnownHosts bool) error {
472474
log.V(1).Infof("setting up git SSH credentials")
473475

474476
var pathToSSHSecret = "/etc/git-secret/ssh"
477+
var pathToSSHKnownHosts = "/etc/git-secret/known_hosts"
475478

476479
fileInfo, err := os.Stat(pathToSSHSecret)
477480
if err != nil {
@@ -482,8 +485,18 @@ func setupGitSSH() error {
482485
return fmt.Errorf("Permissions %s for SSH key are too open. It is recommended to mount secret volume with `defaultMode: 256` (decimal number for octal 0400).", fileInfo.Mode())
483486
}
484487

488+
if setupKnownHosts {
489+
_, err := os.Stat(pathToSSHKnownHosts)
490+
if err != nil {
491+
return fmt.Errorf("error: could not find SSH known_hosts file: %v", err)
492+
}
493+
494+
err = os.Setenv("GIT_SSH_COMMAND", fmt.Sprintf("ssh -q -o UserKnownHostsFile=%s -i %s", pathToSSHKnownHosts, pathToSSHSecret))
495+
} else {
496+
err = os.Setenv("GIT_SSH_COMMAND", fmt.Sprintf("ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i %s", pathToSSHSecret))
497+
}
498+
485499
//set env variable GIT_SSH_COMMAND to force git use customized ssh command
486-
err = os.Setenv("GIT_SSH_COMMAND", fmt.Sprintf("ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i %s", pathToSSHSecret))
487500
if err != nil {
488501
return fmt.Errorf("Failed to set the GIT_SSH_COMMAND env var: %v", err)
489502
}

docs/ssh.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ Git-sync supports using the SSH protocol for pulling git content.
66
Create a Secret to store your SSH private key, with the Secret keyed as "ssh". This can be done one of two ways:
77

88
***Method 1:***
9+
Obtain the host keys for your git server:
10+
11+
```
12+
ssh-keyscan $YOUR_GIT_HOST > /tmp/known_hosts
13+
```
914

1015
Use the ``kubectl create secret`` command and point to the file on your filesystem that stores the key. Ensure that the file is mapped to "ssh" as shown (the file can be located anywhere).
16+
1117
```
12-
kubectl create secret generic git-creds --from-file=ssh=~/.ssh/id_rsa
18+
kubectl create secret generic git-creds --from-file=ssh=~/.ssh/id_rsa --from-file=known_hosts=/tmp/known_hosts
1319
```
1420

1521
***Method 2:***
1622

17-
Write a config file for a Secret that holds your SSH private key, with the key (pasted as plaintext) mapped to the "ssh" field.
23+
Write a config file for a Secret that holds your SSH private key, with the key (pasted in base64 encoded plaintext) mapped to the "ssh" field.
1824
```
1925
{
2026
"kind": "Secret",
@@ -23,7 +29,8 @@ Write a config file for a Secret that holds your SSH private key, with the key (
2329
"name": "git-creds"
2430
},
2531
"data": {
26-
"ssh": <private-key>
32+
"ssh": <base64 encoded private-key>
33+
"known_hosts": <base64 encoded known_hosts>
2734
}
2835
```
2936

@@ -32,6 +39,8 @@ Create the Secret using ``kubectl create -f``.
3239
kubectl create -f /path/to/secret-config.json
3340
```
3441

42+
Invoke the `git-sync` binary with the `-ssh-known-hosts` parameter to enforce `known_hosts` checking. This will be enabled by default in a future release.
43+
3544
## Step 2: Configure Pod/Deployment Volume
3645

3746
In your Pod or Deployment configuration, specify a Volume for mounting the Secret. Ensure that secretName matches the name you used when creating the Secret (e.g. "git-creds" used in both above examples).

0 commit comments

Comments
 (0)