Revert "chore: Adding google maps api key to instance variables using… (#39808)
… file read since system.getenv did not work for prod"
This reverts commit 59948e169e.
## Description
> [!TIP]
> _Add a TL;DR when the description is longer than 500 words or
extremely technical (helps the content, marketing, and DevRel team)._
>
> _Please also include relevant motivation and context. List any
dependencies that are required for this change. Add links to Notion,
Figma or any other documents that might be relevant to the PR._
Fixes #`Issue Number`
_or_
Fixes `Issue URL`
> [!WARNING]
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._
## Automation
/ok-to-test tags=""
### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results -->
> [!CAUTION]
> If you modify the content in this section, you are likely to disrupt
the CI result for your PR.
<!-- end of auto-generated comment: Cypress test results -->
## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
- **Chores**
- Removed an outdated process that transferred the mapping service
configuration, simplifying backend maintenance.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
parent
c21381ba04
commit
9f63ef5e98
|
|
@ -1,178 +0,0 @@
|
|||
package com.appsmith.server.migrations.db.ce;
|
||||
|
||||
import com.appsmith.server.configurations.CommonConfig;
|
||||
import com.appsmith.server.constants.FieldName;
|
||||
import com.appsmith.server.domains.Config;
|
||||
import io.mongock.api.annotations.ChangeUnit;
|
||||
import io.mongock.api.annotations.Execution;
|
||||
import io.mongock.api.annotations.RollbackExecution;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.minidev.json.JSONObject;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.springframework.data.mongodb.core.query.Criteria.where;
|
||||
|
||||
@Slf4j
|
||||
@ChangeUnit(order = "070", id = "copy-google-maps-env-var-to-instance-config", author = "")
|
||||
public class Migration070CopyGoogleMapEnvVarToInstanceConfig {
|
||||
|
||||
private final MongoTemplate mongoTemplate;
|
||||
private final CommonConfig commonConfig;
|
||||
|
||||
/**
|
||||
* This regex pattern matches environment variable declarations like `VAR_NAME=value` or `VAR_NAME="value"` or just
|
||||
* `VAR_NAME=`. It also defines two named capture groups, `name` and `value`, for the variable's name and value
|
||||
* respectively.
|
||||
*/
|
||||
private static final Pattern ENV_VARIABLE_PATTERN = Pattern.compile("^(?<name>[A-Z\\d_]+)\\s*=\\s*(?<value>.*)$");
|
||||
|
||||
public Migration070CopyGoogleMapEnvVarToInstanceConfig(MongoTemplate mongoTemplate, CommonConfig commonConfig) {
|
||||
this.mongoTemplate = mongoTemplate;
|
||||
this.commonConfig = commonConfig;
|
||||
}
|
||||
|
||||
@RollbackExecution
|
||||
public void rollbackExecution() {
|
||||
// This is a data migration, no rollback required
|
||||
}
|
||||
|
||||
@Execution
|
||||
public void copyGoogleMapsEnvVarToInstanceConfig() throws IOException {
|
||||
log.info("Starting migration to copy Google Maps API key from environment variable to instance config");
|
||||
|
||||
final String envName = "APPSMITH_GOOGLE_MAPS_API_KEY";
|
||||
String googleMapsApiKey = System.getenv(envName);
|
||||
|
||||
// If not available in system env, try to read from env file
|
||||
if (StringUtils.isEmpty(googleMapsApiKey)) {
|
||||
googleMapsApiKey = readGoogleMapsApiKeyFromEnvFile(envName);
|
||||
}
|
||||
|
||||
// If still not available, nothing to migrate
|
||||
if (StringUtils.isEmpty(googleMapsApiKey)) {
|
||||
log.info("Google Maps API key not found in environment or env file. Nothing to migrate.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch the instanceConfig from Config collection
|
||||
Query instanceConfigQuery = new Query();
|
||||
instanceConfigQuery.addCriteria(where(Config.Fields.name).is(FieldName.INSTANCE_CONFIG));
|
||||
Config instanceConfig = mongoTemplate.findOne(instanceConfigQuery, Config.class);
|
||||
|
||||
if (instanceConfig == null) {
|
||||
log.error("Instance config not found. Migration cannot proceed.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the current config
|
||||
JSONObject config = instanceConfig.getConfig();
|
||||
if (config == null) {
|
||||
config = new JSONObject();
|
||||
}
|
||||
|
||||
// Get the instanceVariables object
|
||||
JSONObject instanceVariables = (JSONObject) config.get("instanceVariables");
|
||||
if (instanceVariables == null) {
|
||||
instanceVariables = new JSONObject();
|
||||
}
|
||||
|
||||
// Update the Google Maps API key
|
||||
instanceVariables.put("googleMapsKey", googleMapsApiKey);
|
||||
|
||||
// Update config
|
||||
config.put("instanceVariables", instanceVariables);
|
||||
instanceConfig.setConfig(config);
|
||||
|
||||
// Save the updated instanceConfig
|
||||
mongoTemplate.save(instanceConfig);
|
||||
|
||||
log.info("Successfully migrated Google Maps API key from environment variable to instance config");
|
||||
}
|
||||
|
||||
private String readGoogleMapsApiKeyFromEnvFile(String envName) throws IOException {
|
||||
String envFilePath = commonConfig.getEnvFilePath();
|
||||
if (StringUtils.isEmpty(envFilePath)) {
|
||||
log.warn("Environment file path is empty, cannot read Google Maps API key from env file");
|
||||
return null;
|
||||
}
|
||||
|
||||
Path path = Path.of(envFilePath);
|
||||
if (!Files.exists(path)) {
|
||||
log.warn("Environment file does not exist at path: {}", envFilePath);
|
||||
return null;
|
||||
}
|
||||
|
||||
String content = Files.readString(path);
|
||||
Optional<String> keyLine = content.lines()
|
||||
.filter(line -> {
|
||||
Matcher matcher = ENV_VARIABLE_PATTERN.matcher(line);
|
||||
if (matcher.matches()) {
|
||||
String name = matcher.group("name");
|
||||
return name.equals(envName);
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.findFirst();
|
||||
|
||||
if (keyLine.isPresent()) {
|
||||
String line = keyLine.get();
|
||||
Matcher matcher = ENV_VARIABLE_PATTERN.matcher(line);
|
||||
if (matcher.matches()) {
|
||||
String value = matcher.group("value");
|
||||
// Remove quotes if present
|
||||
if (value.startsWith("\"") && value.endsWith("\"")) {
|
||||
value = value.substring(1, value.length() - 1);
|
||||
} else if (value.startsWith("'") && value.endsWith("'")) {
|
||||
value = value.substring(1, value.length() - 1);
|
||||
}
|
||||
return unescapeFromShell(value);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String unescapeFromShell(String input) {
|
||||
final int len = input.length();
|
||||
final StringBuilder valueBuilder = new StringBuilder();
|
||||
Character inQuote = null;
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
final char c = input.charAt(i);
|
||||
|
||||
if (inQuote != null && inQuote == '\'') {
|
||||
if (c == '\'') {
|
||||
inQuote = null;
|
||||
} else {
|
||||
valueBuilder.append(c);
|
||||
}
|
||||
|
||||
} else if (inQuote != null) {
|
||||
// If `inQuote` is not null here, then it can only be the double-quote character.
|
||||
// We don't do variable interpolation here, since we don't expect it to be present in the env file.
|
||||
if (c == '"') {
|
||||
inQuote = null;
|
||||
} else {
|
||||
valueBuilder.append(c);
|
||||
}
|
||||
|
||||
} else if (c == '\'' || c == '"') {
|
||||
inQuote = c;
|
||||
|
||||
} else {
|
||||
valueBuilder.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
return valueBuilder.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user