@@ -6,6 +6,8 @@ const request = require('request');
6
6
const ndjson = require ( 'ndjson' ) ;
7
7
const MAX_LINES_SMALL = 100 ;
8
8
const MAX_LINES_BIG = 1000 ;
9
+ const jwt = require ( 'jsonwebtoken' ) ;
10
+ const uuid = require ( 'uuid' ) ;
9
11
10
12
const logger = require ( 'screwdriver-logger' ) ;
11
13
@@ -144,7 +146,7 @@ module.exports = config => ({
144
146
notes : 'Returns the logs for a step' ,
145
147
tags : [ 'api' , 'builds' , 'steps' , 'log' ] ,
146
148
auth : {
147
- strategies : [ 'token' ] ,
149
+ strategies : [ 'token' , 'session' ] ,
148
150
scope : [ 'user' , 'pipeline' , 'build' ]
149
151
} ,
150
152
plugins : {
@@ -156,7 +158,6 @@ module.exports = config => ({
156
158
const { stepFactory } = req . server . app ;
157
159
const buildId = req . params . id ;
158
160
const stepName = req . params . name ;
159
- const { headers } = req ;
160
161
161
162
return stepFactory
162
163
. get ( { buildId, name : stepName } )
@@ -174,10 +175,28 @@ module.exports = config => ({
174
175
175
176
const isDone = stepModel . code !== undefined ;
176
177
const baseUrl = `${ config . ecosystem . store } /v1/builds/${ buildId } /${ stepName } /log` ;
177
- const authToken = headers . authorization ;
178
- const { sort } = req . query ;
179
- const pagesToLoad = req . query . pages ;
180
- const linesFrom = req . query . from ;
178
+ const authToken = jwt . sign (
179
+ {
180
+ buildId,
181
+ stepName,
182
+ scope : [ 'user' ]
183
+ } ,
184
+ config . authConfig . jwtPrivateKey ,
185
+ {
186
+ algorithm : 'RS256' ,
187
+ expiresIn : '5s' ,
188
+ jwtid : uuid . v4 ( )
189
+ }
190
+ ) ;
191
+ const { sort, type } = req . query ;
192
+ let pagesToLoad = req . query . pages ;
193
+ let linesFrom = req . query . from ;
194
+
195
+ if ( type === 'download' && isDone ) {
196
+ // 100 lines per page
197
+ pagesToLoad = Math . ceil ( stepModel . lines / 100 ) ;
198
+ linesFrom = 0 ;
199
+ }
181
200
182
201
// eslint-disable-next-line max-len
183
202
return getMaxLines ( { baseUrl, authToken } )
@@ -191,9 +210,22 @@ module.exports = config => ({
191
210
maxLines
192
211
} )
193
212
)
194
- . then ( ( [ lines , morePages ] ) =>
195
- h . response ( lines ) . header ( 'X-More-Data' , ( morePages || ! isDone ) . toString ( ) )
196
- ) ;
213
+ . then ( ( [ lines , morePages ] ) => {
214
+ if ( type !== 'download' ) {
215
+ return h . response ( lines ) . header ( 'X-More-Data' , ( morePages || ! isDone ) . toString ( ) ) ;
216
+ }
217
+
218
+ let res = '' ;
219
+
220
+ for ( let i = 0 ; i < lines . length ; i += 1 ) {
221
+ res = `${ res } ${ lines [ i ] . m } \n` ;
222
+ }
223
+
224
+ return h
225
+ . response ( res )
226
+ . type ( 'text/plain' )
227
+ . header ( 'content-disposition' , `attachment; filename="${ stepName } -log.txt"` ) ;
228
+ } ) ;
197
229
} )
198
230
. catch ( err => {
199
231
throw err ;
0 commit comments