-
Notifications
You must be signed in to change notification settings - Fork 0
/
POSHCmdlet.cs
369 lines (350 loc) · 12.3 KB
/
POSHCmdlet.cs
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
namespace POSHCredStore
{
[Cmdlet("Get", "CredentialStoreItem", ConfirmImpact = ConfirmImpact.None, SupportsShouldProcess = false)]
[OutputType(new Type[] {typeof (CredentialStoreItem)})]
public class GetCredentialStoreItem : PSCmdlet
{
private string _file;
private string _host;
private string _user;
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty]
public string Host
{
[DebuggerStepThrough] get
{
return this._host;
}
set
{
this._host = value;
}
}
[ValidateNotNullOrEmpty]
[Parameter(Position = 1)]
public string User
{
[DebuggerStepThrough] get
{
return this._user;
}
set
{
this._user = value;
}
}
[Parameter(Position = 2)]
[ValidateNotNullOrEmpty]
public string File
{
get
{
if (string.IsNullOrEmpty(this._file) || Path.IsPathRooted(this._file))
return this._file;
return Path.Combine(this.SessionState.Path.CurrentFileSystemLocation.ProviderPath, this._file);
}
set
{
ProviderInfo provider;
PSDriveInfo drive;
this._file = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(value, out provider, out drive);
if (!System.IO.File.Exists(this._file))
throw new FileNotFoundException("Credentials file doesn't exist.", this._file);
}
}
protected override void ProcessRecord()
{
FileInfo file = string.IsNullOrEmpty(this.File) ? (FileInfo) null : new FileInfo(this.File);
string str1 = string.IsNullOrEmpty(this.Host) ? "*" : this.Host;
WildcardPattern hostPattern = new WildcardPattern(str1, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
string str2 = string.IsNullOrEmpty(this.User) ? "*" : this.User;
WildcardPattern usernamePattern = new WildcardPattern(str2, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
List<CredentialStoreItem> list = new List<CredentialStoreItem>();
try
{
ICredentialStore credentialStore = (ICredentialStore) null;
try
{
credentialStore = CredentialStoreFactory.CreateCredentialStore(file);
foreach (string str3 in credentialStore.GetHosts())
{
if (hostPattern.IsMatch(str3))
{
foreach (string str4 in credentialStore.GetUsernames(str3))
{
if (usernamePattern.IsMatch(str4))
{
string password = new string(credentialStore.GetPassword(str3, str4));
list.Add((CredentialStoreItem) new CredentialStoreItemImpl(str3, str4, password, this.File));
}
}
}
}
}
finally
{
if (credentialStore != null)
credentialStore.Close();
}
}
catch (Exception ex)
{
this.ThrowTerminatingError(new ErrorRecord(ex, "Core_GetCredentialStoreItem_ProcessRecordget", System.Management.Automation.ErrorCategory.NotSpecified, (object) null));
}
this.WriteObject((object) list, true);
}
private void ReportErrorIfNotFound(string hostPatternString, string userPatternString, WildcardPattern hostPattern, WildcardPattern usernamePattern, IEnumerable<CredentialStoreItem> result)
{
if (!WildcardPattern.ContainsWildcardCharacters(hostPatternString))
{
bool flag = false;
foreach (CredentialStore credentialStoreItem in result)
{
if (hostPattern.IsMatch(credentialStoreItem.Host))
{
flag = true;
break;
}
}
if (!flag)
{
}
}
if (WildcardPattern.ContainsWildcardCharacters(userPatternString))
return;
bool flag1 = false;
foreach (CredentialStoreItem credentialStoreItem in result)
{
if (usernamePattern.IsMatch(credentialStoreItem.User))
{
flag1 = true;
break;
}
}
if (flag1)
return;
}
}
[Cmdlet("New", "CredentialStoreItem", ConfirmImpact = ConfirmImpact.Medium, SupportsShouldProcess = false)]
[OutputType(new Type[] {typeof (CredentialStoreItem)})]
public class NewCredentialStoreItem : PSCmdlet
{
private string _file;
private string _host;
private string _password;
private string _user;
[Parameter(Mandatory = true, Position = 0)]
[ValidateNotNullOrEmpty]
public string Host
{
[DebuggerStepThrough] get
{
return this._host;
}
set
{
this._host = value;
}
}
[Parameter(Mandatory = true, Position = 1)]
[ValidateNotNullOrEmpty]
public string User
{
[DebuggerStepThrough] get
{
return this._user;
}
set
{
this._user = value;
}
}
[Parameter(Mandatory = false, Position = 2)]
public string Password
{
[DebuggerStepThrough] get
{
return this._password;
}
set
{
this._password = value;
}
}
[ValidateNotNullOrEmpty]
[Parameter(Position = 3)]
public string File
{
get
{
if (string.IsNullOrEmpty(this._file) || Path.IsPathRooted(this._file))
return this._file;
return Path.Combine(this.SessionState.Path.CurrentFileSystemLocation.ProviderPath, this._file);
}
set
{
ProviderInfo provider;
PSDriveInfo drive;
this._file = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(value, out provider, out drive);
}
}
protected override void ProcessRecord()
{
FileInfo file = string.IsNullOrEmpty(this.File) ? (FileInfo) null : new FileInfo(this.File);
List<CredentialStoreItemImpl> list = new List<CredentialStoreItemImpl>();
ICredentialStore credentialStore = (ICredentialStore) null;
try
{
credentialStore = CredentialStoreFactory.CreateCredentialStore(file);
if (this.Password == null)
this.Password = "";
credentialStore.AddPassword(this.Host, this.User, this.Password.ToCharArray());
list.Add(new CredentialStoreItemImpl(this.Host, this.User, new string(credentialStore.GetPassword(this.Host, this.User)), this.File));
}
finally
{
if (credentialStore != null)
credentialStore.Close();
}
this.WriteObject((object) list, true);
}
}
[OutputType(new Type[] {typeof (void)})]
[Cmdlet("Remove", "CredentialStoreItem", ConfirmImpact = ConfirmImpact.High, SupportsShouldProcess = true)]
public class RemoveCredentialStoreItem : PSCmdlet
{
private const string ParameterSetByFilters = "ByFilters";
private const string ParameterSetByItemObject = "ByCredentialItemObject";
private string _file;
private string _host;
private CredentialStoreItem[] _items;
private string _user;
[ValidateNotNull]
[Parameter(Mandatory = true, ParameterSetName = "ByCredentialItemObject", Position = 0, ValueFromPipeline = true)]
public CredentialStoreItem[] CredentialStoreItem
{
[DebuggerStepThrough] get
{
return this._items;
}
set
{
this._items = value;
}
}
[Parameter(ParameterSetName = "ByFilters", Position = 0)]
[ValidateNotNullOrEmpty]
public string Host
{
[DebuggerStepThrough] get
{
return this._host;
}
set
{
this._host = value;
}
}
[Parameter(ParameterSetName = "ByFilters", Position = 1)]
[ValidateNotNullOrEmpty]
public string User
{
[DebuggerStepThrough] get
{
return this._user;
}
set
{
this._user = value;
}
}
[ValidateNotNullOrEmpty]
[Parameter(ParameterSetName = "ByFilters", Position = 2)]
public string File
{
get
{
if (string.IsNullOrEmpty(this._file) || Path.IsPathRooted(this._file))
return this._file;
return Path.Combine(this.SessionState.Path.CurrentFileSystemLocation.ProviderPath, this._file);
}
set
{
ProviderInfo provider;
PSDriveInfo drive;
this._file = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(value, out provider, out drive);
if (!System.IO.File.Exists(this._file))
throw new FileNotFoundException("Credentials file doesn't exist.", this._file);
}
}
protected override void ProcessRecord()
{
if (this.ParameterSetName == "ByFilters" && string.IsNullOrEmpty(this.Host) && string.IsNullOrEmpty(this.User))
this.ThrowTerminatingError(VMware.VimAutomation.Sdk.Util10Ps.ErrorHandling.ExceptionHelper.VimExceptionToErrorRecord(VMware.VimAutomation.Sdk.Util10.ErrorHandling.ExceptionHelper.CreateClientSideException("Core_RemoveCredentialStoreItem_ProcessRecord_InvalidArguemnt", VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ErrorCategory.InvalidArgument, (object) null, (Exception) null, typeof (ViError), VMware.VimAutomation.ViCore.Cmdlets.ResourceHelper.ResourceService, (string) null)));
CredentialStoreItem[] credentialStoreItemArray;
if (this.ParameterSetName == "ByCredentialItemObject")
credentialStoreItemArray = this._items;
else if (this.ParameterSetName == "ByFilters")
{
List<CredentialStoreItemImpl> list = new List<CredentialStoreItemImpl>();
FileInfo file = string.IsNullOrEmpty(this.File) ? (FileInfo) null : new FileInfo(this.File);
ICredentialStore credentialStore = (ICredentialStore) null;
try
{
credentialStore = CredentialStoreFactory.CreateCredentialStore(file);
WildcardPattern wildcardPattern1 = new WildcardPattern("*", WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
if (!string.IsNullOrEmpty(this.Host))
wildcardPattern1 = new WildcardPattern(this.Host, WildcardOptions.Compiled | WildcardOptions.IgnoreCase);
WildcardPattern wildcardPattern2 = new WildcardPattern("*", WildcardOptions.Compiled);
if (!string.IsNullOrEmpty(this.User))
wildcardPattern2 = new WildcardPattern(this.User, WildcardOptions.Compiled);
foreach (string str1 in credentialStore.GetHosts())
{
if (wildcardPattern1.IsMatch(str1))
{
foreach (string str2 in credentialStore.GetUsernames(str1))
{
if (wildcardPattern2.IsMatch(str2))
{
string password = new string(credentialStore.GetPassword(str1, str2));
list.Add(new CredentialStoreItemImpl(str1, str2, password, this.File));
}
}
}
}
}
finally
{
if (credentialStore != null)
credentialStore.Close();
}
credentialStoreItemArray = (CredentialStoreItem[]) list.ToArray();
}
else
credentialStoreItemArray = new CredentialStoreItem[0];
foreach (CredentialStoreItem credentialStoreItem in credentialStoreItemArray)
{
if (this.ShouldProcess(string.Format(VMware.VimAutomation.ViCore.Cmdlets.ResourceHelper.ResourceService.GetString("Core_RemoveCredentialStoreItem_ProcessRecord_Action"), (object) credentialStoreItem.Host, (object) credentialStoreItem.User)))
{
FileInfo file = string.IsNullOrEmpty(credentialStoreItem.File) ? (FileInfo) null : new FileInfo(credentialStoreItem.File);
ICredentialStore credentialStore = (ICredentialStore) null;
try
{
credentialStore = CredentialStoreFactory.CreateCredentialStore(file);
credentialStore.RemovePassword(credentialStoreItem.Host, credentialStoreItem.User);
}
finally
{
if (credentialStore != null)
credentialStore.Close();
}
}
}
}
}
}