|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.jfr.api.consumer.security; |
|
27 |
|
28 import java.io.File; |
|
29 import java.io.IOException; |
|
30 import java.io.PrintWriter; |
|
31 import java.nio.file.Files; |
|
32 import java.nio.file.Path; |
|
33 import java.nio.file.Paths; |
|
34 import java.time.Duration; |
|
35 import java.time.Instant; |
|
36 |
|
37 import jdk.jfr.Event; |
|
38 import jdk.jfr.Recording; |
|
39 import jdk.jfr.consumer.EventStream; |
|
40 import jdk.test.lib.process.OutputAnalyzer; |
|
41 import jdk.test.lib.process.ProcessTools; |
|
42 |
|
43 /** |
|
44 * @test |
|
45 * @summary Test that a stream can be opened against a remote repository using |
|
46 * only file permission |
|
47 * @key jfr |
|
48 * @requires vm.hasJFR |
|
49 * @library /test/lib |
|
50 * |
|
51 * @run main/othervm jdk.jfr.api.consumer.security.TestStreamingRemote |
|
52 */ |
|
53 public class TestStreamingRemote { |
|
54 |
|
55 private static final String SUCCESS = "Success!"; |
|
56 |
|
57 public static class TestEvent extends Event { |
|
58 } |
|
59 |
|
60 public static class Test { |
|
61 public static void main(String... args) throws Exception { |
|
62 Path repo = Paths.get(args[0]); |
|
63 System.out.println("Repository: " + repo); |
|
64 try (EventStream es = EventStream.openRepository(repo)) { |
|
65 es.setStartTime(Instant.EPOCH); |
|
66 es.onEvent(e -> { |
|
67 System.out.println(SUCCESS); |
|
68 es.close(); |
|
69 }); |
|
70 es.start(); |
|
71 } |
|
72 } |
|
73 } |
|
74 |
|
75 public static void main(String... args) throws Exception { |
|
76 try (Recording r = new Recording()) { |
|
77 r.setFlushInterval(Duration.ofSeconds(1)); |
|
78 r.start(); |
|
79 String repository = System.getProperty("jdk.jfr.repository"); |
|
80 Path policy = createPolicyFile(repository); |
|
81 TestEvent e = new TestEvent(); |
|
82 e.commit(); |
|
83 String[] c = new String[4]; |
|
84 c[0] = "-Djava.security.manager"; |
|
85 c[1] = "-Djava.security.policy=" + escapeBackslashes(policy.toString()); |
|
86 c[2] = Test.class.getName(); |
|
87 c[3] = repository; |
|
88 OutputAnalyzer oa = ProcessTools.executeTestJvm(c); |
|
89 oa.shouldContain(SUCCESS); |
|
90 } |
|
91 } |
|
92 |
|
93 private static Path createPolicyFile(String repository) throws IOException { |
|
94 Path policy = Paths.get("permission.policy").toAbsolutePath(); |
|
95 try (PrintWriter pw = new PrintWriter(policy.toFile())) { |
|
96 pw.println("grant {"); |
|
97 // All the files and directories the contained in path |
|
98 String dir = escapeBackslashes(repository); |
|
99 String contents = escapeBackslashes(repository + File.separatorChar + "-"); |
|
100 pw.println(" permission java.io.FilePermission \"" + dir + "\", \"read\";"); |
|
101 pw.println(" permission java.io.FilePermission \"" + contents + "\", \"read\";"); |
|
102 pw.println("};"); |
|
103 pw.println(); |
|
104 } |
|
105 System.out.println("Permission file: " + policy); |
|
106 for (String line : Files.readAllLines(policy)) { |
|
107 System.out.println(line); |
|
108 } |
|
109 System.out.println(); |
|
110 return policy; |
|
111 } |
|
112 |
|
113 // Needed for Windows |
|
114 private static String escapeBackslashes(String text) { |
|
115 return text.replace("\\", "\\\\"); |
|
116 } |
|
117 } |