@@ -31,6 +31,7 @@ module.exports = class BaseDownloadController extends CompositeController {
3131
3232 async added ( ) {
3333 let requests = objectPath . get ( this . data , [ 'object' , 'spec' , 'requests' ] , [ ] ) ;
34+
3435 // when failure to download optional resource occurs, should continue to download other requests, but shouldnt reconcile children
3536 let optionalResourceFailure = 0 ;
3637 let lastModifiedArray = objectPath . get ( this . data , [ 'object' , 'status' , 'last-modified' ] , [ ] ) ;
@@ -60,6 +61,21 @@ module.exports = class BaseDownloadController extends CompositeController {
6061 objectPath . set ( reqOpt , 'headers.If-Modified-Since' , objectPath . get ( imsObj , 'last-modified' ) ) ;
6162 }
6263
64+ try {
65+ reqOpt = await this . _fetchHeaderSecrets ( reqOpt ) ;
66+ } catch ( e ) {
67+ // error fetching header secrets
68+ if ( optional ) {
69+ ++ optionalResourceFailure ;
70+ this . log . warn ( e . message ) ;
71+ this . updateRazeeLogs ( 'warn' , { controller : 'BaseDownload' , warn : e . message , url : url } ) ;
72+ this . log . debug ( `skipping download for ${ url } ` ) ;
73+ continue ; // shouldnt continue to try to download if unable to get secret headers
74+ } else {
75+ return Promise . reject ( e . message ) ;
76+ }
77+ }
78+
6379 try {
6480 let res = await this . download ( reqOpt ) ;
6581 if ( res . statusCode >= 200 && res . statusCode < 300 ) {
@@ -157,6 +173,51 @@ module.exports = class BaseDownloadController extends CompositeController {
157173 }
158174 }
159175
176+ async _fetchHeaderSecrets ( requestOptions ) {
177+ requestOptions = clone ( requestOptions ) ;
178+ let headers = objectPath . get ( requestOptions , 'headers' ) ;
179+ if ( headers ) {
180+ for ( let [ hKey , hObject ] of Object . entries ( headers ) ) {
181+ let secretRef = objectPath . get ( hObject , 'valueFrom.secretKeyRef' ) ;
182+ if ( secretRef ) {
183+ let secretName = objectPath . get ( secretRef , 'name' ) ;
184+ let secretNamespace = objectPath . get ( secretRef , 'namespace' , this . namespace ) ;
185+ let secretKey = objectPath . get ( secretRef , 'key' ) ;
186+ try {
187+ objectPath . set ( headers , [ hKey ] , await this . _getSecretData ( secretName , secretKey , secretNamespace ) ) ;
188+ } catch ( e ) {
189+ throw Error ( `Unable to fetch header secret data. { name: ${ secretName } , namespace: ${ secretNamespace } , key: ${ secretKey } }: ${ objectPath . get ( e , 'error.message' ) } ` ) ;
190+ }
191+ }
192+ }
193+ }
194+ return requestOptions ;
195+ }
196+
197+ async _getSecretData ( name , key , ns ) {
198+ ns = ns || this . namespace ;
199+ let res = await this . kubeResourceMeta . request ( { uri : `/api/v1/namespaces/${ ns } /secrets/${ name } ` , json : true } ) ;
200+ let secret = Buffer . from ( objectPath . get ( res , [ 'data' , key ] , '' ) , 'base64' ) . toString ( ) ;
201+ if ( secret === '' ) {
202+ throw {
203+ name : 'StatusCodeError' ,
204+ statusCode : 404 ,
205+ message : `404 - key "${ key } " not found in secret "${ name } ", in namespace "${ ns } "` ,
206+ error : {
207+ kind : 'Status' ,
208+ apiVersion : 'v1' ,
209+ metadata : { } ,
210+ status : 'Failure' ,
211+ message : `key "${ key } " not found in secret "${ name } ", in namespace "${ ns } "` ,
212+ reason : 'NotFound' ,
213+ details : { 'name' : name , 'namespace' : ns , 'kind' : 'secrets' , 'key' : key } ,
214+ code : 404
215+ }
216+ } ;
217+ }
218+ return secret ;
219+ }
220+
160221 async _saveChild ( child ) {
161222 let res = await this . applyChild ( child ) ;
162223 if ( ! res . statusCode || res . statusCode < 200 || res . statusCode >= 300 ) {
0 commit comments