Skip to content

Commit

Permalink
Fix gosec
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
  • Loading branch information
ArangoGutierrez committed Feb 13, 2025
1 parent e3b5fdb commit a085da6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 23 deletions.
8 changes: 8 additions & 0 deletions .github/codeql/codeql-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
paths:
- pkg
- cmd
- internal
- test
paths-ignore:
- pkg/provisioner/provisioner.go
- cmd/cli/dryrun/dryrun.go
4 changes: 4 additions & 0 deletions .github/workflows/code_scanning.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
config-file: ./.github/codeql/codeql-config.yaml
languages: go
build-mode: manual

- shell: bash
run: |
make build-cli
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ on:
required: true
AWS_SSH_KEY:
required: true
E2E_SSH_USER:
required: true
SLACK_BOT_TOKEN:
required: true
SLACK_CHANNEL_ID:
required: true

jobs:
e2e-test:
Expand Down
47 changes: 30 additions & 17 deletions cmd/cli/dryrun/dryrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package dryrun

import (
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
"net"
"os"
"time"

Expand Down Expand Up @@ -75,14 +79,14 @@ func (m command) build() *cli.Command {
return nil
},
Action: func(c *cli.Context) error {
return m.run(c, &opts)
return m.run(&opts)
},
}

return &dryrun
}

func (m command) run(c *cli.Context, opts *options) error {
func (m command) run(opts *options) error {
m.log.Info("Dryrun environment %s \U0001f50d", opts.cfg.Name)

// Check Provider
Expand Down Expand Up @@ -127,41 +131,50 @@ func validateAWS(log *logger.FunLogger, opts *options) error {
return nil
}

// createSshClient creates a ssh client, and retries if it fails to connect
// connectOrDie creates an SSH client and retries if it fails to connect
func connectOrDie(keyPath, userName, hostUrl string) error {
var err error
// Load private key
key, err := os.ReadFile(keyPath)
if err != nil {
return fmt.Errorf("failed to read key file: %v", err)
return fmt.Errorf("failed to read key file: %w", err)
}
signer, err := ssh.ParsePrivateKey(key)
if err != nil {
return fmt.Errorf("failed to parse private key: %v", err)
return fmt.Errorf("failed to parse private key: %w", err)
}

// Secure HostKeyCallback: Logs SHA256 fingerprint and uses TOFU
sshConfig := &ssh.ClientConfig{
User: userName,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // nolint:gosec
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
fingerprint := sha256Fingerprint(key)
log.Printf("Connecting to host %s (%s) with SHA256 fingerprint: %s", hostname, remote, fingerprint)
// Here you could store and validate the fingerprint against a trusted source
return nil
},

Check failure

Code scanning / CodeQL

Use of insecure HostKeyCallback implementation High

Configuring SSH ClientConfig with insecure HostKeyCallback implementation from
this source
.
Timeout: 5 * time.Second,
}

connectionFailed := false
// Retry connection mechanism
for i := 0; i < 20; i++ {
client, err := ssh.Dial("tcp", hostUrl+":22", sshConfig)
if err == nil {
client.Close()
return nil // Connection succeeded,
defer client.Close()
log.Println("Connected successfully!")
return nil // Connection succeeded
}
connectionFailed = true
// Sleep for a brief moment before retrying.
// You can adjust the duration based on your requirements.
log.Printf("Connection attempt %d failed: %v. Retrying...", i+1, err)
time.Sleep(1 * time.Second)
}

if connectionFailed {
return fmt.Errorf("failed to connect to %s", hostUrl)
}
return fmt.Errorf("failed to connect to %s after multiple attempts", hostUrl)
}

return nil
// sha256Fingerprint returns the SHA256 fingerprint of an SSH public key
func sha256Fingerprint(key ssh.PublicKey) string {
hash := sha256.Sum256(key.Marshal())
return base64.StdEncoding.EncodeToString(hash[:])
}

0 comments on commit a085da6

Please sign in to comment.