mirror of
https://github.com/floci-io/floci.git
synced 2026-11-30 17:05:21 -05:00
feat(iam): publish delegation request notifications to the partner's topic (#6199)
IAM temporary delegation had no way out. The exchange token is answered by no operation on the family, so a partner that filed a request could not obtain what it exchanges, and NotificationChannel was stored with nothing reading it. DelegationNotifier publishes the two notifications the user guide documents to the SNS topic the request names, which is the channel AWS gives this feature. Every state the guide's notification table lists notifies: ASSIGNED, PENDING_APPROVAL, ACCEPTED, FINALIZED, REJECTED and EXPIRED, leaving only the UNASSIGNED a request starts in, which the partner filed itself. FINALIZED sends two, a StateChange and the ExchangeToken notification carrying the token and an ExpiresAt of release plus SessionDuration. The payload is the guide's own field list, a JSON string in the Message of an ordinary SNS notification, with the owner account left out until the request carries one, which a request filed at a named customer does from the start. A notice is taken wherever a state is written rather than at each call site, so an expiry found on a read announces itself the way a claim does, and a call that then fails on the expired request still sends it. What notifies is a transition: a second UpdateDelegationRequest on a request already under review writes its notes and tells the partner nothing. NotificationChannel is held to more than its declared shape, which is looser than an ARN: the member's own documentation says only SNS topic ARNs are accepted, so a value naming no topic is refused on the create with InvalidInput rather than accepted into a lifecycle nobody would hear about. IAM publishes as iam.amazonaws.com through ServiceCallAuthorizer, so the topic's own policy decides and nothing the caller holds plays any part, which is the statement the guide has partners add to their topic. The request's own ARN and the account that filed it go as aws:SourceArn and aws:SourceAccount. Both the policy and the topic are read in the account the topic ARN names: the topic is the partner's and most of these transitions are the customer's calls, so account aware storage would otherwise look in the caller's account and find no topic at all. Notifications go out once the delegation lock is released, from a snapshot taken while the transition still owns the record, a stored request being a mutable instance every caller holding it shares. A delivery that fails costs the notification and not the transition behind it.
This commit is contained in:
1 parent
2f57441b84
commit
8427c0b71d
8 files changed
+940
-80
No files matched your search
@@ -0,0 +1,195 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
import io.github.hectorvent.floci.core.common.ServiceCallAuthorizer;
|
||||
import io.github.hectorvent.floci.services.iam.model.DelegationRequest;
|
||||
import io.github.hectorvent.floci.services.sns.SnsService;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The notifications IAM publishes to a partner's SNS topic as a delegation request moves.
|
||||
*
|
||||
* <p>This is the only way a partner learns anything after filing a request: no operation on the
|
||||
* family answers the exchange token, so the token reaches its owner here or not at all. The guide
|
||||
* says as much, "IAM uses Amazon Simple Notification Service (SNS) to communicate delegation
|
||||
* request state changes to you", and lists the states that notify. Every transition out of
|
||||
* {@code UNASSIGNED} is one of them, which is why a notice is taken wherever a state is written
|
||||
* rather than at the six call sites that write one.
|
||||
*
|
||||
* <p>Both shapes are published in full. Every notification carries {@code Type}, {@code RequestId},
|
||||
* {@code RequestorWorkflowId}, {@code State}, {@code OwnerAccountId} and {@code UpdatedAt}, and the
|
||||
* one carrying the token adds {@code ExchangeToken} and {@code ExpiresAt}, which is the release
|
||||
* instant plus {@code SessionDuration}: "Credentials expire at: release time + approved duration".
|
||||
* The delegation fields are a JSON string in the {@code Message} of an ordinary SNS notification,
|
||||
* which is the envelope {@link SnsService#publish} already writes.
|
||||
*
|
||||
* <p>IAM publishes as a service rather than as the caller, so the topic's own policy decides and
|
||||
* nothing the customer or the partner holds plays any part. It reads both of them in the account
|
||||
* the topic ARN names, which is the partner's, where most of the calls that trigger one are the
|
||||
* customer's. The guide has the partner allow
|
||||
* {@code SNS:Publish} to {@code iam.amazonaws.com} on the topic and warns that without it "you
|
||||
* will not receive state change notifications or the exchange token", so a refusal here costs the
|
||||
* notification and not the operation that produced it: the state has already changed, and AWS
|
||||
* rolls nothing back for a notification it could not deliver.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class DelegationNotifier {
|
||||
|
||||
static final String STATE_CHANGE = "StateChange";
|
||||
static final String EXCHANGE_TOKEN = "ExchangeToken";
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(DelegationNotifier.class);
|
||||
/** ISO 8601 to the millisecond, the form both published examples carry. */
|
||||
private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter
|
||||
.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT).withZone(ZoneOffset.UTC);
|
||||
|
||||
private final SnsService snsService;
|
||||
private final ServiceCallAuthorizer serviceCallAuthorizer;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public DelegationNotifier(SnsService snsService, ServiceCallAuthorizer serviceCallAuthorizer,
|
||||
ObjectMapper objectMapper) {
|
||||
this.snsService = snsService;
|
||||
this.serviceCallAuthorizer = serviceCallAuthorizer;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* One notification, as the transition that produced it left the request.
|
||||
*
|
||||
* <p>A stored request is a mutable record shared by every caller holding it, and these are
|
||||
* published after the service's lock is released, so what goes out is copied here while the
|
||||
* transition still owns the record. Publishing from the record itself would send whatever state
|
||||
* a later caller had moved it to.
|
||||
*/
|
||||
public record Notice(String type, String requestId, String requestorWorkflowId, String state,
|
||||
String ownerAccountId, Instant updatedAt, String exchangeToken,
|
||||
Instant expiresAt, String topicArn, String requestorAccountId) {
|
||||
|
||||
/** The notification for a state the request has just moved to. */
|
||||
public static Notice stateChange(DelegationRequest request) {
|
||||
return new Notice(STATE_CHANGE, request.getDelegationRequestId(),
|
||||
request.getRequestorWorkflowId(), String.valueOf(request.getState()),
|
||||
request.getOwnerAccountId(), request.getUpdatedTime(), null, null,
|
||||
request.getNotificationChannel(), request.getRequestorId());
|
||||
}
|
||||
|
||||
/**
|
||||
* The notification that carries the token, sent at {@code FINALIZED} beside the state
|
||||
* change rather than instead of it: the guide's table lists both for that state.
|
||||
*/
|
||||
public static Notice exchangeToken(DelegationRequest request) {
|
||||
return new Notice(EXCHANGE_TOKEN, request.getDelegationRequestId(),
|
||||
request.getRequestorWorkflowId(), String.valueOf(request.getState()),
|
||||
request.getOwnerAccountId(), request.getUpdatedTime(),
|
||||
request.getExchangeToken(),
|
||||
request.getTokenReleasedAt().plusSeconds(request.getSessionDuration()),
|
||||
request.getNotificationChannel(), request.getRequestorId());
|
||||
}
|
||||
}
|
||||
|
||||
/** Publishes each notice, in the order the transitions produced them. */
|
||||
public void publish(List<Notice> notices) {
|
||||
for (Notice notice : notices) {
|
||||
try {
|
||||
publish(notice);
|
||||
} catch (Exception e) {
|
||||
// Nothing a notification does may reach the caller. The transition behind it is
|
||||
// already written and AWS rolls none back for a message it could not deliver, so a
|
||||
// topic that is gone, a policy that cannot be read and a failed delivery all cost
|
||||
// the notification alone, and the notices after this one are still owed theirs.
|
||||
// These are published from a finally, where an escape would also replace whatever
|
||||
// the call was already answering.
|
||||
LOG.warnv(e, "Delegation request {0}: {1} to topic {2} not published",
|
||||
notice.requestId(), notice.type(), notice.topicArn());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a {@code NotificationChannel} can name a topic at all. The member's own shape,
|
||||
* {@code notificationChannelType}, is looser than an ARN, so this is what
|
||||
* {@code CreateDelegationRequest} holds the value to and what a publish checks before trying.
|
||||
*/
|
||||
static boolean namesATopic(String channel) {
|
||||
return AwsArnUtils.isArnFor(channel, "sns") && !AwsArnUtils.parse(channel).resource().isBlank();
|
||||
}
|
||||
|
||||
private void publish(Notice notice) throws Exception {
|
||||
String topicArn = notice.topicArn();
|
||||
if (!namesATopic(topicArn)) {
|
||||
// The create refuses a channel that cannot name a topic, so reaching this is a request
|
||||
// stored before it did. The failure AWS describes for a topic it cannot publish to is
|
||||
// the same one either way: the partner does not receive the notification.
|
||||
LOG.debugv("Delegation request {0}: {1} not published, NotificationChannel {2} is not "
|
||||
+ "an SNS topic", notice.requestId(), notice.type(), topicArn);
|
||||
return;
|
||||
}
|
||||
// The topic belongs to the partner and most of these transitions are the customer's calls,
|
||||
// so both the policy and the topic are read in the account the topic ARN names. Account
|
||||
// aware storage reads the calling account otherwise, where neither one exists.
|
||||
AwsArnUtils.Arn topic = AwsArnUtils.parse(topicArn);
|
||||
RequestScopes.callAsChecked(topic.accountId(), () -> {
|
||||
publishToTopic(notice, topic.region());
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private void publishToTopic(Notice notice, String region) throws JsonProcessingException {
|
||||
String topicArn = notice.topicArn();
|
||||
if (serviceCallAuthorizer.authorize("iam", "sns:Publish", topicArn, sourceArn(notice),
|
||||
notice.requestorAccountId()) != IamPolicyEvaluator.Decision.ALLOW) {
|
||||
return;
|
||||
}
|
||||
snsService.publish(topicArn, null, message(notice), null, region);
|
||||
}
|
||||
|
||||
/**
|
||||
* The request whose own lifecycle triggered the publish, which is what {@code aws:SourceArn}
|
||||
* and {@code aws:SourceAccount} name. AWS publishes a topic policy for this with no condition
|
||||
* on either, so nothing here turns on them; they are set because a topic owner writing the
|
||||
* confused-deputy conditions the SNS guide recommends is testing for exactly this request, in
|
||||
* the partner account that filed it.
|
||||
*/
|
||||
private static String sourceArn(Notice notice) {
|
||||
return AwsArnUtils.Arn.global(AwsArnUtils.parse(notice.topicArn()).partition(), "iam",
|
||||
notice.requestorAccountId(), "delegation-request/" + notice.requestId()).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* The delegation payload. Field order follows the guide's two field tables, the published
|
||||
* examples ordering their keys differently from each other. An account nobody has claimed is
|
||||
* left out rather than sent as null, which is how the rest of IAM answers a member it has no
|
||||
* value for.
|
||||
*/
|
||||
private String message(Notice notice) throws JsonProcessingException {
|
||||
Map<String, Object> payload = new LinkedHashMap<>();
|
||||
payload.put("Type", notice.type());
|
||||
payload.put("RequestId", notice.requestId());
|
||||
payload.put("RequestorWorkflowId", notice.requestorWorkflowId());
|
||||
payload.put("State", notice.state());
|
||||
// "Customer's AWS account ID", which a request filed at a named customer carries from
|
||||
// the start and an open one only once it is claimed. Until then there is none to send.
|
||||
if (notice.ownerAccountId() != null) {
|
||||
payload.put("OwnerAccountId", notice.ownerAccountId());
|
||||
}
|
||||
payload.put("UpdatedAt", TIMESTAMP_FORMAT.format(notice.updatedAt()));
|
||||
if (EXCHANGE_TOKEN.equals(notice.type())) {
|
||||
payload.put("ExchangeToken", notice.exchangeToken());
|
||||
payload.put("ExpiresAt", TIMESTAMP_FORMAT.format(notice.expiresAt()));
|
||||
}
|
||||
return objectMapper.writeValueAsString(payload);
|
||||
}
|
||||
}
|
||||
+138
-58
@@ -17,6 +17,7 @@ import jakarta.inject.Inject;
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Clock;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
@@ -42,7 +43,17 @@ import java.util.regex.Pattern;
|
||||
* {@code ValidationError}, which the reference's common errors publish for exactly that across
|
||||
* every IAM operation and which the rest of IAM answers in the same shape. The modeled
|
||||
* {@code InvalidInput} is kept for a value that cannot be read as the member's type at all, the
|
||||
* way the shared pagination already answers a {@code MaxItems} that is not a number.
|
||||
* way the shared pagination already answers a {@code MaxItems} that is not a number, and that is
|
||||
* what a {@code NotificationChannel} naming no topic gets: the member's declared shape is looser
|
||||
* than an ARN, so the shape alone would let through a channel nothing could be published to.
|
||||
*
|
||||
* <p>Every state the service writes is announced on the partner's topic through
|
||||
* {@link DelegationNotifier}, which is the only channel this family has outwards. A notice is
|
||||
* taken wherever a state is written, so an expiry noticed on a read announces itself the same way
|
||||
* a claim does, and they are published once the lock is released: delivery reaches SNS and its
|
||||
* subscribers, which is not work to hold a lock over, and a notification that cannot be delivered
|
||||
* must not undo the transition that earned it. A call that fails after expiring a request still
|
||||
* announces the expiry, which is why they are published in a finally rather than on the way out.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class DelegationRequestService {
|
||||
@@ -88,6 +99,7 @@ public class DelegationRequestService {
|
||||
private final SecureRandom random = new SecureRandom();
|
||||
private final AccountAwareStorageBackend<DelegationRequest> requests;
|
||||
private final Clock clock;
|
||||
private final DelegationNotifier notifier;
|
||||
/**
|
||||
* Serializes the read, check and write every lifecycle call makes, the way the sibling IAM
|
||||
* services guard theirs. Without it two callers can both claim the same unassigned request, or
|
||||
@@ -96,13 +108,17 @@ public class DelegationRequestService {
|
||||
private final Object requestLock = new Object();
|
||||
|
||||
@Inject
|
||||
public DelegationRequestService(StorageFactory storageFactory, Clock clock) {
|
||||
this(storageFactory.create("iam", "iam-delegation-requests.json", new TypeReference<>() {}), clock);
|
||||
public DelegationRequestService(StorageFactory storageFactory, Clock clock,
|
||||
DelegationNotifier notifier) {
|
||||
this(storageFactory.create("iam", "iam-delegation-requests.json", new TypeReference<>() {}),
|
||||
clock, notifier);
|
||||
}
|
||||
|
||||
DelegationRequestService(AccountAwareStorageBackend<DelegationRequest> requests, Clock clock) {
|
||||
DelegationRequestService(AccountAwareStorageBackend<DelegationRequest> requests, Clock clock,
|
||||
DelegationNotifier notifier) {
|
||||
this.requests = requests;
|
||||
this.clock = clock;
|
||||
this.notifier = notifier;
|
||||
}
|
||||
|
||||
/** The {@code CreateDelegationRequest} request, as the handler parsed it. */
|
||||
@@ -159,6 +175,15 @@ public class DelegationRequestService {
|
||||
throw constraintViolation(command.ownerAccountId(), "ownerAccountId",
|
||||
"Member must satisfy regular expression pattern: " + ACCOUNT_PATTERN.pattern());
|
||||
}
|
||||
// Last, because every check above enforces a declared constraint and this one enforces a
|
||||
// documented one: notificationChannelType is looser than an ARN, while the member's own
|
||||
// documentation says "At this time, only SNS topic ARNs are accepted for notification".
|
||||
// A value inside the shape that names no topic cannot be read as the member's type, which
|
||||
// is what InvalidInput is for here.
|
||||
if (!DelegationNotifier.namesATopic(command.notificationChannel())) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"NotificationChannel must be an Amazon SNS topic ARN.", 400);
|
||||
}
|
||||
synchronized (requestLock) {
|
||||
return fileNewRequest(requestorAccountId, region, command);
|
||||
}
|
||||
@@ -208,24 +233,35 @@ public class DelegationRequestService {
|
||||
* another account answers {@code NoSuchEntity} rather than a refusal that names it.
|
||||
*/
|
||||
public DelegationRequest associate(String callerAccountId, String callerArn, String id) {
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now);
|
||||
requireState(request, DelegationRequestState.UNASSIGNED);
|
||||
request.setOwnerAccountId(callerAccountId);
|
||||
request.setOwnerId(callerArn);
|
||||
return moveTo(request, DelegationRequestState.ASSIGNED, now);
|
||||
List<DelegationNotifier.Notice> notices = new ArrayList<>();
|
||||
try {
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now, notices);
|
||||
requireState(request, DelegationRequestState.UNASSIGNED);
|
||||
request.setOwnerAccountId(callerAccountId);
|
||||
request.setOwnerId(callerArn);
|
||||
return moveTo(request, DelegationRequestState.ASSIGNED, now, notices);
|
||||
}
|
||||
} finally {
|
||||
notifier.publish(notices);
|
||||
}
|
||||
}
|
||||
|
||||
/** Approves a request, from {@code ASSIGNED} or from an administrator's review. */
|
||||
public DelegationRequest accept(String callerAccountId, String callerArn, String id) {
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now);
|
||||
requireState(request, DelegationRequestState.ASSIGNED, DelegationRequestState.PENDING_APPROVAL);
|
||||
request.setApproverId(callerArn);
|
||||
return moveTo(request, DelegationRequestState.ACCEPTED, now);
|
||||
List<DelegationNotifier.Notice> notices = new ArrayList<>();
|
||||
try {
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now, notices);
|
||||
requireState(request, DelegationRequestState.ASSIGNED,
|
||||
DelegationRequestState.PENDING_APPROVAL);
|
||||
request.setApproverId(callerArn);
|
||||
return moveTo(request, DelegationRequestState.ACCEPTED, now, notices);
|
||||
}
|
||||
} finally {
|
||||
notifier.publish(notices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,13 +271,18 @@ public class DelegationRequestService {
|
||||
*/
|
||||
public DelegationRequest reject(String callerAccountId, String id, String notes) {
|
||||
checkText(notes, "notes", 0, 500, TEXT_PATTERN);
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now);
|
||||
requireState(request, DelegationRequestState.ASSIGNED,
|
||||
DelegationRequestState.PENDING_APPROVAL, DelegationRequestState.ACCEPTED);
|
||||
request.setRejectionReason(notes);
|
||||
return moveTo(request, DelegationRequestState.REJECTED, now);
|
||||
List<DelegationNotifier.Notice> notices = new ArrayList<>();
|
||||
try {
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now, notices);
|
||||
requireState(request, DelegationRequestState.ASSIGNED,
|
||||
DelegationRequestState.PENDING_APPROVAL, DelegationRequestState.ACCEPTED);
|
||||
request.setRejectionReason(notes);
|
||||
return moveTo(request, DelegationRequestState.REJECTED, now, notices);
|
||||
}
|
||||
} finally {
|
||||
notifier.publish(notices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,12 +293,18 @@ public class DelegationRequestService {
|
||||
*/
|
||||
public DelegationRequest update(String callerAccountId, String id, String notes) {
|
||||
checkText(notes, "notes", 0, 500, TEXT_PATTERN);
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now);
|
||||
requireState(request, DelegationRequestState.ASSIGNED, DelegationRequestState.PENDING_APPROVAL);
|
||||
request.setNotes(notes);
|
||||
return moveTo(request, DelegationRequestState.PENDING_APPROVAL, now);
|
||||
List<DelegationNotifier.Notice> notices = new ArrayList<>();
|
||||
try {
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now, notices);
|
||||
requireState(request, DelegationRequestState.ASSIGNED,
|
||||
DelegationRequestState.PENDING_APPROVAL);
|
||||
request.setNotes(notes);
|
||||
return moveTo(request, DelegationRequestState.PENDING_APPROVAL, now, notices);
|
||||
}
|
||||
} finally {
|
||||
notifier.publish(notices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,24 +325,32 @@ public class DelegationRequestService {
|
||||
* who accepted the request is a different ARN from the owner who claimed it, and with the flag
|
||||
* set only the second of them may release.
|
||||
*
|
||||
* <p>Nothing hands the token back. AWS publishes it only in the {@code ExchangeToken}
|
||||
* notification on the partner's SNS topic, and no operation on the family answers it, so it
|
||||
* stays on the record until there is a notification to carry it.
|
||||
* <p>No operation on the family answers the token. AWS publishes it only in the
|
||||
* {@code ExchangeToken} notification on the partner's topic, which is sent here beside the
|
||||
* state change rather than instead of it, the guide listing both for {@code FINALIZED}.
|
||||
*/
|
||||
public DelegationRequest release(String callerAccountId, String callerArn, String id) {
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now);
|
||||
requireState(request, DelegationRequestState.ACCEPTED);
|
||||
if (Boolean.TRUE.equals(request.getOnlySendByOwner())
|
||||
&& !request.getOwnerId().equals(callerArn)) {
|
||||
throw new AwsException("AccessDenied", "User is not authorized to perform: "
|
||||
+ "iam:SendDelegationToken on " + request.getDelegationRequestId()
|
||||
+ ", which is marked to be sent only by its owner.", 403);
|
||||
List<DelegationNotifier.Notice> notices = new ArrayList<>();
|
||||
try {
|
||||
synchronized (requestLock) {
|
||||
Instant now = clock.instant();
|
||||
DelegationRequest request = visible(callerAccountId, id, now, notices);
|
||||
requireState(request, DelegationRequestState.ACCEPTED);
|
||||
if (Boolean.TRUE.equals(request.getOnlySendByOwner())
|
||||
&& !request.getOwnerId().equals(callerArn)) {
|
||||
throw new AwsException("AccessDenied", "User is not authorized to perform: "
|
||||
+ "iam:SendDelegationToken on " + request.getDelegationRequestId()
|
||||
+ ", which is marked to be sent only by its owner.", 403);
|
||||
}
|
||||
request.setExchangeToken(newToken());
|
||||
request.setTokenReleasedAt(now);
|
||||
DelegationRequest finalized =
|
||||
moveTo(request, DelegationRequestState.FINALIZED, now, notices);
|
||||
notices.add(DelegationNotifier.Notice.exchangeToken(finalized));
|
||||
return finalized;
|
||||
}
|
||||
request.setExchangeToken(newToken());
|
||||
request.setTokenReleasedAt(now);
|
||||
return moveTo(request, DelegationRequestState.FINALIZED, now);
|
||||
} finally {
|
||||
notifier.publish(notices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -345,8 +400,13 @@ public class DelegationRequestService {
|
||||
|
||||
/** One request, by id, as the calling account is allowed to see it. */
|
||||
public DelegationRequest get(String callerAccountId, String id) {
|
||||
synchronized (requestLock) {
|
||||
return visible(callerAccountId, id, clock.instant());
|
||||
List<DelegationNotifier.Notice> notices = new ArrayList<>();
|
||||
try {
|
||||
synchronized (requestLock) {
|
||||
return visible(callerAccountId, id, clock.instant(), notices);
|
||||
}
|
||||
} finally {
|
||||
notifier.publish(notices);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -362,14 +422,21 @@ public class DelegationRequestService {
|
||||
public List<DelegationRequest> list(String callerAccountId, String ownerId) {
|
||||
checkText(ownerId, "ownerId", 20, 2048, OWNER_ID_PATTERN);
|
||||
Instant now = clock.instant();
|
||||
synchronized (requestLock) {
|
||||
return visibleToOwner(callerAccountId, ownerId, now);
|
||||
List<DelegationNotifier.Notice> notices = new ArrayList<>();
|
||||
try {
|
||||
synchronized (requestLock) {
|
||||
return visibleToOwner(callerAccountId, ownerId, now, notices);
|
||||
}
|
||||
} finally {
|
||||
notifier.publish(notices);
|
||||
}
|
||||
}
|
||||
|
||||
private List<DelegationRequest> visibleToOwner(String callerAccountId, String ownerId, Instant now) {
|
||||
private List<DelegationRequest> visibleToOwner(String callerAccountId, String ownerId,
|
||||
Instant now,
|
||||
List<DelegationNotifier.Notice> notices) {
|
||||
return requests.scanAllAccounts().stream()
|
||||
.map(request -> expireIfDue(request, now))
|
||||
.map(request -> expireIfDue(request, now, notices))
|
||||
.filter(request -> request.getOwnerId() != null
|
||||
&& callerAccountId.equals(request.getOwnerAccountId()))
|
||||
.filter(request -> ownerId == null || ownerId.equals(request.getOwnerId()))
|
||||
@@ -389,7 +456,8 @@ public class DelegationRequestService {
|
||||
* being no per-identity permission on a delegation request to consult, so the last two collapse
|
||||
* into the same answer.
|
||||
*/
|
||||
private DelegationRequest visible(String callerAccountId, String id, Instant now) {
|
||||
private DelegationRequest visible(String callerAccountId, String id, Instant now,
|
||||
List<DelegationNotifier.Notice> notices) {
|
||||
requirePresent(id, "delegationRequestId");
|
||||
checkText(id, "delegationRequestId", 16, 128, ID_PATTERN);
|
||||
DelegationRequest request = requests.scanAllAccounts().stream()
|
||||
@@ -400,7 +468,7 @@ public class DelegationRequestService {
|
||||
&& !request.getOwnerAccountId().equals(callerAccountId)) {
|
||||
throw noSuchRequest(id);
|
||||
}
|
||||
return expireIfDue(request, now);
|
||||
return expireIfDue(request, now, notices);
|
||||
}
|
||||
|
||||
private Optional<DelegationRequest> findByWorkflowId(String requestorAccountId, String workflowId) {
|
||||
@@ -412,8 +480,11 @@ public class DelegationRequestService {
|
||||
/**
|
||||
* Applies the state's own window. A request sitting past it is {@code EXPIRED} from the next
|
||||
* read onwards, and the transition is written back so the answer does not depend on who asks.
|
||||
* It is a transition like any other, so it notifies: the guide lists {@code EXPIRED} among the
|
||||
* states a partner hears about, and the read that notices it is the only moment Floci has.
|
||||
*/
|
||||
private DelegationRequest expireIfDue(DelegationRequest request, Instant now) {
|
||||
private DelegationRequest expireIfDue(DelegationRequest request, Instant now,
|
||||
List<DelegationNotifier.Notice> notices) {
|
||||
if (request.getState() == DelegationRequestState.EXPIRED
|
||||
|| request.getExpirationTime() == null
|
||||
|| now.isBefore(request.getExpirationTime())) {
|
||||
@@ -422,19 +493,28 @@ public class DelegationRequestService {
|
||||
request.setState(DelegationRequestState.EXPIRED);
|
||||
request.setUpdatedTime(now);
|
||||
put(request);
|
||||
notices.add(DelegationNotifier.Notice.stateChange(request));
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Commits a transition. It takes the same instant the lookup expired against, so the deadline
|
||||
* cannot fall between the two and let a request that is already past its window move on
|
||||
* instead of expiring.
|
||||
* Commits a transition and takes the notice for it. It is given the same instant the lookup
|
||||
* expired against, so the deadline cannot fall between the two and let a request that is
|
||||
* already past its window move on instead of expiring.
|
||||
*/
|
||||
private DelegationRequest moveTo(DelegationRequest request, DelegationRequestState state, Instant now) {
|
||||
private DelegationRequest moveTo(DelegationRequest request, DelegationRequestState state,
|
||||
Instant now, List<DelegationNotifier.Notice> notices) {
|
||||
// A StateChange is sent "when a delegation request transitions to a new state", so a
|
||||
// second UpdateDelegationRequest on a request already under review writes the notes it
|
||||
// carries, refreshes the expiry window and tells the partner nothing.
|
||||
boolean moved = request.getState() != state;
|
||||
request.setState(state);
|
||||
request.setUpdatedTime(now);
|
||||
request.setExpirationTime(state.window() == null ? null : now.plus(state.window()));
|
||||
put(request);
|
||||
if (moved) {
|
||||
notices.add(DelegationNotifier.Notice.stateChange(request));
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
@@ -2571,8 +2571,8 @@ public class IamQueryHandler {
|
||||
* Releases the exchange token on an accepted request, which finalizes it.
|
||||
*
|
||||
* <p>The operation answers no output shape at all, so the envelope carries no result, and the
|
||||
* token itself goes nowhere from here: AWS publishes it on the partner's SNS topic and
|
||||
* returns it from no operation on the family.
|
||||
* token leaves by the one door AWS gives it: the {@code ExchangeToken} notification on the
|
||||
* partner's topic, which the release publishes, and no operation on the family returns it.
|
||||
*
|
||||
* <p>The caller's ARN goes with the account because this is the one delegation call that can
|
||||
* be scoped to an identity: {@code OnlySendByOwner} holds the release to whoever claimed the
|
||||
|
||||
Reference in new issue
Block a user