Skip to content

Commit 8fb8cd8

Browse files
author
Hai-May Chao
committed
8339347: keytool -importpass insists prompting the user even if there is no terminal
Reviewed-by: weijun
1 parent 9e1af8c commit 8fb8cd8

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Diff for: src/java.base/share/classes/sun/security/tools/keytool/Main.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1789,7 +1789,8 @@ private char[] promptForKeyPass(String alias, String orig, char[] origPass) thro
17891789
*/
17901790
private char[] promptForCredential() throws Exception {
17911791
// Handle password supplied via stdin
1792-
if (System.console() == null) {
1792+
Console console = System.console();
1793+
if (console == null || !console.isTerminal()) {
17931794
char[] importPass = Password.readPassword(System.in);
17941795
passwords.add(importPass);
17951796
return importPass;
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2024, 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.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8339347
27+
* @summary Test keytool -importpass with password input from System.in for
28+
* the non-terminal console
29+
* @library /test/lib
30+
* @run main TestImportPass
31+
*/
32+
33+
import jdk.test.lib.SecurityTools;
34+
35+
public class TestImportPass {
36+
public static void main(String[] args) throws Throwable {
37+
SecurityTools.setResponse("pass123");
38+
SecurityTools.keytool("-importpass -keystore ks.p12 -storepass changeit " +
39+
"-storetype pkcs12 -alias newentry")
40+
.shouldNotContain("Enter the password to be stored:")
41+
.shouldHaveExitValue(0);
42+
43+
SecurityTools.keytool("-list -keystore ks.p12 -storepass changeit " +
44+
"-v")
45+
.shouldContain("Alias name: newentry")
46+
.shouldHaveExitValue(0);
47+
}
48+
}

0 commit comments

Comments
 (0)