Skip to content

Commit

Permalink
fix: Fix secure and playground connection (#43)
Browse files Browse the repository at this point in the history
Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
Co-authored-by: Charith Ellawala <charithe@users.noreply.github.com>
  • Loading branch information
oguzhand95 and charithe committed Jun 22, 2023
1 parent 4647f08 commit 5c0fac1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
4 changes: 4 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
threads="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand All @@ -9,5 +10,8 @@
<projectFiles>
<directory name="src/Sdk" />
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
34 changes: 25 additions & 9 deletions src/Sdk/Builder/CerbosClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,32 @@ public function withPlayground(string $playgroundInstanceId): CerbosClientBuilde
* @throws Exception
*/
public function build(): CerbosClient {
if (!is_null($this->caCertificate) && !is_null($this->tlsKey) && !is_null($this->tlsCertificate)){
$credentials = ChannelCredentials::createSsl(
$this->caCertificate,
$this->tlsKey,
$this->tlsCertificate
);
} else if ($this->plaintext) {
if ($this->plaintext) {
if ($this->playgroundInstanceId != "") {
throw new Exception("cannot use a plaintext connection to interact with the Cerbos Playground");
}

$credentials = ChannelCredentials::createInsecure();
} else {
throw new Exception("either use the withPlaintext(true) or provide tlsKey and tlsCertificate");
}
else if (!is_null($this->caCertificate)) {
if (!is_null($this->tlsCertificate) && !is_null($this->tlsKey)) {
$credentials = ChannelCredentials::createSsl(
$this->caCertificate,
$this->tlsKey,
$this->tlsCertificate
);
}
else {
$credentials = ChannelCredentials::createSsl(
$this->caCertificate
);
}
}
else {
/**
* @psalm-suppress TooFewArguments
*/
$credentials = ChannelCredentials::createSsl();
}

$csc = new CerbosServiceClient(
Expand Down
30 changes: 30 additions & 0 deletions tests/CerbosClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,34 @@ public function testPlanResourcesValidation(): void{
$this->assertFalse($planResourcesResult->isAlwaysAllowed(), "planResourcesResult is always allowed");
$this->assertFalse($planResourcesResult->isConditional(), "planResourcesResult is conditional");
}

public function testPlayground(): void {
$request = CheckResourcesRequest::newInstance()
->withRequestId("1")
->withPrincipal(
Principal::newInstance("sajit")
->withRole("ADMIN")
->withAttribute("department", AttributeValue::stringValue("IT"))
)
->withResourceEntry(
ResourceEntry::newInstance("expense", "XX125")
->withAttribute("ownerId", AttributeValue::stringValue("sally"))
->withAttribute("createdAt", AttributeValue::stringValue("2021-10-01T10:00:00.021-05:00"))
->withAttribute("vendor", AttributeValue::stringValue("Flux Water Gear"))
->withAttribute("region", AttributeValue::stringValue("EMEA"))
->withAttribute("amount", AttributeValue::intValue(500))
->withAttribute("status", AttributeValue::stringValue("OPEN"))
->withActions(["approve", "delete"])
);

try {
$checkResourcesResult = $this->playgroundClient->checkResources($request);
$resultEntry = $checkResourcesResult->find("XX125");
} catch (Exception $e) {
$this->fail($e->getMessage());
}

$this->assertTrue($resultEntry->isAllowed("approve"), "result of XX125 for approve action is wrong");
$this->assertTrue($resultEntry->isAllowed("delete"), "result of XX125 for delete action is wrong");
}
}
6 changes: 4 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

abstract class TestCase extends \PHPUnit\Framework\TestCase
{
private string $host = "localhost:3593";
private string $playgroundInstanceId = "XhkOi82fFKk3YW60e2c806Yvm0trKEje"; // See: https://play.cerbos.dev/p/XhkOi82fFKk3YW60e2c806Yvm0trKEje
private string $host = 'localhost:3593';
private string $playgroundHost = 'demo-pdp.cerbos.cloud';
private string $playgroundInstanceId = 'XhkOi82fFKk3YW60e2c806Yvm0trKEje'; // See: https://play.cerbos.dev/p/XhkOi82fFKk3YW60e2c806Yvm0trKEje
protected CerbosClient $client;
protected CerbosClient $playgroundClient;

Expand All @@ -27,5 +28,6 @@ protected function setUp(): void
parent::setUp();

$this->client = CerbosClientBuilder::newInstance($this->host)->withPlaintext(true)->build();
$this->playgroundClient = CerbosClientBuilder::newInstance($this->playgroundHost)->withPlayground($this->playgroundInstanceId)->build();
}
}

0 comments on commit 5c0fac1

Please sign in to comment.