Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.

Commit 84549c0

Browse files
committed
wip
1 parent d9c16f2 commit 84549c0

File tree

5 files changed

+119
-12
lines changed

5 files changed

+119
-12
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2017 Nike, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.nike.cerberus.command.core;
18+
19+
import com.beust.jcommander.Parameters;
20+
import com.nike.cerberus.command.Command;
21+
import com.nike.cerberus.operation.Operation;
22+
23+
import static com.nike.cerberus.command.core.UpdateBackupCmkAdminPrincipalsCommand.COMMAND_NAME;
24+
25+
/**
26+
* Command to update which principals besides for the root account will have permissions to use the backup cmk,
27+
* AKA create and restore backups.
28+
*/
29+
@Parameters(
30+
commandNames = COMMAND_NAME,
31+
commandDescription = "Update the IAM Principals that are allowed to create and restore backups."
32+
)
33+
public class UpdateBackupCmkAdminPrincipalsCommand implements Command {
34+
35+
public static final String COMMAND_NAME = "";
36+
37+
@Override
38+
public String getCommandName() {
39+
return null;
40+
}
41+
42+
@Override
43+
public Class<? extends Operation<?>> getOperationClass() {
44+
return null;
45+
}
46+
}

src/main/java/com/nike/cerberus/domain/environment/Environment.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.nike.cerberus.domain.environment;
1818

1919
import java.util.HashMap;
20+
import java.util.LinkedList;
21+
import java.util.List;
2022
import java.util.Map;
2123

2224
/**
@@ -38,6 +40,8 @@ public class Environment {
3840

3941
private Map<String, BackupRegionInfo> regionBackupBucketMap;
4042

43+
private List<String> backupAdminIamPrincipals;
44+
4145
private String metricsTopicArn;
4246

4347
/**
@@ -129,6 +133,14 @@ public void setRegionBackupBucketMap(Map<String, BackupRegionInfo> regionBackupB
129133
this.regionBackupBucketMap = regionBackupBucketMap;
130134
}
131135

136+
public List<String> getBackupAdminIamPrincipals() {
137+
return backupAdminIamPrincipals == null ? new LinkedList<>() : backupAdminIamPrincipals;
138+
}
139+
140+
public void setBackupAdminIamPrincipals(List<String> backupAdminIamPrincipals) {
141+
this.backupAdminIamPrincipals = backupAdminIamPrincipals;
142+
}
143+
132144
public String getMetricsTopicArn() {
133145
return metricsTopicArn;
134146
}

src/main/java/com/nike/cerberus/operation/core/CreateCerberusBackupOperation.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import java.text.SimpleDateFormat;
6868
import java.util.Date;
6969
import java.util.HashMap;
70+
import java.util.LinkedList;
7071
import java.util.List;
7172
import java.util.Map;
7273
import java.util.Optional;
@@ -343,28 +344,35 @@ private String provisionKmsCmkForBackupRegion(String region) {
343344
String accountId = identityResult.getAccount();
344345
String rootArn = String.format("arn:aws:iam::%s:root", accountId);
345346

346-
String adminRoleArn = configStore.getAccountAdminArn().get();
347+
List<String> backupAdminPrincipals = configStore.getBackupAdminIamPrincipals();
348+
349+
if (backupAdminPrincipals.isEmpty()) {
350+
String adminRoleArn = configStore.getAccountAdminArn().get();
351+
backupAdminPrincipals.add(adminRoleArn);
352+
configStore.storeBackupAdminIamPrincipals(backupAdminPrincipals);
353+
}
347354

348355
Policy kmsPolicy = new Policy();
356+
final List<Statement> statements = new LinkedList<>();
349357

350358
// allow the root user all permissions
351359
Statement rootUserStatement = new Statement(Statement.Effect.Allow);
352360
rootUserStatement.withId("Root User Has All Actions");
353361
rootUserStatement.withPrincipals(new Principal(AWS_PROVIDER, rootArn, false));
354362
rootUserStatement.withActions(KMSActions.AllKMSActions);
355363
rootUserStatement.withResources(new Resource("*"));
364+
statements.add(rootUserStatement);
365+
366+
// allow the configured admin iam principals all permissions
367+
backupAdminPrincipals.forEach(principal -> {
368+
statements.add(new Statement(Statement.Effect.Allow)
369+
.withId("Admin principal " + principal + " Has All Actions")
370+
.withPrincipals(new Principal(AWS_PROVIDER, principal, false))
371+
.withActions(KMSActions.AllKMSActions)
372+
.withResources(new Resource("*"));
373+
});
356374

357-
// allow the configured admin user all permissions
358-
Statement adminUserStatement = new Statement(Statement.Effect.Allow);
359-
adminUserStatement.withId("Admin Role Has All Actions");
360-
adminUserStatement.withPrincipals(new Principal(AWS_PROVIDER, adminRoleArn, false));
361-
adminUserStatement.withActions(KMSActions.AllKMSActions);
362-
adminUserStatement.withResources(new Resource("*"));
363-
364-
kmsPolicy.withStatements(
365-
rootUserStatement,
366-
adminUserStatement
367-
);
375+
kmsPolicy.setStatements(statements);
368376

369377
String policyString = kmsPolicy.toJson();
370378

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2017 Nike, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.nike.cerberus.operation.core;
18+
19+
/**
20+
* Operation to update which principals besides for the root account will have permissions to use the backup cmk,
21+
* AKA create and restore backups
22+
*/
23+
public class UpdateBackupCmkAdminPrincipalsOperation {
24+
}

src/main/java/com/nike/cerberus/store/ConfigStore.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ public Optional<String> getAccountAdminArn() {
596596
return Optional.ofNullable(baseParameters.getAccountAdminArn());
597597
}
598598

599+
600+
599601
public String getCerberusBaseUrl() {
600602
return String.format("https://%s", getGatewayStackParamters().getHostname());
601603
}
@@ -955,6 +957,21 @@ public void storeBackupInfoForRegion(String region, String bucket, String kmsCmk
955957
}
956958
}
957959

960+
public List<String> getBackupAdminIamPrincipals() {
961+
synchronized (envDataLock) {
962+
final Environment environment = getEnvironmentData();
963+
return environment.getBackupAdminIamPrincipals();
964+
}
965+
}
966+
967+
public void storeBackupAdminIamPrincipals(List<String> principals) {
968+
synchronized (envDataLock) {
969+
final Environment environment = getEnvironmentData();
970+
environment.setBackupAdminIamPrincipals(principals);
971+
saveEnvironmentData(environment);
972+
}
973+
}
974+
958975
public Optional<String> getMetricsTopicArn() {
959976
synchronized (envDataLock) {
960977
final Environment environment = getEnvironmentData();

0 commit comments

Comments
 (0)