Skip to content

Added basic auth support #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Etcd PHP Client

[![Build Status](https://travis-ci.org/activecollab/etcd.svg?branch=master)](https://travis-ci.org/activecollab/etcd)
[![Build Status](https://travis-ci.org/SR02/etcd.svg?branch=master)](https://travis-ci.org/SR02/etcd)

etcd is a distributed configuration system, part of the coreos project.

This repository provides a client library for etcd for PHP applications. It is based on [linkorb/etcd-php](https://github.com/linkorb/etcd-php). To learn why we forked it, jump [here](#why-fork).
This repository provides a client library for etcd for PHP applications. It is based on [linkorb/etcd-php](https://github.com/linkorb/etcd-php) and [activecollab/etcd](https://github.com/activecollab/etcd).

## Installating etcd

Expand All @@ -27,7 +27,7 @@ Easiest way is to install it using composer:
## Using Client

```php
use use ActiveCollab\Etcd\Client as EtcdClient;
use ActiveCollab\Etcd\Client as EtcdClient;

$client = new EtcdClient('http://127.0.0.1:4001');

Expand Down Expand Up @@ -102,8 +102,4 @@ as well as to use a custom CA file:

```php
$client = (new Client('https://127.0.0.1:4001'))->verifySslPeer(true, '/path/to/ca/file');
```

## Why Fork?

While [original library](https://github.com/linkorb/etcd-php) works well, it depends on two big packages: Symfony Console and Guzzle. For a feature as low level as config access, we wanted something a bit nimbler, so we removed CLI commands and refactored the original library to use PHP's curl extension.
```
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name" : "activecollab/etcd",
"name" : "sr02/etcd",
"type": "library",
"description": "etcd client with minimal dependencies",
"license" : "MIT",
"prefer-stable": true,
"keywords": [ "etcd", "client", "configuration" ],
"homepage": "https://labs.activecollab.com",
"homepage": "https://github.com/SR02/etcd",
"authors": [
{
"name" : "Cong Peijun",
Expand All @@ -14,6 +14,10 @@
{
"name": "Ilija Studen",
"email": "[email protected]"
},
{
"name": "Brian Salvaggio",
"email": "[email protected]"
}
],
"require" : {
Expand Down
38 changes: 37 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,26 @@ class Client implements ClientInterface
*/
private $custom_ca_file;

/**
* @var string
*/
private $etcd_user;
/**
* @var string
*/
private $etcd_pass;

/**
* @param string $server
* @param string $etcd_user
* @param string $etcd_pass
* @param string $api_version
*/
public function __construct($server = 'http://127.0.0.1:4001', $api_version = 'v2')
public function __construct($server = 'http://127.0.0.1:4001', $etcd_user = null, $etcd_pass = null, $api_version = 'v2')
{
$this->setServer($server);
$this->setEtcdUser($etcd_user);
$this->setEtcdPass($etcd_pass);
$this->setApiVersion($api_version);
}

Expand Down Expand Up @@ -140,6 +153,26 @@ public function setApiVersion($version)
return $this;
}

/**
* @param string $user
* @return $this
*/
public function setEtcdUser($user)
{
$this->etcd_user = $user;
return $this;
}

/**
* @param string $pass
* @return $this
*/
public function setEtcdPass($pass)
{
$this->etcd_pass = $pass;
return $this;
}

/**
* @return string
*/
Expand Down Expand Up @@ -614,6 +647,9 @@ private function getCurlHandle($url)
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

if($this->etcd_user !== null) {
curl_setopt($curl, CURLOPT_USERPWD, $this->etcd_user.':'.$this->etcd_pass);
}
if ($this->is_https && $this->verify_ssl_peer) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
Expand Down
2 changes: 1 addition & 1 deletion test/bin/build_etcd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

wget -c https://storage.googleapis.com/golang/go1.4.linux-amd64.tar.gz
tar -zxf go1.4.linux-amd64.tar.gz
git clone https://github.com/coreos/etcd.git
git clone -b release-2.3 https://github.com/coreos/etcd.git
export GOROOT=$PWD/go
#export GOPATH=$PWD/go
export PATH=$GOPATH/bin:$PATH
Expand Down
5 changes: 3 additions & 2 deletions test/src/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->client = new Client();

$this->client->setEtcdUser('user');
$this->client->setEtcdPass('pass');
$this->client->setSandboxPath('/');

try {
$this->client->removeDir($this->dirname, true);
} catch (EtcdException $e) {

die;
}

$create_dir = $this->client->createDir($this->dirname);
Expand Down