@@ -315,12 +315,31 @@ protected function serverCheck(): bool
315315 throw new ConnectToServerFailedException ('the nacos server is not ready to work in 30 seconds, connect to server failed ' );
316316 }
317317
318- private function isWorkerExit ( ): bool
318+ protected function getMetadata ( RequestInterface $ request ): array
319319 {
320- return CoordinatorManager::until (Constants::WORKER_EXIT )->isClosing ();
320+ return match ($ this ->config ->getCloudName ()) {
321+ CloudName::Aliyun => $ this ->getAliyunMetadata ($ request ),
322+ default => $ this ->getDefaultMetadata ($ request ),
323+ };
321324 }
322325
323- private function getMetadata (RequestInterface $ request ): array
326+ protected function getAliyunMetadata (RequestInterface $ request ): array
327+ {
328+ $ accessKey = $ this ->config ->getAccessKey ();
329+ $ accessSecret = $ this ->config ->getAccessSecret ();
330+ $ signHeaders = $ this ->getMseSignHeaders ($ request , $ accessSecret );
331+
332+ return [
333+ 'type ' => $ request ->getType (),
334+ 'clientIp ' => $ this ->ip (),
335+ 'headers ' => [
336+ 'Spas-AccessKey ' => $ accessKey ,
337+ ...$ signHeaders ,
338+ ],
339+ ];
340+ }
341+
342+ protected function getDefaultMetadata (RequestInterface $ request ): array
324343 {
325344 if ($ token = $ this ->getAccessToken ()) {
326345 return [
@@ -338,6 +357,11 @@ private function getMetadata(RequestInterface $request): array
338357 ];
339358 }
340359
360+ private function isWorkerExit (): bool
361+ {
362+ return CoordinatorManager::until (Constants::WORKER_EXIT )->isClosing ();
363+ }
364+
341365 private function grpcDefaultHeaders (): array
342366 {
343367 return [
@@ -358,4 +382,33 @@ private function handleResponse(ResponseInterface $response): array
358382
359383 return Json::decode ($ contents );
360384 }
385+
386+ private function getMseSignHeaders (RequestInterface $ request , string $ secretKey ): array
387+ {
388+ $ data = $ request ->getValue ();
389+ $ group = $ data ['group ' ] ?? '' ;
390+ $ tenant = $ data ['tenant ' ] ?? '' ;
391+ $ timeStamp = (string ) round (microtime (true ) * 1000 );
392+ $ signStr = '' ;
393+
394+ if ($ tenant ) {
395+ $ signStr .= "{$ tenant }+ " ;
396+ }
397+
398+ if ($ group ) {
399+ $ signStr .= "{$ group }+ " ;
400+ }
401+
402+ $ signStr .= "{$ timeStamp }" ;
403+
404+ return [
405+ 'charset ' => 'utf-8 ' ,
406+ 'exConfigInfo ' => 'true ' ,
407+ 'Client-RequestToken ' => md5 ($ timeStamp ),
408+ 'Client-RequestTS ' => $ timeStamp ,
409+ 'Timestamp ' => $ timeStamp ,
410+ 'Spas-Signature ' => base64_encode (hash_hmac ('sha1 ' , $ signStr , $ secretKey , true )),
411+ 'Client-AppName ' => '' ,
412+ ];
413+ }
361414}
0 commit comments