Skip to content

Commit

Permalink
Correctly inject credentials into server-settings, fix #11
Browse files Browse the repository at this point in the history
Web side change to display short SHA in footer
  • Loading branch information
circlesabound committed Oct 22, 2024
1 parent 65f7fce commit 51d5693
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
65 changes: 62 additions & 3 deletions src/agent/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ impl AgentController {

// Server settings is required to start
// Pre-populate with the example file if not exist
let server_settings;
let mut server_settings;
match ServerSettings::read_or_apply_default(version).await {
Ok(ss) => server_settings = ss,
Err(_e) => {
Expand All @@ -862,6 +862,57 @@ impl AgentController {
}
}

// If game is public visibility, we need factorio.com credentials
if server_settings.config.visibility.public {
match Secrets::read().await {
Ok(Some(secrets)) => {
if secrets.username.is_empty() || secrets.token.is_empty() {
self.reply_failed(
AgentOutMessage::Error(
"Missing credentials required for server visible to public".to_owned(),
),
operation_id,
)
.await;
return;
}

// Write them into the config file, since there's no other way to pass them in
server_settings.config.username = Some(secrets.username);
server_settings.config.token = Some(secrets.token);
if let Err(_) = ServerSettings::write(&server_settings).await {
self.reply_failed(
AgentOutMessage::Error(
"Failed to write to server settings file".to_owned()
),
operation_id,
).await;
return;
}
},
Ok(None) => {
self.reply_failed(
AgentOutMessage::Error(
"Missing credentials required for server visible to public".to_owned(),
),
operation_id,
)
.await;
return;
},
Err(_) => {
self.reply_failed(
AgentOutMessage::Error(
"Failed to read secrets".to_owned(),
),
operation_id,
)
.await;
return;
},
}
}

// Admin list
let admin_list;
match AdminList::read_or_apply_default().await {
Expand Down Expand Up @@ -1604,7 +1655,10 @@ impl AgentController {
}

async fn config_server_settings_get(&self, operation_id: OperationId) {
if let Ok(Some(ss)) = ServerSettings::read().await {
if let Ok(Some(mut ss)) = ServerSettings::read().await {
// strip any credentials from the return
ss.config.username = None;
ss.config.token = None;
self.reply_success(
AgentOutMessage::ConfigServerSettings(ss.config),
operation_id,
Expand All @@ -1613,10 +1667,15 @@ impl AgentController {
return;
}

// if there's no existing file, we need to ensure there's an installed Factorio version
// to generate a default from
let vm = self.version_manager.read().await;
if let Some((_, version)) = vm.versions.iter().next() {
match ServerSettings::read_or_apply_default(version).await {
Ok(ss) => {
Ok(mut ss) => {
// strip any credentials from the return
ss.config.username = None;
ss.config.token = None;
self.reply_success(
AgentOutMessage::ConfigServerSettings(ss.config),
operation_id,
Expand Down
5 changes: 4 additions & 1 deletion src/agent/server/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,10 @@ impl ServerSettings {
Some(ls) => Ok(ls),
None => {
info!("Generating server settings using defaults");
let config = ServerSettings::read_default_server_settings(installation).await?;
let mut config = ServerSettings::read_default_server_settings(installation).await?;
// clear the default empty secrets
config.username = None;
config.token = None;
let s = ServerSettings {
config,
path: SERVER_SETTINGS_PATH.clone(),
Expand Down
3 changes: 3 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,9 @@ pub struct ServerSettingsConfig {
pub autosave_only_on_server: bool,
pub non_blocking_saving: bool,

pub username: Option<String>,
pub token: Option<String>,

pub game_password: String,
pub require_user_verification: bool,
pub max_players: u32,
Expand Down
4 changes: 2 additions & 2 deletions web/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ <h1>fctrl</h1>

<footer class="footer">
<div class="content has-text-centered has-text-grey">
<p class="is-size-7 py-0 my-0">agent: {{agentBuildInfo?.commit_hash}}&#64;{{agentBuildInfo?.timestamp}}</p>
<p class="is-size-7 py-0 my-0">mgmt-server: {{agentBuildInfo?.commit_hash}}&#64;{{mgmtServerBuildInfo?.timestamp}}</p>
<p class="is-size-7 py-0 my-0">agent: {{agentBuildInfo?.commit_hash?.slice(0, 7)}}&#64;{{agentBuildInfo?.timestamp}}</p>
<p class="is-size-7 py-0 my-0">mgmt-server: {{agentBuildInfo?.commit_hash?.slice(0, 7)}}&#64;{{mgmtServerBuildInfo?.timestamp}}</p>
</div>
</footer>
2 changes: 1 addition & 1 deletion web/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { faChartBar, faCogs, faTerminal, faWrench } from '@fortawesome/free-soli
import { filter, map, mergeMap } from 'rxjs/operators';
import { OperationService } from './operation.service';
import { MgmtServerRestApiService } from './mgmt-server-rest-api/services';
import { BuildInfoObject, BuildVersion } from './mgmt-server-rest-api/models';
import { BuildVersion } from './mgmt-server-rest-api/models';

@Component({
selector: 'app-root',
Expand Down

0 comments on commit 51d5693

Please sign in to comment.