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
setupDynamixConfigWatch();
// Disabled until we need the access token to work
// TokenRefresh.init();
// If port is unix socket, delete old socket before starting http server
unlinkUnixPort();
startMiddlewareListeners();
// Start webserver
server = await bootstrapNestServer();
startMiddlewareListeners();
// On process exit stop HTTP server
exitHook(async (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);
state.remote.ssoSubIds = stateAsArray.join(',');
},
setLocalApiKey(state, action: PayloadAction<string | null>) {
if (action.payload) {
state.remote.localApiKey = action.payload;
}
},
},
extraReducers(builder) {
builder.addCase(loadConfigFile.pending, (state) => {
@@ -310,6 +315,7 @@ export const {
setWanPortToValue,
setWanAccess,
removeSsoUser,
setLocalApiKey,
} = actions;
/**
@@ -326,7 +332,8 @@ export const configUpdateActionsFlash = isAnyOf(
setupRemoteAccessThunk.fulfilled,
logoutUser.fulfilled,
loginUser.fulfilled,
removeSsoUser
removeSsoUser,
setLocalApiKey
);
/**

View File

@@ -21,7 +21,7 @@ import {
Role,
} from '@app/graphql/generated/api/types';
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';
@Injectable()
@@ -151,33 +151,34 @@ export class ApiKeyService implements OnModuleInit {
if (!environment.IS_MAIN_PROCESS) {
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');
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 (remote.apikey && (!remote.localApiKey || !this.findByKey(remote.localApiKey))) {
const hasExistingKey = this.findByField('name', 'Connect');
if (!remote.localApiKey || !this.findByKey(remote.localApiKey)) {
const existingKey = this.findByField('name', 'Connect');
if (hasExistingKey) {
return;
}
// Create local API key
const localApiKey = await this.createLocalConnectApiKey();
if (localApiKey?.key) {
store.dispatch(
updateUserConfig({
remote: {
localApiKey: localApiKey.key,
},
})
);
if (existingKey) {
this.logger.debug('Found existing Connect key, not set in config, setting');
store.dispatch(setLocalApiKey(existingKey.key));
} 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');
}
}
}
}