Skip to content

Commit 26de9bf

Browse files
committed
migrate aws-node-sdk remaining test from sdk v2 parts to v3
Issue: CLDSRV-846
1 parent 33b1987 commit 26de9bf

File tree

3 files changed

+273
-254
lines changed

3 files changed

+273
-254
lines changed

bin/ensureServiceUser

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ const { errors } = require('arsenal');
88
const { program } = require('commander');
99
const werelogs = require('werelogs');
1010
const async = require('async');
11-
const { IAM } = require('aws-sdk');
11+
const {
12+
IAMClient,
13+
GetUserCommand,
14+
CreateUserCommand,
15+
GetUserPolicyCommand,
16+
PutUserPolicyCommand,
17+
ListAccessKeysCommand,
18+
CreateAccessKeyCommand,
19+
} = require('@aws-sdk/client-iam');
1220
const { version } = require('../package.json');
1321

1422
const systemPrefix = '/scality-internal/';
@@ -33,7 +41,7 @@ function generateUserPolicyDocument() {
3341
}
3442

3543
function createIAMClient(opts) {
36-
return new IAM({
44+
return new IAMClient({
3745
endpoint: opts.iamEndpoint,
3846
region: 'us-east-1',
3947
});
@@ -98,22 +106,18 @@ class UserHandler extends BaseHandler {
98106
resourceType = 'user';
99107

100108
async collect() {
101-
return this.iamClient
102-
.getUser({
103-
UserName: this.serviceName,
104-
})
105-
.promise()
106-
.then(res => res.User);
109+
const res = await this.iamClient.send(new GetUserCommand({
110+
UserName: this.serviceName,
111+
}));
112+
return res.User;
107113
}
108114

109115
async create() {
110-
return this.iamClient
111-
.createUser({
112-
UserName: this.serviceName,
113-
Path: systemPrefix,
114-
})
115-
.promise()
116-
.then(res => res.User);
116+
const res = await this.iamClient.send(new CreateUserCommand({
117+
UserName: this.serviceName,
118+
Path: systemPrefix,
119+
}));
120+
return res.User;
117121
}
118122

119123
conflicts(u) {
@@ -125,26 +129,22 @@ class UserPolicyHandler extends BaseHandler {
125129
resourceType = 'userPolicy';
126130

127131
async collect() {
128-
return this.iamClient
129-
.getUserPolicy({
130-
UserName: this.serviceName,
131-
PolicyName: this.serviceName,
132-
})
133-
.promise()
134-
.then(() => true);
132+
await this.iamClient.send(new GetUserPolicyCommand({
133+
UserName: this.serviceName,
134+
PolicyName: this.serviceName,
135+
}));
136+
return true;
135137
}
136138

137139
async create() {
138140
const doc = generateUserPolicyDocument();
139141

140-
return this.iamClient
141-
.putUserPolicy({
142-
UserName: this.serviceName,
143-
PolicyName: this.serviceName,
144-
PolicyDocument: JSON.stringify(doc),
145-
})
146-
.promise()
147-
.then(() => true);
142+
await this.iamClient.send(new PutUserPolicyCommand({
143+
UserName: this.serviceName,
144+
PolicyName: this.serviceName,
145+
PolicyDocument: JSON.stringify(doc),
146+
}));
147+
return true;
148148
}
149149

150150
conflicts() {
@@ -158,22 +158,18 @@ class AccessKeyHandler extends BaseHandler {
158158
async collect() {
159159
// if at least one key already exists, the script won't create a new key
160160
// and will display the list of existing keys
161-
return this.iamClient
162-
.listAccessKeys({
163-
UserName: this.serviceName,
164-
MaxItems: 100,
165-
})
166-
.promise()
167-
.then(res => res.AccessKeyMetadata);
161+
const res = await this.iamClient.send(new ListAccessKeysCommand({
162+
UserName: this.serviceName,
163+
MaxItems: 100,
164+
}));
165+
return res.AccessKeyMetadata;
168166
}
169167

170168
async create() {
171-
return this.iamClient
172-
.createAccessKey({
173-
UserName: this.serviceName,
174-
})
175-
.promise()
176-
.then(res => res.AccessKey);
169+
const res = await this.iamClient.send(new CreateAccessKeyCommand({
170+
UserName: this.serviceName,
171+
}));
172+
return res.AccessKey;
177173
}
178174

179175
conflicts() {
@@ -185,7 +181,7 @@ function collectResource(v, done) {
185181
v.collect()
186182
.then(res => done(null, res))
187183
.catch((err) => {
188-
if (err.code === 'NoSuchEntity') {
184+
if (err.name === 'NoSuchEntity') {
189185
return done();
190186
}
191187

tests/functional/aws-node-sdk/test/rateLimit/tooling.js

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,65 @@
11
const nodeFetch = require('node-fetch');
2-
const AWS = require('aws-sdk');
2+
const { SignatureV4 } = require('@aws-sdk/signature-v4');
3+
const { HttpRequest } = require('@aws-sdk/protocol-http');
4+
const { Sha256 } = require('@aws-crypto/sha256-js');
35
const xml2js = require('xml2js');
6+
const { URL } = require('url');
47
const { getCredentials } = require('../support/credentials');
58

69
const { config } = require('../../../../../lib/Config');
710

811
const skipIfRateLimitDisabled = config.rateLimiting.enabled ? describe : describe.skip;
912

13+
function buildHttpRequest(method, host, rawPath, body = '') {
14+
const endpoint = new URL(`http://${host}`);
15+
const target = new URL(rawPath, `http://${host}`);
16+
17+
const query = {};
18+
target.searchParams.forEach((value, key) => {
19+
if (Object.prototype.hasOwnProperty.call(query, key)) {
20+
const current = query[key];
21+
query[key] = Array.isArray(current) ? current.concat(value) : [current, value];
22+
} else {
23+
query[key] = value ?? '';
24+
}
25+
});
26+
27+
const request = new HttpRequest({
28+
method: method.toUpperCase(),
29+
protocol: endpoint.protocol,
30+
hostname: endpoint.hostname,
31+
port: endpoint.port ? Number(endpoint.port) : undefined,
32+
path: target.pathname,
33+
query: Object.keys(query).length ? query : undefined,
34+
headers: {
35+
host,
36+
},
37+
body: body || undefined,
38+
});
39+
40+
return { request, target };
41+
}
42+
1043
async function sendRateLimitRequest(method, host, path, body = '') {
11-
const service = 's3';
12-
const endpoint = new AWS.Endpoint(host);
13-
14-
const request = new AWS.HttpRequest(endpoint);
15-
request.method = method.toUpperCase();
16-
request.path = path;
17-
request.body = body;
18-
request.headers.Host = host;
19-
request.headers['X-Amz-Date'] = new Date().toISOString().replace(/[:\-]|\.\d{3}/g, '');
20-
const sha256hash = AWS.util.crypto.sha256(request.body || '', 'hex');
21-
request.headers['X-Amz-Content-SHA256'] = sha256hash;
22-
request.region = 'us-east-1';
23-
24-
const signer = new AWS.Signers.V4(request, service);
44+
const { request, target } = buildHttpRequest(method, host, path, body);
45+
2546
const credentials = getCredentials('lisa');
26-
const awsCredentials = new AWS.Credentials(
27-
credentials.accessKeyId,
28-
credentials.secretAccessKey
29-
);
30-
signer.addAuthorization(awsCredentials, new Date());
47+
const signer = new SignatureV4({
48+
credentials,
49+
region: 'us-east-1',
50+
service: 's3',
51+
sha256: Sha256,
52+
});
53+
const signedRequest = await signer.sign(request);
3154

32-
const url = `http://${host}${path}`;
55+
const url = target.href;
3356
const options = {
34-
method: request.method,
35-
headers: request.headers,
57+
method: signedRequest.method,
58+
headers: signedRequest.headers,
3659
};
3760

38-
if (method !== 'GET' && method !== 'DELETE') {
39-
options.body = request.body;
61+
if (signedRequest.body && method !== 'GET' && method !== 'DELETE') {
62+
options.body = signedRequest.body;
4063
}
4164

4265
const response = await nodeFetch(url, options);

0 commit comments

Comments
 (0)