-
-
Notifications
You must be signed in to change notification settings - Fork 403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: dockge set/update agent friendly name #414
base: master
Are you sure you want to change the base?
Changes from 11 commits
c0fc76a
11f5fff
54e158f
8f142af
62ceb44
5885665
2b64b3f
a8b75fb
9748469
052cf17
dcc48d3
825b727
49eda70
1c72c23
5b2ac56
29cc257
70bcc3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,12 +76,15 @@ export class AgentManager { | |
* @param url | ||
* @param username | ||
* @param password | ||
* @param friendlyname | ||
* @param updatedFriendlyName | ||
*/ | ||
async add(url : string, username : string, password : string) : Promise<Agent> { | ||
async add(url : string, username : string, password : string, friendlyname : string) : Promise<Agent> { | ||
let bean = R.dispense("agent") as Agent; | ||
bean.url = url; | ||
bean.username = username; | ||
bean.password = password; | ||
bean.friendlyname = friendlyname; | ||
await R.store(bean); | ||
return bean; | ||
} | ||
|
@@ -104,6 +107,16 @@ export class AgentManager { | |
} | ||
} | ||
|
||
/** | ||
* | ||
* @param friendlyname | ||
* @param updatedFriendlyName | ||
*/ | ||
|
||
async update(friendlyname : string, updatedFriendlyName : string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would consider accepting the agent's ID is the primary key, so thats probably best to use for updating the agent. But URL is always set and has a unique constraint, so it will work too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with nathan, I would refactor to pass id around for the agent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure it should be the |
||
await R.exec("UPDATE agent SET friendlyname = " + updatedFriendlyName + " WHERE friendlyname = " + friendlyname + ""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SQL injection vuln here - if name has quotes this will break. You can use parameter bindings to build queries safely: await R.exec("UPDATE agent SET friendlyname = ? WHERE friendlyname = ?", [
updatedFriendlyName,
friendlyname
]); https://github.com/louislam/redbean-node/blob/master/docs/Query.md There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of a raw query, you could utilize the redbean ORM methods. See: https://github.com/louislam/redbean-node/blob/master/docs/Create-Update-Bean.md Something like this if by ID (primary key): const agent = await R.load('agent', id);
agent.friendlyName = friendlyName;
await R.store(agent); Or by URL: const agent = await R.findOne('agent', ' url = ? ', [url]);
agent.friendlyName = friendlyName;
await R.store(agent); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the input. Added this with |
||
} | ||
|
||
connect(url : string, username : string, password : string) { | ||
let obj = new URL(url); | ||
let endpoint = obj.host; | ||
|
@@ -276,6 +289,8 @@ export class AgentManager { | |
url: "", | ||
username: "", | ||
endpoint: "", | ||
friendlyname: "", | ||
updatedFriendlyName: "", | ||
}; | ||
|
||
for (let endpoint in list) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ export async function up(knex: Knex): Promise<void> { | |
table.string("url", 255).notNullable().unique(); | ||
table.string("username", 255).notNullable(); | ||
table.string("password", 255).notNullable(); | ||
table.string("friendlyname", 255); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file cannot be modified. Have to create a new file. Migration guide: Also the naming convention, it should be Will come back later, as I would like to finish the next milestone of Uptime Kuma first. |
||
table.boolean("active").notNullable().defaultTo(true); | ||
}); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,8 @@ export class Agent extends BeanModel { | |
url: this.url, | ||
username: this.username, | ||
endpoint: this.endpoint, | ||
friendlyname: this.friendlyname, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. recommendation: you should consider just naming this field |
||
updatedFriendlyName: this.updatedFriendlyName | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot to update this file while adding the other suggestions but it still worked with the correct variables. Where is this needed 🤔 |
||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ export class ManageAgentSocketHandler extends SocketHandler { | |
let data = requestData as LooseObject; | ||
let manager = socket.instanceManager; | ||
await manager.test(data.url, data.username, data.password); | ||
await manager.add(data.url, data.username, data.password); | ||
await manager.add(data.url, data.username, data.password, data.friendlyname); | ||
|
||
// connect to the agent | ||
manager.connect(data.url, data.username, data.password); | ||
|
@@ -66,5 +66,31 @@ export class ManageAgentSocketHandler extends SocketHandler { | |
callbackError(e, callback); | ||
} | ||
}); | ||
|
||
// updateAgent | ||
socket.on("updateAgent", async (friendlyname : string, updatedFriendlyName : string, callback : unknown) => { | ||
try { | ||
log.debug("manage-agent-socket-handler", "updateAgent"); | ||
checkLogin(socket); | ||
|
||
if (typeof(updatedFriendlyName) !== "string") { | ||
throw new Error("FriendlyName must be a string"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the more you know: since this is typescript, you shouldn't actually need to typecheck the input value for friendly name. We already know it's a string from before. |
||
|
||
let manager = socket.instanceManager; | ||
await manager.update(friendlyname, updatedFriendlyName); | ||
|
||
server.disconnectAllSocketClients(undefined, socket.id); | ||
manager.sendAgentList(); | ||
|
||
callbackResult({ | ||
ok: true, | ||
msg: "agentUpdatedSuccessfully", | ||
msgi18n: true, | ||
}, callback); | ||
} catch (e) { | ||
callbackError(e, callback); | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -49,13 +49,25 @@ | |||||||||||||||
</template> | ||||||||||||||||
|
||||||||||||||||
<!-- Agent Display Name --> | ||||||||||||||||
<span v-if="endpoint === ''">{{ $t("currentEndpoint") }}</span> | ||||||||||||||||
<a v-else :href="agent.url" target="_blank">{{ endpoint }}</a> | ||||||||||||||||
<template v-if="$root.agentStatusList[endpoint]"> | ||||||||||||||||
<span v-if="endpoint === '' && agent.friendlyname === ''" class="badge bg-secondary me-2">Controller</span> | ||||||||||||||||
<span v-else-if="agent.friendlyname === ''" :href="agent.url">{{ endpoint }}</span> | ||||||||||||||||
<span v-else :href="agent.url">{{ agent.friendlyname }}</span> | ||||||||||||||||
</template> | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couple of suggestions here:
Suggested change
|
||||||||||||||||
|
||||||||||||||||
<!-- Edit FriendlyName --> | ||||||||||||||||
<font-awesome-icon v-if="agent.friendlyname !== '' && agent.friendlyname !== ''" icon="pen-to-square" @click="showEditAgentFriendlynameDialog[agent.friendlyname] = !showEditAgentFriendlynameDialog[agent.friendlyname]" /> | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: I'm not sure why you're checking for blank string twice here, but if you switch to passing around |
||||||||||||||||
|
||||||||||||||||
<!-- Edit Dialog --> | ||||||||||||||||
<BModal v-model="showEditAgentFriendlynameDialog[agent.friendlyname]" :no-close-on-backdrop="true" :close-on-esc="true" :okTitle="$t('Update Friendlyname')" okVariant="info" @ok="updateFriendlyname(agent.friendlyname, agent.updatedFriendlyName)"> | ||||||||||||||||
<label for="Update Friendlyname" class="form-label">Current value: {{ $t(agent.friendlyname) }}</label> | ||||||||||||||||
<input id="updatedFriendlyName" v-model="agent.updatedFriendlyName" type="text" class="form-control" optional> | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: similar to nathan's comments up above, if you are finding agents by their ID, you can just set |
||||||||||||||||
</BModal> | ||||||||||||||||
|
||||||||||||||||
<!-- Remove Button --> | ||||||||||||||||
<font-awesome-icon v-if="endpoint !== ''" class="ms-2 remove-agent" icon="trash" @click="showRemoveAgentDialog[agent.url] = !showRemoveAgentDialog[agent.url]" /> | ||||||||||||||||
|
||||||||||||||||
<!-- Remoe Agent Dialog --> | ||||||||||||||||
<!-- Remove Agent Dialog --> | ||||||||||||||||
<BModal v-model="showRemoveAgentDialog[agent.url]" :okTitle="$t('removeAgent')" okVariant="danger" @ok="removeAgent(agent.url)"> | ||||||||||||||||
<p>{{ agent.url }}</p> | ||||||||||||||||
{{ $t("removeAgentMsg") }} | ||||||||||||||||
|
@@ -81,6 +93,11 @@ | |||||||||||||||
<input id="password" v-model="agent.password" type="password" class="form-control" required autocomplete="new-password"> | ||||||||||||||||
</div> | ||||||||||||||||
|
||||||||||||||||
<div class="mb-3"> | ||||||||||||||||
<label for="friendlyname" class="form-label">{{ $t("Friendly Name") }}</label> | ||||||||||||||||
<input id="friendlyname" v-model="agent.friendlyname" type="text" class="form-control" optional> | ||||||||||||||||
</div> | ||||||||||||||||
|
||||||||||||||||
<button type="submit" class="btn btn-primary" :disabled="connectingAgent"> | ||||||||||||||||
<template v-if="connectingAgent">{{ $t("connecting") }}</template> | ||||||||||||||||
<template v-else>{{ $t("connect") }}</template> | ||||||||||||||||
|
@@ -121,11 +138,14 @@ export default { | |||||||||||||||
dockerRunCommand: "", | ||||||||||||||||
showAgentForm: false, | ||||||||||||||||
showRemoveAgentDialog: {}, | ||||||||||||||||
showEditAgentFriendlynameDialog: {}, | ||||||||||||||||
connectingAgent: false, | ||||||||||||||||
agent: { | ||||||||||||||||
url: "http://", | ||||||||||||||||
username: "", | ||||||||||||||||
password: "", | ||||||||||||||||
friendlyname: "", | ||||||||||||||||
updatedFriendlyName: "", | ||||||||||||||||
} | ||||||||||||||||
}; | ||||||||||||||||
}, | ||||||||||||||||
|
@@ -199,6 +219,20 @@ export default { | |||||||||||||||
}); | ||||||||||||||||
}, | ||||||||||||||||
|
||||||||||||||||
updateFriendlyname(friendlyname, updatedFriendlyName) { | ||||||||||||||||
//console.log(this.showEditAgentFriendlynameDialog.inputNewFriendlyName); | ||||||||||||||||
this.$root.getSocket().emit("updateAgent", friendlyname, updatedFriendlyName, (res) => { | ||||||||||||||||
this.$root.toastRes(res); | ||||||||||||||||
|
||||||||||||||||
if (res.ok) { | ||||||||||||||||
this.showAgentForm = false; | ||||||||||||||||
this.agent = { | ||||||||||||||||
updatedFriendlyName: "", | ||||||||||||||||
}; | ||||||||||||||||
} | ||||||||||||||||
}); | ||||||||||||||||
}, | ||||||||||||||||
|
||||||||||||||||
getStatusNum(statusName) { | ||||||||||||||||
let num = 0; | ||||||||||||||||
|
||||||||||||||||
|
@@ -286,7 +320,7 @@ export default { | |||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
}, | ||||||||||||||||
}, | ||||||||||||||||
} | ||||||||||||||||
}; | ||||||||||||||||
</script> | ||||||||||||||||
|
||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name Conventions
https://github.com/louislam/dockge/blob/master/CONTRIBUTING.md#name-conventions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all namings has been updated