-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cmd-socketio-server): remove code duplication
- Move config-reading and signMessageJwt helper functions from validators to cmd-socketio-server to remove code duplication. Refactor validators to use these common instead of own implementation. - Remove ValidatorAuthentication.ts that is not used anymore (not part of public interface, it was copied by validators during before couple commits ago). - Updated readme with instructions of how to start asset-trade and electricity-trade samples without docker-compose (to be used during development). Added helper script for patching the config. Signed-off-by: Michal Bajer <[email protected]>
- Loading branch information
Showing
33 changed files
with
317 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
examples/cactus-example-discounted-asset-trade/script-dockerless-config-patch.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env bash | ||
# Copyright 2020-2022 Hyperledger Cactus Contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
COMMON_CACTUS_CONFIG="/etc/cactus/" | ||
|
||
echo "Note - script must executed from within cactus-example-discounted-asset-trade directory!" | ||
|
||
echo "Copy local cactus config to common location ($COMMON_CACTUS_CONFIG)" | ||
sudo rm -rf "$COMMON_CACTUS_CONFIG" | ||
sudo cp -ar "./etc/cactus" "/etc" | ||
sudo chown -hR $(whoami) "$COMMON_CACTUS_CONFIG" | ||
|
||
echo "Patch validators..." | ||
sed -i 's/asset_trade_faio2x_testnet/localhost/g' "${COMMON_CACTUS_CONFIG}/connector-fabric-socketio/default.yaml" | ||
sed -i 's/geth1/localhost/g' "${COMMON_CACTUS_CONFIG}/connector-go-ethereum-socketio/default.yaml" | ||
|
||
echo "Patch validator-registry-config.yaml..." | ||
sed -i 's/ethereum-validator/localhost/g' "${COMMON_CACTUS_CONFIG}/validator-registry-config.yaml" | ||
sed -i 's/fabric-socketio-validator/localhost/g' "${COMMON_CACTUS_CONFIG}/validator-registry-config.yaml" | ||
sed -i 's/indy-validator-nginx/localhost/g' "${COMMON_CACTUS_CONFIG}/validator-registry-config.yaml" | ||
|
||
echo "Patch path to asset-trade modules." | ||
current_pwd=$(pwd) | ||
escaped_pwd=${current_pwd//\//\\/} | ||
sed -i "s/\/root\/cactus/$escaped_pwd/g" "${COMMON_CACTUS_CONFIG}/usersetting.yaml" | ||
|
||
echo "Done." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
examples/cactus-example-electricity-trade/script-dockerless-config-patch.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env bash | ||
# Copyright 2020-2022 Hyperledger Cactus Contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
COMMON_CACTUS_CONFIG="/etc/cactus/" | ||
|
||
echo "Note - script must executed from within cactus-example-electricity-trade directory!" | ||
|
||
echo "Copy local cactus config to common location ($COMMON_CACTUS_CONFIG)" | ||
sudo rm -rf "$COMMON_CACTUS_CONFIG" | ||
sudo cp -ar "./etc/cactus" "/etc" | ||
sudo chown -hR $(whoami) "$COMMON_CACTUS_CONFIG" | ||
|
||
echo "Patch validators..." | ||
sed -i 's/geth1/localhost/g' "${COMMON_CACTUS_CONFIG}/connector-go-ethereum-socketio/default.yaml" | ||
sed -i 's/rest-api/localhost/g' "${COMMON_CACTUS_CONFIG}/connector-sawtooth-socketio/default.yaml" | ||
|
||
echo "Patch validator-registry-config.yaml..." | ||
sed -i 's/ethereum-validator/localhost/g' "${COMMON_CACTUS_CONFIG}/validator-registry-config.yaml" | ||
sed -i 's/sawtooth-validator/localhost/g' "${COMMON_CACTUS_CONFIG}/validator-registry-config.yaml" | ||
|
||
echo "Patch path to electricity-trade modules." | ||
current_pwd=$(pwd) | ||
escaped_pwd=${current_pwd//\//\\/} | ||
sed -i "s/\/root\/cactus/$escaped_pwd/g" "${COMMON_CACTUS_CONFIG}/usersetting.yaml" | ||
|
||
echo "Done." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
packages/cactus-cmd-socketio-server/src/main/typescript/verifier/ValidatorAuthentication.ts
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
packages/cactus-cmd-socketio-server/src/main/typescript/verifier/validator-authentication.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2020-2022 Hyperledger Cactus Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import fs from "fs"; | ||
import jwt from "jsonwebtoken"; | ||
import crypto from "crypto"; | ||
import { configRead } from "../util/config"; | ||
|
||
type PayloadType = Parameters<typeof jwt.sign>[0]; | ||
|
||
const DEFAULT_EXPIRATION_TIME = 60 * 15; // 15 minutes | ||
|
||
const supportedJwtAlgos: jwt.Algorithm[] = [ | ||
"ES256", | ||
"ES384", | ||
"ES512", | ||
"RS256", | ||
"RS384", | ||
"RS512", | ||
]; | ||
|
||
// Will keep the private key once it's succesfully read | ||
let privateKey: string; | ||
|
||
/** | ||
* Sign a message to be sent from socketio connector (validator) to a client. | ||
* | ||
* @param privateKey - Validator private key. Only ECDSA and RSA keys are supported. | ||
* @param payload - Message to be encoded. | ||
* @param jwtAlgo - JWT algorithm to use. Must match key used (ES*** or RS***) | ||
* @param expirationTime - JWT expiration time | ||
* @returns JWT signed message that can be sent over the wire. | ||
*/ | ||
export function signValidatorMessageJwt( | ||
privateKey: jwt.Secret, | ||
payload: PayloadType, | ||
jwtAlgo: jwt.Algorithm = "ES256", | ||
expirationTime: number = DEFAULT_EXPIRATION_TIME, | ||
): string { | ||
if (!supportedJwtAlgos.includes(jwtAlgo)) { | ||
throw new Error( | ||
`Wrong JWT Algorithm. Supported algos: ${supportedJwtAlgos.toString()}`, | ||
); | ||
} | ||
|
||
// Check if key supported and JWT algorithm matches the provided key type | ||
const keyType = crypto.createPrivateKey(privateKey).asymmetricKeyType; | ||
if ( | ||
!( | ||
(keyType === "rsa" && jwtAlgo.startsWith("RS")) || | ||
(keyType === "ec" && jwtAlgo.startsWith("ES")) | ||
) | ||
) { | ||
throw new Error(`Not supported combination ${keyType}/${jwtAlgo}.`); | ||
} | ||
|
||
const option: jwt.SignOptions = { | ||
algorithm: jwtAlgo, | ||
expiresIn: expirationTime, | ||
}; | ||
|
||
return jwt.sign(payload, privateKey, option); | ||
} | ||
|
||
/** | ||
* Validator-side function to sign message to be sent to the client. | ||
* Will read the private key either as value in validator config `sslParam.keyValue`, | ||
* or read from filesystem under path `sslParam.key`. | ||
* | ||
* @param payload - Message to sign | ||
* @returns Signed message | ||
*/ | ||
export function signMessageJwt(payload: object): string { | ||
if (!privateKey) { | ||
try { | ||
privateKey = configRead<string>('sslParam.keyValue'); | ||
} catch { | ||
privateKey = fs.readFileSync(configRead('sslParam.key'), "ascii"); | ||
} | ||
} | ||
const jwtAlgo = configRead<jwt.Algorithm>('sslParam.jwtAlgo', 'ES256'); | ||
return signValidatorMessageJwt(privateKey, payload, jwtAlgo); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.