-
Notifications
You must be signed in to change notification settings - Fork 0
/
clients.go
41 lines (31 loc) · 1.09 KB
/
clients.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package admin
import (
"context"
"fmt"
"os"
"github.com/raito-io/cli/base/util/config"
"golang.org/x/oauth2/google"
gcpadmin "google.golang.org/api/admin/directory/v1"
"google.golang.org/api/option"
"github.com/raito-io/cli-plugin-gcp/internal/common"
)
func NewGcpAdminService(ctx context.Context, configMap *config.ConfigMap) (*gcpadmin.Service, error) {
key := configMap.GetString(common.GcpSAFileLocation)
if key == "" {
key = os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
}
serviceAccountJSON, err := os.ReadFile(key)
if err != nil {
return nil, fmt.Errorf("read file %q: %w", key, err)
}
config, err := google.JWTConfigFromJSON(serviceAccountJSON, gcpadmin.AdminDirectoryGroupReadonlyScope, gcpadmin.AdminDirectoryUserReadonlyScope)
if err != nil {
return nil, fmt.Errorf("create jwt config from file %q: %w", key, err)
}
config.Subject = configMap.GetString(common.GsuiteImpersonateSubject)
service, err := gcpadmin.NewService(ctx, option.WithHTTPClient(config.Client(ctx)))
if err != nil {
return nil, fmt.Errorf("create gcp admin service: %w", err)
}
return service, nil
}