mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(codebuild): give resource-tag conditions the tags of the project or report group a request names (#8596)
The generated request resources give CodeBuild's project and report group types aws:ResourceTag/ keys, but CodeBuildStoredResourceArns did not answer storedTags, so a condition on their tags saw none. It now answers from CodeBuildService.storedTags, which reads the project or the report group stored under the ARN in the account and Region it names, a pre-multi-account one of the default account in place, matched on the whole ARN, and gives its key and value pairs as tags. The tag lists are volatile, as CodeBuild replaces a resource's list whole and never changes one in place.
This commit is contained in:
1 parent
48be04cc21
commit
d58d9ecac9
8 files changed
+181
-11
No files matched your search
@@ -31,9 +31,10 @@ import java.util.Optional;
|
||||
* table bucket or a table; Lambda, for the layer version a layer version ARN selects, the image of a MicroVM and the
|
||||
* execution of a callback, and the tags of a function, an event source mapping, a web function, a MicroVM image or a
|
||||
* network connector; Glue, for the tags of a resource it tags; Step Functions, for the tags of a state machine, its
|
||||
* versions and executions, and an activity); looked up lazily by {@code ResourceArnBuilder} and
|
||||
* {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or an
|
||||
* ARN is of, so IAM never depends on that service directly.</p>
|
||||
* versions and executions, and an activity; CodeBuild, for the project a build belongs to and the tags of a project or
|
||||
* a report group); looked up lazily by {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through
|
||||
* {@code Instance<StoredResourceArns>} for the namespace a request or an ARN is of, so IAM never depends on that
|
||||
* service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
|
||||
@@ -7,7 +7,9 @@ import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.EpochSeconds;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackedMap;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.codebuild.model.Build;
|
||||
import io.floci.aws.services.codebuild.model.BuildPhase;
|
||||
@@ -24,9 +26,11 @@ import jakarta.inject.Inject;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
@@ -43,6 +47,9 @@ public class CodeBuildService {
|
||||
private Map<String, Map<String, Project>> projects = new ConcurrentHashMap<>();
|
||||
// key: region -> arn -> report group
|
||||
private Map<String, Map<String, ReportGroup>> reportGroups = new ConcurrentHashMap<>();
|
||||
/** The backends of {@link #projects} and {@link #reportGroups}, read in another account than the caller's. */
|
||||
private StorageBackend<String, Map<String, Project>> projectStorage;
|
||||
private StorageBackend<String, Map<String, ReportGroup>> reportGroupStorage;
|
||||
// key: region -> arn -> source credential (token is stored but never returned)
|
||||
private Map<String, Map<String, SourceCredential>> sourceCredentials = new ConcurrentHashMap<>();
|
||||
// key: account:region -> buildId -> build (transient: builds are runtime state)
|
||||
@@ -70,10 +77,12 @@ public class CodeBuildService {
|
||||
if (storageFactory == null) {
|
||||
return; // keeps non-CDI unit tests working
|
||||
}
|
||||
this.projects = storageBacked("codebuild-projects.json",
|
||||
this.projectStorage = storageFactory.create("codebuild", "codebuild-projects.json",
|
||||
new TypeReference<Map<String, Map<String, Project>>>() {});
|
||||
this.reportGroups = storageBacked("codebuild-report-groups.json",
|
||||
this.projects = new StorageBackedMap<>(projectStorage);
|
||||
this.reportGroupStorage = storageFactory.create("codebuild", "codebuild-report-groups.json",
|
||||
new TypeReference<Map<String, Map<String, ReportGroup>>>() {});
|
||||
this.reportGroups = new StorageBackedMap<>(reportGroupStorage);
|
||||
this.sourceCredentials = storageBacked("codebuild-source-credentials.json",
|
||||
new TypeReference<Map<String, Map<String, SourceCredential>>>() {});
|
||||
this.persistedBuildCounters = storageBacked("codebuild-build-counters.json",
|
||||
@@ -106,6 +115,48 @@ public class CodeBuildService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the project or the report group stored under {@code arn} in the account and Region it names, read in
|
||||
* place and matched on the whole ARN; empty for an ARN naming nothing stored.
|
||||
*/
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "codebuild")) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String resource = parsed.resource();
|
||||
List<Map<String, String>> tags = null;
|
||||
if (resource.startsWith("project/")) {
|
||||
tags = inRegion(projectStorage, projects, parsed.accountId(), parsed.region())
|
||||
.map(byName -> byName.get(resource.substring("project/".length())))
|
||||
.filter(project -> arn.equals(project.getArn()))
|
||||
.map(Project::getTags)
|
||||
.orElse(null);
|
||||
} else if (resource.startsWith("report-group/")) {
|
||||
tags = inRegion(reportGroupStorage, reportGroups, parsed.accountId(), parsed.region())
|
||||
.map(byArn -> byArn.get(arn))
|
||||
.map(ReportGroup::getTags)
|
||||
.orElse(null);
|
||||
}
|
||||
Map<String, String> byKey = new LinkedHashMap<>();
|
||||
if (tags != null) {
|
||||
for (Map<String, ?> tag : tags) {
|
||||
if (tag.get("key") instanceof String key && tag.get("value") instanceof String value) {
|
||||
byKey.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return byKey;
|
||||
}
|
||||
|
||||
/** The resources stored for the Region in the account, read in place; from {@code local} without storage. */
|
||||
private static <V> Optional<Map<String, V>> inRegion(StorageBackend<String, Map<String, V>> storage,
|
||||
Map<String, Map<String, V>> local, String account,
|
||||
String region) {
|
||||
return storage == null ? Optional.ofNullable(local.get(region))
|
||||
: AccountAwareStorageBackend.readInAccount(storage, account, region);
|
||||
}
|
||||
|
||||
private Map<String, Project> projectsFor(String region) {
|
||||
return projects.computeIfAbsent(region, r -> new ConcurrentHashMap<>());
|
||||
}
|
||||
|
||||
@@ -8,11 +8,13 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* What IAM asks CodeBuild of the resources a request names: the project the caller's build of an id belongs to, as
|
||||
* the service stores them, which CodeBuild checks a call naming the build on.
|
||||
* the service stores them, which CodeBuild checks a call naming the build on, and the tags of a project or a report
|
||||
* group.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class CodeBuildStoredResourceArns implements StoredResourceArns {
|
||||
@@ -49,4 +51,9 @@ public class CodeBuildStoredResourceArns implements StoredResourceArns {
|
||||
.distinct()
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
return codeBuild.storedTags(arn);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class Project {
|
||||
private Integer timeoutInMinutes;
|
||||
private Integer queuedTimeoutInMinutes;
|
||||
private String encryptionKey;
|
||||
private List<Map<String, String>> tags;
|
||||
private volatile List<Map<String, String>> tags;
|
||||
private Double created;
|
||||
private Double lastModified;
|
||||
private Map<String, Object> logsConfig;
|
||||
|
||||
@@ -17,7 +17,7 @@ public class ReportGroup {
|
||||
private Map<String, Object> exportConfig;
|
||||
private Double created;
|
||||
private Double lastModified;
|
||||
private List<Map<String, String>> tags;
|
||||
private volatile List<Map<String, String>> tags;
|
||||
private String status;
|
||||
|
||||
public String getArn() { return arn; }
|
||||
|
||||
Reference in new issue
Block a user