Skip to content

Commit

Permalink
🐛 fix github provider load secret from inventory
Browse files Browse the repository at this point in the history
Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Dec 18, 2024
1 parent f19188a commit 8b2fc8c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
5 changes: 4 additions & 1 deletion providers/github/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func connectionOptionsFromConfigOptions(conf *inventory.Config) (opts githubConn
}

case vault.CredentialType_password:
if opts.Token != "" {
if opts.Token == "" {
opts.Token = string(cred.Secret)
}
}
Expand All @@ -94,6 +94,9 @@ func connectionOptionsFromConfigOptions(conf *inventory.Config) (opts githubConn
}

func NewGithubConnection(id uint32, asset *inventory.Asset) (*GithubConnection, error) {
if asset.Connections == nil {
return nil, errors.New("no connection details for the asset")
}
connectionOpts := connectionOptionsFromConfigOptions(asset.Connections[0])

var client *github.Client
Expand Down
31 changes: 23 additions & 8 deletions providers/github/connection/connection_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

//go:build debugtest
// +build debugtest

package github
package connection

import (
"context"
Expand All @@ -14,16 +11,34 @@ import (
"github.com/google/go-github/v67/github"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/vault"
)

func TestGithub(t *testing.T) {
func TestGithubNoConnection(t *testing.T) {
os.Setenv("GITHUB_TOKEN", "")
p, err := NewGithubConnection(0, &inventory.Config{})
require.NoError(t, err)
_, err := NewGithubConnection(0, &inventory.Asset{})
require.Error(t, err)
}

client := p.Client()
func TestGithubValidConnection(t *testing.T) {
_, err := NewGithubConnection(0, &inventory.Asset{
Connections: []*inventory.Config{{
Credentials: []*vault.Credential{{
Type: vault.CredentialType_password,
Secret: []byte("super_secret"),
},
},
},
},
})
require.NoError(t, err)
}

func TestGithubNeedsFix(t *testing.T) {
t.Skip()
p, err := NewGithubConnection(0, &inventory.Asset{})
orgName := "mondoohq"
client := p.Client()
ctx := context.Background()
org, _, err := client.Organizations.Get(ctx, orgName)
require.NoError(t, err)
Expand Down

0 comments on commit 8b2fc8c

Please sign in to comment.