opticedge-cloud-utils 1.0.1 → 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.
@@ -0,0 +1,2 @@
1
+ export * from './auth/verify';
2
+ export * from './utils/secrets';
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./auth/verify"), exports);
18
+ __exportStar(require("./utils/secrets"), exports);
@@ -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.1",
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 ADDED
@@ -0,0 +1,2 @@
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
+ }
@@ -1,41 +0,0 @@
1
- name: Publish Package
2
-
3
- on:
4
- push:
5
- branches:
6
- - main
7
-
8
- jobs:
9
- publish:
10
- runs-on: ubuntu-latest
11
-
12
- steps:
13
- - name: Checkout repository
14
- uses: actions/checkout@v3
15
-
16
- - name: Setup Git identity
17
- run: |
18
- git config --global user.name "github-actions[bot]"
19
- git config --global user.email "github-actions[bot]@users.noreply.github.com"
20
-
21
- - name: Setup Node.js
22
- uses: actions/setup-node@v3
23
- with:
24
- node-version: '18'
25
- registry-url: 'https://registry.npmjs.org/'
26
-
27
- - name: Install dependencies
28
- run: npm install
29
-
30
- - name: Run tests
31
- run: npm test
32
-
33
- - name: Build package
34
- run: npm run build
35
-
36
- - name: Bump patch version and publish to npm
37
- env:
38
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
39
- run: |
40
- npm version patch -m "ci: bump version to %s [skip ci]"
41
- npm publish
File without changes
File without changes
File without changes