diff options
| author | doufenghu <[email protected]> | 2024-11-06 09:39:06 +0800 |
|---|---|---|
| committer | doufenghu <[email protected]> | 2024-11-06 09:39:06 +0800 |
| commit | 625b40a8b0618eddc5eb9aab0f04eb9bc9d6b489 (patch) | |
| tree | 074b9694c0843448ddd619f2df43249724bffe5b | |
| parent | 696802a16602a54604373d529e0cdf0ffaa3afab (diff) | |
[Improve][HMAC] A nullable value not output an HMAC value.
4 files changed, 44 insertions, 23 deletions
diff --git a/groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java b/groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java index 92417da..87bbf36 100644 --- a/groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java +++ b/groot-common/src/main/java/com/geedgenetworks/common/config/UDFContextConfigOptions.java @@ -49,6 +49,11 @@ public interface UDFContextConfigOptions { .noDefaultValue() .withDescription("The identifier for the parameters of function."); + Option<String> PARAMETERS_SECRET_KEY = Options.key("secret_key") + .stringType() + .noDefaultValue() + .withDescription("The secret key for the function."); + Option<String> FUNCTION = Options.key("function") .stringType() .noDefaultValue() diff --git a/groot-core/src/main/java/com/geedgenetworks/core/udf/Hmac.java b/groot-core/src/main/java/com/geedgenetworks/core/udf/Hmac.java index 098cdef..a18d361 100644 --- a/groot-core/src/main/java/com/geedgenetworks/core/udf/Hmac.java +++ b/groot-core/src/main/java/com/geedgenetworks/core/udf/Hmac.java @@ -3,6 +3,9 @@ package com.geedgenetworks.core.udf; import cn.hutool.crypto.digest.HMac; import cn.hutool.crypto.digest.HmacAlgorithm; import com.geedgenetworks.common.Event; +import com.geedgenetworks.common.config.CheckResult; +import com.geedgenetworks.common.config.CheckUDFContextUtil; +import com.geedgenetworks.common.config.UDFContextConfigOptions; import com.geedgenetworks.common.exception.CommonErrorCode; import com.geedgenetworks.common.exception.GrootStreamRuntimeException; import com.geedgenetworks.common.udf.ScalarFunction; @@ -21,7 +24,7 @@ public class Hmac implements ScalarFunction { @Override public void open(RuntimeContext runtimeContext, UDFContext udfContext) { - checkUdfContext(udfContext); + checkConfig(udfContext); String secretKey = udfContext.getParameters().get("secret_key").toString(); String algorithm = "sha256"; if (udfContext.getParameters().containsKey("algorithm")) { @@ -39,21 +42,22 @@ public class Hmac implements ScalarFunction { @Override public Event evaluate(Event event) { String encodeResult = ""; - String message = (String) event.getExtractedFields().get(lookupFieldName); - if (StringUtil.isNotBlank(message)) { + Object value = event.getExtractedFields().get(lookupFieldName); + if (StringUtil.isNotEmpty(value)) { switch (outputFormat) { case "hex": - encodeResult = hMac.digestHex(message); + encodeResult = hMac.digestHex(value.toString()); break; case "base64": - encodeResult = hMac.digestBase64(message, false); + encodeResult = hMac.digestBase64(value.toString(), false); break; default: - encodeResult = hMac.digestBase64(message, false); + encodeResult = hMac.digestBase64(value.toString(), false); break; } + event.getExtractedFields().put(outputFieldName, encodeResult); } - event.getExtractedFields().put(outputFieldName, encodeResult); + return event; } @@ -67,21 +71,25 @@ public class Hmac implements ScalarFunction { } - private void checkUdfContext(UDFContext udfContext) { - if (udfContext.getParameters() == null || udfContext.getOutputFields() == null) { - throw new GrootStreamRuntimeException(CommonErrorCode.ILLEGAL_ARGUMENT, "Missing required parameters"); - } - if (udfContext.getLookupFields().size() != 1) { - throw new GrootStreamRuntimeException(CommonErrorCode.ILLEGAL_ARGUMENT, "The function lookup fields only support 1 value"); - } - if (udfContext.getOutputFields().size() != 1) { - throw new GrootStreamRuntimeException(CommonErrorCode.ILLEGAL_ARGUMENT, "The function output fields only support 1 value"); + @Override + public void checkConfig(UDFContext udfContext) { + + CheckResult result = CheckUDFContextUtil.checkAllExists(udfContext, + UDFContextConfigOptions.LOOKUP_FIELDS.key(), + UDFContextConfigOptions.OUTPUT_FIELDS.key(), + UDFContextConfigOptions.PARAMETERS.key()); + + if (!result.isSuccess()) { + throw new GrootStreamRuntimeException(CommonErrorCode.CONFIG_VALIDATION_FAILED, result.getMsg()); } - if (!udfContext.getParameters().containsKey("secret_key")) { - throw new GrootStreamRuntimeException(CommonErrorCode.ILLEGAL_ARGUMENT, "parameters must contains secret_key"); + result = CheckUDFContextUtil.checkParametersContainsKeys(udfContext, UDFContextConfigOptions.PARAMETERS_SECRET_KEY.key()); + if (!result.isSuccess()) { + throw new GrootStreamRuntimeException(CommonErrorCode.ILLEGAL_ARGUMENT, result.getMsg()); } + } + private String getHmacAlgorithm(String algorithm) { if (StringUtil.containsIgnoreCase(algorithm, "sha256")) { return HmacAlgorithm.HmacSHA256.getValue(); diff --git a/groot-examples/end-to-end-example/src/main/resources/examples/inline_to_print_test.yaml b/groot-examples/end-to-end-example/src/main/resources/examples/inline_to_print_test.yaml index 69dc9e1..65cd3cb 100644 --- a/groot-examples/end-to-end-example/src/main/resources/examples/inline_to_print_test.yaml +++ b/groot-examples/end-to-end-example/src/main/resources/examples/inline_to_print_test.yaml @@ -142,7 +142,14 @@ processing_pipelines: lookup_fields: [ phone_number ] output_fields: [ phone_number] parameters: - identifier: aes-128-gcm96 + identifier: aes-128-gcm + - function: HMAC + lookup_fields: [ phone_number ] + output_fields: [ phone_number_hmac] + parameters: + secret_key: Galaxy2019# + algorithm: sha256 + output_format: base64 sinks: @@ -155,11 +162,12 @@ application: env: name: example-inline-to-print parallelism: 3 - kms.type: local + kms.type: vault + shade.identifier: aes pipeline: object-reuse: true properties: - projection.encrypt.schema.registry.uri: 192.168.44.12:9999/v1/schema/session_record?option=encrypt_fields + projection.encrypt.schema.registry.uri: 192.168.44.12:9999/v1/database/table/session_record/schema?option=encrypt_fields topology: - name: inline_source #parallelism: 1 diff --git a/groot-examples/end-to-end-example/src/main/resources/grootstream.yaml b/groot-examples/end-to-end-example/src/main/resources/grootstream.yaml index 9dcc4ab..c21cf65 100644 --- a/groot-examples/end-to-end-example/src/main/resources/grootstream.yaml +++ b/groot-examples/end-to-end-example/src/main/resources/grootstream.yaml @@ -15,9 +15,9 @@ grootstream: type: local vault: type: vault - url: https://192.168.40.223:8200 + url: https://192.168.44.12:8200 username: galaxy - password: 123 + password: Galaxy2019# default_key_path: tsg_olap/transit plugin_key_path: tsg_olap/plugin/gmsm |
