-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
106 lines (100 loc) · 3.54 KB
/
index.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Copyright (c) 2020-present, Nimbella, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var redis = require('redis');
var bluebird = require('bluebird');
const { getStorageProvider } = require('@nimbella/storage');
function makeRedisClient() {
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
const redisHost = process.env['__NIM_REDIS_IP'];
if (!redisHost || redisHost.length == 0) {
throw new Error('Key-Value store is not available');
}
const redisPassword = process.env['__NIM_REDIS_PASSWORD'];
if (!redisPassword || redisPassword.length == 0) {
throw new Error('Key-Value store password is not available');
}
const redisParam = {port: 6379, host: redisHost};
const client = redis.createClient(redisParam);
if (client == null) {
throw new Error('Error creating redis client');
}
client.auth(redisPassword, function (err, reply) {
if (err) {
throw new Error('Error authenticating redis client' + reply);
}
});
return client;
}
// The current contract of makeStorageClientis to return a StorageClient handle.
// This works for all object store implementations (currently GCS and S3)
function makeStorageClient(web = false) {
const rawCreds = process.env['__NIM_STORAGE_KEY'];
if (!rawCreds || rawCreds.length == 0) {
throw new Error('Object store credentials are not available');
}
const namespace = process.env['__OW_NAMESPACE'];
const apiHost = process.env['__OW_API_HOST'];
if (!namespace || !apiHost) {
throw new Error(
'Not enough information in the environment to build an object store client'
);
}
let parsedCreds = undefined;
try {
parsedCreds = JSON.parse(rawCreds);
} catch {
throw new Error(
'Object store credentials could not be parsed'
);
}
const provider = parsedCreds.provider || '@nimbella/storage-gcs'
const providerImpl = getStorageProvider(provider)
const creds = providerImpl.prepareCredentials(parsedCreds)
return providerImpl.getClient(namespace, apiHost, web, creds)
}
// Legacy storage client that used to only support Google Cloud Storage.
// This now supports any provider depending on the storage credentials.
async function legacyMakeStorageClient(web = false) {
const handle = makeStorageClient(web)
return handle.getImplementation()
}
async function makeSqlClient() {
const mysql = require('mysql2/promise');
if (!process.env.__NIM_SQL_KEY) {
throw new Error('Sql credentials are not available');
}
const creds = JSON.parse(process.env.__NIM_SQL_KEY);
return await mysql.createConnection({
host: creds.host,
user: creds.user,
database: creds.database,
password: creds.password,
ssl: {
ca: creds.serverCaCert,
cert: creds.clientCert,
key: creds.clientKey
}
});
}
module.exports = {
redis: makeRedisClient,
// Legacy function, returns Promise<Bucket>
storage: legacyMakeStorageClient,
// New version of the function, returns the more abstract type StorageClient
storageClient: makeStorageClient,
mysql: makeSqlClient
};