forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.validation.ts
More file actions
207 lines (167 loc) · 4.27 KB
/
Copy pathenv.validation.ts
File metadata and controls
207 lines (167 loc) · 4.27 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Environment validation and configuration checks
import { Type } from 'class-transformer';
import { IsString, IsNumber, IsBoolean, IsOptional, IsArray, ValidateNested } from 'class-validator';
export enum Environment {
DEVELOPMENT = 'development',
STAGING = 'staging',
PRODUCTION = 'production',
TEST = 'test',
}
/**
* Required environment variables for production
*/
class RequiredEnvVars {
@IsString()
NODE_ENV!: Environment;
@IsString()
DATABASE_HOST!: string;
@IsNumber()
DATABASE_PORT!: number;
@IsString()
DATABASE_USERNAME!: string;
@IsString()
DATABASE_PASSWORD!: string;
@IsString()
DATABASE_NAME!: string;
@IsString()
JWT_SECRET!: string;
@IsString()
REDIS_URL!: string;
}
/**
* Optional but recommended environment variables
*/
class OptionalEnvVars {
@IsOptional()
@IsNumber()
PORT?: number;
@IsOptional()
@IsString()
KAFKA_BROKERS?: string;
@IsOptional()
@IsString()
CLICKHOUSE_HOST?: string;
@IsOptional()
@IsNumber()
CLICKHOUSE_PORT?: number;
@IsOptional()
@IsString()
CLICKHOUSE_USER?: string;
@IsOptional()
@IsString()
CLICKHOUSE_PASSWORD?: string;
@IsOptional()
@IsString()
STELLAR_NETWORK?: string;
@IsOptional()
@IsString()
STELLAR_SECRET_KEY?: string;
@IsOptional()
@IsString()
SMTP_HOST?: string;
@IsOptional()
@IsNumber()
SMTP_PORT?: number;
@IsOptional()
@IsString()
SMTP_USER?: string;
@IsOptional()
@IsString()
SMTP_PASSWORD?: string;
}
/**
* Combined environment config
*/
export class EnvironmentConfig {
@ValidateNested()
@Type(() => RequiredEnvVars)
required!: RequiredEnvVars;
@ValidateNested()
@Type(() => OptionalEnvVars)
optional!: OptionalEnvVars;
@IsOptional()
@IsArray()
allowedOrigins?: string[];
@IsOptional()
@IsBoolean()
enableStrictMode?: boolean;
}
interface ValidationResult {
isValid: boolean;
errors: string[];
warnings: string[];
}
/**
* Validate environment configuration
* Returns validation result with any errors or warnings
*/
export function validateEnvironment(config: Record<string, unknown>): ValidationResult {
const errors: string[] = [];
const warnings: string[] = [];
// Check required variables
const requiredVars = [
'NODE_ENV',
'DATABASE_HOST',
'DATABASE_PORT',
'DATABASE_USERNAME',
'DATABASE_PASSWORD',
'DATABASE_NAME',
'JWT_SECRET',
'REDIS_URL',
];
for (const varName of requiredVars) {
if (!config[varName]) {
errors.push(`Missing required environment variable: ${varName}`);
}
}
// Production-specific checks
const nodeEnv = config.NODE_ENV as Environment;
if (nodeEnv === Environment.PRODUCTION) {
// Check for default/dangerous values
if (config.JWT_SECRET === 'your-secret-key' || (config.JWT_SECRET as string)?.length < 32) {
errors.push('JWT_SECRET must be at least 32 characters in production');
}
// Check for debug mode
if (config.DEBUG === 'true') {
warnings.push('DEBUG is enabled - not recommended for production');
}
// Check CORS settings
if (config.CORS_ORIGIN === 'true' || config.CORS_ORIGIN === '*') {
errors.push('CORS origin cannot be wildcard (*) in production');
}
// Check database host (should not be localhost)
if (config.DATABASE_HOST === 'localhost' || config.DATABASE_HOST === '127.0.0.1') {
warnings.push('DATABASE_HOST is localhost - ensure this is intentional for production');
}
// Check for SSL
if (!config.DATABASE_SSL || config.DATABASE_SSL === 'false') {
warnings.push('Database SSL is disabled - consider enabling for production');
}
}
// Check for deprecated variables
const deprecatedVars = [
{ old: 'REDIS_HOST', new: 'REDIS_URL' },
{ old: 'KAFKA_URL', new: 'KAFKA_BROKERS' },
];
for (const { old, new: newVar } of deprecatedVars) {
if (config[old]) {
warnings.push(`Environment variable ${old} is deprecated, use ${newVar} instead`);
}
}
return {
isValid: errors.length === 0,
errors,
warnings,
};
}
/**
* Get environment info for health checks
*/
export function getEnvironmentInfo() {
return {
nodeEnv: process.env.NODE_ENV || 'development',
nodeVersion: process.version,
platform: process.platform,
timestamp: new Date().toISOString(),
};
}