1
1
import { CLI , Hooks , Serverless , ServerlessPlugin } from "@xapp/serverless-plugin-types" ;
2
2
import { CloudFormation , SharedIniFileCredentials } from "aws-sdk" ;
3
- import * as AWS4 from "aws4" ;
4
3
import * as Path from "path" ;
4
+ import { AWSOptions } from "request" ;
5
5
import * as Request from "request-promise-native" ;
6
6
import * as AwsUtils from "./AwsUtils" ;
7
7
import Config , { Index , Template } from "./Config" ;
@@ -60,47 +60,35 @@ class Plugin implements ServerlessPlugin {
60
60
61
61
const endpoint = domain . startsWith ( "http" ) ? domain : `https://${ domain } ` ;
62
62
63
- const signer : Signer = ( this . config [ "aws-profile" ] ) ?
64
- awsProfileSigner ( domain , this . config [ "aws-profile" ] ) :
65
- doNotingSigner ( ) ;
63
+ const requestOptions : Partial < Request . Options > = { } ;
64
+ if ( this . config [ "aws-profile" ] ) {
65
+ const sharedIni = new SharedIniFileCredentials ( { profile : this . config [ "aws-profile" ] } ) ;
66
+ requestOptions . aws = {
67
+ key : sharedIni . accessKeyId ,
68
+ secret : sharedIni . secretAccessKey ,
69
+ sign_version : 4
70
+ } as AWSOptions ; // The typings are wrong. It need to include "key" and "sign_version"
71
+ }
66
72
67
73
this . cli . log ( "Setting up templates..." ) ;
68
- await setupTemplates ( endpoint , this . config . templates , signer ) ;
74
+ await setupTemplates ( endpoint , this . config . templates , requestOptions ) ;
69
75
this . cli . log ( "Setting up indices..." ) ;
70
- await setupIndices ( endpoint , this . config . indices , signer ) ;
76
+ await setupIndices ( endpoint , this . config . indices , requestOptions ) ;
71
77
this . cli . log ( "Elasticsearch setup complete." ) ;
72
78
}
73
79
}
74
80
75
- function doNotingSigner ( ) : Signer {
76
- return ( url , headers ) => headers ;
77
- }
78
-
79
- function awsProfileSigner ( domain : string , profile : string ) : Signer {
80
- const sharedIni = new SharedIniFileCredentials ( { profile } ) ;
81
- return ( url , headers ) => {
82
- const pathStart = url . indexOf ( domain ) + domain . length ;
83
- const path = url . slice ( pathStart ) ;
84
- const opts = {
85
- host : domain ,
86
- path
87
- } ;
88
- AWS4 . sign ( opts , { accessKeyId : sharedIni . accessKeyId , secretAccessKey : sharedIni . secretAccessKey } ) ;
89
- return { ...headers , opts} ;
90
- } ;
91
- }
92
-
93
81
/**
94
82
* Sets up all the indices in the given object.
95
83
* @param baseUrl The elasticsearch URL
96
84
* @param indices The indices to set up.
97
85
*/
98
- function setupIndices ( baseUrl : string , indices : Index [ ] = [ ] , signer ?: Signer ) {
86
+ function setupIndices ( baseUrl : string , indices : Index [ ] = [ ] , requestOptions : Partial < Request . Options > ) {
99
87
const setupPromises : PromiseLike < Request . FullResponse > [ ] = indices . map ( ( index ) => {
100
88
validateIndex ( index ) ;
101
89
const url = `${ baseUrl } /${ index . name } ` ;
102
90
const settings = require ( Path . resolve ( index . file ) ) ;
103
- return esPut ( url , settings , signer ) . catch ( ( e ) => {
91
+ return esPut ( url , settings , requestOptions ) . catch ( ( e ) => {
104
92
if ( e . error . error . type !== "resource_already_exists_exception" ) {
105
93
throw e ;
106
94
}
@@ -114,12 +102,12 @@ function setupIndices(baseUrl: string, indices: Index[] = [], signer?: Signer) {
114
102
* @param baseUrl The elasticsearch URL
115
103
* @param templates The templates to set up.
116
104
*/
117
- function setupTemplates ( baseUrl : string , templates : Template [ ] = [ ] , signer ?: Signer ) {
105
+ function setupTemplates ( baseUrl : string , templates : Template [ ] = [ ] , requestOptions ?: Partial < Request . Options > ) {
118
106
const setupPromises : PromiseLike < Request . FullResponse > [ ] = templates . map ( ( template ) => {
119
107
validateTemplate ( template ) ;
120
108
const url = `${ baseUrl } /_template/${ template . name } ` ;
121
109
const settings = require ( Path . resolve ( template . file ) ) ;
122
- return esPut ( url , settings , signer ) ;
110
+ return esPut ( url , settings , requestOptions ) ;
123
111
} ) ;
124
112
return Promise . all ( setupPromises ) ;
125
113
}
@@ -142,15 +130,14 @@ function validateTemplate(template: Template) {
142
130
}
143
131
}
144
132
145
- type Signer = ( url : string , headers : object ) => object ;
146
-
147
- function esPut ( url : string , settings : object , signer : Signer = ( ) => ( { } ) ) {
133
+ function esPut ( url : string , settings : object , requestOpts ?: Partial < Request . Options > ) {
148
134
const headers = {
149
135
"Content-Type" : "application/json" ,
150
136
} ;
151
137
return Request . put ( url , {
152
- headers : signer ( url , headers ) ,
153
- json : settings
138
+ headers,
139
+ json : settings ,
140
+ ...requestOpts
154
141
} ) ;
155
142
}
156
143
0 commit comments