-
Notifications
You must be signed in to change notification settings - Fork 0
/
minio.js
86 lines (66 loc) · 2.31 KB
/
minio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require('express')
const crypto = require('crypto')
const got = require('got')
const argon2 = require('argon2')
const { signV4 } = require('minio/dist/main/signing')
const { makeDateLong } = require('minio/dist/main/helpers')
const {
PORT,
MINIO_ACCESS_KEY,
MINIO_SECRET_KEY,
MINIO_ENDPOINT,
MINIO_PORT,
} = process.env
const signRequest = (opts, payload = '', { accessKey, secretKey }) => {
let reqOptions = {
...opts,
}
const date = new Date()
const sha256sum = crypto.createHash('sha256').update(payload).digest('hex')
reqOptions.headers['x-amz-date'] = makeDateLong(date)
reqOptions.headers['x-amz-content-sha256'] = sha256sum
const authorization = signV4(reqOptions, accessKey, secretKey, region, date)
reqOptions.headers.authorization = authorization
return reqOptions
}
const IV_LENGTH = 16
function decrypt(text, key) {
let textParts = text.split(':');
let iv = Buffer.from(textParts.shift(), 'hex');
let encryptedText = Buffer.from(textParts.join(':'), 'hex');
let decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(key), iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}
const minioFetch = (endpoint) => {
const signedOpts = signRequest({
path: endpoint,
headers: {
'user-agent': 'got/1.0',
},
}, '', { accessKey: MINIO_ACCESS_KEY, secretKey: MINIO_SECRET_KEY })
return got(`http://${MINIO_ENDPOINT}:${MINIO_PORT}${endpoint}`, signedOpts)
}
const minioFetchPost = (endpoint, payload) => {
const signedOpts = signRequest({
path: endpoint,
body: payload,
headers: {
'user-agent': 'got/1.0',
},
}, payload, { accessKey: MINIO_ACCESS_KEY, secretKey: MINIO_SECRET_KEY })
return got.post(`http://${MINIO_ENDPOINT}:${MINIO_PORT}${endpoint}`, signedOpts)
}
const region = 'us-east-1'
const app = express()
const minioGet = endpoint => minioFetch(endpoint)
const minioPost = (endpoint, body) => minioFetchPost(endpoint, JSON.stringify(body))
const listUsers = () => minioGet('/minio/admin/v2/list-users').then(r => r.text()).then(async cipherText => {
const key = await argon2.hash(MINIO_SECRET_KEY)
return decrypt(cipherText, key)
})
listUsers().then(console.log).catch(console.error)
app.listen({ port: PORT }, () => {
console.log(`🚀 Server ready at ${PORT}`)
})