2023-06-20 19:15:36 +02:00
|
|
|
package idp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2023-09-04 17:03:44 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-06-20 19:15:36 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
admin "google.golang.org/api/admin/directory/v1"
|
|
|
|
"google.golang.org/api/option"
|
2023-10-03 16:40:28 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
2023-06-20 19:15:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GoogleWorkspaceManager Google Workspace manager client instance.
|
|
|
|
type GoogleWorkspaceManager struct {
|
|
|
|
usersService *admin.UsersService
|
|
|
|
CustomerID string
|
|
|
|
httpClient ManagerHTTPClient
|
|
|
|
credentials ManagerCredentials
|
|
|
|
helper ManagerHelper
|
|
|
|
appMetrics telemetry.AppMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
// GoogleWorkspaceClientConfig Google Workspace manager client configurations.
|
|
|
|
type GoogleWorkspaceClientConfig struct {
|
|
|
|
ServiceAccountKey string
|
|
|
|
CustomerID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GoogleWorkspaceCredentials Google Workspace authentication information.
|
|
|
|
type GoogleWorkspaceCredentials struct {
|
|
|
|
clientConfig GoogleWorkspaceClientConfig
|
|
|
|
helper ManagerHelper
|
|
|
|
httpClient ManagerHTTPClient
|
|
|
|
appMetrics telemetry.AppMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *GoogleWorkspaceCredentials) Authenticate() (JWTToken, error) {
|
|
|
|
return JWTToken{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGoogleWorkspaceManager creates a new instance of the GoogleWorkspaceManager.
|
|
|
|
func NewGoogleWorkspaceManager(config GoogleWorkspaceClientConfig, appMetrics telemetry.AppMetrics) (*GoogleWorkspaceManager, error) {
|
|
|
|
httpTransport := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
httpTransport.MaxIdleConns = 5
|
|
|
|
|
|
|
|
httpClient := &http.Client{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
Transport: httpTransport,
|
|
|
|
}
|
|
|
|
helper := JsonParser{}
|
|
|
|
|
|
|
|
if config.CustomerID == "" {
|
|
|
|
return nil, fmt.Errorf("google IdP configuration is incomplete, CustomerID is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
credentials := &GoogleWorkspaceCredentials{
|
|
|
|
clientConfig: config,
|
|
|
|
httpClient: httpClient,
|
|
|
|
helper: helper,
|
|
|
|
appMetrics: appMetrics,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new Admin SDK Directory service client
|
|
|
|
adminCredentials, err := getGoogleCredentials(config.ServiceAccountKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
service, err := admin.NewService(context.Background(),
|
2023-10-03 16:40:28 +02:00
|
|
|
option.WithScopes(admin.AdminDirectoryUserReadonlyScope),
|
2023-06-20 19:15:36 +02:00
|
|
|
option.WithCredentials(adminCredentials),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &GoogleWorkspaceManager{
|
|
|
|
usersService: service.Users,
|
|
|
|
CustomerID: config.CustomerID,
|
|
|
|
httpClient: httpClient,
|
|
|
|
credentials: credentials,
|
|
|
|
helper: helper,
|
|
|
|
appMetrics: appMetrics,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUserAppMetadata updates user app metadata based on userID and metadata map.
|
2023-10-03 16:40:28 +02:00
|
|
|
func (gm *GoogleWorkspaceManager) UpdateUserAppMetadata(_ string, _ AppMetadata) error {
|
2023-06-20 19:15:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserDataByID requests user data from Google Workspace via ID.
|
|
|
|
func (gm *GoogleWorkspaceManager) GetUserDataByID(userID string, appMetadata AppMetadata) (*UserData, error) {
|
|
|
|
user, err := gm.usersService.Get(userID).Projection("full").Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if gm.appMetrics != nil {
|
|
|
|
gm.appMetrics.IDPMetrics().CountGetUserDataByID()
|
|
|
|
}
|
|
|
|
|
2023-10-03 16:40:28 +02:00
|
|
|
userData := parseGoogleWorkspaceUser(user)
|
|
|
|
userData.AppMetadata = appMetadata
|
|
|
|
|
|
|
|
return userData, nil
|
2023-06-20 19:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccount returns all the users for a given profile.
|
|
|
|
func (gm *GoogleWorkspaceManager) GetAccount(accountID string) ([]*UserData, error) {
|
2023-10-03 16:40:28 +02:00
|
|
|
usersList, err := gm.usersService.List().Customer(gm.CustomerID).Projection("full").Do()
|
2023-06-20 19:15:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
usersData := make([]*UserData, 0)
|
|
|
|
for _, user := range usersList.Users {
|
2023-10-03 16:40:28 +02:00
|
|
|
userData := parseGoogleWorkspaceUser(user)
|
|
|
|
userData.AppMetadata.WTAccountID = accountID
|
2023-06-20 19:15:36 +02:00
|
|
|
|
|
|
|
usersData = append(usersData, userData)
|
|
|
|
}
|
|
|
|
|
|
|
|
return usersData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllAccounts gets all registered accounts with corresponding user data.
|
|
|
|
// It returns a list of users indexed by accountID.
|
|
|
|
func (gm *GoogleWorkspaceManager) GetAllAccounts() (map[string][]*UserData, error) {
|
|
|
|
usersList, err := gm.usersService.List().Customer(gm.CustomerID).Projection("full").Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if gm.appMetrics != nil {
|
|
|
|
gm.appMetrics.IDPMetrics().CountGetAllAccounts()
|
|
|
|
}
|
|
|
|
|
|
|
|
indexedUsers := make(map[string][]*UserData)
|
|
|
|
for _, user := range usersList.Users {
|
2023-10-03 16:40:28 +02:00
|
|
|
userData := parseGoogleWorkspaceUser(user)
|
|
|
|
indexedUsers[UnsetAccountID] = append(indexedUsers[UnsetAccountID], userData)
|
2023-06-20 19:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return indexedUsers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateUser creates a new user in Google Workspace and sends an invitation.
|
2023-10-03 16:40:28 +02:00
|
|
|
func (gm *GoogleWorkspaceManager) CreateUser(_, _, _, _ string) (*UserData, error) {
|
|
|
|
return nil, fmt.Errorf("method CreateUser not implemented")
|
2023-06-20 19:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserByEmail searches users with a given email.
|
|
|
|
// If no users have been found, this function returns an empty list.
|
|
|
|
func (gm *GoogleWorkspaceManager) GetUserByEmail(email string) ([]*UserData, error) {
|
|
|
|
user, err := gm.usersService.Get(email).Projection("full").Do()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if gm.appMetrics != nil {
|
|
|
|
gm.appMetrics.IDPMetrics().CountGetUserByEmail()
|
|
|
|
}
|
|
|
|
|
|
|
|
users := make([]*UserData, 0)
|
2023-10-03 16:40:28 +02:00
|
|
|
users = append(users, parseGoogleWorkspaceUser(user))
|
2023-06-20 19:15:36 +02:00
|
|
|
|
|
|
|
return users, nil
|
|
|
|
}
|
|
|
|
|
2023-07-03 12:20:19 +02:00
|
|
|
// InviteUserByID resend invitations to users who haven't activated,
|
|
|
|
// their accounts prior to the expiration period.
|
|
|
|
func (gm *GoogleWorkspaceManager) InviteUserByID(_ string) error {
|
|
|
|
return fmt.Errorf("method InviteUserByID not implemented")
|
|
|
|
}
|
|
|
|
|
2023-09-19 18:08:40 +02:00
|
|
|
// DeleteUser from GoogleWorkspace.
|
|
|
|
func (gm *GoogleWorkspaceManager) DeleteUser(userID string) error {
|
|
|
|
if err := gm.usersService.Delete(userID).Do(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if gm.appMetrics != nil {
|
|
|
|
gm.appMetrics.IDPMetrics().CountDeleteUser()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-20 19:15:36 +02:00
|
|
|
// getGoogleCredentials retrieves Google credentials based on the provided serviceAccountKey.
|
|
|
|
// It decodes the base64-encoded serviceAccountKey and attempts to obtain credentials using it.
|
|
|
|
// If that fails, it falls back to using the default Google credentials path.
|
|
|
|
// It returns the retrieved credentials or an error if unsuccessful.
|
|
|
|
func getGoogleCredentials(serviceAccountKey string) (*google.Credentials, error) {
|
|
|
|
log.Debug("retrieving google credentials from the base64 encoded service account key")
|
|
|
|
decodeKey, err := base64.StdEncoding.DecodeString(serviceAccountKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode service account key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
creds, err := google.CredentialsFromJSON(
|
|
|
|
context.Background(),
|
|
|
|
decodeKey,
|
2023-10-03 16:40:28 +02:00
|
|
|
admin.AdminDirectoryUserReadonlyScope,
|
2023-06-20 19:15:36 +02:00
|
|
|
)
|
|
|
|
if err == nil {
|
2023-09-04 17:03:44 +02:00
|
|
|
// No need to fallback to the default Google credentials path
|
|
|
|
return creds, nil
|
2023-06-20 19:15:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("failed to retrieve Google credentials from ServiceAccountKey: %v", err)
|
|
|
|
log.Debug("falling back to default google credentials location")
|
|
|
|
|
|
|
|
creds, err = google.FindDefaultCredentials(
|
|
|
|
context.Background(),
|
2023-10-03 16:40:28 +02:00
|
|
|
admin.AdminDirectoryUserReadonlyScope,
|
2023-06-20 19:15:36 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return creds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseGoogleWorkspaceUser parse google user to UserData.
|
2023-10-03 16:40:28 +02:00
|
|
|
func parseGoogleWorkspaceUser(user *admin.User) *UserData {
|
2023-06-20 19:15:36 +02:00
|
|
|
return &UserData{
|
2023-10-03 16:40:28 +02:00
|
|
|
ID: user.Id,
|
|
|
|
Email: user.PrimaryEmail,
|
|
|
|
Name: user.Name.FullName,
|
|
|
|
}
|
2023-06-20 19:15:36 +02:00
|
|
|
}
|