This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
google.go
83 lines (70 loc) · 2.48 KB
/
google.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package auth
import (
"github.com/caos/orbos/internal/operator/boom/api/v1beta2/reconciling/auth/google"
helper2 "github.com/caos/orbos/internal/utils/helper"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
type googleConnector struct {
Issuer string `yaml:"issuer,omitempty"`
ClientID string `yaml:"clientID,omitempty"`
ClientSecret string `yaml:"clientSecret,omitempty"`
RedirectURI string `yaml:"redirectURI,omitempty"`
HostedDomains []string `yaml:"hostedDomains,omitempty"`
Groups []string `yaml:"groups,omitempty"`
ServiceAccountFilePath string `yaml:"serviceAccountFilePath,omitempty"`
AdminEmail string `yaml:"adminEmail,omitempty"`
}
func getGoogle(spec *google.Connector, redirect string) (interface{}, error) {
clientID, err := helper2.GetSecretValue(spec.Config.ClientID, spec.Config.ExistingClientIDSecret)
if err != nil {
return nil, err
}
clientSecret, err := helper2.GetSecretValue(spec.Config.ClientSecret, spec.Config.ExistingClientSecretSecret)
if err != nil {
return nil, err
}
serviceAccountJSON, err := helper2.GetSecretValue(spec.Config.ServiceAccountJSON, spec.Config.ExistingServiceAccountJSONSecret)
if err != nil {
return nil, err
}
if clientID == "" || clientSecret == "" {
return nil, nil
}
// get base path
base, err := filepath.Abs(spec.Config.ServiceAccountFilePath)
if err != nil {
return nil, err
}
// remove file if alread exists
_, err = os.Stat(spec.Config.ServiceAccountFilePath)
if !os.IsNotExist(err) {
if err := os.Remove(spec.Config.ServiceAccountFilePath); err != nil {
return nil, err
}
}
// create all directories to the file
if err := os.MkdirAll(base, os.ModePerm); err != nil {
return nil, err
}
if serviceAccountJSON != "" {
// write json to file
err = ioutil.WriteFile(spec.Config.ServiceAccountFilePath, []byte(serviceAccountJSON), 0644)
if err != nil {
return nil, errors.Wrapf(err, "Error while writing json to file %s", spec.Config.ServiceAccountFilePath)
}
}
google := &googleConnector{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURI: redirect,
Groups: spec.Config.Groups,
HostedDomains: spec.Config.HostedDomains,
ServiceAccountFilePath: spec.Config.ServiceAccountFilePath,
AdminEmail: spec.Config.AdminEmail,
Issuer: "https://accounts.google.com",
}
return google, nil
}