import fs from 'fs' ; import https from 'https' ; import { Logger } from '@terascope/utils' ; import { promisifyAll } from 'bluebird' ; function create ( customConfig , logger ) { const S3 = require ( 'aws-sdk/clients/s3' ) ; logger . info ( `Using S3 endpoint: ${ customConfig . endpoint }` ) ; if ( customConfig . sslEnabled ) { if ( customConfig . certLocation . length === 0 ) { throw new Error ( `Must provide a certificate for S3 endpoint ${ customConfig . endpoint } since SSL is enabled` ) ; } const certs = [ fs . readFileSync ( customConfig . certLocation ) ] ; if ( ! customConfig . httpOptions ) customConfig . httpOptions = { } ; customConfig . httpOptions . agent = new https . Agent ( { rejectUnauthorized : true , ca : certs } ) ; } const client = new S3 ( customConfig ) ; return { client : promisifyAll ( client , { suffix : '_Async' } ) } ; } export default { create , config_schema ( ) { return { endpoint : { doc : 'Target S3 endpoint' , default : '127.0.0.1:80' } , accessKeyId : { doc : '' , default : null , format : String } , secretAccessKey : { doc : '' , default : null , format : String } , region : { doc : '' , default : 'us-east-1' , format : String } , maxRetries : { doc : '' , default : 3 , format : Number } , maxRedirects : { doc : '' , default : 10 , format : Number } , sslEnabled : { doc : '' , default : true , format : Boolean } , certLocation : { doc : 'Location of ssl cert. Must be provided if `sslEnabled` is true' , default : '' , format : String } , s3ForcePathStyle : { doc : '' , default : false , format : Boolean } , s3BucketEndpoint : { doc : '' , default : false , format : Boolean } } ; } } ;