Skip to content

Commit

Permalink
S3express refactorings (#2841)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamRemis authored Nov 28, 2023
1 parent 434fadb commit d866bc3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 41 deletions.
30 changes: 14 additions & 16 deletions src/Identity/S3/S3ExpressIdentityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@ public function __construct(string $clientRegion, array $config = [])

public function __invoke($command)
{
return function () use ($command) {
$bucket = $command['Bucket'];
if ($identity = $this->cache->get($bucket)) {
if (!$identity->isExpired()) {
return Promise\Create::promiseFor($identity);
}
$bucket = $command['Bucket'];
if ($identity = $this->cache->get($bucket)) {
if (!$identity->isExpired()) {
return Promise\Create::promiseFor($identity);
}
$response = $this->s3Client->createSession(['Bucket' => $bucket]);
$identity = new Aws\Identity\S3\S3ExpressIdentity(
$response['Credentials']['AccessKeyId'],
$response['Credentials']['SecretAccessKey'],
$response['Credentials']['SessionToken'],
$response['Credentials']['Expiration']->getTimestamp()
);
$this->cache->set($bucket, $identity);
return Promise\Create::promiseFor($identity);
};
}
$response = $this->s3Client->createSession(['Bucket' => $bucket]);
$identity = new Aws\Identity\S3\S3ExpressIdentity(
$response['Credentials']['AccessKeyId'],
$response['Credentials']['SecretAccessKey'],
$response['Credentials']['SessionToken'],
$response['Credentials']['Expiration']->getTimestamp()
);
$this->cache->set($bucket, $identity);
return Promise\Create::promiseFor($identity);
}
}
2 changes: 1 addition & 1 deletion src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function (TokenInterface $token)
}

if ($signer instanceof S3ExpressSignature) {
$credentialPromise = $config['s3_express_identity_provider']($command)();
$credentialPromise = $config['s3_express_identity_provider']($command);
} else {
$credentialPromise = $credProvider();
}
Expand Down
2 changes: 1 addition & 1 deletion src/S3/S3Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public function createPresignedRequest(CommandInterface $command, $expires, arra
);
if ($signature_version == 'v4-s3express') {
$provider = $this->getConfig('s3_express_identity_provider');
$credentials = $provider($command)()->wait();
$credentials = $provider($command)->wait();
} else {
$credentials = $this->getCredentials()->wait();
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Identity/S3Express/S3ExpressIdentityProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testProvidesIdentity()
'region',
['client' => $client]
);
$identity = $provider($cmd)()->wait();
$identity = $provider($cmd)->wait();
self::assertSame('foo', $identity->getAccessKeyId());
self::assertSame('bar', $identity->getSecretKey());
self::assertSame('baz', $identity->getSecurityToken());
Expand All @@ -65,8 +65,8 @@ public function testCachesIdentity()
'region',
['client' => $client]
);
$provider($cmd)()->wait();
$identity = $provider($cmd)()->wait();
$provider($cmd)->wait();
$identity = $provider($cmd)->wait();
self::assertSame('foo', $identity->getAccessKeyId());
self::assertSame('bar', $identity->getSecretKey());
self::assertSame('baz', $identity->getSecurityToken());
Expand All @@ -88,8 +88,8 @@ public function testRefreshesCache()
'region',
['client' => $client]
);
$provider($cmd)()->wait();
$identity = $provider($cmd)()->wait();
$provider($cmd)->wait();
$identity = $provider($cmd)->wait();
self::assertSame($after, $identity->getExpiration());
}
}
32 changes: 14 additions & 18 deletions tests/S3/S3ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1229,15 +1229,13 @@ public function testS3ExpressAuth()
$client = new S3Client([
'region' => 'us-east-1',
's3_express_identity_provider' => function ($command) {
return static function () {
$identity = new S3ExpressIdentity(
'foo',
'bar',
'baz',
time() + 4000
);
return Promise\Create::promiseFor($identity);
};
$identity = new S3ExpressIdentity(
'foo',
'bar',
'baz',
time() + 4000
);
return Promise\Create::promiseFor($identity);
},
'http_handler' => function (RequestInterface $r) {
$this->assertSame('baz', $r->getHeaderLine('x-amz-s3session-token'));
Expand Down Expand Up @@ -1302,15 +1300,13 @@ public function testS3ExpressPresignedUrl()
'version' => 'latest',
'region' => 'us-east-1',
's3_express_identity_provider' => function ($command) {
return static function () {
$identity = new S3ExpressIdentity(
'foo',
'bar',
'baz',
time() + 4000
);
return Promise\Create::promiseFor($identity);
};
$identity = new S3ExpressIdentity(
'foo',
'bar',
'baz',
time() + 4000
);
return Promise\Create::promiseFor($identity);
},
]);
$client->getHandlerList()->appendBuild(
Expand Down

0 comments on commit d866bc3

Please sign in to comment.