Skip to content

Commit a054455

Browse files
On Demand Rides and Deliveriescopybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 602423856
1 parent af34e23 commit a054455

14 files changed

+256
-3
lines changed

sample/src/main/java/com/google/fleetengine/auth/sample/LmfsConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public class LmfsConfiguration {
2525
public static final String DELIVERY_FLEET_READER_TOKEN_ACCOUNT =
2626
"<service account name>@<project id>.iam.gserviceaccount.com";
2727

28+
// Set to service account with the Fleet Engine Fleet Read SDK role.
29+
public static final String FLEET_READER_TOKEN_ACCOUNT =
30+
"<service account name>@<project id>.iam.gserviceaccount.com";
31+
2832
// Provider Id is the same as your GCP Project Id.
2933
public static final String PROVIDER_ID = "<project id>";
3034

sample/src/main/java/com/google/fleetengine/auth/sample/OdrdConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class OdrdConfiguration {
3232
public static final String DRIVER_TOKEN_ACCOUNT =
3333
"<service account name>@<project id>.iam.gserviceaccount.com";
3434

35+
// Set to service account with the Fleet Engine Fleet Read SDK role.
36+
public static final String FLEET_READER_TOKEN_ACCOUNT =
37+
"<service account name>@<project id>.iam.gserviceaccount.com";
38+
3539

3640
// Provider Id is the same as your GCP Project Id.
3741
public static final String PROVIDER_ID = "<project id>";

sample/src/main/java/com/google/fleetengine/auth/sample/ValidateLmfsRoles.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.google.fleetengine.auth.sample;
22

33
import com.google.fleetengine.auth.AuthTokenMinter;
4+
import com.google.fleetengine.auth.sample.validation.DeliveryConsumerFleetReaderTokenValidationScript;
45
import com.google.fleetengine.auth.sample.validation.DeliveryConsumerTokenValidationScript;
6+
import com.google.fleetengine.auth.sample.validation.DeliveryDriverFleetReaderTokenValidationScript;
57
import com.google.fleetengine.auth.sample.validation.DeliveryFleetReaderTokenValidationScript;
68
import com.google.fleetengine.auth.sample.validation.DeliveryServerTokenValidationScript;
79
import com.google.fleetengine.auth.sample.validation.DeliveryServerTokenValidationScript.Ids;
@@ -18,6 +20,7 @@ public class ValidateLmfsRoles {
1820
private static final String CONSUMER = "deliver consumer";
1921
private static final String UNTRUSTED_DRIVER = "untrusted driver";
2022
private static final String TRUSTED_DRIVER = "trusted driver";
23+
private static final String DELIVERY_FLEET_READER = "delivery fleet reader";
2124
private static final String FLEET_READER = "fleet reader";
2225

2326
public static void run() throws Throwable {
@@ -34,6 +37,8 @@ public static void run() throws Throwable {
3437
LmfsConfiguration.DELIVERY_UNTRUSTED_DRIVER_TOKEN_ACCOUNT))
3538
.setDeliveryFleetReaderSigner(
3639
ImpersonatedSigner.create(LmfsConfiguration.DELIVERY_FLEET_READER_TOKEN_ACCOUNT))
40+
.setFleetReaderSigner(
41+
ImpersonatedSigner.create(LmfsConfiguration.FLEET_READER_TOKEN_ACCOUNT))
3742
.setTokenFactory(
3843
new FleetEngineTokenFactory(
3944
FleetEngineTokenFactorySettings.builder()
@@ -78,11 +83,27 @@ public static void run() throws Throwable {
7883
}
7984

8085
if (configuration.getMinter().deliveryFleetReaderSigner() != null) {
81-
CommandLineRuntime.printRunScriptMessage(FLEET_READER);
86+
CommandLineRuntime.printRunScriptMessage(DELIVERY_FLEET_READER);
8287
new DeliveryFleetReaderTokenValidationScript(runtime, configuration, clientFactory)
8388
.run(ids.getDeliveryVehicleId());
8489
} else {
85-
CommandLineRuntime.printSkipScriptMessage(TRUSTED_DRIVER);
90+
CommandLineRuntime.printSkipScriptMessage(DELIVERY_FLEET_READER);
91+
}
92+
93+
if (configuration.getMinter().deliveryFleetReaderSigner() != null) {
94+
CommandLineRuntime.printRunScriptMessage(FLEET_READER);
95+
new DeliveryConsumerFleetReaderTokenValidationScript(runtime, configuration, clientFactory)
96+
.run(ids.getTrackingId());
97+
} else {
98+
CommandLineRuntime.printSkipScriptMessage(FLEET_READER);
99+
}
100+
101+
if (configuration.getMinter().deliveryFleetReaderSigner() != null) {
102+
CommandLineRuntime.printRunScriptMessage(FLEET_READER);
103+
new DeliveryDriverFleetReaderTokenValidationScript(runtime, configuration, clientFactory)
104+
.run(ids.getDeliveryVehicleId());
105+
} else {
106+
CommandLineRuntime.printSkipScriptMessage(FLEET_READER);
86107
}
87108
}
88109
}

sample/src/main/java/com/google/fleetengine/auth/sample/ValidateOdrdRoles.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.google.fleetengine.auth.AuthTokenMinter;
1818
import com.google.fleetengine.auth.sample.validation.ConsumerTokenValidationScript;
1919
import com.google.fleetengine.auth.sample.validation.DriverTokenValidationScript;
20+
import com.google.fleetengine.auth.sample.validation.ConsumerFleetReaderTokenValidationScript;
2021
import com.google.fleetengine.auth.sample.validation.SampleScriptConfiguration;
2122
import com.google.fleetengine.auth.sample.validation.SampleScriptRuntime;
2223
import com.google.fleetengine.auth.sample.validation.ServerTokenValidationScript;
@@ -30,13 +31,16 @@ final class ValidateOdrdRoles {
3031
private static final String SERVER = "server";
3132
private static final String CONSUMER = "consumer";
3233
private static final String DRIVER = "driver";
34+
private static final String FLEET_READER = "fleet reader";
3335

3436
public static void run() throws Throwable {
3537
AuthTokenMinter minter =
3638
AuthTokenMinter.builder()
3739
.setServerSigner(ImpersonatedSigner.create(OdrdConfiguration.SERVER_TOKEN_ACCOUNT))
3840
.setConsumerSigner(ImpersonatedSigner.create(OdrdConfiguration.CONSUMER_TOKEN_ACCOUNT))
3941
.setDriverSigner(ImpersonatedSigner.create(OdrdConfiguration.DRIVER_TOKEN_ACCOUNT))
42+
.setFleetReaderSigner(
43+
ImpersonatedSigner.create(OdrdConfiguration.FLEET_READER_TOKEN_ACCOUNT))
4044
.setTokenFactory(
4145
new FleetEngineTokenFactory(
4246
FleetEngineTokenFactorySettings.builder()
@@ -70,6 +74,14 @@ public static void run() throws Throwable {
7074
} else {
7175
CommandLineRuntime.printSkipScriptMessage(DRIVER);
7276
}
77+
78+
if (configuration.getMinter().fleetReaderSigner() != null) {
79+
CommandLineRuntime.printRunScriptMessage(FLEET_READER);
80+
new ConsumerFleetReaderTokenValidationScript(runtime, configuration, clientFactory)
81+
.run(ids.getTripId());
82+
} else {
83+
CommandLineRuntime.printSkipScriptMessage(FLEET_READER);
84+
}
7385
}
7486

7587
private ValidateOdrdRoles() {}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.google.fleetengine.auth.sample.validation;
2+
3+
/** Validates that fleet reader tokens provide the correct access. */
4+
public class ConsumerFleetReaderTokenValidationScript {
5+
6+
private final SampleScriptRuntime runtime;
7+
private final SampleScriptConfiguration configuration;
8+
private final CommandsFactory commandsFactory;
9+
10+
public ConsumerFleetReaderTokenValidationScript(
11+
SampleScriptRuntime runtime,
12+
SampleScriptConfiguration configuration,
13+
CommandsFactory commandsFactory) {
14+
this.runtime = runtime;
15+
this.configuration = configuration;
16+
this.commandsFactory = commandsFactory;
17+
}
18+
19+
/**
20+
* Run validation script.
21+
*
22+
* @param tripId existing trip id
23+
*/
24+
public void run(String tripId) throws Throwable {
25+
// Tokens are minted with the fleet reader role and authorized to access tripId.
26+
TripCommands tripCommands =
27+
commandsFactory.createTripCommands(
28+
configuration.getFleetEngineAddress(),
29+
configuration.getProviderId(),
30+
() -> configuration.getMinter().getFleetReaderToken());
31+
32+
VehicleCommands vehicleCommands =
33+
commandsFactory.createVehicleCommands(
34+
configuration.getFleetEngineAddress(),
35+
configuration.getProviderId(),
36+
() -> configuration.getMinter().getFleetReaderToken());
37+
38+
runtime.runCommand(
39+
"Get trip with trip id on fleet reader token", () -> tripCommands.getTrip(tripId));
40+
41+
runtime.runCommand(
42+
"Search for vehicles with fleet reader token",
43+
() -> vehicleCommands.searchVehicles(tripId));
44+
}
45+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.google.fleetengine.auth.sample.validation;
2+
3+
/** Validates that fleet reader tokens provide the correct access. */
4+
public class DeliveryConsumerFleetReaderTokenValidationScript {
5+
6+
private final SampleScriptRuntime runtime;
7+
private final SampleScriptConfiguration configuration;
8+
private final CommandsFactory commandsFactory;
9+
10+
public DeliveryConsumerFleetReaderTokenValidationScript(
11+
SampleScriptRuntime runtime,
12+
SampleScriptConfiguration configuration,
13+
CommandsFactory commandsFactory) {
14+
this.runtime = runtime;
15+
this.configuration = configuration;
16+
this.commandsFactory = commandsFactory;
17+
}
18+
19+
/**
20+
* Run validation script.
21+
*
22+
* @param trackingId existing tracking id
23+
*/
24+
public void run(String trackingId) throws Throwable {
25+
// Tokens are minted with the fleet reader role and authorized to access trackingId.
26+
DeliveryServiceCommands commands =
27+
commandsFactory.createDeliveryServiceCommands(
28+
configuration.getFleetEngineAddress(),
29+
configuration.getProviderId(),
30+
() -> configuration.getMinter().getFleetReaderToken());
31+
32+
runtime.runCommand(
33+
"Search tasks with fleet reader token", () -> commands.searchTasks(trackingId));
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.google.fleetengine.auth.sample.validation;
2+
3+
/** Validates that fleet reader tokens provide the correct access. */
4+
public class DeliveryDriverFleetReaderTokenValidationScript {
5+
6+
private final SampleScriptRuntime runtime;
7+
private final SampleScriptConfiguration configuration;
8+
private final CommandsFactory commandsFactory;
9+
10+
public DeliveryDriverFleetReaderTokenValidationScript(
11+
SampleScriptRuntime runtime,
12+
SampleScriptConfiguration configuration,
13+
CommandsFactory commandsFactory) {
14+
this.runtime = runtime;
15+
this.configuration = configuration;
16+
this.commandsFactory = commandsFactory;
17+
}
18+
19+
/**
20+
* Run validation script.
21+
*
22+
* @param deliveryVehicleId existing delivery vehicle id
23+
*/
24+
public void run(String deliveryVehicleId) throws Throwable {
25+
// Tokens are minted with the fleet reader role and authorized to access deliveryVehicleId.
26+
DeliveryServiceCommands commands =
27+
commandsFactory.createDeliveryServiceCommands(
28+
configuration.getFleetEngineAddress(),
29+
configuration.getProviderId(),
30+
() -> configuration.getMinter().getFleetReaderToken());
31+
32+
runtime.runCommand(
33+
"Get delivery vehicle with fleet reader token",
34+
() -> commands.getDeliveryVehicle(deliveryVehicleId));
35+
}
36+
}

sample/src/main/java/com/google/fleetengine/auth/sample/validation/DeliveryFleetReaderTokenValidationScript.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public void run(String trackingId) throws Throwable {
2424
() -> configuration.getMinter().getDeliveryFleetReaderToken());
2525

2626
runtime.runCommand(
27-
"Search tasks with fleet reader token", () -> commands.searchTasks(trackingId));
27+
"Search tasks with delivery fleet reader token", () -> commands.searchTasks(trackingId));
2828
}
2929
}

src/main/java/com/google/fleetengine/auth/AuthTokenMinter.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public abstract class AuthTokenMinter implements FleetEngineTokenProvider {
9191
@Nullable
9292
public abstract Signer deliveryFleetReaderSigner();
9393

94+
/** Signer responsible for signing JWTs with a fleet reader key. */
95+
@Nullable
96+
public abstract Signer fleetReaderSigner();
97+
9498
/** Signer responsible for signing JWTs with that aren't tied to the standard role set. */
9599
@Nullable
96100
public abstract Signer customSigner();
@@ -345,6 +349,22 @@ public FleetEngineToken getDeliveryFleetReaderToken() throws SigningTokenExcepti
345349
return tokenStateManager().signToken(deliveryFleetReaderSigner(), unsignedToken);
346350
}
347351

352+
/**
353+
* Returns a non expired Fleet Engine Token that was signed with the fleet reader signer.
354+
*
355+
* <p>Tokens will have an expiration of at least {@link
356+
* FleetEngineAuthTokenStateManager#EXPIRATION_WINDOW_DURATION}.
357+
*
358+
* @throws SigningTokenException if the fleet reader server signer was not set, or if there is an
359+
* issue while signing the token.
360+
* @return Fleet Engine token with the "Fleet Reader" role, guaranteed to be valid for {@link
361+
* FleetEngineAuthTokenStateManager#EXPIRATION_WINDOW_DURATION} minutes.
362+
*/
363+
public FleetEngineToken getFleetReaderToken() throws SigningTokenException {
364+
FleetEngineToken unsignedToken = tokenFactory().createFleetReaderToken();
365+
return tokenStateManager().signToken(fleetReaderSigner(), unsignedToken);
366+
}
367+
348368
/**
349369
* Returns a non expired Fleet Engine Token that was signed with the custom signer
350370
* and authorized for use with entities matching the specified claim.
@@ -411,6 +431,9 @@ public abstract static class Builder {
411431
/** Sets the signer responsible for signing delivery fleet JWTs. */
412432
public abstract Builder setDeliveryFleetReaderSigner(Signer deliveryFleetReaderSigner);
413433

434+
/** Sets the signer responsible for signing fleet reader JWTs. */
435+
public abstract Builder setFleetReaderSigner(Signer fleetReaderSigner);
436+
414437
/**
415438
* Sets token factory that creates unsigned tokens.
416439
*

src/main/java/com/google/fleetengine/auth/token/FleetEngineTokenType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public enum FleetEngineTokenType {
6464
*/
6565
DELIVERY_FLEET_READER,
6666

67+
/**
68+
* Fleet reader tokens are usually service accounts associated with the Fleet Engine Fleet Reader
69+
* User role on the Google Cloud project.
70+
*/
71+
FLEET_READER,
72+
6773
/** Custom token type associated with any Fleet Engine Role on the Google Cloud project. */
6874
CUSTOM,
6975
}

0 commit comments

Comments
 (0)