-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
259 lines (225 loc) · 9.83 KB
/
Program.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
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using Vault;
namespace git_credential_cert
{
class Program
{
const string SPECIFICATION = "https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-credential.html";
const string HELP = "usage: git-credential-cert.exe [get|store|list|erase|delete]";
enum ExitCodes : int
{
Success = 0,
GitInputError = 1,
PassError = 2,
PassEntryNotFound = 3,
CertNotFound = 4,
CredentialsNotFound = 5,
ArgumentNotFound = 6,
CredentialsAlreadyAdded = 7,
UnknownCommand = 97,
ShowHelp = 98,
UnknownError = 99
}
static void Exit(ExitCodes code)
{
Environment.Exit((int)code);
}
static void ThrowPanic(string msg, ExitCodes code)
{
Console.Error.WriteLine(msg);
Exit(code);
}
static void Debug(string output)
{
//Console.Error.WriteLine(string.Format("DEBUG:{0}", output));
}
static void ConsoleOutLine(string output)
{
Debug(output);
Console.Out.Write(output + "\n");
}
static (string url, string username, string password) ReadFromInput()
{
string username = "", password = "";
UriBuilder uriBuilder = new UriBuilder();
CultureInfo culture = new CultureInfo("en-US");
string line;
while ((line = Console.ReadLine()) != null)
{
if (line == "")
{
break;
}
else
{
var sp = line.Split('=');
if (sp.Length < 2)
{
ThrowPanic(string.Format("incorrect parameter in string : {1}{0}please, check specification {2}", Environment.NewLine, line, SPECIFICATION), ExitCodes.GitInputError);
}
else
{
var key = sp[0].ToLower();
var val = Regex.Replace(line, string.Format("{0}=", key), "", RegexOptions.IgnoreCase);
ConsoleOutLine(line);
switch (key)
{
case "protocol":
uriBuilder.Scheme = val;
break;
case "host":
uriBuilder.Host = val;
break;
case "path":
uriBuilder.Path = val;
break;
case "username":
username = val;
break;
case "password":
password = val;
break;
default:
ThrowPanic(string.Format("incorrect input parameter: {1}{0}please, check specification {2}", Environment.NewLine, key, SPECIFICATION), ExitCodes.GitInputError);
break;
}
}
}
}
return (uriBuilder.ToString(), username, password);
}
static void Main(string[] args)
{
if (args.Length == 0)
{
ConsoleOutLine(string.Format("Git-Credential-Cert {0}", typeof(Program).Assembly.GetName().Version));
ConsoleOutLine(HELP);
Exit(ExitCodes.ShowHelp);
}
else
{
Debug(string.Format("method:{0}", args[0]));
switch (args[0].ToLower())
{
case "get":
{
if (args.Length > 1)
{
ThrowPanic("get command does not support any additional arguments. Console input should be used.", ExitCodes.GitInputError);
}
{
(string url, string _, string _) = ReadFromInput();
Store store = Store.Open();
try
{
(string protocol, string host, string path, string username, string password) = store.GetCredentialsFor(new UriBuilder(url));
ConsoleOutLine(string.Format("username={0}", username));
ConsoleOutLine(string.Format("password={0}", password));
Exit(ExitCodes.Success);
}
catch (Exception e)
{ // credentials for url not found
Console.Error.WriteLine(e.Message);
}
}
}
break;
case "store":
{
if (args.Length > 1)
{
ThrowPanic("store command does not support any additional arguments. Console input should be used.", ExitCodes.GitInputError);
}
(string url, string username, string password) = ReadFromInput();
Store store = Store.Open();
try
{
store.Add(new UriBuilder(url), username, password);
}
catch (NoCertWithPrivateKeyException e)
{
ThrowPanic(e.Message, ExitCodes.CertNotFound);
}
catch (CredentialsAlreadyExistsException)
{
// additional message are not required, git tries to add same credentials with any even success push
//ThrowPanic(e.Message, ExitCodes.CredentialsAlreadyAdded);
Exit(ExitCodes.CredentialsAlreadyAdded);
}
store.Save();
Exit(ExitCodes.Success);
}
break;
case "list":
{
if (args.Length > 1)
{
ThrowPanic("list command does not support any additional arguments", ExitCodes.UnknownCommand);
}
Store store = Store.Open();
store.GetList().ForEach(signedContainer =>
{
Console.Out.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}",
signedContainer.GetURL(),
signedContainer.GetUserName(),
signedContainer.GetCertificateSubject(),
signedContainer.GetCertificateThumbprint(),
signedContainer.GetCreatedDateTime()
)
);
});
break;
}
case "erase":
{
try
{
(string url, string _, string _) = ReadFromInput();
Store store = Store.Open();
store.Remove(new UriBuilder(url));
store.Save();
Exit(ExitCodes.Success);
}
catch (CouldNotFoundCredentialsException e)
{
ThrowPanic(e.Message, ExitCodes.CredentialsNotFound);
}
catch (Exception e)
{
ThrowPanic(e.Message, ExitCodes.UnknownError);
}
}
break;
case "delete":
{
if (args.Length != 2)
{
ThrowPanic("delete command should have url argument", ExitCodes.ArgumentNotFound);
}
try
{
Store store = Store.Open();
store.Remove(new UriBuilder(args[1]));
store.Save();
Exit(ExitCodes.Success);
}
catch (CouldNotFoundCredentialsException e)
{
ThrowPanic(e.Message, ExitCodes.CredentialsNotFound);
}
catch (Exception e)
{
ThrowPanic(e.Message, ExitCodes.UnknownError);
}
}
break;
default:
ThrowPanic(string.Format("unknown argument '{0}'", args[0]), ExitCodes.UnknownCommand);
break;
}
}
}
}
}