You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/01/20 07:26:13 UTC

[skywalking-php] branch master updated: Add authentication support. (#48)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-php.git


The following commit(s) were added to refs/heads/master by this push:
     new cbc602c  Add authentication support. (#48)
cbc602c is described below

commit cbc602cda714ed665ef3e506211a7bb1109f8373
Author: jmjoy <jm...@apache.org>
AuthorDate: Fri Jan 20 15:26:08 2023 +0800

    Add authentication support. (#48)
---
 docs/en/setup/service-agent/php-agent/README.md |  6 ++++--
 src/lib.rs                                      |  8 ++++++++
 src/module.rs                                   | 14 +++++++++++---
 src/worker.rs                                   |  8 ++++++--
 4 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/docs/en/setup/service-agent/php-agent/README.md b/docs/en/setup/service-agent/php-agent/README.md
index ba963b0..9393239 100644
--- a/docs/en/setup/service-agent/php-agent/README.md
+++ b/docs/en/setup/service-agent/php-agent/README.md
@@ -76,8 +76,10 @@ skywalking_agent.service_name = hello-skywalking
 ; Skywalking version.
 skywalking_agent.skywalking_version = 8
 
-; Skywalking worker threads, 0 will auto set as the cpu core size,
-; default is 0.
+; Skywalking authentication token, let it empty if the backend isn't enabled.
+; skywalking_agent.authentication =
+
+; Skywalking worker threads, 0 will auto set as the cpu core size, default is 0.
 ; skywalking_agent.worker_threads = 3
 
 ; Skywalking agent runtime directory, default is /tmp/skywalking-agent.
diff --git a/src/lib.rs b/src/lib.rs
index 9f279b6..0b6cf29 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -57,6 +57,9 @@ const SKYWALKING_AGENT_LOG_FILE: &str = "skywalking_agent.log_file";
 /// Skywalking agent runtime directory.
 const SKYWALKING_AGENT_RUNTIME_DIR: &str = "skywalking_agent.runtime_dir";
 
+/// Skywalking agent authentication token.
+const SKYWALKING_AGENT_AUTHENTICATION: &str = "skywalking_agent.authentication";
+
 #[php_get_module]
 pub fn get_module() -> Module {
     let mut module = Module::new(
@@ -94,6 +97,11 @@ pub fn get_module() -> Module {
         "/tmp/skywalking-agent".to_string(),
         Policy::System,
     );
+    module.add_ini(
+        SKYWALKING_AGENT_AUTHENTICATION,
+        "".to_string(),
+        Policy::System,
+    );
 
     // Hooks.
     module.on_module_init(module::init);
diff --git a/src/module.rs b/src/module.rs
index e724c9c..6af3fe5 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -18,8 +18,8 @@ use crate::{
     execute::register_execute_functions,
     util::{get_sapi_module_name, IPS},
     worker::init_worker,
-    SKYWALKING_AGENT_ENABLE, SKYWALKING_AGENT_LOG_FILE, SKYWALKING_AGENT_LOG_LEVEL,
-    SKYWALKING_AGENT_RUNTIME_DIR, SKYWALKING_AGENT_SERVICE_NAME,
+    SKYWALKING_AGENT_AUTHENTICATION, SKYWALKING_AGENT_ENABLE, SKYWALKING_AGENT_LOG_FILE,
+    SKYWALKING_AGENT_LOG_LEVEL, SKYWALKING_AGENT_RUNTIME_DIR, SKYWALKING_AGENT_SERVICE_NAME,
     SKYWALKING_AGENT_SKYWALKING_VERSION,
 };
 use once_cell::sync::Lazy;
@@ -77,6 +77,13 @@ pub static SOCKET_FILE_PATH: Lazy<PathBuf> = Lazy::new(|| {
     dir
 });
 
+pub static AUTHENTICATION: Lazy<String> = Lazy::new(|| {
+    ini_get::<Option<&CStr>>(SKYWALKING_AGENT_AUTHENTICATION)
+        .and_then(|s| s.to_str().ok())
+        .map(ToOwned::to_owned)
+        .unwrap_or_default()
+});
+
 pub fn init() {
     if !is_enable() {
         return;
@@ -87,9 +94,10 @@ pub fn init() {
     let service_name = Lazy::force(&SERVICE_NAME);
     let service_instance = Lazy::force(&SERVICE_INSTANCE);
     let skywalking_version = Lazy::force(&SKYWALKING_VERSION);
+    let authentication = Lazy::force(&AUTHENTICATION);
     info!(
         service_name,
-        service_instance, skywalking_version, "Starting skywalking agent"
+        service_instance, skywalking_version, authentication, "Starting skywalking agent"
     );
 
     // Skywalking version check
diff --git a/src/worker.rs b/src/worker.rs
index bdafdc7..1e4fd41 100644
--- a/src/worker.rs
+++ b/src/worker.rs
@@ -15,7 +15,7 @@
 
 use crate::{
     channel::{self, TxReporter},
-    module::{SERVICE_INSTANCE, SERVICE_NAME, SOCKET_FILE_PATH},
+    module::{AUTHENTICATION, SERVICE_INSTANCE, SERVICE_NAME, SOCKET_FILE_PATH},
     util::change_permission,
     SKYWALKING_AGENT_SERVER_ADDR, SKYWALKING_AGENT_WORKER_THREADS,
 };
@@ -171,7 +171,11 @@ async fn start_worker(server_addr: String) -> anyhow::Result<()> {
 
         report_properties_and_keep_alive(TxReporter(tx_));
 
-        let reporter = GrpcReporter::new_with_pc(channel, (), Consumer(rx));
+        let mut reporter = GrpcReporter::new_with_pc(channel, (), Consumer(rx));
+
+        if !AUTHENTICATION.is_empty() {
+            reporter = reporter.with_authentication(&*AUTHENTICATION);
+        }
 
         info!("Worker is ready...");