/
firebase.go
276 lines (238 loc) · 8.29 KB
/
firebase.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package remote
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"sort"
"strings"
"time"
"github.com/NordSecurity/nordvpn-linux/config"
"github.com/NordSecurity/nordvpn-linux/internal"
"github.com/coreos/go-semver/semver"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/firebaseremoteconfig/v1"
"google.golang.org/api/option"
)
const (
firebaseTimeout = time.Second * 8
remoteConfigScope = "https://www.googleapis.com/auth/firebase.remoteconfig"
)
// RemoteConfigService interface
type RemoteConfigService interface {
FetchRemoteConfig() ([]byte, error)
}
type ServiceAccount struct {
Type string `json:"type"`
ProjectID string `json:"project_id"`
PrivateKeyID string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientID string `json:"client_id"`
AuthURI string `json:"auth_uri"`
TokenURI string `json:"token_uri"`
AuthProviderX509CertURL string `json:"auth_provider_x509_cert_url"`
ClientX509CertURL string `json:"client_x509_cert_url"`
}
type RConfig struct {
updatePeriod time.Duration
config *firebaseremoteconfig.RemoteConfig
remoteService RemoteConfigService
configManager config.Manager
}
// NewRConfig creates instance of remote config
func NewRConfig(updatePeriod time.Duration, rs RemoteConfigService, cm config.Manager) *RConfig {
return &RConfig{
updatePeriod: updatePeriod,
config: &firebaseremoteconfig.RemoteConfig{},
remoteService: rs,
configManager: cm,
}
}
func (rc *RConfig) fetchRemoteConfigIfTime() error {
var cfg config.Config
if err := rc.configManager.Load(&cfg); err != nil {
return fmt.Errorf("loading config: %w", err)
}
remoteConfig := []byte(cfg.RemoteConfig)
// don't fetch the remote config too often even if there is nothing cached
if time.Now().After(cfg.RCLastUpdate.Add(rc.updatePeriod)) {
if fetchedConfig, err := rc.fetchAndSaveRemoteConfig(); err != nil {
// if there no is cached config return error
// otherwise use the cached data
if len(remoteConfig) == 0 {
return err
} else {
log.Println(internal.ErrorPrefix, "use cached config because fetch failed:", err)
}
} else {
remoteConfig = fetchedConfig
}
}
if err := json.Unmarshal(remoteConfig, rc.config); err != nil {
return fmt.Errorf("parsing remote config from JSON: %w", err)
}
if rc.config == nil {
return fmt.Errorf("no remote config value")
}
return nil
}
func (rc *RConfig) fetchAndSaveRemoteConfig() (remoteConfig []byte, err error) {
// Always save the last update time and also save the remote config if it was retrieved successfully
defer func() {
errCfgSave := rc.configManager.SaveWith(func(c config.Config) config.Config {
c.RCLastUpdate = time.Now()
if err == nil {
c.RemoteConfig = string(remoteConfig)
}
return c
})
err = errors.Join(err, errCfgSave)
}()
remoteConfigValue, err := rc.remoteService.FetchRemoteConfig()
if err != nil {
return nil, fmt.Errorf("fetching the remote config failed: %w", err)
}
var config firebaseremoteconfig.RemoteConfig
err = json.Unmarshal(remoteConfigValue, &config)
if err != nil {
return nil, fmt.Errorf("parsing the fetched remote config failed: %w", err)
}
return remoteConfigValue, nil
}
// GetValue provides value of requested key from remote config
func (rc *RConfig) GetValue(cfgKey string) (string, error) {
err := rc.fetchRemoteConfigIfTime()
if err != nil {
log.Println(internal.WarningPrefix, "using cached config:", err)
}
// if fetching new config fails, use the cached info
configParam := rc.config.Parameters
for key, val := range configParam {
if key == cfgKey {
return val.DefaultValue.Value, nil
}
}
if err != nil {
// when not found return the original error from fetch
return "", err
}
return "", fmt.Errorf("key %s does not exist in remote config", cfgKey)
}
func stringToSemVersion(stringVersion, prefix string) (*semver.Version, error) {
// removing test suffix if any
stringVersion = strings.TrimSuffix(stringVersion, "_test")
// removing predefined prefix
stringVersion = strings.TrimPrefix(stringVersion, prefix)
// if version development, remove extra suffix
if strings.Contains(stringVersion, "+") {
stringVersion = strings.Split(stringVersion, "+")[0]
}
// if version has v added, remove it.
stringVersion = strings.Replace(stringVersion, "v", "", 1)
// in remote config field name dots are not allowed, using underscores instead,
// need to replace here
stringVersion = strings.ReplaceAll(stringVersion, "_", ".")
return semver.NewVersion(stringVersion)
}
// GetTelioConfig try to find remote config field for app version
// and load json block from that field
func (rc *RConfig) GetTelioConfig(stringVersion string) (string, error) {
if err := rc.fetchRemoteConfigIfTime(); err != nil {
if len(rc.config.Parameters) == 0 {
return "", err
}
log.Println(internal.WarningPrefix, "using cached config:", err)
}
appVersion, err := stringToSemVersion(stringVersion, "")
if err != nil {
return "", err
}
// build descending ordered version list
orderedFields := []*fieldVersion{}
for key := range rc.config.Parameters {
if strings.HasPrefix(key, RcTelioConfigFieldPrefix) {
ver, err := stringToSemVersion(key, RcTelioConfigFieldPrefix)
if err != nil {
log.Println(err)
continue
}
orderedFields = insertFieldVersion(orderedFields, &fieldVersion{ver, key})
}
}
// find exact version match or first older/lower version
versionField, err := findVersionField(orderedFields, appVersion)
if err != nil {
return "", err
}
log.Println("remote config version field:", versionField)
jsonString, err := rc.GetValue(versionField)
if err != nil {
return "", err
}
return jsonString, nil
}
type fieldVersion struct {
version *semver.Version
fieldName string
}
func insertFieldVersion(sourceArray []*fieldVersion, s *fieldVersion) []*fieldVersion {
// build list descending order by sem version
i := sort.Search(len(sourceArray), func(i int) bool { return sourceArray[i].version.Compare(*s.version) <= 0 })
sourceArray = append(sourceArray, nil)
copy(sourceArray[i+1:], sourceArray[i:])
sourceArray[i] = s
return sourceArray
}
func findVersionField(sourceArray []*fieldVersion, appVersion *semver.Version) (string, error) {
for _, item := range sourceArray {
// looking for exact or older version
if appVersion.Compare(*item.version) >= 0 {
return item.fieldName, nil
}
}
return "", errors.New("version field not found in remote config")
}
// FirebaseService is RemoteService implementation for Firebase
type FirebaseService struct {
serviceToken string
}
func NewFirebaseService(st string) *FirebaseService {
return &FirebaseService{st}
}
func (fs *FirebaseService) FetchRemoteConfig() ([]byte, error) {
var serviceAccount ServiceAccount
err := json.Unmarshal([]byte(fs.serviceToken), &serviceAccount)
if err != nil {
return nil, fmt.Errorf("could not load service account data, %w", err)
}
// Add context timeout for no net situation
timeoutCtx, cancel := context.WithTimeout(context.Background(), firebaseTimeout)
timeoutCtx = context.WithValue(timeoutCtx, oauth2.HTTPClient, &http.Client{Timeout: firebaseTimeout})
defer cancel()
creds, err := google.CredentialsFromJSON(timeoutCtx, []byte(fs.serviceToken), remoteConfigScope)
if err != nil {
return nil, fmt.Errorf("could not load credentials, %w", err)
}
firebaseremoteconfigService, err := firebaseremoteconfig.NewService(timeoutCtx, option.WithCredentials(creds))
if err != nil {
return nil, fmt.Errorf("could not create new firebase service, %w", err)
}
// Get project remote config
fireBaseRemoteConfigCall := firebaseremoteconfigService.Projects.GetRemoteConfig("projects/" + serviceAccount.ProjectID).Context(timeoutCtx)
remoteConfig, err := fireBaseRemoteConfigCall.Do()
if err != nil || remoteConfig == nil {
return nil, fmt.Errorf("could not execute firebase remote config call, %w", err)
}
if remoteConfig.ServerResponse.HTTPStatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid remote config server HTTP response: %d", remoteConfig.ServerResponse.HTTPStatusCode)
}
s, err := json.Marshal(*remoteConfig)
if err != nil {
return nil, fmt.Errorf("could not load credentials, %w", err)
}
return s, nil
}