Skip to content

Commit

Permalink
Remove ssh-keyscan from git credential initialization
Browse files Browse the repository at this point in the history
Before this change an SSH Git Secret without a known_hosts would cause Tekton
to perform an ssh-keyscan of the remote host to get its public key. This would
happen in the entrypoint of the Step which meant that every Step container would
have to have a copy of ssh-keyscan.

After this change the reponsibility for fetching missing host keys
now falls on git-init, meaning that the Git PipelineResource and git-clone
catalog task will fetch the missing keys at runtime. This is preferable because
we can guarantee that the git-init image has ssh-keyscan in it.
  • Loading branch information
Scott authored and tekton-robot committed Jul 30, 2020
1 parent 3fc7c03 commit f112277
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 99 deletions.
4 changes: 2 additions & 2 deletions docs/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ aggregates them into their respective files in `$HOME`.
ssh-privatekey: <base64 encoded>
# This is non-standard, but its use is encouraged to make this more secure.
# If it is not provided then the git server's public key will be requested
# with `ssh-keyscan` during credential initialization.
# when the repo is first fetched.
known_hosts: <base64 encoded>
```
Expand Down Expand Up @@ -389,7 +389,7 @@ type: kubernetes.io/ssh-auth
data:
ssh-privatekey: <base64 encoded>
# This is non-standard, but its use is encouraged to make this more secure.
# Omitting this results in the use of ssh-keyscan (see below).
# Omitting this results in the server's public key being blindly accepted.
known_hosts: <base64 encoded>
```

Expand Down
16 changes: 10 additions & 6 deletions examples/v1beta1/taskruns/authenticating-git-commands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ metadata:
data:
# This key was generated for this test and isn't used for anything else.
ssh-privatekey: LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUJsd0FBQUFkemMyZ3RjbgpOaEFBQUFBd0VBQVFBQUFZRUF5T1g3ZG5OWlFBZVk4cHNMOXlaUnp3NXNDVG1yWGh6Zld1YTZuZ2VDQ0VRRTY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0Z1QVFJREJBPT0KLS0tLS1FTkQgT1BFTlNTSCBQUklWQVRFIEtFWS0tLS0tCg==
# This known_hosts file doesn't actually get used; we overwrite it with the public key
# of our temporary test git server. But it's required here because otherwise creds-init
# calls ssh-keyscan which in turn tries to reach out over the network, will fail to make
# contact with the localhost SSH server because it isn't running yet, and the TaskRun
# will end in failure.
known_hosts: Cg==
# Note: we intentionally omit a known_hosts entry here. You should include
# one in your own Secrets as a security measure, otherwise the Git PipelineResource
# and git-clone Tasks will blindly accept any public key returned by a repository.
#
# We're able to omit known_hosts here because the file is generated by the
# git server sidecar. The benefit of omitting it here is that it exercises
# a codepath in Tekton that used to fail. In prior versions Tekton would
# run ssh-keyscan if known_hosts was omitted, which would fail for this example
# because the git server sidecar is not up and running at the time the scan
# would have happened.
---
apiVersion: v1
kind: ServiceAccount
Expand Down
50 changes: 50 additions & 0 deletions examples/v1beta1/taskruns/git-ssh-creds-without-known_hosts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: v1
kind: Secret
metadata:
name: git-ssh-without-known-hosts
annotations:
tekton.dev/git-0: github.com
type: kubernetes.io/ssh-auth
data:
# Generated by:
# cat id_rsa | base64 -w 0
# This deploy key has read-only permissions on github.com/knative/build
ssh-privatekey: LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlKSndJQkFBS0NBZ0VBc0YrY3ZIdUlKRjBwNWZNVDBUUVZhMWpTZVBNVlhtUWhGa1F0andsamJFSXVKaVh5CmVrU1I0WUtDbFZIZVo0M2VOa2p5SzFwTDVmVnZnY3J2bkVUMXE5dmFmOThtNzdHWjZUdnJGY0RyZjViZDFIZGUKTU1MTFZRRU8vN1lkcEJFeVJ1UjA4RXRzbWp0RUVaNERVZnBUWGQ5RnAyTU5UWTYwVzFpa0p0Mm53dGhWVlF0OApubFcxTTRvc1d0SHE1OVFDbnhqUkVmNzNhdHdtUTZ6enlXcCtJM3ZBRFRJUmZpTWcvSjc0UDJHO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crLzNwTDhVOEI5SDFXeVBMNnZmVE0zU3FxVk1nTEJjMlBFdEpNelNETkV6bnhjd3YwY2N2czkKeElSZU1FQVUrWnBvd2RpRkNqQStRU3puVDdFY1RSZXR4WmZXMCtlaTVMMHc0cXZObmpxMG13NkZmSTV5amF2ZwpsSU5LZDVBaEx2VjVIM2pHWVBXMk9Pa01obFBoQkNvQitlQWdrem5GeWdPbHV0WmdVVmh3ajBsT2FHcUFwSzQyCkVRZDlGWTZnZ0tSSXZIOFNINGhLWWJhdDIvQjNvNjhDZ2dFQVNnUGZubVdZcXpQTlpwYXh4VFZ3NXl0cXovMGcKUHlhcW41elhDSHFsdDluNkZFYWdRVjE4bjl0bUZzTWtnOFd4MHp1TjY5ajNvcXVDaWVDSkVRTTFDR05GUWg2MQpEV0xrd210R1FPNXZxc0VhcXpLZk9yWktnYTJ5L3BuejY2SkNlUW83UlJEdVVZVHpPY3NpQmtxNmNUNHFLQjY0ClBCVlVhQTROQkVCZlRoc1o5dDhpWHFDQWhUQnVlTDdhQ3pHV0ZjR1k5dndrZkJHNjNacURXbVlqZkZwRGlqSWIKL21ySnJVTTB0a09HcUhESk5aUWhpZXc1VHF6NWp3VElyTDY3YUdZNFpuNEVyODFlb3JOZnpEeDVnak1NYnoweQoyTGZoN1c0MFlWMDA4ZmZJTk5BbXA4VjNqay9qcWFUSFlWYlFQNGxhdTRIWkc5Q2g1Q1VpSmFvN2xBPT0KLS0tLS1FTkQgUlNBIFBSSVZBVEUgS0VZLS0tLS0K
# Note: We are not including a known_hosts entry here. This forces
# the Git PipelineResource to accept github's public key the first
# time it fetches the repository.
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: robot-git-ssh-without-known-hosts
secrets:
- name: git-ssh-without-known-hosts
---
apiVersion: tekton.dev/v1beta1
kind: TaskRun
metadata:
name: git-ssh-creds-without-known-hosts
spec:
serviceAccountName: robot-git-ssh-without-known-hosts
resources:
inputs:
- name: gitssh
resourceSpec:
type: git
params:
- name: revision
value: master
- name: url
value: git@github.com:tektoncd/pipeline.git
taskSpec:
resources:
inputs:
- name: gitssh
type: git
steps:
- name: config
image: ubuntu
script: |
#!/usr/bin/env bash
cat /workspace/gitssh/README.md
52 changes: 12 additions & 40 deletions pkg/credentials/gitcreds/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ limitations under the License.
package gitcreds

import (
"bytes"
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -108,7 +106,9 @@ func (dc *sshGitConfig) Write(directory string) error {
}
configEntry += fmt.Sprintf(` IdentityFile %s
`, e.path(sshDir))
knownHosts = append(knownHosts, e.knownHosts)
if e.knownHosts != "" {
knownHosts = append(knownHosts, e.knownHosts)
}
}
configEntries = append(configEntries, configEntry)
}
Expand All @@ -117,9 +117,12 @@ func (dc *sshGitConfig) Write(directory string) error {
if err := ioutil.WriteFile(configPath, []byte(configContent), 0600); err != nil {
return err
}
knownHostsPath := filepath.Join(sshDir, "known_hosts")
knownHostsContent := strings.Join(knownHosts, "\n")
return ioutil.WriteFile(knownHostsPath, []byte(knownHostsContent), 0600)
if len(knownHosts) > 0 {
knownHostsPath := filepath.Join(sshDir, "known_hosts")
knownHostsContent := strings.Join(knownHosts, "\n")
return ioutil.WriteFile(knownHostsPath, []byte(knownHostsContent), 0600)
}
return nil
}

type sshEntry struct {
Expand All @@ -132,32 +135,6 @@ func (be *sshEntry) path(sshDir string) string {
return filepath.Join(sshDir, "id_"+be.secretName)
}

func sshKeyScan(host, port string) ([]byte, error) {
var c *exec.Cmd
if port == "" {
c = exec.Command("ssh-keyscan", host)
} else {
c = exec.Command("ssh-keyscan", host, "-p", port)
}
var output bytes.Buffer
c.Stdout = &output
c.Stderr = &output
if err := c.Run(); err != nil {
return nil, err
}
return output.Bytes(), nil
}

// sshKeyScanArgs returns the host and optional port for running ssh-keyscan
// given a url.
func sshKeyScanArgs(url string) (string, string) {
host, port, err := net.SplitHostPort(url)
if err != nil {
return url, ""
}
return host, port
}

func (be *sshEntry) Write(sshDir string) error {
return ioutil.WriteFile(be.path(sshDir), []byte(be.privateKey), 0600)
}
Expand All @@ -171,15 +148,10 @@ func newSSHEntry(url, secretName string) (*sshEntry, error) {
}
privateKey := string(pk)

kh, err := ioutil.ReadFile(filepath.Join(secretPath, sshKnownHosts))
if err != nil {
host, port := sshKeyScanArgs(url)
kh, err = sshKeyScan(host, port)
if err != nil {
return nil, fmt.Errorf("ssh-keyscan error: %w", err)
}
knownHosts := ""
if kh, err := ioutil.ReadFile(filepath.Join(secretPath, sshKnownHosts)); err == nil {
knownHosts = string(kh)
}
knownHosts := string(kh)

return &sshEntry{
secretName: secretName,
Expand Down
51 changes: 0 additions & 51 deletions pkg/credentials/gitcreds/ssh_test.go

This file was deleted.

35 changes: 35 additions & 0 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"

homedir "github.com/mitchellh/go-homedir"
"go.uber.org/zap"
)

const (
sshKnownHostsUserPath = ".ssh/known_hosts"
sshMissingKnownHostsSSHCommand = "ssh -o StrictHostKeyChecking=accept-new"
)

func run(logger *zap.SugaredLogger, dir string, args ...string) (string, error) {
c := exec.Command("git", args...)
var output bytes.Buffer
Expand Down Expand Up @@ -78,6 +84,18 @@ func Fetch(logger *zap.SugaredLogger, spec FetchSpec) error {
if _, err := run(logger, "", "remote", "add", "origin", trimmedURL); err != nil {
return err
}

hasKnownHosts, err := userHasKnownHostsFile(logger)
if err != nil {
return fmt.Errorf("error checking for known_hosts file: %w", err)
}
if !hasKnownHosts {
if _, err := run(logger, "", "config", "--global", "core.sshCommand", sshMissingKnownHostsSSHCommand); err != nil {
err = fmt.Errorf("error disabling strict host key checking: %w", err)
logger.Warnf(err.Error())
return err
}
}
if _, err := run(logger, "", "config", "--global", "http.sslVerify", strconv.FormatBool(spec.SSLVerify)); err != nil {
logger.Warnf("Failed to set http.sslVerify in git config: %s", err)
return err
Expand Down Expand Up @@ -209,3 +227,20 @@ func ensureHomeEnv(logger *zap.SugaredLogger) error {
}
return nil
}

func userHasKnownHostsFile(logger *zap.SugaredLogger) (bool, error) {
homepath, err := homedir.Dir()
if err != nil {
logger.Errorf("Unexpected error: getting the user home directory: %v", err)
return false, err
}
f, err := os.Open(filepath.Join(homepath, sshKnownHostsUserPath))
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
f.Close()
return true, nil
}

0 comments on commit f112277

Please sign in to comment.