mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
fix(iam): generate resource and request tag condition context (#7589)
This commit is contained in:
1 parent
cf4b3f0e82
commit
c3058a8de4
9 files changed
+863
-59
No files matched your search
+103
@@ -244,6 +244,109 @@ class IamConditionContextResolverTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamRoleTagsComeFromStoredStateRatherThanRequestTags() {
|
||||
IamRole role = new IamRole();
|
||||
role.setTags(Map.of("Owner", "team-a"));
|
||||
when(iamService.findRole("123456789012", "tagged")).thenReturn(Optional.of(role));
|
||||
Map<String, List<String>> context = resolver.resolve("iam", "iam:PutRolePolicy",
|
||||
formRequest("RoleName=tagged&Tags.member.1.Key=Owner&Tags.member.1.Value=other"));
|
||||
assertEquals(List.of("team-a"), context.get("aws:ResourceTag/Owner"));
|
||||
assertEquals(List.of("team-a"), context.get("iam:ResourceTag/Owner"));
|
||||
assertNull(context.get("aws:RequestTag/Owner"));
|
||||
assertNull(resolver.resolve("iam", "iam:PassRole", formRequest("RoleName=tagged")));
|
||||
assertNull(resolver.resolve("iam", "iam:GetUser", formRequest("UserName=missing&RoleName=tagged")));
|
||||
assertNull(resolver.resolve("iam", "iam:PutRolePolicy", formRequest("RoleName=missing")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"iam:TagRole", "iam:TagUser", "iam:TagInstanceProfile", "iam:TagMFADevice",
|
||||
"iam:TagOpenIDConnectProvider", "iam:TagSAMLProvider", "iam:TagPolicy", "iam:TagServerCertificate"})
|
||||
void iamTaggingCarriesRequestedValuesAndEveryKey(String action) {
|
||||
Map<String, List<String>> context = resolver.resolve("iam", action,
|
||||
formRequest("Tags.member.2.Key=Owner&Tags.member.2.Value=team-a&Tags.7.Key=Env&Tags.7.Value="));
|
||||
assertEquals(List.of("team-a"), context.get("aws:RequestTag/Owner"));
|
||||
assertEquals(List.of(""), context.get("aws:RequestTag/Env"));
|
||||
assertEquals(List.of("Owner", "Env"), context.get("aws:TagKeys"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"iam:UntagRole", "iam:UntagUser", "iam:UntagInstanceProfile", "iam:UntagMFADevice",
|
||||
"iam:UntagOpenIDConnectProvider", "iam:UntagSAMLProvider", "iam:UntagPolicy", "iam:UntagServerCertificate"})
|
||||
void iamUntaggingReadsSparseKeysAndIgnoresTagValues(String action) {
|
||||
assertEquals(Map.of("aws:TagKeys", List.of("Owner", "Env")), resolver.resolve("iam", action,
|
||||
formRequest("TagKeys.member.2=Owner&TagKeys.7=Env&Tags.member.1.Key=Wrong&Tags.member.1.Value=x")));
|
||||
assertNull(resolver.resolve("iam", action, formRequest("")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamRoleTagsAreAccountScopedAndUseTheFirstBodyValue() {
|
||||
IamRole role = new IamRole();
|
||||
role.setTags(Map.of("Owner", "team-a"));
|
||||
when(iamService.findRole("123456789012", "tagged")).thenReturn(Optional.of(role));
|
||||
ContainerRequestContext request = formRequest("RoleName=missing&RoleName=tagged");
|
||||
UriInfo uri = mock(UriInfo.class);
|
||||
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
|
||||
query.putSingle("RoleName", "tagged");
|
||||
when(request.getUriInfo()).thenReturn(uri);
|
||||
when(uri.getQueryParameters()).thenReturn(query);
|
||||
assertNull(resolver.resolve("iam", "iam:GetRole", request));
|
||||
requestContext.setAccountId("222222222222");
|
||||
assertNull(resolver.resolve("iam", "iam:GetRole", formRequest("RoleName=tagged")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"iam:GetUser", "iam:PutUserPolicy", "iam:TagUser", "iam:CreateAccessKey"})
|
||||
void iamUserTagsFollowTheGeneratedTargetAndRequestAccount(String action) {
|
||||
IamUser user = new IamUser();
|
||||
user.setTags(Map.of("Owner", "user-team"));
|
||||
IamRole role = new IamRole();
|
||||
role.setTags(Map.of("Owner", "role-team"));
|
||||
when(iamService.findUser("123456789012", "tagged")).thenReturn(Optional.of(user));
|
||||
when(iamService.findRole("123456789012", "decoy")).thenReturn(Optional.of(role));
|
||||
assertEquals(Map.of("aws:ResourceTag/Owner", List.of("user-team"),
|
||||
"iam:ResourceTag/Owner", List.of("user-team")),
|
||||
resolver.resolve("iam", action, formRequest("UserName=tagged&RoleName=decoy")));
|
||||
assertNull(resolver.resolve("iam", action, formRequest("UserName=missing&RoleName=decoy")));
|
||||
assertNull(resolver.resolve("iam", action, formRequest("UserName=missing&UserName=tagged")));
|
||||
requestContext.setAccountId("222222222222");
|
||||
assertNull(resolver.resolve("iam", action, formRequest("UserName=tagged&RoleName=decoy")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void secondaryRoleAndCreationTargetsDoNotSupplyStoredResourceTags() {
|
||||
IamRole role = new IamRole();
|
||||
role.setTags(Map.of("Owner", "team-a"));
|
||||
when(iamService.findRole("123456789012", "tagged")).thenReturn(Optional.of(role));
|
||||
for (String action : List.of("iam:AddRoleToInstanceProfile", "iam:RemoveRoleFromInstanceProfile",
|
||||
"iam:CreateRole", "iam:PassRole")) {
|
||||
assertNull(resolver.resolve("iam", action, formRequest("RoleName=tagged&InstanceProfileName=profile")));
|
||||
}
|
||||
IamUser user = new IamUser();
|
||||
user.setTags(Map.of("Owner", "team-a"));
|
||||
when(iamService.findUser("123456789012", "tagged")).thenReturn(Optional.of(user));
|
||||
assertNull(resolver.resolve("iam", "iam:CreateUser", formRequest("UserName=tagged")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamRequestTagValuesMatchTheQueryHandlerForGetAndDuplicateParameters() {
|
||||
assertEquals(List.of("first"), resolver.resolve("iam", "iam:TagUser",
|
||||
formRequest("Tags.member.1.Key=Owner&Tags.member.1.Value=first&Tags.member.1.Value=second"))
|
||||
.get("aws:RequestTag/Owner"));
|
||||
ContainerRequestContext request = mock(ContainerRequestContext.class);
|
||||
when(request.getMethod()).thenReturn("GET");
|
||||
when(request.getProperty(AwsProtocolClaimFilter.CLAIM_PROPERTY))
|
||||
.thenReturn(new ProtocolClaim(WireProtocol.AWS_QUERY, null, "TagUser", null));
|
||||
UriInfo uri = mock(UriInfo.class);
|
||||
MultivaluedMap<String, String> query = new MultivaluedHashMap<>();
|
||||
query.putSingle("Tags.3.Key", "Owner");
|
||||
query.putSingle("Tags.3.Value", "team-a");
|
||||
when(request.getUriInfo()).thenReturn(uri);
|
||||
when(uri.getQueryParameters()).thenReturn(query);
|
||||
assertEquals(Map.of("aws:RequestTag/Owner", List.of("team-a"), "aws:TagKeys", List.of("Owner")),
|
||||
resolver.resolve("iam", "iam:TagUser", request));
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamPolicyContextKeepsEmptyValuesAndOmitsMissingValues() {
|
||||
assertNull(resolver.resolve("iam", "iam:AttachRolePolicy", formRequest("RoleName=role")));
|
||||
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.AwsTestClients;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures;
|
||||
import io.github.hectorvent.floci.testing.EnforcementFixtures.UserCredentials;
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.PartitionCleanup;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.iam.model.IamException;
|
||||
import software.amazon.awssdk.services.iam.model.Tag;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class IamRoleTagConditionIntegrationTest {
|
||||
private static final String TRUST = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
|
||||
"Principal":{"Service":"ec2.amazonaws.com"},"Action":"sts:AssumeRole"}]}""";
|
||||
private static final String INLINE = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}""";
|
||||
|
||||
@TestHTTPResource("/")
|
||||
URI baseUri;
|
||||
@RegisterExtension
|
||||
final PartitionCleanup cleanup = new PartitionCleanup();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"aws:ResourceTag/", "iam:ResourceTag/"})
|
||||
void storedRoleTagsGatePolicyUpdatesAndExplicitDenies(String prefix) {
|
||||
try (IamClient admin = AwsTestClients.create(IamClient.builder(), "test", Region.US_EAST_1)) {
|
||||
String allowed = role(admin, List.of(tag("Owner", "team-a")));
|
||||
String other = role(admin, List.of(tag("Owner", "team-b")));
|
||||
String untagged = role(admin, List.of());
|
||||
String blocked = role(admin, List.of(tag("Owner", "team-a"), tag("Blocked", "yes")));
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"iam:PutRolePolicy","Resource":"*",
|
||||
"Condition":{"StringEquals":{"%sOwner":"team-a"}}},
|
||||
{"Effect":"Deny","Action":"iam:PutRolePolicy","Resource":"*",
|
||||
"Condition":{"StringEquals":{"%sBlocked":"yes"}}}]}""".formatted(prefix, prefix);
|
||||
try (IamClient caller = caller(policy)) {
|
||||
caller.putRolePolicy(r -> r.roleName(allowed).policyName("inline").policyDocument(INLINE));
|
||||
for (String name : List.of(other, untagged, blocked)) {
|
||||
denied(() -> caller.putRolePolicy(r -> r.roleName(name).policyName("inline").policyDocument(INLINE)));
|
||||
assertEquals(List.of(), admin.listRolePolicies(r -> r.roleName(name)).policyNames());
|
||||
}
|
||||
admin.untagRole(r -> r.roleName(allowed).tagKeys("Owner"));
|
||||
denied(() -> caller.putRolePolicy(r -> r.roleName(allowed).policyName("second").policyDocument(INLINE)));
|
||||
assertEquals(List.of("inline"), admin.listRolePolicies(r -> r.roleName(allowed)).policyNames());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagRoleDistinguishesOldTagsFromTheRequestedUpdate() {
|
||||
try (IamClient admin = AwsTestClients.create(IamClient.builder(), "test", Region.US_EAST_1)) {
|
||||
String owned = role(admin, List.of(tag("Owner", "team-a")));
|
||||
String other = role(admin, List.of(tag("Owner", "team-b")));
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"iam:TagRole","Resource":"*",
|
||||
"Condition":{"StringEquals":{"aws:ResourceTag/Owner":"team-a","aws:RequestTag/Owner":"team-b"},
|
||||
"ForAllValues:StringEquals":{"aws:TagKeys":["Owner"]},"Null":{"aws:TagKeys":"false"}}}]}""";
|
||||
try (IamClient caller = caller(policy)) {
|
||||
denied(() -> caller.tagRole(r -> r.roleName(other).tags(tag("Owner", "team-b"))));
|
||||
denied(() -> caller.tagRole(r -> r.roleName(owned).tags(tag("Owner", "team-a"))));
|
||||
denied(() -> caller.tagRole(r -> r.roleName(owned).tags(tag("Owner", "team-b"), tag("Extra", "x"))));
|
||||
assertEquals(List.of(tag("Owner", "team-a")), admin.listRoleTags(r -> r.roleName(owned)).tags());
|
||||
caller.tagRole(r -> r.roleName(owned).tags(tag("Owner", "team-b")));
|
||||
assertEquals(List.of(tag("Owner", "team-b")), admin.listRoleTags(r -> r.roleName(owned)).tags());
|
||||
denied(() -> caller.tagRole(r -> r.roleName(owned).tags(tag("Owner", "team-b"))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagUserUsesRequestedValuesAndUntagUserChecksEveryRequestedKey() {
|
||||
try (IamClient admin = AwsTestClients.create(IamClient.builder(), "test", Region.US_EAST_1)) {
|
||||
String user = "tag-target-" + suffix();
|
||||
admin.createUser(r -> r.userName(user));
|
||||
cleanup.register(() -> EnforcementFixtures.removeUser(baseUri.getPort(), auth("test", "iam"), user));
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"iam:TagUser","Resource":"*",
|
||||
"Condition":{"StringEquals":{"aws:RequestTag/Env":"dev"},
|
||||
"ForAllValues:StringEquals":{"aws:TagKeys":["Env"]}}},
|
||||
{"Effect":"Allow","Action":"iam:UntagUser","Resource":"*",
|
||||
"Condition":{"ForAllValues:StringEquals":{"aws:TagKeys":["Env"]},
|
||||
"Null":{"aws:TagKeys":"false"}}}]}""";
|
||||
try (IamClient caller = caller(policy)) {
|
||||
denied(() -> caller.tagUser(r -> r.userName(user).tags(tag("Env", "prod"))));
|
||||
denied(() -> caller.tagUser(r -> r.userName(user).tags(tag("Env", "dev"), tag("Owner", "other"))));
|
||||
caller.tagUser(r -> r.userName(user).tags(tag("Env", "dev")));
|
||||
denied(() -> caller.untagUser(r -> r.userName(user).tagKeys("Env", "Owner")));
|
||||
assertEquals(List.of(tag("Env", "dev")), admin.listUserTags(r -> r.userName(user)).tags());
|
||||
caller.untagUser(r -> r.userName(user).tagKeys("Env"));
|
||||
assertEquals(List.of(), admin.listUserTags(r -> r.userName(user)).tags());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"aws:ResourceTag/", "iam:ResourceTag/"})
|
||||
void storedUserTagsGatePolicyUpdatesAndExplicitDenies(String prefix) {
|
||||
try (IamClient admin = AwsTestClients.create(IamClient.builder(), "test", Region.US_EAST_1)) {
|
||||
String owned = user(admin, List.of(tag("Owner", "team-a")));
|
||||
String other = user(admin, List.of(tag("Owner", "team-b")));
|
||||
String untagged = user(admin, List.of());
|
||||
String blocked = user(admin, List.of(tag("Owner", "team-a"), tag("Blocked", "yes")));
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"iam:PutUserPolicy","Resource":"*",
|
||||
"Condition":{"StringEquals":{"%sOwner":"team-a"}}},
|
||||
{"Effect":"Deny","Action":"iam:PutUserPolicy","Resource":"*",
|
||||
"Condition":{"StringEquals":{"%sBlocked":"yes"}}}]}""".formatted(prefix, prefix);
|
||||
try (IamClient caller = caller(policy)) {
|
||||
caller.putUserPolicy(r -> r.userName(owned).policyName("inline").policyDocument(INLINE));
|
||||
for (String name : List.of(other, untagged, blocked)) {
|
||||
denied(() -> caller.putUserPolicy(r -> r.userName(name).policyName("inline").policyDocument(INLINE)));
|
||||
assertEquals(List.of(), admin.listUserPolicies(r -> r.userName(name)).policyNames());
|
||||
}
|
||||
admin.untagUser(r -> r.userName(owned).tagKeys("Owner"));
|
||||
denied(() -> caller.putUserPolicy(r -> r.userName(owned).policyName("second").policyDocument(INLINE)));
|
||||
assertEquals(List.of("inline"), admin.listUserPolicies(r -> r.userName(owned)).policyNames());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyTaggingAndUntaggingEnforceGeneratedRequestTagConditions() {
|
||||
try (IamClient admin = AwsTestClients.create(IamClient.builder(), "test", Region.US_EAST_1)) {
|
||||
String arn = admin.createPolicy(r -> r.policyName("tag-policy-" + suffix())
|
||||
.policyDocument(INLINE)).policy().arn();
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[
|
||||
{"Effect":"Allow","Action":"iam:TagPolicy","Resource":"*",
|
||||
"Condition":{"StringEquals":{"aws:RequestTag/Env":"dev"},
|
||||
"ForAllValues:StringEquals":{"aws:TagKeys":["Env"]}}},
|
||||
{"Effect":"Allow","Action":"iam:UntagPolicy","Resource":"*",
|
||||
"Condition":{"ForAllValues:StringEquals":{"aws:TagKeys":["Env"]},
|
||||
"Null":{"aws:TagKeys":"false"}}}]}""";
|
||||
try (IamClient caller = caller(policy)) {
|
||||
denied(() -> caller.tagPolicy(r -> r.policyArn(arn).tags(tag("Env", "prod"))));
|
||||
denied(() -> caller.tagPolicy(r -> r.policyArn(arn).tags(tag("Env", "dev"), tag("Owner", "other"))));
|
||||
assertEquals(List.of(), admin.listPolicyTags(r -> r.policyArn(arn)).tags());
|
||||
caller.tagPolicy(r -> r.policyArn(arn).tags(tag("Env", "dev")));
|
||||
denied(() -> caller.untagPolicy(r -> r.policyArn(arn).tagKeys("Env", "Owner")));
|
||||
assertEquals(List.of(tag("Env", "dev")), admin.listPolicyTags(r -> r.policyArn(arn)).tags());
|
||||
caller.untagPolicy(r -> r.policyArn(arn).tagKeys("Env"));
|
||||
assertEquals(List.of(), admin.listPolicyTags(r -> r.policyArn(arn)).tags());
|
||||
} finally {
|
||||
admin.deletePolicy(r -> r.policyArn(arn));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String user(IamClient admin, List<Tag> tags) {
|
||||
String name = "tag-user-" + suffix();
|
||||
admin.createUser(r -> r.userName(name).tags(tags));
|
||||
cleanup.register(() -> EnforcementFixtures.removeUser(baseUri.getPort(), auth("test", "iam"), name));
|
||||
return name;
|
||||
}
|
||||
|
||||
private String role(IamClient admin, List<Tag> tags) {
|
||||
String name = "tag-role-" + suffix();
|
||||
admin.createRole(r -> r.roleName(name).path("/tag-context/").assumeRolePolicyDocument(TRUST).tags(tags));
|
||||
cleanup.register(() -> EnforcementFixtures.removeRole(baseUri.getPort(), auth("test", "iam"), name));
|
||||
return name;
|
||||
}
|
||||
|
||||
private IamClient caller(String policy) {
|
||||
String name = "tag-caller-" + suffix();
|
||||
UserCredentials credentials = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth("test", "iam"), name);
|
||||
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth("test", "iam"), name, "tags", policy);
|
||||
return AwsTestClients.create(IamClient.builder(), credentials.accessKeyId(), credentials.secretAccessKey(),
|
||||
Region.US_EAST_1);
|
||||
}
|
||||
|
||||
private static void denied(Runnable operation) {
|
||||
IamException error = assertThrows(IamException.class, operation::run);
|
||||
assertEquals(403, error.statusCode());
|
||||
assertEquals("AccessDenied", error.awsErrorDetails().errorCode());
|
||||
}
|
||||
|
||||
private static Tag tag(String key, String value) {
|
||||
return Tag.builder().key(key).value(value).build();
|
||||
}
|
||||
|
||||
private static String suffix() {
|
||||
return UUID.randomUUID().toString().substring(0, 8);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user