This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Nike-Inc/feature/add_update_cms_config_co…
…mmand Add update-cms-config command
- Loading branch information
Showing
8 changed files
with
337 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/com/nike/cerberus/command/cms/UpdateCmsConfigCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2017 Nike, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.nike.cerberus.command.cms; | ||
|
||
import com.beust.jcommander.DynamicParameter; | ||
import com.beust.jcommander.Parameter; | ||
import com.beust.jcommander.Parameters; | ||
import com.nike.cerberus.command.Command; | ||
import com.nike.cerberus.operation.Operation; | ||
import com.nike.cerberus.operation.cms.UpdateCmsConfigOperation; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static com.nike.cerberus.command.cms.CreateCmsClusterCommand.COMMAND_NAME; | ||
|
||
/** | ||
* Command to create the CMS cluster. | ||
*/ | ||
@Parameters(commandNames = COMMAND_NAME, commandDescription = "Updates the CMS config.") | ||
public class UpdateCmsConfigCommand implements Command { | ||
|
||
public static final String COMMAND_NAME = "update-cms-config"; | ||
public static final String OVERWRITE_LONG_ARG = "--overwrite"; | ||
|
||
@Parameter(names = CreateCmsConfigCommand.ADMIN_GROUP_LONG_ARG, description = "Group that has admin privileges in CMS.") | ||
private String adminGroup; | ||
|
||
@Parameter(names = OVERWRITE_LONG_ARG, description = "Overwrite -P parameters completely.") | ||
private boolean overwrite; | ||
|
||
@DynamicParameter(names = CreateCmsConfigCommand.PROPERTY_SHORT_ARG, description = "Dynamic parameters for setting additional properties in the CMS environment configuration.") | ||
private Map<String, String> additionalProperties = new HashMap<>(); | ||
|
||
public String getAdminGroup() { | ||
return adminGroup; | ||
} | ||
|
||
public boolean getOverwrite() { | ||
return overwrite; | ||
} | ||
|
||
public Map<String, String> getAdditionalProperties() { | ||
return additionalProperties; | ||
} | ||
|
||
@Override | ||
public String getCommandName() { | ||
return COMMAND_NAME; | ||
} | ||
|
||
@Override | ||
public Class<? extends Operation<?>> getOperationClass() { | ||
return UpdateCmsConfigOperation.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/com/nike/cerberus/operation/cms/UpdateCmsConfigOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) 2017 Nike, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.nike.cerberus.operation.cms; | ||
|
||
import com.nike.cerberus.command.cms.UpdateCmsConfigCommand; | ||
import com.nike.cerberus.operation.Operation; | ||
import com.nike.cerberus.store.ConfigStore; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Inject; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
|
||
import static com.nike.cerberus.ConfigConstants.SYSTEM_CONFIGURED_CMS_PROPERTIES; | ||
import static com.nike.cerberus.ConfigConstants.CMS_ADMIN_GROUP_KEY; | ||
|
||
/** | ||
* Gathers all of the CMS environment configuration and puts it in the config bucket. | ||
*/ | ||
public class UpdateCmsConfigOperation implements Operation<UpdateCmsConfigCommand> { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
private final ConfigStore configStore; | ||
|
||
@Inject | ||
public UpdateCmsConfigOperation(final ConfigStore configStore) { | ||
this.configStore = configStore; | ||
} | ||
|
||
@Override | ||
public void run(final UpdateCmsConfigCommand command) { | ||
|
||
logger.debug("Retrieving configuration data from the configuration bucket."); | ||
|
||
final Properties newProperties = configStore.getCmsSystemProperties(); | ||
final Properties existingCustomProperties = configStore.getExistingCmsUserProperties(); | ||
if (! command.getOverwrite()) { | ||
// keep existing custom properties | ||
newProperties.putAll(existingCustomProperties); | ||
} | ||
|
||
// update existing custom properties, add new ones | ||
command.getAdditionalProperties().forEach((k, v) -> { | ||
if (! SYSTEM_CONFIGURED_CMS_PROPERTIES.contains(k)) { | ||
newProperties.put(k, v); | ||
} else { | ||
logger.warn("Ignoring additional property that would override system configured property, " + k); | ||
} | ||
}); | ||
|
||
final String existingAdminGroup = existingCustomProperties.getProperty(CMS_ADMIN_GROUP_KEY); | ||
final String adminGroupParameter = command.getAdminGroup(); | ||
String newAdminGroupValue = existingAdminGroup; // keep existing admin group by default | ||
|
||
if (shouldOverwriteAdminGroup(existingAdminGroup, adminGroupParameter)) { | ||
logger.warn(String.format("Updating CMS admin group from '%s' to '%s'", existingAdminGroup, adminGroupParameter)); | ||
configStore.storeCmsAdminGroup(adminGroupParameter); | ||
newAdminGroupValue = adminGroupParameter; // overwrite admin group | ||
} | ||
newProperties.put(CMS_ADMIN_GROUP_KEY, newAdminGroupValue); | ||
|
||
logger.info("Uploading the CMS configuration to the configuration bucket."); | ||
configStore.storeCmsEnvConfig(newProperties); | ||
|
||
logger.info("Uploading complete."); | ||
} | ||
|
||
@Override | ||
public boolean isRunnable(final UpdateCmsConfigCommand command) { | ||
boolean isRunnable = true; | ||
final Optional<String> cmsVaultToken = configStore.getCmsVaultToken(); | ||
final Optional<String> cmsDatabasePassword = configStore.getCmsDatabasePassword(); | ||
|
||
if (!cmsVaultToken.isPresent()) { | ||
logger.error("CMS Vault token not present for specified environment."); | ||
isRunnable = false; | ||
} | ||
|
||
if (!cmsDatabasePassword.isPresent()) { | ||
logger.error("CMS database password not present for specified environment."); | ||
isRunnable = false; | ||
} | ||
|
||
return isRunnable; | ||
} | ||
|
||
private boolean shouldOverwriteAdminGroup(final String existingAdminGroup, final String newAdminGroup) { | ||
if (newAdminGroup == null && existingAdminGroup == null) { | ||
throw new IllegalStateException("Admin group does not exist in S3 config and was not provided as a " + | ||
"parameter. Please use --admin-group parameter to fix."); | ||
} | ||
|
||
if (newAdminGroup == null) { | ||
logger.warn("Admin group not provided, using existing group."); | ||
return false; | ||
} | ||
|
||
return !StringUtils.equals(newAdminGroup, existingAdminGroup); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.