Skip to content

Commit

Permalink
add agent info in upstream requests
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev committed Mar 8, 2024
1 parent 50e4098 commit a8e0e53
Show file tree
Hide file tree
Showing 28 changed files with 100 additions and 73 deletions.
3 changes: 2 additions & 1 deletion apps/cnquery/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
cnquery_providers "go.mondoo.com/cnquery/v10/providers"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/sysinfo"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/upstream"
rangerUtils "go.mondoo.com/cnquery/v10/utils/ranger"
"go.mondoo.com/ranger-rpc"
"go.mondoo.com/ranger-rpc/codes"
"go.mondoo.com/ranger-rpc/plugins/authentication/statictoken"
Expand Down Expand Up @@ -72,7 +73,7 @@ func register(token string, annotations map[string]string, timer int, splay int)
if err != nil {
return cli_errors.NewCommandError(errors.Wrap(err, "could not gather client information"), 1)
}
defaultPlugins := defaultRangerPlugins(sysInfo, cnquery.DefaultFeatures)
defaultPlugins := rangerUtils.DefaultRangerPlugins(cnquery.DefaultFeatures)

apiEndpoint := viper.GetString("api_endpoint")
token = strings.TrimSpace(token)
Expand Down
10 changes: 2 additions & 8 deletions apps/cnquery/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"go.mondoo.com/cnquery/v10/cli/config"
cli_errors "go.mondoo.com/cnquery/v10/cli/errors"
cnquery_providers "go.mondoo.com/cnquery/v10/providers"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/sysinfo"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/upstream"
rangerUtils "go.mondoo.com/cnquery/v10/utils/ranger"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -48,19 +48,13 @@ ensure the credentials cannot be used in the future.
// print the used config to the user
config.DisplayUsedConfig()

// determine information about the client
sysInfo, err := sysinfo.Get()
if err != nil {
return errors.Wrap(err, "could not gather client information")
}

// check valid client authentication
serviceAccount := opts.GetServiceCredential()
if serviceAccount == nil {
return cli_errors.NewCommandError(errors.Wrap(err, "could not initialize client authentication"), ConfigurationErrorCode)
}

plugins := defaultRangerPlugins(sysInfo, opts.GetFeatures())
plugins := rangerUtils.DefaultRangerPlugins(opts.GetFeatures())
certAuth, err := upstream.NewServiceAccountRangerPlugin(serviceAccount)
if err != nil {
log.Error().Err(err).Msg("could not initialize client authentication")
Expand Down
37 changes: 0 additions & 37 deletions apps/cnquery/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package cmd

import (
"net/http"
"os"
"regexp"
"strings"
Expand All @@ -14,15 +13,11 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/viper"
"go.mondoo.com/cnquery/v10"
"go.mondoo.com/cnquery/v10/cli/config"
cli_errors "go.mondoo.com/cnquery/v10/cli/errors"
"go.mondoo.com/cnquery/v10/cli/providers"
"go.mondoo.com/cnquery/v10/cli/theme"
"go.mondoo.com/cnquery/v10/logger"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/sysinfo"
"go.mondoo.com/ranger-rpc"
"go.mondoo.com/ranger-rpc/plugins/scope"
)

const (
Expand Down Expand Up @@ -138,38 +133,6 @@ func initLogger() {
logger.Set(level)
}

func defaultRangerPlugins(sysInfo *sysinfo.SystemInfo, features cnquery.Features) []ranger.ClientPlugin {
plugins := []ranger.ClientPlugin{}
plugins = append(plugins, scope.NewRequestIDRangerPlugin())
plugins = append(plugins, sysInfoHeader(sysInfo, features))
return plugins
}

func sysInfoHeader(sysInfo *sysinfo.SystemInfo, features cnquery.Features) ranger.ClientPlugin {
const (
HttpHeaderUserAgent = "User-Agent"
HttpHeaderClientFeatures = "Mondoo-Features"
HttpHeaderPlatformID = "Mondoo-PlatformID"
)

h := http.Header{}
info := map[string]string{
"cnquery": cnquery.Version,
"build": cnquery.Build,
}
if sysInfo != nil {
info["PN"] = sysInfo.Platform.Name
info["PR"] = sysInfo.Platform.Version
info["PA"] = sysInfo.Platform.Arch
info["IP"] = sysInfo.IP
info["HN"] = sysInfo.Hostname
h.Set(HttpHeaderPlatformID, sysInfo.PlatformId)
}
h.Set(HttpHeaderUserAgent, scope.XInfoHeader(info))
h.Set(HttpHeaderClientFeatures, features.Encode())
return scope.NewCustomHeaderRangerPlugin(h)
}

var reMdName = regexp.MustCompile(`/([^/]+)\.md$`)

func GenerateMarkdown(dir string) error {
Expand Down
6 changes: 3 additions & 3 deletions explorer/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
// plan scan jobs
reporter := NewAggregateReporter()
if job.Bundle == nil && upstream != nil && upstream.Creds != nil {
client, err := upstream.InitClient()
client, err := upstream.InitClient(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
spaceMrn := ""
var services *explorer.Services
if upstream != nil && upstream.ApiEndpoint != "" && !upstream.Incognito {
client, err := upstream.InitClient()
client, err := upstream.InitClient(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -447,7 +447,7 @@ func (s *LocalScanner) runMotorizedAsset(job *AssetJob) (*AssetReport, error) {
runtimeErr := inmemory.WithDb(job.runtime, func(db *inmemory.Db, services *explorer.LocalServices) error {
if job.UpstreamConfig != nil && job.UpstreamConfig.ApiEndpoint != "" && !job.UpstreamConfig.Incognito {
log.Debug().Msg("using API endpoint " + job.UpstreamConfig.ApiEndpoint)
client, err := job.UpstreamConfig.InitClient()
client, err := job.UpstreamConfig.InitClient(job.Ctx)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions providers-sdk/v1/upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
package upstream

import (
"context"
"errors"
"net/http"
"net/url"

"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v10"
"go.mondoo.com/cnquery/v10/utils/multierr"
rangerUtils "go.mondoo.com/cnquery/v10/utils/ranger"
"go.mondoo.com/ranger-rpc"
guard_cert_auth "go.mondoo.com/ranger-rpc/plugins/authentication/cert"
"go.mondoo.com/ranger-rpc/plugins/rangerguard/crypto"
Expand Down Expand Up @@ -52,15 +55,17 @@ type UpstreamClient struct {
HttpClient *http.Client
}

func (c *UpstreamConfig) InitClient() (*UpstreamClient, error) {
func (c *UpstreamConfig) InitClient(ctx context.Context) (*UpstreamClient, error) {
certAuth, err := NewServiceAccountRangerPlugin(c.Creds)
if err != nil {
return nil, multierr.Wrap(err, "could not initialize client authentication")
}
plugins := []ranger.ClientPlugin{certAuth}
plugins = append(plugins, rangerUtils.DefaultRangerPlugins(cnquery.GetFeatures(ctx))...)

res := UpstreamClient{
UpstreamConfig: *c,
Plugins: []ranger.ClientPlugin{certAuth},
Plugins: plugins,
HttpClient: c.httpClient(),
}

Expand Down
3 changes: 2 additions & 1 deletion providers/arista/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"net/url"
"strconv"
Expand Down Expand Up @@ -132,7 +133,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/atlassian/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"

"go.mondoo.com/cnquery/v10/llx"
Expand Down Expand Up @@ -145,7 +146,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/aws/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"strings"

Expand Down Expand Up @@ -214,7 +215,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/azure/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"fmt"

Expand Down Expand Up @@ -211,7 +212,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/core/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"

"go.mondoo.com/cnquery/v10/llx"
Expand Down Expand Up @@ -40,7 +41,7 @@ func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
var upstream *upstream.UpstreamClient
var err error
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/equinix/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"os"

Expand Down Expand Up @@ -116,7 +117,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/gcp/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"os"

Expand Down Expand Up @@ -229,7 +230,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/github/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"os"
"strings"
Expand Down Expand Up @@ -135,7 +136,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/gitlab/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"os"
"strconv"
Expand Down Expand Up @@ -149,7 +150,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/google-workspace/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"os"

Expand Down Expand Up @@ -167,7 +168,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/ipmi/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"
"net/url"
"strconv"
Expand Down Expand Up @@ -126,7 +127,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/k8s/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"

"go.mondoo.com/cnquery/v10"
Expand Down Expand Up @@ -164,7 +165,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion providers/ms365/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package provider

import (
"context"
"errors"

"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory"
Expand Down Expand Up @@ -123,7 +124,7 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba

var upstream *upstream.UpstreamClient
if req.Upstream != nil && !req.Upstream.Incognito {
upstream, err = req.Upstream.InitClient()
upstream, err = req.Upstream.InitClient(context.Background())
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit a8e0e53

Please sign in to comment.