-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathValidations.cs
More file actions
247 lines (189 loc) · 8.96 KB
/
Validations.cs
File metadata and controls
247 lines (189 loc) · 8.96 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
namespace ServiceControl.Config.Validation
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using FluentValidation;
using ServiceControlInstaller.Engine.Ports;
public static class Validations
{
public static IRuleBuilderOptions<T, string> ValidPort<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.Must((t, port) =>
{
if (int.TryParse(port, out var result))
{
return result is >= 1 and <= 49151;
}
return false;
})
.WithMessage(MSG_USE_PORTS_IN_RANGE);
}
public static IRuleBuilderOptions<T, string> PortAvailable<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.Must((t, port) =>
{
if (int.TryParse(port, out var result))
{
var portNotInUse = !PortUtils.IsPortInUse(result);
return portNotInUse;
}
return false;
})
.WithMessage(MSG_PORT_IN_USE);
}
public static IRuleBuilderOptions<T, string> ErrorInstanceDatabaseMaintenancePortAvailable<T>(
this IRuleBuilder<T, string> ruleBuilder,
Func<T, string> getInstanceName)
{
return ruleBuilder.Must((t, port) =>
{
var instanceName = getInstanceName(t);
var errorInstancePort = Extensions.Validations.GetErrorInstanceDatabaseMaintenancePort(instanceName);
if (int.TryParse(port, out int newPort))
{
if (errorInstancePort == newPort)
{
return true;
}
var portNotInUse = !PortUtils.IsPortInUse(newPort);
return portNotInUse;
}
return false;
})
.WithMessage(MSG_PORT_IN_USE);
}
public static IRuleBuilderOptions<T, string> ErrorInstancePortAvailable<T>(
this IRuleBuilder<T, string> ruleBuilder,
Func<T, string> getInstanceName)
{
return ruleBuilder.Must((t, port) =>
{
var instanceName = getInstanceName(t);
var errorInstancePort = Extensions.Validations.GetErrorInstancePort(instanceName);
if (int.TryParse(port, out int newPort))
{
if (errorInstancePort == newPort)
{
return true;
}
var portNotInUse = !PortUtils.IsPortInUse(newPort);
return portNotInUse;
}
return false;
})
.WithMessage(MSG_PORT_IN_USE);
}
public static IRuleBuilderOptions<T, string> AuditInstanceDatabaseMaintenancePortAvailable<T>(
this IRuleBuilder<T, string> ruleBuilder,
Func<T, string> getInstanceName)
{
return ruleBuilder.Must((t, port) =>
{
var instanceName = getInstanceName(t);
var errorInstancePort = Extensions.Validations.GetAuditInstanceDatabaseMaintenancePort(instanceName);
if (int.TryParse(port, out int newPort))
{
if (errorInstancePort == newPort)
{
return true;
}
var portNotInUse = !PortUtils.IsPortInUse(newPort);
return portNotInUse;
}
return false;
})
.WithMessage(MSG_PORT_IN_USE);
}
public static IRuleBuilderOptions<T, string> AuditInstancePortAvailable<T>(
this IRuleBuilder<T, string> ruleBuilder,
Func<T, string> getInstanceName)
{
return ruleBuilder.Must((t, port) =>
{
var instanceName = getInstanceName(t);
var errorInstancePort = Extensions.Validations.GetAuditInstancePort(instanceName);
if (int.TryParse(port, out int newPort))
{
if (errorInstancePort == newPort)
{
return true;
}
var portNotInUse = !PortUtils.IsPortInUse(newPort);
return portNotInUse;
}
return false;
})
.WithMessage(MSG_PORT_IN_USE);
}
public static IRuleBuilderOptions<T, string> MonitoringInstancePortAvailable<T>(
this IRuleBuilder<T, string> ruleBuilder,
Func<T, string> getInstanceName)
{
return ruleBuilder.Must((t, port) =>
{
var instanceName = getInstanceName(t);
var errorInstancePort = Extensions.Validations.GetMonitoringInstancePort(instanceName);
if (int.TryParse(port, out int newPort))
{
if (errorInstancePort == newPort)
{
return true;
}
var portNotInUse = !PortUtils.IsPortInUse(newPort);
return portNotInUse;
}
return false;
})
.WithMessage(MSG_PORT_IN_USE);
}
public static IRuleBuilderOptions<T, string> ValidPath<T>(this IRuleBuilder<T, string> rulebuilder)
{
return rulebuilder.Must((t, path) => { return path != null && !path.Intersect(ILLEGAL_PATH_CHARS).Any(); })
.WithMessage(string.Format(MSG_ILLEGAL_PATH_CHAR, string.Join(' ', ILLEGAL_PATH_CHARS)));
}
public static IRuleBuilderOptions<T, TProperty> MustNotBeIn<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder, Func<T, IEnumerable<TProperty>> list) where TProperty : class
{
return ruleBuilder.Must((t, p) => p != null && !list(t).Contains(p));
}
public static IRuleBuilderOptions<T, string> MustNotContainWhitespace<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.Must(s => !string.IsNullOrEmpty(s) && !s.Any(c => char.IsWhiteSpace(c))).WithMessage(MSG_CANTCONTAINWHITESPACE);
}
public static IRuleBuilderOptions<T, string> MustBeUniqueWindowsServiceName<T>(this IRuleBuilder<T, string> ruleBuilder)
{
var windowsServices = ServiceController.GetServices();
return ruleBuilder.Must(instanceName =>
{
var serviceDoesNotExist = !windowsServices.Any(service => service.ServiceName.Equals(instanceName, StringComparison.InvariantCultureIgnoreCase));
return serviceDoesNotExist;
});
}
public const string MSG_EMAIL_NOT_VALID = "Not Valid.";
public const string MSG_THIS_TRANSPORT_REQUIRES_A_CONNECTION_STRING = "This transport requires a connection string.";
public const string MSG_CANTCONTAINWHITESPACE = "{0} cannot contain white space.";
public const string MSG_SELECTAUDITFORWARDING = "Must select audit forwarding.";
public const string MSG_SELECTERRORFORWARDING = "Must select error forwarding.";
public const string MSG_UNIQUEQUEUENAME = "Must not equal {0} queue name.";
public const string MSG_QUEUENAMES_NOT_EQUAL = "{0} queue name must not equal {1} queue name.";
public const string MSG_PORTS_NOT_EQUAL = "{0} port number must not equal {1} port number.";
public const string MSG_FORWARDINGQUEUENAME = "{0} queue name cannot be empty if forwarding is enabled.";
public const string MSG_QUEUENAME = "{0} queue name cannot be empty.";
public const string MSG_USE_PORTS_IN_RANGE = "{0} should be between 1 - 49151. Ephemeral port range should not be used (49152 to 65535).";
public const string MSG_PORT_IN_USE = "{0} specified is already in use by another process.";
public const string MSG_MUST_BE_UNIQUE = "{0} must be unique across all instances";
public const string MSG_QUEUE_ALREADY_ASSIGNED = "{0} queue is already assigned to another instance";
public const string WRN_HOSTNAME_SHOULD_BE_LOCALHOST = "Not using localhost can expose ServiceControl to anonymous access.";
public const string MSG_ILLEGAL_PATH_CHAR = "Paths cannot contain characters {0}";
static char[] ILLEGAL_PATH_CHARS =
{
'*',
'?',
'"',
'<',
'>',
'|'
};
}
}