Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(storage): warn when s3 storage has no credentials

Yudhi Armyndharisb2a6ac19b625
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ce243e7f4..04f539d0a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -54,6 +54,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The webhook `secret` example in Swagger and the API reference was shorter than the 16-character floor
the route enforces, so pasting it back answered `400`; it now passes, and the schema publishes the
length bounds ([#1491](https://github.com/rmyndharis/OpenWA/issues/1491)). Thanks @onepay-ye.
+- `STORAGE_TYPE=s3` with no `S3_ACCESS_KEY_ID`/`S3_SECRET_ACCESS_KEY` now warns at startup instead of
+ silently writing every file to local disk and leaving the bucket empty. Thanks @onepay-ye.
### Dependencies
diff --git a/src/common/storage/storage.service.s3.spec.ts b/src/common/storage/storage.service.s3.spec.ts
index ad6087354..686a1314d 100644
--- a/src/common/storage/storage.service.s3.spec.ts
+++ b/src/common/storage/storage.service.s3.spec.ts
@@ -25,6 +25,7 @@ jest.mock('@aws-sdk/client-s3', () => {
});
import { S3Client } from '@aws-sdk/client-s3';
+import { LoggerService } from '../services/logger.service';
import { StorageService } from './storage.service';
// The subset of the AWS SDK's S3Client config this service sets — used purely to type the mock's call
@@ -113,13 +114,20 @@ describe('StorageService (s3) client init', () => {
expect(svc.isS3Available()).toBe(true);
});
- it('falls back to local (no client) when credentials are missing', async () => {
+ it('falls back to local (no client) when credentials are missing, and says so', async () => {
+ // Without a client none of the S3 logging further down ever runs, so this fallback used to be
+ // the one that produced no output at all: the bucket simply stayed empty. Name the two vars
+ // that fix it, since that is the whole point of warning here.
+ const warn = jest.spyOn(LoggerService.prototype, 'warn').mockImplementation(() => undefined);
const svc = new StorageService(makeConfig({ region: 'us-east-1' }));
await flush();
expect(mockedS3Client).not.toHaveBeenCalled();
expect(svc.isS3Available()).toBe(false);
expect(svc.getCurrentStorageType()).toBe('s3');
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('S3_ACCESS_KEY_ID'));
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('S3_SECRET_ACCESS_KEY'));
+ warn.mockRestore();
});
it('prefers canonical env names (S3_ACCESS_KEY_ID / S3_SECRET_ACCESS_KEY) over config', async () => {
diff --git a/src/common/storage/storage.service.ts b/src/common/storage/storage.service.ts
index e556a6678..e091613d7 100644
--- a/src/common/storage/storage.service.ts
+++ b/src/common/storage/storage.service.ts
@@ -92,6 +92,16 @@ export class StorageService implements OnModuleDestroy {
this.s3Bucket = process.env.S3_BUCKET || s3Config.bucket || 'openwa';
void this.initializeS3Bucket();
this.startS3Reprobe();
+ } else {
+ // Every other degradation in this service announces itself, but this one could not: the
+ // logging all lives past the client construction above, so an s3 deployment missing its
+ // credentials built no client, wrote every file to local disk, and said nothing at all.
+ // The operator's first symptom was an empty bucket with no failure to point at.
+ this.logger.warn(
+ `STORAGE_TYPE=s3 but no S3 credentials were found; media is being written to the local dir ` +
+ `'${this.localPath}' instead of the bucket. Set S3_ACCESS_KEY_ID and S3_SECRET_ACCESS_KEY ` +
+ `(the built-in MinIO uses minioadmin/minioadmin).`,
+ );
}
}