Skip to content

Commit

Permalink
Merge pull request #1989 from liujunzxcv/patch_1077
Browse files Browse the repository at this point in the history
[ISSUE #1077] Extraction of constants [GithubProtocol HookConfigOperationManage WebHookController]
close #1077
  • Loading branch information
xwm1992 authored Oct 28, 2022
2 parents 4c0dddb + 793b102 commit 97ee665
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@

public class WebHookController {

private static final String WEBHOOK_PRODUCER_CONNECTOR_PROP = "eventMesh.webHook.producer.connector";

private static final String PROTOCOL_ADAPTOR = "webhook";

private static final String CONTENT_TYPE = "content-type";

private static final String UUID_GENERATE_MODE = "uuid";

private static final String DOT = ".";

/**
* protocol pool
*/
Expand All @@ -60,9 +70,9 @@ public class WebHookController {

public void init() throws Exception {
this.webHookMQProducer = new WebHookMQProducer(configurationWrapper.getProperties(),
configurationWrapper.getProp("eventMesh.webHook.producer.connector"));
configurationWrapper.getProp(WEBHOOK_PRODUCER_CONNECTOR_PROP));
this.hookConfigOperationManage = new HookConfigOperationManage(configurationWrapper);
this.protocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor("webhook");
this.protocolAdaptor = ProtocolPluginFactory.getProtocolAdaptor(PROTOCOL_ADAPTOR);
}

/**
Expand All @@ -83,9 +93,9 @@ public void execute(String path, Map<String, String> header, byte[] body) throws
throw new Exception("No matching webhookConfig.");
}

if (!Objects.equals(webHookConfig.getContentType(), header.get("content-type"))) {
if (!Objects.equals(webHookConfig.getContentType(), header.get(CONTENT_TYPE))) {
throw new Exception(
"http request header content-type value is mismatch. current value " + header.get("content-type"));
"http request header content-type value is mismatch. current value " + header.get(CONTENT_TYPE));
}

// 2. get ManufacturerProtocol and execute
Expand All @@ -100,9 +110,9 @@ public void execute(String path, Map<String, String> header, byte[] body) throws
}

// 3. convert to cloudEvent obj
String cloudEventId = "uuid".equals(webHookConfig.getCloudEventIdGenerateMode()) ? UUID.randomUUID().toString()
String cloudEventId = UUID_GENERATE_MODE.equals(webHookConfig.getCloudEventIdGenerateMode()) ? UUID.randomUUID().toString()
: webHookRequest.getManufacturerEventId();
String eventType = manufacturerName + "." + webHookConfig.getManufacturerEventName();
String eventType = manufacturerName + DOT + webHookConfig.getManufacturerEventName();

WebhookProtocolTransportObject webhookProtocolTransportObject = WebhookProtocolTransportObject.builder()
.cloudEventId(cloudEventId).eventType(eventType).cloudEventName(webHookConfig.getCloudEventName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,35 @@

public class GithubProtocol implements ManufacturerProtocol {

private static final String MANU_FACTURER_NAME = "github";

private static final String FROM_SIGNATURE = "x-hub-signature-256";

private static final String MANU_FACTURER_EVENT_ID = "x-github-delivery";

private static final String HASH = "sha256=";

private static final String H_MAC_SHA = "HmacSHA256";

private static final Char ZERO_CHAR = '0';

public Logger logger = LoggerFactory.getLogger(this.getClass());

@Override
public String getManufacturerName() {
return "github";
return MANU_FACTURER_NAME;
}

@Override
public void execute(WebHookRequest webHookRequest, WebHookConfig webHookConfig, Map<String, String> header) throws Exception {

String fromSignature = header.get("x-hub-signature-256");
String fromSignature = header.get(FROM_SIGNATURE);
if (!isValid(fromSignature, webHookRequest.getData(), webHookConfig.getSecret())) {
throw new Exception("webhook-GithubProtocol authenticate failed");
}

try {
webHookRequest.setManufacturerEventId(header.get("x-github-delivery"));
webHookRequest.setManufacturerEventId(header.get(MANU_FACTURER_EVENT_ID));
webHookRequest.setManufacturerEventName(webHookConfig.getManufacturerEventName());
webHookRequest.setManufacturerSource(getManufacturerName());
} catch (Exception e) {
Expand All @@ -66,10 +78,10 @@ public void execute(WebHookRequest webHookRequest, WebHookConfig webHookConfig,
* @return Authentication result
*/
private Boolean isValid(String fromSignature, byte[] data, String secret) {
String hash = "sha256=";
String hash = HASH;
try {
Mac sha = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(Constants.DEFAULT_CHARSET), "HmacSHA256");
Mac sha = Mac.getInstance(H_MAC_SHA);
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(Constants.DEFAULT_CHARSET), H_MAC_SHA);
sha.init(secretKey);
byte[] bytes = sha.doFinal(data);
hash += byteArrayToHexString(bytes);
Expand All @@ -91,7 +103,7 @@ private String byteArrayToHexString(byte[] b) {
for (int n = 0; b != null && n < b.length; n++) {
stmp = Integer.toHexString(b[n] & 0XFF);
if (stmp.length() == 1) {
hs.append('0');
hs.append(ZERO_CHAR);
}
hs.append(stmp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@

public class HookConfigOperationManage implements WebHookConfigOperation {

private static final String FILE_OPERATION_MODE = "file";

private static final String WEBHOOK_FILEMODE_FILEPATH_PROP = "eventMesh.webHook.fileMode.filePath";

private static final String NACOS_OPERATION_MODE = "nacos";

private static final String WEBHOOK_NACOSMODE = "eventMesh.webHook.nacosMode";

/**
* webhook config pool -> key is CallbackPath
*/
Expand All @@ -64,10 +72,10 @@ public HookConfigOperationManage(ConfigurationWrapper configurationWrapper) thro

this.operationMode = configurationWrapper.getProp(WebHookOperationConstant.OPERATION_MODE_CONFIG_NAME);

if ("file".equals(operationMode)) {
new WebhookFileListener(configurationWrapper.getProp("eventMesh.webHook.fileMode.filePath"), cacheWebHookConfig);
} else if ("nacos".equals(operationMode)) {
nacosModeInit(configurationWrapper.getPropertiesByConfig("eventMesh.webHook.nacosMode", true));
if (FILE_OPERATION_MODE.equals(operationMode)) {
new WebhookFileListener(configurationWrapper.getProp(WEBHOOK_FILEMODE_FILEPATH_PROP), cacheWebHookConfig);
} else if (NACOS_OPERATION_MODE.equals(operationMode)) {
nacosModeInit(configurationWrapper.getPropertiesByConfig(WEBHOOK_NACOSMODE, true));
}
}

Expand All @@ -77,9 +85,9 @@ private void nacosModeInit(Properties config) throws NacosException {

@Override
public WebHookConfig queryWebHookConfigById(WebHookConfig webHookConfig) {
if ("file".equals(operationMode)) {
if (FILE_OPERATION_MODE.equals(operationMode)) {
return cacheWebHookConfig.get(StringUtils.getFileName(webHookConfig.getCallbackPath()));
} else if ("nacos".equals(operationMode)) {
} else if (NACOS_OPERATION_MODE.equals(operationMode)) {
try {
String content = nacosConfigService.getConfig(webHookConfig.getManufacturerEventName() + DATA_ID_EXTENSION,
GROUP_PREFIX + webHookConfig.getManufacturerName(), TIMEOUT_MS);
Expand Down

0 comments on commit 97ee665

Please sign in to comment.