No Description

ApplicationDefaultCredentialsTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /*
  3. * Copyright 2015 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace Google\Auth\Tests;
  18. use Google\Auth\ApplicationDefaultCredentials;
  19. use Google\Auth\Credentials\GCECredentials;
  20. use Google\Auth\Credentials\ServiceAccountCredentials;
  21. use GuzzleHttp\Psr7;
  22. use PHPUnit\Framework\TestCase;
  23. class ADCGetTest extends TestCase
  24. {
  25. private $originalHome;
  26. protected function setUp()
  27. {
  28. $this->originalHome = getenv('HOME');
  29. }
  30. protected function tearDown()
  31. {
  32. if ($this->originalHome != getenv('HOME')) {
  33. putenv('HOME=' . $this->originalHome);
  34. }
  35. putenv(ServiceAccountCredentials::ENV_VAR); // removes it from
  36. }
  37. /**
  38. * @expectedException DomainException
  39. */
  40. public function testIsFailsEnvSpecifiesNonExistentFile()
  41. {
  42. $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
  43. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  44. ApplicationDefaultCredentials::getCredentials('a scope');
  45. }
  46. public function testLoadsOKIfEnvSpecifiedIsValid()
  47. {
  48. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  49. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  50. $this->assertNotNull(
  51. ApplicationDefaultCredentials::getCredentials('a scope')
  52. );
  53. }
  54. public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
  55. {
  56. putenv('HOME=' . __DIR__ . '/fixtures');
  57. $this->assertNotNull(
  58. ApplicationDefaultCredentials::getCredentials('a scope')
  59. );
  60. }
  61. /**
  62. * @expectedException DomainException
  63. */
  64. public function testFailsIfNotOnGceAndNoDefaultFileFound()
  65. {
  66. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  67. // simulate not being GCE by return 500
  68. $httpHandler = getHandler([
  69. buildResponse(500),
  70. ]);
  71. ApplicationDefaultCredentials::getCredentials('a scope', $httpHandler);
  72. }
  73. public function testSuccedsIfNoDefaultFilesButIsOnGCE()
  74. {
  75. $wantedTokens = [
  76. 'access_token' => '1/abdef1234567890',
  77. 'expires_in' => '57',
  78. 'token_type' => 'Bearer',
  79. ];
  80. $jsonTokens = json_encode($wantedTokens);
  81. // simulate the response from GCE.
  82. $httpHandler = getHandler([
  83. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  84. buildResponse(200, [], Psr7\stream_for($jsonTokens)),
  85. ]);
  86. $this->assertNotNull(
  87. ApplicationDefaultCredentials::getCredentials('a scope', $httpHandler)
  88. );
  89. }
  90. }
  91. class ADCGetMiddlewareTest extends TestCase
  92. {
  93. private $originalHome;
  94. protected function setUp()
  95. {
  96. $this->originalHome = getenv('HOME');
  97. }
  98. protected function tearDown()
  99. {
  100. if ($this->originalHome != getenv('HOME')) {
  101. putenv('HOME=' . $this->originalHome);
  102. }
  103. putenv(ServiceAccountCredentials::ENV_VAR); // removes it if assigned
  104. }
  105. /**
  106. * @expectedException DomainException
  107. */
  108. public function testIsFailsEnvSpecifiesNonExistentFile()
  109. {
  110. $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
  111. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  112. ApplicationDefaultCredentials::getMiddleware('a scope');
  113. }
  114. public function testLoadsOKIfEnvSpecifiedIsValid()
  115. {
  116. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  117. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  118. $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
  119. }
  120. public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
  121. {
  122. putenv('HOME=' . __DIR__ . '/fixtures');
  123. $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope'));
  124. }
  125. /**
  126. * @expectedException DomainException
  127. */
  128. public function testFailsIfNotOnGceAndNoDefaultFileFound()
  129. {
  130. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  131. // simulate not being GCE by return 500
  132. $httpHandler = getHandler([
  133. buildResponse(500),
  134. ]);
  135. ApplicationDefaultCredentials::getMiddleware('a scope', $httpHandler);
  136. }
  137. public function testWithCacheOptions()
  138. {
  139. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  140. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  141. $httpHandler = getHandler([
  142. buildResponse(200),
  143. ]);
  144. $cacheOptions = [];
  145. $cachePool = $this->getMock('Psr\Cache\CacheItemPoolInterface');
  146. $middleware = ApplicationDefaultCredentials::getMiddleware(
  147. 'a scope',
  148. $httpHandler,
  149. $cacheOptions,
  150. $cachePool
  151. );
  152. }
  153. public function testSuccedsIfNoDefaultFilesButIsOnGCE()
  154. {
  155. $wantedTokens = [
  156. 'access_token' => '1/abdef1234567890',
  157. 'expires_in' => '57',
  158. 'token_type' => 'Bearer',
  159. ];
  160. $jsonTokens = json_encode($wantedTokens);
  161. // simulate the response from GCE.
  162. $httpHandler = getHandler([
  163. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  164. buildResponse(200, [], Psr7\stream_for($jsonTokens)),
  165. ]);
  166. $this->assertNotNull(ApplicationDefaultCredentials::getMiddleware('a scope', $httpHandler));
  167. }
  168. }
  169. class ADCGetCredentialsAppEngineTest extends BaseTest
  170. {
  171. private $originalHome;
  172. private $originalServiceAccount;
  173. protected function setUp()
  174. {
  175. // set home to be somewhere else
  176. $this->originalHome = getenv('HOME');
  177. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  178. // remove service account path
  179. $this->originalServiceAccount = getenv(ServiceAccountCredentials::ENV_VAR);
  180. putenv(ServiceAccountCredentials::ENV_VAR);
  181. }
  182. protected function tearDown()
  183. {
  184. // removes it if assigned
  185. putenv('HOME=' . $this->originalHome);
  186. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $this->originalServiceAccount);
  187. putenv('GAE_INSTANCE');
  188. }
  189. public function testAppEngineStandard()
  190. {
  191. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  192. $this->assertInstanceOf(
  193. 'Google\Auth\Credentials\AppIdentityCredentials',
  194. ApplicationDefaultCredentials::getCredentials()
  195. );
  196. }
  197. public function testAppEngineFlexible()
  198. {
  199. $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
  200. putenv('GAE_INSTANCE=aef-default-20180313t154438');
  201. $httpHandler = getHandler([
  202. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  203. ]);
  204. $this->assertInstanceOf(
  205. 'Google\Auth\Credentials\GCECredentials',
  206. ApplicationDefaultCredentials::getCredentials(null, $httpHandler)
  207. );
  208. }
  209. }
  210. // @todo consider a way to DRY this and above class up
  211. class ADCGetSubscriberTest extends BaseTest
  212. {
  213. private $originalHome;
  214. protected function setUp()
  215. {
  216. $this->onlyGuzzle5();
  217. $this->originalHome = getenv('HOME');
  218. }
  219. protected function tearDown()
  220. {
  221. if ($this->originalHome != getenv('HOME')) {
  222. putenv('HOME=' . $this->originalHome);
  223. }
  224. putenv(ServiceAccountCredentials::ENV_VAR); // removes it if assigned
  225. }
  226. /**
  227. * @expectedException DomainException
  228. */
  229. public function testIsFailsEnvSpecifiesNonExistentFile()
  230. {
  231. $keyFile = __DIR__ . '/fixtures' . '/does-not-exist-private.json';
  232. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  233. ApplicationDefaultCredentials::getSubscriber('a scope');
  234. }
  235. public function testLoadsOKIfEnvSpecifiedIsValid()
  236. {
  237. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  238. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  239. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
  240. }
  241. public function testLoadsDefaultFileIfPresentAndEnvVarIsNotSet()
  242. {
  243. putenv('HOME=' . __DIR__ . '/fixtures');
  244. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope'));
  245. }
  246. /**
  247. * @expectedException DomainException
  248. */
  249. public function testFailsIfNotOnGceAndNoDefaultFileFound()
  250. {
  251. putenv('HOME=' . __DIR__ . '/not_exist_fixtures');
  252. // simulate not being GCE by return 500
  253. $httpHandler = getHandler([
  254. buildResponse(500),
  255. ]);
  256. ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler);
  257. }
  258. public function testWithCacheOptions()
  259. {
  260. $keyFile = __DIR__ . '/fixtures' . '/private.json';
  261. putenv(ServiceAccountCredentials::ENV_VAR . '=' . $keyFile);
  262. $httpHandler = getHandler([
  263. buildResponse(200),
  264. ]);
  265. $cacheOptions = [];
  266. $cachePool = $this->getMock('Psr\Cache\CacheItemPoolInterface');
  267. $subscriber = ApplicationDefaultCredentials::getSubscriber(
  268. 'a scope',
  269. $httpHandler,
  270. $cacheOptions,
  271. $cachePool
  272. );
  273. }
  274. public function testSuccedsIfNoDefaultFilesButIsOnGCE()
  275. {
  276. $wantedTokens = [
  277. 'access_token' => '1/abdef1234567890',
  278. 'expires_in' => '57',
  279. 'token_type' => 'Bearer',
  280. ];
  281. $jsonTokens = json_encode($wantedTokens);
  282. // simulate the response from GCE.
  283. $httpHandler = getHandler([
  284. buildResponse(200, [GCECredentials::FLAVOR_HEADER => 'Google']),
  285. buildResponse(200, [], Psr7\stream_for($jsonTokens)),
  286. ]);
  287. $this->assertNotNull(ApplicationDefaultCredentials::getSubscriber('a scope', $httpHandler));
  288. }
  289. }