Skip to content

Commit

Permalink
Runnable as cloud foundry app and fix basic auth with php fcgi
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHlt committed May 12, 2015
1 parent 651064e commit a95937f
Show file tree
Hide file tree
Showing 27 changed files with 862 additions and 105 deletions.
4 changes: 4 additions & 0 deletions .bp-config/options.json
@@ -0,0 +1,4 @@
{
"WEBDIR": "app",
"COMPOSER_VENDOR_DIR": "vendor"
}
10 changes: 10 additions & 0 deletions .cfignore
@@ -0,0 +1,10 @@
*.phar
/.idea
logs
/cache
/tests
/.scrutinizer.yml
/.gitattributes
/.travis.yml
/phpunit.xml.dist
/README.md
3 changes: 0 additions & 3 deletions .gitattributes
@@ -1,8 +1,5 @@
/tests export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.gitattributes export-ignore
/.travis.yml export-ignore
/apigen.neon export-ignore
/phpunit.xml.dist export-ignore
/screen.png export-ignore
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -3,4 +3,4 @@ vendor
*.phar
/.idea
logs
/cache/service-broker.sqlite
/cache/service-broker.sqlite
16 changes: 9 additions & 7 deletions README.md
@@ -1,6 +1,6 @@
# php-cf-service-broker

[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE) [![Build Status](https://travis-ci.org/cloudfoundry-community/php-cf-service-broker.svg?branch=master)](https://travis-ci.org/cloudfoundry-community/php-cf-service-broker) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cloudfoundry-community/php-cf-service-broker/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/cloudfoundry-community/php-cf-service-broker/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/cloudfoundry-community/php-cf-service-broker/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/cloudfoundry-community/php-cf-service-broker/?branch=master)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE) [![Stories in Ready](https://badge.waffle.io/cloudfoundry-community/php-cf-service-broker.png?label=ready&title=Ready)](https://waffle.io/cloudfoundry-community/php-cf-service-broker) [![Build Status](https://travis-ci.org/cloudfoundry-community/php-cf-service-broker.svg?branch=master)](https://travis-ci.org/cloudfoundry-community/php-cf-service-broker) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/cloudfoundry-community/php-cf-service-broker/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/cloudfoundry-community/php-cf-service-broker/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/cloudfoundry-community/php-cf-service-broker/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/cloudfoundry-community/php-cf-service-broker/?branch=master)

Php project for creating Cloud Foundry service brokers.

Expand Down Expand Up @@ -54,6 +54,14 @@ service.broker.list:
- Unvalidate cache by doing in command line `touch sphring/main.yml` or set cache to `false` inside [/config/config.yml](https://github.com/cloudfoundry-community/php-cf-service-broker/blob/master/config/config.yml)
- Your new service broker is available

## Run as a Cloud Foundry app

1. Create a database service which his name follow this regex: `/.*(postgres|pgsql|db|database|my|maria|oracle|oci).*/i` (**note**: add in `composer.json` other pdo driver than pdo_mysql)
2. Push the app (run `cf push`)
3. Bind your database service on your app
4. Restage your app
5. Service broker app is ready try go to `http://your_url/v2/catalog` you should see your catalog

## Run the tests

Tests use [PHPUnit](https://phpunit.de/).
Expand All @@ -65,9 +73,3 @@ Run with your command line:
1. Update [services.json](https://github.com/cloudfoundry-community/php-cf-service-broker/blob/master/tests/Sphring/MicroWebFramework/Resources/Sphring/services.json) inside tests configuration directory.
2. Update [service-broker.yml](https://github.com/cloudfoundry-community/php-cf-service-broker/blob/master/tests/Sphring/MicroWebFramework/Resources/Sphring/service-broker.yml) inside tests configuration directory.
3. Run `vendor/bin/phpunit --bootstrap tests\bootstrap.php --no-configuration tests`

## Todo

- ~~Add tests~~
- Follow [new spec](https://docs.google.com/document/d/12ghe1B3YPhHLGcAOgJe_1PcpDUbhaaz1RentoWepwsA/edit?usp=sharing) to hand asynchronous service broker
- Make it Cloud Foundry ready to run as an app inside Cloud Foundry by using [cf-helper-php](https://github.com/cloudfoundry-community/cf-helper-php)
3 changes: 3 additions & 0 deletions app/.htaccess
Expand Up @@ -8,6 +8,9 @@ DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On

# Rewrite for basic auth
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -20,6 +20,7 @@
"doctrine/orm": "2.4.*",
"jms/serializer": "0.16.0",
"sphring/sphring-cache": "dev-master",
"cloudfoundry-community/cf-helper-php": "1.5.*",
"ramsey/uuid": "^2.8",
"vierbergenlars/php-semver": "^3.0"
},
Expand Down
12 changes: 12 additions & 0 deletions manifest.yml
@@ -0,0 +1,12 @@
---
#Generated manifest
applications:
- name: php-cf-service-broker
memory: 1G
instances: 1
path: ./
host: php-cf-service-broker
# Bind your database
#services:
# - test-sb-php
buildpack: https://github.com/cloudfoundry/php-buildpack.git # cause we need composer alpha10 and current php_buildpack on run.pivotal.io is not up-to-date
2 changes: 2 additions & 0 deletions sphring/doctrine.yml
Expand Up @@ -12,6 +12,8 @@ microwebframe.doctrine:
- ../config/doctrine-driver.yml
cache:
ref: microwebframe.doctrine.cache
cloudFoundryBoot:
ref: util.cloudFoundryBoot
fileCreation:
file: %CONTEXTROOT%/..%config.cacheDir%/db.ct
microwebframe.doctrine.cache:
Expand Down
2 changes: 2 additions & 0 deletions sphring/util.yml
Expand Up @@ -6,3 +6,5 @@ util.serviceDescribeCreator:
ref: microwebframe.doctrine
services:
json: %CONTEXTROOT%/../config/services.json
util.cloudFoundryBoot:
class: Sphring\MicroWebFramework\CloudFoundry\CloudFoundry
51 changes: 38 additions & 13 deletions src/Sphring/MicroWebFramework/Auth/HttpBasicAuthentifier.php
Expand Up @@ -15,6 +15,7 @@

use Arthurh\Sphring\Annotations\AnnotationsSphring\Required;
use Sphring\MicroWebFramework\Security\Encoder;
use Symfony\Component\HttpFoundation\Request;

class HttpBasicAuthentifier
{
Expand All @@ -28,33 +29,33 @@ class HttpBasicAuthentifier
*/
private $validUsers;

/**
* @var Request
*/
private $request;

function __construct()
{

}

function auth()
{
if (empty($this->validUsers[$_SERVER['PHP_AUTH_USER']])) {
if ($this->request === null) {
return false;
}
$user = $this->request->server->get('PHP_AUTH_USER');
$password = $this->request->server->get('PHP_AUTH_PW');
if (empty($this->validUsers[$user])) {
return false;
}
$cryptPass = $this->encoder->crypt($_SERVER['PHP_AUTH_PW']);
if ($cryptPass !== $this->validUsers[$_SERVER['PHP_AUTH_USER']]) {
$cryptPass = $this->encoder->crypt($password);
if ($cryptPass !== $this->validUsers[$user]) {
return false;
}
return true;
}

/**
* @Required
* @param Encoder $encoder
*/
public function setEncoder(Encoder $encoder)
{
$this->encoder = $encoder;
}


/**
*
* @return \string[]
Expand All @@ -81,5 +82,29 @@ public function getEncoder()
return $this->encoder;
}

/**
* @Required
* @param Encoder $encoder
*/
public function setEncoder(Encoder $encoder)
{
$this->encoder = $encoder;
}

/**
* @return Request
*/
public function getRequest()
{
return $this->request;
}

/**
* @param Request $request
*/
public function setRequest($request)
{
$this->request = $request;
}

}
89 changes: 89 additions & 0 deletions src/Sphring/MicroWebFramework/CloudFoundry/CloudFoundry.php
@@ -0,0 +1,89 @@
<?php
/**
* Copyright (C) 2015 Arthur Halet
*
* This software is distributed under the terms and conditions of the 'MIT'
* license which can be found in the file 'LICENSE' in this package distribution
* or at 'http://opensource.org/licenses/MIT'.
*
* Author: Arthur Halet
* Date: 12/05/2015
*/


namespace Sphring\MicroWebFramework\CloudFoundry;


use Arhframe\Util\File;
use CfCommunity\CfHelper\CfHelper;
use Doctrine\Common\Cache\FilesystemCache;

class CloudFoundry
{
/**
* @var CfHelper
*/
private $cfHelper;

public function __construct()
{
$this->cfHelper = CfHelper::getInstance();
}

public function getDatabaseUrl()
{
if (!$this->isInCloudFoundry()) {
return null;
}
$dbConnector = $this->cfHelper->getDatabaseConnector();
$dbConnector->load();
$credentials = $dbConnector->getCredentials();
if ($credentials === null) {
return null;
}
$port = null;
if (isset($credentials['port'])) {
$port = ":" . $credentials['port'];
}
$password = null;
if (isset($credentials['pass'])) {
$password = ":" . $credentials['pass'];
}
return $credentials['type'] . "://" . $credentials['user']
. $password . '@' . $credentials['host'] . $port . '/' . $credentials['database'];
}

public function isInCloudFoundry()
{
return $this->cfHelper->isInCloudFoundry();
}

public function getDoctrineCache()
{
if (!$this->isInCloudFoundry()) {
return null;
}
return new FilesystemCache(sys_get_temp_dir());
}

public function getFolderCache()
{
if (!$this->isInCloudFoundry()) {
return null;
}
return sys_get_temp_dir();
}

public function getFileCreation()
{
if (!$this->isInCloudFoundry()) {
return null;
}
$file = new File(sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'db.ct');
$file->createFolder();
if (!$file->isFile()) {
$file->setContent(0);
}
return $file;
}
}
2 changes: 2 additions & 0 deletions src/Sphring/MicroWebFramework/Controller/IndexController.php
Expand Up @@ -26,6 +26,7 @@ class IndexController extends AbstractController

public function action()
{

$version = $this->getRequest()->headers->get('X-Broker-API-Version') . '.0';
try {
$satisfy = $this->getBrokerVersionExpression()->satisfiedBy(new version($version));
Expand All @@ -39,6 +40,7 @@ public function action()
return Response::$statusTexts[Response::HTTP_PRECONDITION_FAILED];
}
$basicAuth = $this->getBasicAuth();
$basicAuth->setRequest($this->request);
if (!$basicAuth->auth()) {
$this->response->setStatusCode(Response::HTTP_UNAUTHORIZED);
$this->response->headers->add(['WWW-Authenticate' => 'Basic realm="' . HttpBasicAuthentifier::REALM . '"']);
Expand Down

0 comments on commit a95937f

Please sign in to comment.