chore: small fixes

This commit is contained in:
apotdevin 2021-12-08 00:42:23 -05:00
parent 69e1a5cbb0
commit 4d4bee26cd
No known key found for this signature in database
GPG key ID: 4403F1DFBE779457
7 changed files with 49 additions and 59 deletions

View file

@ -5,6 +5,7 @@
!README*.md
/node_modules
/.next
/dist
/docs
/.github
.vscode

View file

@ -1,29 +1,3 @@
const ymlEnv = {
YML_ENV_1: process.env.YML_ENV_1 || '',
YML_ENV_2: process.env.YML_ENV_2 || '',
YML_ENV_3: process.env.YML_ENV_3 || '',
YML_ENV_4: process.env.YML_ENV_4 || '',
};
const ssoEnv = {
cookiePath: process.env.COOKIE_PATH || '',
lnServerUrl: process.env.SSO_SERVER_URL || '',
lnCertPath: process.env.SSO_CERT_PATH || '',
macaroonPath: process.env.SSO_MACAROON_PATH || '',
dangerousNoSSOAuth:
process.env.DANGEROUS_NO_SSO_AUTH === 'true' ? true : false,
};
const sslEnv = {
publicUrl: process.env.PUBLIC_URL || '',
sslPort: process.env.SSL_PORT || '',
sslSave: process.env.SSL_SAVE || '',
};
const accountConfig = {
accountConfigPath: process.env.ACCOUNT_CONFIG_PATH || '',
};
const urls = {
mempoolUrl: process.env.MEMPOOL_URL || 'https://mempool.space',
};
@ -33,15 +7,6 @@ module.exports = {
distDir: '../../.next',
poweredByHeader: false,
basePath: process.env.BASE_PATH || '',
serverRuntimeConfig: {
nodeEnv: process.env.NODE_ENV || 'development',
logLevel: process.env.LOG_LEVEL || 'info',
torProxy: process.env.TOR_PROXY_SERVER || '',
...ssoEnv,
...accountConfig,
...sslEnv,
...ymlEnv,
},
publicRuntimeConfig: {
...urls,
nodeEnv: process.env.NODE_ENV || 'development',

View file

@ -16,6 +16,13 @@ type Urls = {
mempool: string;
};
export type YamlEnvs = {
YML_ENV_1: string;
YML_ENV_2: string;
YML_ENV_3: string;
YML_ENV_4: string;
};
type ConfigType = {
isProduction: boolean;
playground: boolean;
@ -27,6 +34,7 @@ type ConfigType = {
sso: SSOConfig;
throttler: Throttler;
urls: Urls;
yamlEnvs: YamlEnvs;
};
export default (): ConfigType => {
@ -57,6 +65,13 @@ export default (): ConfigType => {
limit: Number(process.env.THROTTLE_LIMIT) || 10,
};
const yamlEnvs = {
YML_ENV_1: process.env.YML_ENV_1 || '',
YML_ENV_2: process.env.YML_ENV_2 || '',
YML_ENV_3: process.env.YML_ENV_3 || '',
YML_ENV_4: process.env.YML_ENV_4 || '',
};
const config: ConfigType = {
isProduction,
playground: !isProduction,
@ -68,6 +83,7 @@ export default (): ConfigType => {
sso,
urls,
jwtSecret,
yamlEnvs,
};
if (!isProduction) {

View file

@ -1,4 +1,4 @@
import { Injectable, Inject } from '@nestjs/common';
import { Injectable, Inject, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { FilesService } from '../files/files.service';
import { authenticatedLndGrpc } from 'ln-service';
@ -7,16 +7,18 @@ import { Logger } from 'winston';
import { EnrichedAccount } from './accounts.types';
@Injectable()
export class AccountsService {
export class AccountsService implements OnModuleInit {
accounts: { [key: string]: EnrichedAccount } = {};
constructor(
private configService: ConfigService,
private filesService: FilesService,
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger
) {
) {}
async onModuleInit(): Promise<void> {
// Initialize cookie file if cookie path is provided
filesService.readCookie();
this.filesService.readCookie();
const macaroonPath = this.configService.get('sso.macaroonPath');
const certPath = this.configService.get('sso.certPath');
@ -68,4 +70,16 @@ export class AccountsService {
getAllAccounts() {
return this.accounts;
}
updateAccountMacaroon(id: string, macaroon: string): void {
if (this.accounts?.[id]) {
const { socket, cert } = this.accounts[id];
const { lnd } = authenticatedLndGrpc({ socket, cert, macaroon });
this.accounts[id].macaroon = macaroon;
this.accounts[id].lnd = lnd;
} else {
this.logger.error(`Account not found to update macaroon`, { id });
}
}
}

View file

@ -119,18 +119,19 @@ export class AuthResolver {
const isProduction = this.configService.get('isProduction');
if (account.encrypted) {
// In development NestJS rebuilds the files so this only works in production env.
if (!isProduction) {
this.logger.error(
'Encrypted accounts only work in a production environment'
);
throw new Error('UnableToLogin');
const message =
'Encrypted accounts only work in a production environment';
this.logger.error(message);
throw new Error(message);
}
const macaroon = decodeMacaroon(account.encryptedMacaroon, password);
// Store decrypted macaroon in memory.
// In development NextJS rebuilds the files so this only works in production env.
account.macaroon = macaroon;
this.accountsService.updateAccountMacaroon(id, macaroon);
this.logger.debug(`Decrypted the macaroon for account ${id}`);
} else {

View file

@ -210,7 +210,9 @@ export class FilesService {
masterPassword: string | null,
defaultNetwork: BitcoinNetwork
): ParsedAccount | null {
const resolvedAccount = resolveEnvVarsInAccount(account);
const yamlEnvs = this.configService.get('yamlEnvs');
const resolvedAccount = resolveEnvVarsInAccount(account, yamlEnvs);
const {
name,

View file

@ -1,21 +1,12 @@
import getConfig from 'next/config';
import { YamlEnvs } from '../config/configuration';
import {
AccountType,
UnresolvedAccountType,
} from '../modules/files/files.types';
const { serverRuntimeConfig } = getConfig() || { serverRuntimeConfig: {} };
const { YML_ENV_1, YML_ENV_2, YML_ENV_3, YML_ENV_4 } = serverRuntimeConfig;
const env: { [key: string]: string } = {
YML_ENV_1,
YML_ENV_2,
YML_ENV_3,
YML_ENV_4,
};
export const resolveEnvVarsInAccount = (
account: UnresolvedAccountType
account: UnresolvedAccountType,
yamlEnvs: YamlEnvs
): AccountType => {
const regex = /(?<=\{)(.*?)(?=\})/;
@ -26,7 +17,7 @@ export const resolveEnvVarsInAccount = (
}
const match: string | boolean =
env[v.toString().match(regex)?.[0] || ''] || v;
yamlEnvs[v.toString().match(regex)?.[0] || ''] || v;
if (match === 'true') {
return [k, true];