Skip to content

Commit

Permalink
🧹 Add tech url segments for google workspace. Improve asset name.
Browse files Browse the repository at this point in the history
Signed-off-by: Preslav <preslav@mondoo.com>
  • Loading branch information
preslavgerchev committed Jul 29, 2024
1 parent c07ed24 commit 5f75cda
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
11 changes: 11 additions & 0 deletions providers/google-workspace/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package config

import (
"go.mondoo.com/cnquery/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers/google-workspace/provider"
)
Expand Down Expand Up @@ -55,4 +56,14 @@ The provider requires these three flags:
},
},
},
AssetUrlTrees: []*inventory.AssetUrlBranch{
{
PathSegments: []string{"technology=google-workspace"},
Key: "customer",
Title: "Customer",
Values: map[string]*inventory.AssetUrlBranch{
"*": nil,
},
},
},
}
39 changes: 27 additions & 12 deletions providers/google-workspace/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package provider
import (
"context"
"errors"
"fmt"
"os"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -44,24 +45,28 @@ func readEnvs(envs ...string) []string {
}

// to be used by gcp/googleworkspace cmds, fetches the creds from either the env vars provided or from a flag in the provided cmd
func getGoogleCreds(credentialPath string, envs ...string) []byte {
func getGoogleCreds(credentialPath string, envs ...string) ([]byte, error) {
var credsPaths []string
// env vars have precedence over the --credentials-path arg
credsPaths = readEnvs(envs...)

errs := []error{}
if credentialPath != "" {
credsPaths = append(credsPaths, credentialPath)
}

for i := range credsPaths {
path := credsPaths[i]

_, err := os.Stat(path)
if err != nil {
errs = append(errs, err)
}
serviceAccount, err := os.ReadFile(path)
if err == nil {
return serviceAccount
return serviceAccount, nil
}
}
return nil
return nil, errors.Join(errs...)
}

func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error) {
Expand All @@ -87,7 +92,11 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
"GOOGLEWORKSPACE_CLOUD_KEYFILE_JSON",
"GOOGLE_CREDENTIALS",
}
serviceAccount := getGoogleCreds(credentialsPath, envVars...)
serviceAccount, err := getGoogleCreds(credentialsPath, envVars...)
if err != nil {
log.Error().Err(err).Msg("could not read service account credentials")
return nil, err
}
if serviceAccount != nil {
conf.Credentials = append(conf.Credentials, &vault.Credential{
Type: vault.CredentialType_json,
Expand Down Expand Up @@ -192,14 +201,20 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
}

func (s *Service) detect(asset *inventory.Asset, conn *connection.GoogleWorkspaceConnection) error {
asset.Name = conn.Conf.Host

pd, err := resources.GetPrimaryDomain(conn)
if err != nil {
log.Error().Err(err).Msg("could not get primary domain for google workspace")
asset.Name = conn.CustomerID()
} else {
asset.Name = fmt.Sprintf("%s %s", pd, conn.CustomerID())
}
asset.Platform = &inventory.Platform{
Name: "google-workspace",
Family: []string{"google"},
Kind: "api",
Title: "Google Workspace",
Runtime: "google-workspace",
Name: "google-workspace",
Family: []string{"google"},
Kind: "api",
Title: "Google Workspace",
Runtime: "google-workspace",
TechnologyUrlSegments: []string{"google-workspace", conn.CustomerID()},
}

asset.PlatformIds = []string{"//platformid.api.mondoo.app/runtime/googleworkspace/customer/" + conn.CustomerID()}
Expand Down
21 changes: 21 additions & 0 deletions providers/google-workspace/resources/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package resources

import (
"errors"
"time"

"go.mondoo.com/cnquery/v11/llx"
Expand All @@ -12,6 +13,26 @@ import (
directory "google.golang.org/api/admin/directory/v1"
)

func GetPrimaryDomain(conn *connection.GoogleWorkspaceConnection) (string, error) {
directoryService, err := directoryService(conn, directory.AdminDirectoryDomainReadonlyScope)
if err != nil {
return "", err
}

domains, err := directoryService.Domains.List(conn.CustomerID()).Do()
if err != nil {
return "", err
}

for _, domain := range domains.Domains {
if domain.IsPrimary {
return domain.DomainName, nil
}
}

return "", errors.New("no primary domain found")
}

func (g *mqlGoogleworkspace) domains() ([]interface{}, error) {
conn := g.MqlRuntime.Connection.(*connection.GoogleWorkspaceConnection)
directoryService, err := directoryService(conn, directory.AdminDirectoryDomainReadonlyScope)
Expand Down

0 comments on commit 5f75cda

Please sign in to comment.