Skip to content

Commit 4641053

Browse files
authored
feat: add signature to actor get-public-url (#767)
This PR is part of apify/apify-core#19363, which updates `KeyValueStore.getPublicUrl(recordKey)` to generate signed links using HMAC. This PR: - adds signature to actor get-public-url to Apify CLI
1 parent df5d049 commit 4641053

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"@apify/actor-templates": "^0.1.5",
6363
"@apify/consts": "^2.36.0",
6464
"@apify/input_schema": "^3.12.0",
65-
"@apify/utilities": "^2.12.0",
65+
"@apify/utilities": "^2.15.1",
6666
"@crawlee/memory-storage": "^3.12.0",
6767
"@oclif/core": "~4.2.0",
6868
"@oclif/plugin-help": "~6.2.8",

src/commands/actor/get-public-url.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ACTOR_ENV_VARS, APIFY_ENV_VARS } from '@apify/consts';
2+
import { createHmacSignature } from '@apify/utilities';
23
import { Args } from '@oclif/core';
34

5+
import { getApifyStorageClient } from '../../lib/actor.js';
46
import { ApifyCommand } from '../../lib/apify_command.js';
57
import { CommandExitCodes } from '../../lib/consts.js';
68
import { error } from '../../lib/outputs.js';
@@ -27,6 +29,35 @@ export class GetPublicUrlCommand extends ApifyCommand<typeof GetPublicUrlCommand
2729
const apiBase = process.env[APIFY_ENV_VARS.API_PUBLIC_BASE_URL];
2830
const storeId = process.env[ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID];
2931

30-
console.log(`${apiBase}/v2/key-value-stores/${storeId}/records/${key}`);
32+
// This should never happen, but handle it gracefully to prevent crashes.
33+
if (!storeId) {
34+
error({
35+
message: `Missing environment variable: ${ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID}. Please set it before running the command.`,
36+
});
37+
process.exitCode = CommandExitCodes.InvalidInput;
38+
return;
39+
}
40+
41+
const apifyClient = await getApifyStorageClient();
42+
const store = await apifyClient.keyValueStore(storeId).get();
43+
44+
const publicUrl = new URL(`${apiBase}/v2/key-value-stores/${storeId}/records/${key}`);
45+
46+
if (!store) {
47+
error({
48+
message: `Key-Value store with ID '${storeId}' was not found. Ensure the store exists and that the correct ID is set in ${ACTOR_ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID}.`,
49+
});
50+
process.exitCode = CommandExitCodes.NotFound;
51+
return;
52+
}
53+
54+
// @ts-expect-error Add types to client
55+
const { urlSigningSecretKey } = store;
56+
57+
if (urlSigningSecretKey) {
58+
publicUrl.searchParams.append('signature', createHmacSignature(urlSigningSecretKey as string, key));
59+
}
60+
61+
console.log(publicUrl.toString());
3162
}
3263
}

src/lib/consts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,6 @@ export enum CommandExitCodes {
112112
InvalidInput = 5,
113113
InvalidActorJson = 5,
114114

115+
NotFound = 250,
115116
NotImplemented = 255,
116117
}

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ __metadata:
130130
languageName: node
131131
linkType: hard
132132

133-
"@apify/utilities@npm:^2.12.0, @apify/utilities@npm:^2.15.1, @apify/utilities@npm:^2.7.10, @apify/utilities@npm:^2.9.3":
133+
"@apify/utilities@npm:^2.15.1, @apify/utilities@npm:^2.7.10, @apify/utilities@npm:^2.9.3":
134134
version: 2.15.1
135135
resolution: "@apify/utilities@npm:2.15.1"
136136
dependencies:
@@ -3886,7 +3886,7 @@ __metadata:
38863886
"@apify/eslint-config-ts": "npm:^0.4.1"
38873887
"@apify/input_schema": "npm:^3.12.0"
38883888
"@apify/tsconfig": "npm:^0.1.0"
3889-
"@apify/utilities": "npm:^2.12.0"
3889+
"@apify/utilities": "npm:^2.15.1"
38903890
"@biomejs/biome": "npm:^1.8.3"
38913891
"@crawlee/memory-storage": "npm:^3.12.0"
38923892
"@crawlee/types": "npm:^3.11.1"

0 commit comments

Comments
 (0)