Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,19 @@ public class ClientConfig {
public static final String DS_PRIVATE_KEY_PEM = DS_IDENTITY_PREFIX + "private_key_pem";
/**
* <code>scalar.dl.client.authentication_method</code> (Optional)<br>
* The authentication method for a client and servers. Use {@code "digital-signature"} (default)
* or {@code "hmac"}.
* The authentication method for clients and Ledger/Auditor servers. {@code "digital-signature"}
* (default) or {@code "hmac"} can be specified. This must be consistent with the Ledger/Auditor
* configuration.
*
* @deprecated This variable will be deleted in release 5.0.0. Use {@code
* scalar.dl.client.authentication.method} instead.
*/
public static final String DEPRECATED_AUTHENTICATION_METHOD = PREFIX + "authentication_method";
/**
* <code>scalar.dl.client.authentication.method</code> (Optional)<br>
* The authentication method for a client and servers. Use {@code "digital-signature"} (default)
* or {@code "hmac"}.
* The authentication method for clients and Ledger/Auditor servers. {@code "digital-signature"}
* (default) or {@code "hmac"} can be specified. This must be consistent with the Ledger/Auditor
* configuration.
*/
public static final String AUTHENTICATION_METHOD = PREFIX + "authentication.method";
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2425,7 +2425,9 @@ public void execute_AuditorEnabledAndValidAuditorHmacSignatureGiven_ShouldExecut
Properties props2 = createProperties();
props2.put(LedgerConfig.AUDITOR_ENABLED, "true");
props2.put(LedgerConfig.PROOF_ENABLED, "true");
props2.put(LedgerConfig.SERVERS_AUTHENTICATION_HMAC_SECRET_KEY, SECRET_KEY_A);
props2.put(LedgerConfig.AUTHENTICATION_METHOD, AuthenticationMethod.HMAC.getMethod());
props2.put(LedgerConfig.AUTHENTICATION_HMAC_CIPHER_KEY, SOME_CIPHER_KEY);
props2.put(LedgerConfig.SERVERS_AUTHENTICATION_HMAC_SECRET_KEY, SECRET_KEY_B);
createServices(new LedgerConfig(props2));
String nonce = UUID.randomUUID().toString();
JsonNode contractArgument =
Expand All @@ -2436,18 +2438,18 @@ public void execute_AuditorEnabledAndValidAuditorHmacSignatureGiven_ShouldExecut
String argument = Argument.format(contractArgument, nonce, Collections.emptyList());

byte[] serialized =
ContractExecutionRequest.serialize(CREATE_CONTRACT_ID3, argument, ENTITY_ID_A, KEY_VERSION);
ContractExecutionRequest.serialize(CREATE_CONTRACT_ID3, argument, ENTITY_ID_C, KEY_VERSION);
ContractExecutionRequest request =
new ContractExecutionRequest(
nonce,
ENTITY_ID_A,
ENTITY_ID_C,
KEY_VERSION,
CREATE_CONTRACT_ID3,
argument,
Collections.emptyList(),
null,
dsSigner1.sign(serialized),
hmacSigner1.sign(nonce.getBytes(StandardCharsets.UTF_8)));
hmacSigner1.sign(serialized),
hmacSigner2.sign(nonce.getBytes(StandardCharsets.UTF_8)));

// Act
Throwable thrown = catchThrowable(() -> ledgerService.execute(request));
Expand All @@ -2463,6 +2465,8 @@ public void execute_AuditorEnabledAndValidAuditorHmacSignatureGiven_ShouldExecut
Properties props2 = createProperties();
props2.put(LedgerConfig.AUDITOR_ENABLED, "true");
props2.put(LedgerConfig.PROOF_ENABLED, "true");
props2.put(LedgerConfig.AUTHENTICATION_METHOD, AuthenticationMethod.HMAC.getMethod());
props2.put(LedgerConfig.AUTHENTICATION_HMAC_CIPHER_KEY, SOME_CIPHER_KEY);
props2.put(LedgerConfig.SERVERS_AUTHENTICATION_HMAC_SECRET_KEY, SECRET_KEY_A);
createServices(new LedgerConfig(props2));
String nonce = UUID.randomUUID().toString();
Expand All @@ -2474,17 +2478,17 @@ public void execute_AuditorEnabledAndValidAuditorHmacSignatureGiven_ShouldExecut
String argument = Argument.format(contractArgument, nonce, Collections.emptyList());

byte[] serialized =
ContractExecutionRequest.serialize(CREATE_CONTRACT_ID3, argument, ENTITY_ID_A, KEY_VERSION);
ContractExecutionRequest.serialize(CREATE_CONTRACT_ID3, argument, ENTITY_ID_C, KEY_VERSION);
ContractExecutionRequest request =
new ContractExecutionRequest(
nonce,
ENTITY_ID_A,
ENTITY_ID_C,
KEY_VERSION,
CREATE_CONTRACT_ID3,
argument,
Collections.emptyList(),
null,
dsSigner1.sign(serialized),
hmacSigner1.sign(serialized),
hmacSigner2.sign(nonce.getBytes(StandardCharsets.UTF_8))); // invalid HMAC signature

// Act
Expand Down
19 changes: 9 additions & 10 deletions ledger/src/main/java/com/scalar/dl/ledger/config/LedgerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public class LedgerConfig implements ServerConfig, ServersHmacAuthenticatable {
public static final String NAMESPACE = PREFIX + "namespace";
/**
* <code>scalar.dl.ledger.authentication.method</code> (Optional)<br>
* The authentication method for a client and servers. ("digital-signature" by default) This has
* to be consistent with the client configuration.
* The authentication method for clients and Ledger servers. {@code "digital-signature"} (default)
* or {@code "hmac"} can be specified.
*/
public static final String AUTHENTICATION_METHOD = PREFIX + "authentication.method";
/**
Expand Down Expand Up @@ -446,20 +446,19 @@ private void load() {
}
serversAuthHmacSecretKey =
ConfigUtils.getString(props, SERVERS_AUTHENTICATION_HMAC_SECRET_KEY, null);
if (serversAuthHmacSecretKey == null && proofPrivateKey == null) {
if (authenticationMethod == AuthenticationMethod.DIGITAL_SIGNATURE
&& proofPrivateKey == null) {
throw new IllegalArgumentException(
String.format(
"Authentication between Ledger and Auditor is not correctly configured."
+ "Set %s or set a private key with %s or %s.",
SERVERS_AUTHENTICATION_HMAC_SECRET_KEY,
PROOF_PRIVATE_KEY_PATH,
PROOF_PRIVATE_KEY_PEM));
}
if (authenticationMethod == AuthenticationMethod.HMAC && serversAuthHmacSecretKey == null) {
+ " Set a private key with %s or %s if you use digital signature authentication with Auditor enabled.",
PROOF_PRIVATE_KEY_PATH, PROOF_PRIVATE_KEY_PEM));
} else if (authenticationMethod == AuthenticationMethod.HMAC
&& serversAuthHmacSecretKey == null) {
throw new IllegalArgumentException(
String.format(
"Authentication between Ledger and Auditor is not correctly configured."
+ "Set %s if you use HMAC authentication with Auditor enabled.",
+ " Set %s if you use HMAC authentication with Auditor enabled.",
SERVERS_AUTHENTICATION_HMAC_SECRET_KEY));
}
} else { // Auditor disabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,20 +449,6 @@ public void constructor_AuditorAndProofEnabledAndPrivateKeyGiven_ShouldConstruct
assertThat(thrown).doesNotThrowAnyException();
}

@Test
public void constructor_AuditorAndProofEnabledAndSecretKeyGiven_ShouldConstructProperly() {
// Arrange
props.setProperty(LedgerConfig.AUDITOR_ENABLED, "true");
props.setProperty(LedgerConfig.PROOF_ENABLED, "true");
props.setProperty(LedgerConfig.SERVERS_AUTHENTICATION_HMAC_SECRET_KEY, SOME_KEY);

// Act
Throwable thrown = catchThrowable(() -> new LedgerConfig(props));

// Assert
assertThat(thrown).doesNotThrowAnyException();
}

@Test
public void
constructor_AuditorAndProofEnabledButNeitherPrivateKeyNorSecretKeyGiven_ShouldThrowIllegalArgumentException() {
Expand Down Expand Up @@ -524,12 +510,13 @@ public void constructor_AuditorEnabledButProofDisabled_ShouldThrowIllegalArgumen

@Test
public void
constructor_AuditorAndProofEnabledAndHmacConfiguredButSecretKeyNotGiven_ShouldThrowIllegalArgumentException() {
constructor_AuditorEnabledButProofDisabledWithHmacConfiguration_ShouldThrowIllegalArgumentException() {
// Arrange
props.setProperty(LedgerConfig.AUDITOR_ENABLED, "true");
props.setProperty(LedgerConfig.PROOF_ENABLED, "false");
props.setProperty(LedgerConfig.AUTHENTICATION_METHOD, AuthenticationMethod.HMAC.getMethod());
props.setProperty(LedgerConfig.AUTHENTICATION_HMAC_CIPHER_KEY, SOME_CIPHER_KEY);
props.setProperty(LedgerConfig.SERVERS_AUTHENTICATION_HMAC_SECRET_KEY, SOME_SECRET_KEY);

// Act
Throwable thrown = catchThrowable(() -> new LedgerConfig(props));
Expand All @@ -552,4 +539,38 @@ public void constructor_HmacEnabledButCipherKeyNotGiven_ShouldThrowIllegalArgume
// Assert
assertThat(thrown).isExactlyInstanceOf(IllegalArgumentException.class);
}

@Test
public void
constructor_AuditorAndProofEnabledInDigitalSignatureConfigurationButPrivateKeyNotGiven_ShouldThrowIllegalArgumentException() {
// Arrange
props.setProperty(LedgerConfig.AUDITOR_ENABLED, "true");
props.setProperty(LedgerConfig.PROOF_ENABLED, "true");
props.setProperty(
LedgerConfig.AUTHENTICATION_METHOD, AuthenticationMethod.DIGITAL_SIGNATURE.getMethod());
props.setProperty(LedgerConfig.SERVERS_AUTHENTICATION_HMAC_SECRET_KEY, SOME_SECRET_KEY);

// Act
Throwable thrown = catchThrowable(() -> new LedgerConfig(props));

// Assert
assertThat(thrown).isExactlyInstanceOf(IllegalArgumentException.class);
}

@Test
public void
constructor_AuditorAndProofEnabledInHmacConfigurationButSecretKeyNotGiven_ShouldThrowIllegalArgumentException() {
// Arrange
props.setProperty(LedgerConfig.AUDITOR_ENABLED, "true");
props.setProperty(LedgerConfig.PROOF_ENABLED, "true");
props.setProperty(LedgerConfig.AUTHENTICATION_METHOD, AuthenticationMethod.HMAC.getMethod());
props.setProperty(LedgerConfig.AUTHENTICATION_HMAC_CIPHER_KEY, SOME_CIPHER_KEY);
props.setProperty(LedgerConfig.PROOF_PRIVATE_KEY_PEM, SOME_PEM);

// Act
Throwable thrown = catchThrowable(() -> new LedgerConfig(props));

// Assert
assertThat(thrown).isExactlyInstanceOf(IllegalArgumentException.class);
}
}