Repository navigation
Secure keystores with filesystem acl, use pkcs12 #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mgaffigan
wants to merge
2
commits into
OpenIntegrationEngine:main
Choose a base branch
from
mgaffigan:fix/keystore-privs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
38 changes: 38 additions & 0 deletions
38
server/src/main/java/com/mirth/connect/server/util/FilePermissionUtil.java
This file contains hidden or 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,38 @@ | ||
| package com.mirth.connect.server.util; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.attribute.PosixFilePermission; | ||
| import java.nio.file.attribute.PosixFilePermissions; | ||
| import java.util.EnumSet; | ||
| import java.util.Set; | ||
|
|
||
| import com.sun.jna.Platform; | ||
|
|
||
| public class FilePermissionUtil { | ||
|
jonbartels marked this conversation as resolved.
Resolved
|
||
|
|
||
| private static final Set<PosixFilePermission> OWNER_ONLY = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE); | ||
|
|
||
| private FilePermissionUtil() {} | ||
|
|
||
| /* | ||
| * Creates a file that only the account the server runs as, and whoever administers the machine, | ||
| * may read or write. | ||
| */ | ||
| public static void createOwnerOnlyFile(File file) throws IOException { | ||
| Path path = file.toPath(); | ||
| File parent = file.getParentFile(); | ||
|
|
||
| if (parent != null) { | ||
| Files.createDirectories(parent.toPath()); | ||
| } | ||
|
|
||
| if (Platform.isWindows()) { | ||
| WindowsFilePermissionUtil.createRestrictedFile(path); | ||
| } else { | ||
| Files.createFile(path, PosixFilePermissions.asFileAttribute(OWNER_ONLY)); | ||
| } | ||
| } | ||
| } | ||
94 changes: 94 additions & 0 deletions
94
server/src/main/java/com/mirth/connect/server/util/WindowsFilePermissionUtil.java
This file contains hidden or 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,94 @@ | ||
| package com.mirth.connect.server.util; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.FileAlreadyExistsException; | ||
| import java.nio.file.Path; | ||
|
|
||
| import com.sun.jna.Native; | ||
| import com.sun.jna.Pointer; | ||
| import com.sun.jna.platform.win32.Kernel32; | ||
| import com.sun.jna.platform.win32.Kernel32Util; | ||
| import com.sun.jna.platform.win32.WinBase; | ||
| import com.sun.jna.platform.win32.WinError; | ||
| import com.sun.jna.platform.win32.WinNT; | ||
| import com.sun.jna.platform.win32.WinNT.HANDLE; | ||
| import com.sun.jna.ptr.IntByReference; | ||
| import com.sun.jna.ptr.PointerByReference; | ||
| import com.sun.jna.win32.StdCallLibrary; | ||
| import com.sun.jna.win32.W32APIOptions; | ||
| import com.sun.security.auth.module.NTSystem; | ||
|
|
||
| /* | ||
| * The Windows half of FilePermissionUtil, kept apart so that nothing here is loaded on a platform | ||
| * that has no Win32 API to call. | ||
| */ | ||
| class WindowsFilePermissionUtil { | ||
|
|
||
| /* | ||
| * Full control to creator, LocalSystem and the local administrators. Matches | ||
| * user profile directories. | ||
| */ | ||
| private static final String SDDL = "D:P(A;;FA;;;{CURRENTUSERSID})(A;;FA;;;SY)(A;;FA;;;BA)"; | ||
|
|
||
| private static final int SDDL_REVISION_1 = 1; | ||
|
|
||
| /* | ||
| * The SDDL conversions are not part of JNA's Advapi32 mapping. | ||
| */ | ||
| private interface Advapi32Sddl extends StdCallLibrary { | ||
| Advapi32Sddl INSTANCE = Native.loadLibrary("Advapi32", Advapi32Sddl.class, W32APIOptions.DEFAULT_OPTIONS); | ||
|
|
||
| boolean ConvertStringSecurityDescriptorToSecurityDescriptor(String sddl, int revision, PointerByReference securityDescriptor, IntByReference size); | ||
| } | ||
|
|
||
| private WindowsFilePermissionUtil() {} | ||
|
|
||
| /* | ||
| * Creates the file with a DACL that only lets the account the server runs as and the machine's | ||
| * administrators near it. The DACL is handed to CreateFile rather than applied afterwards, so | ||
| * the file is never briefly readable by anyone else. | ||
| */ | ||
| static void createRestrictedFile(Path path) throws IOException { | ||
| Pointer securityDescriptor = buildSecurityDescriptor(); | ||
|
|
||
| try { | ||
| createFile(path, securityDescriptor); | ||
| } finally { | ||
| Kernel32.INSTANCE.LocalFree(securityDescriptor); | ||
| } | ||
| } | ||
|
|
||
| private static Pointer buildSecurityDescriptor() throws IOException { | ||
| PointerByReference securityDescriptor = new PointerByReference(); | ||
|
|
||
| String sddl = SDDL.replace("{CURRENTUSERSID}", new NTSystem().getUserSID()); | ||
| if (!Advapi32Sddl.INSTANCE.ConvertStringSecurityDescriptorToSecurityDescriptor(sddl, SDDL_REVISION_1, securityDescriptor, null)) { | ||
| throw lastError("Could not build a security descriptor from \"" + sddl + "\""); | ||
| } | ||
|
|
||
| return securityDescriptor.getValue(); | ||
| } | ||
|
|
||
| private static void createFile(Path path, Pointer securityDescriptor) throws IOException { | ||
| WinBase.SECURITY_ATTRIBUTES securityAttributes = new WinBase.SECURITY_ATTRIBUTES(); | ||
| securityAttributes.lpSecurityDescriptor = securityDescriptor; | ||
| securityAttributes.bInheritHandle = false; | ||
|
|
||
| HANDLE handle = Kernel32.INSTANCE.CreateFile(path.toAbsolutePath().toString(), WinNT.GENERIC_WRITE, 0, securityAttributes, WinNT.CREATE_NEW, WinNT.FILE_ATTRIBUTE_NORMAL, null); | ||
|
|
||
| if (WinBase.INVALID_HANDLE_VALUE.equals(handle)) { | ||
| // reported the same way as Files.createFile, so that callers need not care which is which | ||
| if (Kernel32.INSTANCE.GetLastError() == WinError.ERROR_FILE_EXISTS) { | ||
| throw new FileAlreadyExistsException(path.toString()); | ||
| } | ||
|
|
||
| throw lastError("Could not create " + path); | ||
| } | ||
|
|
||
| Kernel32.INSTANCE.CloseHandle(handle); | ||
| } | ||
|
|
||
| private static IOException lastError(String message) { | ||
| return new IOException(message + ": " + Kernel32Util.formatMessage(Kernel32.INSTANCE.GetLastError())); | ||
| } | ||
| } |
76 changes: 76 additions & 0 deletions
76
server/src/test/java/com/mirth/connect/server/util/FilePermissionUtilTest.java
This file contains hidden or 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,76 @@ | ||
| package com.mirth.connect.server.util; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assume.assumeFalse; | ||
| import static org.junit.Assume.assumeTrue; | ||
|
|
||
| import java.io.File; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.attribute.PosixFilePermissions; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.commons.io.IOUtils; | ||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.junit.rules.TemporaryFolder; | ||
|
|
||
| import com.sun.jna.Platform; | ||
|
|
||
| public class FilePermissionUtilTest { | ||
|
|
||
| @Rule | ||
| public TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
|
||
|
|
||
| @Test | ||
| public void testPosixPermissions() throws Exception { | ||
| assumeFalse("Test only applies on POSIX filesystems", Platform.isWindows()); | ||
| File file = new File(temporaryFolder.getRoot(), "keystore.p12"); | ||
|
|
||
| FilePermissionUtil.createOwnerOnlyFile(file); | ||
|
|
||
| assertEquals("rw-------", PosixFilePermissions.toString(Files.getPosixFilePermissions(file.toPath()))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWindowsPermissions() throws Exception { | ||
| assumeTrue("Test only applies on windows", Platform.isWindows()); | ||
| assumeTrue("Test assumes english locale", System.getProperty("user.language").equals("en")); | ||
| File file = new File(temporaryFolder.getRoot(), "keystore.p12"); | ||
| String path = file.getAbsolutePath(); | ||
|
|
||
| FilePermissionUtil.createOwnerOnlyFile(file); | ||
| var icaclsOutput = system("icacls", path); | ||
| assertContains(icaclsOutput, System.getProperty("user.name") + ":(F)"); | ||
| assertContains(icaclsOutput, "BUILTIN\\Administrators:(F)"); | ||
| assertContains(icaclsOutput, "NT AUTHORITY\\SYSTEM:(F)"); | ||
| assertNotContains(icaclsOutput, "Everyone:"); | ||
| assertNotContains(icaclsOutput, "Users:"); | ||
| assertNotContains(icaclsOutput, "(I)" /* inherited permissions */); | ||
| } | ||
|
|
||
| private void assertContains(String output, String expected) { | ||
| assertTrue("Expected output to contain '" + expected + "' but was '" + output + "'", output.contains(expected)); | ||
| } | ||
|
|
||
| private void assertNotContains(String output, String expected) { | ||
| assertFalse("Expected output to not contain '" + expected + "' but was '" + output + "'", output.contains(expected)); | ||
| } | ||
|
|
||
| private String system(String command, String... arguments) throws Exception { | ||
| List<String> commandLine = new ArrayList<String>(); | ||
| commandLine.add(command); | ||
| commandLine.addAll(Arrays.asList(arguments)); | ||
|
|
||
| Process process = new ProcessBuilder(commandLine).redirectErrorStream(true).start(); | ||
| String output = IOUtils.toString(process.getInputStream(), Charset.defaultCharset()); | ||
|
|
||
| assertEquals(output, 0, process.waitFor()); | ||
| return output; | ||
| } | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A failed first boot leaves an empty keystore that blocks every later boot.
Tthe new
createOwnerOnlyFilecall createskeystore.pfxbefore the encryption key and default certificate are generated and written. Before this PR, the file was only created just before writing.If key or certificate setup throws (for example, a bad encryption setting), the error is only logged and a 0-byte
keystore.pfxstays on disk. On the next start the file exists, loading it fails, and security setup fails on every boot until someone deletes the file by hand.Fix: create the owner-only file right before writing, or delete it when setup fails.