-
Notifications
You must be signed in to change notification settings - Fork 0
/
gke_capabilities.go
189 lines (148 loc) · 4.01 KB
/
gke_capabilities.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package capabilities
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/rancher/kontainer-engine/drivers/gke"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/container/v1"
"google.golang.org/api/iam/v1"
)
type capabilitiesRequestBody struct {
Credentials string `json:"credentials,omitempty"`
ProjectID string `json:"projectId,omitempty"`
Zone string `json:"zone,omitempty"`
Region string `json:"region,omitempty"`
}
func validateCapabilitiesRequestBody(writer http.ResponseWriter, body *capabilitiesRequestBody) error {
credentials := body.Credentials
projectID := body.ProjectID
if projectID == "" {
writer.WriteHeader(http.StatusBadRequest)
return fmt.Errorf("invalid projectId")
}
if credentials == "" {
writer.WriteHeader(http.StatusBadRequest)
return fmt.Errorf("invalid credentials")
}
return nil
}
func getOAuthClient(ctx context.Context, credentialContent string) (*http.Client, error) {
// The google SDK has no sane way to pass in a TokenSource give all the different types (user, service account, etc)
// So we actually set an environment variable and then unset it
gke.EnvMutex.Lock()
locked := true
setEnv := false
cleanup := func() {
if setEnv {
os.Unsetenv(defaultCredentialEnv)
}
if locked {
gke.EnvMutex.Unlock()
locked = false
}
}
defer cleanup()
file, err := ioutil.TempFile("", "credential-file")
if err != nil {
return nil, err
}
defer os.Remove(file.Name())
defer file.Close()
if _, err := io.Copy(file, strings.NewReader(credentialContent)); err != nil {
return nil, err
}
setEnv = true
os.Setenv(defaultCredentialEnv, file.Name())
ts, err := google.DefaultTokenSource(ctx, container.CloudPlatformScope)
if err != nil {
return nil, err
}
// Unlocks
cleanup()
return oauth2.NewClient(ctx, ts), nil
}
func handleErr(writer http.ResponseWriter, originalErr error) {
resp := errorResponse{originalErr.Error()}
asJSON, err := json.Marshal(resp)
if err != nil {
logrus.Error("error while marshalling error message '" + originalErr.Error() + "' error was '" + err.Error() + "'")
writer.Write([]byte(err.Error()))
return
}
writer.Write([]byte(asJSON))
}
func preCheck(writer http.ResponseWriter, req *http.Request) *capabilitiesRequestBody {
if req.Method != http.MethodPost {
writer.WriteHeader(http.StatusMethodNotAllowed)
return nil
}
writer.Header().Set("Content-Type", "application/json")
var body capabilitiesRequestBody
err := extractRequestBody(writer, req, &body)
if err != nil {
handleErr(writer, err)
return nil
}
err = validateCapabilitiesRequestBody(writer, &body)
if err != nil {
handleErr(writer, err)
return nil
}
return &body
}
func postCheck(writer http.ResponseWriter, result interface{}, err error) {
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
serialized, err := json.Marshal(&result)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
handleErr(writer, err)
return
}
writer.Write(serialized)
}
func getComputeServiceClient(ctx context.Context, credentialContent string) (*compute.Service, error) {
client, err := getOAuthClient(ctx, credentialContent)
if err != nil {
return nil, err
}
service, err := compute.New(client)
if err != nil {
return nil, err
}
return service, nil
}
func getIamServiceClient(ctx context.Context, credentialContent string) (*iam.Service, error) {
client, err := getOAuthClient(ctx, credentialContent)
if err != nil {
return nil, err
}
service, err := iam.New(client)
if err != nil {
return nil, err
}
return service, nil
}
func getContainerServiceClient(ctx context.Context, credentialContent string) (*container.Service, error) {
client, err := getOAuthClient(ctx, credentialContent)
if err != nil {
return nil, err
}
service, err := container.New(client)
if err != nil {
return nil, err
}
return service, nil
}