1010use YdbPlatform \Ydb \Jwt \Signer \Sha256 ;
1111use YdbPlatform \Ydb \Contracts \IamTokenContract ;
1212
13+ use function filter_var ;
14+
1315class Iam implements IamTokenContract
1416{
1517 use Traits \LoggerTrait;
@@ -48,7 +50,7 @@ public function __construct(array $config = [], LoggerInterface $logger = null)
4850 {
4951 if ($ config )
5052 {
51- $ this ->config = $ config ;
53+ $ this ->config = $ this -> parseConfig ( $ config) ;
5254 }
5355
5456 $ this ->logger = $ logger ;
@@ -95,7 +97,6 @@ public function newToken()
9597 else if ($ this ->config ('private_key ' ))
9698 {
9799 $ token = $ this ->getJwtToken ();
98-
99100 $ request_data = [
100101 'jwt ' => $ token ->toString (),
101102 ];
@@ -159,6 +160,11 @@ public function newToken()
159160 */
160161 public function getCredentials ()
161162 {
163+ if ($ this ->config ('insecure ' ))
164+ {
165+ return ChannelCredentials::createInsecure ();
166+ }
167+
162168 $ root_pem_file = $ this ->config ('root_cert_file ' );
163169
164170 if ($ root_pem_file && is_file ($ root_pem_file ))
@@ -169,28 +175,72 @@ public function getCredentials()
169175 return ChannelCredentials::createSsl ($ pem ?? null );
170176 }
171177
178+ /**
179+ * @param array $config
180+ * @return array
181+ */
182+ protected function parseConfig (array $ config )
183+ {
184+ $ parsedConfig = [];
185+
186+ $ stringParams = [
187+ 'temp_dir ' ,
188+ 'root_cert_file ' ,
189+ 'oauth_token ' ,
190+ 'key_id ' ,
191+ 'service_account_id ' ,
192+ 'private_key_file ' ,
193+ 'service_file ' ,
194+ ];
195+
196+ foreach ($ stringParams as $ param )
197+ {
198+ $ parsedConfig [$ param ] = (string )($ config [$ param ] ?? '' );
199+ }
200+
201+ $ boolParams = [
202+ 'use_metadata ' ,
203+ 'anonymous ' ,
204+ 'insecure ' ,
205+ ];
206+
207+ foreach ($ boolParams as $ param )
208+ {
209+ $ parsedConfig [$ param ] = (
210+ isset ($ config [$ param ])
211+ && filter_var ($ config [$ param ], \FILTER_VALIDATE_BOOLEAN )
212+ );
213+ }
214+
215+ return $ parsedConfig ;
216+ }
217+
172218 /**
173219 * @return void
174220 * @throws Exception
175221 */
176222 protected function initConfig ()
177223 {
178- if (empty ( $ this ->config [ 'temp_dir ' ] ))
224+ if (! $ this ->config ( 'temp_dir ' ))
179225 {
180226 $ this ->config ['temp_dir ' ] = sys_get_temp_dir ();
181227 }
182228
183- if (!empty ($ this ->config ['use_metadata ' ]))
229+ if ($ this ->config ('anonymous ' ))
230+ {
231+ $ this ->logger ()->info ('YDB: Authentication method: Anonymous ' );
232+ }
233+ else if ($ this ->config ('use_metadata ' ))
184234 {
185235 $ this ->logger ()->info ('YDB: Authentication method: Metadata URL ' );
186236 }
187- else if (! empty ( $ this ->config [ 'service_file ' ] ))
237+ else if ($ serviceFile = $ this ->config ( 'service_file ' ))
188238 {
189- if (is_file ($ this -> config [ ' service_file ' ] ))
239+ if (is_file ($ serviceFile ))
190240 {
191241 $ this ->logger ()->info ('YDB: Authentication method: SA JSON file ' );
192242
193- $ service = json_decode (file_get_contents ($ this -> config [ ' service_file ' ] ));
243+ $ service = json_decode (file_get_contents ($ serviceFile ));
194244
195245 if (is_object ($ service )
196246 && isset ($ service ->id )
@@ -203,28 +253,28 @@ protected function initConfig()
203253 }
204254 else
205255 {
206- throw new Exception ('Service file [ ' . $ this -> config [ ' service_file ' ] . '] is broken. ' );
256+ throw new Exception ('Service file [ ' . $ serviceFile . '] is broken. ' );
207257 }
208258 }
209259 else
210260 {
211- throw new Exception ('Service file [ ' . $ this -> config [ ' service_file ' ] . '] is missing. ' );
261+ throw new Exception ('Service file [ ' . $ serviceFile . '] is missing. ' );
212262 }
213263 }
214- else if (! empty ( $ this ->config [ 'private_key_file ' ] ))
264+ else if ($ privateKeyFile = $ this ->config ( 'private_key_file ' ))
215265 {
216266 $ this ->logger ()->info ('YDB: Authentication method: Private key ' );
217267
218- if (is_file ($ this -> config [ ' private_key_file ' ] ))
268+ if (is_file ($ privateKeyFile ))
219269 {
220- $ this ->config ['private_key ' ] = file_get_contents ($ this -> config [ ' private_key_file ' ] );
270+ $ this ->config ['private_key ' ] = file_get_contents ($ privateKeyFile );
221271 }
222272 else
223273 {
224- throw new Exception ('Private key [ ' . $ this -> config [ ' private_key_file ' ] . '] is missing. ' );
274+ throw new Exception ('Private key [ ' . $ privateKeyFile . '] is missing. ' );
225275 }
226276 }
227- else if (! empty ( $ this ->config [ 'oauth_token ' ] ))
277+ else if ($ this ->config ( 'oauth_token ' ))
228278 {
229279 $ this ->logger ()->info ('YDB: Authentication method: OAuth token ' );
230280 }
0 commit comments