Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Jul 19, 2018
0 parents commit f388441
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "fruitcake/aws-iot-manager",
"description": "AWS IoT Manager",
"license": "MIT",
"authors": [
{
"name": "Fruitcake",
"email": "[email protected]"
}
],
"require": {
"php": ">=7",
"aws/aws-sdk-php": "^3.63"
},
"autoload": {
"psr-4": {
"Fruitcake\\AwsIot\\": "src/"
}
}
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# AWS IoT Manager for PHP
38 changes: 38 additions & 0 deletions src/CertificateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Fruitcake\AwsIot;

class CertificateHelper
{
public static function generateKeypair()
{
$config = array(
'digest_alg' => 'sha512',
'private_key_bits' => 4096,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
);

$res = openssl_pkey_new($config);

openssl_pkey_export($res, $privateKey);
$publicKey = openssl_pkey_get_details($res)['key'];

return ['public' => $publicKey, 'private' => $privateKey];
}

public static function generateCsr($privateKey, $country, $organizationName, $commonName)
{
$dn = array(
'countryName' => $country,
'organizationName' => $organizationName,
'commonName' => $commonName,
);

$privkey = openssl_pkey_get_private($privateKey);

openssl_csr_export(openssl_csr_new($dn, $privkey), $csr);

return $csr;
}

}
127 changes: 127 additions & 0 deletions src/ThingManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Fruitcake\AwsIot;

use Aws\Sdk;

class ThingManager
{
/** @var \Aws\Iot\IotClient */
private $client;

public function __construct(Sdk $sdk)
{
$this->client = $sdk->createIot();
}

public function createThing($thingName, $typeName, array $attributes)
{
$result = $this->client->createThing([
'thingName' => $thingName,
'thingTypeName' => $typeName,
'attributePayload' => [
'attributes' => $attributes,
]
]);

return $result;
}

public function updateThing($thingName, $typeName, $attributes)
{
return $this->client->updateThing([
'thingName' => $thingName,
'thingTypeName' => $typeName,
'attributePayload' => [
'attributes' => $attributes,
'merge' => true,
]
]);
}

public function createKeysAndCertificate($active = true)
{
return $this->client->createKeysAndCertificate([
'setAsActive' => $active,
]);
}

public function createCertificateFromCsr($csr, $active = true)
{
return $this->client->createCertificateFromCsr([
'certificateSigningRequest' => $csr,
'setAsActive' => true,
]);
}

public function attachCertificate($thingName, $certificateArn, $policyName = null)
{
$this->client->attachThingPrincipal([
'principal' => $certificateArn,
'thingName' => $thingName,
]);

if ($policyName) {
$this->client->attachPrincipalPolicy([
'policyName' => $policyName,
'principal' => $certificateArn,
]);
}
}

public function deactiveCertificate($certificateId)
{
return $this->client->updateCertificate([
'certificateId' => $certificateId,
'newStatus' => 'INACTIVE',
]);
}

public function deleteCertificate($certificateId)
{
return $this->client->deleteCertificate([
'certificateId' => $certificateId,
]);
}

public function deleteThing($thingName)
{
$principals = $this->client->listThingPrincipals([
'thingName' => $thingName,
]);

foreach ($principals['principals'] as $principal) {
// Parse the full name
$principalParts = explode(':', $principal);
list($type, $id) = explode('/', array_pop($principalParts), 2);

$policies = $this->client->listPrincipalPolicies([
'principal' => $principal,
]);

// Delete the policies
foreach ($policies['policies'] as $policy) {
$this->client->detachPrincipalPolicy([
'policyName' => $policy['policyName'],
'principal' => $principal,
]);
}

// Detach the principals
$this->client->detachThingPrincipal([
'principal' => $principal,
'thingName' => $thingName,
]);

// Deactive the certificates and delete it
if ($type === 'cert') {
$this->deactiveCertificate($id);
$this->deleteCertificate($id);
}
}

return $this->client->deleteThing([
'thingName' => $thingName,
]);
}
}

0 comments on commit f388441

Please sign in to comment.