Skip to content

Commit a749f71

Browse files
committed
feature: allow users to manage ssh keys
1 parent b815d10 commit a749f71

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+18209
-5102
lines changed

firewall/static/firewall/firewall.js

-1
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,6 @@ app.controller('secureSSHCTRL', function ($scope, $http) {
721721

722722
}
723723

724-
725724
$scope.saveKey = function (key) {
726725

727726
$scope.secureSSHLoading = false;

firewall/templates/firewall/secureSSH.html

+109-109
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
{% block title %}{% trans "Secure SSH - CyberPanel" %}{% endblock %}
44
{% block content %}
55

6-
{% load static %}
7-
{% get_current_language as LANGUAGE_CODE %}
8-
<!-- Current language: {{ LANGUAGE_CODE }} -->
9-
10-
11-
<div class="container">
12-
<div id="page-title">
13-
<h2>{% trans "Secure SSH" %} - <a target="_blank" href="http://go.cyberpanel.net/ssh-docs" style="height: 23px;line-height: 21px;" class="btn btn-border btn-alt border-red btn-link font-red" title=""><span>{% trans "SSH Docs" %}</span></a></h2>
14-
<p>{% trans "Secure or harden SSH Configurations." %}</p>
15-
</div>
6+
{% load static %}
7+
{% get_current_language as LANGUAGE_CODE %}
8+
<!-- Current language: {{ LANGUAGE_CODE }} -->
9+
10+
11+
<div class="container">
12+
<div id="page-title">
13+
<h2>{% trans "Secure SSH" %} - <a target="_blank" href="http://go.cyberpanel.net/ssh-docs"
14+
style="height: 23px;line-height: 21px;"
15+
class="btn btn-border btn-alt border-red btn-link font-red"
16+
title=""><span>{% trans "SSH Docs" %}</span></a></h2>
17+
<p>{% trans "Secure or harden SSH Configurations." %}</p>
18+
</div>
1619

17-
<div ng-controller="secureSSHCTRL" class="panel">
20+
<div ng-controller="secureSSHCTRL" class="panel">
1821
<div class="panel-body">
1922
<h3 class="content-box-header">
2023
{% trans "Secure SSH" %} <img ng-hide="secureSSHLoading" src="{% static 'images/loading.gif' %}">
@@ -37,159 +40,156 @@ <h3 class="content-box-header">
3740
</ul>
3841
<div class="tab-content">
3942
<div class="tab-pane fade active in" id="tab-example-1">
40-
<div class="example-box-wrapper">
43+
<div class="example-box-wrapper">
4144

4245

43-
<form action="/" class="form-horizontal bordered-row">
46+
<form action="/" class="form-horizontal bordered-row">
4447

4548

46-
<div ng-hide="emailDetails" class="form-group">
47-
<label class="col-sm-3 control-label">{% trans "SSH Port" %}</label>
48-
<div class="col-sm-6">
49-
<input type="text" class="form-control" ng-model="sshPort" required>
50-
</div>
49+
<div ng-hide="emailDetails" class="form-group">
50+
<label class="col-sm-3 control-label">{% trans "SSH Port" %}</label>
51+
<div class="col-sm-6">
52+
<input type="text" class="form-control" ng-model="sshPort" required>
53+
</div>
5154

52-
</div>
55+
</div>
5356

5457

55-
<div class="form-group">
56-
<label class="col-sm-3 control-label">{% trans "Permit Root Login" %}</label>
57-
<div class="col-md-8 text-white">
58-
<input type="checkbox" id="rootLogin" data-toggle="toggle" data-on="Enabled">
59-
<div style="margin-top: 2%;margin-bottom: -2%" class="alert alert-warning">
60-
<p>{% trans "Before disabling root login, make sure you have another account with sudo priviliges on server." %}</p>
61-
</div>
62-
</div>
63-
</div>
58+
<div class="form-group">
59+
<label class="col-sm-3 control-label">{% trans "Permit Root Login" %}</label>
60+
<div class="col-md-8 text-white">
61+
<input type="checkbox" id="rootLogin" data-toggle="toggle"
62+
data-on="Enabled">
63+
<div style="margin-top: 2%;margin-bottom: -2%" class="alert alert-warning">
64+
<p>{% trans "Before disabling root login, make sure you have another account with sudo priviliges on server." %}</p>
65+
</div>
66+
</div>
67+
</div>
6468

65-
<div class="form-group">
66-
<label class="col-sm-3 control-label"></label>
67-
<div class="col-sm-4">
68-
<button type="button" ng-click="saveChanges()" class="btn btn-primary btn-lg">{% trans "Save Changes" %}</button>
69+
<div class="form-group">
70+
<label class="col-sm-3 control-label"></label>
71+
<div class="col-sm-4">
72+
<button type="button" ng-click="saveChanges()"
73+
class="btn btn-primary btn-lg">{% trans "Save Changes" %}</button>
6974

70-
</div>
71-
</div>
75+
</div>
76+
</div>
7277

7378

74-
<div class="form-group">
75-
<label class="col-sm-3 control-label"></label>
76-
<div class="col-sm-4">
77-
<div ng-hide="couldNotSave" class="alert alert-danger">
78-
<p>{% trans "Error message: " %}{$ errorMessage $}</p>
79-
</div>
79+
<div class="form-group">
80+
<label class="col-sm-3 control-label"></label>
81+
<div class="col-sm-4">
82+
<div ng-hide="couldNotSave" class="alert alert-danger">
83+
<p>{% trans "Error message: " %}{$ errorMessage $}</p>
84+
</div>
8085

81-
<div ng-hide="detailsSaved" class="alert alert-success">
82-
<p>{% trans "SSH Configurations Saved." %}</p>
83-
</div>
84-
85-
86-
<div ng-hide="couldNotConnect" class="alert alert-danger">
87-
<p>{% trans "Could not connect. Please refresh this page." %}</p>
88-
</div>
89-
</div>
86+
<div ng-hide="detailsSaved" class="alert alert-success">
87+
<p>{% trans "SSH Configurations Saved." %}</p>
88+
</div>
9089

9190

92-
93-
</div>
94-
91+
<div ng-hide="couldNotConnect" class="alert alert-danger">
92+
<p>{% trans "Could not connect. Please refresh this page." %}</p>
93+
</div>
94+
</div>
9595

9696

97-
</form>
97+
</div>
9898

9999

100+
</form>
100101

101102

102-
</div>
103+
</div>
103104
</div>
104105

105106

106-
107107
<div class="tab-pane fade" id="tab-example-3">
108108

109-
<form action="/" class="form-horizontal bordered-row">
109+
<form action="/" class="form-horizontal bordered-row">
110110

111111
<!------ List of records --------------->
112112

113-
<div class="form-group">
114-
115-
<div class="col-sm-12">
116-
117-
<table class="table">
118-
<thead>
119-
<tr>
120-
<th>{% trans "User Name" %}</th>
121-
<th>{% trans "Key" %}</th>
122-
<th>{% trans "Delete" %}</th>
123-
</tr>
124-
</thead>
125-
<tbody>
126-
<tr ng-repeat="record in records track by $index">
127-
<td ng-bind="record.userName">root</td>
128-
<td ng-bind="record.key"></td>
129-
<td ng-click="deleteKey(record.key)"><div class="h4 text-danger text-bold">X</div></td>
130-
</tr>
131-
</tbody>
132-
</table>
133-
</div>
134-
</div>
135-
113+
<div class="form-group">
114+
115+
<div class="col-sm-12">
116+
117+
<table class="table">
118+
<thead>
119+
<tr>
120+
<th>{% trans "User Name" %}</th>
121+
<th>{% trans "Key" %}</th>
122+
<th>{% trans "Delete" %}</th>
123+
</tr>
124+
</thead>
125+
<tbody>
126+
<tr ng-repeat="record in records track by $index">
127+
<td ng-bind="record.userName">root</td>
128+
<td ng-bind="record.key"></td>
129+
<td ng-click="deleteKey(record.key)">
130+
<div class="h4 text-danger text-bold">X</div>
131+
</td>
132+
</tr>
133+
</tbody>
134+
</table>
135+
</div>
136+
</div>
136137

137138

138-
<!------ List of records --------------->
139+
<!------ List of records --------------->
139140

140-
<div ng-hide="keyBox" class="form-group">
141-
<div class="col-sm-12">
142-
<textarea placeholder="Paste your public key here..." ng-model="keyData" rows="6" class="form-control">{{ logs }}</textarea>
141+
<div ng-hide="keyBox" class="form-group">
142+
<div class="col-sm-12">
143+
<textarea placeholder="Paste your public key here..." ng-model="keyData"
144+
rows="6" class="form-control">{{ logs }}</textarea>
145+
</div>
143146
</div>
144-
</div>
145147

146148

147-
<div ng-hide="showKeyBox" class="form-group">
148-
<label class="col-sm-3 control-label"></label>
149-
<div class="col-sm-4">
150-
<button type="button" ng-click="addKey()" class="btn btn-primary btn-lg">{% trans "Add Key" %}</button>
149+
<div ng-hide="showKeyBox" class="form-group">
150+
<label class="col-sm-3 control-label"></label>
151+
<div class="col-sm-4">
152+
<button type="button" ng-click="addKey()"
153+
class="btn btn-primary btn-lg">{% trans "Add Key" %}</button>
151154

152-
</div>
153-
</div>
155+
</div>
156+
</div>
154157

155-
<div ng-hide="saveKeyBtn" class="form-group">
156-
<label class="col-sm-3 control-label"></label>
157-
<div class="col-sm-4">
158-
<button type="button" ng-click="saveKey()" class="btn btn-primary btn-lg">{% trans "Save" %}</button>
159-
</div>
160-
</div>
158+
<div ng-hide="saveKeyBtn" class="form-group">
159+
<label class="col-sm-3 control-label"></label>
160+
<div class="col-sm-4">
161+
<button type="button" ng-click="saveKey()"
162+
class="btn btn-primary btn-lg">{% trans "Save" %}</button>
163+
</div>
164+
</div>
161165

162-
<div class="form-group">
163-
<label class="col-sm-3 control-label"></label>
164-
<div class="col-sm-4">
166+
<div class="form-group">
167+
<label class="col-sm-3 control-label"></label>
168+
<div class="col-sm-4">
165169

166-
<div ng-hide="keyDeleted" class="alert alert-success">
170+
<div ng-hide="keyDeleted" class="alert alert-success">
167171
<p>{% trans "SSH Key Deleted" %}</p>
168-
</div>
172+
</div>
169173

170174

171-
<div ng-hide="couldNotConnect" class="alert alert-danger">
175+
<div ng-hide="couldNotConnect" class="alert alert-danger">
172176
<p>{% trans "Could not connect. Please refresh this page." %}</p>
177+
</div>
178+
</div>
173179
</div>
174-
</div>
175-
</div>
176-
177-
178-
179180

180181

181182
</form>
182183

183184

184-
185185
</div>
186186
</div>
187187
</div>
188188
</div>
189189
</div>
190190

191191

192-
</div>
192+
</div>
193193

194194

195195
{% endblock %}

plogical/firewallUtilities.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,20 @@ def saveSSHConfigs(type, sshPort, rootLogin):
130130
print("0," + str(msg))
131131

132132
@staticmethod
133-
def addSSHKey(tempPath):
133+
def addSSHKey(tempPath, path=None):
134134
try:
135135
key = open(tempPath, 'r').read()
136136

137-
sshDir = "/root/.ssh"
137+
if path == None:
138+
sshDir = "/root/.ssh"
139+
pathToSSH = "/root/.ssh/authorized_keys"
138140

139-
pathToSSH = "/root/.ssh/authorized_keys"
140-
141-
if os.path.exists(sshDir):
142-
pass
141+
if os.path.exists(sshDir):
142+
pass
143+
else:
144+
os.mkdir(sshDir)
143145
else:
144-
os.mkdir(sshDir)
146+
pathToSSH = path
145147

146148
if os.path.exists(pathToSSH):
147149
pass
@@ -176,10 +178,14 @@ def addSSHKey(tempPath):
176178
print("0," + str(msg))
177179

178180
@staticmethod
179-
def deleteSSHKey(key):
181+
def deleteSSHKey(key, path=None):
180182
try:
181183
keyPart = key.split(" ")[1]
182-
pathToSSH = "/root/.ssh/authorized_keys"
184+
185+
if path == None:
186+
pathToSSH = "/root/.ssh/authorized_keys"
187+
else:
188+
pathToSSH = path
183189

184190
data = open(pathToSSH, 'r').readlines()
185191

@@ -212,16 +218,23 @@ def main():
212218
parser.add_argument("--sshPort", help="SSH Port")
213219
parser.add_argument("--rootLogin", help="Root Login")
214220
parser.add_argument("--key", help="Key")
221+
parser.add_argument("--path", help="Path to key file.")
215222

216223

217224
args = parser.parse_args()
218225

219226
if args.function == "saveSSHConfigs":
220227
FirewallUtilities.saveSSHConfigs(args.type, args.sshPort, args.rootLogin)
221228
elif args.function == "addSSHKey":
222-
FirewallUtilities.addSSHKey(args.tempPath)
229+
if not args.path:
230+
FirewallUtilities.addSSHKey(args.tempPath)
231+
else:
232+
FirewallUtilities.addSSHKey(args.tempPath, args.path)
223233
elif args.function == "deleteSSHKey":
224-
FirewallUtilities.deleteSSHKey(args.key)
234+
if not args.path:
235+
FirewallUtilities.deleteSSHKey(args.key)
236+
else:
237+
FirewallUtilities.deleteSSHKey(args.key, args.path)
225238

226239

227240

0 commit comments

Comments
 (0)