diff --git a/CHANGELOG.md b/CHANGELOG.md index ff9acde0d..5d1a47f23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,4 +14,5 @@ - [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) - [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) \ No newline at end of file +- [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) diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java index b94d141b8..4fdc07e8d 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java @@ -195,6 +195,11 @@ public class OrderConstant { */ 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. */ diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 81973912e..a20d549f7 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -251,6 +251,16 @@ com.tencent.polaris ${polaris.version} + + com.tencent.polaris + stat-audit + ${polaris.version} + + + com.tencent.polaris + polaris-config + ${polaris.version} + polaris-threadlocal diff --git a/spring-cloud-tencent-polaris-context/pom.xml b/spring-cloud-tencent-polaris-context/pom.xml index ce2c0a6d4..bfce23094 100644 --- a/spring-cloud-tencent-polaris-context/pom.xml +++ b/spring-cloud-tencent-polaris-context/pom.xml @@ -21,6 +21,15 @@ + + com.tencent.polaris + polaris-config + + + com.tencent.polaris + stat-audit + + org.springframework spring-web diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/AuditLogConfigModifier.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/AuditLogConfigModifier.java new file mode 100644 index 000000000..abfb85fa5 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/AuditLogConfigModifier.java @@ -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; + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogProperties.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogProperties.java new file mode 100644 index 000000000..842cf1bf3 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogProperties.java @@ -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; + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesAutoConfiguration.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesAutoConfiguration.java new file mode 100644 index 000000000..c6bbcdd50 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesAutoConfiguration.java @@ -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); + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 8b283025d..3ac1214a0 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -30,6 +30,18 @@ "defaultValue": "", "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", "type": "java.lang.Boolean", diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index c400229e0..c7e48c90a 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-tencent-rpc-enhancement/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,3 +1,4 @@ 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.config.RpcEnhancementPropertiesAutoConfiguration diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/AuditLogConfigModifierTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/AuditLogConfigModifierTest.java new file mode 100644 index 000000000..4d9b83bf5 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/AuditLogConfigModifierTest.java @@ -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"); + }); + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesAutoConfigurationTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesAutoConfigurationTest.java new file mode 100644 index 000000000..d1964f356 --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesAutoConfigurationTest.java @@ -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); + }); + } +} diff --git a/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesTest.java b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesTest.java new file mode 100644 index 000000000..77ba546cb --- /dev/null +++ b/spring-cloud-tencent-rpc-enhancement/src/test/java/com/tencent/cloud/rpc/enhancement/audit/config/PolarisAuditLogPropertiesTest.java @@ -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"); + }); + } +}