From 8fa8165fddf07212b02728a63d5db77f96366ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 21 Jan 2025 10:21:37 +0100 Subject: [PATCH] Update reading secrets from config file --- module.nix | 10 +++++----- src/consumers/influxdb.ts | 3 ++- src/consumers/mqtt.ts | 9 +++++---- src/tauron/index.ts | 5 +++-- src/types/mqtt.ts | 4 ++-- src/types/tauron.ts | 4 ++-- src/utils/index.ts | 20 +++++++++++++++++++- 7 files changed, 38 insertions(+), 17 deletions(-) diff --git a/module.nix b/module.nix index f87d0a4..981cade 100644 --- a/module.nix +++ b/module.nix @@ -23,8 +23,8 @@ in { timezone = "Europe/Warsaw"; tauron = { - usernamePath = "/run/tauron.username.key"; - passwordPath = "/run/tauron.password.key"; + username = "$_file:/run/tauron.username.key"; + password = "$_file:/run/tauron.password.key"; point = "123456"; }; @@ -32,7 +32,7 @@ in { influxdb = { enable = true; databaseURL = "https://influxdb.lan"; - apiToken = "/run/mqtt/influxdb.token.key"; + apiToken = "$__file:/run/mqtt/influxdb.token.key"; organization = "home"; bucket = "tauron"; }; @@ -40,8 +40,8 @@ in { mqtt = { enable = true; brokerURL = "https://mqtt.lan"; - usernamePath = "/run/mqtt.username.key"; - passwordPath = "/run/mqtt.password.key"; + username = "$__file:/run/mqtt.username.key"; + password = "$__file:/run/mqtt.password.key"; clientId = "tauron-scrapper"; prefix = "tauron"; publishOptions = { diff --git a/src/consumers/influxdb.ts b/src/consumers/influxdb.ts index 104d1ad..aa2639b 100644 --- a/src/consumers/influxdb.ts +++ b/src/consumers/influxdb.ts @@ -3,6 +3,7 @@ import { InfluxDB, Point } from "@influxdata/influxdb-client"; import { InfluxDBConfig, Measurement } from "@types"; import { Consumer } from "./abstract"; import { Dayjs } from "dayjs"; +import { enhancedStringConfig } from "../utils"; export class InfluxDBConsumer extends Consumer { public name = 'influxdb'; @@ -17,7 +18,7 @@ export class InfluxDBConsumer extends Consumer { protected async publish({ databaseURL, apiToken, organization, bucket }: InfluxDBConfig, date: Dayjs, measurement: Measurement) { const db = new InfluxDB({ url: databaseURL, - token: readFileSync(apiToken, 'utf8'), + token: enhancedStringConfig(apiToken), }); const api = db.getWriteApi(organization, bucket); diff --git a/src/consumers/mqtt.ts b/src/consumers/mqtt.ts index 94a49e8..e3cc42e 100644 --- a/src/consumers/mqtt.ts +++ b/src/consumers/mqtt.ts @@ -4,17 +4,18 @@ import { Measurement } from "@types"; import { Dayjs } from 'dayjs'; import { MQTTConfig } from '../types/mqtt'; import { Consumer } from './abstract'; +import { enhancedStringConfig } from '../utils'; export * from '../types/mqtt'; export class MQTTConsumer extends Consumer { public name = "mqtt"; - protected requiredFields = ['brokerURL', 'usernamePath', 'passwordPath'] as const; + protected requiredFields = ['brokerURL', 'username', 'password'] as const; - protected publish({ brokerURL, usernamePath, passwordPath, clientId, prefix, publishOptions }: MQTTConfig, date: Dayjs, measurement: Measurement) { + protected publish({ brokerURL, username, password, clientId, prefix, publishOptions }: MQTTConfig, date: Dayjs, measurement: Measurement) { const client = mqtt.connect(brokerURL, { - username: fs.readFileSync(usernamePath, 'utf8'), - password: fs.readFileSync(passwordPath, 'utf8'), + username: enhancedStringConfig(username), + password: enhancedStringConfig(password), clientId: clientId || "tauron-scrapper" }); diff --git a/src/tauron/index.ts b/src/tauron/index.ts index 6807953..49ff619 100644 --- a/src/tauron/index.ts +++ b/src/tauron/index.ts @@ -4,6 +4,7 @@ import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, Readin import { TauronConfig } from '../config'; import { Dayjs } from 'dayjs'; import { withCookies } from './cookie'; +import { enhancedStringConfig } from '../utils'; export * from '../types/tauron'; @@ -55,8 +56,8 @@ export class Tauron { await this.#http.get(LOGIN_API); await this.#http.postForm(LOGIN_API, { - username: fs.readFileSync(this.#config.usernamePath, 'utf8'), - password: fs.readFileSync(this.#config.passwordPath, 'utf8'), + username: enhancedStringConfig(this.#config.username), + password: enhancedStringConfig(this.#config.password), service: BASE_URL }); diff --git a/src/types/mqtt.ts b/src/types/mqtt.ts index 5b066e3..a863907 100644 --- a/src/types/mqtt.ts +++ b/src/types/mqtt.ts @@ -11,12 +11,12 @@ export type MQTTConfig = { /** * Path to file containing a username of MQTT user. */ - usernamePath: string; + username: string; /** * Path to file containing a password of MQTT user. */ - passwordPath: string; + password: string; /** * Optional client ID used to connect to MQTT (visible in MQTT broker logs). diff --git a/src/types/tauron.ts b/src/types/tauron.ts index 284c13b..a172b8e 100644 --- a/src/types/tauron.ts +++ b/src/types/tauron.ts @@ -2,12 +2,12 @@ export type TauronConfig = { /** * Path to file containing a username of Tauron account */ - usernamePath: string; + username: string; /** * Path to file containing a password of Tauron account */ - passwordPath: string; + password: string; /** * The measure point name - should be retrieved with browser dev-tools on website diff --git a/src/utils/index.ts b/src/utils/index.ts index ec012d0..35dbf7b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,5 @@ +import { readFileSync } from "fs"; + export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); export const gaussianRandom = (mean = 0, stddev = 1) => { @@ -5,4 +7,20 @@ export const gaussianRandom = (mean = 0, stddev = 1) => { let u2 = Math.random(); let z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2.0 * Math.PI * u2); return z0 * stddev + mean; -} \ No newline at end of file +} + +const specialOptions: Record string> = { + $__file: (arg: string) => readFileSync(arg, 'utf8').trim() +}; + +export const enhancedStringConfig = (value: string) => { + const trimmed = value.trim(); + + for(const opt of Object.keys(specialOptions)) { + if(trimmed.startsWith(`${opt}:`) && opt in specialOptions) { + return specialOptions[opt](trimmed.slice(opt.length + 1).trim()); + } + } + + return trimmed; +}; \ No newline at end of file