-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.js
More file actions
181 lines (159 loc) · 4.13 KB
/
Copy pathcommon.js
File metadata and controls
181 lines (159 loc) · 4.13 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const functionName = process.env.FUNCTION_TARGET || null;
const env = process.env.ENV || null;
const country = process.env.COUNTRY || null;
const project = process.env.PROJECT || null;
if(functionName === null)
{
throw new Error('env var not defined : FUNCTION_TARGET');
}
if( env === null)
{
throw new Error('env var not defined : ENV' );
}
if( country === null)
{
throw new Error('env var not defined : COUNTRY' );
}
if( project === null)
{
throw new Error('env var not defined : PROJECT' );
}
const {SecretManagerServiceClient} = require('@google-cloud/secret-manager');
const secretManagerServiceClient = new SecretManagerServiceClient();
const { Logging } = require('@google-cloud/logging');
const logging = new Logging();
const logger = logging.log(functionName);
async function getSecret(secretName){
// Access the secret.
let secretPath = "projects/"+project.toLowerCase()+"-"+country+"-"+env+"/secrets/"+secretName+"/versions/latest";
logDebug("accessing secret with path "+secretPath);
const [accessResponse] = await secretManagerServiceClient.accessSecretVersion({name: secretPath});
return accessResponse.payload.data.toString('utf8');
}
function getProjectName()
{
return (project+"-"+country+"-"+env).toLowerCase();
}
function setCors(request, response)
{
response.set('Access-Control-Allow-Origin', "*");
response.set('Access-Control-Allow-Methods', 'GET, POST');
//respond to CORS preflight requests
if (request.method === 'OPTIONS')
{
response.status(204).send('');
return true;
}
return false;
}
const METADATA = {
resource: {
type: 'cloud_function',
labels: {
function_name: functionName,
region: 'europe-west1'
}
}
}
;
async function logCritical(message, extraData)
{
const logData = {
data: extraData,
severity: 'critical',
message: message
};
try
{
return logger.critical(logger.entry(METADATA, logData));
}
catch(exception)
{//fall back ton console.error
console.error('error while logging to stack driver', [logData,exception]);
}
}
async function logError(message, extraData)
{
const logData = {
data: extraData,
severity: 'error',
message: message
};
try
{
return logger.error(logger.entry(METADATA, logData));
}
catch(exception)
{//fall back ton console.error
console.error('error while logging to stack driver', [logData,exception]);
}
}
async function logWarn(message, extraData)
{
const logData = {
data: extraData,
severity: 'warning',
message: message
};
try
{
return logger.warn(logger.entry(METADATA, logData));
}
catch(exception)
{//fall back ton console.error
console.warn('error while logging to stack driver', [logData,exception]);
}
}
async function logDebug(message, extraData)
{
const logData = {
data: extraData,
severity: 'debug',
message: message
};
try
{
return logger.debug(logger.entry(METADATA, logData));
}
catch(exception)
{//fall back ton console.error
console.debug('error while logging to stack driver', [logData,exception]);
}
}
async function logInfo( message, extraData)
{
const logData = {
data: extraData,
severity: 'info',
message: message
};
try
{
return logger.info(logger.entry(METADATA, logData));
}
catch(exception)
{//fall back ton console.error
console.info('error while logging to stack driver', [logData,exception]);
}
}
function handleFirestoreError(err){
if (err && err.name === 'PartialFailureError') {
if (err.errors && err.errors.length > 0) {
common.logError('Insert errors:');
err.errors.forEach(err => common.logError(err));
}
} else {
common.logError('ERROR:', err);
}
}
module.exports = {
getProjectName : getProjectName,
getSecret : getSecret,
setCors : setCors ,
logError : logError,
logWarn : logWarn,
logInfo : logInfo,
logDebug : logDebug,
handleFirestoreError:handleFirestoreError
};