Skip to content
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

Catch security exception in get environment call #1852

Merged
merged 2 commits into from
Aug 2, 2022
Merged

Catch security exception in get environment call #1852

merged 2 commits into from
Aug 2, 2022

Conversation

xyang16
Copy link
Contributor

@xyang16 xyang16 commented Aug 1, 2022

Description

Brief description of what this PR is about

This PR is to fix #1848

The customer run into the get environment security issue. This PR put the environment call into a utility function and catch the exception.

  • If this change is a backward incompatible change, why must this change be made?
  • Interesting edge cases to note here

@xyang16 xyang16 self-assigned this Aug 1, 2022
@codecov-commenter
Copy link

codecov-commenter commented Aug 1, 2022

Codecov Report

Merging #1852 (3531c54) into master (bb5073f) will decrease coverage by 2.33%.
The diff coverage is 64.50%.

❗ Current head 3531c54 differs from pull request most recent head e9c2737. Consider uploading reports for the commit e9c2737 to get more accurate results

@@             Coverage Diff              @@
##             master    #1852      +/-   ##
============================================
- Coverage     72.08%   69.74%   -2.34%     
- Complexity     5126     5532     +406     
============================================
  Files           473      526      +53     
  Lines         21970    24449    +2479     
  Branches       2351     2660     +309     
============================================
+ Hits          15838    17053    +1215     
- Misses         4925     6089    +1164     
- Partials       1207     1307     +100     
Impacted Files Coverage Δ
api/src/main/java/ai/djl/modality/cv/Image.java 69.23% <ø> (-4.11%) ⬇️
...rc/main/java/ai/djl/modality/cv/MultiBoxPrior.java 76.00% <ø> (ø)
...rc/main/java/ai/djl/modality/cv/output/Joints.java 71.42% <ø> (ø)
.../main/java/ai/djl/modality/cv/output/Landmark.java 100.00% <ø> (ø)
...main/java/ai/djl/modality/cv/output/Rectangle.java 72.41% <0.00%> (ø)
...i/djl/modality/cv/translator/BigGANTranslator.java 21.42% <0.00%> (-5.24%) ⬇️
...odality/cv/translator/BigGANTranslatorFactory.java 33.33% <0.00%> (+8.33%) ⬆️
...nslator/InstanceSegmentationTranslatorFactory.java 14.28% <0.00%> (-3.90%) ⬇️
.../cv/translator/SemanticSegmentationTranslator.java 0.00% <0.00%> (ø)
.../cv/translator/StyleTransferTranslatorFactory.java 40.00% <ø> (ø)
... and 421 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 17f80f8...e9c2737. Read the comment docs.

Copy link
Contributor

@frankfliu frankfliu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can merge this PR create a follow up PR to improve the API.

try {
return System.getenv(name);
} catch (SecurityException e) {
logger.error("Security manager doesn't allow access to the environment variable");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.error("Security manager doesn't allow access to the environment variable");
logger.warn("Security manager doesn't allow access to the environment variable");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

* the system environment or security manager doesn't allow access to the environment
* variable
*/
public static String getenv(String name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better have an overload function:

public static String getenv(String name, String def) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

* variable
*/
public static String getenv(String name) {
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return getenv(name, null);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

} catch (SecurityException e) {
logger.warn("Security manager doesn't allow access to the environment variable");
}
return Collections.unmodifiableMap(new HashMap<>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Collections.unmodifiableMap(new HashMap<>());
return Collections.emptyMap();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*
* @param name the name of the environment variable
* @param def a default value
* @return the string value of the variable, or {@code null} if the variable is not defined in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return the string value of the variable, or {@code null} if the variable is not defined in
* @return the string value of the variable, or {@code def} if the variable is not defined in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -30,7 +31,9 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import java.util.HashMap;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@xyang16 xyang16 merged commit eb1197b into deepjavalibrary:master Aug 2, 2022
@xyang16 xyang16 deleted the issue_1848 branch August 2, 2022 01:34
patins1 pushed a commit to patins1/djl that referenced this pull request Aug 26, 2022
* Catch security exception in get environment call

* Review changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants