Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix loading private key from inventory #5044

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion providers/github/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func connectionOptionsFromConfigOptions(conf *inventory.Config) (opts githubConn
switch cred.Type {

case vault.CredentialType_private_key:
if opts.AppPrivateKeyFile != "" {
if opts.AppPrivateKeyFile == "" {
opts.AppPrivateKey = cred.Secret
}

Expand Down
34 changes: 33 additions & 1 deletion providers/github/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ package connection

import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"os"
"testing"

Expand All @@ -20,7 +24,35 @@ func TestGithubNoConnection(t *testing.T) {
require.Error(t, err)
}

func TestGithubValidConnection(t *testing.T) {
func TestGithubValidConnection_Private_Key(t *testing.T) {
// Generate a new RSA private key
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) // 2048-bit key size
require.NoError(t, err)
privateKeyDER := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyPEM := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: privateKeyDER,
}
pemData := pem.EncodeToMemory(privateKeyPEM)

_, err = NewGithubConnection(0, &inventory.Asset{
Connections: []*inventory.Config{{
Options: map[string]string{
OPTION_APP_ID: "123",
OPTION_APP_INSTALLATION_ID: "890",
},
Credentials: []*vault.Credential{{
Type: vault.CredentialType_private_key,
Secret: pemData,
},
},
},
},
})
require.NoError(t, err)
}

func TestGithubValidConnection_Password(t *testing.T) {
_, err := NewGithubConnection(0, &inventory.Asset{
Connections: []*inventory.Config{{
Credentials: []*vault.Credential{{
Expand Down
60 changes: 60 additions & 0 deletions test/providers/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package providers

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v11/test"
)

func TestGithubScanFlags(t *testing.T) {
once.Do(setup)

t.Run("github scan WITHOUT flags", func(t *testing.T) {
// NOTE this will fail but, it will load the flags and fail with the right message
r := test.NewCliTestRunner("./cnquery", "scan", "github", "repo", "foo")
err := r.Run()
require.NoError(t, err)
assert.Equal(t, 0, r.ExitCode())
assert.NotNil(t, r.Stdout())
assert.NotNil(t, r.Stderr())

assert.Contains(t, string(r.Stderr()),
"a valid GitHub authentication is required",
)
})
t.Run("github scan WITH flags but missing app auth key", func(t *testing.T) {
// NOTE this will fail but, it will load the flags and fail with the right message
r := test.NewCliTestRunner("./cnquery", "scan", "github", "repo", "foo",
"--app-id", "123", "--app-installation-id", "456",
)
err := r.Run()
require.NoError(t, err)
assert.Equal(t, 1, r.ExitCode())
assert.NotNil(t, r.Stdout())
assert.NotNil(t, r.Stderr())

assert.Contains(t, string(r.Stderr()),
"could not parse private key", // expected! it means we loaded the flags
)
})
t.Run("github scan WITH all required flags for app auth", func(t *testing.T) {
// NOTE this will fail but, it will load the flags and fail with the right message
r := test.NewCliTestRunner("./cnquery", "scan", "github", "repo", "foo",
"--app-id", "123", "--app-installation-id", "456", "--app-private-key", "private-key.pem",
)
err := r.Run()
require.NoError(t, err)
assert.Equal(t, 1, r.ExitCode())
assert.NotNil(t, r.Stdout())
assert.NotNil(t, r.Stderr())

assert.Contains(t, string(r.Stderr()),
"could not read private key", // expected! it means we loaded the flags
)
})
}
2 changes: 1 addition & 1 deletion test/providers/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var once sync.Once

// setup builds cnquery locally
func setup() {
// build cnspec
// build cnquery
if err := exec.Command("go", "build", "../../apps/cnquery/cnquery.go").Run(); err != nil {
log.Fatalf("building cnquery: %v", err)
}
Expand Down
Loading