Skip to content

Commit aa097fc

Browse files
authored
Merge pull request #571 from turtle0x1/support-lxd-tokens
Support lxd tokens
2 parents 170acf6 + d0bda77 commit aa097fc

File tree

7 files changed

+527
-428
lines changed

7 files changed

+527
-428
lines changed

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"require": {
44
"php-http/guzzle6-adapter": "^1.1",
55
"php-di/php-di": "^5.4",
6-
"dhope0000/lxd": "0.24.0",
6+
"dhope0000/lxd": "^0.25.0",
77
"vlucas/phpdotenv": "^2.5",
88
"symfony/http-foundation": "^4.2",
99
"symfony/filesystem": "^4.3",

Diff for: composer.lock

+11-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/classes/Tools/Hosts/AddHosts.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AddHosts
1515
private $getDetails;
1616
private $lxdClient;
1717
private $fetchUserDetails;
18-
18+
1919
public function __construct(
2020
AddHostModel $addHost,
2121
GenerateCert $generateCert,
@@ -60,7 +60,8 @@ public function add($userId, array $hostsDetails)
6060
$result = $this->generateCert->createCertAndPushToServer(
6161
$hostName,
6262
$hostsDetail["trustPassword"],
63-
$socketPath
63+
$socketPath,
64+
$hostsDetail["token"]
6465
);
6566

6667
$alias = null;
@@ -145,10 +146,13 @@ private function validateDetails($host)
145146
return true;
146147
}
147148

149+
$missingTrustPassword = !isset($host["trustPassword"]) || empty($host["trustPassword"]);
150+
$missingToken = !isset($host["token"]) || empty($host["token"]);
151+
148152
if (!isset($host["name"]) || empty($host["name"])) {
149153
throw new \Exception("Please provide name", 1);
150-
} elseif (!isset($host["trustPassword"]) || empty($host["trustPassword"])) {
151-
throw new \Exception("Please provide trust password", 1);
154+
} elseif ($missingTrustPassword && $missingToken) {
155+
throw new \Exception("Please provide token or trust password", 1);
152156
}
153157
return true;
154158
}

Diff for: src/classes/Tools/Hosts/GenerateCert.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class GenerateCert
1010
{
1111
private $lxdClient;
12-
12+
1313
private $certSettings = [
1414
"countryName" => "UK",
1515
"stateOrProvinceName" => "Isle Of Wight",
@@ -27,14 +27,15 @@ public function __construct(LxdClient $lxdClient)
2727

2828
public function createCertAndPushToServer(
2929
$urlAndPort,
30-
$trustPassword,
31-
$socketPath
30+
$trustPassword = null,
31+
$socketPath = null,
32+
$token = null
3233
) {
3334
$paths = $this->createCertKeyAndCombinedPaths($urlAndPort, $socketPath);
3435

3536
$this->generateCert($paths);
3637

37-
$lxdResponse = $this->addToServer($urlAndPort, $trustPassword, $paths["combined"], $socketPath);
38+
$lxdResponse = $this->addToServer($urlAndPort, $trustPassword, $token, $paths["combined"], $socketPath);
3839

3940
$shortPaths = $this->createShortPaths($paths);
4041

@@ -68,12 +69,12 @@ private function createShortPaths($pathsArray)
6869
return $pathsArray;
6970
}
7071

71-
private function addToServer($urlAndPort, $trustPassword, $pathToCert, $socketPath)
72+
private function addToServer($urlAndPort, $trustPassword, $token, $pathToCert, $socketPath)
7273
{
7374
$config = $this->lxdClient->createConfigArray($pathToCert, $socketPath);
7475
$config["timeout"] = 2;
7576
$lxd = $this->lxdClient->createNewClient($urlAndPort, $config);
76-
return $lxd->certificates->add(file_get_contents($pathToCert), $trustPassword);
77+
return $lxd->certificates->add(file_get_contents($pathToCert), $trustPassword, null, $token);
7778
}
7879

7980
private function generateCert($paths)

Diff for: src/views/boxes/settings.php

+39-14
Original file line numberDiff line numberDiff line change
@@ -772,25 +772,37 @@ function loadInstanceSettings(){
772772
}
773773
774774
$("#instanceHostsBox").on("click", "#addHostBtn", function(){
775-
$.confirm({
776-
icon: "fa fa-server",
777-
title: "Add Host",
778-
content: `<div class="mb-2">
775+
$.confirm({
776+
icon: "fa fa-server",
777+
title: "Add Host",
778+
columnClass: "col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1",
779+
content: `<div class="mb-2">
779780
<label>Alias</label>
780781
<input class="form-control" name="alias" placeholder="PC, Laptop, Server-1"/>
781782
</div>
782783
<label class="mb-2">How Do You Want To Access The Host?</label>
783-
<nav class="text-dark">
784+
<nav>
784785
<div class="nav nav-pills mb-3" id="host-nav" role="tablist">
785-
<button class="nav-link w-50 text-dark active" id="nav-network-tab" data-bs-toggle="tab" data-bs-target="#nav-network" type="button" role="tab" aria-controls="nav-network" aria-selected="true">Trust Password</button>
786-
<button class="nav-link w-50 text-dark" id="nav-socket-tab" data-bs-toggle="tab" data-bs-target="#nav-socket" type="button" role="tab" aria-controls="nav-socket" aria-selected="false">Socket</button>
786+
<button class="nav-link text-dark active" id="nav-token-tab" data-bs-toggle="tab" data-bs-target="#nav-token" type="button" role="tab" aria-controls="nav-token" aria-selected="true">Token</button>
787+
<button class="nav-link text-dark" id="nav-network-tab" data-bs-toggle="tab" data-bs-target="#nav-network" type="button" role="tab" aria-controls="nav-network" aria-selected="true">Trust Password (Depreciated)</button>
788+
<button class="nav-link text-dark" id="nav-socket-tab" data-bs-toggle="tab" data-bs-target="#nav-socket" type="button" role="tab" aria-controls="nav-socket" aria-selected="false">Socket</button>
787789
</div>
788790
</nav>
789791
<div class="tab-content" id="host-navContent">
790-
<div class="tab-pane fade show active" id="nav-network" role="tabpanel" aria-labelledby="nav-network-tab">
792+
<div class="tab-pane fade show active" id="nav-token" role="tabpanel" aria-labelledby="nav-token-tab">
791793
<div class="mb-2">
792794
<label>IP Address / Hostname</label>
793-
<input class="form-control" name="name" placeholder=""/>
795+
<input class="form-control" name="name" placeholder="" autocomplete="off"/>
796+
</div>
797+
<div class="mb-2">
798+
<label>Token</label>
799+
<textarea class="form-control" type="text" name="token" placeholder="" autocomplete="off"></textarea>
800+
</div>
801+
</div>
802+
<div class="tab-pane fade show" id="nav-network" role="tabpanel" aria-labelledby="nav-network-tab">
803+
<div class="mb-2">
804+
<label>IP Address / Hostname</label>
805+
<input class="form-control" name="name" placeholder="" autocomplete="off"/>
794806
</div>
795807
<div class="mb-2">
796808
<label>Trust Password</label>
@@ -800,7 +812,7 @@ function loadInstanceSettings(){
800812
<div class="tab-pane fade" id="nav-socket" role="tabpanel" aria-labelledby="nav-socket-tab">
801813
<div class="mb-2">
802814
<label>Socket Path</label>
803-
<input class="form-control" name="socketPath" placeholder="/var/snap/lxd/common/lxd/unix.socket"/>
815+
<input class="form-control" name="socketPath" value="/var/snap/lxd/common/lxd/unix.socket"/>
804816
</div>
805817
</div>
806818
</div>
@@ -819,6 +831,7 @@ function loadInstanceSettings(){
819831
820832
let name = ""
821833
let trustPassword = ""
834+
let token = ""
822835
let socketPath = ""
823836
824837
let activeNav = this.$content.find("#host-nav .active").data().bsTarget
@@ -832,19 +845,31 @@ function loadInstanceSettings(){
832845
}
833846
}else{
834847
name = this.$content.find('input[name=name]').val().trim();
835-
trustPassword = this.$content.find('input[name=trustPassword]').val().trim();
848+
836849
if(name === ""){
837850
$.alert("Please enter ip address / hostname")
838851
return false;
839-
}else if(trustPassword === ""){
840-
$.alert("Please enter trust password")
841-
return false;
842852
}
853+
if(activeNav == "#nav-token"){
854+
token = this.$content.find('textarea[name=token]').val().trim();
855+
if(token === ""){
856+
$.alert("Please enter token")
857+
return false;
858+
}
859+
}else{
860+
trustPassword = this.$content.find('input[name=trustPassword]').val().trim();
861+
if(trustPassword === ""){
862+
$.alert("Please enter trust password")
863+
return false;
864+
}
865+
}
866+
843867
}
844868
let x = {hostsDetails: [
845869
{
846870
name: name,
847871
trustPassword: trustPassword,
872+
token: token,
848873
socketPath: socketPath,
849874
alias: alias
850875
}

0 commit comments

Comments
 (0)