-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_cloud_provider.go
162 lines (135 loc) · 4.81 KB
/
google_cloud_provider.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package iapProvider
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/hexa-org/policy-mapper/api/policyprovider"
"github.com/hexa-org/policy-mapper/models/formats/gcpBind"
"github.com/hexa-org/policy-mapper/pkg/hexapolicy"
"google.golang.org/api/option"
"google.golang.org/api/transport/http"
"github.com/go-playground/validator/v10"
)
const ProviderTypeGoogleCloudIAP string = "gcp_iap"
const ProviderTypeGoogleCloud string = "google_cloud"
type GoogleProvider struct {
HttpClientOverride HTTPClient
GcpMapper *gcpBind.GooglePolicyMapper
}
func (g *GoogleProvider) initMapper() {
if g.GcpMapper == nil {
g.GcpMapper = gcpBind.New(map[string]string{})
}
}
func (g *GoogleProvider) Name() string {
return ProviderTypeGoogleCloudIAP
}
func (g *GoogleProvider) Project(key []byte) string {
return g.credentials(key).ProjectId
}
func (g *GoogleProvider) DiscoverApplications(info policyprovider.IntegrationInfo) (apps []policyprovider.ApplicationInfo, err error) {
if !strings.EqualFold(info.Name, g.Name()) {
return apps, err
}
key := info.Key
foundCredentials := g.credentials(key)
client, createClientErr := g.getHttpClient(key)
if createClientErr != nil {
fmt.Println("Unable to create google http client.")
return apps, createClientErr
}
googleClient := GoogleClient{client, foundCredentials.ProjectId}
backendApplications, err1 := googleClient.GetBackendApplications()
apps = append(apps, backendApplications...)
appEngineApplications, err2 := googleClient.GetAppEngineApplications()
apps = append(apps, appEngineApplications...)
err = err2
// report first error
if err1 != nil {
err = err1
}
return apps, err
}
func (g *GoogleProvider) GetPolicyInfo(integration policyprovider.IntegrationInfo, app policyprovider.ApplicationInfo) (infos []hexapolicy.PolicyInfo, err error) {
g.initMapper()
key := integration.Key
foundCredentials := g.credentials(key)
client, createClientErr := g.getHttpClient(key)
if createClientErr != nil {
fmt.Println("Unable to create google http client.")
return infos, createClientErr
}
googleClient := GoogleClient{client, foundCredentials.ProjectId}
bindings, err := googleClient.GetBackendPolicy(app.Name, app.ObjectID)
if err != nil {
return infos, err
}
// bindingBytes, _ := json.MarshalIndent(bindings, "", " ")
// fmt.Println(string(bindingBytes))
result := make([]hexapolicy.PolicyInfo, len(bindings))
for i, binding := range bindings {
result[i], err = g.GcpMapper.MapBindingToPolicy(app.ObjectID, binding)
if err != nil {
return infos, err
}
}
return result, nil
}
func (g *GoogleProvider) SetPolicyInfo(integration policyprovider.IntegrationInfo, app policyprovider.ApplicationInfo, policyInfos []hexapolicy.PolicyInfo) (int, error) {
g.initMapper()
validate := validator.New() // todo - move this up?
errApp := validate.Struct(app)
if errApp != nil {
return 500, errApp
}
errPolicies := validate.Var(policyInfos, "omitempty,dive")
if errPolicies != nil {
return 500, errPolicies
}
key := integration.Key
foundCredentials := g.credentials(key)
client, createClientErr := g.getHttpClient(key)
if createClientErr != nil {
fmt.Println("Unable to create google http client.")
return 500, createClientErr
}
googleClient := GoogleClient{client, foundCredentials.ProjectId}
for _, policyInfo := range policyInfos {
binding, err := g.GcpMapper.MapPolicyToBinding(policyInfo)
if err != nil {
return 500, err
}
err = googleClient.SetBackendPolicy(app.Name, app.ObjectID, binding)
if err != nil {
return 500, err
}
}
return 201, nil
}
func (g *GoogleProvider) NewHttpClient(key []byte) (HTTPClient, error) {
var opts []option.ClientOption
opt := option.WithCredentialsJSON(key)
if key == nil || len(key) == 0 {
return nil, errors.New("missing credentials")
}
opts = append([]option.ClientOption{option.WithScopes("https://www.googleapis.com/auth/cloud-platform")}, opt)
client, _, err := http.NewClient(context.Background(), opts...)
return client, err
}
type credentials struct {
ProjectId string `json:"project_id"`
}
func (g *GoogleProvider) credentials(key []byte) credentials {
var foundCredentials credentials
_ = json.NewDecoder(bytes.NewReader(key)).Decode(&foundCredentials)
return foundCredentials
}
func (g *GoogleProvider) getHttpClient(key []byte) (HTTPClient, error) {
if g.HttpClientOverride != nil {
return g.HttpClientOverride, nil
}
return g.NewHttpClient(key)
}