Skip to content

Commit 7d9e732

Browse files
fix(GAT-9247): Fixes GSM failure while calling old service no longer … (#1707)
1 parent 879309d commit 7d9e732

7 files changed

Lines changed: 92 additions & 47 deletions

File tree

.env.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ RATE_LIMIT=10000
107107
## Service layer ##
108108
TRASER_SERVICE_URL="http://traser:8002"
109109
TED_SERVICE_URL="http://ted:8001"
110-
GMI_SERVICE_URL="http://metadata-fed:9889"
111110
SEARCH_SERVICE_URL="http://search-service:8080"
112111

113112
GOOGLE_SECRETS_GMI_PREPEND_NAME="dev-mfs-"

app/Http/Controllers/Api/V1/FederationController.php

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
use App\Models\TeamUserHasRole;
2626
use App\Models\User;
2727
use App\Services\GatewayMetadataIngestionService;
28+
use App\Services\GoogleSecretManagerService;
2829
use Auditor;
2930
use Config;
3031
use Exception;
3132
use Illuminate\Http\Request;
32-
use Illuminate\Support\Facades\Http;
3333

3434
class FederationController extends Controller
3535
{
@@ -327,18 +327,14 @@ public function store(CreateFederation $request, int $teamId)
327327

328328
if ($secrets_payload) {
329329
$auth_secret_key_location = config('gateway.google_secrets_gmi_prepend_name') . $federation->id;
330-
$payload = [
331-
"path" => config('gateway.google_application_project_path'),
332-
"secret_id" => $auth_secret_key_location,
333-
"payload" => json_encode($secrets_payload)
334-
];
335-
$response = Http::withHeaders($loggingContext)->post(config('services.gmi.url') . '/federation', $payload);
336330

337-
if (!$response->successful()) {
331+
try {
332+
app(GoogleSecretManagerService::class)->createSecret($auth_secret_key_location, json_encode($secrets_payload));
333+
} catch (Exception $e) {
338334
Federation::where('id', $federation->id)->delete();
339335
return response()->json([
340336
'message' => 'failed to save secrets for this federation',
341-
'details' => $response->json(),
337+
'details' => $e->getMessage(),
342338
], 400);
343339
}
344340

@@ -496,22 +492,16 @@ public function update(UpdateFederation $request, int $teamId, int $federationId
496492

497493
$secrets_payload = $this->getSecretsPayload($input);
498494
if ($secrets_payload) {
499-
$auth_secret_key_location = config('gateway.google_secrets_gmi_prepend_name') . $federationId;
500-
$payload = [
501-
"path" => config('gateway.google_application_project_path'),
502-
"secret_id" => $auth_secret_key_location,
503-
"payload" => json_encode($secrets_payload)
504-
];
505-
506-
$response = Http::withHeaders($loggingContext)->patch(config('services.gmi.url') . '/federation', $payload);
507-
508-
if (!$response->successful()) {
495+
try {
496+
$auth_secret_key_location = $this->upsertFederationSecret($federationId, $secrets_payload);
497+
} catch (Exception $e) {
509498
return response()->json([
510499
'message' => 'something gone wrong with updating federation secret key',
511-
'details' => $response->json(),
500+
'details' => $e->getMessage(),
512501
], 400);
513502
}
514503

504+
Federation::where('id', $federationId)->update(["auth_secret_key_location" => $auth_secret_key_location]);
515505
}
516506

517507
$federationNotifications = FederationHasNotification::where([
@@ -673,22 +663,16 @@ public function edit(EditFederation $request, int $teamId, int $federationId)
673663

674664
$secrets_payload = $this->getSecretsPayload($input);
675665
if ($secrets_payload) {
676-
$auth_secret_key_location = config('gateway.google_secrets_gmi_prepend_name') . $federationId;
677-
$payload = [
678-
"path" => config('gateway.google_application_project_path'),
679-
"secret_id" => $auth_secret_key_location,
680-
"payload" => json_encode($secrets_payload)
681-
];
682-
683-
$response = Http::withHeaders($loggingContext)->patch(config('services.gmi.url') . '/federation', $payload);
684-
685-
if (!$response->successful()) {
666+
try {
667+
$auth_secret_key_location = $this->upsertFederationSecret($federationId, $secrets_payload);
668+
} catch (Exception $e) {
686669
return response()->json([
687670
'message' => 'something gone wrong with updating federation secret key',
688-
'details' => $response->json(),
671+
'details' => $e->getMessage(),
689672
], 400);
690673
}
691674

675+
Federation::where('id', $federationId)->update(["auth_secret_key_location" => $auth_secret_key_location]);
692676
}
693677

694678
if (array_key_exists('notifications', $input)) {
@@ -830,6 +814,15 @@ public function destroy(DeleteFederation $request, int $teamId, int $federationI
830814
FederationHasNotification::where('notification_id', $federationNotification)->delete();
831815
}
832816

817+
$federation = Federation::where('id', $federationId)->first();
818+
if ($federation && $federation->auth_secret_key_location) {
819+
try {
820+
app(GoogleSecretManagerService::class)->deleteSecret($federation->auth_secret_key_location);
821+
} catch (Exception $e) {
822+
\Log::info('failed to delete federation secret: ' . $e->getMessage(), $loggingContext);
823+
}
824+
}
825+
833826
Federation::where('id', $federationId)->delete();
834827

835828
TeamHasFederation::where([
@@ -1006,6 +999,22 @@ public function runNow(RunNowFederation $request, int $teamId, int $federationId
1006999
}
10071000
}
10081001

1002+
private function upsertFederationSecret(int $federationId, array $secretsPayload): string
1003+
{
1004+
$federation = Federation::where('id', $federationId)->first();
1005+
$gsms = app(GoogleSecretManagerService::class);
1006+
1007+
if ($federation->auth_secret_key_location) {
1008+
$gsms->addSecretVersion($federation->auth_secret_key_location, json_encode($secretsPayload));
1009+
return $federation->auth_secret_key_location;
1010+
}
1011+
1012+
$auth_secret_key_location = config('gateway.google_secrets_gmi_prepend_name') . $federationId;
1013+
$gsms->createSecret($auth_secret_key_location, json_encode($secretsPayload));
1014+
1015+
return $auth_secret_key_location;
1016+
}
1017+
10091018
private function getSecretsPayload(array $input)
10101019
{
10111020
$secrets_payload = [];

app/Services/DatasetService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class DatasetService
3535
public function __construct(
3636
private readonly GwdmVersionContext $gwdmVersionContext,
3737
private readonly GwdmHandlerFactory $handlerFactory,
38-
) {}
38+
) {
39+
}
3940

4041
public function list(
4142
?string $filterStatus,

app/Services/GoogleSecretManagerService.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44

55
use Config;
66
use Google\Cloud\SecretManager\V1\AccessSecretVersionRequest;
7+
use Google\Cloud\SecretManager\V1\AddSecretVersionRequest;
78
use Google\Cloud\SecretManager\V1\Client\SecretManagerServiceClient;
9+
use Google\Cloud\SecretManager\V1\CreateSecretRequest;
10+
use Google\Cloud\SecretManager\V1\DeleteSecretRequest;
11+
use Google\Cloud\SecretManager\V1\Replication;
12+
use Google\Cloud\SecretManager\V1\Replication\Automatic;
13+
use Google\Cloud\SecretManager\V1\Secret;
14+
use Google\Cloud\SecretManager\V1\SecretPayload;
815

916
class GoogleSecretManagerService
1017
{
@@ -26,8 +33,42 @@ public function getSecret(string $secretName, string $version = 'latest'): strin
2633
return $response->getPayload()->getData();
2734
}
2835

29-
public function createSecret(): void
36+
public function createSecret(string $secretId, string $payload): void
3037
{
31-
// to do
38+
$projectPath = Config::get('metadata.google_project_path');
39+
40+
$secret = (new Secret())->setReplication(
41+
(new Replication())->setAutomatic(new Automatic())
42+
);
43+
44+
$request = (new CreateSecretRequest())
45+
->setParent($projectPath)
46+
->setSecretId($secretId)
47+
->setSecret($secret);
48+
49+
$this->client->createSecret($request);
50+
51+
$this->addSecretVersion($secretId, $payload);
52+
}
53+
54+
public function addSecretVersion(string $secretId, string $payload): void
55+
{
56+
$projectPath = Config::get('metadata.google_project_path');
57+
58+
$request = (new AddSecretVersionRequest())
59+
->setParent($projectPath . '/secrets/' . $secretId)
60+
->setPayload((new SecretPayload())->setData($payload));
61+
62+
$this->client->addSecretVersion($request);
63+
}
64+
65+
public function deleteSecret(string $secretId): void
66+
{
67+
$projectPath = Config::get('metadata.google_project_path');
68+
69+
$request = (new DeleteSecretRequest())
70+
->setName($projectPath . '/secrets/' . $secretId);
71+
72+
$this->client->deleteSecret($request);
3273
}
3374
}

config/gateway.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"darq_service" => env("DARQ_SERVICE", ""),
77
"feature_flagging_config_url" => env("FEATURE_FLAGGING_CONFIG_URL", ""),
88
"gateway_url" => env("GATEWAY_URL", "http://localhost"),
9-
"google_application_project_path" => env("GOOGLE_APPLICATION_PROJECT_PATH", ""),
109
"google_secrets_gmi_prepend_name" => env("GOOGLE_SECRETS_GMI_PREPEND_NAME", ""),
1110
"media_url" => env("MEDIA_URL", ""),
1211
"omop_seeding_nchunks" => env("OMOP_SEEDING_NCHUNKS", 500),

config/services.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@
105105
'url' => env('TRASER_SERVICE_URL', 'http://localhost:8002'),
106106
],
107107

108-
'gmi' => [
109-
'url' => env('GMI_SERVICE_URL', ''),
110-
],
111-
112108
'mjml' => [
113109
'render_url' => env('MJML_RENDER_URL', ''),
114110
],

tests/Traits/MockExternalApis.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Nyholm\Psr7\Response;
88
use App\Jobs\LinkageExtraction;
99
use App\Jobs\TermExtraction;
10+
use App\Services\GoogleSecretManagerService;
1011
use Illuminate\Support\Facades\Queue;
1112
use Illuminate\Support\Facades\Http;
1213
use Illuminate\Support\Facades\Mail;
@@ -1166,13 +1167,12 @@ public function setUp(): void
11661167
// )
11671168
// ]);
11681169

1169-
Http::fake([
1170-
config('services.gmi.url').'*' => Http::response(
1171-
['message' => 'success'],
1172-
200,
1173-
['application/json']
1174-
)
1175-
]);
1170+
$this->mock(GoogleSecretManagerService::class, function ($mock) {
1171+
$mock->shouldReceive('createSecret')->andReturn(null);
1172+
$mock->shouldReceive('addSecretVersion')->andReturn(null);
1173+
$mock->shouldReceive('deleteSecret')->andReturn(null);
1174+
$mock->shouldReceive('getSecret')->andReturn(json_encode(['bearer_token' => 'fake-token']));
1175+
});
11761176

11771177
Http::fake([
11781178
// DELETE

0 commit comments

Comments
 (0)