Skip to content

Commit

Permalink
Added http cursor basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
chamorin committed Oct 17, 2023
1 parent 3c055db commit 3b60290
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface RunOptions {
authIssueUrl: string;
delayBeforeStart: number;
cursorPath: string;
httpCursorAuth: string;
productionMode: string;
restartInactivitySeconds: number;
hostname: string;
Expand Down Expand Up @@ -73,6 +74,10 @@ function handleHeaders(value: string, previous: Headers) {
return headers;
}

function handleHttpCursorAuth(value: string) {
return btoa(value);
}

export function run(program: Command, pkg: Package) {
return program.command("run")
.showHelpAfterError()
Expand All @@ -87,6 +92,7 @@ export function run(program: Command, pkg: Package) {
.addOption(new Option("--auth-issue-url <string>", "URL used to issue a token").default(DEFAULT_AUTH_ISSUE_URL).env("AUTH_ISSUE_URL"))
.addOption(new Option("--delay-before-start <int>", "Delay (ms) before starting Substreams").default(DEFAULT_DELAY_BEFORE_START).env("DELAY_BEFORE_START"))
.addOption(new Option("--cursor-path <string>", "File path or URL to cursor lock file").default(DEFAULT_CURSOR_PATH).env("CURSOR_PATH"))
.addOption(new Option("--http-cursor-auth <string>", "Basic auth credentials for http cursor (ex: username:password)").env("HTTP_CURSOR_AUTH").argParser(handleHttpCursorAuth))
.addOption(new Option("--production-mode <boolean>", "Enable production mode, allows cached substreams data if available").default(DEFAULT_PRODUCTION_MODE).env("PRODUCTION_MODE"))
.addOption(new Option("--restart-inactivity-seconds <int>", "If set, the sink will restart when inactive for over a certain amount of seconds").default(DEFAULT_RESTART_INACTIVITY_SECONDS).env("RESTART_INACTIVITY_SECONDS"))
.addOption(new Option("--hostname <string>", "The process will listen on this hostname for any HTTP and Prometheus metrics requests").default(DEFAULT_HOSTNAME).env("HOSTNAME"))
Expand Down
5 changes: 3 additions & 2 deletions src/cursor/httpCursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export function onCursor(emitter: BlockEmitter, cursorPath: string) {
});
}

export async function readCursor(cursorPath: string) {
const response = await fetch(cursorPath);
export async function readCursor(cursorPath: string, httpCursorAuth?: string) {
const headers = httpCursorAuth ? { Authorization: `Basic ${httpCursorAuth}` } : undefined;
const response = await fetch(cursorPath, { headers });

if (!response.ok) {
return "";
Expand Down
2 changes: 1 addition & 1 deletion src/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function handleSession(session: SessionInit) {
}

export function handleManifest(emitter: BlockEmitter, moduleHash: string, options: RunOptions) {
logger.info("manifest", { moduleHash, manifest: options.manifest, substreamsEndpoint: options.substreamsEndpoint, finalBlocksOnly: options.finalBlocksOnly, production_mode: options.productionMode });
logger.info("manifest", { moduleHash, manifest: options.manifest, substreamsEndpoint: options.substreamsEndpoint, finalBlocksOnly: options.finalBlocksOnly, productionMode: options.productionMode });
const labelNames = ["module_hash", "manifest", "output_module", "substreams_endpoint", "start_block_num", "stop_block_num", "production_mode", "final_blocks_only"];
const gauge = registerGauge("manifest", "Substreams manifest and sha256 hash of map module", labelNames) as Gauge;
gauge.labels({
Expand Down
3 changes: 2 additions & 1 deletion src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export async function setup(options: RunOptions) {
const params = options.params;
const headers = options.headers;
const cursorPath = options.cursorPath;
const httpCursorAuth = options.httpCursorAuth;
const productionMode = String(options.productionMode) === "true";
const finalBlocksOnly = String(options.finalBlocksOnly) === "true";

Expand All @@ -51,7 +52,7 @@ export async function setup(options: RunOptions) {
const cursor = cursorPath.startsWith("http") ? httpCursor : fileCursor;

// Connect Transport
const startCursor = await cursor.readCursor(cursorPath);
const startCursor = await cursor.readCursor(cursorPath, httpCursorAuth);
const registry = createRegistry(substreamPackage);
const transport = createDefaultTransport(baseUrl, token, registry, headers);
const request = createRequest({
Expand Down

0 comments on commit 3b60290

Please sign in to comment.