feat: support audit log

pull/1812/head
fishtailfu 1 month ago
parent 859b0258d8
commit 0f2f4a473f

@ -90,7 +90,7 @@
<properties>
<!-- Project revision -->
<revision>2.1.2.0-2024.0.3</revision>
<revision>2.2.0.0-2024.0.3-SNAPSHOT</revision>
<!-- Spring Framework -->
<spring.framework.version>6.2.18</spring.framework.version>

@ -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.
*/

@ -71,10 +71,10 @@
<properties>
<!-- Project revision -->
<revision>2.1.2.0-2024.0.3</revision>
<revision>2.2.0.0-2024.0.3-SNAPSHOT</revision>
<!-- Polaris SDK version -->
<polaris.version>2.1.2.0</polaris.version>
<polaris.version>2.2.0.0-SNAPSHOT</polaris.version>
<!-- Dependencies -->
<springdoc.version>2.8.14</springdoc.version>

@ -0,0 +1,49 @@
/*
* 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.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.consumer.AuditLogConfigImpl;
/**
* 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) {
AuditLogConfigImpl auditLogConfig = configuration.getConsumer().getAuditLog();
auditLogConfig.setEnable(auditLogProperties.isEnabled());
auditLogConfig.setFormat(auditLogProperties.getFormat());
}
@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": "",
"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",

@ -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

@ -0,0 +1,99 @@
/*
* 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.consumer.AuditLogConfig;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.consumer.AuditLogConfigImpl;
import com.tencent.polaris.factory.config.consumer.ConsumerConfigImpl;
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.verify;
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);
ConsumerConfigImpl consumerConfig = mock(ConsumerConfigImpl.class);
AuditLogConfigImpl auditLogConfig = mock(AuditLogConfigImpl.class);
when(configuration.getConsumer()).thenReturn(consumerConfig);
when(consumerConfig.getAuditLog()).thenReturn(auditLogConfig);
AuditLogConfigModifier modifier = new AuditLogConfigModifier(properties);
modifier.modify(configuration);
verify(auditLogConfig).setEnable(true);
verify(auditLogConfig).setFormat("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);
AuditLogConfig auditLogConfig = contextManager.getSDKContext().getConfig().getConsumer().getAuditLog();
assertThat(auditLogConfig.isEnable()).isTrue();
assertThat(auditLogConfig.getFormat()).isEqualTo("json");
});
}
@Test
void testAuditLogDisabledByDefault() {
contextRunner.run(context -> {
PolarisSDKContextManager contextManager = context.getBean(PolarisSDKContextManager.class);
AuditLogConfig auditLogConfig = contextManager.getSDKContext().getConfig().getConsumer().getAuditLog();
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