1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
<?php
namespace Basho\Riak\Node;
use Basho\Riak\Node;
/**
* This class follows the Builder design pattern and is the preferred method for creating Basho\Riak\Node objects for
* connecting to your Riak node cluster.
*
* <code>
* // simple local development / testing cluster
* use Basho\Riak\Node;
*
* $nodes = (new Node\Builder)
* ->buildLocalhost([10018, 10028, 10038, 10048, 10058]);
* </code>
*
* <code>
* // password authentication to production cluster
* use Basho\Riak\Node;
*
* $nodes = (new Node\Builder)
* ->onPort(8098)
* ->usingPasswordAuthentication('riakuser', 'riakpassword')
* ->withCertificateAuthorityFile(getcwd() . '/path/to/cacert.pem')
* ->buildCluster(['riak1.company.int','riak2.company.int','riak3.company.int']);
* </code>
*
* <code>
* // certificate authentication to production load balanced cluster
* use Basho\Riak\Node;
*
* $node = (new Node\Builder)
* ->atHost('riak.company.int')
* ->onPort(8098)
* ->usingCertificateAuthentication(getcwd() . '/path/to/client.crt')
* ->withCertificateAuthorityFile(getcwd() . '/path/to/cacert.pem')
* ->build();
* </code>
*
* <code>
* // pam authentication to production load balanced cluster
* use Basho\Riak\Node;
*
* $node = (new Node\Builder)
* ->atHost('riak.company.int')
* ->onPort(8098)
* ->usingPamAuthentication('riakuser')
* ->withCertificateAuthorityFile(getcwd() . '/path/to/cacert.pem')
* ->build();
* </code>
*
* @author Christopher Mancini <cmancini at basho d0t com>
*/
class Builder
{
/**
* Internal storage
*
* @var Config|null
*/
protected $config = null;
public function __construct()
{
$this->config = new Config();
}
/**
* usingTrustAuthentication
*
* Build nodes with trust authentication
*
* User authentication and access rules are only available in Riak versions 2 and above. To use this feature, TSL
* is required to communicate with your Riak nodes.
*
* @param string $user
*
* @return $this
*/
public function usingTrustAuthentication($user = '')
{
$this->config->setUser($user);
$this->config->setAuth(true);
return $this;
}
/**
* usingPasswordAuthentication
*
* Build nodes with password authentication
*
* User authentication and access rules are only available in Riak versions 2 and above. To use this feature, TSL
* is required to communicate with your Riak nodes.
*
* @param $user
* @param $pass
* @return $this
*/
public function usingPasswordAuthentication($user, $pass = '')
{
$this->config->setUser($user);
$this->config->setPass($pass);
$this->config->setAuth(true);
return $this;
}
/**
* usingCertificateAuthentication
*
* Build nodes with certificate authentication
*
* User authentication and access rules are only available in Riak versions 2 and above. To use this feature, TSL
* is required to communicate with your Riak nodes.
*
* CURRENTLY NOT SUPPORTED OVER THE RIAK HTTP API
*
* @param $certificate
* @param string $password
*
* @return $this
* @throws Builder\Exception
*/
public function usingCertificateAuthentication($certificate, $password = '')
{
$this->config->setCertificate($certificate);
$this->config->setCertificatePassword($password);
$this->config->setAuth(true);
throw new Node\Builder\Exception('Riak over HTTP does not support Certificate Authentication.');
//return $this;
}
/**
* usingPamAuthentication
*
* Build nodes with PAM authentication
*
* User authentication and access rules are only available in Riak versions 2 and above. To use this feature, TSL
* is required to communicate with your Riak nodes.
*
* @param $user
*
* @return $this
*/
public function usingPamAuthentication($user)
{
$this->config->setUser($user);
$this->config->setAuth(true);
return $this;
}
/**
* withCertificateAuthorityDirectory
*
* Path to CA file. A Certificate Authority file is required for any secure connections to Riak
*
* @param $ca_file
*
* @return $this
*
*/
public function withCertificateAuthorityFile($ca_file)
{
$this->config->setCaFile($ca_file);
return $this;
}
/**
* withCertificateAuthorityDirectory
*
* Directory where the CA file can be found. A Certificate Authority file is required for any secure connections to
* Riak
*
* @param $ca_directory
*
* @return $this
*
*/
public function withCertificateAuthorityDirectory($ca_directory)
{
$this->config->setCaDirectory($ca_directory);
return $this;
}
public function withPrivateKey($private_key, $password = '')
{
$this->config->setPrivateKey($private_key);
$this->config->setPrivateKeyPassword($password);
return $this;
}
/**
* Client side connection timeout for requests
*
* @param $timeout
*/
public function withConnectionTimeout($timeout) {
$this->config->setConnectionTimeout($timeout);
}
/**
* @return Config|null
*/
public function getConfig()
{
return $this->config;
}
/**
* Build distributed cluster
*
* Build node objects configured to listen on the same port but different hosts. Commonly used in
* staging and production environments where you have multiple Riak nodes on multiple machines / vms.
*
* @param array $hosts
* @return Node[]
*/
public function buildCluster(array $hosts = ['localhost'])
{
$nodes = [];
foreach ($hosts as $host) {
$nodes[] = $this->atHost($host)->build();
}
return $nodes;
}
/**
* Build node
*
* Validate configuration for a single node object, then build it
*
* @return Node
*/
public function build()
{
$this->validate();
return new Node(clone $this->config);
}
/**
* Builder configuration validation
*
* Checks the current configuration of the Node Builder for errors. This method should be executed before each Node
* is built.
*
* @throws Builder\Exception
*/
protected function validate()
{
// verify we have a host address and port
if (!$this->config->getHost() || !$this->config->getPort()) {
throw new Node\Builder\Exception('Node host address and port number are required.');
}
if ($this->config->getUser() && $this->config->getCertificate()) {
throw new Node\Builder\Exception('Connect with password OR certificate authentication, not both.');
}
if ($this->config->isAuth() && !$this->config->getCaDirectory() && !$this->config->getCaFile()) {
throw new Node\Builder\Exception('Certificate authority file is required for authentication.');
}
}
/**
* Build with host address
*
* Build node objects with configuration to use a specific host address
*
* @param $host
* @return $this
*/
public function atHost($host)
{
$this->config->setHost($host);
return $this;
}
/**
* Build local node cluster
*
* Build multiple node objects configured with the same host address but different ports. Commonly used in
* development environments where you have multiple Riak nodes on a single machine / vm.
*
* @param array $ports
* @return Node[]
*/
public function buildLocalhost(array $ports = [8087])
{
$nodes = [];
$this->atHost('localhost');
foreach ($ports as $port) {
$nodes[] = $this->onPort($port)->build();
}
return $nodes;
}
/**
* Build node objects with configuration to use a specific port number
*
* @param $port
* @return $this
*/
public function onPort($port)
{
$this->config->setPort($port);
return $this;
}
}