Skip to content

Commit

Permalink
🧹 add tests for config loading (#221)
Browse files Browse the repository at this point in the history
- add tests for config parser
- extract common config into a separate struct
- return nil for service accounts if no data was defined in config

Signed-off-by: Christoph Hartmann <[email protected]>

Signed-off-by: Christoph Hartmann <[email protected]>
  • Loading branch information
chris-rock authored Oct 8, 2022
1 parent 861b777 commit 3e8cd8f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
22 changes: 17 additions & 5 deletions apps/cnquery/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ func ReadConfig() (*CliConfig, error) {
}

type CliConfig struct {
// inherit common config
CommonCliConfig `mapstructure:",squash"`
}

type CommonCliConfig struct {
// client identifier
AgentMrn string `json:"agent_mrn,omitempty" mapstructure:"agent_mrn"`

// service account credentials
ServiceAccountMrn string `json:"mrn,omitempty" mapstructure:"mrn"`
ParentMrn string `json:"space_mrn,omitempty" mapstructure:"parent_mrn"`
ParentMrn string `json:"parent_mrn,omitempty" mapstructure:"parent_mrn"`
SpaceMrn string `json:"space_mrn,omitempty" mapstructure:"space_mrn"`
PrivateKey string `json:"private_key,omitempty" mapstructure:"private_key"`
Certificate string `json:"certificate,omitempty" mapstructure:"certificate"`
Expand All @@ -40,7 +45,7 @@ type CliConfig struct {
Labels map[string]string `json:"labels,omitempty" mapstructure:"labels"`
}

func (c *CliConfig) GetFeatures() cnquery.Features {
func (c *CommonCliConfig) GetFeatures() cnquery.Features {
bitSet := make([]bool, 256)
flags := []byte{}

Expand All @@ -66,7 +71,14 @@ func (c *CliConfig) GetFeatures() cnquery.Features {
return flags
}

func (v *CliConfig) GetServiceCredential() *upstream.ServiceAccountCredentials {
// GetServiceCredential returns the service credential that is defined in the config.
// If no service credential is defined, it will return an nil.
func (v *CommonCliConfig) GetServiceCredential() *upstream.ServiceAccountCredentials {
// return nil when no service account is defined
if v.ServiceAccountMrn == "" && v.PrivateKey == "" && v.Certificate == "" {
return nil
}

return &upstream.ServiceAccountCredentials{
Mrn: v.ServiceAccountMrn,
ParentMrn: v.GetParentMrn(),
Expand All @@ -76,7 +88,7 @@ func (v *CliConfig) GetServiceCredential() *upstream.ServiceAccountCredentials {
}
}

func (o *CliConfig) GetParentMrn() string {
func (o *CommonCliConfig) GetParentMrn() string {
parent := o.ParentMrn

// fallback to old space_mrn config
Expand All @@ -87,7 +99,7 @@ func (o *CliConfig) GetParentMrn() string {
return parent
}

func (o *CliConfig) UpstreamApiEndpoint() string {
func (o *CommonCliConfig) UpstreamApiEndpoint() string {
apiEndpoint := o.APIEndpoint

// fallback to default api if nothing was set
Expand Down
40 changes: 40 additions & 0 deletions apps/cnquery/cmd/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"

"github.com/spf13/viper"
)

func TestConfigParsing(t *testing.T) {
data := `
agent_mrn: //agents.api.mondoo.app/spaces/musing-saha-952142/agents/1zDY7auR20SgrFfiGUT5qZWx6mE
api_endpoint: https://us.api.mondoo.com
certificate: |
-----BEGIN CERTIFICATE-----
MIICV .. fis=
-----END CERTIFICATE-----
mrn: //agents.api.mondoo.app/spaces/musing-saha-952142/serviceaccounts/1zDY7cJ7bA84JxxNBWDxBdui2xE
private_key: |
-----BEGIN PRIVATE KEY-----
MIG2AgE....C0Dvs=
-----END PRIVATE KEY-----
space_mrn: //captain.api.mondoo.app/spaces/musing-saha-952142
`

viper.SetConfigType("yaml")
viper.ReadConfig(strings.NewReader(data))

cfg, err := ReadConfig()
require.NoError(t, err)
assert.Equal(t, "//agents.api.mondoo.app/spaces/musing-saha-952142/agents/1zDY7auR20SgrFfiGUT5qZWx6mE", cfg.AgentMrn)
assert.Equal(t, "//agents.api.mondoo.app/spaces/musing-saha-952142/serviceaccounts/1zDY7cJ7bA84JxxNBWDxBdui2xE", cfg.ServiceAccountMrn)
assert.Equal(t, "-----BEGIN PRIVATE KEY-----\nMIG2AgE....C0Dvs=\n-----END PRIVATE KEY-----\n", cfg.PrivateKey)
assert.Equal(t, "-----BEGIN CERTIFICATE-----\nMIICV .. fis=\n-----END CERTIFICATE-----\n", cfg.Certificate)
}

0 comments on commit 3e8cd8f

Please sign in to comment.