Skip to content

Commit

Permalink
chore(protocol)!: Introduce Shield name (#158)
Browse files Browse the repository at this point in the history
* chore(protocol): Introduce Shield name

* Change suspicious to shield
  • Loading branch information
davidmytton authored Feb 2, 2024
1 parent c7dbdba commit 311713b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 55 deletions.
18 changes: 9 additions & 9 deletions protocol/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ArcjetErrorReason,
ArcjetRateLimitReason,
ArcjetRuleResult,
ArcjetSuspiciousReason,
ArcjetShieldReason,
ArcjetBotType,
ArcjetConclusion,
ArcjetDecision,
Expand Down Expand Up @@ -40,7 +40,7 @@ import {
RuleResult,
RuleState,
SDKStack,
SuspiciousReason,
ShieldReason,
} from "./gen/es/decide/v1alpha1/decide_pb.js";

export function ArcjetModeToProtocol(mode: ArcjetMode) {
Expand Down Expand Up @@ -256,10 +256,10 @@ export function ArcjetReasonFromProtocol(proto?: Reason) {
case "edgeRule": {
return new ArcjetEdgeRuleReason();
}
case "suspicious": {
case "shield": {
const reason = proto.reason.value;
return new ArcjetSuspiciousReason({
wafTriggered: reason.wafTriggered,
return new ArcjetShieldReason({
shieldTriggered: reason.shieldTriggered,
});
}
case "email": {
Expand Down Expand Up @@ -326,12 +326,12 @@ export function ArcjetReasonToProtocol(reason: ArcjetReason): Reason {
});
}

if (reason.isSuspicious()) {
if (reason.isShield()) {
return new Reason({
reason: {
case: "suspicious",
value: new SuspiciousReason({
wafTriggered: reason.wafTriggered,
case: "shield",
value: new ShieldReason({
shieldTriggered: reason.shieldTriggered,
}),
},
});
Expand Down
2 changes: 1 addition & 1 deletion protocol/gen/es/decide/v1alpha1/decide_connect.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protoc-gen-connect-es v1.1.3
// @generated by protoc-gen-connect-es v1.3.0
// @generated from file decide/v1alpha1/decide.proto (package proto.decide.v1alpha1, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down
2 changes: 1 addition & 1 deletion protocol/gen/es/decide/v1alpha1/decide_connect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protoc-gen-connect-es v1.1.3
// @generated by protoc-gen-connect-es v1.3.0
// @generated from file decide/v1alpha1/decide.proto (package proto.decide.v1alpha1, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down
45 changes: 26 additions & 19 deletions protocol/gen/es/decide/v1alpha1/decide_pb.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protoc-gen-es v1.4.2
// @generated by protoc-gen-es v1.7.1
// @generated from file decide/v1alpha1/decide.proto (package proto.decide.v1alpha1, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down Expand Up @@ -327,13 +327,12 @@ export declare class Reason extends Message<Reason> {
case: "bot";
} | {
/**
* Contains details about why the request was considered suspicious
* when the decision was based on a WAF rule.
* Contains details about why Arcjet Shield was triggered.
*
* @generated from field: proto.decide.v1alpha1.SuspiciousReason suspicious = 4;
* @generated from field: proto.decide.v1alpha1.ShieldReason shield = 4;
*/
value: SuspiciousReason;
case: "suspicious";
value: ShieldReason;
case: "shield";
} | {
/**
* Contains details about the email when the decision was made based
Expand Down Expand Up @@ -518,33 +517,41 @@ export declare class BotReason extends Message<BotReason> {
}

/**
* Details of why we consider the request suspicious.
* Details of an Arcjet Shield decision.
*
* @generated from message proto.decide.v1alpha1.SuspiciousReason
* @generated from message proto.decide.v1alpha1.ShieldReason
*/
export declare class SuspiciousReason extends Message<SuspiciousReason> {
export declare class ShieldReason extends Message<ShieldReason> {
/**
* Whether the WAF was triggered. Log into the Arcjet dashboard and search
* for the decision ID to find more details about the WAF rules which were
* Whether Arcjet Shield was triggered. Log into the Arcjet dashboard and
* search for the decision ID to find more details about which rules were
* triggered.
*
* @generated from field: bool waf_triggered = 1;
* @generated from field: bool shield_triggered = 1;
*/
wafTriggered: boolean;
shieldTriggered: boolean;

constructor(data?: PartialMessage<SuspiciousReason>);
/**
* Whether the request was considered suspicious based on background
* analysis of the request
*
* @generated from field: bool suspicious = 2;
*/
suspicious: boolean;

constructor(data?: PartialMessage<ShieldReason>);

static readonly runtime: typeof proto3;
static readonly typeName = "proto.decide.v1alpha1.SuspiciousReason";
static readonly typeName = "proto.decide.v1alpha1.ShieldReason";
static readonly fields: FieldList;

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): SuspiciousReason;
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): ShieldReason;

static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): SuspiciousReason;
static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): ShieldReason;

static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): SuspiciousReason;
static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): ShieldReason;

static equals(a: SuspiciousReason | PlainMessage<SuspiciousReason> | undefined, b: SuspiciousReason | PlainMessage<SuspiciousReason> | undefined): boolean;
static equals(a: ShieldReason | PlainMessage<ShieldReason> | undefined, b: ShieldReason | PlainMessage<ShieldReason> | undefined): boolean;
}

/**
Expand Down
15 changes: 8 additions & 7 deletions protocol/gen/es/decide/v1alpha1/decide_pb.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// @generated by protoc-gen-es v1.4.2
// @generated by protoc-gen-es v1.7.1
// @generated from file decide/v1alpha1/decide.proto (package proto.decide.v1alpha1, syntax proto3)
/* eslint-disable */
// @ts-nocheck
Expand Down Expand Up @@ -119,7 +119,7 @@ export const Reason = proto3.makeMessageType(
{ no: 1, name: "rate_limit", kind: "message", T: RateLimitReason, oneof: "reason" },
{ no: 2, name: "edge_rule", kind: "message", T: EdgeRuleReason, oneof: "reason" },
{ no: 3, name: "bot", kind: "message", T: BotReason, oneof: "reason" },
{ no: 4, name: "suspicious", kind: "message", T: SuspiciousReason, oneof: "reason" },
{ no: 4, name: "shield", kind: "message", T: ShieldReason, oneof: "reason" },
{ no: 5, name: "email", kind: "message", T: EmailReason, oneof: "reason" },
{ no: 6, name: "error", kind: "message", T: ErrorReason, oneof: "reason" },
],
Expand Down Expand Up @@ -170,14 +170,15 @@ export const BotReason = proto3.makeMessageType(
);

/**
* Details of why we consider the request suspicious.
* Details of an Arcjet Shield decision.
*
* @generated from message proto.decide.v1alpha1.SuspiciousReason
* @generated from message proto.decide.v1alpha1.ShieldReason
*/
export const SuspiciousReason = proto3.makeMessageType(
"proto.decide.v1alpha1.SuspiciousReason",
export const ShieldReason = proto3.makeMessageType(
"proto.decide.v1alpha1.ShieldReason",
() => [
{ no: 1, name: "waf_triggered", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
{ no: 1, name: "shield_triggered", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
{ no: 2, name: "suspicious", kind: "scalar", T: 8 /* ScalarType.BOOL */ },
],
);

Expand Down
18 changes: 9 additions & 9 deletions protocol/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const ArcjetRuleType: ArcjetEnum<ArcjetRuleType> = Object.freeze({
});

export class ArcjetReason {
type?: "RATE_LIMIT" | "BOT" | "EDGE_RULE" | "SUSPICIOUS" | "EMAIL" | "ERROR";
type?: "RATE_LIMIT" | "BOT" | "EDGE_RULE" | "SHIELD" | "EMAIL" | "ERROR";

isRateLimit(): this is ArcjetRateLimitReason {
return this.type === "RATE_LIMIT";
Expand All @@ -81,8 +81,8 @@ export class ArcjetReason {
return this.type === "EDGE_RULE";
}

isSuspicious(): this is ArcjetSuspiciousReason {
return this.type === "SUSPICIOUS";
isShield(): this is ArcjetShieldReason {
return this.type === "SHIELD";
}

isEmail(): this is ArcjetEmailReason {
Expand Down Expand Up @@ -156,15 +156,15 @@ export class ArcjetEdgeRuleReason extends ArcjetReason {
type: "EDGE_RULE" = "EDGE_RULE";
}

export class ArcjetSuspiciousReason extends ArcjetReason {
type: "SUSPICIOUS" = "SUSPICIOUS";
export class ArcjetShieldReason extends ArcjetReason {
type: "SHIELD" = "SHIELD";

wafTriggered: boolean;
shieldTriggered: boolean;

constructor(init: { wafTriggered?: boolean }) {
constructor(init: { shieldTriggered?: boolean }) {
super();

this.wafTriggered = init.wafTriggered ?? false;
this.shieldTriggered = init.shieldTriggered ?? false;
}
}

Expand Down Expand Up @@ -256,7 +256,7 @@ export class ArcjetRuleResult {
* one of `"ALLOW"`, `"DENY"`, `"CHALLENGE"`, or `"ERROR"`.
* @property `reason` - A structured data type about the reason for the
* decision. One of: {@link ArcjetRateLimitReason}, {@link ArcjetEdgeRuleReason},
* {@link ArcjetBotReason}, {@link ArcjetSuspiciousReason},
* {@link ArcjetBotReason}, {@link ArcjetShieldReason},
* {@link ArcjetEmailReason}, or {@link ArcjetErrorReason}.
* @property `ttl` - The duration in milliseconds this decision should be
* considered valid, also known as time-to-live.
Expand Down
16 changes: 7 additions & 9 deletions protocol/test/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
ArcjetRateLimitReason,
ArcjetReason,
ArcjetRuleResult,
ArcjetSuspiciousReason,
ArcjetShieldReason,
} from "../index.js";
import { Timestamp } from "@bufbuild/protobuf";

Expand Down Expand Up @@ -315,14 +315,14 @@ describe("convert", () => {
ArcjetReasonFromProtocol(
new Reason({
reason: {
case: "suspicious",
case: "shield",
value: {
wafTriggered: true,
shieldTriggered: true,
},
},
}),
),
).toBeInstanceOf(ArcjetSuspiciousReason);
).toBeInstanceOf(ArcjetShieldReason);
expect(ArcjetReasonFromProtocol(new Reason())).toBeInstanceOf(ArcjetReason);
expect(
ArcjetReasonFromProtocol(
Expand Down Expand Up @@ -432,15 +432,13 @@ describe("convert", () => {
}),
);
expect(
ArcjetReasonToProtocol(
new ArcjetSuspiciousReason({ wafTriggered: true }),
),
ArcjetReasonToProtocol(new ArcjetShieldReason({ shieldTriggered: true })),
).toEqual(
new Reason({
reason: {
case: "suspicious",
case: "shield",
value: {
wafTriggered: true,
shieldTriggered: true,
},
},
}),
Expand Down

0 comments on commit 311713b

Please sign in to comment.