-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_context_impl.go
255 lines (225 loc) · 6.7 KB
/
security_context_impl.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package secure_backend
import (
"cloud.google.com/go/compute/metadata"
"context"
"crypto/rsa"
"encoding/json"
firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/patrickmn/go-cache"
"golang.org/x/xerrors"
"google.golang.org/api/option"
"google.golang.org/api/servicecontrol/v1"
"io/ioutil"
"net/url"
"os"
"time"
)
type securityContextImpl struct {
ctx context.Context
logger *Logger
/*
Google Cloud Platform data.
*/
gcp struct {
/*
Validated API Keys on memory.
*/
validApiKeys *cache.Cache
serviceAccountJson []byte
/*
Current public Key
*/
serviceAccountPublicKeys *googlePublicKeyCache
/*
Service Account email address.
*/
clientEmail string
/*
GCP Project ID
*/
projectId string
/*
Firebase Auth API Client.
*/
firebaseAuth *auth.Client
/*
Google ServiceControl API client.
*/
serviceControlClient *servicecontrol.Service
}
}
func (it *securityContextImpl) logInfo(message string) {
it.logger.logInfo(message)
}
func (it *securityContextImpl) NewFirebaseAuthVerifier() FirebaseAuthVerifier {
return &firebaseAuthVerifierImpl{
owner: it,
logger: it.logger,
}
}
func (it *securityContextImpl) NewGoogleApiKeyVerifier() GoogleApiKeyVerifier {
return &googleApiKeyVerifierImpl{
owner: it,
logger: it.logger,
}
}
func (it *securityContextImpl) getGoogleProjectInfoFromJson(file []byte) (projectId string, email string, publicKey *googlePublicKey, err error) {
type ServiceAccountModel struct {
ProjectId string `json:"project_id"`
ClientEmail string `json:"client_email,omitempty"`
PrivateKeyId string `json:"private_key_id"`
PrivateKey string `json:"private_key,omitempty"`
}
dto := ServiceAccountModel{}
if err := json.Unmarshal(file, &dto); err != nil {
return "", "", nil, xerrors.Errorf("service account parse error %w", err)
}
var privateKey *rsa.PrivateKey
if pem, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(dto.PrivateKey)); err != nil {
return "", "", nil, xerrors.Errorf("private key parse error %w", err)
} else {
privateKey = pem
}
return dto.ProjectId, dto.ClientEmail, &googlePublicKey{
kid: dto.PrivateKeyId,
publicKey: &privateKey.PublicKey,
}, nil
}
func (it *securityContextImpl) getGoogleProjectInfoFromMetadata() (projectId string, serviceAccountEmail string, err error) {
projectId, err = metadata.ProjectID()
if err != nil {
return "", "", xerrors.Errorf("metadata read failed(%v): %w", "ProjectId", err)
}
serviceAccountEmail, err = metadata.Get("instance/service-accounts/default/email")
if err != nil {
return "", "", xerrors.Errorf("metadata read failed(%v): %w", "instance/service-accounts/default/email", err)
}
return projectId, serviceAccountEmail, nil
}
func (it *securityContextImpl) initForGcp() error {
ctx := it.ctx
serviceAccountJson := it.gcp.serviceAccountJson
if serviceAccountJson == nil {
it.logInfo("load GOOGLE_APPLICATION_CREDENTIALS")
path := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
if len(path) > 0 {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return xerrors.Errorf("service account load failed %w", err)
}
serviceAccountJson = bytes
}
}
if len(serviceAccountJson) > 0 {
it.logInfo("Init service account from JSON key")
} else {
it.logInfo("Init service account from System key")
}
// init Firebase App.
firebaseApp, err := func() (*firebase.App, error) {
if len(serviceAccountJson) > 0 {
return firebase.NewApp(ctx, nil, option.WithCredentialsJSON(serviceAccountJson))
} else {
return firebase.NewApp(ctx, nil)
}
}()
if err != nil {
return xerrors.Errorf("Firebase App init failed: %w", err)
}
firebaseAuth, err := firebaseApp.Auth(ctx)
if err != nil {
return xerrors.Errorf("Firebase Auth initialize error: %w", err)
}
// init ServiceControl.
serviceCtrl, err := func() (*servicecontrol.Service, error) {
if len(serviceAccountJson) > 0 {
return servicecontrol.NewService(ctx, option.WithCredentialsJSON(serviceAccountJson))
} else {
return servicecontrol.NewService(ctx)
}
}()
if err != nil {
return xerrors.Errorf("ServiceControl init failed: %w", err)
}
it.gcp.validApiKeys = cache.New(time.Hour, time.Minute)
it.gcp.firebaseAuth = firebaseAuth
it.gcp.serviceAccountJson = serviceAccountJson
it.gcp.serviceControlClient = serviceCtrl
if serviceAccountJson != nil {
it.logInfo("config load from JSON")
projectId, email, publicKey, err := it.getGoogleProjectInfoFromJson(serviceAccountJson)
if err != nil {
return xerrors.Errorf("ServiceAccount file parse failed: %w", err)
}
it.logInfo(fmt.Sprintf("GCP initialize success: %v", projectId))
it.gcp.clientEmail = email
it.gcp.projectId = projectId
keyCache := newGooglePublicKeyCache(
"https://www.googleapis.com/robot/v1/metadata/x509/"+url.PathEscape(email), it.logger)
keyCache.addOfflineKey(publicKey)
err = keyCache.refreshKeys()
if err != nil {
return xerrors.Errorf("Public key refresh failed: %w", err)
}
it.gcp.serviceAccountPublicKeys = keyCache
} else {
it.logInfo("GCP config load from metadata")
projectId, email, err := it.getGoogleProjectInfoFromMetadata()
if err != nil {
return xerrors.Errorf("Metadata parse failed: %w", err)
}
it.logInfo(fmt.Sprintf("GCP initialize success: %v", projectId))
it.gcp.clientEmail = email
it.gcp.projectId = projectId
keyCache := newGooglePublicKeyCache(
"https://www.googleapis.com/robot/v1/metadata/x509/"+url.PathEscape(email), it.logger)
err = keyCache.refreshKeys()
if err != nil {
return xerrors.Errorf("Public key refresh failed: %w", err)
}
it.gcp.serviceAccountPublicKeys = keyCache
}
it.logInfo("Google Cloud Platform load completed.")
it.logInfo(fmt.Sprintf(" * projectId: %v", it.gcp.projectId))
it.logInfo(fmt.Sprintf(" * service account: %v", it.gcp.clientEmail))
if it.gcp.serviceAccountPublicKeys.latestKey != nil {
it.logInfo(fmt.Sprintf(" * public key default: %v", it.gcp.serviceAccountPublicKeys.latestKey.kid))
}
for kid, _ := range it.gcp.serviceAccountPublicKeys.allKeys {
it.logInfo(fmt.Sprintf(" * public key online: %v", kid))
}
return nil
}
/*
Initialize context.
*/
func (it *securityContextImpl) init() error {
if it.ctx == nil {
it.ctx = context.Background()
}
if it.logger == nil {
it.logger = &Logger{}
}
if err := it.initForGcp(); err != nil {
return err
}
return nil
}
/*
New instance.
*/
func NewSecurityContext(configs *SecurityContextConfigs) (SecurityContext, error) {
result := &securityContextImpl{}
if configs != nil {
result.ctx = configs.Context
result.logger = configs.Logger
result.gcp.serviceAccountJson = configs.GoogleServiceAccountJson
}
if err := result.init(); err != nil {
return nil, err
}
return result, nil
}