-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathSshKeyFileController.cs
More file actions
114 lines (96 loc) · 3.41 KB
/
SshKeyFileController.cs
File metadata and controls
114 lines (96 loc) · 3.41 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
using System;
using System.Linq;
using System.Collections.Generic;
using QueryTree.Models;
using System.IO;
using QueryTree.Managers;
using Renci.SshNet;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Http;
using QueryTree.Enums;
namespace QueryTree.Controllers
{
[Authorize]
public class SshKeyFileController : IdentityController
{
IPasswordManager _passwordManager;
public SshKeyFileController(
ApplicationDbContext dbContext,
IPasswordManager passwordManager,
UserManager<ApplicationUser> userManager)
: base(userManager, dbContext)
{
_passwordManager = passwordManager;
}
[HttpPost]
public ActionResult Upload()
{
ClearOldFiles();
if (Request.Form.Files.Any())
{
var file = Request.Form.Files[0];
MemoryStream target = new MemoryStream();
file.OpenReadStream().CopyTo(target);
byte[] data = target.ToArray();
SshKeyFile keyFile = new SshKeyFile()
{
Filename = file.FileName,
ContentType = file.ContentType,
CreatedOn = DateTime.Now,
CreatedBy = CurrentUser
};
if (ValidateSshKeyFile(data))
{
db.SshKeyFiles.Add(keyFile);
db.SaveChanges();
string dataString = Convert.ToBase64String(data);
_passwordManager.SetSecret(SecretType.SshKeyFile.ToString() + "_" + keyFile.Id, dataString);
}
else
{
return Json(new { Status = "error", Message = "The file you uploaded was not a valid SSH key file, please upload a different file." });
}
return Json(new { Status = "ok", SshKeyFileID = keyFile.Id, Filename = keyFile.Filename });
} else {
return Json(new { Status = "error" , Message = "Error uploading file, please try again" });
}
}
private bool ValidateSshKeyFile(byte[] data)
{
try
{
PrivateKeyFile p = new PrivateKeyFile(new MemoryStream(data));
return true;
}
catch
{
return false;
}
}
private void ClearOldFiles()
{
DateTime threshold = DateTime.Now.AddMinutes(-5);
var filesToDelete = db.SshKeyFiles
.GroupJoin(db.DatabaseConnections, k => k.Id, d => d.SshKeyFileID, (k, ds) => new { SshKeyFile = k, InUse = ds.DefaultIfEmpty().Any() })
.Where(_ => _.InUse == false && _.SshKeyFile.CreatedOn < threshold)
.Select(_ => _.SshKeyFile)
.ToList();
foreach (var keyFile in filesToDelete)
{
_passwordManager.DeleteSecret(SecretType.SshKeyFile.ToString() + "_" + keyFile.Id);
}
db.SshKeyFiles.RemoveRange(filesToDelete);
db.SaveChanges();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}