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
Enable securitymanager #10717
Merged
Merged
Enable securitymanager #10717
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
270cb9f
enable securitymanager
rmuir 7b6e470
Merge branch 'master' into put_me_in_coach
rmuir 0865d22
Remove crazy permissions for filestores, ssds, now that
rmuir ffd8db6
Merge branch 'master' into put_me_in_coach
rmuir e2861bd
ensure we only pull system filestores once time
rmuir 573e81d
Ensure paths exist (or more permissions are needed later)
rmuir 5d2153f
remove stray sop
rmuir b2850bf
remove logging statements for another bikeshed
rmuir 500c956
Remove policy config file, its a resource.
rmuir de109bd
Buffer this inputstream out of paranoia. guava copy goes byte-by-byte...
rmuir 2d778df
Merge branch 'master' into put_me_in_coach
rmuir fca05ed
add constant only used once to make it harder to read the code
rmuir File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
133 changes: 133 additions & 0 deletions
133
src/main/java/org/elasticsearch/bootstrap/Security.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,133 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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 org.elasticsearch.bootstrap; | ||
|
||
import org.apache.lucene.util.StringHelper; | ||
import org.elasticsearch.common.logging.Loggers; | ||
import org.elasticsearch.env.Environment; | ||
|
||
import java.io.BufferedOutputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* Initializes securitymanager with necessary permissions. | ||
* <p> | ||
* We use a template file (the one we test with), and add additional | ||
* permissions based on the environment (data paths, etc) | ||
*/ | ||
class Security { | ||
|
||
/** | ||
* Initializes securitymanager for the environment | ||
* Can only happen once! | ||
*/ | ||
static void configure(Environment environment) throws IOException { | ||
// init lucene random seed. it will use /dev/urandom where available. | ||
StringHelper.randomId(); | ||
Path newConfig = processTemplate(environment.configFile().resolve("security.policy"), environment); | ||
System.setProperty("java.security.policy", newConfig.toString()); | ||
System.setSecurityManager(new SecurityManager()); | ||
try { | ||
Files.delete(newConfig); | ||
} catch (Exception e) { | ||
Loggers.getLogger(Security.class).warn("unable to remove temporary file: " + newConfig, e); | ||
} | ||
} | ||
|
||
// package-private for testing | ||
static Path processTemplate(Path template, Environment environment) throws IOException { | ||
Path processed = Files.createTempFile(null, null); | ||
try (OutputStream output = new BufferedOutputStream(Files.newOutputStream(processed))) { | ||
// copy the template as-is. | ||
Files.copy(template, output); | ||
|
||
// add permissions for all configured paths. | ||
Set<Path> paths = new HashSet<>(); | ||
paths.add(environment.homeFile()); | ||
paths.add(environment.configFile()); | ||
paths.add(environment.logsFile()); | ||
paths.add(environment.pluginsFile()); | ||
paths.add(environment.workFile()); | ||
paths.add(environment.workWithClusterFile()); | ||
for (Path path : environment.dataFiles()) { | ||
paths.add(path); | ||
} | ||
for (Path path : environment.dataWithClusterFiles()) { | ||
paths.add(path); | ||
} | ||
output.write(createPermissions(paths)); | ||
} | ||
return processed; | ||
} | ||
|
||
// package private for testing | ||
static byte[] createPermissions(Set<Path> paths) throws IOException { | ||
ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
|
||
// all policy files are UTF-8: | ||
// https://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html | ||
try (Writer writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) { | ||
writer.write(System.lineSeparator()); | ||
writer.write("grant {"); | ||
writer.write(System.lineSeparator()); | ||
for (Path path : paths) { | ||
// data paths actually may not exist yet. | ||
Files.createDirectories(path); | ||
// add each path twice: once for itself, again for files underneath it | ||
addPath(writer, encode(path), "read,readlink,write,delete"); | ||
addRecursivePath(writer, encode(path), "read,readlink,write,delete"); | ||
} | ||
writer.write("};"); | ||
writer.write(System.lineSeparator()); | ||
} | ||
|
||
return stream.toByteArray(); | ||
} | ||
|
||
static void addPath(Writer writer, String path, String permissions) throws IOException { | ||
writer.write("permission java.io.FilePermission \"" + path + "\", \"" + permissions + "\";"); | ||
writer.write(System.lineSeparator()); | ||
} | ||
|
||
static void addRecursivePath(Writer writer, String path, String permissions) throws IOException { | ||
writer.write("permission java.io.FilePermission \"" + path + "${/}-\", \"" + permissions + "\";"); | ||
writer.write(System.lineSeparator()); | ||
} | ||
|
||
// Any backslashes in paths must be escaped, because it is the escape character when parsing. | ||
// See "Note Regarding File Path Specifications on Windows Systems". | ||
// https://docs.oracle.com/javase/7/docs/technotes/guides/security/PolicyFiles.html | ||
static String encode(Path path) { | ||
return encode(path.toString()); | ||
} | ||
|
||
static String encode(String path) { | ||
return path.replace("\\", "\\\\"); | ||
} | ||
} |
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
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.
can we debug log the default? also leaning to have info the "non default" setting, thats what we try to do most times in other components to try and keep the startup logs clean and informative.
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.
This is officially back and forth at this point. I started at info and went to warn based on feedback. Guys get your shit together, im not playing this game.