@@ -60,9 +60,8 @@ public class TelegrafMetricController {
6060 private final TenantProvider tenantProvider ;
6161
6262 private static final String [] ignoreTags = {"host" };
63- private static final Pattern VALID_NAME = Pattern .compile ("[a-zA-Z0-9._\\ -]+" );
64- private static final Pattern CONTROL_CHARS = Pattern .compile ("[\\ p{Cntrl}]" );
65- private static final int MAX_LOG_LENGTH = 100 ;
63+ private static final Pattern VALID_HOST_NAME = Pattern .compile ("[a-z]([a-z0-9\\ -._]{0,254}[a-z0-9])?" );
64+ private static final Pattern VALID_GROUP_NAME = Pattern .compile ("[A-Za-z0-9][A-Za-z0-9.\\ -_]*" );
6665
6766 public TelegrafMetricController (SystemMetricService systemMetricService ,
6867 SystemMetricDataTypeService systemMetricMetadataService ,
@@ -76,33 +75,34 @@ public TelegrafMetricController(SystemMetricService systemMetricService,
7675
7776
7877 @ PostMapping (value = "/telegraf" )
79- public ResponseEntity <Void > saveSystemMetric (
78+ public ResponseEntity <String > saveSystemMetric (
8079 @ RequestHeader (value = "hostGroupName" ) String hostGroupName ,
8180 @ RequestBody TelegrafMetrics telegrafMetrics , BindingResult bindingResult
8281 ) throws BindException {
8382 if (bindingResult .hasErrors ()) {
8483 SimpleErrorMessage simpleErrorMessage = new SimpleErrorMessage (bindingResult );
85- logger .warn ("metric binding error. header=hostGroupName:{} errorCount:{} {}" , sanitizeForLog ( hostGroupName ) , bindingResult .getErrorCount (), simpleErrorMessage );
84+ logger .warn ("metric binding error. header=hostGroupName:{} errorCount:{} {}" , hostGroupName , bindingResult .getErrorCount (), simpleErrorMessage );
8685 throw new BindException (bindingResult );
8786 }
8887
89- if (!VALID_NAME .matcher (hostGroupName ).matches ()) {
90- logger .warn ("invalid hostGroupName='{}'" , sanitizeForLog (hostGroupName ));
88+ if (!isValidGroupName (hostGroupName )) {
89+ logger .warn ("invalid hostGroupName='{}'" , hostGroupName );
90+ return ResponseEntity .badRequest ().body ("invalid hostGroupName='" + hostGroupName + "'" );
9191 }
9292
9393 String hostName = getHost (telegrafMetrics );
9494 if (StringUtils .isEmpty (hostName )) {
9595 // hostname null check
96- logger .info ("hostName is empty. hostGroupName={}" , sanitizeForLog ( hostGroupName ) );
97- return ResponseEntity .badRequest ().build ( );
96+ logger .info ("hostName is empty. hostGroupName={}" , hostGroupName );
97+ return ResponseEntity .badRequest ().body ( "hostName is empty" );
9898 }
9999
100- if (!VALID_NAME . matcher (hostName ). matches ( )) {
101- logger .warn ("invalid hostName='{}', hostGroupName='{}'" , sanitizeForLog ( hostName ), sanitizeForLog ( hostGroupName ) );
100+ if (!isValidHostName (hostName )) {
101+ logger .warn ("invalid hostName='{}', hostGroupName='{}'" , hostName , hostGroupName );
102102 }
103103
104104 if (logger .isDebugEnabled ()) {
105- logger .debug ("hostGroupName:{} host:{} size:{}" , sanitizeForLog ( hostGroupName ), sanitizeForLog ( hostName ) , telegrafMetrics .size ());
105+ logger .debug ("hostGroupName:{} host:{} size:{}" , hostGroupName , hostName , telegrafMetrics .size ());
106106 }
107107
108108 String tenantId = tenantProvider .getTenantId ();
@@ -112,7 +112,7 @@ public ResponseEntity<Void> saveSystemMetric(
112112 updateMetadata (systemMetric );
113113 systemMetricService .insert (systemMetric );
114114
115- return ResponseEntity .ok (). build ( );
115+ return ResponseEntity .ok (null );
116116 }
117117
118118 private String getHost (TelegrafMetrics metrics ) {
@@ -176,15 +176,12 @@ private Tag getHost(List<Tag> tTags) {
176176 return null ;
177177 }
178178
179- static String sanitizeForLog (String value ) {
180- if (value == null ) {
181- return null ;
182- }
183- String sanitized = CONTROL_CHARS .matcher (value ).replaceAll ("_" );
184- if (sanitized .length () > MAX_LOG_LENGTH ) {
185- sanitized = sanitized .substring (0 , MAX_LOG_LENGTH ) + "...(truncated)" ;
186- }
187- return sanitized ;
179+ static boolean isValidHostName (String value ) {
180+ return VALID_HOST_NAME .matcher (value ).matches ();
181+ }
182+
183+ static boolean isValidGroupName (String value ) {
184+ return VALID_GROUP_NAME .matcher (value ).matches ();
188185 }
189186
190187 static List <Tag > filterTag (List <Tag > tTags , String [] ignoreTagName ) {
0 commit comments