Skip to content

Commit

Permalink
GCP - generate RO credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrajodas committed Jan 13, 2025
1 parent 4d5218b commit 7fdd5dd
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function generateReadCredentials(): array
{
$sapi = $this->initSapi();
/** @var string */
$backupId = $sapi->generateId();
$backupId = $this->config->getBackupId() !== '' ? $this->config->getBackupId() : $sapi->generateId();
if ($this->config->isUserDefinedCredentials()) {
$path = $this->config->getPath();
} else {
Expand Down
52 changes: 50 additions & 2 deletions src/Storages/GoogleCloudStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,56 @@ public function __construct(readonly GcsConfig $config, readonly LoggerInterface

public function generateTempReadCredentials(string $backupId, string $path): array
{
// @TODO
return [];
$credentials = new ServiceAccountCredentials(
'https://www.googleapis.com/auth/cloud-platform',
(array) json_decode($this->config->getJsonKey(), true),
);

$httpHandler = HttpHandlerFactory::build();
$authToken = $credentials->fetchAuthToken($httpHandler);

$sts = new CloudSecurityToken(new Google_Client([
'keyFile' => json_decode($this->config->getJsonKey(), true),
]));
$request = new GoogleIdentityStsV1ExchangeTokenRequest();

$permissionOptions = [
'accessBoundary' => [
'accessBoundaryRules' => [
[
'availableResource' => sprintf(
'//storage.googleapis.com/projects/_/buckets/%s',
$this->config->getBucket(),
),
'availablePermissions' => [
'inRole:roles/storage.objectViewer',
],
'availabilityCondition' => [
'expression' => sprintf(
'resource.name == \'projects/_/buckets/%s/objects/%s\'',
$this->config->getBucket(),
$path . 'signedUrls.json',
),
],
],
],
],
];

$request->setOptions(urlencode(json_encode($permissionOptions, JSON_THROW_ON_ERROR)));
$request->setGrantType('urn:ietf:params:oauth:grant-type:token-exchange');
$request->setRequestedTokenType('urn:ietf:params:oauth:token-type:access_token');
$request->setSubjectToken($authToken['access_token']);
$request->setSubjectTokenType('urn:ietf:params:oauth:token-type:access_token');

$response = $sts->v1->token($request);

return [
'backupId' => $backupId,
'region' => $this->config->getRegion(),
'container' => $path,
'credentials' => $response,
];
}

public function getBackup(Client $sapi, string $path): Backup
Expand Down

0 comments on commit 7fdd5dd

Please sign in to comment.