fix: create api key for connect on startup

This commit is contained in:
Eli Bosley
2025-01-29 12:55:10 -05:00
parent 74b9fd0159
commit 69cd92f974
3 changed files with 31 additions and 26 deletions

View File

@@ -83,17 +83,14 @@ try {
// Start listening to dynamix config file changes // Start listening to dynamix config file changes
setupDynamixConfigWatch(); setupDynamixConfigWatch();
// Disabled until we need the access token to work
// TokenRefresh.init();
// If port is unix socket, delete old socket before starting http server // If port is unix socket, delete old socket before starting http server
unlinkUnixPort(); unlinkUnixPort();
startMiddlewareListeners();
// Start webserver // Start webserver
server = await bootstrapNestServer(); server = await bootstrapNestServer();
startMiddlewareListeners();
// On process exit stop HTTP server // On process exit stop HTTP server
exitHook(async (signal) => { exitHook(async (signal) => {
console.log('exithook', signal); console.log('exithook', signal);

View File

@@ -229,6 +229,11 @@ export const config = createSlice({
const stateAsArray = state.remote.ssoSubIds.split(',').filter((id) => id !== action.payload); const stateAsArray = state.remote.ssoSubIds.split(',').filter((id) => id !== action.payload);
state.remote.ssoSubIds = stateAsArray.join(','); state.remote.ssoSubIds = stateAsArray.join(',');
}, },
setLocalApiKey(state, action: PayloadAction<string | null>) {
if (action.payload) {
state.remote.localApiKey = action.payload;
}
},
}, },
extraReducers(builder) { extraReducers(builder) {
builder.addCase(loadConfigFile.pending, (state) => { builder.addCase(loadConfigFile.pending, (state) => {
@@ -310,6 +315,7 @@ export const {
setWanPortToValue, setWanPortToValue,
setWanAccess, setWanAccess,
removeSsoUser, removeSsoUser,
setLocalApiKey,
} = actions; } = actions;
/** /**
@@ -326,7 +332,8 @@ export const configUpdateActionsFlash = isAnyOf(
setupRemoteAccessThunk.fulfilled, setupRemoteAccessThunk.fulfilled,
logoutUser.fulfilled, logoutUser.fulfilled,
loginUser.fulfilled, loginUser.fulfilled,
removeSsoUser removeSsoUser,
setLocalApiKey
); );
/** /**

View File

@@ -21,7 +21,7 @@ import {
Role, Role,
} from '@app/graphql/generated/api/types'; } from '@app/graphql/generated/api/types';
import { getters, store } from '@app/store'; import { getters, store } from '@app/store';
import { updateUserConfig } from '@app/store/modules/config'; import { setLocalApiKey } from '@app/store/modules/config';
import { FileLoadStatus } from '@app/store/types'; import { FileLoadStatus } from '@app/store/types';
@Injectable() @Injectable()
@@ -151,33 +151,34 @@ export class ApiKeyService implements OnModuleInit {
if (!environment.IS_MAIN_PROCESS) { if (!environment.IS_MAIN_PROCESS) {
return; return;
} }
const { remote, status } = getters.config();
if (getters.config().status !== FileLoadStatus.LOADED) { if (status !== FileLoadStatus.LOADED) {
this.logger.error('Config file not loaded, cannot create local API key'); this.logger.error('Config file not loaded, cannot create local API key');
return; return;
} }
if (!remote.apikey) {
return;
}
const { remote } = getters.config();
// If the remote API Key is set and the local key is either not set or not found on disk, create a key // If the remote API Key is set and the local key is either not set or not found on disk, create a key
if (remote.apikey && (!remote.localApiKey || !this.findByKey(remote.localApiKey))) { if (!remote.localApiKey || !this.findByKey(remote.localApiKey)) {
const hasExistingKey = this.findByField('name', 'Connect'); const existingKey = this.findByField('name', 'Connect');
if (hasExistingKey) { if (existingKey) {
return; this.logger.debug('Found existing Connect key, not set in config, setting');
} store.dispatch(setLocalApiKey(existingKey.key));
// Create local API key
const localApiKey = await this.createLocalConnectApiKey();
if (localApiKey?.key) {
store.dispatch(
updateUserConfig({
remote: {
localApiKey: localApiKey.key,
},
})
);
} else { } else {
this.logger.error('Failed to create local API key - no key returned'); this.logger.debug('Creating a new key for Connect');
// Create local API key
const localApiKey = await this.createLocalConnectApiKey();
if (localApiKey?.key) {
store.dispatch(setLocalApiKey(localApiKey.key));
} else {
this.logger.error('Failed to create local API key - no key returned');
}
} }
} }
} }