opticedge-cloud-utils 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils/secrets.d.ts +8 -0
- package/dist/utils/secrets.js +22 -0
- package/dist/utils/secrets.test.d.ts +1 -0
- package/dist/utils/secrets.test.js +38 -0
- package/package.json +2 -1
- package/src/index.ts +2 -1
- package/src/utils/secrets.test.ts +44 -0
- package/src/utils/secrets.ts +24 -0
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the latest value of a Secret Manager secret.
|
|
3
|
+
*
|
|
4
|
+
* @param projectId – GCP project that owns the secret.
|
|
5
|
+
* @param secretName – Secret name (without version).
|
|
6
|
+
* @returns – UTF-8 string value.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getSecret(projectId: string, secretName: string): Promise<string>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getSecret = getSecret;
|
|
4
|
+
const secret_manager_1 = require("@google-cloud/secret-manager");
|
|
5
|
+
/**
|
|
6
|
+
* Returns the latest value of a Secret Manager secret.
|
|
7
|
+
*
|
|
8
|
+
* @param projectId – GCP project that owns the secret.
|
|
9
|
+
* @param secretName – Secret name (without version).
|
|
10
|
+
* @returns – UTF-8 string value.
|
|
11
|
+
*/
|
|
12
|
+
async function getSecret(projectId, secretName) {
|
|
13
|
+
if (!projectId)
|
|
14
|
+
throw new Error('projectId is required');
|
|
15
|
+
if (!secretName)
|
|
16
|
+
throw new Error('secretName is required');
|
|
17
|
+
const secretClient = new secret_manager_1.SecretManagerServiceClient();
|
|
18
|
+
const [version] = await secretClient.accessSecretVersion({
|
|
19
|
+
name: `projects/${projectId}/secrets/${secretName}/versions/latest`,
|
|
20
|
+
});
|
|
21
|
+
return version.payload?.data?.toString('utf-8') ?? '';
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const secrets_1 = require("./secrets");
|
|
4
|
+
const secret_manager_1 = require("@google-cloud/secret-manager");
|
|
5
|
+
jest.mock('@google-cloud/secret-manager');
|
|
6
|
+
// Mock implementation
|
|
7
|
+
const mockAccessSecretVersion = jest.fn();
|
|
8
|
+
secret_manager_1.SecretManagerServiceClient.mockImplementation(() => ({
|
|
9
|
+
accessSecretVersion: mockAccessSecretVersion,
|
|
10
|
+
}));
|
|
11
|
+
describe('getSecret', () => {
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
jest.clearAllMocks();
|
|
14
|
+
});
|
|
15
|
+
it('returns secret value as string', async () => {
|
|
16
|
+
mockAccessSecretVersion.mockResolvedValue([
|
|
17
|
+
{
|
|
18
|
+
payload: { data: Buffer.from('super-secret-value') },
|
|
19
|
+
},
|
|
20
|
+
]);
|
|
21
|
+
const result = await (0, secrets_1.getSecret)('test-project', 'test-secret');
|
|
22
|
+
expect(result).toBe('super-secret-value');
|
|
23
|
+
expect(mockAccessSecretVersion).toHaveBeenCalledWith({
|
|
24
|
+
name: 'projects/test-project/secrets/test-secret/versions/latest',
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
it('throws if projectId is missing', async () => {
|
|
28
|
+
await expect((0, secrets_1.getSecret)('', 'secret')).rejects.toThrow('projectId is required');
|
|
29
|
+
});
|
|
30
|
+
it('throws if secretName is missing', async () => {
|
|
31
|
+
await expect((0, secrets_1.getSecret)('project', '')).rejects.toThrow('secretName is required');
|
|
32
|
+
});
|
|
33
|
+
it('returns empty string if payload or data is missing', async () => {
|
|
34
|
+
mockAccessSecretVersion.mockResolvedValue([{}]); // no payload
|
|
35
|
+
const result = await (0, secrets_1.getSecret)('project', 'secret');
|
|
36
|
+
expect(result).toBe('');
|
|
37
|
+
});
|
|
38
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opticedge-cloud-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Common utilities for cloud functions",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"author": "Evans Musonda",
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"dependencies": {
|
|
16
|
+
"@google-cloud/secret-manager": "^6.0.1",
|
|
16
17
|
"google-auth-library": "^9.15.1"
|
|
17
18
|
},
|
|
18
19
|
"devDependencies": {
|
package/src/index.ts
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export * from './auth/verify';
|
|
1
|
+
export * from './auth/verify';
|
|
2
|
+
export * from './utils/secrets';
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getSecret } from './secrets';
|
|
2
|
+
import { SecretManagerServiceClient } from '@google-cloud/secret-manager';
|
|
3
|
+
|
|
4
|
+
jest.mock('@google-cloud/secret-manager');
|
|
5
|
+
|
|
6
|
+
// Mock implementation
|
|
7
|
+
const mockAccessSecretVersion = jest.fn();
|
|
8
|
+
(SecretManagerServiceClient as unknown as jest.Mock).mockImplementation(() => ({
|
|
9
|
+
accessSecretVersion: mockAccessSecretVersion,
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
describe('getSecret', () => {
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
jest.clearAllMocks();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('returns secret value as string', async () => {
|
|
18
|
+
mockAccessSecretVersion.mockResolvedValue([
|
|
19
|
+
{
|
|
20
|
+
payload: { data: Buffer.from('super-secret-value') },
|
|
21
|
+
},
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const result = await getSecret('test-project', 'test-secret');
|
|
25
|
+
expect(result).toBe('super-secret-value');
|
|
26
|
+
expect(mockAccessSecretVersion).toHaveBeenCalledWith({
|
|
27
|
+
name: 'projects/test-project/secrets/test-secret/versions/latest',
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('throws if projectId is missing', async () => {
|
|
32
|
+
await expect(getSecret('', 'secret')).rejects.toThrow('projectId is required');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('throws if secretName is missing', async () => {
|
|
36
|
+
await expect(getSecret('project', '')).rejects.toThrow('secretName is required');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('returns empty string if payload or data is missing', async () => {
|
|
40
|
+
mockAccessSecretVersion.mockResolvedValue([{}]); // no payload
|
|
41
|
+
const result = await getSecret('project', 'secret');
|
|
42
|
+
expect(result).toBe('');
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SecretManagerServiceClient } from '@google-cloud/secret-manager';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns the latest value of a Secret Manager secret.
|
|
5
|
+
*
|
|
6
|
+
* @param projectId – GCP project that owns the secret.
|
|
7
|
+
* @param secretName – Secret name (without version).
|
|
8
|
+
* @returns – UTF-8 string value.
|
|
9
|
+
*/
|
|
10
|
+
export async function getSecret(
|
|
11
|
+
projectId: string,
|
|
12
|
+
secretName: string
|
|
13
|
+
): Promise<string> {
|
|
14
|
+
if (!projectId) throw new Error('projectId is required');
|
|
15
|
+
if (!secretName) throw new Error('secretName is required');
|
|
16
|
+
|
|
17
|
+
const secretClient = new SecretManagerServiceClient();
|
|
18
|
+
|
|
19
|
+
const [version] = await secretClient.accessSecretVersion({
|
|
20
|
+
name: `projects/${projectId}/secrets/${secretName}/versions/latest`,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return (version.payload?.data as Buffer)?.toString('utf-8') ?? '';
|
|
24
|
+
}
|