forked from appsmithorg/appsmith
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Env Manager API and super user signup API (appsmithorg#6473)
* Add API for env management and super user * Add missing files * Add API for signing up for super user * Fix types in client code * Add docs for env manager API * Minor refactoring * Remove unused updates to app startup * Better error logging when unable to write file Co-authored-by: Nidhi <[email protected]> * Don't cache the user count (duh!) Co-authored-by: Nidhi <[email protected]>
- Loading branch information
1 parent
0f41886
commit e20d616
Showing
16 changed files
with
372 additions
and
29 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
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
35 changes: 35 additions & 0 deletions
35
...ppsmith-server/src/main/java/com/appsmith/server/controllers/InstanceAdminController.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,35 @@ | ||
package com.appsmith.server.controllers; | ||
|
||
import com.appsmith.server.constants.Url; | ||
import com.appsmith.server.dtos.ResponseDTO; | ||
import com.appsmith.server.solutions.EnvManager; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import reactor.core.publisher.Mono; | ||
|
||
import javax.validation.Valid; | ||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping(Url.INSTANCE_ADMIN_URL) | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class InstanceAdminController { | ||
|
||
private final EnvManager envManager; | ||
|
||
@PutMapping("/env") | ||
public Mono<ResponseDTO<Boolean>> saveEnvChanges( | ||
@Valid @RequestBody Map<String, String> changes | ||
) { | ||
log.debug("Applying env updates {}", changes); | ||
return envManager.applyChanges(changes) | ||
.thenReturn(new ResponseDTO<>(HttpStatus.OK.value(), true, null)); | ||
} | ||
|
||
} |
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
23 changes: 13 additions & 10 deletions
23
app/server/appsmith-server/src/main/java/com/appsmith/server/dtos/UserProfileDTO.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 |
---|---|---|
@@ -1,19 +1,22 @@ | ||
package com.appsmith.server.dtos; | ||
|
||
import com.appsmith.server.domains.Organization; | ||
import com.appsmith.server.domains.User; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Getter | ||
@Setter | ||
@Data | ||
public class UserProfileDTO { | ||
|
||
User user; | ||
String email; | ||
|
||
Organization currentOrganization; | ||
Set<String> organizationIds; | ||
|
||
String username; | ||
|
||
String name; | ||
|
||
String gender; | ||
|
||
boolean isEmptyInstance = false; | ||
|
||
List<ApplicationNameIdDTO> applications; | ||
} |
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
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
109 changes: 109 additions & 0 deletions
109
app/server/appsmith-server/src/main/java/com/appsmith/server/solutions/EnvManager.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,109 @@ | ||
package com.appsmith.server.solutions; | ||
|
||
import com.appsmith.server.acl.AclPermission; | ||
import com.appsmith.server.exceptions.AppsmithError; | ||
import com.appsmith.server.exceptions.AppsmithException; | ||
import com.appsmith.server.helpers.PolicyUtils; | ||
import com.appsmith.server.services.SessionUserService; | ||
import com.appsmith.server.services.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class EnvManager { | ||
|
||
private final SessionUserService sessionUserService; | ||
private final UserService userService; | ||
private final PolicyUtils policyUtils; | ||
|
||
@Value("${appsmith.admin.envfile:/opt/appsmith/docker.env}") | ||
public String envFilePath; | ||
|
||
private static final Pattern ENV_VARIABLE_PATTERN = Pattern.compile( | ||
"^(?<name>[A-Z0-9_]+)\\s*=\\s*\"?(?<value>.*?)\"?$" | ||
); | ||
|
||
private static final Set<String> VARIABLE_BLACKLIST = Set.of( | ||
"APPSMITH_ENCRYPTION_PASSWORD", | ||
"APPSMITH_ENCRYPTION_SALT" | ||
); | ||
|
||
/** | ||
* Updates values of variables in the envContent string, based on the changes map given. This function **only** | ||
* updates values of variables that already defined in envContent. It NEVER adds new env variables to it. This is so | ||
* a malicious request won't insert new dubious env variables. | ||
* @param envContent String content of an env file. | ||
* @param changes A map with variable name to new value. | ||
* @return List of string lines for updated env file content. | ||
*/ | ||
public static List<String> transformEnvContent(String envContent, Map<String, String> changes) { | ||
for (final String variable : VARIABLE_BLACKLIST) { | ||
if (changes.containsKey(variable)) { | ||
throw new AppsmithException(AppsmithError.UNAUTHORIZED_ACCESS); | ||
} | ||
} | ||
|
||
return envContent.lines() | ||
.map(line -> { | ||
final Matcher matcher = ENV_VARIABLE_PATTERN.matcher(line); | ||
if (!matcher.matches()) { | ||
return line; | ||
} | ||
final String name = matcher.group("name"); | ||
if (!changes.containsKey(name)) { | ||
return line; | ||
} | ||
return line.substring(0, matcher.start("value")) | ||
+ changes.get(name) | ||
+ line.substring(matcher.end("value")); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public Mono<Void> applyChanges(Map<String, String> changes) { | ||
return sessionUserService.getCurrentUser() | ||
.flatMap(user -> userService.findByEmail(user.getEmail())) | ||
.filter(user -> policyUtils.isPermissionPresentForUser( | ||
user.getPolicies(), | ||
AclPermission.MANAGE_INSTANCE_ENV.getValue(), | ||
user.getUsername() | ||
)) | ||
.switchIfEmpty(Mono.error(new AppsmithException(AppsmithError.UNAUTHORIZED_ACCESS))) | ||
.flatMap(user -> { | ||
final String originalContent; | ||
try { | ||
originalContent = Files.readString(Path.of(envFilePath)); | ||
} catch (IOException e) { | ||
log.error("Unable to read env file " + envFilePath, e); | ||
return Mono.error(e); | ||
} | ||
|
||
final List<String> changedContent = transformEnvContent(originalContent, changes); | ||
|
||
try { | ||
Files.write(Path.of(envFilePath), changedContent); | ||
} catch (IOException e) { | ||
log.error("Unable to write to env file " + envFilePath, e); | ||
return Mono.error(e); | ||
} | ||
|
||
return Mono.empty(); | ||
}); | ||
} | ||
|
||
} |
Oops, something went wrong.