Skip to content

Commit 19f97f4

Browse files
authored
Make hmacSha256SigningSecret optional and settable by users (DEV-24702) (#73)
1 parent a95fce0 commit 19f97f4

25 files changed

+310
-923
lines changed

core/src/main/java/com/backblaze/b2/client/B2StorageClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.backblaze.b2.client.structures.B2DownloadAuthorization;
2323
import com.backblaze.b2.client.structures.B2DownloadByIdRequest;
2424
import com.backblaze.b2.client.structures.B2DownloadByNameRequest;
25-
import com.backblaze.b2.client.structures.B2EventNotificationRuleForRequest;
25+
import com.backblaze.b2.client.structures.B2EventNotificationRule;
2626
import com.backblaze.b2.client.structures.B2FileVersion;
2727
import com.backblaze.b2.client.structures.B2FinishLargeFileRequest;
2828
import com.backblaze.b2.client.structures.B2GetBucketNotificationRulesRequest;
@@ -1119,7 +1119,7 @@ default String getDownloadByNameUrl(String bucketName,
11191119
* @throws B2Exception if there's any trouble.
11201120
*/
11211121
default B2SetBucketNotificationRulesResponse setBucketNotificationRules(String bucketId,
1122-
List<B2EventNotificationRuleForRequest> eventNotificationRules) throws B2Exception {
1122+
List<B2EventNotificationRule> eventNotificationRules) throws B2Exception {
11231123
return setBucketNotificationRules(B2SetBucketNotificationRulesRequest.builder(bucketId, eventNotificationRules).build());
11241124
}
11251125

core/src/main/java/com/backblaze/b2/client/structures/B2CustomHeaderForRequest.java

Lines changed: 0 additions & 99 deletions
This file was deleted.

core/src/main/java/com/backblaze/b2/client/structures/B2EventNotification.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ public String toString() {
6262

6363
/**
6464
* Create a new EventNotification from JSON content with signature check.
65+
*
66+
* @param json - The JSON content to create an B2EventNotification object from. The byte array should be UTF-8 encoded
67+
* @return The B2EventNotification object
68+
* @throws B2JsonException - If the content is not valid JSON
69+
*/
70+
public static B2EventNotification parse(byte[] json) throws IOException, B2JsonException {
71+
B2Preconditions.checkArgumentIsNotNull(json, "json");
72+
B2Preconditions.checkArgument(json.length > 0);
73+
74+
return B2Json.get().fromJson(json , B2EventNotification.class);
75+
}
76+
77+
/**
78+
* Create a new EventNotification from JSON content with signature check.
79+
*
80+
* @param json - The JSON content to create an B2EventNotification object from.
81+
* @return The B2EventNotification object
82+
* @throws B2JsonException - If the content is not valid JSON
83+
*/
84+
public static B2EventNotification parse(String json) throws B2JsonException {
85+
B2Preconditions.checkArgument(!B2StringUtil.isEmpty(json), "json is required");
86+
87+
return B2Json.get().fromJson(json, B2EventNotification.class);
88+
}
89+
90+
/**
91+
* Create a new EventNotification from JSON content with signature check.
92+
*
6593
* @param json - The JSON content to create an B2EventNotification object from. The byte array should be UTF-8 encoded
6694
* @param signatureFromHeader - The value of the X-Bz-Event-Notification-Signature header
6795
* @param signingSecret - The secret for computing the signature.
@@ -75,14 +103,17 @@ public static B2EventNotification parse(byte[] json,
75103
throws B2JsonException, IOException, B2SignatureVerificationException {
76104
B2Preconditions.checkArgumentIsNotNull(json, "json");
77105
B2Preconditions.checkArgument(json.length > 0);
78-
B2Preconditions.checkArgumentIsNotNull(signatureFromHeader, "signatureFromHeader");
79-
B2Preconditions.checkArgumentIsNotNull(signingSecret, "signingSecret");
80-
SignatureUtils.verifySignature(json, signatureFromHeader, signingSecret);
106+
107+
// Only validate the signature if signature and secret are both provided
108+
if (signatureFromHeader != null && signingSecret != null) {
109+
SignatureUtils.verifySignature(json, signatureFromHeader, signingSecret);
110+
}
81111
return B2Json.get().fromJson(json , B2EventNotification.class);
82112
}
83113

84114
/**
85115
* Create a new EventNotification from JSON content with signature check.
116+
*
86117
* @param json - The JSON content to create an B2EventNotification object from.
87118
* @param signatureFromHeader - The value of the X-Bz-Event-Notification-Signature header
88119
* @param signingSecret - The secret for computing the signature.
@@ -94,12 +125,14 @@ public static B2EventNotification parse(String json,
94125
String signatureFromHeader,
95126
String signingSecret)
96127
throws B2JsonException, B2SignatureVerificationException {
97-
B2Preconditions.checkArgumentIsNotNull(json, "json");
98-
B2Preconditions.checkArgument(json.length() > 0);
99-
B2Preconditions.checkArgumentIsNotNull(signatureFromHeader, "signatureFromHeader");
100-
B2Preconditions.checkArgumentIsNotNull(signingSecret, "signingSecret");
101-
SignatureUtils.verifySignature(json.getBytes(StandardCharsets.UTF_8), signatureFromHeader, signingSecret);
102-
return B2Json.get().fromJson(json, B2EventNotification.class);
128+
B2Preconditions.checkArgument(!B2StringUtil.isEmpty(json), "json is required");
129+
130+
// Only validate the signature if signature and secret are both provided
131+
if (signatureFromHeader != null && signingSecret != null) {
132+
SignatureUtils.verifySignature(
133+
json.getBytes(StandardCharsets.UTF_8), signatureFromHeader, signingSecret);
134+
}
135+
return B2Json.get().fromJson(json , B2EventNotification.class);
103136
}
104137

105138
/*testing*/
Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package com.backblaze.b2.client.structures;
66

77
import com.backblaze.b2.json.B2Json;
8-
import com.backblaze.b2.util.B2Preconditions;
9-
import com.backblaze.b2.util.B2StringUtil;
108

119
import java.util.Comparator;
1210
import java.util.Objects;
@@ -15,14 +13,14 @@
1513
/**
1614
* One rule about under what condition(s) to send notifications for events in a bucket.
1715
*/
18-
public class B2EventNotificationRuleForResponse implements Comparable<B2EventNotificationRuleForResponse> {
19-
private static final Comparator<B2EventNotificationRuleForResponse> COMPARATOR = Comparator.comparing(B2EventNotificationRuleForResponse::getName)
16+
public class B2EventNotificationRule implements Comparable<B2EventNotificationRule> {
17+
private static final Comparator<B2EventNotificationRule> COMPARATOR = Comparator.comparing(B2EventNotificationRule::getName)
2018
.thenComparing(rule -> String.join(",", rule.getEventTypes()))
21-
.thenComparing(B2EventNotificationRuleForResponse::getObjectNamePrefix)
19+
.thenComparing(B2EventNotificationRule::getObjectNamePrefix)
2220
.thenComparing(rule -> rule.getTargetConfiguration().toString())
23-
.thenComparing(B2EventNotificationRuleForResponse::isEnabled)
24-
.thenComparing(B2EventNotificationRuleForResponse::isSuspended)
25-
.thenComparing(B2EventNotificationRuleForResponse::getSuspensionReason);
21+
.thenComparing(B2EventNotificationRule::isEnabled)
22+
.thenComparing(B2EventNotificationRule::isSuspended)
23+
.thenComparing(B2EventNotificationRule::getSuspensionReason);
2624

2725
/**
2826
* A name for identifying the rule. Names must be unique within a bucket.
@@ -52,7 +50,7 @@ public class B2EventNotificationRuleForResponse implements Comparable<B2EventNot
5250
* The target configuration for the event notification.
5351
*/
5452
@B2Json.required
55-
private final B2EventNotificationTargetConfigurationForResponse targetConfiguration;
53+
private final B2EventNotificationTargetConfiguration targetConfiguration;
5654

5755
/**
5856
* Indicates if the rule is enabled.
@@ -63,8 +61,8 @@ public class B2EventNotificationRuleForResponse implements Comparable<B2EventNot
6361
/**
6462
* Indicates if the rule is suspended.
6563
*/
66-
@B2Json.required
67-
private final boolean isSuspended;
64+
@B2Json.optional
65+
private final Boolean isSuspended;
6866

6967
/**
7068
* If isSuspended is true, specifies the reason the rule was
@@ -74,18 +72,13 @@ public class B2EventNotificationRuleForResponse implements Comparable<B2EventNot
7472
private final String suspensionReason;
7573

7674
@B2Json.constructor
77-
public B2EventNotificationRuleForResponse(String name,
78-
TreeSet<String> eventTypes,
79-
String objectNamePrefix,
80-
B2EventNotificationTargetConfigurationForResponse targetConfiguration,
81-
boolean isEnabled,
82-
boolean isSuspended,
83-
String suspensionReason) {
84-
85-
B2Preconditions.checkArgument(
86-
!isSuspended || !B2StringUtil.isEmpty(suspensionReason),
87-
"A suspension reason is required if isSuspended is true"
88-
);
75+
public B2EventNotificationRule(String name,
76+
TreeSet<String> eventTypes,
77+
String objectNamePrefix,
78+
B2EventNotificationTargetConfiguration targetConfiguration,
79+
boolean isEnabled,
80+
Boolean isSuspended,
81+
String suspensionReason) {
8982

9083
this.name = name;
9184
this.eventTypes = new TreeSet<>(eventTypes);
@@ -96,12 +89,12 @@ public B2EventNotificationRuleForResponse(String name,
9689
this.suspensionReason = suspensionReason;
9790
}
9891

99-
public B2EventNotificationRuleForResponse(String name,
100-
TreeSet<String> eventTypes,
101-
String objectNamePrefix,
102-
B2EventNotificationTargetConfigurationForResponse targetConfiguration,
103-
boolean isEnabled) {
104-
this(name, eventTypes, objectNamePrefix, targetConfiguration, isEnabled, false, null);
92+
public B2EventNotificationRule(String name,
93+
TreeSet<String> eventTypes,
94+
String objectNamePrefix,
95+
B2EventNotificationTargetConfiguration targetConfiguration,
96+
boolean isEnabled) {
97+
this(name, eventTypes, objectNamePrefix, targetConfiguration, isEnabled, null, null);
10598
}
10699

107100
public String getName() {
@@ -116,7 +109,7 @@ public String getObjectNamePrefix() {
116109
return objectNamePrefix;
117110
}
118111

119-
public B2EventNotificationTargetConfigurationForResponse getTargetConfiguration() {
112+
public B2EventNotificationTargetConfiguration getTargetConfiguration() {
120113
return targetConfiguration;
121114
}
122115

@@ -136,13 +129,13 @@ public String getSuspensionReason() {
136129
public boolean equals(Object o) {
137130
if (this == o) return true;
138131
if (o == null || getClass() != o.getClass()) return false;
139-
final B2EventNotificationRuleForResponse that = (B2EventNotificationRuleForResponse) o;
132+
final B2EventNotificationRule that = (B2EventNotificationRule) o;
140133
return isEnabled == that.isEnabled &&
141-
isSuspended == that.isSuspended &&
142134
name.equals(that.name) &&
143135
eventTypes.equals(that.eventTypes) &&
144136
objectNamePrefix.equals(that.objectNamePrefix) &&
145137
targetConfiguration.equals(that.targetConfiguration) &&
138+
Objects.equals(isSuspended, that.isSuspended) &&
146139
Objects.equals(suspensionReason, that.suspensionReason);
147140
}
148141

@@ -161,7 +154,7 @@ public int hashCode() {
161154

162155
@Override
163156
public String toString() {
164-
return "B2EventNotificationRuleForResponse{" +
157+
return "B2EventNotificationRule{" +
165158
"name='" + name + '\'' +
166159
", eventTypes=" + eventTypes +
167160
", objectNamePrefix='" + objectNamePrefix + '\'' +
@@ -176,7 +169,7 @@ public String toString() {
176169
* Rules are sorted by name first, and then additional attributes if necessary.
177170
*/
178171
@Override
179-
public int compareTo(B2EventNotificationRuleForResponse r) {
172+
public int compareTo(B2EventNotificationRule r) {
180173
return COMPARATOR.compare(this, r);
181174
}
182175
}

0 commit comments

Comments
 (0)