Skip to content

Commit cf69a0b

Browse files
committed
WIP: Generate singular HMAC key and save it in the database
Signed-off-by: Ben <[email protected]>
1 parent 7cdf4be commit cf69a0b

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

src/sdk/namespace_gcp.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class NamespaceGCP {
2727
* private_key: string,
2828
* access_mode: string,
2929
* stats: import('./endpoint_stats_collector').EndpointStatsCollector,
30+
* hmac_key: {
31+
* access_id: string,
32+
* secret_key: string,
33+
* }
3034
* }} params
3135
*/
3236
constructor({
@@ -37,26 +41,16 @@ class NamespaceGCP {
3741
private_key,
3842
access_mode,
3943
stats,
44+
hmac_key,
4045
}) {
4146
this.namespace_resource_id = namespace_resource_id;
4247
this.project_id = project_id;
4348
this.client_email = client_email;
4449
this.private_key = private_key;
45-
this.gcs = new GoogleCloudStorage({
46-
projectId: this.project_id,
47-
credentials: {
48-
client_email: this.client_email,
49-
private_key: this.private_key,
50-
}
51-
});
52-
this.gcs.createHmacKey(client_email).then(res => {
53-
this.hmac_key = res[0];
54-
this.hmac_secret = res[1];
55-
});
5650
this.s3_client = new AWS.S3({
5751
endpoint: 'https://storage.googleapis.com',
58-
accessKeyId: this.hmac_key,
59-
secretAccessKey: this.hmac_secret
52+
accessKeyId: hmac_key.access_id,
53+
secretAccessKey: hmac_key.secret_key
6054
});
6155

6256
this.bucket = target_bucket;

src/sdk/object_sdk.js

+2
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ class ObjectSDK {
402402
/**
403403
* @returns {nb.Namespace}
404404
*/
405+
// resource is a namespace_resource
405406
_setup_single_namespace({ resource: r, path: p }, bucket_id, options) {
406407

407408
if (r.endpoint_type === 'NOOBAA') {
@@ -461,6 +462,7 @@ class ObjectSDK {
461462
private_key,
462463
access_mode: r.access_mode,
463464
stats: this.stats,
465+
hmac_key: r.gcp_hmac_key,
464466
});
465467
}
466468
if (r.fs_root_path || r.fs_root_path === '') {

src/server/system_services/account_server.js

+20
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,26 @@ async function add_external_connection(req) {
737737
};
738738
}
739739

740+
// If the connection is for Google, generate an HMAC key for S3-compatible actions (e.g. multipart uploads)
741+
if (info.endpoint_type === 'GOOGLE') {
742+
dbg.log0('add_external_connection: creating HMAC key for Google connection')
743+
const key_file = JSON.parse(req.rpc_params.secret.unwrap());
744+
const credentials = _.pick(key_file, 'client_email', 'private_key');
745+
const gs_client = new GoogleStorage({ credentials, projectId: key_file.project_id });
746+
try {
747+
const [hmac_key, secret] = await gs_client.createHmacKey(credentials.client_email);
748+
info.gcp_hmac_key = {
749+
access_id: hmac_key.metadata.accessId,
750+
secret_key: system_store.master_key_manager.encrypt_sensitive_string_with_master_key_id(
751+
new SensitiveString(secret), req.account.master_key_id._id)
752+
};
753+
} catch (err) {
754+
// The most likely reason is that the storage account already has 10 existing HMAC keys, which is the limit
755+
dbg.error('add_external_connection: failed to create HMAC key for Google connection', err);
756+
throw new RpcError('INTERNAL_ERROR', 'Failed to create HMAC key for Google connection');
757+
}
758+
}
759+
740760
info.cp_code = req.rpc_params.cp_code || undefined;
741761
info.auth_method = req.rpc_params.auth_method || config.DEFAULT_S3_AUTH_METHOD[info.endpoint_type] || undefined;
742762
info = _.omitBy(info, _.isUndefined);

src/server/system_services/master_key_manager.js

+7
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ class MasterKeysManager {
335335
decipher: crypto.createDecipheriv(m_key.cipher_type, m_key.cipher_key, m_key.cipher_iv)
336336
}, undefined);
337337
}
338+
if (keys.gcp_hmac_key?.secret_key) {
339+
keys.gcp_hmac_key.secret_key = await this.secret_keys_cache.get_with_cache({
340+
encrypted_value: keys.gcp_hmac_key.secret_key,
341+
decipher: crypto.createDecipheriv(m_key.cipher_type, m_key.cipher_key, m_key.cipher_iv)
342+
}, undefined);
343+
344+
}
338345
}
339346
}
340347
}

0 commit comments

Comments
 (0)