|
25 | 25 | use App\Models\TeamUserHasRole; |
26 | 26 | use App\Models\User; |
27 | 27 | use App\Services\GatewayMetadataIngestionService; |
| 28 | +use App\Services\GoogleSecretManagerService; |
28 | 29 | use Auditor; |
29 | 30 | use Config; |
30 | 31 | use Exception; |
31 | 32 | use Illuminate\Http\Request; |
32 | | -use Illuminate\Support\Facades\Http; |
33 | 33 |
|
34 | 34 | class FederationController extends Controller |
35 | 35 | { |
@@ -327,18 +327,14 @@ public function store(CreateFederation $request, int $teamId) |
327 | 327 |
|
328 | 328 | if ($secrets_payload) { |
329 | 329 | $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); |
336 | 330 |
|
337 | | - if (!$response->successful()) { |
| 331 | + try { |
| 332 | + app(GoogleSecretManagerService::class)->createSecret($auth_secret_key_location, json_encode($secrets_payload)); |
| 333 | + } catch (Exception $e) { |
338 | 334 | Federation::where('id', $federation->id)->delete(); |
339 | 335 | return response()->json([ |
340 | 336 | 'message' => 'failed to save secrets for this federation', |
341 | | - 'details' => $response->json(), |
| 337 | + 'details' => $e->getMessage(), |
342 | 338 | ], 400); |
343 | 339 | } |
344 | 340 |
|
@@ -496,22 +492,16 @@ public function update(UpdateFederation $request, int $teamId, int $federationId |
496 | 492 |
|
497 | 493 | $secrets_payload = $this->getSecretsPayload($input); |
498 | 494 | 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) { |
509 | 498 | return response()->json([ |
510 | 499 | 'message' => 'something gone wrong with updating federation secret key', |
511 | | - 'details' => $response->json(), |
| 500 | + 'details' => $e->getMessage(), |
512 | 501 | ], 400); |
513 | 502 | } |
514 | 503 |
|
| 504 | + Federation::where('id', $federationId)->update(["auth_secret_key_location" => $auth_secret_key_location]); |
515 | 505 | } |
516 | 506 |
|
517 | 507 | $federationNotifications = FederationHasNotification::where([ |
@@ -673,22 +663,16 @@ public function edit(EditFederation $request, int $teamId, int $federationId) |
673 | 663 |
|
674 | 664 | $secrets_payload = $this->getSecretsPayload($input); |
675 | 665 | 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) { |
686 | 669 | return response()->json([ |
687 | 670 | 'message' => 'something gone wrong with updating federation secret key', |
688 | | - 'details' => $response->json(), |
| 671 | + 'details' => $e->getMessage(), |
689 | 672 | ], 400); |
690 | 673 | } |
691 | 674 |
|
| 675 | + Federation::where('id', $federationId)->update(["auth_secret_key_location" => $auth_secret_key_location]); |
692 | 676 | } |
693 | 677 |
|
694 | 678 | if (array_key_exists('notifications', $input)) { |
@@ -830,6 +814,15 @@ public function destroy(DeleteFederation $request, int $teamId, int $federationI |
830 | 814 | FederationHasNotification::where('notification_id', $federationNotification)->delete(); |
831 | 815 | } |
832 | 816 |
|
| 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 | + |
833 | 826 | Federation::where('id', $federationId)->delete(); |
834 | 827 |
|
835 | 828 | TeamHasFederation::where([ |
@@ -1006,6 +999,22 @@ public function runNow(RunNowFederation $request, int $teamId, int $federationId |
1006 | 999 | } |
1007 | 1000 | } |
1008 | 1001 |
|
| 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 | + |
1009 | 1018 | private function getSecretsPayload(array $input) |
1010 | 1019 | { |
1011 | 1020 | $secrets_payload = []; |
|
0 commit comments