Skip to content
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

Make authentication check for each API method explicit at build time #55

Merged
merged 1 commit into from
Jan 24, 2020
Merged
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
8 changes: 7 additions & 1 deletion build/build.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,18 @@ function __autoload($className)
// initialize variable for API methods
$apiMethods = '';

$anonymousFunctions = array(
'apiinfo.version',
);

// build API methods
foreach ($apiArray as $resource => $actions) {
foreach ($actions as $action) {
$apiMethod = $resource.'.'.$action;
$methodPlaceholders = array(
'API_METHOD' => $resource.'.'.$action,
'API_METHOD' => $apiMethod,
'PHP_METHOD' => $resource.ucfirst($action),
'IS_AUTHENTICATION_REQUIRED' => in_array($apiMethod, $anonymousFunctions, true) ? 'false' : 'true',
);
$apiMethods .= replacePlaceholders($matches[4], $methodPlaceholders);
}
Expand Down
22 changes: 3 additions & 19 deletions build/templates/abstract.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ abstract class <CLASSNAME_ABSTRACT>
{<!START_API_CONSTANT>
const <PHP_CONST_NAME> = <PHP_CONST_VALUE>;
<!END_API_CONSTANT>
/**
* Anonymous API functions.
*
* @var string[]
*/
private static $anonymousFunctions = array(
'apiinfo.version',
);

/**
* Boolean if requests/responses should be printed out (JSON).
*
Expand Down Expand Up @@ -210,7 +201,7 @@ public function setBasicAuthorization($user, $password)
/**
* Sets the context for SSL-enabled connections.
*
* @see http://php.net/manual/en/context.ssl.php.
* @see https://php.net/manual/en/context.ssl.php.
*
* @param array $context Array with the SSL context
*
Expand Down Expand Up @@ -490,7 +481,7 @@ final public function userLogout($params = array(), $arrayKeyProperty = '')
}
<!START_API_METHOD>
/**
* Requests the Zabbix API and returns the response of the API method "<API_METHOD>".
* Requests the Zabbix API and returns the response of the method "<API_METHOD>".
*
* The $params Array can be used, to pass parameters to the Zabbix API.
* For more information about these parameters, check the Zabbix API
Expand All @@ -510,14 +501,7 @@ final public function userLogout($params = array(), $arrayKeyProperty = '')
*/
public function <PHP_METHOD>($params = array(), $arrayKeyProperty = '')
{
// get params array for request
$params = $this->getRequestParamsArray($params);

// check if we've to authenticate
$auth = !in_array('<API_METHOD>', self::$anonymousFunctions, true);

// request
return $this->request('<API_METHOD>', $params, $arrayKeyProperty, $auth);
return $this->request('<API_METHOD>', $this->getRequestParamsArray($params), $arrayKeyProperty, <IS_AUTHENTICATION_REQUIRED>);
}
<!END_API_METHOD>
/**
Expand Down
35 changes: 35 additions & 0 deletions tests/ZabbixApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@ public function testUserLoginOnConsecutiveCalls()
$this->removeTokenCacheDir($cacheDir);
}

/**
* @dataProvider getAuthenticationRequired
*
* @param string $method
* @param string $apiMethod
* @param bool $isAuthenticationRequired
*/
public function testAuthenticationRequired($method, $apiMethod, $isAuthenticationRequired)
{
$this->assertTrue(is_callable(array('ZabbixApi\ZabbixApi', $method)));

$zabbix = $this->getMockBuilder('ZabbixApi\ZabbixApi')
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->setMethods(array('request'))
->getMock();

$zabbix
->expects($this->once())
->method('request')
->with($apiMethod, array(), '', $isAuthenticationRequired);

$zabbix->$method();
}

public function getAuthenticationRequired()
{
return array(
array('method' => 'userGet', 'api_method' => 'user.get', 'is_authentication_required' => true),
array('method' => 'apiinfoVersion', 'api_method' => 'apiinfo.version', 'is_authentication_required' => false),
array('method' => 'hostGet', 'api_method' => 'host.get', 'is_authentication_required' => true),
);
}

public function testZabbixApiConnectionNotTriggered()
{
$zabbix = new ZabbixApi('http://localhost/json_rpc.php');
Expand Down