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 !README*.md
/node_modules /node_modules
/.next /.next
/dist
/docs /docs
/.github /.github
.vscode .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 = { const urls = {
mempoolUrl: process.env.MEMPOOL_URL || 'https://mempool.space', mempoolUrl: process.env.MEMPOOL_URL || 'https://mempool.space',
}; };
@ -33,15 +7,6 @@ module.exports = {
distDir: '../../.next', distDir: '../../.next',
poweredByHeader: false, poweredByHeader: false,
basePath: process.env.BASE_PATH || '', 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: { publicRuntimeConfig: {
...urls, ...urls,
nodeEnv: process.env.NODE_ENV || 'development', nodeEnv: process.env.NODE_ENV || 'development',

View file

@ -16,6 +16,13 @@ type Urls = {
mempool: string; mempool: string;
}; };
export type YamlEnvs = {
YML_ENV_1: string;
YML_ENV_2: string;
YML_ENV_3: string;
YML_ENV_4: string;
};
type ConfigType = { type ConfigType = {
isProduction: boolean; isProduction: boolean;
playground: boolean; playground: boolean;
@ -27,6 +34,7 @@ type ConfigType = {
sso: SSOConfig; sso: SSOConfig;
throttler: Throttler; throttler: Throttler;
urls: Urls; urls: Urls;
yamlEnvs: YamlEnvs;
}; };
export default (): ConfigType => { export default (): ConfigType => {
@ -57,6 +65,13 @@ export default (): ConfigType => {
limit: Number(process.env.THROTTLE_LIMIT) || 10, 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 = { const config: ConfigType = {
isProduction, isProduction,
playground: !isProduction, playground: !isProduction,
@ -68,6 +83,7 @@ export default (): ConfigType => {
sso, sso,
urls, urls,
jwtSecret, jwtSecret,
yamlEnvs,
}; };
if (!isProduction) { 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 { ConfigService } from '@nestjs/config';
import { FilesService } from '../files/files.service'; import { FilesService } from '../files/files.service';
import { authenticatedLndGrpc } from 'ln-service'; import { authenticatedLndGrpc } from 'ln-service';
@ -7,16 +7,18 @@ import { Logger } from 'winston';
import { EnrichedAccount } from './accounts.types'; import { EnrichedAccount } from './accounts.types';
@Injectable() @Injectable()
export class AccountsService { export class AccountsService implements OnModuleInit {
accounts: { [key: string]: EnrichedAccount } = {}; accounts: { [key: string]: EnrichedAccount } = {};
constructor( constructor(
private configService: ConfigService, private configService: ConfigService,
private filesService: FilesService, private filesService: FilesService,
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger
) { ) {}
async onModuleInit(): Promise<void> {
// Initialize cookie file if cookie path is provided // Initialize cookie file if cookie path is provided
filesService.readCookie(); this.filesService.readCookie();
const macaroonPath = this.configService.get('sso.macaroonPath'); const macaroonPath = this.configService.get('sso.macaroonPath');
const certPath = this.configService.get('sso.certPath'); const certPath = this.configService.get('sso.certPath');
@ -68,4 +70,16 @@ export class AccountsService {
getAllAccounts() { getAllAccounts() {
return this.accounts; 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'); const isProduction = this.configService.get('isProduction');
if (account.encrypted) { if (account.encrypted) {
// In development NestJS rebuilds the files so this only works in production env.
if (!isProduction) { if (!isProduction) {
this.logger.error( const message =
'Encrypted accounts only work in a production environment' 'Encrypted accounts only work in a production environment';
);
throw new Error('UnableToLogin'); this.logger.error(message);
throw new Error(message);
} }
const macaroon = decodeMacaroon(account.encryptedMacaroon, password); const macaroon = decodeMacaroon(account.encryptedMacaroon, password);
// Store decrypted macaroon in memory. // Store decrypted macaroon in memory.
// In development NextJS rebuilds the files so this only works in production env. this.accountsService.updateAccountMacaroon(id, macaroon);
account.macaroon = macaroon;
this.logger.debug(`Decrypted the macaroon for account ${id}`); this.logger.debug(`Decrypted the macaroon for account ${id}`);
} else { } else {

View file

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

View file

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