Skip to content

Commit 4ff6413

Browse files
committed
fix: Fixing aws signing
1 parent d972a43 commit 4ff6413

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

src/Plugin.ts

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CLI, Hooks, Serverless, ServerlessPlugin} from "@xapp/serverless-plugin-types";
22
import { CloudFormation, SharedIniFileCredentials } from "aws-sdk";
3-
import * as AWS4 from "aws4";
43
import * as Path from "path";
4+
import { AWSOptions } from "request";
55
import * as Request from "request-promise-native";
66
import * as AwsUtils from "./AwsUtils";
77
import Config, { Index, Template } from "./Config";
@@ -60,47 +60,35 @@ class Plugin implements ServerlessPlugin {
6060

6161
const endpoint = domain.startsWith("http") ? domain : `https://${domain}`;
6262

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+
}
6672

6773
this.cli.log("Setting up templates...");
68-
await setupTemplates(endpoint, this.config.templates, signer);
74+
await setupTemplates(endpoint, this.config.templates, requestOptions);
6975
this.cli.log("Setting up indices...");
70-
await setupIndices(endpoint, this.config.indices, signer);
76+
await setupIndices(endpoint, this.config.indices, requestOptions);
7177
this.cli.log("Elasticsearch setup complete.");
7278
}
7379
}
7480

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-
9381
/**
9482
* Sets up all the indices in the given object.
9583
* @param baseUrl The elasticsearch URL
9684
* @param indices The indices to set up.
9785
*/
98-
function setupIndices(baseUrl: string, indices: Index[] = [], signer?: Signer) {
86+
function setupIndices(baseUrl: string, indices: Index[] = [], requestOptions: Partial<Request.Options>) {
9987
const setupPromises: PromiseLike<Request.FullResponse>[] = indices.map((index) => {
10088
validateIndex(index);
10189
const url = `${baseUrl}/${index.name}`;
10290
const settings = require(Path.resolve(index.file));
103-
return esPut(url, settings, signer).catch((e) => {
91+
return esPut(url, settings, requestOptions).catch((e) => {
10492
if (e.error.error.type !== "resource_already_exists_exception") {
10593
throw e;
10694
}
@@ -114,12 +102,12 @@ function setupIndices(baseUrl: string, indices: Index[] = [], signer?: Signer) {
114102
* @param baseUrl The elasticsearch URL
115103
* @param templates The templates to set up.
116104
*/
117-
function setupTemplates(baseUrl: string, templates: Template[] = [], signer?: Signer) {
105+
function setupTemplates(baseUrl: string, templates: Template[] = [], requestOptions?: Partial<Request.Options>) {
118106
const setupPromises: PromiseLike<Request.FullResponse>[] = templates.map((template) => {
119107
validateTemplate(template);
120108
const url = `${baseUrl}/_template/${template.name}`;
121109
const settings = require(Path.resolve(template.file));
122-
return esPut(url, settings, signer);
110+
return esPut(url, settings, requestOptions);
123111
});
124112
return Promise.all(setupPromises);
125113
}
@@ -142,15 +130,14 @@ function validateTemplate(template: Template) {
142130
}
143131
}
144132

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>) {
148134
const headers = {
149135
"Content-Type": "application/json",
150136
};
151137
return Request.put(url, {
152-
headers: signer(url, headers),
153-
json: settings
138+
headers,
139+
json: settings,
140+
...requestOpts
154141
});
155142
}
156143

0 commit comments

Comments
 (0)