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

backport kali & google workspace fixes to v7 #982

Merged
merged 2 commits into from
Feb 26, 2023
Merged
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
119 changes: 119 additions & 0 deletions resources/packs/googleworkspace/connected_apps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package googleworkspace

import (
"go.mondoo.com/cnquery/resources/packs/core"
"go.mondoo.com/cnquery/stringx"
)

type connectedApp struct {
clientID string
scopes []string
name string
users []*mqlGoogleworkspaceUser
tokens []*mqlGoogleworkspaceToken
}

func (g *mqlGoogleworkspace) GetConnectedApps() ([]interface{}, error) {
// get all users
users, err := g.Users()
if err != nil {
return nil, err
}

connectedApps := map[string]*connectedApp{}
for _, user := range users {
usr := user.(*mqlGoogleworkspaceUser)
// get all token from user
tokens, err := usr.GetTokens()
if err != nil {
return nil, err
}

for _, token := range tokens {
tk := token.(*mqlGoogleworkspaceToken)

clientID, err := tk.ClientId()
if err != nil {
return nil, err
}

cApp, ok := connectedApps[clientID]
if !ok {
cApp = &connectedApp{
clientID: clientID,
users: []*mqlGoogleworkspaceUser{},
tokens: []*mqlGoogleworkspaceToken{},
}
}

// assign name
displayText, err := tk.DisplayText()
if err != nil {
return nil, err
}
cApp.name = displayText

// merge scopes
scopes, err := tk.Scopes()
if err != nil {
return nil, err
}
stringScopes := []string{}
for _, scope := range scopes {
stringScopes = append(stringScopes, scope.(string))
}
cApp.scopes = stringx.DedupStringArray(append(cApp.scopes, stringScopes...))

cApp.tokens = append(cApp.tokens, tk)
cApp.users = append(cApp.users, usr)

connectedApps[clientID] = cApp
}
}

// group token by client id
runtime := g.MotorRuntime
res := make([]interface{}, len(connectedApps))
i := 0
for k := range connectedApps {
connectedApp := connectedApps[k]

mqlUsers := make([]interface{}, len(connectedApp.users))
if connectedApp.users != nil && len(connectedApp.users) > 0 {
for i := range connectedApp.users {
mqlUsers[i] = connectedApp.users[i]
}
}

mqlTokens := make([]interface{}, len(connectedApp.tokens))
if connectedApp.tokens != nil && len(connectedApp.tokens) > 0 {
for i := range connectedApp.tokens {
mqlTokens[i] = connectedApp.tokens[i]
}
}

mqlApp, err := runtime.CreateResource("googleworkspace.connectedApp",
"clientId", connectedApp.clientID,
"name", connectedApp.name,
"scopes", core.StrSliceToInterface(connectedApp.scopes),
"users", mqlUsers,
"tokens", mqlTokens,
)
if err != nil {
return nil, err
}
res[i] = mqlApp
i++
}

return res, err
}

func (g *mqlGoogleworkspaceConnectedApp) id() (string, error) {
clientId, err := g.ClientId()
if err != nil {
return "", err
}

return "googleworkspace.connectedApp/" + clientId, nil
}
16 changes: 16 additions & 0 deletions resources/packs/googleworkspace/googleworkspace.lr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ googleworkspace {
groups() []googleworkspace.group
// Retrieves a list of all roles for the Google Workspace account
roles() []googleworkspace.role
// Retrieves a list of all apps for the Google Workspace account
connectedApps() []googleworkspace.connectedApp
}

// Google Workspace organizational unit
Expand Down Expand Up @@ -96,6 +98,20 @@ private googleworkspace.token @defaults("displayText") {
userKey string
}

// Google Workspace Third-party Connected Apps
private googleworkspace.connectedApp @defaults("name clientId") {
// The unique ID of the application
clientId string
// The application's name
name string
// Aggregated scopes across all tokens issued to the application
scopes []string
// Google Workspace User that use the 3rd-party application
users []googleworkspace.user
// Returns the user-issued tokens to 3rd party applications
tokens []googleworkspace.token
}

// Google Workspace Group
private googleworkspace.group @defaults("email") {
// The unique ID of a group
Expand Down
Loading