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
parent
4ed44e001a
commit
8f15826c5f
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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…
Reference in new issue