@@ -225,28 +225,16 @@ func readFromCloud(
225
225
return nil , errors .New ("expected config to have cloud section" )
226
226
}
227
227
228
- // empty if not cached, since its a separate request, which we check next
229
- tlsCertificate := cfg .Cloud .TLSCertificate
230
- tlsPrivateKey := cfg .Cloud .TLSPrivateKey
228
+ tls := tlsConfig {
229
+ // both fields are empty if not cached, since its a separate request, which we
230
+ // check next
231
+ certificate : cfg .Cloud .TLSCertificate ,
232
+ privateKey : cfg .Cloud .TLSPrivateKey ,
233
+ }
231
234
if ! cached {
232
- // get cached certificate data
233
- // read cached config from fs.
234
- // process the config with fromReader() use processed config as cachedConfig to update the cert data.
235
- unproccessedCachedConfig , err := readFromCache (cloudCfg .ID )
236
- if err == nil {
237
- cachedConfig , err := processConfigFromCloud (unproccessedCachedConfig , logger )
238
- if err != nil {
239
- // clear cache
240
- logger .Warn ("Detected failure to process the cached config when retrieving TLS config, clearing cache." )
241
- clearCache (cloudCfg .ID )
242
- return nil , err
243
- }
244
-
245
- if cachedConfig .Cloud != nil {
246
- tlsCertificate = cachedConfig .Cloud .TLSCertificate
247
- tlsPrivateKey = cachedConfig .Cloud .TLSPrivateKey
248
- }
249
- } else if ! os .IsNotExist (err ) {
235
+ // Try to get TLS information from the cached config (if it exists) even if we
236
+ // got a new config from the cloud.
237
+ if err := tls .readFromCache (cloudCfg .ID , logger ); err != nil {
250
238
return nil , err
251
239
}
252
240
}
@@ -255,7 +243,7 @@ func readFromCloud(
255
243
checkForNewCert = true
256
244
}
257
245
258
- if checkForNewCert || tlsCertificate == "" || tlsPrivateKey == "" {
246
+ if checkForNewCert || tls . certificate == "" || tls . privateKey == "" {
259
247
logger .Debug ("reading tlsCertificate from the cloud" )
260
248
// Use the SignalingInsecure from the Cloud config returned from the app not the initial config.
261
249
@@ -264,13 +252,13 @@ func readFromCloud(
264
252
if ! errors .Is (err , context .DeadlineExceeded ) {
265
253
return nil , err
266
254
}
267
- if tlsCertificate == "" || tlsPrivateKey == "" {
255
+ if tls . certificate == "" || tls . privateKey == "" {
268
256
return nil , errors .Wrap (err , "error getting certificate data from cloud; try again later" )
269
257
}
270
258
logger .Warnw ("failed to refresh certificate data; using cached for now" , "error" , err )
271
259
} else {
272
- tlsCertificate = certData .TLSCertificate
273
- tlsPrivateKey = certData .TLSPrivateKey
260
+ tls . certificate = certData .TLSCertificate
261
+ tls . privateKey = certData .TLSPrivateKey
274
262
}
275
263
}
276
264
@@ -291,14 +279,14 @@ func readFromCloud(
291
279
to .Cloud .ManagedBy = managedBy
292
280
to .Cloud .LocationSecret = locationSecret
293
281
to .Cloud .LocationSecrets = locationSecrets
294
- to .Cloud .TLSCertificate = tlsCertificate
295
- to .Cloud .TLSPrivateKey = tlsPrivateKey
282
+ to .Cloud .TLSCertificate = tls . certificate
283
+ to .Cloud .TLSPrivateKey = tls . privateKey
296
284
}
297
285
298
286
mergeCloudConfig (cfg )
299
287
// TODO(RSDK-1960): add more tests around config caching
300
- unprocessedConfig .Cloud .TLSCertificate = tlsCertificate
301
- unprocessedConfig .Cloud .TLSPrivateKey = tlsPrivateKey
288
+ unprocessedConfig .Cloud .TLSCertificate = tls . certificate
289
+ unprocessedConfig .Cloud .TLSPrivateKey = tls . privateKey
302
290
303
291
if err := storeToCache (cloudCfg .ID , unprocessedConfig ); err != nil {
304
292
logger .Errorw ("failed to cache config" , "error" , err )
@@ -307,6 +295,33 @@ func readFromCloud(
307
295
return cfg , nil
308
296
}
309
297
298
+ type tlsConfig struct {
299
+ certificate string
300
+ privateKey string
301
+ }
302
+
303
+ func (tls * tlsConfig ) readFromCache (id string , logger logging.Logger ) error {
304
+ cachedCfg , err := readFromCache (id )
305
+ switch {
306
+ case os .IsNotExist (err ):
307
+ logger .Warn ("No cached config, using cloud TLS config." )
308
+ case err != nil :
309
+ return err
310
+ case cachedCfg .Cloud == nil :
311
+ logger .Warn ("Cached config is not a cloud config, using cloud TLS config." )
312
+ default :
313
+ if err := cachedCfg .Cloud .ValidateTLS ("cloud" ); err != nil {
314
+ logger .Warn ("Detected failure to process the cached config when retrieving TLS config, clearing cache." )
315
+ clearCache (id )
316
+ return err
317
+ }
318
+
319
+ tls .certificate = cachedCfg .Cloud .TLSCertificate
320
+ tls .privateKey = cachedCfg .Cloud .TLSPrivateKey
321
+ }
322
+ return nil
323
+ }
324
+
310
325
// Read reads a config from the given file.
311
326
func Read (
312
327
ctx context.Context ,
0 commit comments