Skip to content

Commit 5b1fc10

Browse files
authored
Omit wallet configuration credentials from config (#285)
1 parent ed9fa42 commit 5b1fc10

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

src/db/configuration/getConfiguration.ts

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ import { logger } from "../../utils/logger";
77
import { prisma } from "../client";
88
import { updateConfiguration } from "./updateConfiguration";
99

10-
interface Config extends Configuration {
10+
interface Config
11+
extends Omit<
12+
Configuration,
13+
| "awsAccessKeyId"
14+
| "awsSecretAccessKey"
15+
| "awsRegion"
16+
| "gcpApplicationProjectId"
17+
| "gcpKmsLocationId"
18+
| "gcpKmsKeyRingId"
19+
| "gcpApplicationCredentialEmail"
20+
| "gcpApplicationCredentialPrivateKey"
21+
> {
1122
walletConfiguration:
1223
| {
1324
type: WalletType.local;
@@ -29,90 +40,103 @@ interface Config extends Configuration {
2940
}
3041

3142
const withWalletConfig = async (config: Configuration): Promise<Config> => {
43+
// We destructure the config to omit wallet related fields to prevent direct access
44+
const {
45+
awsAccessKeyId,
46+
awsSecretAccessKey,
47+
awsRegion,
48+
gcpApplicationProjectId,
49+
gcpKmsLocationId,
50+
gcpKmsKeyRingId,
51+
gcpApplicationCredentialEmail,
52+
gcpApplicationCredentialPrivateKey,
53+
...restConfig
54+
} = config;
55+
3256
// TODO: Remove backwards compatibility with next breaking change
33-
if (config.awsAccessKeyId && config.awsSecretAccessKey && config.awsRegion) {
57+
if (awsAccessKeyId && awsSecretAccessKey && awsRegion) {
3458
// First try to load the aws secret using the encryption password
35-
let awsSecretAccessKey = decrypt(
36-
config.awsSecretAccessKey,
59+
let decryptedSecretAccessKey = decrypt(
60+
awsSecretAccessKey,
3761
env.ENCRYPTION_PASSWORD,
3862
);
3963

4064
// If that fails, try to load the aws secret using the thirdweb api secret key
4165
if (!awsSecretAccessKey) {
42-
awsSecretAccessKey = decrypt(
43-
config.awsSecretAccessKey,
66+
decryptedSecretAccessKey = decrypt(
67+
awsSecretAccessKey,
4468
env.THIRDWEB_API_SECRET_KEY,
4569
);
4670

4771
// If that succeeds, update the configuration with the encryption password instead
48-
if (awsSecretAccessKey) {
72+
if (decryptedSecretAccessKey) {
4973
logger.worker.info(
5074
`[Encryption] Updating awsSecretAccessKey to use ENCRYPTION_PASSWORD`,
5175
);
5276
await updateConfiguration({
53-
awsSecretAccessKey,
77+
awsSecretAccessKey: decryptedSecretAccessKey,
5478
});
5579
}
5680
}
5781

5882
return {
59-
...config,
83+
...restConfig,
6084
walletConfiguration: {
6185
type: WalletType.awsKms,
62-
awsRegion: config.awsRegion,
63-
awsAccessKeyId: config.awsAccessKeyId,
64-
awsSecretAccessKey,
86+
awsRegion,
87+
awsAccessKeyId,
88+
awsSecretAccessKey: decryptedSecretAccessKey,
6589
},
6690
};
6791
}
6892

6993
// TODO: Remove backwards compatibility with next breaking change
7094
if (
71-
config.gcpApplicationProjectId &&
72-
config.gcpKmsLocationId &&
73-
config.gcpKmsKeyRingId &&
74-
config.gcpApplicationCredentialEmail &&
75-
config.gcpApplicationCredentialPrivateKey
95+
gcpApplicationProjectId &&
96+
gcpKmsLocationId &&
97+
gcpKmsKeyRingId &&
98+
gcpApplicationCredentialEmail &&
99+
gcpApplicationCredentialPrivateKey
76100
) {
77101
// First try to load the gcp secret using the encryption password
78-
let gcpApplicationCredentialPrivateKey = decrypt(
79-
config.gcpApplicationCredentialPrivateKey,
102+
let decryptedGcpKey = decrypt(
103+
gcpApplicationCredentialPrivateKey,
80104
env.ENCRYPTION_PASSWORD,
81105
);
82106

83107
// If that fails, try to load the gcp secret using the thirdweb api secret key
84108
if (!gcpApplicationCredentialPrivateKey) {
85-
gcpApplicationCredentialPrivateKey = decrypt(
86-
config.gcpApplicationCredentialPrivateKey,
109+
decryptedGcpKey = decrypt(
110+
gcpApplicationCredentialPrivateKey,
87111
env.THIRDWEB_API_SECRET_KEY,
88112
);
89113

90114
// If that succeeds, update the configuration with the encryption password instead
91-
if (gcpApplicationCredentialPrivateKey) {
115+
if (decryptedGcpKey) {
92116
logger.worker.info(
93117
`[Encryption] Updating gcpApplicationCredentialPrivateKey to use ENCRYPTION_PASSWORD`,
94118
);
95119
await updateConfiguration({
96-
gcpApplicationCredentialPrivateKey,
120+
gcpApplicationCredentialPrivateKey: decryptedGcpKey,
97121
});
98122
}
99123
}
100124

101125
return {
102-
...config,
126+
...restConfig,
103127
walletConfiguration: {
104128
type: WalletType.gcpKms,
105-
gcpApplicationProjectId: config.gcpApplicationProjectId,
106-
gcpKmsLocationId: config.gcpKmsLocationId,
107-
gcpKmsKeyRingId: config.gcpKmsKeyRingId,
108-
gcpApplicationCredentialEmail: config.gcpApplicationCredentialEmail,
109-
gcpApplicationCredentialPrivateKey,
129+
gcpApplicationProjectId,
130+
gcpKmsLocationId,
131+
gcpKmsKeyRingId,
132+
gcpApplicationCredentialEmail,
133+
gcpApplicationCredentialPrivateKey: decryptedGcpKey,
110134
},
111135
};
112136
}
113137

114138
return {
115-
...config,
139+
...restConfig,
116140
walletConfiguration: {
117141
type: WalletType.local,
118142
},

0 commit comments

Comments
 (0)