feat: support audit log (#1812)

* feat: support audit log

* feat: support audit log

* refactor: move audit log config under statReporter.plugin

Co-Authored-By: Codex GPT-5 <noreply@openai.com>
Signed-off-by: fishtailfu <fishtailfu@tencent.com>

---------

Signed-off-by: fishtailfu <fishtailfu@tencent.com>
Co-authored-by: fishtailfu <fishtailfu@tencent.com>
Co-authored-by: Codex GPT-5 <noreply@openai.com>
pull/1819/head
Fishtail 4 weeks ago committed by evelynwei
parent 4ed44e001a
commit 8f15826c5f

@ -14,4 +14,5 @@
- [fix: split contract base-package for springdoc scan](https://github.com/Tencent/spring-cloud-tencent/pull/1807) - [fix: split contract base-package for springdoc scan](https://github.com/Tencent/spring-cloud-tencent/pull/1807)
- [fix: fix route failure caused by Spring Cloud Gateway 5.x property prefix change](https://github.com/Tencent/spring-cloud-tencent/pull/1811) - [fix: fix route failure caused by Spring Cloud Gateway 5.x property prefix change](https://github.com/Tencent/spring-cloud-tencent/pull/1811)
- [feat: support using seperated cb in wildcard-api level and add cb counters expire interval config](https://github.com/Tencent/spring-cloud-tencent/pull/1809) - [feat: support using seperated cb in wildcard-api level and add cb counters expire interval config](https://github.com/Tencent/spring-cloud-tencent/pull/1809)
- [feat: adapt to polaris-java ReportClientRequestCustomizer plugin for config watch reporting](https://github.com/Tencent/spring-cloud-tencent/pull/1813) - [feat: adapt to polaris-java ReportClientRequestCustomizer plugin for config watch reporting](https://github.com/Tencent/spring-cloud-tencent/pull/1813)
- [feat: support audit log](https://github.com/Tencent/spring-cloud-tencent/pull/1812)

@ -195,6 +195,11 @@ public class OrderConstant {
*/ */
public static Integer STAT_REPORTER_ORDER = 1; public static Integer STAT_REPORTER_ORDER = 1;
/**
* Order of audit log configuration modifier.
*/
public static Integer AUDIT_LOG_ORDER = 1;
/** /**
* Load Balancer config modifier order. * Load Balancer config modifier order.
*/ */

@ -251,6 +251,16 @@
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<version>${polaris.version}</version> <version>${polaris.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>stat-audit</artifactId>
<version>${polaris.version}</version>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-config</artifactId>
<version>${polaris.version}</version>
</dependency>
<dependency> <dependency>
<artifactId>polaris-threadlocal</artifactId> <artifactId>polaris-threadlocal</artifactId>

@ -21,6 +21,15 @@
</dependency> </dependency>
<!-- Spring Cloud Tencent dependencies end --> <!-- Spring Cloud Tencent dependencies end -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-config</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>stat-audit</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId> <artifactId>spring-web</artifactId>

@ -0,0 +1,54 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.rpc.enhancement.audit.config;
import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.api.config.global.StatReporterConfig;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.global.StatReporterConfigImpl;
import com.tencent.polaris.plugins.stat.audit.AuditLogConfig;
/**
* Config modifier for Polaris service call audit logging.
*
* @author Yuwei Fu
*/
public class AuditLogConfigModifier implements PolarisConfigModifier {
private final PolarisAuditLogProperties auditLogProperties;
public AuditLogConfigModifier(PolarisAuditLogProperties auditLogProperties) {
this.auditLogProperties = auditLogProperties;
}
@Override
public void modify(ConfigurationImpl configuration) {
StatReporterConfigImpl statReporterConfig = configuration.getGlobal().getStatReporter();
AuditLogConfig auditLogConfig = statReporterConfig.getPluginConfig(
StatReporterConfig.DEFAULT_REPORTER_AUDIT_LOG, AuditLogConfig.class);
auditLogConfig.setEnable(auditLogProperties.isEnabled());
auditLogConfig.setFormat(auditLogProperties.getFormat());
statReporterConfig.setPluginConfig(StatReporterConfig.DEFAULT_REPORTER_AUDIT_LOG, auditLogConfig);
}
@Override
public int getOrder() {
return OrderConstant.Modifier.AUDIT_LOG_ORDER;
}
}

@ -0,0 +1,55 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.rpc.enhancement.audit.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Properties for Polaris service call audit logging.
*
* @author Yuwei Fu
*/
@ConfigurationProperties("spring.cloud.polaris.audit-log")
public class PolarisAuditLogProperties {
/**
* Whether service call audit logging is enabled.
*/
private boolean enabled = false;
/**
* Audit log output format.
*/
private String format = "json";
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
}

@ -0,0 +1,42 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.rpc.enhancement.audit.config;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Auto-configuration for Polaris service call audit logging.
*
* @author Yuwei Fu
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnPolarisEnabled
@EnableConfigurationProperties(PolarisAuditLogProperties.class)
public class PolarisAuditLogPropertiesAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public AuditLogConfigModifier auditLogConfigModifier(PolarisAuditLogProperties auditLogProperties) {
return new AuditLogConfigModifier(auditLogProperties);
}
}

@ -30,6 +30,18 @@
"defaultValue": "", "defaultValue": "",
"description": "Specify the Http status code(s) that needs to be reported as FAILED." "description": "Specify the Http status code(s) that needs to be reported as FAILED."
}, },
{
"name": "spring.cloud.polaris.audit-log.enabled",
"type": "java.lang.Boolean",
"defaultValue": false,
"description": "Whether service call audit logging is enabled."
},
{
"name": "spring.cloud.polaris.audit-log.format",
"type": "java.lang.String",
"defaultValue": "json",
"description": "Service call audit log output format."
},
{ {
"name": "spring.cloud.polaris.stat.enabled", "name": "spring.cloud.polaris.stat.enabled",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",

@ -1,3 +1,4 @@
com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration com.tencent.cloud.rpc.enhancement.config.RpcEnhancementAutoConfiguration
com.tencent.cloud.rpc.enhancement.audit.config.PolarisAuditLogPropertiesAutoConfiguration
com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration com.tencent.cloud.rpc.enhancement.stat.config.PolarisStatPropertiesAutoConfiguration
com.tencent.cloud.rpc.enhancement.config.RpcEnhancementPropertiesAutoConfiguration com.tencent.cloud.rpc.enhancement.config.RpcEnhancementPropertiesAutoConfiguration

@ -0,0 +1,109 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.rpc.enhancement.audit.config;
import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.polaris.api.config.global.StatReporterConfig;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.global.GlobalConfigImpl;
import com.tencent.polaris.factory.config.global.StatReporterConfigImpl;
import com.tencent.polaris.plugins.stat.audit.AuditLogConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test for {@link AuditLogConfigModifier}.
*
* @author Yuwei Fu
*/
public class AuditLogConfigModifierTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PolarisContextAutoConfiguration.class,
PolarisAuditLogPropertiesAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true")
.withPropertyValues("spring.application.name=audit-test")
.withPropertyValues("spring.cloud.gateway.enabled=false");
@BeforeEach
void setUp() {
PolarisSDKContextManager.innerDestroy();
}
@Test
void testModify() {
PolarisAuditLogProperties properties = new PolarisAuditLogProperties();
properties.setEnabled(true);
properties.setFormat("json");
ConfigurationImpl configuration = mock(ConfigurationImpl.class);
GlobalConfigImpl globalConfig = mock(GlobalConfigImpl.class);
StatReporterConfigImpl statReporterConfig = mock(StatReporterConfigImpl.class);
AuditLogConfig auditLogConfig = new AuditLogConfig();
when(configuration.getGlobal()).thenReturn(globalConfig);
when(globalConfig.getStatReporter()).thenReturn(statReporterConfig);
when(statReporterConfig.getPluginConfig(
StatReporterConfig.DEFAULT_REPORTER_AUDIT_LOG, AuditLogConfig.class))
.thenReturn(auditLogConfig);
AuditLogConfigModifier modifier = new AuditLogConfigModifier(properties);
modifier.modify(configuration);
assertThat(auditLogConfig.isEnable()).isTrue();
assertThat(auditLogConfig.getFormat()).isEqualTo("json");
assertThat(modifier.getOrder()).isEqualTo(OrderConstant.Modifier.AUDIT_LOG_ORDER);
}
@Test
void testModifySdkConfiguration() {
contextRunner
.withPropertyValues("spring.cloud.polaris.audit-log.enabled=true")
.withPropertyValues("spring.cloud.polaris.audit-log.format=json")
.run(context -> {
PolarisSDKContextManager contextManager = context.getBean(PolarisSDKContextManager.class);
StatReporterConfig statReporter = contextManager.getSDKContext().getConfig()
.getGlobal().getStatReporter();
AuditLogConfig auditLogConfig = statReporter.getPluginConfig(
StatReporterConfig.DEFAULT_REPORTER_AUDIT_LOG, AuditLogConfig.class);
assertThat(auditLogConfig.isEnable()).isTrue();
assertThat(auditLogConfig.getFormat()).isEqualTo("json");
});
}
@Test
void testAuditLogDisabledByDefault() {
contextRunner.run(context -> {
PolarisSDKContextManager contextManager = context.getBean(PolarisSDKContextManager.class);
StatReporterConfig statReporter = contextManager.getSDKContext().getConfig()
.getGlobal().getStatReporter();
AuditLogConfig auditLogConfig = statReporter.getPluginConfig(
StatReporterConfig.DEFAULT_REPORTER_AUDIT_LOG, AuditLogConfig.class);
assertThat(auditLogConfig.isEnable()).isFalse();
assertThat(auditLogConfig.getFormat()).isEqualTo("json");
});
}
}

@ -0,0 +1,46 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.rpc.enhancement.audit.config;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisAuditLogPropertiesAutoConfiguration}.
*
* @author Yuwei Fu
*/
public class PolarisAuditLogPropertiesAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisAuditLogPropertiesAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true");
@Test
void testDefaultInitialization() {
contextRunner.run(context -> {
assertThat(context).hasSingleBean(PolarisAuditLogPropertiesAutoConfiguration.class);
assertThat(context).hasSingleBean(PolarisAuditLogProperties.class);
assertThat(context).hasSingleBean(AuditLogConfigModifier.class);
});
}
}

@ -0,0 +1,58 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 Tencent. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.rpc.enhancement.audit.config;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PolarisAuditLogProperties}.
*
* @author Yuwei Fu
*/
public class PolarisAuditLogPropertiesTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisAuditLogPropertiesAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.enabled=true");
@Test
void testDefaultValues() {
contextRunner.run(context -> {
PolarisAuditLogProperties properties = context.getBean(PolarisAuditLogProperties.class);
assertThat(properties.isEnabled()).isFalse();
assertThat(properties.getFormat()).isEqualTo("json");
});
}
@Test
void testConfiguredValues() {
contextRunner
.withPropertyValues("spring.cloud.polaris.audit-log.enabled=true")
.withPropertyValues("spring.cloud.polaris.audit-log.format=json")
.run(context -> {
PolarisAuditLogProperties properties = context.getBean(PolarisAuditLogProperties.class);
assertThat(properties.isEnabled()).isTrue();
assertThat(properties.getFormat()).isEqualTo("json");
});
}
}
Loading…
Cancel
Save