mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 14:10:49 -05:00
feat(organizations): add management account email configuration and tests (#2749)
Signed-off-by: Matheus André <matheusandr2@gmail.com>
This commit is contained in:
1 parent
c18af5b68a
commit
89fd37a5c4
4 files changed
+90
-3
No files matched your search
@@ -1402,6 +1402,8 @@ public interface EmulatorConfig {
|
||||
|
||||
@WithDefault("false")
|
||||
boolean scpEnforcementEnabled();
|
||||
|
||||
Optional<String> managementAccountEmail();
|
||||
}
|
||||
|
||||
interface AthenaServiceConfig {
|
||||
|
||||
+45
-3
@@ -151,11 +151,16 @@ public class OrganizationsService implements ScpProvider {
|
||||
private final AccountAwareStorageBackend<CreateAccountStatus> createAccountStatuses;
|
||||
private final AccountAwareStorageBackend<Handshake> handshakes;
|
||||
private final boolean scpEnforcementEnabled;
|
||||
private final String managementAccountEmail;
|
||||
|
||||
@Inject
|
||||
public OrganizationsService(StorageFactory storageFactory, ObjectMapper objectMapper,
|
||||
EmulatorConfig config) {
|
||||
this.scpEnforcementEnabled = config.services().organizations().scpEnforcementEnabled();
|
||||
EmulatorConfig.OrganizationsServiceConfig organizationsConfig = config.services().organizations();
|
||||
this.scpEnforcementEnabled = organizationsConfig.scpEnforcementEnabled();
|
||||
this.managementAccountEmail = organizationsConfig.managementAccountEmail()
|
||||
.map(OrganizationsService::requireValidConfiguredEmail)
|
||||
.orElse(null);
|
||||
this.objectMapper = objectMapper;
|
||||
this.organizations = storageFactory.create("organizations", "organizations-organizations.json",
|
||||
new TypeReference<Map<String, Organization>>() {});
|
||||
@@ -190,6 +195,19 @@ public class OrganizationsService implements ScpProvider {
|
||||
AccountAwareStorageBackend<CreateAccountStatus> createAccountStatuses,
|
||||
AccountAwareStorageBackend<Handshake> handshakes,
|
||||
boolean scpEnforcementEnabled) {
|
||||
this(objectMapper, organizations, accounts, organizationalUnits, policies,
|
||||
createAccountStatuses, handshakes, scpEnforcementEnabled, null);
|
||||
}
|
||||
|
||||
OrganizationsService(ObjectMapper objectMapper,
|
||||
AccountAwareStorageBackend<Organization> organizations,
|
||||
AccountAwareStorageBackend<OrganizationAccount> accounts,
|
||||
AccountAwareStorageBackend<OrganizationalUnit> organizationalUnits,
|
||||
AccountAwareStorageBackend<OrganizationPolicy> policies,
|
||||
AccountAwareStorageBackend<CreateAccountStatus> createAccountStatuses,
|
||||
AccountAwareStorageBackend<Handshake> handshakes,
|
||||
boolean scpEnforcementEnabled,
|
||||
String managementAccountEmail) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.organizations = organizations;
|
||||
this.accounts = accounts;
|
||||
@@ -198,6 +216,9 @@ public class OrganizationsService implements ScpProvider {
|
||||
this.createAccountStatuses = createAccountStatuses;
|
||||
this.handshakes = handshakes;
|
||||
this.scpEnforcementEnabled = scpEnforcementEnabled;
|
||||
this.managementAccountEmail = managementAccountEmail == null
|
||||
? null
|
||||
: requireValidConfiguredEmail(managementAccountEmail);
|
||||
}
|
||||
|
||||
/** A parent reference as returned by {@code ListParents}. */
|
||||
@@ -244,7 +265,9 @@ public class OrganizationsService implements ScpProvider {
|
||||
organization.setFeatureSet(resolvedFeatureSet);
|
||||
organization.setMasterAccountId(callerAccountId);
|
||||
organization.setMasterAccountArn(arn(callerAccountId, "account/" + organizationId + "/" + callerAccountId));
|
||||
organization.setMasterAccountEmail("master@" + callerAccountId + ".example.com");
|
||||
organization.setMasterAccountEmail(managementAccountEmail != null
|
||||
? managementAccountEmail
|
||||
: "master@" + callerAccountId + ".example.com");
|
||||
organization.setCreatedTimestamp(Instant.now());
|
||||
|
||||
Root root = new Root();
|
||||
@@ -1848,11 +1871,30 @@ public class OrganizationsService implements ScpProvider {
|
||||
if (email == null || email.isEmpty()) {
|
||||
throw invalidInput("Email is required.");
|
||||
}
|
||||
if (email.length() < 6 || email.length() > 64 || !EMAIL_PATTERN.matcher(email).matches()) {
|
||||
if (!isValidEmail(email)) {
|
||||
throw invalidInput("Email must be a valid address between 6 and 64 characters.");
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isValidEmail(String email) {
|
||||
return email.length() >= 6 && email.length() <= 64 && EMAIL_PATTERN.matcher(email).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the same rules as {@link #validateEmail} to the configured management-account
|
||||
* email, but fails at startup rather than surfacing an operator misconfiguration to an
|
||||
* API caller as {@code InvalidInputException}.
|
||||
*/
|
||||
private static String requireValidConfiguredEmail(String email) {
|
||||
if (!isValidEmail(email)) {
|
||||
throw new IllegalArgumentException(
|
||||
"floci.services.organizations.management-account-email"
|
||||
+ " (FLOCI_SERVICES_ORGANIZATIONS_MANAGEMENT_ACCOUNT_EMAIL) must be a valid"
|
||||
+ " email address between 6 and 64 characters, got \"" + email + "\"");
|
||||
}
|
||||
return email;
|
||||
}
|
||||
|
||||
private void validateServicePrincipal(String servicePrincipal) {
|
||||
if (servicePrincipal == null || !SERVICE_PRINCIPAL_PATTERN.matcher(servicePrincipal).matches()) {
|
||||
throw invalidInput("ServicePrincipal must match [\\w+=,.@-]{1,128}.");
|
||||
|
||||
@@ -562,6 +562,7 @@ floci:
|
||||
enabled: true
|
||||
organizations:
|
||||
enabled: true
|
||||
# management-account-email: "" # When set, overrides the management-account email reported by DescribeOrganization and account listings.
|
||||
rum:
|
||||
enabled: true
|
||||
networkfirewall:
|
||||
|
||||
+42
@@ -352,4 +352,46 @@ class OrganizationsServiceTest {
|
||||
.getAccountId();
|
||||
assertNull(disabled.effectiveScpLevels(member));
|
||||
}
|
||||
|
||||
@Test
|
||||
void managementAccountEmailDefaultsToSynthesizedAddress() {
|
||||
Organization organization = service.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
|
||||
|
||||
assertEquals("master@" + MANAGEMENT_ACCOUNT + ".example.com", organization.getMasterAccountEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
void managementAccountEmailOverrideIsUsedForOrganizationAndAccount() {
|
||||
OrganizationsService configured = new OrganizationsService(
|
||||
new ObjectMapper(),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
true,
|
||||
"root@corp.example");
|
||||
Organization organization = configured.createOrganization(MANAGEMENT_ACCOUNT, "ALL");
|
||||
|
||||
assertEquals("root@corp.example", organization.getMasterAccountEmail());
|
||||
assertEquals("root@corp.example",
|
||||
configured.describeAccount(MANAGEMENT_ACCOUNT, MANAGEMENT_ACCOUNT).getEmail());
|
||||
assertEquals("root@corp.example",
|
||||
configured.describeOrganization(MANAGEMENT_ACCOUNT).getMasterAccountEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedManagementAccountEmailOverrideIsRejected() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new OrganizationsService(
|
||||
new ObjectMapper(),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
AccountAwareStorageBackend.inMemory(MANAGEMENT_ACCOUNT),
|
||||
true,
|
||||
"not-an-email"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user