Skip to content

Commit f24da75

Browse files
committed
migrate latest aws sdk v2 parts to aws sdk v3
1 parent 33b1987 commit f24da75

6 files changed

Lines changed: 313 additions & 301 deletions

File tree

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 && (err.code === 'NoSuchEntity' || err.name === 'NoSuchEntity')) {
189185
return done();
190186
}
191187

docs/CLIENTS.rst

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,25 @@ JavaScript
124124

125125
.. code:: javascript
126126
127-
const AWS = require('aws-sdk');
128-
129-
const s3 = new AWS.S3({
130-
accessKeyId: 'accessKey1',
131-
secretAccessKey: 'verySecretKey1',
132-
endpoint: 'localhost:8000',
133-
sslEnabled: false,
134-
s3ForcePathStyle: true,
127+
const { S3Client, ListBucketsCommand } = require('@aws-sdk/client-s3');
128+
129+
const s3 = new S3Client({
130+
region: 'us-east-1',
131+
endpoint: 'http://localhost:8000',
132+
forcePathStyle: true,
133+
credentials: {
134+
accessKeyId: 'accessKey1',
135+
secretAccessKey: 'verySecretKey1',
136+
},
135137
});
136138
139+
async function listBuckets() {
140+
const result = await s3.send(new ListBucketsCommand({}));
141+
console.log(result.Buckets);
142+
}
143+
144+
listBuckets().catch(console.error);
145+
137146
JAVA
138147
~~~~
139148

docs/GETTING_STARTED.rst

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,13 @@ if you are using the AWS SDK for JavaScript, instantiate your client like this:
247247

248248
.. code:: js
249249
250-
const s3 = new aws.S3({
251-
endpoint: 'http://127.0.0.1:8000',
252-
s3ForcePathStyle: true,
253-
});
250+
const { S3Client } = require('@aws-sdk/client-s3');
251+
252+
const s3 = new S3Client({
253+
endpoint: 'http://127.0.0.1:8000',
254+
forcePathStyle: true,
255+
region: 'us-east-1',
256+
});
254257
255258
Setting Your Own Access and Secret Key Pairs
256259
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -379,47 +382,44 @@ SSL certificates.
379382
Test the Config
380383
^^^^^^^^^^^^^^^
381384

382-
If aws-sdk is not installed, run ``$> yarn install aws-sdk``.
385+
If the AWS SDK for JavaScript v3 packages are not installed locally, run ``$> yarn add @aws-sdk/client-s3 @aws-sdk/node-http-handler``.
383386

384387
Paste the following script into a file named "test.js":
385388

386389
.. code:: js
387390
388-
const AWS = require('aws-sdk');
389-
const fs = require('fs');
390-
const https = require('https');
391-
392-
const httpOptions = {
393-
agent: new https.Agent({
394-
// path on your host of the self-signed certificate
395-
ca: fs.readFileSync('./ca.crt', 'ascii'),
396-
}),
397-
};
398-
399-
const s3 = new AWS.S3({
400-
httpOptions,
401-
accessKeyId: 'accessKey1',
402-
secretAccessKey: 'verySecretKey1',
403-
// The endpoint must be s3.scality.test, else SSL will not work
404-
endpoint: 'https://s3.scality.test:8000',
405-
sslEnabled: true,
406-
// With this setup, you must use path-style bucket access
407-
s3ForcePathStyle: true,
408-
});
409-
410-
const bucket = 'cocoriko';
411-
412-
s3.createBucket({ Bucket: bucket }, err => {
413-
if (err) {
414-
return console.log('err createBucket', err);
415-
}
416-
return s3.deleteBucket({ Bucket: bucket }, err => {
417-
if (err) {
418-
return console.log('err deleteBucket', err);
419-
}
420-
return console.log('SSL is cool!');
421-
});
422-
});
391+
const { S3Client, CreateBucketCommand, DeleteBucketCommand } = require('@aws-sdk/client-s3');
392+
const { NodeHttpHandler } = require('@aws-sdk/node-http-handler');
393+
const fs = require('fs');
394+
const https = require('https');
395+
396+
const httpsAgent = new https.Agent({
397+
// path on your host of the self-signed certificate
398+
ca: fs.readFileSync('./ca.crt', 'ascii'),
399+
});
400+
401+
const s3 = new S3Client({
402+
requestHandler: new NodeHttpHandler({ httpsAgent }),
403+
credentials: {
404+
accessKeyId: 'accessKey1',
405+
secretAccessKey: 'verySecretKey1',
406+
},
407+
endpoint: 'https://s3.scality.test:8000',
408+
forcePathStyle: true,
409+
region: 'us-east-1',
410+
});
411+
412+
const bucket = 'cocoriko';
413+
414+
(async () => {
415+
try {
416+
await s3.send(new CreateBucketCommand({ Bucket: bucket }));
417+
await s3.send(new DeleteBucketCommand({ Bucket: bucket }));
418+
console.log('SSL is cool!');
419+
} catch (err) {
420+
console.error('error running sample', err);
421+
}
422+
})();
423423
424424
Now run this script with:
425425

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"@smithy/node-http-handler": "^3.0.0",
3333
"arsenal": "git+https://github.com/scality/arsenal#8.3.1",
3434
"async": "2.6.4",
35-
"aws-sdk": "^2.1692.0",
3635
"bucketclient": "scality/bucketclient#8.2.7",
3736
"bufferutil": "^4.0.8",
3837
"commander": "^12.1.0",

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
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

10-
async function sendRateLimitRequest(method, host, path, body = '') {
11-
const service = 's3';
12-
const endpoint = new AWS.Endpoint(host);
13+
function buildHttpRequest(method, host, path, body = '') {
14+
const url = new URL(`http://${host}`);
15+
return new HttpRequest({
16+
method: method.toUpperCase(),
17+
protocol: url.protocol,
18+
hostname: url.hostname,
19+
port: url.port ? Number(url.port) : undefined,
20+
path,
21+
headers: {
22+
host,
23+
},
24+
body: body || undefined,
25+
});
26+
}
1327

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';
28+
async function sendRateLimitRequest(method, host, path, body = '') {
29+
const request = buildHttpRequest(method, host, path, body);
2330

24-
const signer = new AWS.Signers.V4(request, service);
2531
const credentials = getCredentials('lisa');
26-
const awsCredentials = new AWS.Credentials(
27-
credentials.accessKeyId,
28-
credentials.secretAccessKey
29-
);
30-
signer.addAuthorization(awsCredentials, new Date());
32+
const signer = new SignatureV4({
33+
credentials,
34+
region: 'us-east-1',
35+
service: 's3',
36+
sha256: Sha256,
37+
});
38+
const signedRequest = await signer.sign(request);
3139

3240
const url = `http://${host}${path}`;
3341
const options = {
34-
method: request.method,
35-
headers: request.headers,
42+
method: signedRequest.method,
43+
headers: signedRequest.headers,
3644
};
3745

38-
if (method !== 'GET' && method !== 'DELETE') {
39-
options.body = request.body;
46+
if (signedRequest.body && method !== 'GET' && method !== 'DELETE') {
47+
options.body = signedRequest.body;
4048
}
4149

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

0 commit comments

Comments
 (0)